Annotate

spec/net_http_parser_spec.lua @ 12475:553c6204fe5b 0.12

util.argparse: Return final 'arg' table with positional arguments for convenience This is the same as the input table (which is mutated during processing), but if that table was created on the fly, such as by packing `...` it's convenient if it also gets returned from the parse function.
author Matthew Wild <mwild1@gmail.com>
date Mon, 25 Apr 2022 15:09:41 +0100
parent 11033:cb5555443852
child 12882:9ed628635dc6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
1 local http_parser = require "net.http.parser";
11030
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
2 local sha1 = require "util.hashes".sha1;
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
3
11033
cb5555443852 net.http.parser: Allow configuration of the chunk size fed to the parser
Matthew Wild <mwild1@gmail.com>
parents: 11032
diff changeset
4 local parser_input_bytes = 3;
cb5555443852 net.http.parser: Allow configuration of the chunk size fed to the parser
Matthew Wild <mwild1@gmail.com>
parents: 11032
diff changeset
5
11032
28de68414750 net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents: 11031
diff changeset
6 local function CRLF(s)
28de68414750 net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents: 11031
diff changeset
7 return (s:gsub("\n", "\r\n"));
28de68414750 net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents: 11031
diff changeset
8 end
28de68414750 net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents: 11031
diff changeset
9
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
10 local function test_stream(stream, expect)
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
11 local success_cb = spy.new(function (packet)
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
12 assert.is_table(packet);
11021
9673c95895fb net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents: 10497
diff changeset
13 if packet.body ~= false then
9673c95895fb net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents: 10497
diff changeset
14 assert.is_equal(expect.body, packet.body);
9673c95895fb net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents: 10497
diff changeset
15 end
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
16 end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
17
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
18 local parser = http_parser.new(success_cb, error, stream:sub(1,4) == "HTTP" and "client" or "server")
11033
cb5555443852 net.http.parser: Allow configuration of the chunk size fed to the parser
Matthew Wild <mwild1@gmail.com>
parents: 11032
diff changeset
19 for chunk in stream:gmatch("."..string.rep(".?", parser_input_bytes-1)) do
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
20 parser:feed(chunk);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
21 end
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
22
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
23 assert.spy(success_cb).was_called(expect.count or 1);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
24 end
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
25
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
26
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
27 describe("net.http.parser", function()
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
28 describe("parser", function()
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
29 it("should handle requests with no content-length or body", function ()
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
30 test_stream(
11032
28de68414750 net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents: 11031
diff changeset
31 CRLF[[
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 GET / HTTP/1.1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 Host: example.com
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
35 ]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
36 {
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
37 body = "";
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
38 }
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
39 );
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
40 end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
41
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
42 it("should handle responses with empty body", function ()
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
43 test_stream(
11032
28de68414750 net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents: 11031
diff changeset
44 CRLF[[
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 Content-Length: 0
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
48 ]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
49 {
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
50 body = "";
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
51 }
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
52 );
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
53 end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
54
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
55 it("should handle simple responses", function ()
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
56 test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
57
11032
28de68414750 net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents: 11031
diff changeset
58 CRLF[[
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 Content-Length: 7
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 Hello
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
63 ]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
64 {
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
65 body = "Hello\r\n", count = 1;
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
66 }
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
67 );
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
68 end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
69
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
70 it("should handle chunked encoding in responses", function ()
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
71 test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
72
11032
28de68414750 net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents: 11031
diff changeset
73 CRLF[[
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
74 HTTP/1.1 200 OK
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
75 Transfer-Encoding: chunked
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
76
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
77 1
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
78 H
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
79 1
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
80 e
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
81 2
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
82 ll
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
83 1
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
84 o
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
85 0
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
86
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
87
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
88 ]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
89 {
11021
9673c95895fb net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents: 10497
diff changeset
90 body = "Hello", count = 2;
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
91 }
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
92 );
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
93 end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
94
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
95 it("should handle a stream of responses", function ()
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
96 test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
97
11032
28de68414750 net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents: 11031
diff changeset
98 CRLF[[
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
99 HTTP/1.1 200 OK
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
100 Content-Length: 5
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
101
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
102 Hello
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 Transfer-Encoding: chunked
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 H
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 e
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 2
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 ll
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 o
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 0
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
117 ]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
118 {
11021
9673c95895fb net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents: 10497
diff changeset
119 body = "Hello", count = 3;
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
120 }
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
121 );
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 end);
11030
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
124
11032
28de68414750 net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents: 11031
diff changeset
125 it("should handle large chunked responses", function ()
11031
57739c591a8c net.http.parser: Fix incorrect path in test
Matthew Wild <mwild1@gmail.com>
parents: 11030
diff changeset
126 local data = io.open("spec/inputs/http/httpstream-chunked-test.txt", "rb"):read("*a");
11030
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
127
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
128 -- Just a sanity check... text editors and things may mess with line endings, etc.
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
129 assert.equal("25930f021785ae14053a322c2dbc1897c3769720", sha1(data, true), "test data malformed");
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
130
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
131 test_stream(data, {
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
132 body = string.rep("~", 11085), count = 2;
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
133 });
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
134 end);
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 end);