Skip to content

Commit

Permalink
[WIP] Add first HTTP tests
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
  • Loading branch information
jviotti committed Nov 21, 2023
1 parent ea07dbe commit 80e34cb
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 2 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions Brewfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
brew "cmake"
brew "clang-format"
brew "doxygen"
brew "node"
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Programs
CMAKE = cmake
CTEST = ctest
# For test server
NODE = node
KILLALL = killall
SLEEP = sleep

# Options
PRESET = Debug
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmake/FindGoogleTest.cmake
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
1 change: 1 addition & 0 deletions doxygen/index.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ cmake --build ./build --config <Debug|Release> --target clang_format
# Build the project
cmake --build ./build --config <Debug|Release>
# Run the test suite
node test/http/stub.js
ctest --test-dir ./build --build-config <Debug|Release> --output-on-failure --progress
```

Expand Down
13 changes: 13 additions & 0 deletions test/http/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions test/http/request_1_1_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <gtest/gtest.h>
#include <sourcemeta/hydra/http.h>

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);
}
20 changes: 20 additions & 0 deletions test/http/stub.js
Original file line number Diff line number Diff line change
@@ -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}`);
});

0 comments on commit 80e34cb

Please sign in to comment.