|
21 | 21 | import java.lang.reflect.Modifier;
|
22 | 22 | import java.net.URI;
|
23 | 23 | import java.util.List;
|
| 24 | +import java.util.stream.Collectors; |
24 | 25 |
|
25 | 26 | import org.apache.http.client.config.RequestConfig;
|
26 | 27 | import org.junit.jupiter.api.Test;
|
|
35 | 36 | import org.springframework.http.RequestEntity;
|
36 | 37 | import org.springframework.http.client.ClientHttpRequest;
|
37 | 38 | import org.springframework.http.client.ClientHttpRequestFactory;
|
38 |
| -import org.springframework.http.client.ClientHttpRequestInterceptor; |
39 | 39 | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
40 |
| -import org.springframework.http.client.InterceptingClientHttpRequestFactory; |
41 | 40 | import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
|
42 | 41 | import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
43 |
| -import org.springframework.http.client.support.BasicAuthenticationInterceptor; |
44 | 42 | import org.springframework.mock.env.MockEnvironment;
|
45 | 43 | import org.springframework.mock.http.client.MockClientHttpRequest;
|
46 | 44 | import org.springframework.mock.http.client.MockClientHttpResponse;
|
@@ -102,25 +100,11 @@ public void useTheSameRequestFactoryClassWithBasicAuth() {
|
102 | 100 | TestRestTemplate testRestTemplate = new TestRestTemplate(builder)
|
103 | 101 | .withBasicAuth("test", "test");
|
104 | 102 | RestTemplate restTemplate = testRestTemplate.getRestTemplate();
|
| 103 | + assertThat(restTemplate.getRequestFactory().getClass().getName()) |
| 104 | + .contains("BasicAuth"); |
105 | 105 | Object requestFactory = ReflectionTestUtils
|
106 | 106 | .getField(restTemplate.getRequestFactory(), "requestFactory");
|
107 |
| - assertThat(requestFactory).isNotEqualTo(customFactory) |
108 |
| - .hasSameClassAs(customFactory); |
109 |
| - } |
110 |
| - |
111 |
| - @Test |
112 |
| - public void withBasicAuthWhenRequestFactoryTypeCannotBeInstantiatedShouldFallback() { |
113 |
| - TestClientHttpRequestFactory customFactory = new TestClientHttpRequestFactory( |
114 |
| - "my-request-factory"); |
115 |
| - RestTemplateBuilder builder = new RestTemplateBuilder() |
116 |
| - .requestFactory(() -> customFactory); |
117 |
| - TestRestTemplate testRestTemplate = new TestRestTemplate(builder) |
118 |
| - .withBasicAuth("test", "test"); |
119 |
| - RestTemplate restTemplate = testRestTemplate.getRestTemplate(); |
120 |
| - Object requestFactory = ReflectionTestUtils |
121 |
| - .getField(restTemplate.getRequestFactory(), "requestFactory"); |
122 |
| - assertThat(requestFactory).isNotEqualTo(customFactory) |
123 |
| - .isInstanceOf(CustomHttpComponentsClientHttpRequestFactory.class); |
| 107 | + assertThat(requestFactory).isEqualTo(customFactory).hasSameClassAs(customFactory); |
124 | 108 | }
|
125 | 109 |
|
126 | 110 | @Test
|
@@ -148,9 +132,10 @@ public void getRootUriRootUriNotSet() {
|
148 | 132 |
|
149 | 133 | @Test
|
150 | 134 | public void authenticated() {
|
151 |
| - assertThat(new TestRestTemplate("user", "password").getRestTemplate() |
152 |
| - .getRequestFactory()) |
153 |
| - .isInstanceOf(InterceptingClientHttpRequestFactory.class); |
| 135 | + RestTemplate restTemplate = new TestRestTemplate("user", "password") |
| 136 | + .getRestTemplate(); |
| 137 | + ClientHttpRequestFactory factory = restTemplate.getRequestFactory(); |
| 138 | + assertThat(factory.getClass().getName()).contains("BasicAuthentication"); |
154 | 139 | }
|
155 | 140 |
|
156 | 141 | @Test
|
@@ -227,43 +212,39 @@ private Object mockArgument(Class<?> type) throws Exception {
|
227 | 212 | }
|
228 | 213 |
|
229 | 214 | @Test
|
230 |
| - public void withBasicAuthAddsBasicAuthInterceptorWhenNotAlreadyPresent() { |
231 |
| - TestRestTemplate originalTemplate = new TestRestTemplate(); |
232 |
| - TestRestTemplate basicAuthTemplate = originalTemplate.withBasicAuth("user", |
233 |
| - "password"); |
234 |
| - assertThat(basicAuthTemplate.getRestTemplate().getMessageConverters()) |
235 |
| - .containsExactlyElementsOf( |
236 |
| - originalTemplate.getRestTemplate().getMessageConverters()); |
237 |
| - assertThat(basicAuthTemplate.getRestTemplate().getRequestFactory()) |
238 |
| - .isInstanceOf(InterceptingClientHttpRequestFactory.class); |
| 215 | + public void withBasicAuthAddsBasicAuthClientFactoryWhenNotAlreadyPresent() { |
| 216 | + TestRestTemplate original = new TestRestTemplate(); |
| 217 | + TestRestTemplate basicAuth = original.withBasicAuth("user", "password"); |
| 218 | + assertThat(getConverterClasses(original)) |
| 219 | + .containsExactlyElementsOf(getConverterClasses(basicAuth)); |
| 220 | + assertThat(basicAuth.getRestTemplate().getRequestFactory().getClass().getName()) |
| 221 | + .contains("BasicAuth"); |
239 | 222 | assertThat(ReflectionTestUtils.getField(
|
240 |
| - basicAuthTemplate.getRestTemplate().getRequestFactory(), |
241 |
| - "requestFactory")) |
| 223 | + basicAuth.getRestTemplate().getRequestFactory(), "requestFactory")) |
242 | 224 | .isInstanceOf(CustomHttpComponentsClientHttpRequestFactory.class);
|
243 |
| - assertThat(basicAuthTemplate.getRestTemplate().getUriTemplateHandler()) |
244 |
| - .isSameAs(originalTemplate.getRestTemplate().getUriTemplateHandler()); |
245 |
| - assertThat(basicAuthTemplate.getRestTemplate().getInterceptors()).hasSize(1); |
246 |
| - assertBasicAuthorizationInterceptorCredentials(basicAuthTemplate, "user", |
247 |
| - "password"); |
| 225 | + assertThat(basicAuth.getRestTemplate().getInterceptors()).isEmpty(); |
| 226 | + assertBasicAuthorizationCredentials(basicAuth, "user", "password"); |
248 | 227 | }
|
249 | 228 |
|
250 | 229 | @Test
|
251 |
| - public void withBasicAuthReplacesBasicAuthInterceptorWhenAlreadyPresent() { |
| 230 | + public void withBasicAuthReplacesBasicAuthClientFactoryWhenAlreadyPresent() { |
252 | 231 | TestRestTemplate original = new TestRestTemplate("foo", "bar")
|
253 | 232 | .withBasicAuth("replace", "replace");
|
254 | 233 | TestRestTemplate basicAuth = original.withBasicAuth("user", "password");
|
255 |
| - assertThat(basicAuth.getRestTemplate().getMessageConverters()) |
256 |
| - .containsExactlyElementsOf( |
257 |
| - original.getRestTemplate().getMessageConverters()); |
258 |
| - assertThat(basicAuth.getRestTemplate().getRequestFactory()) |
259 |
| - .isInstanceOf(InterceptingClientHttpRequestFactory.class); |
| 234 | + assertThat(getConverterClasses(basicAuth)) |
| 235 | + .containsExactlyElementsOf(getConverterClasses(original)); |
| 236 | + assertThat(basicAuth.getRestTemplate().getRequestFactory().getClass().getName()) |
| 237 | + .contains("BasicAuth"); |
260 | 238 | assertThat(ReflectionTestUtils.getField(
|
261 | 239 | basicAuth.getRestTemplate().getRequestFactory(), "requestFactory"))
|
262 | 240 | .isInstanceOf(CustomHttpComponentsClientHttpRequestFactory.class);
|
263 |
| - assertThat(basicAuth.getRestTemplate().getUriTemplateHandler()) |
264 |
| - .isSameAs(original.getRestTemplate().getUriTemplateHandler()); |
265 |
| - assertThat(basicAuth.getRestTemplate().getInterceptors()).hasSize(1); |
266 |
| - assertBasicAuthorizationInterceptorCredentials(basicAuth, "user", "password"); |
| 241 | + assertThat(basicAuth.getRestTemplate().getInterceptors()).isEmpty(); |
| 242 | + assertBasicAuthorizationCredentials(basicAuth, "user", "password"); |
| 243 | + } |
| 244 | + |
| 245 | + private List<Class<?>> getConverterClasses(TestRestTemplate testRestTemplate) { |
| 246 | + return testRestTemplate.getRestTemplate().getMessageConverters().stream() |
| 247 | + .map(Object::getClass).collect(Collectors.toList()); |
267 | 248 | }
|
268 | 249 |
|
269 | 250 | @Test
|
@@ -394,17 +375,14 @@ private void verifyRelativeUriHandling(TestRestTemplateCallback callback)
|
394 | 375 | verify(requestFactory).createRequest(eq(absoluteUri), any(HttpMethod.class));
|
395 | 376 | }
|
396 | 377 |
|
397 |
| - private void assertBasicAuthorizationInterceptorCredentials( |
398 |
| - TestRestTemplate testRestTemplate, String username, String password) { |
399 |
| - @SuppressWarnings("unchecked") |
400 |
| - List<ClientHttpRequestInterceptor> requestFactoryInterceptors = (List<ClientHttpRequestInterceptor>) ReflectionTestUtils |
401 |
| - .getField(testRestTemplate.getRestTemplate().getRequestFactory(), |
402 |
| - "interceptors"); |
403 |
| - assertThat(requestFactoryInterceptors).hasSize(1); |
404 |
| - ClientHttpRequestInterceptor interceptor = requestFactoryInterceptors.get(0); |
405 |
| - assertThat(interceptor).isInstanceOf(BasicAuthenticationInterceptor.class); |
406 |
| - assertThat(interceptor).hasFieldOrPropertyWithValue("username", username); |
407 |
| - assertThat(interceptor).hasFieldOrPropertyWithValue("password", password); |
| 378 | + private void assertBasicAuthorizationCredentials(TestRestTemplate testRestTemplate, |
| 379 | + String username, String password) { |
| 380 | + ClientHttpRequestFactory requestFactory = testRestTemplate.getRestTemplate() |
| 381 | + .getRequestFactory(); |
| 382 | + Object authentication = ReflectionTestUtils.getField(requestFactory, |
| 383 | + "authentication"); |
| 384 | + assertThat(authentication).hasFieldOrPropertyWithValue("username", username); |
| 385 | + assertThat(authentication).hasFieldOrPropertyWithValue("password", password); |
408 | 386 |
|
409 | 387 | }
|
410 | 388 |
|
|
0 commit comments