-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.lua
138 lines (112 loc) · 2.71 KB
/
app.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
126
127
128
129
130
131
132
133
134
135
136
137
138
local Router = require('resty.router')
local router = Router:new()
local cnt = 0
local error_cnt = 0
-- Test events
router:on('add', function(ctx)
cnt = cnt + 1
end)
router:on('error', function(ctx)
error_cnt = error_cnt + 1
end)
-- Test plugins
router:use(function(ctx)
ctx.cnt = cnt
ctx.error_cnt = error_cnt
end)
-- 1. Test static path
router:get("/hello", function()
return "Hello World"
end)
-- Test custom success status code
router:get("/hello-201", function()
return "Hello World", 201
end)
-- 2. Test JSON response
router:get("/json", function()
return { message = "success", code = 0 }
end)
-- 3. Test dynamic path parameters
router:get("/users/#id", function(ctx)
return {
id = ctx.params.id,
type = type(ctx.params.id)
}
end)
router:get("/users/:name", function(ctx)
return {
name = ctx.params.name,
type = type(ctx.params.name)
}
end)
-- 4. Test regex path
router:get([[/version/<ver>\d+\.\d+]], function(ctx)
return {
version = ctx.params.ver
}
end)
-- 5. Test wildcard
router:get("/files/*path", function(ctx)
return ctx.params.path
end)
-- 6. Test other HTTP methods
router:post("/accounts", function(ctx)
return { method = "POST" }
end)
router:put("/accounts/#id", function(ctx)
return { method = "PUT", id = ctx.params.id }
end)
-- 7. Test error handling
router:get("/error", function()
error("Test Error")
end)
-- Test custom error thrown by error()
router:get("/custom-error", function()
error({ code = 400, message = "Custom Error" })
end)
-- Test error in the form of return nil, err, code
router:get("/return-error", function()
return nil, "Parameter Error", 402
end)
-- Test handled error
router:get("/handled-error", function()
error { "handled error" }
end)
-- 8. Test status code
router:get("/404", function()
return nil, "Not Found", 404
end)
-- 9. Test HTML response
router:get("/html", function()
return "<h1>Hello HTML</h1>"
end)
-- Test HTML error
router:get("/html-error", function()
return nil, "<h1>Hello HTML error</h1>", 501
end)
-- Test HTML error thrown by error()
router:get("/html-error2", function()
error { "<h1>Hello HTML error2</h1>" }
end)
-- 10. Test function return
router:get("/func", function(ctx)
return function()
ngx.header.content_type = 'text/plain; charset=utf-8'
ngx.print("function called")
end
end)
-- 11. Check if events are executing properly
router:get("/add", function(ctx)
router:emit('add')
return ctx.cnt
end)
router:get("/events", function(ctx)
return ctx.cnt
end)
-- 12. ctx.response.body
router:get("/response-body", function(ctx)
ctx.response.body = "response body"
end)
router:fs("./api")
assert(io.open("logs/router.log", "w")):write(require("resty.repr")(router)):close()
return router