From 80e34cbd1e2bb88d17249d0dd11806a43ecfa002 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Mon, 20 Nov 2023 20:40:16 -0400 Subject: [PATCH] [WIP] Add first HTTP tests Signed-off-by: Juan Cruz Viotti --- .github/workflows/ci.yml | 10 +++++++++- Brewfile | 1 + CMakeLists.txt | 5 +++++ Makefile | 10 +++++++++- cmake/FindGoogleTest.cmake | 1 + doxygen/index.markdown | 1 + test/http/CMakeLists.txt | 13 +++++++++++++ test/http/request_1_1_test.cc | 9 +++++++++ test/http/stub.js | 20 ++++++++++++++++++++ 9 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 test/http/CMakeLists.txt create mode 100644 test/http/request_1_1_test.cc create mode 100644 test/http/stub.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9e07a4ee..bfa3b6b9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -57,7 +57,7 @@ jobs: steps: - name: Install dependencies (GNU/Linux) if: runner.os == 'linux' - run: sudo apt-get install --yes clang-format libcurl4-openssl-dev + run: sudo apt-get install --yes clang-format libcurl4-openssl-dev nodejs # See https://github.com/actions/runner-images/issues/8659 - name: Workaround Clang issue (GNU/Linux) @@ -105,6 +105,14 @@ jobs: cmake --install ./build --prefix ./build/dist --config Release --verbose --component sourcemeta_hydra_dev + # Run stubs + - name: Run HTTP stub (Windows) + run: Start-Process -NoNewWindow node test\http\stub.js && Start-Sleep -Seconds 2 + if: runner.os == 'windows' + - name: Run HTTP stub (*nix) + run: (node test/http/stub.js &) && sleep 2 + if: runner.os != 'windows' + # Not every CTest version supports the --test-dir option. If such option # is not recognized, `ctest` will successfully exit finding no tests. # Better to be sure and `cd` all the time here. diff --git a/Brewfile b/Brewfile index 97725f39..a485ab67 100644 --- a/Brewfile +++ b/Brewfile @@ -1,3 +1,4 @@ brew "cmake" brew "clang-format" brew "doxygen" +brew "node" diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e3d4ce0..3f00cc84 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,6 +72,11 @@ endif() if(HYDRA_TESTS) find_package(GoogleTest REQUIRED) enable_testing() + + if(HYDRA_HTTP) + add_subdirectory(test/http) + endif() + # TODO: Add tests if(PROJECT_IS_TOP_LEVEL) # Otherwise we need the child project to link diff --git a/Makefile b/Makefile index 276bf27c..241c40c9 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,10 @@ # Programs CMAKE = cmake CTEST = ctest +# For test server +NODE = node +KILLALL = killall +SLEEP = sleep # Options PRESET = Debug @@ -30,10 +34,14 @@ compile: .always lint: .always $(CMAKE) --build ./build --config $(PRESET) --target clang_tidy -test: .always +test: test/http/stub.js .always + $(KILLALL) $(NODE) || true + $(NODE) $< & + $(SLEEP) 1 $(CMAKE) -E env UBSAN_OPTIONS=print_stacktrace=1 \ $(CTEST) --test-dir ./build --build-config $(PRESET) \ --output-on-failure --progress --parallel + $(KILLALL) $(NODE) doxygen: .always $(CMAKE) --build ./build --config $(PRESET) --target doxygen diff --git a/cmake/FindGoogleTest.cmake b/cmake/FindGoogleTest.cmake index 6f307628..7916cd5d 100644 --- a/cmake/FindGoogleTest.cmake +++ b/cmake/FindGoogleTest.cmake @@ -1,3 +1,4 @@ +include(GoogleTest) set(BUILD_GMOCK OFF CACHE BOOL "disable googlemock") set(INSTALL_GTEST OFF CACHE BOOL "disable installation") add_subdirectory("${PROJECT_SOURCE_DIR}/vendor/googletest") diff --git a/doxygen/index.markdown b/doxygen/index.markdown index b7a4c43f..7780a6b1 100644 --- a/doxygen/index.markdown +++ b/doxygen/index.markdown @@ -120,6 +120,7 @@ cmake --build ./build --config --target clang_format # Build the project cmake --build ./build --config # Run the test suite +node test/http/stub.js ctest --test-dir ./build --build-config --output-on-failure --progress ``` diff --git a/test/http/CMakeLists.txt b/test/http/CMakeLists.txt new file mode 100644 index 00000000..ba809664 --- /dev/null +++ b/test/http/CMakeLists.txt @@ -0,0 +1,13 @@ +# Tests +add_executable(sourcemeta_hydra_http_unit + request_1_1_test.cc) +sourcemeta_hydra_add_compile_options(sourcemeta_hydra_http_unit) +target_compile_definitions(sourcemeta_hydra_http_unit PRIVATE + BASE_URL="http://localhost:9999") +target_link_libraries(sourcemeta_hydra_http_unit + PRIVATE GTest::gtest GTest::gtest_main) +target_link_libraries(sourcemeta_hydra_http_unit + PRIVATE sourcemeta::hydra::http) +set_target_properties(sourcemeta_hydra_http_unit + PROPERTIES FOLDER "Hydra/HTTP") +gtest_discover_tests(sourcemeta_hydra_http_unit) diff --git a/test/http/request_1_1_test.cc b/test/http/request_1_1_test.cc new file mode 100644 index 00000000..2a2e3849 --- /dev/null +++ b/test/http/request_1_1_test.cc @@ -0,0 +1,9 @@ +#include +#include + +TEST(HTTP_Request_1_1, XXX) { + sourcemeta::hydra::http::Request request{BASE_URL}; + request.method(sourcemeta::hydra::http::Method::GET); + sourcemeta::hydra::http::Response response{request.send().get()}; + EXPECT_EQ(response.status(), sourcemeta::hydra::http::Status::OK); +} diff --git a/test/http/stub.js b/test/http/stub.js new file mode 100644 index 00000000..8e134ecb --- /dev/null +++ b/test/http/stub.js @@ -0,0 +1,20 @@ +const http = require('http'); + +const server = http.createServer((req, res) => { + // Set the response header to indicate JSON content + res.setHeader('Content-Type', 'application/json'); + + // Define a simple JSON response + const jsonResponse = { + message: 'Hello, JSON World!', + timestamp: new Date().toISOString(), + }; + + // Send the JSON response + res.end(JSON.stringify(jsonResponse)); +}); + +const PORT = 9999; +server.listen(PORT, () => { + console.log(`Server is listening on port ${PORT}`); +});