diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextCustomizer.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextCustomizer.java index 93e78c17e0..1bc409e762 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextCustomizer.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextCustomizer.java @@ -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 { diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java b/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java index 84a4061050..dfb6fafa0b 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java @@ -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 * localhost:${local.server.port}. * * @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; } } diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java index 7b9c5cf9ad..60425672fa 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java @@ -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"); + } + }