diff options
Diffstat (limited to 'test/httpfixturetest.lua')
| -rw-r--r-- | test/httpfixturetest.lua | 68 |
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. | ||
| 7 | local http = require("socket.http") | ||
| 8 | local fixture = require("httpfixture") | ||
| 9 | |||
| 10 | dofile("testsupport.lua") | ||
| 11 | |||
| 12 | local url = "http://" .. fixture.host .. ":" .. fixture.port .. "/" | ||
| 13 | |||
| 14 | local control, remote = fixture.connect() | ||
| 15 | |||
| 16 | io.write("testing plain GET with a known body: ") | ||
| 17 | do | ||
| 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)) | ||
| 28 | end | ||
| 29 | print("ok") | ||
| 30 | |||
| 31 | io.write("testing chunked-transfer-encoded body: ") | ||
| 32 | do | ||
| 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)) | ||
| 43 | end | ||
| 44 | print("ok") | ||
| 45 | |||
| 46 | io.write("testing a body delivered across separately-timed writes: ") | ||
| 47 | do | ||
| 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)) | ||
| 63 | end | ||
| 64 | print("ok") | ||
| 65 | |||
| 66 | remote("os.exit()") | ||
| 67 | |||
| 68 | print("the library passed all tests") | ||
