Skip to content

Commit

Permalink
Add healthcheck endpoint in waiting strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
kapishmalik committed Oct 14, 2023
1 parent 3bc486c commit f7eb089
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
private static final String WIREMOCK_2_LATEST_TAG = "2.35.0";
/*package*/ static final String WIREMOCK_2_MINIMUM_SUPPORTED_VERSION = "2.0.0";

static final String WIREMOCK_HEALTH_CHECK_SUPPORT_MINIMUM_VERSION = "3.0.0-1";

public static final DockerImageName WIREMOCK_2_LATEST =
DockerImageName.parse(OFFICIAL_IMAGE_NAME).withTag(WIREMOCK_2_LATEST_TAG);

Expand All @@ -61,6 +63,11 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
.forHttp("/__admin/mappings")
.withMethod("GET")
.forStatusCode(200);

private static final WaitStrategy HEALTH_CHECK_ENDPOINT_WAITER = Wait
.forHttp("/__admin/health")
.withMethod("GET")
.forStatusCode(200);
private static final int PORT = 8080;
private final StringBuilder wireMockArgs;
private final Map<String, Stub> mappingStubs = new HashMap<>();
Expand Down Expand Up @@ -91,7 +98,13 @@ public WireMockContainer(DockerImageName dockerImage) {
}

wireMockArgs = new StringBuilder();
setWaitStrategy(DEFAULT_WAITER);

if (version.isGreaterThanOrEqualTo(WIREMOCK_HEALTH_CHECK_SUPPORT_MINIMUM_VERSION)) {
setWaitStrategy(HEALTH_CHECK_ENDPOINT_WAITER);
}
else {
setWaitStrategy(DEFAULT_WAITER);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.wiremock.integrations.testcontainers;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse;
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;

import static org.assertj.core.api.Assertions.assertThat;

@Testcontainers
public class WireMockContainerWithWireMockVersionThreeTest
{
@Container
WireMockContainer wiremockServer = new WireMockContainer("wiremock/wiremock:" + WireMockContainer.WIREMOCK_HEALTH_CHECK_SUPPORT_MINIMUM_VERSION)
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class,
"hello-world-resource-response.xml");


@ParameterizedTest
@ValueSource(strings = {
"hello",
"/hello"
})
void helloWorld(String path) throws Exception {
// given
String url = wiremockServer.getUrl(path);

// when
HttpResponse response = new TestHttpClient().get(url);

// then
assertThat(response.getBody())
.as("Wrong response body")
.contains("Hello, world!");
}
}

0 comments on commit f7eb089

Please sign in to comment.