diff --git a/src/main/java/org/wiremock/spring/ConfigureWireMock.java b/src/main/java/org/wiremock/spring/ConfigureWireMock.java index 3a5eb3e..096af1d 100644 --- a/src/main/java/org/wiremock/spring/ConfigureWireMock.java +++ b/src/main/java/org/wiremock/spring/ConfigureWireMock.java @@ -22,6 +22,11 @@ */ int port() default 0; + /** + * @return true for HTTPS, else false. + */ + boolean useHttps() default false; + /** * The name of WireMock server. * diff --git a/src/main/java/org/wiremock/spring/internal/WireMockServerCreator.java b/src/main/java/org/wiremock/spring/internal/WireMockServerCreator.java index 79e7d7e..5f6c2ff 100644 --- a/src/main/java/org/wiremock/spring/internal/WireMockServerCreator.java +++ b/src/main/java/org/wiremock/spring/internal/WireMockServerCreator.java @@ -33,8 +33,13 @@ public WireMockServer createWireMockServer( final ConfigurableApplicationContext context, final ConfigureWireMock options) { final int serverPort = this.getServerProperty(context.getEnvironment(), options); - final WireMockConfiguration serverOptions = - options().port(serverPort).notifier(new Slf4jNotifier(options.name())); + final WireMockConfiguration serverOptions = options(); + if (options.useHttps()) { + serverOptions.httpsPort(serverPort); + } else { + serverOptions.port(serverPort); + } + serverOptions.notifier(new Slf4jNotifier(options.name())); this.configureFilesUnderDirectory(options.filesUnderDirectory(), "/" + options.name()) .ifPresentOrElse( @@ -106,7 +111,8 @@ public WireMockServer createWireMockServer( .collect(Collectors.toList()) .forEach( propertyName -> { - final String property = propertyName + "=" + newServer.port(); + final int port = options.useHttps() ? newServer.httpsPort() : newServer.port(); + final String property = propertyName + "=" + port; this.logger.info("Adding property '{}' to Spring application context", property); TestPropertyValues.of(property).applyTo(context.getEnvironment()); }); diff --git a/src/main/java/org/wiremock/spring/internal/WireMockSpringJunitExtension.java b/src/main/java/org/wiremock/spring/internal/WireMockSpringJunitExtension.java index 9e6c893..b2799ea 100644 --- a/src/main/java/org/wiremock/spring/internal/WireMockSpringJunitExtension.java +++ b/src/main/java/org/wiremock/spring/internal/WireMockSpringJunitExtension.java @@ -73,7 +73,13 @@ private void configureWireMockForDefaultInstance(final ExtensionContext extensio + "' on '" + wiremock.port() + "'."); - WireMock.configureFor(wiremock.port()); + final String host = "localhost"; + if (wiremock.isHttpsEnabled()) { + WireMock.configureFor( + WireMock.create().https().host(host).port(wiremock.httpsPort()).build()); + } else { + WireMock.configureFor(WireMock.create().http().host(host).port(wiremock.port()).build()); + } } } diff --git a/wiremock-spring-boot-example/src/test/java/app/HttpsTest.java b/wiremock-spring-boot-example/src/test/java/app/HttpsTest.java new file mode 100644 index 0000000..e23d1ec --- /dev/null +++ b/wiremock-spring-boot-example/src/test/java/app/HttpsTest.java @@ -0,0 +1,60 @@ +package app; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static org.assertj.core.api.Assertions.assertThat; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; +import io.restassured.RestAssured; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.wiremock.spring.ConfigureWireMock; +import org.wiremock.spring.EnableWireMock; +import org.wiremock.spring.InjectWireMock; + +@SpringBootTest +@EnableWireMock({@ConfigureWireMock(useHttps = true)}) +class HttpsTest { + + @InjectWireMock private WireMockServer wiremock; + + @Value("${wiremock.server.port}") + private int wiremockPort; + + @Value("${wiremock.server.baseUrl}") + private String wiremockUrl; + + @BeforeEach + public void before() { + RestAssured.useRelaxedHTTPSValidation(); + } + + @Test + void testProperties() { + assertThat(this.wiremockPort).isNotNull(); + assertThat(this.wiremockUrl).startsWith("https://").contains(String.valueOf(this.wiremockPort)); + } + + @Test + void testInjectedClient() { + this.wiremock.stubFor(get("/injected-client").willReturn(aResponse().withStatus(202))); + + RestAssured.when().get(this.wiremockUrl + "/injected-client").then().statusCode(202); + + assertThat(this.wiremock.findAll(anyRequestedFor(anyUrl()))).hasSize(1); + } + + @Test + void testDefaultClient() { + WireMock.stubFor(WireMock.get("/with-default-client").willReturn(aResponse().withStatus(202))); + + RestAssured.when().get(this.wiremockUrl + "/with-default-client").then().statusCode(202); + + assertThat(WireMock.findAll(anyRequestedFor(anyUrl()))).hasSize(1); + } +}