Skip to content

Commit 556ce14

Browse files
committed
Fix TestRestTemplate.withBasicAuth interceptors
Update `TestRestTemplate` to handle `BasicAuthorizationInterceptor`s correctly. Prior to this commit the `BasicAuthorizationInterceptor` was added directly to the `ClientHttpRequestFactory` rather than to the `RestTemplate`. This meant that it could not easily be removed when `TestRestTemplate.withBasicAuth` was invoked. The `TestRestTemplate` now sets the interceptor on `RestTemplate` directly and relies on the logic in `InterceptingHttpAccessor` to add it to the `ClientHttpRequestFactory`. Fixes spring-projectsgh-7812
1 parent 9ccf473 commit 556ce14

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java

+15-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,7 +48,6 @@
4848
import org.springframework.http.client.ClientHttpRequestInterceptor;
4949
import org.springframework.http.client.ClientHttpResponse;
5050
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
51-
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
5251
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
5352
import org.springframework.util.Assert;
5453
import org.springframework.util.ClassUtils;
@@ -144,11 +143,19 @@ private void addAuthentication(RestTemplate restTemplate, String username,
144143
if (username == null) {
145144
return;
146145
}
147-
List<ClientHttpRequestInterceptor> interceptors = Collections
148-
.<ClientHttpRequestInterceptor>singletonList(
149-
new BasicAuthorizationInterceptor(username, password));
150-
restTemplate.setRequestFactory(new InterceptingClientHttpRequestFactory(
151-
restTemplate.getRequestFactory(), interceptors));
146+
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
147+
if (interceptors == null) {
148+
interceptors = Collections.emptyList();
149+
}
150+
interceptors = new ArrayList<ClientHttpRequestInterceptor>(interceptors);
151+
Iterator<ClientHttpRequestInterceptor> iterator = interceptors.iterator();
152+
while (iterator.hasNext()) {
153+
if (iterator.next() instanceof BasicAuthorizationInterceptor) {
154+
iterator.remove();
155+
}
156+
}
157+
interceptors.add(new BasicAuthorizationInterceptor(username, password));
158+
restTemplate.setInterceptors(interceptors);
152159
}
153160

154161
/**
@@ -985,8 +992,7 @@ public RestTemplate getRestTemplate() {
985992
public TestRestTemplate withBasicAuth(String username, String password) {
986993
RestTemplate restTemplate = new RestTemplate();
987994
restTemplate.setMessageConverters(getRestTemplate().getMessageConverters());
988-
restTemplate.setInterceptors(
989-
removeBasicAuthInterceptorIfPresent(getRestTemplate().getInterceptors()));
995+
restTemplate.setInterceptors(getRestTemplate().getInterceptors());
990996
restTemplate.setRequestFactory(getRestTemplate().getRequestFactory());
991997
restTemplate.setUriTemplateHandler(getRestTemplate().getUriTemplateHandler());
992998
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, username,
@@ -996,19 +1002,6 @@ public TestRestTemplate withBasicAuth(String username, String password) {
9961002
return testRestTemplate;
9971003
}
9981004

999-
private List<ClientHttpRequestInterceptor> removeBasicAuthInterceptorIfPresent(
1000-
List<ClientHttpRequestInterceptor> interceptors) {
1001-
List<ClientHttpRequestInterceptor> updatedInterceptors = new ArrayList<ClientHttpRequestInterceptor>(
1002-
interceptors);
1003-
Iterator<ClientHttpRequestInterceptor> iterator = updatedInterceptors.iterator();
1004-
while (iterator.hasNext()) {
1005-
if (iterator.next() instanceof BasicAuthorizationInterceptor) {
1006-
iterator.remove();
1007-
}
1008-
}
1009-
return interceptors;
1010-
}
1011-
10121005
/**
10131006
* Options used to customize the Apache Http Client if it is used.
10141007
*/

spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -160,16 +160,15 @@ public void withBasicAuthAddsBasicAuthInterceptorWhenNotAlreadyPresent() {
160160
.isInstanceOf(CustomHttpComponentsClientHttpRequestFactory.class);
161161
assertThat(basicAuthTemplate.getRestTemplate().getUriTemplateHandler())
162162
.isSameAs(originalTemplate.getRestTemplate().getUriTemplateHandler());
163-
assertThat(basicAuthTemplate.getRestTemplate().getInterceptors())
164-
.containsExactlyElementsOf(
165-
originalTemplate.getRestTemplate().getInterceptors());
163+
assertThat(basicAuthTemplate.getRestTemplate().getInterceptors()).hasSize(1);
166164
assertBasicAuthorizationInterceptorCredentials(basicAuthTemplate, "user",
167165
"password");
168166
}
169167

170168
@Test
171169
public void withBasicAuthReplacesBasicAuthInterceptorWhenAlreadyPresent() {
172-
TestRestTemplate original = new TestRestTemplate("foo", "bar");
170+
TestRestTemplate original = new TestRestTemplate("foo", "bar")
171+
.withBasicAuth("replace", "repalce");
173172
TestRestTemplate basicAuth = original.withBasicAuth("user", "password");
174173
assertThat(basicAuth.getRestTemplate().getMessageConverters())
175174
.containsExactlyElementsOf(
@@ -181,8 +180,7 @@ public void withBasicAuthReplacesBasicAuthInterceptorWhenAlreadyPresent() {
181180
.isInstanceOf(CustomHttpComponentsClientHttpRequestFactory.class);
182181
assertThat(basicAuth.getRestTemplate().getUriTemplateHandler())
183182
.isSameAs(original.getRestTemplate().getUriTemplateHandler());
184-
assertThat(basicAuth.getRestTemplate().getInterceptors())
185-
.containsExactlyElementsOf(original.getRestTemplate().getInterceptors());
183+
assertThat(basicAuth.getRestTemplate().getInterceptors()).hasSize(1);
186184
assertBasicAuthorizationInterceptorCredentials(basicAuth, "user", "password");
187185
}
188186

0 commit comments

Comments
 (0)