spec/net_http_parser_spec.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 21 Aug 2020 13:49:10 +0100
changeset 11035 57739c591a8c
parent 11034 388f599f66d1
child 11036 28de68414750
permissions -rw-r--r--
net.http.parser: Fix incorrect path in test
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
     1
local http_parser = require "net.http.parser";
11034
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11025
diff changeset
     2
local sha1 = require "util.hashes".sha1;
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
     3
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
     4
local function test_stream(stream, expect)
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
     5
	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: 8239
diff changeset
     6
		assert.is_table(packet);
11025
9673c95895fb net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents: 10501
diff changeset
     7
		if packet.body ~= false then
9673c95895fb net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents: 10501
diff changeset
     8
			assert.is_equal(expect.body, packet.body);
9673c95895fb net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents: 10501
diff changeset
     9
		end
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    10
	end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    11
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    12
	stream = stream:gsub("\n", "\r\n");
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    13
	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: 8239
diff changeset
    14
	for chunk in stream:gmatch("..?.?") do
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    15
		parser:feed(chunk);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    16
	end
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    17
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    18
	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: 8239
diff changeset
    19
end
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    20
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    21
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    22
describe("net.http.parser", function()
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    23
	describe("parser", function()
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    24
		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: 8239
diff changeset
    25
			test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    26
[[
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    27
GET / HTTP/1.1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    28
Host: example.com
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    29
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    30
]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    31
				{
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    32
					body = "";
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    33
				}
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    34
			);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    35
		end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    36
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    37
		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: 8239
diff changeset
    38
			test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    39
[[
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    40
HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    41
Content-Length: 0
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    42
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    43
]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    44
				{
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    45
					body = "";
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    46
				}
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    47
			);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    48
		end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    49
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    50
		it("should handle simple responses", function ()
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    51
			test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    52
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    53
[[
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    54
HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    55
Content-Length: 7
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    56
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    57
Hello
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    58
]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    59
				{
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    60
					body = "Hello\r\n", count = 1;
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    61
				}
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    62
			);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    63
		end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    64
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    65
		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: 8239
diff changeset
    66
			test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    67
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    68
[[
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    69
HTTP/1.1 200 OK
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    70
Transfer-Encoding: chunked
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    71
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    72
1
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    73
H
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    74
1
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    75
e
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    76
2
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    77
ll
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    78
1
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    79
o
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    80
0
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    81
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    82
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    83
]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    84
				{
11025
9673c95895fb net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents: 10501
diff changeset
    85
					body = "Hello", count = 2;
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    86
				}
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    87
			);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    88
		end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    89
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    90
		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: 8239
diff changeset
    91
			test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    92
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    93
[[
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    94
HTTP/1.1 200 OK
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    95
Content-Length: 5
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    96
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    97
Hello
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    98
HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    99
Transfer-Encoding: chunked
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   100
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   101
1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   102
H
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   103
1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   104
e
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   105
2
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   106
ll
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   107
1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   108
o
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   109
0
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   110
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   111
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
   112
]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
   113
				{
11025
9673c95895fb net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents: 10501
diff changeset
   114
					body = "Hello", count = 3;
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
   115
				}
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
   116
			);
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   117
		end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   118
	end);
11034
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11025
diff changeset
   119
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11025
diff changeset
   120
	pending("should handle large chunked responses", function ()
11035
57739c591a8c net.http.parser: Fix incorrect path in test
Matthew Wild <mwild1@gmail.com>
parents: 11034
diff changeset
   121
		local data = io.open("spec/inputs/http/httpstream-chunked-test.txt", "rb"):read("*a");
11034
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11025
diff changeset
   122
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11025
diff changeset
   123
		-- 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: 11025
diff changeset
   124
		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: 11025
diff changeset
   125
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11025
diff changeset
   126
		test_stream(data, {
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11025
diff changeset
   127
			body = string.rep("~", 11085), count = 2;
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11025
diff changeset
   128
		});
388f599f66d1 net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents: 11025
diff changeset
   129
	end);
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   130
end);