-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathnot_found_spec.lua
executable file
·125 lines (108 loc) · 3.41 KB
/
not_found_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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("not found test, error middleware and final handler should be reached correctly.", function()
it("test case 1", function()
local count = 0
local errorMsg = "an error occurs."
local userRouter = lor:Router()
userRouter:get("/find/:id", function(req, res, next)
count = 1
error(errorMsg)
end)
app:use("/user", userRouter())
app:erroruse("/user", function(err, req, res, next)
count = err
req.params.id = "2222"
next(err)
end)
app:erroruse(function(err, req, res, next)
count = err
req.params.id = "1111"
end)
req.path = "/user/find/456"
req.method = "get"
app:handle(req, res)
assert.is.equals(req.params.id, "1111")
assert.is.equals(true, string.find(count, errorMsg) > 1)
end)
it("test case 2", function()
local count = 0
local errorMsg = "an error occurs."
local userRouter = lor:Router()
userRouter:get("/find/:id", function(req, res, next)
count = 1
error(errorMsg)
end)
app:use("/user", userRouter())
app:erroruse("/user", function(err, req, res, next)
count = err
req.params.id = "2222"
next(err)
end)
app:erroruse(function(err, req, res, next)
count = "stop exec final handler"
req.params.id = "1111"
-- next(err) -- not invoke it, the final handler will not be reached!
end)
req.params.id = nil --empty it
req.path = "/user/notfound"
req.method = "post"
app:handle(req, res, function(err)
if err then
count = "not found error catched"
end
end)
assert.is.equals(404, res.http_status)
assert.is.equals(req.params.id, "1111")
assert.is.equals("stop exec final handler", count)
end)
it("test case 3", function()
local count = 0
local errorMsg = "an error occurs."
local userRouter = lor:Router()
userRouter:get("/find/:id", function(req, res, next)
count = 1
error(errorMsg)
end)
app:use("/user", userRouter())
app:erroruse("/user", function(err, req, res, next)
count = err
req.params.id = "2222"
next(err)
end)
app:erroruse(function(err, req, res, next)
count = "stop exec final handler"
req.params.id = "1111"
next(err) -- invoke it, the final handler will be reached!
end)
req.params.id = nil --empty it
req.path = "/notfound"
req.method = "post"
app:handle(req, res, function(err)
req.params.id = "3333"
if err then
count = "not found error catched"
end
end)
--print(app.router.trie:gen_graph())
assert.is.equals(404, res.http_status)
assert.is.equals(req.params.id, "3333")
assert.is.equals("not found error catched", count)
end)
end)