20
20
import java .lang .reflect .Method ;
21
21
import java .lang .reflect .Modifier ;
22
22
import java .net .URI ;
23
- import java .util .List ;
24
23
25
24
import org .apache .http .client .config .RequestConfig ;
26
25
import org .junit .jupiter .api .Test ;
27
26
28
27
import org .springframework .boot .test .web .client .TestRestTemplate .CustomHttpComponentsClientHttpRequestFactory ;
29
28
import org .springframework .boot .test .web .client .TestRestTemplate .HttpClientOption ;
29
+ import org .springframework .boot .web .client .BasicAuthenticationClientHttpRequestFactory ;
30
30
import org .springframework .boot .web .client .RestTemplateBuilder ;
31
31
import org .springframework .core .ParameterizedTypeReference ;
32
32
import org .springframework .http .HttpEntity ;
35
35
import org .springframework .http .RequestEntity ;
36
36
import org .springframework .http .client .ClientHttpRequest ;
37
37
import org .springframework .http .client .ClientHttpRequestFactory ;
38
- import org .springframework .http .client .ClientHttpRequestInterceptor ;
39
38
import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
40
- import org .springframework .http .client .InterceptingClientHttpRequestFactory ;
41
39
import org .springframework .http .client .OkHttp3ClientHttpRequestFactory ;
42
40
import org .springframework .http .client .SimpleClientHttpRequestFactory ;
43
- import org .springframework .http .client .support .BasicAuthenticationInterceptor ;
44
41
import org .springframework .mock .env .MockEnvironment ;
45
42
import org .springframework .mock .http .client .MockClientHttpRequest ;
46
43
import org .springframework .mock .http .client .MockClientHttpResponse ;
@@ -150,7 +147,7 @@ public void getRootUriRootUriNotSet() {
150
147
public void authenticated () {
151
148
assertThat (new TestRestTemplate ("user" , "password" ).getRestTemplate ()
152
149
.getRequestFactory ())
153
- .isInstanceOf (InterceptingClientHttpRequestFactory .class );
150
+ .isInstanceOf (BasicAuthenticationClientHttpRequestFactory .class );
154
151
}
155
152
156
153
@ Test
@@ -235,16 +232,15 @@ public void withBasicAuthAddsBasicAuthInterceptorWhenNotAlreadyPresent() {
235
232
.containsExactlyElementsOf (
236
233
originalTemplate .getRestTemplate ().getMessageConverters ());
237
234
assertThat (basicAuthTemplate .getRestTemplate ().getRequestFactory ())
238
- .isInstanceOf (InterceptingClientHttpRequestFactory .class );
235
+ .isInstanceOf (BasicAuthenticationClientHttpRequestFactory .class );
239
236
assertThat (ReflectionTestUtils .getField (
240
237
basicAuthTemplate .getRestTemplate ().getRequestFactory (),
241
238
"requestFactory" ))
242
239
.isInstanceOf (CustomHttpComponentsClientHttpRequestFactory .class );
243
240
assertThat (basicAuthTemplate .getRestTemplate ().getUriTemplateHandler ())
244
241
.isSameAs (originalTemplate .getRestTemplate ().getUriTemplateHandler ());
245
- assertThat (basicAuthTemplate .getRestTemplate ().getInterceptors ()).hasSize (1 );
246
- assertBasicAuthorizationInterceptorCredentials (basicAuthTemplate , "user" ,
247
- "password" );
242
+ assertThat (basicAuthTemplate .getRestTemplate ().getInterceptors ()).isEmpty ();
243
+ assertBasicAuthorizationCredentials (basicAuthTemplate , "user" , "password" );
248
244
}
249
245
250
246
@ Test
@@ -256,14 +252,14 @@ public void withBasicAuthReplacesBasicAuthInterceptorWhenAlreadyPresent() {
256
252
.containsExactlyElementsOf (
257
253
original .getRestTemplate ().getMessageConverters ());
258
254
assertThat (basicAuth .getRestTemplate ().getRequestFactory ())
259
- .isInstanceOf (InterceptingClientHttpRequestFactory .class );
255
+ .isInstanceOf (BasicAuthenticationClientHttpRequestFactory .class );
260
256
assertThat (ReflectionTestUtils .getField (
261
257
basicAuth .getRestTemplate ().getRequestFactory (), "requestFactory" ))
262
258
.isInstanceOf (CustomHttpComponentsClientHttpRequestFactory .class );
263
259
assertThat (basicAuth .getRestTemplate ().getUriTemplateHandler ())
264
260
.isSameAs (original .getRestTemplate ().getUriTemplateHandler ());
265
- assertThat (basicAuth .getRestTemplate ().getInterceptors ()).hasSize ( 1 );
266
- assertBasicAuthorizationInterceptorCredentials (basicAuth , "user" , "password" );
261
+ assertThat (basicAuth .getRestTemplate ().getInterceptors ()).isEmpty ( );
262
+ assertBasicAuthorizationCredentials (basicAuth , "user" , "password" );
267
263
}
268
264
269
265
@ Test
@@ -394,17 +390,14 @@ private void verifyRelativeUriHandling(TestRestTemplateCallback callback)
394
390
verify (requestFactory ).createRequest (eq (absoluteUri ), any (HttpMethod .class ));
395
391
}
396
392
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 );
393
+ private void assertBasicAuthorizationCredentials (TestRestTemplate testRestTemplate ,
394
+ String username , String password ) {
395
+ ClientHttpRequestFactory requestFactory = testRestTemplate .getRestTemplate ()
396
+ .getRequestFactory ();
397
+ Object authentication = ReflectionTestUtils .getField (requestFactory ,
398
+ "authentication" );
399
+ assertThat (authentication ).hasFieldOrPropertyWithValue ("username" , username );
400
+ assertThat (authentication ).hasFieldOrPropertyWithValue ("password" , password );
408
401
409
402
}
410
403
0 commit comments