From f5a95e0e5632134319e7a11dbf59d79b20e21099 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 21 Nov 2023 19:34:32 -0400 Subject: [PATCH] [WIP] Implement more tests Signed-off-by: Juan Cruz Viotti --- test/http/request_1_1_test.cc | 90 ++++++++++++++++++++++++++++++++++- test/http/stub.js | 23 ++++----- 2 files changed, 97 insertions(+), 16 deletions(-) diff --git a/test/http/request_1_1_test.cc b/test/http/request_1_1_test.cc index 5029bdff..9be9b8b0 100644 --- a/test/http/request_1_1_test.cc +++ b/test/http/request_1_1_test.cc @@ -1,11 +1,99 @@ +#include +#include +#include +#include + #include #include // TODO: Add more tests -TEST(HTTP_Request_1_1, XXX) { +// - Different methods +// - Different exit codes +// - Request header +// - Request body +// - Response body +// - Response header with capture +// - Response header without capture +// - Response header with partial intersection capture + +static auto body(sourcemeta::hydra::http::Response &response) -> std::string { + std::ostringstream result; + std::copy( + std::istreambuf_iterator(response.body()), + std::istreambuf_iterator(), + std::ostreambuf_iterator(result)); + return result.str(); +} + +TEST(HTTP_Request_1_1, GET_root) { 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); + EXPECT_EQ(body(response), "RECEIVED GET /"); +} + +TEST(HTTP_Request_1_1, HEAD_root) { + sourcemeta::hydra::http::Request request{BASE_URL}; + request.method(sourcemeta::hydra::http::Method::HEAD); + sourcemeta::hydra::http::Response response{request.send().get()}; + EXPECT_EQ(response.status(), sourcemeta::hydra::http::Status::OK); + EXPECT_EQ(body(response), "RECEIVED HEAD /"); +} + +TEST(HTTP_Request_1_1, POST_root) { + sourcemeta::hydra::http::Request request{BASE_URL}; + request.method(sourcemeta::hydra::http::Method::POST); + sourcemeta::hydra::http::Response response{request.send().get()}; + EXPECT_EQ(response.status(), sourcemeta::hydra::http::Status::OK); + EXPECT_EQ(body(response), "RECEIVED POST /"); +} + +TEST(HTTP_Request_1_1, PUT_root) { + sourcemeta::hydra::http::Request request{BASE_URL}; + request.method(sourcemeta::hydra::http::Method::PUT); + sourcemeta::hydra::http::Response response{request.send().get()}; + EXPECT_EQ(response.status(), sourcemeta::hydra::http::Status::OK); + EXPECT_EQ(body(response), "RECEIVED PUT /"); +} + +TEST(HTTP_Request_1_1, DELETE_root) { + sourcemeta::hydra::http::Request request{BASE_URL}; + request.method(sourcemeta::hydra::http::Method::DELETE); + sourcemeta::hydra::http::Response response{request.send().get()}; + EXPECT_EQ(response.status(), sourcemeta::hydra::http::Status::OK); + EXPECT_EQ(body(response), "RECEIVED DELETE /"); +} + +TEST(HTTP_Request_1_1, CONNECT_root) { + sourcemeta::hydra::http::Request request{BASE_URL}; + request.method(sourcemeta::hydra::http::Method::CONNECT); + sourcemeta::hydra::http::Response response{request.send().get()}; + EXPECT_EQ(response.status(), sourcemeta::hydra::http::Status::OK); + EXPECT_EQ(body(response), "RECEIVED CONNECT /"); +} + +TEST(HTTP_Request_1_1, OPTIONS_root) { + sourcemeta::hydra::http::Request request{BASE_URL}; + request.method(sourcemeta::hydra::http::Method::OPTIONS); + sourcemeta::hydra::http::Response response{request.send().get()}; + EXPECT_EQ(response.status(), sourcemeta::hydra::http::Status::OK); + EXPECT_EQ(body(response), "RECEIVED OPTIONS /"); +} + +TEST(HTTP_Request_1_1, TRACE_root) { + sourcemeta::hydra::http::Request request{BASE_URL}; + request.method(sourcemeta::hydra::http::Method::TRACE); + sourcemeta::hydra::http::Response response{request.send().get()}; + EXPECT_EQ(response.status(), sourcemeta::hydra::http::Status::OK); + EXPECT_EQ(body(response), "RECEIVED TRACE /"); +} + +TEST(HTTP_Request_1_1, PATCH_root) { + sourcemeta::hydra::http::Request request{BASE_URL}; + request.method(sourcemeta::hydra::http::Method::PATCH); + sourcemeta::hydra::http::Response response{request.send().get()}; + EXPECT_EQ(response.status(), sourcemeta::hydra::http::Status::OK); + EXPECT_EQ(body(response), "RECEIVED PATCH /"); } diff --git a/test/http/stub.js b/test/http/stub.js index 8e134ecb..3d025215 100644 --- a/test/http/stub.js +++ b/test/http/stub.js @@ -1,20 +1,13 @@ const http = require('http'); +const PORT = 9999; -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(), - }; +http.createServer((request, response) => { + for (const [key, value] of Object.entries(request.headers)) { + response.setHeader(`X-${key}`, value); + } - // Send the JSON response - res.end(JSON.stringify(jsonResponse)); -}); - -const PORT = 9999; -server.listen(PORT, () => { + response.setHeader('Content-Type', 'text/plain'); + response.end(`RECEIVED ${request.method} ${request.url}`); +}).listen(PORT, () => { console.log(`Server is listening on port ${PORT}`); });