From c5f169ec303a70707bc48fbece9b52cc90a48de0 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Mon, 31 Aug 2026 22:08:11 +0200 Subject: Add CI-runnable HTTP test fixture on the testsrvr/testclnt harness Drives socket.http.request against fully scripted, controlled raw HTTP responses over a real socket by reusing the existing testsrvr.lua remote-execution server -- no Apache, no Docker, no third-party hosts. Proves the fixture with a plain-GET smoke test, a chunked-transfer- encoding smoke test, and a test asserting a response body delivered across separately-timed writes (the incremental-delivery capability the upcoming SSE tests need). Wires the new test into the CI regression step. Claude-Session: https://claude.ai/code/session_01SW789SftppghLPVXYbWsGi --- test/httpfixturetest.lua | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 test/httpfixturetest.lua (limited to 'test/httpfixturetest.lua') diff --git a/test/httpfixturetest.lua b/test/httpfixturetest.lua new file mode 100644 index 0000000..89dfd90 --- /dev/null +++ b/test/httpfixturetest.lua @@ -0,0 +1,68 @@ +-- Smoke tests proving out the httpfixture.lua harness: drive +-- socket.http.request against fully scripted, controlled raw HTTP +-- responses over a real socket via the testsrvr.lua remote-execution +-- server -- no Apache, no Docker, no third-party hosts. Later SSE tests +-- build on this same fixture for real-socket, incremental-delivery +-- coverage. +local http = require("socket.http") +local fixture = require("httpfixture") + +dofile("testsupport.lua") + +local url = "http://" .. fixture.host .. ":" .. fixture.port .. "/" + +local control, remote = fixture.connect() + +io.write("testing plain GET with a known body: ") +do + local body = "hello, luasocket" + local response = fixture.response("200 OK", { + "Content-Length: " .. #body, + "Connection: close", + }, body) + fixture.accept_and_send(remote, { response }) + + local respbody, code = assert(http.request(url .. "get")) + assert(respbody == body, "body mismatch: " .. tostring(respbody)) + assert(code == 200, "status code mismatch: " .. tostring(code)) +end +print("ok") + +io.write("testing chunked-transfer-encoded body: ") +do + local pieces = { "Hello, ", "chunked ", "world!" } + local response = fixture.response("200 OK", { + "Transfer-Encoding: chunked", + "Connection: close", + }, fixture.chunked(pieces)) + fixture.accept_and_send(remote, { response }) + + local respbody, code = assert(http.request(url .. "chunked")) + assert(respbody == table.concat(pieces), "body mismatch: " .. tostring(respbody)) + assert(code == 200, "status code mismatch: " .. tostring(code)) +end +print("ok") + +io.write("testing a body delivered across separately-timed writes: ") +do + local part1, part2 = "partial-", "delivery" + local body = part1 .. part2 + local headersblock = fixture.response("200 OK", { + "Content-Length: " .. #body, + "Connection: close", + }, "") + fixture.accept_and_send(remote, { + headersblock, + { part1, delay = 0.2 }, + part2, + }) + + local respbody, code = assert(http.request(url .. "slow")) + assert(respbody == body, "body mismatch: " .. tostring(respbody)) + assert(code == 200, "status code mismatch: " .. tostring(code)) +end +print("ok") + +remote("os.exit()") + +print("the library passed all tests") -- cgit v1.2.3-55-g6feb