-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
67 lines (54 loc) · 1.72 KB
/
router_test.go
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
package peony
import (
"testing"
)
func TestRule(t *testing.T) {
router := NewRouter()
r := &Rule{Path: "/path/12<string:oo>-<int:m>"}
router.complieRule(r)
path := "/path/12-9090-123"
_, params := r.Match("GET", path)
t.Logf("%t\n", r.Build(params) == path)
r = &Rule{Path: "/path"}
err := router.complieRule(r)
if err != nil {
panic(err)
}
path = "/path"
_, params = r.Match("GET", path)
t.Logf("%t\n", r.Build(params) == path)
}
func TestRouter(t *testing.T) {
router := NewRouter()
r := &Rule{Path: "/path/12<string:p1>-<int:p2>", Action: "action.method1", HttpMethods: HttpMethods}
router.AddRule(r)
r = &Rule{Path: "/path/12<string:p1>-<int:p2>-", Action: "action.method2", HttpMethods: HttpMethods}
router.AddRule(r)
r = &Rule{Path: "/path/13<re(\\d{1,5}):p3>-<int:p2>--", Action: "action.method3", HttpMethods: HttpMethods}
router.AddRule(r)
r = &Rule{Path: "/path/15<p3>-<int:p2>--", Action: "action.method4", HttpMethods: HttpMethods}
router.AddRule(r)
r = &Rule{Path: "/path", Action: "static", HttpMethods: HttpMethods}
router.AddRule(r)
router.Refresh()
path := "/path/12-9090-123"
action, params := router.Match("GET", path)
t.Log(params)
_, p := router.Build(action, params)
t.Logf("%q, %t \n", action, p == path)
path = "/path"
action, params = router.Match("GET", path)
t.Log(params)
_, p = router.Build(action, params)
t.Logf("%q, %t\n", action, p == path)
path = "/path/12-9090-123-"
action, params = router.Match("GET", path)
t.Log(params)
_, p = router.Build(action, params)
t.Logf("%q, %t\n", action, p == path)
path = "/path/15909-123--"
action, params = router.Match("GET", path)
t.Log(params)
_, p = router.Build(action, params)
t.Logf("%q, %t, %q\n", action, p == path)
}