Fix TestRestTemplate request factory management

This commit fixes two issues in `TestRestTemplate`:

* it improves the detection of the underlying request factory, using
reflection to look inside the intercepting request factory if
interceptors were configured

* it avoids reusing the same request factory when creating a new
`TestRestTemplate` with `withBasicAuth`. Sharing the same instance would
result in sharing authentication state (HTTP cookies). Since the
original request factory can't be detected consistently, a new one is
selected automatically

See gh-8697
pull/12098/head
Brian Clozel 7 years ago
parent 51de220b55
commit db7268b841

@ -134,11 +134,17 @@ public class TestRestTemplate {
httpClientOptions); httpClientOptions);
} }
private static RestTemplate buildRestTemplate(
RestTemplateBuilder restTemplateBuilder) {
Assert.notNull(restTemplateBuilder, "RestTemplateBuilder must not be null");
return restTemplateBuilder.build();
}
private TestRestTemplate(RestTemplate restTemplate, String username, String password, private TestRestTemplate(RestTemplate restTemplate, String username, String password,
HttpClientOption... httpClientOptions) { HttpClientOption... httpClientOptions) {
Assert.notNull(restTemplate, "RestTemplate must not be null"); Assert.notNull(restTemplate, "RestTemplate must not be null");
this.httpClientOptions = httpClientOptions; this.httpClientOptions = httpClientOptions;
if (restTemplate.getRequestFactory().getClass().getName() if (getRequestFactoryClass(restTemplate).getName()
.equals("org.springframework.http.client.HttpComponentsClientHttpRequestFactory")) { .equals("org.springframework.http.client.HttpComponentsClientHttpRequestFactory")) {
restTemplate.setRequestFactory( restTemplate.setRequestFactory(
new CustomHttpComponentsClientHttpRequestFactory(httpClientOptions)); new CustomHttpComponentsClientHttpRequestFactory(httpClientOptions));
@ -148,10 +154,16 @@ public class TestRestTemplate {
this.restTemplate = restTemplate; this.restTemplate = restTemplate;
} }
private static RestTemplate buildRestTemplate( private Class<? extends ClientHttpRequestFactory> getRequestFactoryClass(RestTemplate restTemplate) {
RestTemplateBuilder restTemplateBuilder) { ClientHttpRequestFactory requestFactory = restTemplate.getRequestFactory();
Assert.notNull(restTemplateBuilder, "RestTemplateBuilder must not be null"); if (InterceptingClientHttpRequestFactory.class.isAssignableFrom(requestFactory.getClass())) {
return restTemplateBuilder.build(); Field requestFactoryField = ReflectionUtils
.findField(RestTemplate.class, "requestFactory");
ReflectionUtils.makeAccessible(requestFactoryField);
requestFactory = (ClientHttpRequestFactory)
ReflectionUtils.getField(requestFactoryField, restTemplate);
}
return requestFactory.getClass();
} }
private void addAuthentication(RestTemplate restTemplate, String username, private void addAuthentication(RestTemplate restTemplate, String username,
@ -1022,11 +1034,11 @@ public class TestRestTemplate {
* @since 1.4.1 * @since 1.4.1
*/ */
public TestRestTemplate withBasicAuth(String username, String password) { public TestRestTemplate withBasicAuth(String username, String password) {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplateBuilder()
restTemplate.setMessageConverters(getRestTemplate().getMessageConverters()); .messageConverters(getRestTemplate().getMessageConverters())
restTemplate.setInterceptors(getRestTemplate().getInterceptors()); .interceptors(getRestTemplate().getInterceptors())
restTemplate.setRequestFactory(getRequestFactory(getRestTemplate())); .uriTemplateHandler(getRestTemplate().getUriTemplateHandler())
restTemplate.setUriTemplateHandler(getRestTemplate().getUriTemplateHandler()); .build();
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, username, TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, username,
password, this.httpClientOptions); password, this.httpClientOptions);
testRestTemplate.getRestTemplate() testRestTemplate.getRestTemplate()
@ -1034,18 +1046,6 @@ public class TestRestTemplate {
return testRestTemplate; return testRestTemplate;
} }
private ClientHttpRequestFactory getRequestFactory(RestTemplate restTemplate) {
ClientHttpRequestFactory requestFactory = restTemplate.getRequestFactory();
if (InterceptingClientHttpRequestFactory.class.isAssignableFrom(requestFactory.getClass())) {
Field requestFactoryField = ReflectionUtils
.findField(RestTemplate.class, "requestFactory");
ReflectionUtils.makeAccessible(requestFactoryField);
requestFactory = (ClientHttpRequestFactory)
ReflectionUtils.getField(requestFactoryField, getRestTemplate());
}
return requestFactory;
}
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "rawtypes", "unchecked" })
private RequestEntity<?> createRequestEntityWithRootAppliedUri( private RequestEntity<?> createRequestEntityWithRootAppliedUri(
RequestEntity<?> requestEntity) { RequestEntity<?> requestEntity) {

Loading…
Cancel
Save