|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | +#include <gtest/gtest.h> |
| 4 | +#include "testcontainers.hpp" |
| 5 | + |
| 6 | +using namespace testcontainers; |
| 7 | + |
| 8 | +class WireMockTestContainerClass : public ::testing::Test { |
| 9 | + |
| 10 | +const char* WIREMOCK_IMAGE = "wiremock/wiremock:3.0.1-1"; |
| 11 | +const char* WIREMOCK_ADMIN_MAPPING_ENDPOINT = "/__admin/mappings"; |
| 12 | + |
| 13 | +protected: |
| 14 | + void SetUp() override { |
| 15 | + std::cout << "Creating new container: " << WIREMOCK_IMAGE << '\n'; |
| 16 | + |
| 17 | + container = std::make_unique<Container>(WIREMOCK_IMAGE); |
| 18 | + container->expose_port(TcpPort{8080}); |
| 19 | + container->wait_for_http(TcpPort{8080}, WIREMOCK_ADMIN_MAPPING_ENDPOINT); |
| 20 | + container->with_file("test_data/hello.json", "/home/wiremock/mappings/hello.json"); |
| 21 | + container->with_file("test_data/hello_with_resource.json", "/home/wiremock/mappings/hello2.json"); |
| 22 | + container->with_file("test_data/hello_with_missing_resource.json", "/home/wiremock/mappings/hello3.json"); |
| 23 | + container->with_file("test_data/response.xml", "/home/wiremock/__files/response.xml"); |
| 24 | + |
| 25 | + auto result = container->run(); |
| 26 | + |
| 27 | + EXPECT_TRUE(!result.has_value()) << "Failed to run the container: " << result.error(); |
| 28 | + }; |
| 29 | + |
| 30 | + std::unique_ptr<Container> container; |
| 31 | +}; |
| 32 | + |
| 33 | +TEST_F(WireMockTestContainerClass, HelloWorld) { |
| 34 | + std::cout << "Sending HTTP request to the container\n"; |
| 35 | + |
| 36 | + auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello"); |
| 37 | + |
| 38 | + ASSERT_TRUE(response.has_value()) << "Failed to send HTTP request: " << response.error(); |
| 39 | + ASSERT_EQ(code, 200) << "Received wrong response code: " << response.error(); |
| 40 | + |
| 41 | + std::cout << "Server Response: HTTP-" << code << '\n' << response.value() << '\n'; |
| 42 | +} |
| 43 | + |
| 44 | +TEST_F(WireMockTestContainerClass, HelloWorldFromResource) { |
| 45 | + std::cout << "Sending HTTP request to the container\n"; |
| 46 | + |
| 47 | + auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello-from-resource"); |
| 48 | + |
| 49 | + ASSERT_TRUE(response.has_value()) << "Failed to send HTTP request: " << response.error(); |
| 50 | + ASSERT_EQ(code, 200) << "Received wrong response code: " << response.error(); |
| 51 | + |
| 52 | + std::cout << "Server Response: HTTP-" << code << '\n' << response.value() << '\n'; |
| 53 | +} |
| 54 | + |
| 55 | +TEST_F(WireMockTestContainerClass, HelloWorldFromMissingResource) { |
| 56 | + std::cout << "Sending HTTP request to the container\n"; |
| 57 | + |
| 58 | + auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello-from-missing-resource"); |
| 59 | + |
| 60 | + ASSERT_EQ(code, 500) << "The request should have failed"; |
| 61 | +} |
0 commit comments