Update auto-configured TestRestTemplate to use SSL when it’s enabled
Closes gh-6731
This commit is contained in:
parent
46e03529fb
commit
2377d78a40
|
|
@ -22,8 +22,11 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
|||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.Ssl;
|
||||
import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
|
@ -89,12 +92,32 @@ class SpringBootTestContextCustomizer implements ContextCustomizer {
|
|||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
RestTemplateBuilder builder = getRestTemplateBuilder(applicationContext);
|
||||
TestRestTemplate template = new TestRestTemplate(builder.build());
|
||||
template.setUriTemplateHandler(
|
||||
new LocalHostUriTemplateHandler(applicationContext.getEnvironment()));
|
||||
boolean sslEnabled = isSslEnabled(applicationContext);
|
||||
TestRestTemplate template;
|
||||
if (sslEnabled) {
|
||||
template = new TestRestTemplate(builder.build(), null, null,
|
||||
HttpClientOption.SSL);
|
||||
}
|
||||
else {
|
||||
template = new TestRestTemplate(builder.build());
|
||||
}
|
||||
template.setUriTemplateHandler(new LocalHostUriTemplateHandler(
|
||||
applicationContext.getEnvironment(), sslEnabled ? "https" : "http"));
|
||||
this.object = template;
|
||||
}
|
||||
|
||||
private boolean isSslEnabled(ApplicationContext applicationContext) {
|
||||
try {
|
||||
Ssl ssl = applicationContext
|
||||
.getBean(AbstractConfigurableEmbeddedServletContainer.class)
|
||||
.getSsl();
|
||||
return ssl != null && ssl.isEnabled();
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private RestTemplateBuilder getRestTemplateBuilder(
|
||||
ApplicationContext applicationContext) {
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -23,26 +23,49 @@ import org.springframework.web.util.DefaultUriTemplateHandler;
|
|||
import org.springframework.web.util.UriTemplateHandler;
|
||||
|
||||
/**
|
||||
* {@link UriTemplateHandler} will automatically prefix relative URLs with
|
||||
* {@link UriTemplateHandler} will automatically prefix relative URIs with
|
||||
* <code>localhost:${local.server.port}</code>.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public class LocalHostUriTemplateHandler extends RootUriTemplateHandler {
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
private final String scheme;
|
||||
|
||||
/**
|
||||
* Create a new {@code LocalHostUriTemplateHandler} that will generate {@code http}
|
||||
* URIs using the given {@code environment} to determine the port.
|
||||
*
|
||||
* @param environment the environment used to determine the port
|
||||
*/
|
||||
public LocalHostUriTemplateHandler(Environment environment) {
|
||||
this(environment, "http");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code LocalHostUriTemplateHandler} the will generate URIs with the
|
||||
* given {@code scheme} and use the given {@code environment} to determine the port.
|
||||
*
|
||||
* @param environment the environment used to determine the port
|
||||
* @param scheme the scheme of the root uri
|
||||
* @since 1.4.1
|
||||
*/
|
||||
public LocalHostUriTemplateHandler(Environment environment, String scheme) {
|
||||
super(new DefaultUriTemplateHandler());
|
||||
Assert.notNull(environment, "Environment must not be null");
|
||||
Assert.notNull(scheme, "Scheme must not be null");
|
||||
this.environment = environment;
|
||||
this.scheme = scheme;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRootUri() {
|
||||
String port = this.environment.getProperty("local.server.port", "8080");
|
||||
return "http://localhost:" + port;
|
||||
return this.scheme + "://localhost:" + port;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
* Tests for {@link LocalHostUriTemplateHandler}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class LocalHostUriTemplateHandlerTests {
|
||||
|
||||
|
|
@ -42,7 +43,14 @@ public class LocalHostUriTemplateHandlerTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void getBaseUrlShouldUseLocalServerPort() throws Exception {
|
||||
public void createWhenSchemeIsNullShouldThrowException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Scheme must not be null");
|
||||
new LocalHostUriTemplateHandler(new MockEnvironment(), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRootUriShouldUseLocalServerPort() throws Exception {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.setProperty("local.server.port", "1234");
|
||||
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(
|
||||
|
|
@ -51,11 +59,19 @@ public class LocalHostUriTemplateHandlerTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void getBaseUrlWhenLocalServerPortMissingShouldUsePort8080() throws Exception {
|
||||
public void getRootUriWhenLocalServerPortMissingShouldUsePort8080() throws Exception {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(
|
||||
environment);
|
||||
assertThat(handler.getRootUri()).isEqualTo("http://localhost:8080");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRootUriUsesCustomScheme() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(environment,
|
||||
"https");
|
||||
assertThat(handler.getRootUri()).isEqualTo("https://localhost:8080");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue