Annotate

spec/net_http_parser_spec.lua @ 11032:28de68414750

net.http.parser: Switch tests so that CRLF conversion of input data is optional
author Matthew Wild <mwild1@gmail.com>
date Fri, 21 Aug 2020 14:12:51 +0100
parent 11031:57739c591a8c
child 11033:cb5555443852
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
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
4 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
5 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
6 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
7
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
8 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
9 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
10 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
11 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
12 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
13 end
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
14 end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
15
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
16 local parser = http_parser.new(success_cb, error, stream:sub(1,4) == "HTTP" and "client" or "server")
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
17 for chunk in stream:gmatch("..?.?") do
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
18 parser:feed(chunk);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
19 end
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
20
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
21 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
22 end
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
23
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
24
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
25 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
26 describe("parser", function()
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
27 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
28 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
29 CRLF[[
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 GET / HTTP/1.1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 Host: example.com
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
33 ]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
34 {
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
35 body = "";
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 );
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
38 end);
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 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
41 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
42 CRLF[[
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 Content-Length: 0
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
46 ]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
47 {
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
48 body = "";
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 );
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
51 end);
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 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
54 test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
55
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
56 CRLF[[
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 Content-Length: 7
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 Hello
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
61 ]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
62 {
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
63 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
64 }
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
65 );
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
66 end);
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 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
69 test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
70
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
71 CRLF[[
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
72 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
73 Transfer-Encoding: chunked
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
74
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
75 1
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
76 H
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 e
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
79 2
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
80 ll
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
81 1
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
82 o
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
83 0
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
84
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
85
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 {
11021
9673c95895fb net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents: 10497
diff changeset
88 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
89 }
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
90 );
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
91 end);
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 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
94 test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
95
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
96 CRLF[[
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
97 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
98 Content-Length: 5
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
99
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
100 Hello
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 Transfer-Encoding: chunked
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 H
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 e
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 2
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 ll
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 o
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 0
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114
10497
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
115 ]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
116 {
11021
9673c95895fb net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents: 10497
diff changeset
117 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
118 }
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
119 );
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 end);
11030
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
122
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
123 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
124 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
125
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
126 -- 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
127 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
128
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
129 test_stream(data, {
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
130 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
131 });
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11021
diff changeset
132 end);
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 end);