Skip to content

Commit

Permalink
[Enhancement]: Update waiting strategy for Mockserver container (#6951)
Browse files Browse the repository at this point in the history
Replace `Wait.forHttp` for `Wait.forLogMessage`. This change will allow Mockserver to work with HTTP and HTTPS requests.

Fixes #6647
  • Loading branch information
DenilssonMontoya authored May 2, 2023
1 parent ab90c0f commit cce90eb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
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;
import java.io.InputStreamReader;
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 {
Expand All @@ -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();
}
}
}

0 comments on commit cce90eb

Please sign in to comment.