|
|
|
|
@ -18,8 +18,11 @@ package org.springframework.boot.test;
@@ -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;
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|