|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <string> |
| 5 | + |
| 6 | +#include "testcontainers.hpp" |
| 7 | + |
| 8 | +using namespace testcontainers; |
| 9 | + |
| 10 | +class WireMockTestContainerClass : public ::testing::Test { |
| 11 | + const char* WIREMOCK_IMAGE = "wiremock/wiremock:3.0.1-1"; |
| 12 | + const char* WIREMOCK_ADMIN_MAPPING_ENDPOINT = "/__admin/mappings"; |
| 13 | + |
| 14 | +protected: |
| 15 | + void SetUp() override { |
| 16 | + using namespace std::literals; |
| 17 | + |
| 18 | + builder.expose_port(TcpPort{8080}).wait_for_http(TcpPort{8080}, WIREMOCK_ADMIN_MAPPING_ENDPOINT); |
| 19 | + } |
| 20 | + |
| 21 | + Container::Builder builder = Container::Builder{WIREMOCK_IMAGE}; |
| 22 | +}; |
| 23 | + |
| 24 | +/// This test runs a "Hello World" example. |
| 25 | +TEST_F(WireMockTestContainerClass, HelloWorld) { |
| 26 | + auto container = builder.with_file("test_data/hello.json", "/home/wiremock/mappings/hello.json").build(); |
| 27 | + ASSERT_TRUE(container.has_value()) << "Failed to run the container"; |
| 28 | + |
| 29 | + auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello"); |
| 30 | + ASSERT_EQ(code, 200) << "Expected 200 OK. Received response: " << response; |
| 31 | +} |
| 32 | + |
| 33 | +/// This test responds to an HTTP request with response from a |
| 34 | +/// prepared file. |
| 35 | +TEST_F(WireMockTestContainerClass, HelloWorldFromResource) { |
| 36 | + auto container = builder.with_file("test_data/hello_with_resource.json", "/home/wiremock/mappings/hello2.json") |
| 37 | + .with_file("test_data/response.xml", "/home/wiremock/__files/response.xml") |
| 38 | + .build(); |
| 39 | + ASSERT_TRUE(container.has_value()) << "Failed to run the container"; |
| 40 | + |
| 41 | + auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello-from-resource"); |
| 42 | + ASSERT_EQ(code, 200) << "Expected 200 OK. Received response: " << response; |
| 43 | +} |
| 44 | + |
| 45 | +/// This test performs a request that is guaranteed to fail and |
| 46 | +/// return an HTTP-500. |
| 47 | +TEST_F(WireMockTestContainerClass, HelloWorldFromMissingResource) { |
| 48 | + auto container = |
| 49 | + builder.with_file("test_data/hello_with_missing_resource.json", "/home/wiremock/mappings/hello3.json").build(); |
| 50 | + ASSERT_TRUE(container.has_value()) << "Failed to run the container"; |
| 51 | + |
| 52 | + auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello-from-missing-resource"); |
| 53 | + ASSERT_EQ(code, 500) << "Expected 500 Internal Server Error. Received response: " << response; |
| 54 | +} |
0 commit comments