Skip to content

Commit

Permalink
Merge pull request #61 from wiremock/feature/issue-60-https-support
Browse files Browse the repository at this point in the history
feat: optionally use HTTPS (refs #60)
  • Loading branch information
tomasbjerre authored Oct 27, 2024
2 parents 4c710d7 + 6fc0a77 commit fdb382c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/main/java/org/wiremock/spring/ConfigureWireMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
*/
int port() default 0;

/**
* @return true for HTTPS, else false.
*/
boolean useHttps() default false;

/**
* The name of WireMock server.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

Expand Down
60 changes: 60 additions & 0 deletions wiremock-spring-boot-example/src/test/java/app/HttpsTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit fdb382c

Please sign in to comment.