17
17
package org .springframework .boot .test .web .client ;
18
18
19
19
import java .io .IOException ;
20
- import java .lang .reflect .Field ;
21
20
import java .net .URI ;
22
21
import java .util .Arrays ;
23
22
import java .util .HashSet ;
24
23
import java .util .Map ;
25
24
import java .util .Set ;
26
- import java .util .function .Supplier ;
27
25
28
26
import org .apache .http .client .HttpClient ;
29
27
import org .apache .http .client .config .CookieSpecs ;
36
34
import org .apache .http .protocol .HttpContext ;
37
35
import org .apache .http .ssl .SSLContextBuilder ;
38
36
39
- import org .springframework .beans .BeanInstantiationException ;
40
- import org .springframework .beans .BeanUtils ;
41
- import org .springframework .boot .web .client .BasicAuthentication ;
42
- import org .springframework .boot .web .client .BasicAuthenticationClientHttpRequestFactory ;
43
- import org .springframework .boot .web .client .ClientHttpRequestFactorySupplier ;
44
37
import org .springframework .boot .web .client .RestTemplateBuilder ;
45
38
import org .springframework .boot .web .client .RootUriTemplateHandler ;
46
39
import org .springframework .core .ParameterizedTypeReference ;
49
42
import org .springframework .http .HttpMethod ;
50
43
import org .springframework .http .RequestEntity ;
51
44
import org .springframework .http .ResponseEntity ;
52
- import org .springframework .http .client .AbstractClientHttpRequestFactoryWrapper ;
53
45
import org .springframework .http .client .ClientHttpRequestFactory ;
54
46
import org .springframework .http .client .ClientHttpResponse ;
55
47
import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
56
- import org .springframework .http .client .InterceptingClientHttpRequestFactory ;
57
48
import org .springframework .util .Assert ;
58
- import org .springframework .util .ReflectionUtils ;
59
49
import org .springframework .web .client .DefaultResponseErrorHandler ;
60
50
import org .springframework .web .client .RequestCallback ;
61
51
import org .springframework .web .client .ResponseExtractor ;
89
79
*/
90
80
public class TestRestTemplate {
91
81
92
- private final RestTemplate restTemplate ;
82
+ private final RestTemplateBuilder builder ;
93
83
94
84
private final HttpClientOption [] httpClientOptions ;
95
85
86
+ private final RestTemplate restTemplate ;
87
+
96
88
/**
97
89
* Create a new {@link TestRestTemplate} instance.
98
90
* @param restTemplateBuilder builder used to configure underlying
@@ -124,64 +116,30 @@ public TestRestTemplate(String username, String password,
124
116
125
117
/**
126
118
* Create a new {@link TestRestTemplate} instance with the specified credentials.
127
- * @param restTemplateBuilder builder used to configure underlying
128
- * {@link RestTemplate}
119
+ * @param builder builder used to configure underlying {@link RestTemplate}
129
120
* @param username the username to use (or {@code null})
130
121
* @param password the password (or {@code null})
131
122
* @param httpClientOptions client options to use if the Apache HTTP Client is used
132
123
* @since 2.0.0
133
124
*/
134
- public TestRestTemplate (RestTemplateBuilder restTemplateBuilder , String username ,
135
- String password , HttpClientOption ... httpClientOptions ) {
136
- this ((restTemplateBuilder != null ) ? restTemplateBuilder .build () : null , username ,
137
- password , httpClientOptions );
138
- }
139
-
140
- private TestRestTemplate (RestTemplate restTemplate , String username , String password ,
125
+ public TestRestTemplate (RestTemplateBuilder builder , String username , String password ,
141
126
HttpClientOption ... httpClientOptions ) {
142
- Assert .notNull (restTemplate , "RestTemplate must not be null" );
127
+ Assert .notNull (builder , "Builder must not be null" );
128
+ this .builder = builder ;
143
129
this .httpClientOptions = httpClientOptions ;
144
- if (getRequestFactoryClass (restTemplate )
145
- .isAssignableFrom (HttpComponentsClientHttpRequestFactory .class )) {
146
- restTemplate .setRequestFactory (
147
- new CustomHttpComponentsClientHttpRequestFactory (httpClientOptions ));
148
- }
149
- addAuthentication (restTemplate , username , password );
150
- restTemplate .setErrorHandler (new NoOpResponseErrorHandler ());
151
- this .restTemplate = restTemplate ;
152
- }
153
-
154
- private Class <? extends ClientHttpRequestFactory > getRequestFactoryClass (
155
- RestTemplate restTemplate ) {
156
- return getRequestFactory (restTemplate ).getClass ();
157
- }
158
-
159
- private ClientHttpRequestFactory getRequestFactory (RestTemplate restTemplate ) {
160
- ClientHttpRequestFactory requestFactory = restTemplate .getRequestFactory ();
161
- while (requestFactory instanceof InterceptingClientHttpRequestFactory
162
- || requestFactory instanceof BasicAuthenticationClientHttpRequestFactory ) {
163
- requestFactory = unwrapRequestFactory (
164
- ((AbstractClientHttpRequestFactoryWrapper ) requestFactory ));
130
+ if (httpClientOptions != null ) {
131
+ ClientHttpRequestFactory requestFactory = builder .buildRequestFactory ();
132
+ if (requestFactory instanceof HttpComponentsClientHttpRequestFactory ) {
133
+ builder = builder .requestFactory (
134
+ () -> new CustomHttpComponentsClientHttpRequestFactory (
135
+ httpClientOptions ));
136
+ }
165
137
}
166
- return requestFactory ;
167
- }
168
-
169
- private ClientHttpRequestFactory unwrapRequestFactory (
170
- AbstractClientHttpRequestFactoryWrapper requestFactory ) {
171
- Field field = ReflectionUtils .findField (
172
- AbstractClientHttpRequestFactoryWrapper .class , "requestFactory" );
173
- ReflectionUtils .makeAccessible (field );
174
- return (ClientHttpRequestFactory ) ReflectionUtils .getField (field , requestFactory );
175
- }
176
-
177
- private void addAuthentication (RestTemplate restTemplate , String username ,
178
- String password ) {
179
- if (username == null || password == null ) {
180
- return ;
138
+ if (username != null || password != null ) {
139
+ builder = builder .basicAuthentication (username , password );
181
140
}
182
- ClientHttpRequestFactory requestFactory = getRequestFactory (restTemplate );
183
- restTemplate .setRequestFactory (new BasicAuthenticationClientHttpRequestFactory (
184
- new BasicAuthentication (username , password ), requestFactory ));
141
+ this .restTemplate = builder .build ();
142
+ this .restTemplate .setErrorHandler (new NoOpResponseErrorHandler ());
185
143
}
186
144
187
145
/**
@@ -1038,25 +996,10 @@ public RestTemplate getRestTemplate() {
1038
996
* @since 1.4.1
1039
997
*/
1040
998
public TestRestTemplate withBasicAuth (String username , String password ) {
1041
- RestTemplate restTemplate = new RestTemplateBuilder ()
1042
- .requestFactory (getRequestFactorySupplier ())
1043
- .messageConverters (getRestTemplate ().getMessageConverters ())
1044
- .interceptors (getRestTemplate ().getInterceptors ())
1045
- .uriTemplateHandler (getRestTemplate ().getUriTemplateHandler ()).build ();
1046
- return new TestRestTemplate (restTemplate , username , password ,
999
+ TestRestTemplate template = new TestRestTemplate (this .builder , username , password ,
1047
1000
this .httpClientOptions );
1048
- }
1049
-
1050
- private Supplier <ClientHttpRequestFactory > getRequestFactorySupplier () {
1051
- return () -> {
1052
- try {
1053
- return BeanUtils
1054
- .instantiateClass (getRequestFactoryClass (getRestTemplate ()));
1055
- }
1056
- catch (BeanInstantiationException ex ) {
1057
- return new ClientHttpRequestFactorySupplier ().get ();
1058
- }
1059
- };
1001
+ template .setUriTemplateHandler (getRestTemplate ().getUriTemplateHandler ());
1002
+ return template ;
1060
1003
}
1061
1004
1062
1005
@ SuppressWarnings ({ "rawtypes" , "unchecked" })
@@ -1078,7 +1021,7 @@ private URI applyRootUriIfNecessary(URI uri) {
1078
1021
}
1079
1022
1080
1023
/**
1081
- * Options used to customize the Apache Http Client if it is used .
1024
+ * Options used to customize the Apache HTTP Client.
1082
1025
*/
1083
1026
public enum HttpClientOption {
1084
1027
0 commit comments