spec/net_http_parser_spec.lua
author Kim Alvefur <zash@zash.se>
Sat, 01 Aug 2020 18:41:23 +0200
changeset 11025 9673c95895fb
parent 10501 a9fb553b6dbb
child 11034 388f599f66d1
permissions -rw-r--r--
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
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";
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
     2
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
     3
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
     4
	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
     5
		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
     6
		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
     7
			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
     8
		end
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
     9
	end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    10
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    11
	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
    12
	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
    13
	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
    14
		parser:feed(chunk);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    15
	end
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    16
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    17
	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
    18
end
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    19
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
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
    22
	describe("parser", function()
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    23
		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
    24
			test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    25
[[
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    26
GET / HTTP/1.1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    27
Host: example.com
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    28
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    29
]],
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
					body = "";
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    32
				}
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
		end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    35
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    36
		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
    37
			test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    38
[[
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    39
HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    40
Content-Length: 0
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    41
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    42
]],
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
					body = "";
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    45
				}
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
		end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    48
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    49
		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
    50
			test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    51
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    52
[[
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    53
HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    54
Content-Length: 7
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    55
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    56
Hello
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    57
]],
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
					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
    60
				}
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
		end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    63
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    64
		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
    65
			test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    66
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
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
    69
Transfer-Encoding: chunked
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    70
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    71
1
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    72
H
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    73
1
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    74
e
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    75
2
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    76
ll
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    77
1
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    78
o
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    79
0
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    80
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
				{
11025
9673c95895fb net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents: 10501
diff changeset
    84
					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
    85
				}
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
		end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    88
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    89
		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
    90
			test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    91
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
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
    94
Content-Length: 5
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    95
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    96
Hello
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    97
HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    98
Transfer-Encoding: chunked
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    99
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   100
1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   101
H
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   102
1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   103
e
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   104
2
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   105
ll
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
o
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   108
0
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   109
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   110
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
   111
]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
   112
				{
11025
9673c95895fb net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents: 10501
diff changeset
   113
					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
   114
				}
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
   115
			);
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   116
		end);
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);