Polish "RestTemplateCustomizers should be applied at the end"

Closes gh-13358
pull/13214/merge
Stephane Nicoll 7 years ago
parent ce3420748f
commit c612ed2695

@ -532,7 +532,6 @@ public class RestTemplateBuilder {
restTemplate.getInterceptors().add(this.basicAuthorization); restTemplate.getInterceptors().add(this.basicAuthorization);
} }
restTemplate.getInterceptors().addAll(this.interceptors); restTemplate.getInterceptors().addAll(this.interceptors);
if (!CollectionUtils.isEmpty(this.restTemplateCustomizers)) { if (!CollectionUtils.isEmpty(this.restTemplateCustomizers)) {
for (RestTemplateCustomizer customizer : this.restTemplateCustomizers) { for (RestTemplateCustomizer customizer : this.restTemplateCustomizers) {
customizer.customize(restTemplate); customizer.customize(restTemplate);

@ -389,6 +389,32 @@ public class RestTemplateBuilderTests {
verify(customizer2).customize(template); 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 @Test
public void buildShouldReturnRestTemplate() { public void buildShouldReturnRestTemplate() {
RestTemplate template = this.builder.build(); RestTemplate template = this.builder.build();
@ -498,38 +524,6 @@ public class RestTemplateBuilderTests {
.isInstanceOf(BufferingClientHttpRequestFactory.class); .isInstanceOf(BufferingClientHttpRequestFactory.class);
} }
@Test
public void customizerShouldBeAppliedInTheEnd() {
ClientHttpRequestInterceptor interceptor = this.interceptor;
HttpMessageConverter<Object> messageConverter = this.messageConverter;
ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class);
this.builder.interceptors(interceptor).messageConverters(messageConverter)
.rootUri("http://localhost:8080").errorHandler(errorHandler)
.basicAuthorization("spring", "boot")
.requestFactory(HttpComponentsClientHttpRequestFactory.class)
.customizers((restTemplate) -> {
ClientHttpRequestFactory requestFactory = restTemplate
.getRequestFactory();
assertThat(restTemplate.getInterceptors()).hasSize(2)
.contains(interceptor).anyMatch(
(ic) -> ic instanceof BasicAuthorizationInterceptor);
assertThat(restTemplate.getMessageConverters())
.contains(messageConverter);
assertThat(restTemplate.getUriTemplateHandler())
.isInstanceOf(RootUriTemplateHandler.class);
assertThat(restTemplate.getErrorHandler()).isEqualTo(errorHandler);
assertThat(requestFactory)
.isInstanceOf(InterceptingClientHttpRequestFactory.class);
assertThat(ReflectionTestUtils.getField(requestFactory,
"requestFactory")).isInstanceOf(
HttpComponentsClientHttpRequestFactory.class);
}).build();
}
public static class RestTemplateSubclass extends RestTemplate { public static class RestTemplateSubclass extends RestTemplate {
} }

Loading…
Cancel
Save