-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Juan Cruz Viotti <[email protected]>
- Loading branch information
Showing
6 changed files
with
139 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,124 @@ | ||
#include <algorithm> | ||
#include <iterator> | ||
#include <sstream> | ||
#include <string> | ||
|
||
#include <gtest/gtest.h> | ||
#include <sourcemeta/hydra/http.h> | ||
|
||
// TODO: Add more tests | ||
// - Request body | ||
// - Response body | ||
// - Request header | ||
// - 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<std::ostringstream::char_type>(response.body()), | ||
std::istreambuf_iterator<std::ostringstream::char_type>(), | ||
std::ostreambuf_iterator<std::ostringstream::char_type>(result)); | ||
return result.str(); | ||
} | ||
|
||
TEST(HTTP_Request_1_1, XXX) { | ||
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_FALSE(response.empty()); | ||
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_TRUE(response.empty()); | ||
} | ||
|
||
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_FALSE(response.empty()); | ||
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_FALSE(response.empty()); | ||
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_FALSE(response.empty()); | ||
EXPECT_EQ(body(response), "RECEIVED DELETE /"); | ||
} | ||
|
||
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_FALSE(response.empty()); | ||
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_FALSE(response.empty()); | ||
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_FALSE(response.empty()); | ||
EXPECT_EQ(body(response), "RECEIVED PATCH /"); | ||
} | ||
|
||
TEST(HTTP_Request_1_1, GET_root_foo_bar) { | ||
sourcemeta::hydra::http::Request request{std::string{BASE_URL} + "/foo/bar"}; | ||
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_FALSE(response.empty()); | ||
EXPECT_EQ(body(response), "RECEIVED GET /foo/bar"); | ||
} | ||
|
||
TEST(HTTP_Request_1_1, GET_root_custom_code_string) { | ||
sourcemeta::hydra::http::Request request{BASE_URL}; | ||
request.method(sourcemeta::hydra::http::Method::GET); | ||
request.header("X-Code", "400"); | ||
sourcemeta::hydra::http::Response response{request.send().get()}; | ||
EXPECT_EQ(response.status(), sourcemeta::hydra::http::Status::BAD_REQUEST); | ||
EXPECT_FALSE(response.empty()); | ||
EXPECT_EQ(body(response), "RECEIVED GET /"); | ||
} | ||
|
||
TEST(HTTP_Request_1_1, GET_root_custom_code_integer) { | ||
sourcemeta::hydra::http::Request request{BASE_URL}; | ||
request.method(sourcemeta::hydra::http::Method::GET); | ||
request.header("X-Code", 400); | ||
sourcemeta::hydra::http::Response response{request.send().get()}; | ||
EXPECT_EQ(response.status(), sourcemeta::hydra::http::Status::BAD_REQUEST); | ||
EXPECT_FALSE(response.empty()); | ||
EXPECT_EQ(body(response), "RECEIVED GET /"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,17 @@ | ||
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)); | ||
}); | ||
if (request.headers['x-code']) { | ||
response.statusCode = parseInt(request.headers['x-code'], 10); | ||
} | ||
|
||
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}`); | ||
}); |