31
31
import org .springframework .boot .web .client .RestTemplateBuilder ;
32
32
import org .springframework .core .ParameterizedTypeReference ;
33
33
import org .springframework .http .HttpEntity ;
34
+ import org .springframework .http .HttpHeaders ;
34
35
import org .springframework .http .HttpMethod ;
35
36
import org .springframework .http .HttpStatus ;
36
37
import org .springframework .http .RequestEntity ;
43
44
import org .springframework .mock .http .client .MockClientHttpRequest ;
44
45
import org .springframework .mock .http .client .MockClientHttpResponse ;
45
46
import org .springframework .test .util .ReflectionTestUtils ;
47
+ import org .springframework .util .Base64Utils ;
46
48
import org .springframework .util .ReflectionUtils ;
47
49
import org .springframework .util .ReflectionUtils .MethodCallback ;
48
50
import org .springframework .web .client .ResponseErrorHandler ;
@@ -97,7 +99,8 @@ void useTheSameRequestFactoryClassWithBasicAuth() {
97
99
RestTemplateBuilder builder = new RestTemplateBuilder ().requestFactory (() -> customFactory );
98
100
TestRestTemplate testRestTemplate = new TestRestTemplate (builder ).withBasicAuth ("test" , "test" );
99
101
RestTemplate restTemplate = testRestTemplate .getRestTemplate ();
100
- assertThat (restTemplate .getRequestFactory ().getClass ().getName ()).contains ("BasicAuth" );
102
+ assertThat (restTemplate .getRequestFactory ().getClass ().getName ())
103
+ .contains ("HttpHeadersCustomizingClientHttpRequestFactory" );
101
104
Object requestFactory = ReflectionTestUtils .getField (restTemplate .getRequestFactory (), "requestFactory" );
102
105
assertThat (requestFactory ).isEqualTo (customFactory ).hasSameClassAs (customFactory );
103
106
}
@@ -125,10 +128,9 @@ void getRootUriRootUriNotSet() {
125
128
}
126
129
127
130
@ Test
128
- void authenticated () {
129
- RestTemplate restTemplate = new TestRestTemplate ("user" , "password" ).getRestTemplate ();
130
- ClientHttpRequestFactory factory = restTemplate .getRequestFactory ();
131
- assertThat (factory .getClass ().getName ()).contains ("BasicAuthentication" );
131
+ void authenticated () throws Exception {
132
+ TestRestTemplate restTemplate = new TestRestTemplate ("user" , "password" );
133
+ assertBasicAuthorizationCredentials (restTemplate , "user" , "password" );
132
134
}
133
135
134
136
@ Test
@@ -201,23 +203,25 @@ private Object mockArgument(Class<?> type) throws Exception {
201
203
}
202
204
203
205
@ Test
204
- void withBasicAuthAddsBasicAuthClientFactoryWhenNotAlreadyPresent () {
206
+ void withBasicAuthAddsBasicAuthClientFactoryWhenNotAlreadyPresent () throws Exception {
205
207
TestRestTemplate original = new TestRestTemplate ();
206
208
TestRestTemplate basicAuth = original .withBasicAuth ("user" , "password" );
207
209
assertThat (getConverterClasses (original )).containsExactlyElementsOf (getConverterClasses (basicAuth ));
208
- assertThat (basicAuth .getRestTemplate ().getRequestFactory ().getClass ().getName ()).contains ("BasicAuth" );
210
+ assertThat (basicAuth .getRestTemplate ().getRequestFactory ().getClass ().getName ())
211
+ .contains ("HttpHeadersCustomizingClientHttpRequestFactory" );
209
212
assertThat (ReflectionTestUtils .getField (basicAuth .getRestTemplate ().getRequestFactory (), "requestFactory" ))
210
213
.isInstanceOf (CustomHttpComponentsClientHttpRequestFactory .class );
211
214
assertThat (basicAuth .getRestTemplate ().getInterceptors ()).isEmpty ();
212
215
assertBasicAuthorizationCredentials (basicAuth , "user" , "password" );
213
216
}
214
217
215
218
@ Test
216
- void withBasicAuthReplacesBasicAuthClientFactoryWhenAlreadyPresent () {
219
+ void withBasicAuthReplacesBasicAuthClientFactoryWhenAlreadyPresent () throws Exception {
217
220
TestRestTemplate original = new TestRestTemplate ("foo" , "bar" ).withBasicAuth ("replace" , "replace" );
218
221
TestRestTemplate basicAuth = original .withBasicAuth ("user" , "password" );
219
222
assertThat (getConverterClasses (basicAuth )).containsExactlyElementsOf (getConverterClasses (original ));
220
- assertThat (basicAuth .getRestTemplate ().getRequestFactory ().getClass ().getName ()).contains ("BasicAuth" );
223
+ assertThat (basicAuth .getRestTemplate ().getRequestFactory ().getClass ().getName ())
224
+ .contains ("HttpHeadersCustomizingClientHttpRequestFactory" );
221
225
assertThat (ReflectionTestUtils .getField (basicAuth .getRestTemplate ().getRequestFactory (), "requestFactory" ))
222
226
.isInstanceOf (CustomHttpComponentsClientHttpRequestFactory .class );
223
227
assertThat (basicAuth .getRestTemplate ().getInterceptors ()).isEmpty ();
@@ -342,11 +346,12 @@ private void verifyRelativeUriHandling(TestRestTemplateCallback callback) throws
342
346
}
343
347
344
348
private void assertBasicAuthorizationCredentials (TestRestTemplate testRestTemplate , String username ,
345
- String password ) {
349
+ String password ) throws Exception {
346
350
ClientHttpRequestFactory requestFactory = testRestTemplate .getRestTemplate ().getRequestFactory ();
347
- Object authentication = ReflectionTestUtils .getField (requestFactory , "authentication" );
348
- assertThat (authentication ).hasFieldOrPropertyWithValue ("username" , username );
349
- assertThat (authentication ).hasFieldOrPropertyWithValue ("password" , password );
351
+ ClientHttpRequest request = requestFactory .createRequest (URI .create ("http://localhost" ), HttpMethod .POST );
352
+ assertThat (request .getHeaders ()).containsKeys (HttpHeaders .AUTHORIZATION );
353
+ assertThat (request .getHeaders ().get (HttpHeaders .AUTHORIZATION )).containsExactly (
354
+ "Basic " + Base64Utils .encodeToString (String .format ("%s:%s" , username , password ).getBytes ()));
350
355
351
356
}
352
357
@@ -356,16 +361,4 @@ private interface TestRestTemplateCallback {
356
361
357
362
}
358
363
359
- static class TestClientHttpRequestFactory implements ClientHttpRequestFactory {
360
-
361
- TestClientHttpRequestFactory (String value ) {
362
- }
363
-
364
- @ Override
365
- public ClientHttpRequest createRequest (URI uri , HttpMethod httpMethod ) throws IOException {
366
- return null ;
367
- }
368
-
369
- }
370
-
371
364
}
0 commit comments