forked from celrenheit/lion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_test.go
95 lines (79 loc) · 2.48 KB
/
route_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
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
package lion
import "testing"
func TestRouteGeneratePath(t *testing.T) {
l := New()
register := []struct {
pattern, name string
}{
{"/a/:name", "a_name"},
{"/a/:name/:n([0-9]+)", "a_name_n"},
{"/a/b/:dest/*path", "a_b_dest_path"},
{"/e/:file.:ext", "e_file_ext"},
}
for _, r := range register {
l.Get(r.pattern, fakeHandler()).WithName(r.name)
}
tests := []struct {
routename string
params map[string]string
expectedPath string
expectedErr bool
}{
{routename: "a_name", params: mss{"name": "batman"}, expectedPath: "/a/batman"},
{routename: "a_name_n", params: mss{"name": "batman", "n": "123"}, expectedPath: "/a/batman/123"},
{routename: "a_name_n", params: mss{"name": "batman", "n": "1d23"}, expectedErr: true},
{routename: "a_b_dest_path", params: mss{"dest": "batman", "path": "subfolder/test/hello.jpeg"}, expectedPath: "/a/b/batman/subfolder/test/hello.jpeg"},
{routename: "e_file_ext", params: mss{"file": "test", "ext": "mp4"}, expectedPath: "/e/test.mp4"},
}
for _, test := range tests {
path, err := l.Route(test.routename).Path(test.params)
// Test with RoutePathBuilder
builder := l.Route(test.routename).Build()
for k, v := range test.params {
builder.WithParam(k, v)
}
if builtpath, _ := builder.Path(); path != builtpath {
t.Errorf("RoutePathBuilder's path should be the name as Path")
}
if test.expectedErr && err == nil {
t.Errorf("Should have errored")
}
if !test.expectedErr && err != nil {
t.Error(err)
}
if path != test.expectedPath {
t.Errorf("Incorrect path: got '%s' want '%s'", path, test.expectedPath)
}
}
}
func TestGetRoutesSubrouter(t *testing.T) {
l := New()
l.Get("/hello", fakeHandler())
api := l.Group("/api")
api.Get("/users", fakeHandler())
api.Get("/posts", fakeHandler())
api.Get("/sessions", fakeHandler())
got := len(l.Routes())
if got != 4 {
t.Errorf("Number of routes should be 4 but got %d: %v", got, l.Routes())
}
got = len(api.Routes())
if got != 3 {
t.Errorf("Number of routes should be 3 but got %d: %v", got, api.Routes())
}
lv2 := New()
lv2.Get("/users2", fakeHandler())
lv2.Get("/posts2", fakeHandler())
lv2.Get("/sessions2", fakeHandler())
l.Mount("/v2", lv2)
got = len(l.Routes())
if got != 7 {
t.Errorf("Number of routes should be 7 but got %d: %v", got, l.Routes())
}
lv2.Get("/categories", fakeHandler())
l.Mount("/v2", lv2)
got = len(l.Routes())
if got != 8 {
t.Errorf("Number of routes should be 8 but got %d: %v", got, l.Routes())
}
}