forked from sumory/lor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_pattern_1_spec.lua
executable file
·113 lines (96 loc) · 2.59 KB
/
path_pattern_1_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
before_each(function()
lor = _G.lor
app = lor({
debug = false
})
Request = _G.request
Response = _G.response
req = Request:new()
res = Response:new()
count = 0
app:use("/", function(req, res, next)
count = 1
next()
end)
app:use("/user/", function(req, res, next)
count = 2
next()
end)
app:use("/user/:id/view", function(req, res, next)
count = 3
next()
end)
app:get("/user/123/view", function(req, res, next)
count = 4
next()
end)
app:post("/book" , function(req, res, next)
count = 5
next()
end)
local testRouter = lor:Router() -- 一个新的router,区别于主router
testRouter:get("/get", function(req, res, next)
count = 6
next()
end)
testRouter:post("/foo/bar", function(req, res, next)
count = 7
next()
end)
app:use("/test", testRouter())
app:erroruse(function(err, req, res, next)
count = 999
end)
end)
after_each(function()
lor = nil
app = nil
Request = nil
Response = nil
req = nil
res = nil
end)
describe("next function usages test", function()
it("test case 1", function()
req.path = "/user/123/view"
req.method = "get"
app:handle(req, res, function(err)
assert.is_true(req:is_found())
end)
assert.is.equals(4, count)
assert.is.equals(nil, req.params.id)
end)
it("test case 2", function() -- route found
app:conf("strict_route", false) -- 设置为非严格匹配
req.path = "/user/123/view/" -- match app:get("/user/123/view", fn())
req.method = "get"
app:handle(req, res, function(err)
assert.is_true(req:is_found())
end)
assert.is.equals(4, count)
assert.is.equals(nil, req.params.id)
end)
it("test case 3", function()
req.path = "/book"
req.method = "get"
app:handle(req, res)
assert.is.equals(999, count)
assert.is_nil( req.params.id)
req.method = "post" -- post match
app:handle(req, res, function(err)
assert.is_true(req:is_found())
end)
assert.is.equals(5, count)
assert.is_nil( req.params.id)
end)
it("test case 4", function()
req.path = "/notfound"
req.method = "get"
app:handle(req, res, function(err)
assert.is_not_true(req:is_found())
assert.is_nil(err)
end)
assert.is.equals(999, count)
assert.is_nil(req.params.id)
end)
end)