1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
#!/usr/bin/env lua5.4
local parser = require('parser')
local function build_msg(body)
return 'Content-Length: ' .. tostring(string.len(body)) .. '\r\n\r\n' .. body
end
local lunatest = require('lunatest')
function test_complete_msg() -- luacheck: ignore 111
local msg = build_msg('foo')
local p = parser.Parser()
local err = p:add(msg)
lunatest.assert_nil(err)
local msgs = p:get_msgs()
lunatest.assert_table(msgs)
lunatest.assert_len(1, msgs)
lunatest.assert_equal(msgs[1], 'foo')
end
function test_two_complete_msgs() -- luacheck: ignore 111
local p = parser.Parser()
local data = build_msg('foo')
local err = p:add(data)
lunatest.assert_nil(err)
local msgs = p:get_msgs()
lunatest.assert_table(msgs)
lunatest.assert_len(1, msgs)
lunatest.assert_equal(msgs[1], 'foo')
data = build_msg('bar')
err = p:add(data)
lunatest.assert_nil(err)
msgs = p:get_msgs()
lunatest.assert_table(msgs)
lunatest.assert_len(1, msgs)
lunatest.assert_equal(msgs[1], 'bar')
end
function test_two_complete_msgs_at_once() -- luacheck: ignore 111
local data = build_msg('foo') .. build_msg('bar')
local p = parser.Parser()
local err = p:add(data)
lunatest.assert_nil(err)
local msgs = p:get_msgs()
lunatest.assert_table(msgs)
lunatest.assert_len(2, msgs)
lunatest.assert_equal(msgs[1], 'foo')
lunatest.assert_equal(msgs[2], 'bar')
end
function test_split_msg() -- luacheck: ignore 111
local msg = build_msg('foo')
local part1 = msg:sub(1, -3)
local part2 = msg:sub(-2)
local p = parser.Parser()
local err = p:add(part1)
lunatest.assert_nil(err)
err = p:add(part2)
lunatest.assert_nil(err)
local msgs = p:get_msgs()
lunatest.assert_table(msgs)
lunatest.assert_len(1, msgs)
lunatest.assert_equal(msgs[1], 'foo')
end
function test_complete_and_split_msg() -- luacheck: ignore 111
local msg = build_msg('foo') .. build_msg('bar')
local part1 = msg:sub(1, -3)
local part2 = msg:sub(-2)
local p = parser.Parser()
local err = p:add(part1)
lunatest.assert_nil(err)
err = p:add(part2)
lunatest.assert_nil(err)
local msgs = p:get_msgs()
lunatest.assert_table(msgs)
lunatest.assert_len(2, msgs)
lunatest.assert_equal(msgs[1], 'foo')
lunatest.assert_equal(msgs[2], 'bar')
end
function test_split_hdr() -- luacheck: ignore 111
local msg = build_msg('foo')
local part1 = msg:sub(1, 3)
local part2 = msg:sub(4)
local p = parser.Parser()
local err = p:add(part1)
lunatest.assert_nil(err)
err = p:add(part2)
lunatest.assert_nil(err)
local msgs = p:get_msgs()
lunatest.assert_table(msgs)
lunatest.assert_len(1, msgs)
lunatest.assert_equal(msgs[1], 'foo')
end
function test_split_hdr_body_sep() -- luacheck: ignore 111
local msg = build_msg('foo')
local part1 = msg:sub(1, 19)
local part2 = msg:sub(20)
local p = parser.Parser()
local err = p:add(part1)
lunatest.assert_nil(err)
err = p:add(part2)
lunatest.assert_nil(err)
local msgs = p:get_msgs()
lunatest.assert_table(msgs)
lunatest.assert_len(1, msgs)
lunatest.assert_equal(msgs[1], 'foo')
end
lunatest.run()
|