-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathrouter_test.go
175 lines (156 loc) · 5.74 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package golf
import (
"testing"
)
func assertStringEqual(t *testing.T, expected, got string) {
if expected != got {
t.Errorf("Expected %v, got %v", expected, got)
}
}
func assertSliceEqual(t *testing.T, expected, got []string) {
if len(expected) != len(got) {
t.Errorf("Slice length not equal, expected: %v, got %v", expected, got)
}
for i := 0; i < len(expected); i++ {
if expected[i] != got[i] {
t.Errorf("Slice not equal, expected: %v, got %v", expected, got)
}
}
}
type route struct {
method string
path string
testPath string
params map[string]string
}
var githubAPI = []route{
// OAuth Authorizations
{"GET", "/authorizations", "/authorizations", map[string]string{}},
{"GET", "/auth", "/auth", map[string]string{}},
{"GET", "/authorizations/:id", "/authorizations/12345", map[string]string{"id": "12345"}},
{"POST", "/authorizations", "/authorizations", map[string]string{}},
{"DELETE", "/authorizations/:id", "/authorizations/12345", map[string]string{"id": "12345"}},
{"GET", "/applications/:client_id/tokens/:access_token", "/applications/12345/tokens/67890", map[string]string{"client_id": "12345", "access_token": "67890"}},
{"DELETE", "/applications/:client_id/tokens", "/applications/12345/tokens", map[string]string{"client_id": "12345"}},
{"DELETE", "/applications/:client_id/tokens/:access_token", "/applications/12345/tokens/67890", map[string]string{"client_id": "12345", "access_token": "67890"}},
// Activity
{"GET", "/events", "/events", nil},
{"GET", "/repos/:owner/:repo/events", "/repos/dinever/golf/events", map[string]string{"owner": "dinever", "repo": "golf"}},
{"GET", "/networks/:owner/:repo/events", "/networks/dinever/golf/events", map[string]string{"owner": "dinever", "repo": "golf"}},
{"GET", "/orgs/:org/events", "/orgs/golf/events", map[string]string{"org": "golf"}},
{"GET", "/users/:user/received_events", "/users/dinever/received_events", nil},
{"GET", "/users/:user/received_events/public", "/users/dinever/received_events/public", nil},
}
func handler(ctx *Context) {
}
func TestRouter(t *testing.T) {
router := newRouter()
for _, route := range githubAPI {
router.AddRoute(route.method, route.path, handler)
}
for _, route := range githubAPI {
_, param, err := router.FindRoute(route.method, route.testPath)
if err != nil {
t.Errorf("Can not find route: %v", route.testPath)
}
for key, expected := range route.params {
val, err := param.ByName(key)
if err != nil {
t.Errorf("Can not retrieve parameter from route %v: %v", route.testPath, key)
} else {
assertStringEqual(t, expected, val)
}
val, err = param.ByName(key)
if err != nil {
t.Errorf("Can not retrieve parameter from route %v: %v", route.testPath, key)
} else {
assertStringEqual(t, expected, val)
}
}
}
}
func TestSplitURLPath(t *testing.T) {
var table = map[string][2][]string{
"/users/:name": {{"/users/", ":"}, {"name"}},
"/users/:name/put": {{"/users/", ":", "/put"}, {"name"}},
"/users/:name/put/:section": {{"/users/", ":", "/put/", ":"}, {"name", "section"}},
"/customers/:name/put/:section": {{"/customers/", ":", "/put/", ":"}, {"name", "section"}},
"/customers/groups/:name/put/:section": {{"/customers/groups/", ":", "/put/", ":"}, {"name", "section"}},
}
for path, result := range table {
parts, _ := splitURLPath(path)
assertSliceEqual(t, parts, result[0])
}
}
func TestIncorrectPath(t *testing.T) {
path := "/users/foo:name/"
defer func() {
if err := recover(); err != nil {
}
}()
router := newRouter()
router.AddRoute("GET", path, handler)
t.Errorf("Incorrect path should raise an error.")
}
func TestPathNotFound(t *testing.T) {
path := []struct {
method, path, incomingMethod, incomingPath string
}{
{"GET", "/users/name/", "GET", "/users/name/dinever/"},
{"GET", "/dinever/repo/", "POST", "/dinever/repo/"},
}
defer func() {
if err := recover(); err != nil {
}
}()
router := newRouter()
for _, path := range path {
router.AddRoute(path.method, path.path, handler)
h, p, err := router.FindRoute(path.incomingMethod, path.incomingPath)
if h != nil {
t.Errorf("Should return nil handler when path not found.")
}
if p.Len() != 0 {
t.Errorf("Should return nil parameter when path not found.")
}
if err == nil {
t.Errorf("Should rasie an error when path not found.")
}
}
}
func TestRouterWithOptionalEndingSlash(t *testing.T) {
var cases = []route{
// Path with optional ending slash
{"GET", "/", "/", nil},
{"GET", "/events", "/events/", nil},
{"GET", "/repos/:owner/:repo/events", "/repos/dinever/golf/events/", map[string]string{"owner": "dinever", "repo": "golf"}},
{"GET", "/networks/:owner/:repo/events/", "/networks/dinever/golf/events", map[string]string{"owner": "dinever", "repo": "golf"}},
{"GET", "/orgs/:org/events/", "/orgs/golf/events", map[string]string{"org": "golf"}},
{"GET", "/users/:user/received_events", "/users/dinever/received_events/", nil},
{"GET", "/users/:user/received_events/public", "/users/dinever/received_events/public", nil},
}
router := newRouter()
for _, route := range cases {
router.AddRoute(route.method, route.path, handler)
}
for _, route := range cases {
_, param, err := router.FindRoute(route.method, route.testPath)
if err != nil {
t.Errorf("Can not find route: %v", route.testPath)
}
for key, expected := range route.params {
val, err := param.ByName(key)
if err != nil {
t.Errorf("Can not retrieve parameter from route %v: %v", route.testPath, key)
} else {
assertStringEqual(t, expected, val)
}
val, err = param.ByName(key)
if err != nil {
t.Errorf("Can not retrieve parameter from route %v: %v", route.testPath, key)
} else {
assertStringEqual(t, expected, val)
}
}
}
}