18
18
19
19
import java .io .IOException ;
20
20
import java .net .URI ;
21
+ import java .util .Arrays ;
21
22
import java .util .Collections ;
23
+ import java .util .HashSet ;
22
24
import java .util .List ;
25
+ import java .util .Set ;
23
26
24
27
import org .apache .http .client .config .CookieSpecs ;
25
28
import org .apache .http .client .config .RequestConfig ;
41
44
* Convenient subclass of {@link RestTemplate} that is suitable for integration tests.
42
45
* They are fault tolerant, and optionally can carry Basic authentication headers. If
43
46
* Apache Http Client 4.3.2 or better is available (recommended) it will be used as the
44
- * client, and configured to ignore cookies and redirects.
47
+ * client, and by default configured to ignore cookies and redirects.
45
48
*
46
49
* @author Dave Syer
47
50
* @author Phillip Webb
@@ -50,19 +53,23 @@ public class TestRestTemplate extends RestTemplate {
50
53
51
54
/**
52
55
* Create a new {@link TestRestTemplate} instance.
56
+ * @param httpClientOptions client options to use if the Apache HTTP Client is used
53
57
*/
54
- public TestRestTemplate () {
55
- this (null , null );
58
+ public TestRestTemplate (HtppClientOption ... httpClientOptions ) {
59
+ this (null , null , httpClientOptions );
56
60
}
57
61
58
62
/**
59
63
* Create a new {@link TestRestTemplate} instance with the specified credentials.
60
64
* @param username the username to use (or {@code null})
61
65
* @param password the password (or {@code null})
66
+ * @param httpClientOptions client options to use if the Apache HTTP Client is used
62
67
*/
63
- public TestRestTemplate (String username , String password ) {
68
+ public TestRestTemplate (String username , String password ,
69
+ HtppClientOption ... httpClientOptions ) {
64
70
if (ClassUtils .isPresent ("org.apache.http.client.config.RequestConfig" , null )) {
65
- new HttpComponentsCustomizer ().customize (this );
71
+ setRequestFactory (new CustomHttpComponentsClientHttpRequestFactory (
72
+ httpClientOptions ));
66
73
}
67
74
addAuthentication (username , password );
68
75
setErrorHandler (new DefaultResponseErrorHandler () {
@@ -84,6 +91,23 @@ private void addAuthentication(String username, String password) {
84
91
interceptors ));
85
92
}
86
93
94
+ /**
95
+ * Options used to customize the Apache Http Client if it is used.
96
+ */
97
+ public static enum HtppClientOption {
98
+
99
+ /**
100
+ * Enable cookies.
101
+ */
102
+ ENABLE_COOKIES ,
103
+
104
+ /**
105
+ * Enable redirects.
106
+ */
107
+ ENABLE_REDIRECTS
108
+
109
+ }
110
+
87
111
private static class BasicAuthorizationInterceptor implements
88
112
ClientHttpRequestInterceptor {
89
113
@@ -107,22 +131,35 @@ public ClientHttpResponse intercept(HttpRequest request, byte[] body,
107
131
108
132
}
109
133
110
- private static class HttpComponentsCustomizer {
111
-
112
- public void customize ( RestTemplate restTemplate ) {
113
- restTemplate . setRequestFactory ( new HttpComponentsClientHttpRequestFactory () {
114
- @ Override
115
- protected HttpContext createHttpContext ( HttpMethod httpMethod , URI uri ) {
116
- HttpClientContext context = HttpClientContext . create ();
117
- Builder builder = RequestConfig . custom ()
118
- . setCookieSpec ( CookieSpecs . IGNORE_COOKIES )
119
- . setAuthenticationEnabled ( false ). setRedirectsEnabled ( false );
120
- context . setRequestConfig ( builder . build ( ));
121
- return context ;
122
- }
123
- } );
134
+ protected static class CustomHttpComponentsClientHttpRequestFactory extends
135
+ HttpComponentsClientHttpRequestFactory {
136
+
137
+ private final String cookieSpec ;
138
+
139
+ private final boolean enableRedirects ;
140
+
141
+ public CustomHttpComponentsClientHttpRequestFactory (
142
+ HtppClientOption [] httpClientOptions ) {
143
+ Set < HtppClientOption > options = new HashSet < TestRestTemplate . HtppClientOption >(
144
+ Arrays . asList ( httpClientOptions ));
145
+ this . cookieSpec = ( options . contains ( HtppClientOption . ENABLE_COOKIES ) ? CookieSpecs . STANDARD
146
+ : CookieSpecs . IGNORE_COOKIES );
147
+ this . enableRedirects = options . contains ( HtppClientOption . ENABLE_REDIRECTS );
124
148
}
125
149
126
- }
150
+ @ Override
151
+ protected HttpContext createHttpContext (HttpMethod httpMethod , URI uri ) {
152
+ HttpClientContext context = HttpClientContext .create ();
153
+ context .setRequestConfig (getRequestConfig ());
154
+ return context ;
155
+ }
156
+
157
+ protected RequestConfig getRequestConfig () {
158
+ Builder builder = RequestConfig .custom ().setCookieSpec (this .cookieSpec )
159
+ .setAuthenticationEnabled (false )
160
+ .setRedirectsEnabled (this .enableRedirects );
161
+ return builder .build ();
162
+ }
127
163
164
+ }
128
165
}
0 commit comments