spec/net_http_parser_spec.lua
author Matthew Wild <mwild1@gmail.com>
Mon, 09 Dec 2019 12:43:32 +0000
changeset 10501 a9fb553b6dbb
parent 8239 4878e4159e12
child 11025 9673c95895fb
permissions -rw-r--r--
net.http.parser tests: Expand tests to include validation of results
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);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
     6
		assert.is_equal(expect.body, packet.body);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
     7
	end);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
     8
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
     9
	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
    10
	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
    11
	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
    12
		parser:feed(chunk);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    13
	end
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    14
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    15
	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
    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
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    19
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
    20
	describe("parser", function()
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    21
		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
    22
			test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    23
[[
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    24
GET / HTTP/1.1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    25
Host: example.com
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    26
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    27
]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    28
				{
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    29
					body = "";
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
		end);
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
		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
    35
			test_stream(
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    36
[[
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    37
HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    38
Content-Length: 0
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    39
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    40
]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    41
				{
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    42
					body = "";
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
		end);
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
		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
    48
			test_stream(
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
[[
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    51
HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    52
Content-Length: 7
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    53
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    54
Hello
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    55
]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    56
				{
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    57
					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
    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
		end);
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
		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
    63
			test_stream(
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
[[
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    66
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
    67
Transfer-Encoding: chunked
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
1
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    70
H
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
e
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    73
2
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    74
ll
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    75
1
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    76
o
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    77
0
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    78
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    79
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
					body = "Hello", count = 1;
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
			);
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    85
		end);
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
		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
    88
			test_stream(
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
[[
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
    91
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
    92
Content-Length: 5
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
Hello
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    95
HTTP/1.1 200 OK
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    96
Transfer-Encoding: chunked
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    97
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    98
1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    99
H
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
e
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   102
2
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   103
ll
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   104
1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   105
o
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   106
0
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   107
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   108
10501
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
   109
]],
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
   110
				{
a9fb553b6dbb net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
   111
					body = "Hello", count = 2;
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
			);
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   114
		end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   115
	end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   116
end);