Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optionally use HTTPS (refs #60) #61

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link

@dogruis dogruis Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be wrong but I thought you could have one wiremock instance with both http and https enabled. The current setup would force me to have two wiremock instances if I want to run on http and https at the same time.

Having to use
@EnableWireMock{{@ConfigureWireMock(port = 8081)}, @ConfigureWireMock(useHttps = true, port = 8082)}

I would prefer to use

@EnableWireMock(@ConfigureWireMock(useHttps = true, port = 8081, httpsPort = 8082)


@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);
}
}
Loading