-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintegration_spec.lua
More file actions
90 lines (84 loc) · 2.43 KB
/
integration_spec.lua
File metadata and controls
90 lines (84 loc) · 2.43 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
local lux = require 'luxure'
local cosock = require 'cosock'
local Request = require 'luncheon.request'
local Response = require 'luncheon.response'
local log = require 'log'
local test_utils = require 'spec.test_utils'
function get_socket(ip, port)
local sock = assert(cosock.socket.tcp())
assert(sock:connect(ip, port))
return sock
end
function make_request(req, ip, port)
local sock = get_socket(ip, port)
req.socket = sock
req:send()
local res = assert(Response.tcp_source(sock))
return res
end
local tests = {
index = function(ip, port)
local index_req = Request.new('GET', '/')
local index_res = make_request(index_req, ip, port)
assert.are.equal(200, index_res.status)
assert.are.equal('index', index_res:get_body())
end,
not_found = function(ip, port)
local req = Request.new('GET', '/not-found')
local res = make_request(req, ip, port)
assert.are.equal(404, res.status)
end,
error = function(ip, port)
local req = Request.new('GET', '/error')
local res = make_request(req, ip, port)
assert.are.equal(500, res.status)
assert(res:get_body():find('Error!'))
end,
error_after_send = function(ip, port)
local req = Request.new('GET', '/error-after-send')
local res = make_request(req, ip, port)
assert.are.equal(200, res.status)
end,
}
describe('Integration Test', function()
it('works #integration',test_utils.wrap(function()
local server = assert(lux.Server.new(lux.Opts.new():set_env('debug')))
server:listen()
server:get('/', function(req, res)
res:send('index')
end)
server:get('/error', function(req, res)
lux.Error.raise('Error!')
end)
server:get('/error-after-send', function(req, res)
res:send()
lux.Error.raise('Error!')
end)
server:get('/close-socket', function(req, res)
res:send()
server.sock:close()
end)
local ip = assert(server.ip)
local port = assert(server.port)
local total_tests = (function()
local ret = 0
for _ in pairs(tests) do ret = ret + 1 end
return ret
end)()
local ct = 0
cosock.spawn(function()
for name, f in pairs(tests) do
ct = ct + 1
log.info('starting', name)
f(ip, port)
log.info('completed', name)
end
end, 'client loop')
server:spawn(function(dets)
print("ERROR: dets", dets)
error()
end, function()
return ct < total_tests
end)
end))
end)