Skip to content

Commit c2ff7ca

Browse files
committed
Add HtppClientOptions to TestRestTemplate
Allow customization of the Apache HTTP Client used in TestRestTemplate via an options enum. Fixes gh-1497
1 parent bedb44a commit c2ff7ca

File tree

2 files changed

+72
-20
lines changed

2 files changed

+72
-20
lines changed

spring-boot/src/main/java/org/springframework/boot/test/TestRestTemplate.java

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818

1919
import java.io.IOException;
2020
import java.net.URI;
21+
import java.util.Arrays;
2122
import java.util.Collections;
23+
import java.util.HashSet;
2224
import java.util.List;
25+
import java.util.Set;
2326

2427
import org.apache.http.client.config.CookieSpecs;
2528
import org.apache.http.client.config.RequestConfig;
@@ -41,7 +44,7 @@
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
}

spring-boot/src/test/java/org/springframework/boot/test/TestRestTemplateTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@
1616

1717
package org.springframework.boot.test;
1818

19+
import org.apache.http.client.config.RequestConfig;
1920
import org.junit.Test;
21+
import org.springframework.boot.test.TestRestTemplate.CustomHttpComponentsClientHttpRequestFactory;
22+
import org.springframework.boot.test.TestRestTemplate.HtppClientOption;
2023
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
2124
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
2225

26+
import static org.hamcrest.Matchers.equalTo;
27+
import static org.junit.Assert.assertThat;
2328
import static org.junit.Assert.assertTrue;
2429

2530
/**
2631
* Tests for {@link TestRestTemplate}.
2732
*
2833
* @author Dave Syer
34+
* @author Phillip Webb
2935
*/
3036
public class TestRestTemplateTests {
3137

@@ -40,4 +46,13 @@ public void authenticated() {
4046
assertTrue(new TestRestTemplate("user", "password").getRequestFactory() instanceof InterceptingClientHttpRequestFactory);
4147
}
4248

49+
@Test
50+
public void options() throws Exception {
51+
TestRestTemplate template = new TestRestTemplate(
52+
HtppClientOption.ENABLE_REDIRECTS);
53+
CustomHttpComponentsClientHttpRequestFactory factory = (CustomHttpComponentsClientHttpRequestFactory) template
54+
.getRequestFactory();
55+
RequestConfig config = factory.getRequestConfig();
56+
assertThat(config.isRedirectsEnabled(), equalTo(true));
57+
}
4358
}

0 commit comments

Comments
 (0)