Repository navigation
Add HTTP/1.1 wire tests for One’s server #1334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
HarshPopat23
wants to merge
2
commits into
sourcemeta:main
from
HarshPopat23:test/http1-wire-conformance
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| find_program(NODE_BIN NAMES node REQUIRED) | ||
|
|
||
| sourcemeta_executable(NAMESPACE sourcemeta PROJECT one NAME http | ||
| VARIANT wire_echo SOURCES echo.cc) | ||
| target_link_libraries(sourcemeta_one_http_wire_echo | ||
| PRIVATE sourcemeta::one::http) | ||
|
|
||
| add_test(NAME one.http.wire COMMAND "${NODE_BIN}" | ||
| "${CMAKE_CURRENT_SOURCE_DIR}/wire.test.cjs" | ||
| "$<TARGET_FILE:sourcemeta_one_http_wire_echo>") | ||
| set_tests_properties(one.http.wire PROPERTIES TIMEOUT 30) |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # HTTP/1.1 wire tests | ||
|
|
||
| The CTest target `one.http.wire` starts a test-only echo handler using | ||
| One's HTTPServer, HTTPRequest, and HTTPResponse. It binds IPv4 loopback | ||
| at an OS-selected port and announces the port on stdout. The runner waits | ||
| for the announcement, bounds socket and process operations, and shuts down | ||
| the child on success and failure. The existing Docker CTest run picks up | ||
| this test when ONE_TESTS and ONE_SERVER are enabled. | ||
|
|
||
| Run from a One build configured with `ONE_TESTS=ON` and `ONE_SERVER=ON`, | ||
| with Node available: | ||
|
|
||
| ```sh | ||
| cmake --build build --target sourcemeta_one_http_wire_echo | ||
| ctest --test-dir build -R '^one.http.wire$' --output-on-failure | ||
| ``` | ||
|
|
||
| The cases are independently written from RFC 9112 and motivated by the | ||
| reviewed categories in the [h1spec revision pinned by uWebSockets v20.81.0](https://github.com/uNetworking/h1spec/tree/f0a5650a20c575fbea0f7179a3a9cfa50f20ba6e). | ||
| The test takes no runtime dependency on h1spec or Deno. | ||
|
|
||
| | Case | Expected outcome | Basis | | ||
| | --- | --- | --- | | ||
| | Origin-form GET and a request split across writes | 200 and exact path | RFC 9112 sections 2.2 and 3.2.1 | | ||
| | Fixed-length binary body | 200 and exact bytes | RFC 9112 section 6.3 | | ||
| | Chunked body | 200 and decoded bytes | RFC 9112 section 7.1 | | ||
| | Missing Host | 400 | RFC 9112 section 3.2 | | ||
| | Bodies of 64 and 65 bytes | Exact echo at the cap and 413 above it | One HTTPRequest::body fixture policy | | ||
|
|
||
| The remaining h1spec cases are outside this initial subset. In particular, | ||
| this gate excludes timeout-only cases, special method or interim-response | ||
| handling, and cases whose broad status ranges need separate RFC review. | ||
| The split-write checks complete the requests and verify the response. | ||
| TCP is a byte stream, so the test does not assume that each client write | ||
| maps to a separate server read. |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #include <sourcemeta/core/http.h> | ||
|
|
||
| #include <sourcemeta/one/http.h> | ||
|
|
||
| #include <cstdint> // std::uint16_t | ||
| #include <exception> // std::exception_ptr | ||
| #include <iostream> // std::cout, std::endl | ||
| #include <string> // std::string | ||
|
|
||
| auto main() -> int { | ||
| using namespace sourcemeta::one; | ||
| const HTTPServer server{ | ||
| 0, | ||
| [](HTTPRequest &request, HTTPResponse &response) { | ||
| if (request.method() == "get") { | ||
| response.send(request, request.path(), Encoding::Identity); | ||
| } else { | ||
| request.body( | ||
| [](HTTPRequest &snapshot, HTTPResponse &reply, std::string body, | ||
| const bool too_big) { | ||
| if (too_big) { | ||
| reply.write_status( | ||
| sourcemeta::core::HTTP_STATUS_CONTENT_TOO_LARGE); | ||
| reply.send_without_content(); | ||
| } else { | ||
| reply.send(snapshot, body, Encoding::Identity); | ||
| } | ||
| }, | ||
| [](HTTPRequest &, HTTPResponse &reply, std::exception_ptr) { | ||
| reply.write_status( | ||
| sourcemeta::core::HTTP_STATUS_INTERNAL_SERVER_ERROR); | ||
| reply.send_without_content(); | ||
| }, | ||
| 64); | ||
| } | ||
| }, | ||
| [](const std::uint16_t port) { std::cout << port << std::endl; }, | ||
| [](const std::uint16_t port) { | ||
| std::cerr << "Failed to listen on 127.0.0.1:" << port << '\n'; | ||
| }, | ||
| "127.0.0.1"}; | ||
| return server.stopped_gracefully() ? 0 : 1; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| const assert = require("node:assert/strict"); | ||
| const { spawn } = require("node:child_process"); | ||
| const { once } = require("node:events"); | ||
| const { createConnection } = require("node:net"); | ||
| const { createInterface } = require("node:readline"); | ||
| const { setTimeout: delay } = require("node:timers/promises"); | ||
|
|
||
| const HOST = "127.0.0.1"; | ||
| const DEADLINE = 3000; | ||
|
|
||
| function request(port, parts) { | ||
| return new Promise((resolve, reject) => { | ||
| const socket = createConnection({ host: HOST, port }); | ||
| const chunks = []; | ||
| const deadline = setTimeout(() => socket.destroy(new Error("Response deadline exceeded")), DEADLINE); | ||
| socket.on("data", (chunk) => chunks.push(chunk)); | ||
| socket.once("error", reject); | ||
| socket.once("close", (failed) => { | ||
| clearTimeout(deadline); | ||
| if (!failed) resolve(Buffer.concat(chunks)); | ||
| }); | ||
| socket.once("connect", async () => { | ||
| try { | ||
| for (const [index, part] of parts.entries()) { | ||
| if (index > 0) { | ||
| await delay(40); | ||
| assert.equal(chunks.length, 0, "response arrived before the request was complete"); | ||
| assert.equal(socket.destroyed, false, "connection closed before the request was complete"); | ||
| } | ||
| socket.write(part); | ||
| } | ||
| } catch (error) { | ||
| socket.destroy(error); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function checkResponse(raw, status, expectedBody) { | ||
| const split = raw.indexOf("\r\n\r\n"); | ||
| assert.ok(split > 0, "expected a complete response header block"); | ||
| const [line, ...headers] = raw.subarray(0, split).toString("latin1").split("\r\n"); | ||
| assert.equal(Number(/^HTTP\/1\.1 (\d{3})(?: |$)/.exec(line)?.[1]), status, line); | ||
| if (expectedBody !== undefined) { | ||
| const body = Buffer.from(expectedBody); | ||
| assert.deepEqual(raw.subarray(split + 4), body); | ||
| assert.deepEqual(headers.filter((header) => /^content-length:/i.test(header)) | ||
| .map((header) => header.split(":")[1].trim()), [String(body.length)]); | ||
| } | ||
| } | ||
|
|
||
| async function run(port) { | ||
| const get = "GET /example?query=1 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"; | ||
| const post = "POST /echo HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n"; | ||
| const binary = Buffer.from([0, 0x41, 0xff, 0x0a]); | ||
| // These cases re-express behaviors covered by h1spec. See README.md. | ||
| const cases = [ | ||
| ["GET request target", [get], 200, "/example"], | ||
| ["request line and header split across writes", [get.slice(0, 9), get.slice(9, 30), get.slice(30)], 200, "/example"], | ||
| ["fixed-length binary body", [post + "Content-Length: 4\r\n\r\n", binary], 200, binary], | ||
| ["chunked body", [post + "Transfer-Encoding: chunked\r\n\r\n2\r\nhe\r\n3\r\nllo\r\n0\r\n\r\n"], 200, "hello"], | ||
| ["missing Host", ["GET /example HTTP/1.1\r\nConnection: close\r\n\r\n"], 400], | ||
| // The body limit is specific to the test fixture and exercises One's wrapper. | ||
| ["body at the limit", [post + "Content-Length: 64\r\n\r\n" + "a".repeat(64)], 200, "a".repeat(64)], | ||
| ["body above the limit", [post + "Content-Length: 65\r\n\r\n" + "a".repeat(65)], 413], | ||
| ]; | ||
| for (const [name, parts, status, body] of cases) { | ||
| try { | ||
| checkResponse(await request(port, parts), status, body); | ||
| console.log("PASS " + name); | ||
| } catch (error) { | ||
| throw new Error(name + ": " + error.message, { cause: error }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function main() { | ||
| const child = spawn(process.argv[2], [], { stdio: ["ignore", "pipe", "inherit"] }); | ||
| const lines = createInterface({ input: child.stdout }); | ||
| const ready = once(lines, "line").then(([line]) => Number(line)); | ||
| const closed = once(child, "close"); | ||
| let timeout; | ||
| let failed = false; | ||
| const readinessDeadline = new Promise((_, reject) => { | ||
| timeout = setTimeout(() => reject(new Error( | ||
| "Echo server did not announce readiness before the deadline" | ||
| )), DEADLINE); | ||
| }); | ||
| try { | ||
| const port = await Promise.race([ | ||
| ready, | ||
| readinessDeadline, | ||
| closed.then(([code, signal]) => { throw new Error("Echo server exited before readiness: " + (signal || code)); }), | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
|
||
| ]); | ||
| clearTimeout(timeout); | ||
| assert.ok(Number.isInteger(port) && port > 0 && port <= 65535, "expected an ephemeral port"); | ||
| await run(port); | ||
| } catch (error) { | ||
| failed = true; | ||
| throw error; | ||
| } finally { | ||
| clearTimeout(timeout); | ||
| lines.close(); | ||
| child.kill("SIGTERM"); | ||
| const kill = setTimeout(() => child.kill("SIGKILL"), DEADLINE); | ||
| try { | ||
| const [code, signal] = await closed; | ||
| assert.equal(signal, null, "echo server terminated by " + signal); | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
|
||
| assert.equal(code, 0, "expected a graceful server exit"); | ||
| } catch (shutdownError) { | ||
| if (!failed) throw shutdownError; | ||
| console.error("Echo server cleanup also failed:", shutdownError.message); | ||
| } finally { | ||
| clearTimeout(kill); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| main().catch((error) => { | ||
| console.error(error); | ||
| process.exitCode = 1; | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general try to avoid these markdown files. Its unnecessary documentation that can easily go out of date.