aboutsummaryrefslogtreecommitdiff
path: root/test/httpfixturetest.lua
blob: 89dfd90529f76af6501fbdb0f4c7330479341a94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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")