|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 WireMock Inc, Oleg Nenashev and all project contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.wiremock.integrations.testcontainers.wiremock2; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.testcontainers.Testcontainers.exposeHostPorts; |
| 20 | +import static org.testcontainers.shaded.org.awaitility.Awaitility.await; |
| 21 | + |
| 22 | +import java.nio.file.Paths; |
| 23 | +import java.time.Duration; |
| 24 | +import java.util.Collections; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | +import org.testcontainers.containers.GenericContainer; |
| 29 | +import org.testcontainers.containers.output.Slf4jLogConsumer; |
| 30 | +import org.testcontainers.junit.jupiter.Container; |
| 31 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 32 | +import org.wiremock.integrations.testcontainers.TestConfig; |
| 33 | +import org.wiremock.integrations.testcontainers.WireMockContainer; |
| 34 | +import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse; |
| 35 | +import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient; |
| 36 | +import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpServer; |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests the WireMock Webhook extension and TestContainers Networking |
| 40 | + * For this type of tests we should use following steps: |
| 41 | + * <p> |
| 42 | + * Use {@link GenericContainer#withAccessToHost(boolean)} to force the host access mechanism |
| 43 | + * <p> |
| 44 | + * Use {@link org.testcontainers.Testcontainers#exposeHostPorts(int...)} to expose host machine ports to containers |
| 45 | + * <p> |
| 46 | + * Use {@link GenericContainer#INTERNAL_HOST_HOSTNAME} to calculate hostname for callback |
| 47 | + * |
| 48 | + * @see <a href="https://www.testcontainers.org/features/networking/">Testcontainers Networking</a> |
| 49 | + */ |
| 50 | +@Testcontainers |
| 51 | +class WebhooksExtensionTest { |
| 52 | + |
| 53 | + private static final Logger LOGGER = LoggerFactory.getLogger(WebhooksExtensionTest.class); |
| 54 | + private static final String WIREMOCK_PATH = "/wiremock/callback-trigger"; |
| 55 | + private static final String APPLICATION_PATH = "/application/callback-receiver"; |
| 56 | + |
| 57 | + |
| 58 | + TestHttpServer applicationServer = TestHttpServer.newInstance(); |
| 59 | + @Container |
| 60 | + WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_2_IMAGE) |
| 61 | + .withLogConsumer(new Slf4jLogConsumer(LOGGER)) |
| 62 | + .withCliArg("--global-response-templating") |
| 63 | + .withMapping("webhook-callback-template", WebhooksExtensionTest.class, "webhook-callback-template.json") |
| 64 | + .withExtensions("Webhooks", |
| 65 | + Collections.singleton("org.wiremock.webhooks.Webhooks"), |
| 66 | + Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-webhooks-extension-2.35.0.jar").toFile())) |
| 67 | + .withAccessToHost(true); // Force the host access mechanism |
| 68 | + |
| 69 | + @Test |
| 70 | + void callbackUsingJsonStub() throws Exception { |
| 71 | + // given |
| 72 | + exposeHostPorts(applicationServer.getPort()); // Exposing host ports to the container |
| 73 | + |
| 74 | + String wiremockUrl = wiremockServer.getUrl(WIREMOCK_PATH); |
| 75 | + String applicationCallbackUrl = String.format("http://%s:%d%s", GenericContainer.INTERNAL_HOST_HOSTNAME, applicationServer.getPort(), APPLICATION_PATH); |
| 76 | + |
| 77 | + // when |
| 78 | + HttpResponse response = new TestHttpClient().post( |
| 79 | + wiremockUrl, |
| 80 | + "{\"callbackMethod\": \"PUT\", \"callbackUrl\": \"" + applicationCallbackUrl + "\"}" |
| 81 | + ); |
| 82 | + |
| 83 | + // then |
| 84 | + assertThat(response).as("Wiremock Response").isNotNull().satisfies(it -> { |
| 85 | + assertThat(it.getStatusCode()).as("Wiremock Response Status").isEqualTo(200); |
| 86 | + assertThat(it.getBody()).as("Wiremock Response Body") |
| 87 | + .contains("Please wait callback") |
| 88 | + .contains("PUT") |
| 89 | + .contains(applicationCallbackUrl); |
| 90 | + }); |
| 91 | + |
| 92 | + await().atMost(Duration.ofMillis(5000)).untilAsserted(() -> { |
| 93 | + assertThat(applicationServer.getRecordedRequests()).as("Received Callback") |
| 94 | + .hasSize(1) |
| 95 | + .first().usingRecursiveComparison() |
| 96 | + .isEqualTo(new TestHttpServer.RecordedRequest("PUT", APPLICATION_PATH, "Async processing Finished")); |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments