Software /
code /
prosody
Comparison
spec/net_http_parser_spec.lua @ 10497:a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 09 Dec 2019 12:43:32 +0000 |
parent | 8236:4878e4159e12 |
child | 11021:9673c95895fb |
comparison
equal
deleted
inserted
replaced
10496:ec67425dedf1 | 10497:a9fb553b6dbb |
---|---|
1 local httpstreams = { [[ | 1 local http_parser = require "net.http.parser"; |
2 | |
3 local function test_stream(stream, expect) | |
4 local success_cb = spy.new(function (packet) | |
5 assert.is_table(packet); | |
6 assert.is_equal(expect.body, packet.body); | |
7 end); | |
8 | |
9 stream = stream:gsub("\n", "\r\n"); | |
10 local parser = http_parser.new(success_cb, error, stream:sub(1,4) == "HTTP" and "client" or "server") | |
11 for chunk in stream:gmatch("..?.?") do | |
12 parser:feed(chunk); | |
13 end | |
14 | |
15 assert.spy(success_cb).was_called(expect.count or 1); | |
16 end | |
17 | |
18 | |
19 describe("net.http.parser", function() | |
20 describe("parser", function() | |
21 it("should handle requests with no content-length or body", function () | |
22 test_stream( | |
23 [[ | |
2 GET / HTTP/1.1 | 24 GET / HTTP/1.1 |
3 Host: example.com | 25 Host: example.com |
4 | 26 |
5 ]], [[ | 27 ]], |
28 { | |
29 body = ""; | |
30 } | |
31 ); | |
32 end); | |
33 | |
34 it("should handle responses with empty body", function () | |
35 test_stream( | |
36 [[ | |
6 HTTP/1.1 200 OK | 37 HTTP/1.1 200 OK |
7 Content-Length: 0 | 38 Content-Length: 0 |
8 | 39 |
9 ]], [[ | 40 ]], |
41 { | |
42 body = ""; | |
43 } | |
44 ); | |
45 end); | |
46 | |
47 it("should handle simple responses", function () | |
48 test_stream( | |
49 | |
50 [[ | |
10 HTTP/1.1 200 OK | 51 HTTP/1.1 200 OK |
11 Content-Length: 7 | 52 Content-Length: 7 |
53 | |
54 Hello | |
55 ]], | |
56 { | |
57 body = "Hello\r\n", count = 1; | |
58 } | |
59 ); | |
60 end); | |
61 | |
62 it("should handle chunked encoding in responses", function () | |
63 test_stream( | |
64 | |
65 [[ | |
66 HTTP/1.1 200 OK | |
67 Transfer-Encoding: chunked | |
68 | |
69 1 | |
70 H | |
71 1 | |
72 e | |
73 2 | |
74 ll | |
75 1 | |
76 o | |
77 0 | |
78 | |
79 | |
80 ]], | |
81 { | |
82 body = "Hello", count = 1; | |
83 } | |
84 ); | |
85 end); | |
86 | |
87 it("should handle a stream of responses", function () | |
88 test_stream( | |
89 | |
90 [[ | |
91 HTTP/1.1 200 OK | |
92 Content-Length: 5 | |
12 | 93 |
13 Hello | 94 Hello |
14 HTTP/1.1 200 OK | 95 HTTP/1.1 200 OK |
15 Transfer-Encoding: chunked | 96 Transfer-Encoding: chunked |
16 | 97 |
23 1 | 104 1 |
24 o | 105 o |
25 0 | 106 0 |
26 | 107 |
27 | 108 |
28 ]] | 109 ]], |
29 } | 110 { |
30 | 111 body = "Hello", count = 2; |
31 | 112 } |
32 local http_parser = require "net.http.parser"; | 113 ); |
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); | 114 end); |
51 end); | 115 end); |
52 end); | 116 end); |