Repository navigation
Expand file tree
/
Copy pathcompat_prosody_spec.lua
More file actions
112 lines (112 loc) · 3.38 KB
/
Copy pathcompat_prosody_spec.lua
File metadata and controls
112 lines (112 loc) · 3.38 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
describe("http.compat.prosody module", function()
local cqueues = require "cqueues"
local request = require "http.compat.prosody".request
local new_headers = require "http.headers".new
local server = require "http.server"
it("invalid uris fail", function()
local s = spy.new(function() end)
assert(cqueues.new():wrap(function()
assert.same({nil, "invalid-url"}, {request("this is not a url", {}, s)})
end):loop())
assert.spy(s).was.called()
end)
it("can construct a request from a uri", function()
-- Only step; not loop. use `error` as callback as it should never be called
assert(cqueues.new():wrap(function()
assert(request("http://example.com", {}, error))
end):step())
assert(cqueues.new():wrap(function()
local r = assert(request("http://example.com/something", {
method = "PUT";
body = '{}';
headers = {
["content-type"] = "application/json";
}
}, error))
assert.same("PUT", r.headers:get(":method"))
assert.same("application/json", r.headers:get("content-type"))
assert.same("2", r.headers:get("content-length"))
assert.same("{}", r.body)
end):step())
end)
it("can perform a GET request", function()
local cq = cqueues.new()
local s = server.listen {
host = "localhost";
port = 0;
onstream = function(s, stream)
local h = assert(stream:get_headers())
assert.same("http", h:get ":scheme")
assert.same("GET", h:get ":method")
assert.same("/", h:get ":path")
local headers = new_headers()
headers:append(":status", "200")
headers:append("connection", "close")
assert(stream:write_headers(headers, false))
assert(stream:write_chunk("success!", true))
stream:shutdown()
stream.connection:shutdown()
s:close()
end;
}
assert(s:listen())
local _, host, port = s:localname()
cq:wrap(function()
assert_loop(s)
end)
cq:wrap(function()
request(string.format("http://%s:%d", host, port), {}, function(b, c)
assert.same(200, c)
assert.same("success!", b)
end)
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
end)
it("can perform a POST request", function()
local cq = cqueues.new()
local s = server.listen {
host = "localhost";
port = 0;
onstream = function(s, stream)
local h = assert(stream:get_headers())
assert.same("http", h:get ":scheme")
assert.same("POST", h:get ":method")
assert.same("/path", h:get ":path")
assert.same("text/plain", h:get "content-type")
local b = assert(stream:get_body_as_string())
assert.same("this is a body", b)
local headers = new_headers()
headers:append(":status", "201")
headers:append("connection", "close")
-- send duplicate headers
headers:append("someheader", "foo")
headers:append("someheader", "bar")
assert(stream:write_headers(headers, false))
assert(stream:write_chunk("success!", true))
stream:shutdown()
stream.connection:shutdown()
s:close()
end;
}
assert(s:listen())
local _, host, port = s:localname()
cq:wrap(function()
assert_loop(s)
end)
cq:wrap(function()
request(string.format("http://%s:%d/path", host, port), {
headers = {
["content-type"] = "text/plain";
};
body = "this is a body";
}, function(b, c, r)
assert.same(201, c)
assert.same("success!", b)
assert.same("foo,bar", r.headers.someheader)
end)
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
end)
end)