forked from sumory/lor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_middleware_spec.lua
executable file
·108 lines (91 loc) · 2.61 KB
/
error_middleware_spec.lua
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
before_each(function()
lor = _G.lor
app = lor({
debug = true
})
Request = _G.request
Response = _G.response
req = Request:new()
res = Response:new()
end)
after_each(function()
lor = nil
app = nil
Request = nil
Response = nil
req = nil
res = nil
end)
describe("error middleware test:", function()
it("error middleware should stop the left error middlewares if has no `next`.", function()
local count = 1
app:use("/user", function(req, res, next)
count = 2
next()
end)
app:use("/user/123", function(req, res, next)
count = 3
next()
end)
app:get("/user/123/create", function(req, res, next)
count = 4
error("an error occurs")
end)
app:erroruse(function(err, req, res, next)
count = 5
end)
app:erroruse(function(err, req, res, next)
count = 100
end)
req.path = "/user/123/create"
req.method = "get"
app:handle(req, res)
assert.is.equals(count, 5)
end)
it("error middleware should continue the left error middlewares if has `next`.", function()
local count = 1
app:use("/user", function(req, res, next)
count = 2
next()
end)
app:use("/user/123", function(req, res, next)
count = 3
next()
end)
app:get("/user/123/create", function(req, res, next)
count = 4
error("an error occurs")
end)
app:erroruse(function(err, req, res, next)
count = 5
next(err)
end)
app:erroruse(function(err, req, res, next)
assert.is.truthy(err)
count = 100
end)
req.path = "/user/123/create"
req.method = "get"
app:handle(req, res)
assert.is.equals(count, 100)
end)
describe("if finall handler defined, it will always be executed", function()
it("error middleware should continue the left error middlewares if has `next`.", function()
local count = 1
app:use("/user", function(req, res, next)
count = 2
next()
end)
app:use("/user/123", function(req, res, next)
count = 3
next()
end)
req.path = "/user/123/create"
req.method = "get"
app:handle(req, res, function(err)
count = 111
end)
assert.is.equals(count, 111)
end)
end)
end)