Add RestTemplateBuilder support
Add a RestTemplateBuilder that allows RestTemplates to be easily created and configured. See gh-5507
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.boot.test.web.client;
|
||||
|
||||
import org.springframework.boot.web.client.RootUriTemplateHandler;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.util.DefaultUriTemplateHandler;
|
||||
@@ -28,17 +29,18 @@ import org.springframework.web.util.UriTemplateHandler;
|
||||
* @author Phillip Webb
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public class LocalHostUriTemplateHandler extends DefaultUriTemplateHandler {
|
||||
public class LocalHostUriTemplateHandler extends RootUriTemplateHandler {
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
public LocalHostUriTemplateHandler(Environment environment) {
|
||||
super(new DefaultUriTemplateHandler());
|
||||
Assert.notNull(environment, "Environment must not be null");
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBaseUrl() {
|
||||
public String getRootUri() {
|
||||
String port = this.environment.getProperty("local.server.port", "8080");
|
||||
return "http://localhost:" + port;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.boot.test.web.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@@ -37,20 +36,18 @@ import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.ssl.SSLContextBuilder;
|
||||
|
||||
import org.springframework.boot.web.client.BasicAuthorizationInterceptor;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.Base64Utils;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.web.client.DefaultResponseErrorHandler;
|
||||
import org.springframework.web.client.RequestCallback;
|
||||
@@ -76,8 +73,6 @@ import org.springframework.web.util.UriTemplateHandler;
|
||||
*/
|
||||
public class TestRestTemplate {
|
||||
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
/**
|
||||
@@ -912,29 +907,6 @@ public class TestRestTemplate {
|
||||
|
||||
}
|
||||
|
||||
private static class BasicAuthorizationInterceptor
|
||||
implements ClientHttpRequestInterceptor {
|
||||
|
||||
private final String username;
|
||||
|
||||
private final String password;
|
||||
|
||||
BasicAuthorizationInterceptor(String username, String password) {
|
||||
this.username = username;
|
||||
this.password = (password == null ? "" : password);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
|
||||
ClientHttpRequestExecution execution) throws IOException {
|
||||
String token = Base64Utils.encodeToString(
|
||||
(this.username + ":" + this.password).getBytes(UTF_8));
|
||||
request.getHeaders().add("Authorization", "Basic " + token);
|
||||
return execution.execute(request, body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link HttpComponentsClientHttpRequestFactory} to apply customizations.
|
||||
*/
|
||||
|
||||
@@ -47,7 +47,7 @@ public class LocalHostUriTemplateHandlerTests {
|
||||
environment.setProperty("local.server.port", "1234");
|
||||
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(
|
||||
environment);
|
||||
assertThat(handler.getBaseUrl()).isEqualTo("http://localhost:1234");
|
||||
assertThat(handler.getRootUri()).isEqualTo("http://localhost:1234");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -55,7 +55,7 @@ public class LocalHostUriTemplateHandlerTests {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(
|
||||
environment);
|
||||
assertThat(handler.getBaseUrl()).isEqualTo("http://localhost:8080");
|
||||
assertThat(handler.getRootUri()).isEqualTo("http://localhost:8080");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user