This repository was archived by the owner on Mar 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.lua
More file actions
58 lines (45 loc) · 1.76 KB
/
Copy pathtest.lua
File metadata and controls
58 lines (45 loc) · 1.76 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
local heart = require 'heart'
-- Test URL dispatching
local simple_routes, pattern_routes = {}, {}
local function register_route(route, handler)
local pattern, params = heart.route_parse(route, handler)
if params then
pattern_routes[pattern] = {handler, params}
else
simple_routes[pattern] = handler
end
end
local function dispatch_request(path)
local handler, params = heart.dispatch(path, simple_routes, pattern_routes)
assert(handler)
return handler(params)
end
function table_eq(a, b)
for i, v in ipairs(a) do
assert(b[i] == v)
end
return true
end
local function identity(x) return function() return x end end
-- simple url
register_route('/', function () return true end)
-- simple url with a pattern matching character in it
register_route('/hello/:name', function(params) return params.name end)
-- match a name
register_route('/$', function() return true end)
-- match a fancier route
register_route('/birthday/<name:%w%a*>/<age:%d+>', function(params) return {params.name, params.age } end)
-- same but with builtin types
register_route('/birthday2/<name:identifier>/<age:int>', function(params) return {params.name, params.age } end)
-- just try some stuff
register_route('/hey-hows-it-going ;)/', identity(true))
register_route('/static/<path:.+>', function(params) return params.path end)
assert(dispatch_request('/') == true)
assert(dispatch_request('/$') == true)
assert(dispatch_request('/hello/Billy') == 'Billy')
assert(table_eq(dispatch_request('/birthday/Jim/20'), {'Jim', '20'}))
assert(table_eq(dispatch_request('/birthday2/Jim/20'), {'Jim', '20'}))
assert(dispatch_request('/hey-hows-it-going ;)/'))
assert(dispatch_request('/static/file.txt') == 'file.txt')
assert(dispatch_request('/static/file/') == 'file/')
print('done testing!')