Apply root to URIs directly rather than relying on expansion

Previously, TestRestTemplate applied the root URI to URIs by
converting them to a String and then passing the String to the
RestTemplate delegate. Being a String, meant that the URI passed
through RestTemplate's standard URI template expansion processing
using the configured UriTemplateHandler. While this caused the root
URI to be applied, it also had the unwanted side-effect of
encoding the URI for a second time.

This commit updates TestRestTemplate so that, when configured with a
RootUriTemplateHandler, it applies the root URI directly and then
passes a modified URI to the RestTemplate delegate. Being a URI means
that no template expansion is performed and the possible double
encoding is avoided.

Closes gh-8163
This commit is contained in:
Andy Wilkinson
2017-02-02 13:28:52 +00:00
parent 7e44029224
commit 2f50d515a1
2 changed files with 42 additions and 29 deletions

View File

@@ -38,6 +38,7 @@ import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.mock.http.client.MockClientHttpRequest;
import org.springframework.mock.http.client.MockClientHttpResponse;
import org.springframework.test.util.ReflectionTestUtils;
@@ -47,7 +48,6 @@ import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriTemplateHandler;
import org.springframework.web.util.UriTemplateHandler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@@ -427,18 +427,18 @@ public class TestRestTemplateTests {
ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class);
MockClientHttpRequest request = new MockClientHttpRequest();
request.setResponse(new MockClientHttpResponse(new byte[0], HttpStatus.OK));
URI relativeUri = URI.create("a/b/c.txt");
URI absoluteUri = URI.create("http://localhost:8080/" + relativeUri.toString());
URI absoluteUri = URI
.create("http://localhost:8080/a/b/c.txt?param=%7Bsomething%7D");
given(requestFactory.createRequest(eq(absoluteUri), (HttpMethod) any()))
.willReturn(request);
RestTemplate delegate = new RestTemplate();
TestRestTemplate template = new TestRestTemplate(delegate);
delegate.setRequestFactory(requestFactory);
UriTemplateHandler uriTemplateHandler = mock(UriTemplateHandler.class);
given(uriTemplateHandler.expand(relativeUri.toString(), new Object[0]))
.willReturn(absoluteUri);
LocalHostUriTemplateHandler uriTemplateHandler = new LocalHostUriTemplateHandler(
new MockEnvironment());
template.setUriTemplateHandler(uriTemplateHandler);
callback.doWithTestRestTemplate(template, relativeUri);
callback.doWithTestRestTemplate(template,
URI.create("/a/b/c.txt?param=%7Bsomething%7D"));
verify(requestFactory).createRequest(eq(absoluteUri), (HttpMethod) any());
}