Repository navigation
Expand file tree
/
Copy pathclient_spec.lua
More file actions
192 lines (192 loc) · 5.75 KB
/
Copy pathclient_spec.lua
File metadata and controls
192 lines (192 loc) · 5.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
describe("http.client module", function()
local client = require "http.client"
local http_connection_common = require "http.connection_common"
local http_h1_connection = require "http.h1_connection"
local http_h2_connection = require "http.h2_connection"
local http_headers = require "http.headers"
local http_server = require "http.server"
local http_tls = require "http.tls"
local cqueues = require "cqueues"
local ca = require "cqueues.auxlib"
local cs = require "cqueues.socket"
local cdh = require "cqueues.dns.hosts"
local cdr = require "cqueues.dns.resolver"
local cdrs = require "cqueues.dns.resolvers"
local openssl_pkey = require "openssl.pkey"
local openssl_ctx = require "openssl.ssl.context"
local openssl_x509 = require "openssl.x509"
it("throws error on invalid family+path combination", function()
assert.has.errors(function()
client.connect{family = cs.AF_INET, path = "/somepath"}
end)
end)
it("invalid network parameters return nil, err, errno", function()
-- Invalid network parameters will return nil, err, errno
local ok, err, errno = client.connect{host="127.0.0.1", port="invalid"}
assert.same(nil, ok)
assert.same("string", type(err))
assert.same("number", type(errno))
end)
local function send_request(conn)
local stream = conn:new_stream()
local req_headers = http_headers.new()
req_headers:append(":authority", "myauthority")
req_headers:append(":method", "GET")
req_headers:append(":path", "/")
req_headers:append(":scheme", conn:checktls() and "https" or "http")
assert(stream:write_headers(req_headers, true))
local res_headers = assert(stream:get_headers())
assert.same("200", res_headers:get(":status"))
end
local function test(client_cb)
local cq = cqueues.new()
local s = assert(http_server.listen {
host = "localhost";
port = 0;
onstream = function(s, stream)
assert(stream:get_headers())
local resp_headers = http_headers.new()
resp_headers:append(":status", "200")
assert(stream:write_headers(resp_headers, false))
assert(stream:write_chunk("hello world", true))
stream:shutdown()
stream.connection:shutdown()
s:close()
end;
})
assert(s:listen())
local family, host, port = s:localname()
cq:wrap(function()
assert_loop(s)
end)
cq:wrap(client_cb, family, host, port)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
end
it("works with a cqueues.dns.resolver object", function()
test(function(family, ip, port)
local hosts = cdh.new()
hosts:insert(ip, "example.com")
send_request(assert(client.connect {
dns_resolver = cdr.new(nil, hosts);
family = family;
host = "example.com";
port = port;
}))
end)
end)
it("works with a cqueues.dns.resolvers object", function()
test(function(family, ip, port)
local hosts = cdh.new()
hosts:insert(ip, "example.com")
send_request(assert(client.connect {
dns_resolver = cdrs.new(nil, hosts);
family = family;
host = "example.com";
port = port;
}))
end)
end)
local function test_pair(client_options, server_func)
local s, c = ca.assert(cs.pair())
local cq = cqueues.new();
cq:wrap(function()
local conn = assert(client.negotiate(c, client_options))
send_request(conn)
end)
cq:wrap(function()
s = server_func(s)
if not s then return end
if client_options.tls then
local ssl = s:checktls()
assert.same(client_options.sendname, ssl:getHostName())
end
local stream = assert(s:get_next_incoming_stream())
assert(stream:get_headers())
local res_headers = http_headers.new()
res_headers:append(":status", "200")
assert(stream:write_headers(res_headers, true))
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
c:close()
s:close()
end
local function new_server_ctx()
local key = openssl_pkey.new()
local crt = openssl_x509.new()
crt:setPublicKey(key)
crt:sign(key)
local ctx = http_tls.new_server_context()
assert(ctx:setPrivateKey(key))
assert(ctx:setCertificate(crt))
return ctx
end
it("works with an http/1.1 server", function()
test_pair({}, function(s)
return http_h1_connection.new(s, "server", 1.1)
end)
end)
it("works with an http/2 server", function()
test_pair({
version = 2;
}, function(s)
return http_h2_connection.new(s, "server", {})
end)
end)
it("fails with unknown http version", function()
assert.has.error(function()
test_pair({
version = 5;
}, function() end)
end)
end)
it("works with an https/1.1 server", function()
local client_ctx = http_tls.new_client_context()
client_ctx:setVerify(openssl_ctx.VERIFY_NONE)
test_pair({
tls = true;
ctx = client_ctx;
sendname = "mysendname";
}, function(s)
assert(s:starttls(new_server_ctx()))
return http_h1_connection.new(s, "server", 1.1)
end)
end)
-- pending as older openssl (used by e.g. travis-ci) doesn't have any non-disallowed ciphers
pending("works with an https/2 server", function()
local client_ctx = http_tls.new_client_context()
client_ctx:setVerify(openssl_ctx.VERIFY_NONE)
test_pair({
tls = true;
ctx = client_ctx;
sendname = "mysendname";
version = 2;
}, function(s)
assert(s:starttls(new_server_ctx()))
return http_h2_connection.new(s, "server", {})
end)
end)
it("reports errors from :starttls", function()
-- default settings should fail as it should't allow self-signed
local s, c = ca.assert(cs.pair())
local cq = cqueues.new();
cq:wrap(function()
local ok, err = client.negotiate(c, {
tls = true;
})
assert.falsy(ok)
assert.truthy(err:match("starttls: "))
end)
cq:wrap(function()
s:onerror(http_connection_common.onerror)
local ok, err = s:starttls()
assert.falsy(ok)
assert.truthy(err:match("starttls: "))
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
c:close()
s:close()
end)
end)