forked from qiangxue/fasthttp-routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_test.go
160 lines (138 loc) · 5.75 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
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
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package routing
import (
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type mockStore struct {
*store
data map[string]interface{}
}
func newMockStore() *mockStore {
return &mockStore{newStore(), make(map[string]interface{})}
}
func (s *mockStore) Add(key string, data interface{}) int {
for _, handler := range data.([]Handler) {
handler(nil)
}
return s.store.Add(key, data)
}
func TestRouteNew(t *testing.T) {
router := New()
group := newRouteGroup("/admin", router, nil)
r1 := newRoute("/users", group)
assert.Equal(t, "/admin/users", r1.name, "route.name =")
assert.Equal(t, "/admin/users", r1.path, "route.path =")
assert.Equal(t, "/admin/users", r1.template, "route.template =")
_, exists := router.routes[r1.name]
assert.True(t, exists, "router.routes[name] is ")
r2 := newRoute("/users/<id:\\d+>/*", group)
assert.Equal(t, "/admin/users/<id:\\d+>/*", r2.name, "route.name =")
assert.Equal(t, "/admin/users/<id:\\d+>/<:.*>", r2.path, "route.path =")
assert.Equal(t, "/admin/users/<id>/<>", r2.template, "route.template =")
_, exists = router.routes[r2.name]
assert.True(t, exists, "router.routes[name] is ")
}
func TestRouteName(t *testing.T) {
router := New()
group := newRouteGroup("/admin", router, nil)
r1 := newRoute("/users", group)
assert.Equal(t, "/admin/users", r1.name, "route.name =")
r1.Name("user")
assert.Equal(t, "user", r1.name, "route.name =")
_, exists := router.routes[r1.name]
assert.True(t, exists, "router.routes[name] is ")
}
func TestRouteURL(t *testing.T) {
router := New()
group := newRouteGroup("/admin", router, nil)
r := newRoute("/users/<id:\\d+>/<action>/*", group)
assert.Equal(t, "/admin/users/123/address/<>", r.URL("id", 123, "action", "address"), "Route.URL@1 =")
assert.Equal(t, "/admin/users/123/<action>/<>", r.URL("id", 123), "Route.URL@2 =")
assert.Equal(t, "/admin/users/123//<>", r.URL("id", 123, "action"), "Route.URL@3 =")
assert.Equal(t, "/admin/users/123/profile/", r.URL("id", 123, "action", "profile", ""), "Route.URL@4 =")
assert.Equal(t, "/admin/users/123/profile/xyz%2Fabc", r.URL("id", 123, "action", "profile", "", "xyz/abc"), "Route.URL@5 =")
assert.Equal(t, "/admin/users/123/a%2C%3C%3E%3F%23/<>", r.URL("id", 123, "action", "a,<>?#"), "Route.URL@6 =")
}
func newHandler(tag string, buf *bytes.Buffer) Handler {
return func(*Context) error {
fmt.Fprintf(buf, tag)
return nil
}
}
func TestRouteAdd(t *testing.T) {
store := newMockStore()
router := New()
router.stores["GET"] = store
assert.Equal(t, 0, store.count, "router.stores[GET].count =")
var buf bytes.Buffer
group := newRouteGroup("/admin", router, []Handler{newHandler("1.", &buf), newHandler("2.", &buf)})
newRoute("/users", group).Get(newHandler("3.", &buf), newHandler("4.", &buf))
assert.Equal(t, "1.2.3.4.", buf.String(), "buf@1 =")
buf.Reset()
group = newRouteGroup("/admin", router, []Handler{})
newRoute("/users", group).Get(newHandler("3.", &buf), newHandler("4.", &buf))
assert.Equal(t, "3.4.", buf.String(), "buf@2 =")
buf.Reset()
group = newRouteGroup("/admin", router, []Handler{newHandler("1.", &buf), newHandler("2.", &buf)})
newRoute("/users", group).Get()
assert.Equal(t, "1.2.", buf.String(), "buf@3 =")
}
func TestRouteMethods(t *testing.T) {
router := New()
for _, method := range Methods {
store := newMockStore()
router.stores[method] = store
assert.Equal(t, 0, store.count, "router.stores["+method+"].count =")
}
group := newRouteGroup("/admin", router, nil)
newRoute("/users", group).Get()
assert.Equal(t, 1, router.stores["GET"].(*mockStore).count, "router.stores[GET].count =")
newRoute("/users", group).Post()
assert.Equal(t, 1, router.stores["POST"].(*mockStore).count, "router.stores[POST].count =")
newRoute("/users", group).Patch()
assert.Equal(t, 1, router.stores["PATCH"].(*mockStore).count, "router.stores[PATCH].count =")
newRoute("/users", group).Put()
assert.Equal(t, 1, router.stores["PUT"].(*mockStore).count, "router.stores[PUT].count =")
newRoute("/users", group).Delete()
assert.Equal(t, 1, router.stores["DELETE"].(*mockStore).count, "router.stores[DELETE].count =")
newRoute("/users", group).Connect()
assert.Equal(t, 1, router.stores["CONNECT"].(*mockStore).count, "router.stores[CONNECT].count =")
newRoute("/users", group).Head()
assert.Equal(t, 1, router.stores["HEAD"].(*mockStore).count, "router.stores[HEAD].count =")
newRoute("/users", group).Options()
assert.Equal(t, 1, router.stores["OPTIONS"].(*mockStore).count, "router.stores[OPTIONS].count =")
newRoute("/users", group).Trace()
assert.Equal(t, 1, router.stores["TRACE"].(*mockStore).count, "router.stores[TRACE].count =")
newRoute("/posts", group).To("GET,POST")
assert.Equal(t, 2, router.stores["GET"].(*mockStore).count, "router.stores[GET].count =")
assert.Equal(t, 2, router.stores["POST"].(*mockStore).count, "router.stores[POST].count =")
assert.Equal(t, 1, router.stores["PUT"].(*mockStore).count, "router.stores[PUT].count =")
}
func TestBuildURLTemplate(t *testing.T) {
tests := []struct {
path, expected string
}{
{"", ""},
{"/users", "/users"},
{"<id>", "<id>"},
{"<id", "<id"},
{"/users/<id>", "/users/<id>"},
{"/users/<id:\\d+>", "/users/<id>"},
{"/users/<:\\d+>", "/users/<>"},
{"/users/<id>/xyz", "/users/<id>/xyz"},
{"/users/<id:\\d+>/xyz", "/users/<id>/xyz"},
{"/users/<id:\\d+>/<test>", "/users/<id>/<test>"},
{"/users/<id:\\d+>/<test>/", "/users/<id>/<test>/"},
{"/users/<id:\\d+><test>", "/users/<id><test>"},
{"/users/<id:\\d+><test>/", "/users/<id><test>/"},
}
for _, test := range tests {
actual := buildURLTemplate(test.path)
assert.Equal(t, test.expected, actual, "buildURLTemplate("+test.path+") =")
}
}