Comparison

spec/net_http_parser_spec.lua @ 11200:bf8f2da84007

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 05 Nov 2020 22:31:25 +0100
parent 11033:cb5555443852
child 12882:9ed628635dc6
comparison
equal deleted inserted replaced
11199:6c7c50a4de32 11200:bf8f2da84007
1 local httpstreams = { [[ 1 local http_parser = require "net.http.parser";
2 local sha1 = require "util.hashes".sha1;
3
4 local parser_input_bytes = 3;
5
6 local function CRLF(s)
7 return (s:gsub("\n", "\r\n"));
8 end
9
10 local function test_stream(stream, expect)
11 local success_cb = spy.new(function (packet)
12 assert.is_table(packet);
13 if packet.body ~= false then
14 assert.is_equal(expect.body, packet.body);
15 end
16 end);
17
18 local parser = http_parser.new(success_cb, error, stream:sub(1,4) == "HTTP" and "client" or "server")
19 for chunk in stream:gmatch("."..string.rep(".?", parser_input_bytes-1)) do
20 parser:feed(chunk);
21 end
22
23 assert.spy(success_cb).was_called(expect.count or 1);
24 end
25
26
27 describe("net.http.parser", function()
28 describe("parser", function()
29 it("should handle requests with no content-length or body", function ()
30 test_stream(
31 CRLF[[
2 GET / HTTP/1.1 32 GET / HTTP/1.1
3 Host: example.com 33 Host: example.com
4 34
5 ]], [[ 35 ]],
36 {
37 body = "";
38 }
39 );
40 end);
41
42 it("should handle responses with empty body", function ()
43 test_stream(
44 CRLF[[
6 HTTP/1.1 200 OK 45 HTTP/1.1 200 OK
7 Content-Length: 0 46 Content-Length: 0
8 47
9 ]], [[ 48 ]],
49 {
50 body = "";
51 }
52 );
53 end);
54
55 it("should handle simple responses", function ()
56 test_stream(
57
58 CRLF[[
10 HTTP/1.1 200 OK 59 HTTP/1.1 200 OK
11 Content-Length: 7 60 Content-Length: 7
61
62 Hello
63 ]],
64 {
65 body = "Hello\r\n", count = 1;
66 }
67 );
68 end);
69
70 it("should handle chunked encoding in responses", function ()
71 test_stream(
72
73 CRLF[[
74 HTTP/1.1 200 OK
75 Transfer-Encoding: chunked
76
77 1
78 H
79 1
80 e
81 2
82 ll
83 1
84 o
85 0
86
87
88 ]],
89 {
90 body = "Hello", count = 2;
91 }
92 );
93 end);
94
95 it("should handle a stream of responses", function ()
96 test_stream(
97
98 CRLF[[
99 HTTP/1.1 200 OK
100 Content-Length: 5
12 101
13 Hello 102 Hello
14 HTTP/1.1 200 OK 103 HTTP/1.1 200 OK
15 Transfer-Encoding: chunked 104 Transfer-Encoding: chunked
16 105
23 1 112 1
24 o 113 o
25 0 114 0
26 115
27 116
28 ]] 117 ]],
29 } 118 {
30 119 body = "Hello", count = 3;
31 120 }
32 local http_parser = require "net.http.parser"; 121 );
33
34 describe("net.http.parser", function()
35 describe("#new()", function()
36 it("should work", function()
37 for _, stream in ipairs(httpstreams) do
38 local success;
39 local function success_cb(packet)
40 success = true;
41 end
42 stream = stream:gsub("\n", "\r\n");
43 local parser = http_parser.new(success_cb, error, stream:sub(1,4) == "HTTP" and "client" or "server")
44 for chunk in stream:gmatch("..?.?") do
45 parser:feed(chunk);
46 end
47
48 assert.is_true(success);
49 end
50 end); 122 end);
51 end); 123 end);
124
125 it("should handle large chunked responses", function ()
126 local data = io.open("spec/inputs/http/httpstream-chunked-test.txt", "rb"):read("*a");
127
128 -- Just a sanity check... text editors and things may mess with line endings, etc.
129 assert.equal("25930f021785ae14053a322c2dbc1897c3769720", sha1(data, true), "test data malformed");
130
131 test_stream(data, {
132 body = string.rep("~", 11085), count = 2;
133 });
134 end);
52 end); 135 end);