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

Loading…
Cancel
Save