From 75ff6ee528124979ae6a5a3b2f12d6f3d76eadd3 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 9 Dec 2019 12:43:32 +0000 Subject: net.http.parser tests: Expand tests to include validation of results --- spec/net_http_parser_spec.lua | 108 +++++++++++++++++++++++++++++++++--------- 1 file changed, 86 insertions(+), 22 deletions(-) (limited to 'spec/net_http_parser_spec.lua') diff --git a/spec/net_http_parser_spec.lua b/spec/net_http_parser_spec.lua index 6bba087c..8310a451 100644 --- a/spec/net_http_parser_spec.lua +++ b/spec/net_http_parser_spec.lua @@ -1,16 +1,68 @@ -local httpstreams = { [[ +local http_parser = require "net.http.parser"; + +local function test_stream(stream, expect) + local success_cb = spy.new(function (packet) + assert.is_table(packet); + assert.is_equal(expect.body, packet.body); + end); + + stream = stream:gsub("\n", "\r\n"); + local parser = http_parser.new(success_cb, error, stream:sub(1,4) == "HTTP" and "client" or "server") + for chunk in stream:gmatch("..?.?") do + parser:feed(chunk); + end + + assert.spy(success_cb).was_called(expect.count or 1); +end + + +describe("net.http.parser", function() + describe("parser", function() + it("should handle requests with no content-length or body", function () + test_stream( +[[ GET / HTTP/1.1 Host: example.com -]], [[ +]], + { + body = ""; + } + ); + end); + + it("should handle responses with empty body", function () + test_stream( +[[ HTTP/1.1 200 OK Content-Length: 0 -]], [[ +]], + { + body = ""; + } + ); + end); + + it("should handle simple responses", function () + test_stream( + +[[ HTTP/1.1 200 OK Content-Length: 7 Hello +]], + { + body = "Hello\r\n", count = 1; + } + ); + end); + + it("should handle chunked encoding in responses", function () + test_stream( + +[[ HTTP/1.1 200 OK Transfer-Encoding: chunked @@ -25,28 +77,40 @@ o 0 -]] -} +]], + { + body = "Hello", count = 1; + } + ); + end); + it("should handle a stream of responses", function () + test_stream( -local http_parser = require "net.http.parser"; +[[ +HTTP/1.1 200 OK +Content-Length: 5 -describe("net.http.parser", function() - describe("#new()", function() - it("should work", function() - for _, stream in ipairs(httpstreams) do - local success; - local function success_cb(packet) - success = true; - end - stream = stream:gsub("\n", "\r\n"); - local parser = http_parser.new(success_cb, error, stream:sub(1,4) == "HTTP" and "client" or "server") - for chunk in stream:gmatch("..?.?") do - parser:feed(chunk); - end - - assert.is_true(success); - end +Hello +HTTP/1.1 200 OK +Transfer-Encoding: chunked + +1 +H +1 +e +2 +ll +1 +o +0 + + +]], + { + body = "Hello", count = 2; + } + ); end); end); end); -- cgit v1.2.3 From 91d2ab91086d2aebcbc4d47a5bce05c6cd3abdcb Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 1 Aug 2020 18:41:23 +0200 Subject: net.http.parser: Allow specifying sink for large request bodies This enables uses such as saving uploaded files directly to a file on disk or streaming parsing of payloads. See #726 --- spec/net_http_parser_spec.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'spec/net_http_parser_spec.lua') diff --git a/spec/net_http_parser_spec.lua b/spec/net_http_parser_spec.lua index 8310a451..4ac3cab9 100644 --- a/spec/net_http_parser_spec.lua +++ b/spec/net_http_parser_spec.lua @@ -3,7 +3,9 @@ local http_parser = require "net.http.parser"; local function test_stream(stream, expect) local success_cb = spy.new(function (packet) assert.is_table(packet); - assert.is_equal(expect.body, packet.body); + if packet.body ~= false then + assert.is_equal(expect.body, packet.body); + end end); stream = stream:gsub("\n", "\r\n"); @@ -79,7 +81,7 @@ o ]], { - body = "Hello", count = 1; + body = "Hello", count = 2; } ); end); @@ -108,7 +110,7 @@ o ]], { - body = "Hello", count = 2; + body = "Hello", count = 3; } ); end); -- cgit v1.2.3 From 076d8b698f664a8a47a38e16f54157cb702f43a8 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 21 Aug 2020 13:41:51 +0100 Subject: net.http.parser: Add failing test for (large?) chunk-encoded responses --- spec/net_http_parser_spec.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'spec/net_http_parser_spec.lua') diff --git a/spec/net_http_parser_spec.lua b/spec/net_http_parser_spec.lua index 4ac3cab9..6a35d717 100644 --- a/spec/net_http_parser_spec.lua +++ b/spec/net_http_parser_spec.lua @@ -1,4 +1,5 @@ local http_parser = require "net.http.parser"; +local sha1 = require "util.hashes".sha1; local function test_stream(stream, expect) local success_cb = spy.new(function (packet) @@ -115,4 +116,15 @@ o ); end); end); + + pending("should handle large chunked responses", function () + local data = io.open("spec/inputs/httpstream-chunked-test.txt", "rb"):read("*a"); + + -- Just a sanity check... text editors and things may mess with line endings, etc. + assert.equal("25930f021785ae14053a322c2dbc1897c3769720", sha1(data, true), "test data malformed"); + + test_stream(data, { + body = string.rep("~", 11085), count = 2; + }); + end); end); -- cgit v1.2.3 From 70b697de98ce0501cd5d139890c9c428acd71f14 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 21 Aug 2020 13:49:10 +0100 Subject: net.http.parser: Fix incorrect path in test --- spec/net_http_parser_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/net_http_parser_spec.lua') diff --git a/spec/net_http_parser_spec.lua b/spec/net_http_parser_spec.lua index 6a35d717..1a15302d 100644 --- a/spec/net_http_parser_spec.lua +++ b/spec/net_http_parser_spec.lua @@ -118,7 +118,7 @@ o end); pending("should handle large chunked responses", function () - local data = io.open("spec/inputs/httpstream-chunked-test.txt", "rb"):read("*a"); + local data = io.open("spec/inputs/http/httpstream-chunked-test.txt", "rb"):read("*a"); -- Just a sanity check... text editors and things may mess with line endings, etc. assert.equal("25930f021785ae14053a322c2dbc1897c3769720", sha1(data, true), "test data malformed"); -- cgit v1.2.3 From 76dd86054ce12963190255a135cf7c16ba4c4615 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 21 Aug 2020 14:12:51 +0100 Subject: net.http.parser: Switch tests so that CRLF conversion of input data is optional --- spec/net_http_parser_spec.lua | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'spec/net_http_parser_spec.lua') diff --git a/spec/net_http_parser_spec.lua b/spec/net_http_parser_spec.lua index 1a15302d..1a86dbba 100644 --- a/spec/net_http_parser_spec.lua +++ b/spec/net_http_parser_spec.lua @@ -1,6 +1,10 @@ local http_parser = require "net.http.parser"; local sha1 = require "util.hashes".sha1; +local function CRLF(s) + return (s:gsub("\n", "\r\n")); +end + local function test_stream(stream, expect) local success_cb = spy.new(function (packet) assert.is_table(packet); @@ -9,7 +13,6 @@ local function test_stream(stream, expect) end end); - stream = stream:gsub("\n", "\r\n"); local parser = http_parser.new(success_cb, error, stream:sub(1,4) == "HTTP" and "client" or "server") for chunk in stream:gmatch("..?.?") do parser:feed(chunk); @@ -23,7 +26,7 @@ describe("net.http.parser", function() describe("parser", function() it("should handle requests with no content-length or body", function () test_stream( -[[ +CRLF[[ GET / HTTP/1.1 Host: example.com @@ -36,7 +39,7 @@ Host: example.com it("should handle responses with empty body", function () test_stream( -[[ +CRLF[[ HTTP/1.1 200 OK Content-Length: 0 @@ -50,7 +53,7 @@ Content-Length: 0 it("should handle simple responses", function () test_stream( -[[ +CRLF[[ HTTP/1.1 200 OK Content-Length: 7 @@ -65,7 +68,7 @@ Hello it("should handle chunked encoding in responses", function () test_stream( -[[ +CRLF[[ HTTP/1.1 200 OK Transfer-Encoding: chunked @@ -90,7 +93,7 @@ o it("should handle a stream of responses", function () test_stream( -[[ +CRLF[[ HTTP/1.1 200 OK Content-Length: 5 @@ -117,7 +120,7 @@ o end); end); - pending("should handle large chunked responses", function () + it("should handle large chunked responses", function () local data = io.open("spec/inputs/http/httpstream-chunked-test.txt", "rb"):read("*a"); -- Just a sanity check... text editors and things may mess with line endings, etc. -- cgit v1.2.3 From 54e37ffe8d4562adc974a61359a8bfef8e0dcd29 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 21 Aug 2020 14:14:29 +0100 Subject: net.http.parser: Allow configuration of the chunk size fed to the parser --- spec/net_http_parser_spec.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'spec/net_http_parser_spec.lua') diff --git a/spec/net_http_parser_spec.lua b/spec/net_http_parser_spec.lua index 1a86dbba..f71cad20 100644 --- a/spec/net_http_parser_spec.lua +++ b/spec/net_http_parser_spec.lua @@ -1,6 +1,8 @@ local http_parser = require "net.http.parser"; local sha1 = require "util.hashes".sha1; +local parser_input_bytes = 3; + local function CRLF(s) return (s:gsub("\n", "\r\n")); end @@ -14,7 +16,7 @@ local function test_stream(stream, expect) end); local parser = http_parser.new(success_cb, error, stream:sub(1,4) == "HTTP" and "client" or "server") - for chunk in stream:gmatch("..?.?") do + for chunk in stream:gmatch("."..string.rep(".?", parser_input_bytes-1)) do parser:feed(chunk); end -- cgit v1.2.3