diff --git a/modules/mockserver/src/main/java/org/testcontainers/containers/MockServerContainer.java b/modules/mockserver/src/main/java/org/testcontainers/containers/MockServerContainer.java index 8b39d5d3763..374cf2d928d 100644 --- a/modules/mockserver/src/main/java/org/testcontainers/containers/MockServerContainer.java +++ b/modules/mockserver/src/main/java/org/testcontainers/containers/MockServerContainer.java @@ -36,7 +36,7 @@ public MockServerContainer(DockerImageName dockerImageName) { super(dockerImageName); dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME, DockerImageName.parse("mockserver/mockserver")); - waitingFor(Wait.forHttp("/mockserver/status").withMethod("PUT").forStatusCode(200)); + waitingFor(Wait.forLogMessage(".*started on port: " + PORT + ".*", 1)); withCommand("-serverPort " + PORT); addExposedPorts(PORT); @@ -46,6 +46,10 @@ public String getEndpoint() { return String.format("http://%s:%d", getHost(), getMappedPort(PORT)); } + public String getSecureEndpoint() { + return String.format("https://%s:%d", getHost(), getMappedPort(PORT)); + } + public Integer getServerPort() { return getMappedPort(PORT); } diff --git a/modules/mockserver/src/test/java/org/testcontainers/containers/MockServerContainerTest.java b/modules/mockserver/src/test/java/org/testcontainers/containers/MockServerContainerTest.java index c7225cfe1c2..3f4a8fef293 100644 --- a/modules/mockserver/src/test/java/org/testcontainers/containers/MockServerContainerTest.java +++ b/modules/mockserver/src/test/java/org/testcontainers/containers/MockServerContainerTest.java @@ -33,6 +33,53 @@ public void shouldCallActualMockserverVersion() throws Exception { } } + @Test + public void shouldCallMockserverUsingTlsProtocol() throws Exception { + try (MockServerContainer mockServer = new MockServerContainer(MOCKSERVER_IMAGE)) { + mockServer.start(); + + String expectedBody = "Hello World!"; + + try ( + MockServerClient client = new MockServerClient(mockServer.getHost(), mockServer.getServerPort()) + .withSecure(true) + ) { + assertThat(client.hasStarted()).as("Mockserver running").isTrue(); + + client.when(request().withPath("/hello")).respond(response().withBody(expectedBody)); + + assertThat(SimpleHttpClient.secureResponseFromMockserver(mockServer, "/hello")) + .as("MockServer returns correct result") + .isEqualTo(expectedBody); + } + } + } + + @Test + public void shouldCallMockserverUsingMutualTlsProtocol() throws Exception { + try ( + MockServerContainer mockServer = new MockServerContainer(MOCKSERVER_IMAGE) + .withEnv("MOCKSERVER_TLS_MUTUAL_AUTHENTICATION_REQUIRED", "true") + ) { + mockServer.start(); + + String expectedBody = "Hello World!"; + + try ( + MockServerClient client = new MockServerClient(mockServer.getHost(), mockServer.getServerPort()) + .withSecure(true) + ) { + assertThat(client.hasStarted()).as("Mockserver running").isTrue(); + + client.when(request().withPath("/hello")).respond(response().withBody(expectedBody)); + + assertThat(SimpleHttpClient.secureResponseFromMockserver(mockServer, "/hello")) + .as("MockServer returns correct result") + .isEqualTo(expectedBody); + } + } + } + @Test public void newVersionStartsWithDefaultWaitStrategy() { try (MockServerContainer mockServer = new MockServerContainer(MOCKSERVER_IMAGE)) { diff --git a/modules/mockserver/src/test/java/org/testcontainers/containers/SimpleHttpClient.java b/modules/mockserver/src/test/java/org/testcontainers/containers/SimpleHttpClient.java index da60ca9396a..6c7e75c4622 100644 --- a/modules/mockserver/src/test/java/org/testcontainers/containers/SimpleHttpClient.java +++ b/modules/mockserver/src/test/java/org/testcontainers/containers/SimpleHttpClient.java @@ -1,6 +1,9 @@ package org.testcontainers.containers; import lombok.Cleanup; +import org.mockserver.configuration.Configuration; +import org.mockserver.logging.MockServerLogger; +import org.mockserver.socket.tls.KeyStoreFactory; import java.io.BufferedReader; import java.io.IOException; @@ -8,6 +11,8 @@ import java.net.URL; import java.net.URLConnection; +import javax.net.ssl.HttpsURLConnection; + public class SimpleHttpClient { public static String responseFromMockserver(MockServerContainer mockServer, String path) throws IOException { @@ -16,4 +21,21 @@ public static String responseFromMockserver(MockServerContainer mockServer, Stri BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); return reader.readLine(); } + + public static String secureResponseFromMockserver(MockServerContainer mockServer, String path) throws IOException { + HttpsURLConnection httpUrlConnection = (HttpsURLConnection) new URL(mockServer.getSecureEndpoint() + path) + .openConnection(); + try { + httpUrlConnection.setSSLSocketFactory( + new KeyStoreFactory(Configuration.configuration(), new MockServerLogger()) + .sslContext() + .getSocketFactory() + ); + @Cleanup + BufferedReader reader = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream())); + return reader.readLine(); + } finally { + httpUrlConnection.disconnect(); + } + } }