From c2ff7ca80fa0531aa8ee51f5be1208a1b9ff26e7 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 3 Sep 2014 14:11:47 -0700 Subject: [PATCH] Add HtppClientOptions to TestRestTemplate Allow customization of the Apache HTTP Client used in TestRestTemplate via an options enum. Fixes gh-1497 --- .../boot/test/TestRestTemplate.java | 77 ++++++++++++++----- .../boot/test/TestRestTemplateTests.java | 15 ++++ 2 files changed, 72 insertions(+), 20 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/test/TestRestTemplate.java b/spring-boot/src/main/java/org/springframework/boot/test/TestRestTemplate.java index 25ddd4c2d5..339de9fb8d 100644 --- a/spring-boot/src/main/java/org/springframework/boot/test/TestRestTemplate.java +++ b/spring-boot/src/main/java/org/springframework/boot/test/TestRestTemplate.java @@ -18,8 +18,11 @@ package org.springframework.boot.test; import java.io.IOException; import java.net.URI; +import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.RequestConfig; @@ -41,7 +44,7 @@ import org.springframework.web.client.RestTemplate; * Convenient subclass of {@link RestTemplate} that is suitable for integration tests. * They are fault tolerant, and optionally can carry Basic authentication headers. If * Apache Http Client 4.3.2 or better is available (recommended) it will be used as the - * client, and configured to ignore cookies and redirects. + * client, and by default configured to ignore cookies and redirects. * * @author Dave Syer * @author Phillip Webb @@ -50,19 +53,23 @@ public class TestRestTemplate extends RestTemplate { /** * Create a new {@link TestRestTemplate} instance. + * @param httpClientOptions client options to use if the Apache HTTP Client is used */ - public TestRestTemplate() { - this(null, null); + public TestRestTemplate(HtppClientOption... httpClientOptions) { + this(null, null, httpClientOptions); } /** * Create a new {@link TestRestTemplate} instance with the specified credentials. * @param username the username to use (or {@code null}) * @param password the password (or {@code null}) + * @param httpClientOptions client options to use if the Apache HTTP Client is used */ - public TestRestTemplate(String username, String password) { + public TestRestTemplate(String username, String password, + HtppClientOption... httpClientOptions) { if (ClassUtils.isPresent("org.apache.http.client.config.RequestConfig", null)) { - new HttpComponentsCustomizer().customize(this); + setRequestFactory(new CustomHttpComponentsClientHttpRequestFactory( + httpClientOptions)); } addAuthentication(username, password); setErrorHandler(new DefaultResponseErrorHandler() { @@ -84,6 +91,23 @@ public class TestRestTemplate extends RestTemplate { interceptors)); } + /** + * Options used to customize the Apache Http Client if it is used. + */ + public static enum HtppClientOption { + + /** + * Enable cookies. + */ + ENABLE_COOKIES, + + /** + * Enable redirects. + */ + ENABLE_REDIRECTS + + } + private static class BasicAuthorizationInterceptor implements ClientHttpRequestInterceptor { @@ -107,22 +131,35 @@ public class TestRestTemplate extends RestTemplate { } - private static class HttpComponentsCustomizer { - - public void customize(RestTemplate restTemplate) { - restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory() { - @Override - protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) { - HttpClientContext context = HttpClientContext.create(); - Builder builder = RequestConfig.custom() - .setCookieSpec(CookieSpecs.IGNORE_COOKIES) - .setAuthenticationEnabled(false).setRedirectsEnabled(false); - context.setRequestConfig(builder.build()); - return context; - } - }); + protected static class CustomHttpComponentsClientHttpRequestFactory extends + HttpComponentsClientHttpRequestFactory { + + private final String cookieSpec; + + private final boolean enableRedirects; + + public CustomHttpComponentsClientHttpRequestFactory( + HtppClientOption[] httpClientOptions) { + Set options = new HashSet( + Arrays.asList(httpClientOptions)); + this.cookieSpec = (options.contains(HtppClientOption.ENABLE_COOKIES) ? CookieSpecs.STANDARD + : CookieSpecs.IGNORE_COOKIES); + this.enableRedirects = options.contains(HtppClientOption.ENABLE_REDIRECTS); } - } + @Override + protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) { + HttpClientContext context = HttpClientContext.create(); + context.setRequestConfig(getRequestConfig()); + return context; + } + + protected RequestConfig getRequestConfig() { + Builder builder = RequestConfig.custom().setCookieSpec(this.cookieSpec) + .setAuthenticationEnabled(false) + .setRedirectsEnabled(this.enableRedirects); + return builder.build(); + } + } } diff --git a/spring-boot/src/test/java/org/springframework/boot/test/TestRestTemplateTests.java b/spring-boot/src/test/java/org/springframework/boot/test/TestRestTemplateTests.java index 6d125ecdf7..6a675cafb6 100644 --- a/spring-boot/src/test/java/org/springframework/boot/test/TestRestTemplateTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/test/TestRestTemplateTests.java @@ -16,16 +16,22 @@ package org.springframework.boot.test; +import org.apache.http.client.config.RequestConfig; import org.junit.Test; +import org.springframework.boot.test.TestRestTemplate.CustomHttpComponentsClientHttpRequestFactory; +import org.springframework.boot.test.TestRestTemplate.HtppClientOption; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.InterceptingClientHttpRequestFactory; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; /** * Tests for {@link TestRestTemplate}. * * @author Dave Syer + * @author Phillip Webb */ public class TestRestTemplateTests { @@ -40,4 +46,13 @@ public class TestRestTemplateTests { assertTrue(new TestRestTemplate("user", "password").getRequestFactory() instanceof InterceptingClientHttpRequestFactory); } + @Test + public void options() throws Exception { + TestRestTemplate template = new TestRestTemplate( + HtppClientOption.ENABLE_REDIRECTS); + CustomHttpComponentsClientHttpRequestFactory factory = (CustomHttpComponentsClientHttpRequestFactory) template + .getRequestFactory(); + RequestConfig config = factory.getRequestConfig(); + assertThat(config.isRedirectsEnabled(), equalTo(true)); + } }