Merge branch '1.1.x'

pull/1385/merge
Phillip Webb 10 years ago
commit 114cd444ed

@ -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<HtppClientOption> options = new HashSet<TestRestTemplate.HtppClientOption>(
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();
}
}
}

@ -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));
}
}

Loading…
Cancel
Save