1818
1919import java .io .IOException ;
2020import java .net .URI ;
21+ import java .util .Arrays ;
2122import java .util .Collections ;
23+ import java .util .HashSet ;
2224import java .util .List ;
25+ import java .util .Set ;
2326
2427import org .apache .http .client .config .CookieSpecs ;
2528import org .apache .http .client .config .RequestConfig ;
4144 * Convenient subclass of {@link RestTemplate} that is suitable for integration tests.
4245 * They are fault tolerant, and optionally can carry Basic authentication headers. If
4346 * 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.
4548 *
4649 * @author Dave Syer
4750 * @author Phillip Webb
@@ -50,19 +53,23 @@ public class TestRestTemplate extends RestTemplate {
5053
5154 /**
5255 * Create a new {@link TestRestTemplate} instance.
56+ * @param httpClientOptions client options to use if the Apache HTTP Client is used
5357 */
54- public TestRestTemplate () {
55- this (null , null );
58+ public TestRestTemplate (HtppClientOption ... httpClientOptions ) {
59+ this (null , null , httpClientOptions );
5660 }
5761
5862 /**
5963 * Create a new {@link TestRestTemplate} instance with the specified credentials.
6064 * @param username the username to use (or {@code null})
6165 * @param password the password (or {@code null})
66+ * @param httpClientOptions client options to use if the Apache HTTP Client is used
6267 */
63- public TestRestTemplate (String username , String password ) {
68+ public TestRestTemplate (String username , String password ,
69+ HtppClientOption ... httpClientOptions ) {
6470 if (ClassUtils .isPresent ("org.apache.http.client.config.RequestConfig" , null )) {
65- new HttpComponentsCustomizer ().customize (this );
71+ setRequestFactory (new CustomHttpComponentsClientHttpRequestFactory (
72+ httpClientOptions ));
6673 }
6774 addAuthentication (username , password );
6875 setErrorHandler (new DefaultResponseErrorHandler () {
@@ -84,6 +91,23 @@ private void addAuthentication(String username, String password) {
8491 interceptors ));
8592 }
8693
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+
87111 private static class BasicAuthorizationInterceptor implements
88112 ClientHttpRequestInterceptor {
89113
@@ -107,22 +131,35 @@ public ClientHttpResponse intercept(HttpRequest request, byte[] body,
107131
108132 }
109133
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 );
124148 }
125149
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+ }
127163
164+ }
128165}
0 commit comments