3131import org .springframework .boot .web .client .RestTemplateBuilder ;
3232import org .springframework .core .ParameterizedTypeReference ;
3333import org .springframework .http .HttpEntity ;
34+ import org .springframework .http .HttpHeaders ;
3435import org .springframework .http .HttpMethod ;
3536import org .springframework .http .HttpStatus ;
3637import org .springframework .http .RequestEntity ;
4344import org .springframework .mock .http .client .MockClientHttpRequest ;
4445import org .springframework .mock .http .client .MockClientHttpResponse ;
4546import org .springframework .test .util .ReflectionTestUtils ;
47+ import org .springframework .util .Base64Utils ;
4648import org .springframework .util .ReflectionUtils ;
4749import org .springframework .util .ReflectionUtils .MethodCallback ;
4850import org .springframework .web .client .ResponseErrorHandler ;
@@ -97,7 +99,8 @@ void useTheSameRequestFactoryClassWithBasicAuth() {
9799 RestTemplateBuilder builder = new RestTemplateBuilder ().requestFactory (() -> customFactory );
98100 TestRestTemplate testRestTemplate = new TestRestTemplate (builder ).withBasicAuth ("test" , "test" );
99101 RestTemplate restTemplate = testRestTemplate .getRestTemplate ();
100- assertThat (restTemplate .getRequestFactory ().getClass ().getName ()).contains ("BasicAuth" );
102+ assertThat (restTemplate .getRequestFactory ().getClass ().getName ())
103+ .contains ("RestTemplateBuilderClientHttpRequestFactoryWrapper" );
101104 Object requestFactory = ReflectionTestUtils .getField (restTemplate .getRequestFactory (), "requestFactory" );
102105 assertThat (requestFactory ).isEqualTo (customFactory ).hasSameClassAs (customFactory );
103106 }
@@ -125,10 +128,9 @@ void getRootUriRootUriNotSet() {
125128 }
126129
127130 @ 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" );
132134 }
133135
134136 @ Test
@@ -201,23 +203,25 @@ private Object mockArgument(Class<?> type) throws Exception {
201203 }
202204
203205 @ Test
204- void withBasicAuthAddsBasicAuthClientFactoryWhenNotAlreadyPresent () {
206+ void withBasicAuthAddsBasicAuthClientFactoryWhenNotAlreadyPresent () throws Exception {
205207 TestRestTemplate original = new TestRestTemplate ();
206208 TestRestTemplate basicAuth = original .withBasicAuth ("user" , "password" );
207209 assertThat (getConverterClasses (original )).containsExactlyElementsOf (getConverterClasses (basicAuth ));
208- assertThat (basicAuth .getRestTemplate ().getRequestFactory ().getClass ().getName ()).contains ("BasicAuth" );
210+ assertThat (basicAuth .getRestTemplate ().getRequestFactory ().getClass ().getName ())
211+ .contains ("RestTemplateBuilderClientHttpRequestFactoryWrapper" );
209212 assertThat (ReflectionTestUtils .getField (basicAuth .getRestTemplate ().getRequestFactory (), "requestFactory" ))
210213 .isInstanceOf (CustomHttpComponentsClientHttpRequestFactory .class );
211214 assertThat (basicAuth .getRestTemplate ().getInterceptors ()).isEmpty ();
212215 assertBasicAuthorizationCredentials (basicAuth , "user" , "password" );
213216 }
214217
215218 @ Test
216- void withBasicAuthReplacesBasicAuthClientFactoryWhenAlreadyPresent () {
219+ void withBasicAuthReplacesBasicAuthClientFactoryWhenAlreadyPresent () throws Exception {
217220 TestRestTemplate original = new TestRestTemplate ("foo" , "bar" ).withBasicAuth ("replace" , "replace" );
218221 TestRestTemplate basicAuth = original .withBasicAuth ("user" , "password" );
219222 assertThat (getConverterClasses (basicAuth )).containsExactlyElementsOf (getConverterClasses (original ));
220- assertThat (basicAuth .getRestTemplate ().getRequestFactory ().getClass ().getName ()).contains ("BasicAuth" );
223+ assertThat (basicAuth .getRestTemplate ().getRequestFactory ().getClass ().getName ())
224+ .contains ("RestTemplateBuilderClientHttpRequestFactoryWrapper" );
221225 assertThat (ReflectionTestUtils .getField (basicAuth .getRestTemplate ().getRequestFactory (), "requestFactory" ))
222226 .isInstanceOf (CustomHttpComponentsClientHttpRequestFactory .class );
223227 assertThat (basicAuth .getRestTemplate ().getInterceptors ()).isEmpty ();
@@ -342,11 +346,12 @@ private void verifyRelativeUriHandling(TestRestTemplateCallback callback) throws
342346 }
343347
344348 private void assertBasicAuthorizationCredentials (TestRestTemplate testRestTemplate , String username ,
345- String password ) {
349+ String password ) throws Exception {
346350 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 ()));
350355
351356 }
352357
@@ -356,16 +361,4 @@ private interface TestRestTemplateCallback {
356361
357362 }
358363
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-
371364}
0 commit comments