@ -134,11 +134,17 @@ public class TestRestTemplate {
httpClientOptions ) ;
httpClientOptions ) ;
}
}
private static RestTemplate buildRestTemplate (
RestTemplateBuilder restTemplateBuilder ) {
Assert . notNull ( restTemplateBuilder , "RestTemplateBuilder must not be null" ) ;
return restTemplateBuilder . build ( ) ;
}
private TestRestTemplate ( RestTemplate restTemplate , String username , String password ,
private TestRestTemplate ( RestTemplate restTemplate , String username , String password ,
HttpClientOption . . . httpClientOptions ) {
HttpClientOption . . . httpClientOptions ) {
Assert . notNull ( restTemplate , "RestTemplate must not be null" ) ;
Assert . notNull ( restTemplate , "RestTemplate must not be null" ) ;
this . httpClientOptions = httpClientOptions ;
this . httpClientOptions = httpClientOptions ;
if ( restTemplate . getRequestFactory ( ) . getClass ( ) . getName ( )
if ( getRequestFactoryClass( restTemplate ) . getName ( )
. equals ( "org.springframework.http.client.HttpComponentsClientHttpRequestFactory" ) ) {
. equals ( "org.springframework.http.client.HttpComponentsClientHttpRequestFactory" ) ) {
restTemplate . setRequestFactory (
restTemplate . setRequestFactory (
new CustomHttpComponentsClientHttpRequestFactory ( httpClientOptions ) ) ;
new CustomHttpComponentsClientHttpRequestFactory ( httpClientOptions ) ) ;
@ -148,10 +154,16 @@ public class TestRestTemplate {
this . restTemplate = restTemplate ;
this . restTemplate = restTemplate ;
}
}
private static RestTemplate buildRestTemplate (
private Class < ? extends ClientHttpRequestFactory > getRequestFactoryClass ( RestTemplate restTemplate ) {
RestTemplateBuilder restTemplateBuilder ) {
ClientHttpRequestFactory requestFactory = restTemplate . getRequestFactory ( ) ;
Assert . notNull ( restTemplateBuilder , "RestTemplateBuilder must not be null" ) ;
if ( InterceptingClientHttpRequestFactory . class . isAssignableFrom ( requestFactory . getClass ( ) ) ) {
return restTemplateBuilder . build ( ) ;
Field requestFactoryField = ReflectionUtils
. findField ( RestTemplate . class , "requestFactory" ) ;
ReflectionUtils . makeAccessible ( requestFactoryField ) ;
requestFactory = ( ClientHttpRequestFactory )
ReflectionUtils . getField ( requestFactoryField , restTemplate ) ;
}
return requestFactory . getClass ( ) ;
}
}
private void addAuthentication ( RestTemplate restTemplate , String username ,
private void addAuthentication ( RestTemplate restTemplate , String username ,
@ -1022,11 +1034,11 @@ public class TestRestTemplate {
* @since 1.4 .1
* @since 1.4 .1
* /
* /
public TestRestTemplate withBasicAuth ( String username , String password ) {
public TestRestTemplate withBasicAuth ( String username , String password ) {
RestTemplate restTemplate = new RestTemplate ( ) ;
RestTemplate restTemplate = new RestTemplate Builder ( )
restTemplate . setM essageConverters( getRestTemplate ( ) . getMessageConverters ( ) ) ;
. m essageConverters( getRestTemplate ( ) . getMessageConverters ( ) )
restTemplate . setI nterceptors( getRestTemplate ( ) . getInterceptors ( ) ) ;
. i nterceptors( getRestTemplate ( ) . getInterceptors ( ) )
restTemplate . setRequestFactory ( getRequestFactory ( getRestTemplate ( ) ) ) ;
. uriTemplateHandler ( getRestTemplate ( ) . getUriTemplateHandler ( ) )
restTemplate . setUriTemplateHandler ( getRestTemplate ( ) . getUriTemplateHandler ( ) ) ;
. build ( ) ;
TestRestTemplate testRestTemplate = new TestRestTemplate ( restTemplate , username ,
TestRestTemplate testRestTemplate = new TestRestTemplate ( restTemplate , username ,
password , this . httpClientOptions ) ;
password , this . httpClientOptions ) ;
testRestTemplate . getRestTemplate ( )
testRestTemplate . getRestTemplate ( )
@ -1034,18 +1046,6 @@ public class TestRestTemplate {
return testRestTemplate ;
return testRestTemplate ;
}
}
private ClientHttpRequestFactory getRequestFactory ( RestTemplate restTemplate ) {
ClientHttpRequestFactory requestFactory = restTemplate . getRequestFactory ( ) ;
if ( InterceptingClientHttpRequestFactory . class . isAssignableFrom ( requestFactory . getClass ( ) ) ) {
Field requestFactoryField = ReflectionUtils
. findField ( RestTemplate . class , "requestFactory" ) ;
ReflectionUtils . makeAccessible ( requestFactoryField ) ;
requestFactory = ( ClientHttpRequestFactory )
ReflectionUtils . getField ( requestFactoryField , getRestTemplate ( ) ) ;
}
return requestFactory ;
}
@SuppressWarnings ( { "rawtypes" , "unchecked" } )
@SuppressWarnings ( { "rawtypes" , "unchecked" } )
private RequestEntity < ? > createRequestEntityWithRootAppliedUri (
private RequestEntity < ? > createRequestEntityWithRootAppliedUri (
RequestEntity < ? > requestEntity ) {
RequestEntity < ? > requestEntity ) {