Skip to content

Commit b9f73a4

Browse files
Merge pull request #76 from kapishmalik/feature/add-healthcheck-endpoint-in-waiting-strategy
Add /health endpoint in waiting strategy
2 parents 1d9922d + f7eb089 commit b9f73a4

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
5050
private static final String WIREMOCK_2_LATEST_TAG = "2.35.0";
5151
/*package*/ static final String WIREMOCK_2_MINIMUM_SUPPORTED_VERSION = "2.0.0";
5252

53+
static final String WIREMOCK_HEALTH_CHECK_SUPPORT_MINIMUM_VERSION = "3.0.0-1";
54+
5355
public static final DockerImageName WIREMOCK_2_LATEST =
5456
DockerImageName.parse(OFFICIAL_IMAGE_NAME).withTag(WIREMOCK_2_LATEST_TAG);
5557

@@ -61,6 +63,11 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
6163
.forHttp("/__admin/mappings")
6264
.withMethod("GET")
6365
.forStatusCode(200);
66+
67+
private static final WaitStrategy HEALTH_CHECK_ENDPOINT_WAITER = Wait
68+
.forHttp("/__admin/health")
69+
.withMethod("GET")
70+
.forStatusCode(200);
6471
private static final int PORT = 8080;
6572
private final StringBuilder wireMockArgs;
6673
private final Map<String, Stub> mappingStubs = new HashMap<>();
@@ -91,7 +98,13 @@ public WireMockContainer(DockerImageName dockerImage) {
9198
}
9299

93100
wireMockArgs = new StringBuilder();
94-
setWaitStrategy(DEFAULT_WAITER);
101+
102+
if (version.isGreaterThanOrEqualTo(WIREMOCK_HEALTH_CHECK_SUPPORT_MINIMUM_VERSION)) {
103+
setWaitStrategy(HEALTH_CHECK_ENDPOINT_WAITER);
104+
}
105+
else {
106+
setWaitStrategy(DEFAULT_WAITER);
107+
}
95108
}
96109

97110
/**
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.wiremock.integrations.testcontainers;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.ValueSource;
5+
import org.testcontainers.junit.jupiter.Container;
6+
import org.testcontainers.junit.jupiter.Testcontainers;
7+
import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse;
8+
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
@Testcontainers
13+
public class WireMockContainerWithWireMockVersionThreeTest
14+
{
15+
@Container
16+
WireMockContainer wiremockServer = new WireMockContainer("wiremock/wiremock:" + WireMockContainer.WIREMOCK_HEALTH_CHECK_SUPPORT_MINIMUM_VERSION)
17+
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
18+
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
19+
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class,
20+
"hello-world-resource-response.xml");
21+
22+
23+
@ParameterizedTest
24+
@ValueSource(strings = {
25+
"hello",
26+
"/hello"
27+
})
28+
void helloWorld(String path) throws Exception {
29+
// given
30+
String url = wiremockServer.getUrl(path);
31+
32+
// when
33+
HttpResponse response = new TestHttpClient().get(url);
34+
35+
// then
36+
assertThat(response.getBody())
37+
.as("Wrong response body")
38+
.contains("Hello, world!");
39+
}
40+
}

0 commit comments

Comments
 (0)