Mercurial > prosody-hg
comparison 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 |
| parents | 57739c591a8c |
| children | cb5555443852 |
comparison
equal
deleted
inserted
replaced
| 11031:57739c591a8c | 11032:28de68414750 |
|---|---|
| 1 local http_parser = require "net.http.parser"; | 1 local http_parser = require "net.http.parser"; |
| 2 local sha1 = require "util.hashes".sha1; | 2 local sha1 = require "util.hashes".sha1; |
| 3 | |
| 4 local function CRLF(s) | |
| 5 return (s:gsub("\n", "\r\n")); | |
| 6 end | |
| 3 | 7 |
| 4 local function test_stream(stream, expect) | 8 local function test_stream(stream, expect) |
| 5 local success_cb = spy.new(function (packet) | 9 local success_cb = spy.new(function (packet) |
| 6 assert.is_table(packet); | 10 assert.is_table(packet); |
| 7 if packet.body ~= false then | 11 if packet.body ~= false then |
| 8 assert.is_equal(expect.body, packet.body); | 12 assert.is_equal(expect.body, packet.body); |
| 9 end | 13 end |
| 10 end); | 14 end); |
| 11 | 15 |
| 12 stream = stream:gsub("\n", "\r\n"); | |
| 13 local parser = http_parser.new(success_cb, error, stream:sub(1,4) == "HTTP" and "client" or "server") | 16 local parser = http_parser.new(success_cb, error, stream:sub(1,4) == "HTTP" and "client" or "server") |
| 14 for chunk in stream:gmatch("..?.?") do | 17 for chunk in stream:gmatch("..?.?") do |
| 15 parser:feed(chunk); | 18 parser:feed(chunk); |
| 16 end | 19 end |
| 17 | 20 |
| 21 | 24 |
| 22 describe("net.http.parser", function() | 25 describe("net.http.parser", function() |
| 23 describe("parser", function() | 26 describe("parser", function() |
| 24 it("should handle requests with no content-length or body", function () | 27 it("should handle requests with no content-length or body", function () |
| 25 test_stream( | 28 test_stream( |
| 26 [[ | 29 CRLF[[ |
| 27 GET / HTTP/1.1 | 30 GET / HTTP/1.1 |
| 28 Host: example.com | 31 Host: example.com |
| 29 | 32 |
| 30 ]], | 33 ]], |
| 31 { | 34 { |
| 34 ); | 37 ); |
| 35 end); | 38 end); |
| 36 | 39 |
| 37 it("should handle responses with empty body", function () | 40 it("should handle responses with empty body", function () |
| 38 test_stream( | 41 test_stream( |
| 39 [[ | 42 CRLF[[ |
| 40 HTTP/1.1 200 OK | 43 HTTP/1.1 200 OK |
| 41 Content-Length: 0 | 44 Content-Length: 0 |
| 42 | 45 |
| 43 ]], | 46 ]], |
| 44 { | 47 { |
| 48 end); | 51 end); |
| 49 | 52 |
| 50 it("should handle simple responses", function () | 53 it("should handle simple responses", function () |
| 51 test_stream( | 54 test_stream( |
| 52 | 55 |
| 53 [[ | 56 CRLF[[ |
| 54 HTTP/1.1 200 OK | 57 HTTP/1.1 200 OK |
| 55 Content-Length: 7 | 58 Content-Length: 7 |
| 56 | 59 |
| 57 Hello | 60 Hello |
| 58 ]], | 61 ]], |
| 63 end); | 66 end); |
| 64 | 67 |
| 65 it("should handle chunked encoding in responses", function () | 68 it("should handle chunked encoding in responses", function () |
| 66 test_stream( | 69 test_stream( |
| 67 | 70 |
| 68 [[ | 71 CRLF[[ |
| 69 HTTP/1.1 200 OK | 72 HTTP/1.1 200 OK |
| 70 Transfer-Encoding: chunked | 73 Transfer-Encoding: chunked |
| 71 | 74 |
| 72 1 | 75 1 |
| 73 H | 76 H |
| 88 end); | 91 end); |
| 89 | 92 |
| 90 it("should handle a stream of responses", function () | 93 it("should handle a stream of responses", function () |
| 91 test_stream( | 94 test_stream( |
| 92 | 95 |
| 93 [[ | 96 CRLF[[ |
| 94 HTTP/1.1 200 OK | 97 HTTP/1.1 200 OK |
| 95 Content-Length: 5 | 98 Content-Length: 5 |
| 96 | 99 |
| 97 Hello | 100 Hello |
| 98 HTTP/1.1 200 OK | 101 HTTP/1.1 200 OK |
| 115 } | 118 } |
| 116 ); | 119 ); |
| 117 end); | 120 end); |
| 118 end); | 121 end); |
| 119 | 122 |
| 120 pending("should handle large chunked responses", function () | 123 it("should handle large chunked responses", function () |
| 121 local data = io.open("spec/inputs/http/httpstream-chunked-test.txt", "rb"):read("*a"); | 124 local data = io.open("spec/inputs/http/httpstream-chunked-test.txt", "rb"):read("*a"); |
| 122 | 125 |
| 123 -- Just a sanity check... text editors and things may mess with line endings, etc. | 126 -- Just a sanity check... text editors and things may mess with line endings, etc. |
| 124 assert.equal("25930f021785ae14053a322c2dbc1897c3769720", sha1(data, true), "test data malformed"); | 127 assert.equal("25930f021785ae14053a322c2dbc1897c3769720", sha1(data, true), "test data malformed"); |
| 125 | 128 |
