aboutsummaryrefslogtreecommitdiff
path: root/test/httpfixturetest.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/httpfixturetest.lua')
-rw-r--r--test/httpfixturetest.lua68
1 files changed, 68 insertions, 0 deletions
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 @@
1-- Smoke tests proving out the httpfixture.lua harness: drive
2-- socket.http.request against fully scripted, controlled raw HTTP
3-- responses over a real socket via the testsrvr.lua remote-execution
4-- server -- no Apache, no Docker, no third-party hosts. Later SSE tests
5-- build on this same fixture for real-socket, incremental-delivery
6-- coverage.
7local http = require("socket.http")
8local fixture = require("httpfixture")
9
10dofile("testsupport.lua")
11
12local url = "http://" .. fixture.host .. ":" .. fixture.port .. "/"
13
14local control, remote = fixture.connect()
15
16io.write("testing plain GET with a known body: ")
17do
18 local body = "hello, luasocket"
19 local response = fixture.response("200 OK", {
20 "Content-Length: " .. #body,
21 "Connection: close",
22 }, body)
23 fixture.accept_and_send(remote, { response })
24
25 local respbody, code = assert(http.request(url .. "get"))
26 assert(respbody == body, "body mismatch: " .. tostring(respbody))
27 assert(code == 200, "status code mismatch: " .. tostring(code))
28end
29print("ok")
30
31io.write("testing chunked-transfer-encoded body: ")
32do
33 local pieces = { "Hello, ", "chunked ", "world!" }
34 local response = fixture.response("200 OK", {
35 "Transfer-Encoding: chunked",
36 "Connection: close",
37 }, fixture.chunked(pieces))
38 fixture.accept_and_send(remote, { response })
39
40 local respbody, code = assert(http.request(url .. "chunked"))
41 assert(respbody == table.concat(pieces), "body mismatch: " .. tostring(respbody))
42 assert(code == 200, "status code mismatch: " .. tostring(code))
43end
44print("ok")
45
46io.write("testing a body delivered across separately-timed writes: ")
47do
48 local part1, part2 = "partial-", "delivery"
49 local body = part1 .. part2
50 local headersblock = fixture.response("200 OK", {
51 "Content-Length: " .. #body,
52 "Connection: close",
53 }, "")
54 fixture.accept_and_send(remote, {
55 headersblock,
56 { part1, delay = 0.2 },
57 part2,
58 })
59
60 local respbody, code = assert(http.request(url .. "slow"))
61 assert(respbody == body, "body mismatch: " .. tostring(respbody))
62 assert(code == 200, "status code mismatch: " .. tostring(code))
63end
64print("ok")
65
66remote("os.exit()")
67
68print("the library passed all tests")