Merge pull request #13358 from nosan:rest-template-customizers

* pr/13358:
  Polish "RestTemplateCustomizers should be applied at the end"
  RestTemplateCustomizers should be applied at the end
pull/13214/merge
Stephane Nicoll 7 years ago
commit 48c078f0d0

@ -531,12 +531,12 @@ public class RestTemplateBuilder {
if (this.basicAuthorization != null) {
restTemplate.getInterceptors().add(this.basicAuthorization);
}
restTemplate.getInterceptors().addAll(this.interceptors);
if (!CollectionUtils.isEmpty(this.restTemplateCustomizers)) {
for (RestTemplateCustomizer customizer : this.restTemplateCustomizers) {
customizer.customize(restTemplate);
}
}
restTemplate.getInterceptors().addAll(this.interceptors);
return restTemplate;
}

@ -32,6 +32,7 @@ import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
@ -388,6 +389,32 @@ public class RestTemplateBuilderTests {
verify(customizer2).customize(template);
}
@Test
public void customizerShouldBeAppliedInTheEnd() {
ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class);
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
this.builder.interceptors(this.interceptor)
.messageConverters(this.messageConverter).rootUri("http://localhost:8080")
.errorHandler(errorHandler).basicAuthorization("spring", "boot")
.requestFactory(() -> requestFactory).customizers((restTemplate) -> {
assertThat(restTemplate.getInterceptors()).hasSize(2)
.contains(this.interceptor).anyMatch(
(ic) -> ic instanceof BasicAuthorizationInterceptor);
assertThat(restTemplate.getMessageConverters())
.contains(this.messageConverter);
assertThat(restTemplate.getUriTemplateHandler())
.isInstanceOf(RootUriTemplateHandler.class);
assertThat(restTemplate.getErrorHandler()).isEqualTo(errorHandler);
ClientHttpRequestFactory actualRequestFactory = restTemplate
.getRequestFactory();
assertThat(actualRequestFactory)
.isInstanceOf(InterceptingClientHttpRequestFactory.class);
assertThat(ReflectionTestUtils.getField(actualRequestFactory,
"requestFactory")).isSameAs(requestFactory);
}).build();
}
@Test
public void buildShouldReturnRestTemplate() {
RestTemplate template = this.builder.build();

Loading…
Cancel
Save