Repository navigation
Expand file tree
/
Copy pathstream_common_spec.lua
More file actions
116 lines (116 loc) · 3.75 KB
/
Copy pathstream_common_spec.lua
File metadata and controls
116 lines (116 loc) · 3.75 KB
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
describe("http.stream_common", function()
local h1_connection = require "http.h1_connection"
local new_headers = require "http.headers".new
local cqueues = require "cqueues"
local ca = require "cqueues.auxlib"
local cs = require "cqueues.socket"
local function new_pair(version)
local s, c = ca.assert(cs.pair())
s = h1_connection.new(s, "server", version)
c = h1_connection.new(c, "client", version)
return s, c
end
it("Can read a number of characters", function()
local server, client = new_pair(1.1)
local cq = cqueues.new()
cq:wrap(function()
local stream = client:new_stream()
local headers = new_headers()
headers:append(":authority", "myauthority")
headers:append(":method", "GET")
headers:append(":path", "/")
assert(stream:write_headers(headers, false))
assert(stream:write_chunk("foo", false))
assert(stream:write_chunk("\nb", false))
assert(stream:write_chunk("ar\n", true))
end)
cq:wrap(function()
local stream = server:get_next_incoming_stream()
-- same size as next chunk
assert.same("foo", stream:get_body_chars(3))
-- less than chunk
assert.same("\n", stream:get_body_chars(1))
-- crossing chunks
assert.same("bar", stream:get_body_chars(3))
-- more than available
assert.same("\n", stream:get_body_chars(8))
-- when none available
assert.same(nil, stream:get_body_chars(8))
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
client:close()
server:close()
end)
it("Can read a line", function()
local server, client = new_pair(1.1)
local cq = cqueues.new()
cq:wrap(function()
local stream = client:new_stream()
local headers = new_headers()
headers:append(":authority", "myauthority")
headers:append(":method", "GET")
headers:append(":path", "/")
assert(stream:write_headers(headers, false))
assert(stream:write_chunk("foo", false))
assert(stream:write_chunk("\nb", false))
assert(stream:write_chunk("ar\n", true))
end)
cq:wrap(function()
local stream = server:get_next_incoming_stream()
assert.same("foo", stream:get_body_until("\n", true, false))
assert.same("bar", stream:get_body_until("\n", true, false))
assert.same(nil, stream:get_body_until("\n", true, false))
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
client:close()
server:close()
end)
it("can read into a temporary file", function()
local server, client = new_pair(1.1)
local cq = cqueues.new()
cq:wrap(function()
local stream = client:new_stream()
local headers = new_headers()
headers:append(":authority", "myauthority")
headers:append(":method", "GET")
headers:append(":path", "/")
assert(stream:write_headers(headers, false))
assert(stream:write_chunk("hello world!", true))
end)
cq:wrap(function()
local stream = assert(server:get_next_incoming_stream())
local file = assert(stream:get_body_as_file())
assert.same("hello world!", file:read"*a")
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
client:close()
server:close()
end)
it("can write body from temporary file", function()
local server, client = new_pair(1.1)
local cq = cqueues.new()
cq:wrap(function()
local file = io.tmpfile()
assert(file:write("hello world!"))
assert(file:seek("set"))
local stream = client:new_stream()
local headers = new_headers()
headers:append(":authority", "myauthority")
headers:append(":method", "GET")
headers:append(":path", "/")
assert(stream:write_headers(headers, false))
assert(stream:write_body_from_file(file))
end)
cq:wrap(function()
local stream = assert(server:get_next_incoming_stream())
assert.same("hello world!", assert(stream:get_body_as_string()))
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
client:close()
server:close()
end)
end)