Skip to content

Commit bb81807

Browse files
authored
Add first simple HTTP test (#4)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 1b4dd1a commit bb81807

File tree

9 files changed

+68
-2
lines changed

9 files changed

+68
-2
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
steps:
5858
- name: Install dependencies (GNU/Linux)
5959
if: runner.os == 'linux'
60-
run: sudo apt-get install --yes clang-format libcurl4-openssl-dev
60+
run: sudo apt-get install --yes clang-format libcurl4-openssl-dev nodejs
6161

6262
# See https://github.com/actions/runner-images/issues/8659
6363
- name: Workaround Clang issue (GNU/Linux)
@@ -105,6 +105,14 @@ jobs:
105105
cmake --install ./build --prefix ./build/dist --config Release --verbose
106106
--component sourcemeta_hydra_dev
107107
108+
# Run stubs
109+
- name: Run HTTP stub (Windows)
110+
run: cmd /c "start /b node test\http\stub.js" && Start-Sleep -Seconds 2
111+
if: runner.os == 'windows'
112+
- name: Run HTTP stub (*nix)
113+
run: (node test/http/stub.js &) && sleep 2
114+
if: runner.os != 'windows'
115+
108116
# Not every CTest version supports the --test-dir option. If such option
109117
# is not recognized, `ctest` will successfully exit finding no tests.
110118
# Better to be sure and `cd` all the time here.

Brewfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
brew "cmake"
22
brew "clang-format"
33
brew "doxygen"
4+
brew "node"

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ endif()
7373
if(HYDRA_TESTS)
7474
find_package(GoogleTest REQUIRED)
7575
enable_testing()
76+
77+
if(HYDRA_HTTP)
78+
add_subdirectory(test/http)
79+
endif()
80+
7681
# TODO: Add tests
7782
if(PROJECT_IS_TOP_LEVEL)
7883
# Otherwise we need the child project to link

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Programs
22
CMAKE = cmake
33
CTEST = ctest
4+
# For test server
5+
NODE = node
6+
KILLALL = killall
7+
SLEEP = sleep
48

59
# Options
610
PRESET = Debug
@@ -30,10 +34,14 @@ compile: .always
3034
lint: .always
3135
$(CMAKE) --build ./build --config $(PRESET) --target clang_tidy
3236

33-
test: .always
37+
test: test/http/stub.js .always
38+
$(KILLALL) $(NODE) || true
39+
$(NODE) $< &
40+
$(SLEEP) 1
3441
$(CMAKE) -E env UBSAN_OPTIONS=print_stacktrace=1 \
3542
$(CTEST) --test-dir ./build --build-config $(PRESET) \
3643
--output-on-failure --progress --parallel
44+
$(KILLALL) $(NODE)
3745

3846
doxygen: .always
3947
$(CMAKE) --build ./build --config $(PRESET) --target doxygen

cmake/FindGoogleTest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
include(GoogleTest)
12
set(BUILD_GMOCK OFF CACHE BOOL "disable googlemock")
23
set(INSTALL_GTEST OFF CACHE BOOL "disable installation")
34
add_subdirectory("${PROJECT_SOURCE_DIR}/vendor/googletest")

doxygen/index.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ cmake --build ./build --config <Debug|Release> --target clang_format
120120
# Build the project
121121
cmake --build ./build --config <Debug|Release>
122122
# Run the test suite
123+
node test/http/stub.js
123124
ctest --test-dir ./build --build-config <Debug|Release> --output-on-failure --progress
124125
```
125126

test/http/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Tests
2+
add_executable(sourcemeta_hydra_http_unit
3+
request_1_1_test.cc)
4+
sourcemeta_hydra_add_compile_options(sourcemeta_hydra_http_unit)
5+
target_compile_definitions(sourcemeta_hydra_http_unit PRIVATE
6+
BASE_URL="http://localhost:9999")
7+
target_link_libraries(sourcemeta_hydra_http_unit
8+
PRIVATE GTest::gtest GTest::gtest_main)
9+
target_link_libraries(sourcemeta_hydra_http_unit
10+
PRIVATE sourcemeta::hydra::http)
11+
set_target_properties(sourcemeta_hydra_http_unit
12+
PROPERTIES FOLDER "Hydra/HTTP")
13+
gtest_discover_tests(sourcemeta_hydra_http_unit)

test/http/request_1_1_test.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <gtest/gtest.h>
2+
#include <sourcemeta/hydra/http.h>
3+
4+
TEST(HTTP_Request_1_1, XXX) {
5+
sourcemeta::hydra::http::Request request{BASE_URL};
6+
request.method(sourcemeta::hydra::http::Method::GET);
7+
sourcemeta::hydra::http::Response response{request.send().get()};
8+
EXPECT_EQ(response.status(), sourcemeta::hydra::http::Status::OK);
9+
}

test/http/stub.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const http = require('http');
2+
3+
const server = http.createServer((req, res) => {
4+
// Set the response header to indicate JSON content
5+
res.setHeader('Content-Type', 'application/json');
6+
7+
// Define a simple JSON response
8+
const jsonResponse = {
9+
message: 'Hello, JSON World!',
10+
timestamp: new Date().toISOString(),
11+
};
12+
13+
// Send the JSON response
14+
res.end(JSON.stringify(jsonResponse));
15+
});
16+
17+
const PORT = 9999;
18+
server.listen(PORT, () => {
19+
console.log(`Server is listening on port ${PORT}`);
20+
});

0 commit comments

Comments
 (0)