This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 577
/
route_mappings_test.go
213 lines (160 loc) · 3.79 KB
/
route_mappings_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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package buffalo
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_App_Routes_without_Root(t *testing.T) {
r := require.New(t)
a := New(Options{})
r.Nil(a.root)
a.GET("/foo", voidHandler)
routes := a.Routes()
r.Len(routes, 1)
route := routes[0]
r.Equal("GET", route.Method)
r.Equal("/foo/", route.Path)
r.NotZero(route.HandlerName)
}
func Test_App_Routes_with_Root(t *testing.T) {
r := require.New(t)
a := New(Options{})
r.Nil(a.root)
g := a.Group("/api/v1")
g.GET("/foo", voidHandler)
routes := a.Routes()
r.Len(routes, 1)
route := routes[0]
r.Equal("GET", route.Method)
r.Equal("/api/v1/foo/", route.Path)
r.NotZero(route.HandlerName)
r.Equal(a.Routes(), g.Routes())
}
func Test_App_RouteName(t *testing.T) {
r := require.New(t)
a := New(Options{})
cases := map[string]string{
"cool": "coolPath",
"coolPath": "coolPath",
"coco_path": "cocoPath",
"ouch_something_cool": "ouchSomethingCoolPath",
}
ri := a.GET("/something", voidHandler)
for k, v := range cases {
ri.Name(k)
r.Equal(ri.PathName, v)
}
}
func Test_RouteList_Lookup(t *testing.T) {
r := require.New(t)
a := New(Options{})
r.Nil(a.root)
a.GET("/foo", voidHandler)
a.GET("/test", voidHandler)
routes := a.Routes()
for _, route := range routes {
lRoute, err := routes.Lookup(route.PathName)
r.NoError(err)
r.Equal(lRoute, route)
}
lRoute, err := routes.Lookup("a")
r.Error(err)
r.Nil(lRoute)
}
func Test_App_RouteHelpers(t *testing.T) {
r := require.New(t)
a := New(Options{})
r.Nil(a.root)
a.GET("/foo", voidHandler)
a.GET("/test/{id}", voidHandler)
rh := a.RouteHelpers()
r.Len(rh, 2)
f, ok := rh["fooPath"]
r.True(ok)
x, err := f(map[string]interface{}{})
r.NoError(err)
r.Equal("/foo/", string(x))
f, ok = rh["testPath"]
r.True(ok)
x, err = f(map[string]interface{}{
"id": 1,
})
r.NoError(err)
r.Equal("/test/1/", string(x))
}
type resourceHandler struct{}
func (r resourceHandler) List(Context) error {
return nil
}
func (r resourceHandler) Show(Context) error {
return nil
}
func (r resourceHandler) Create(Context) error {
return nil
}
func (r resourceHandler) Update(Context) error {
return nil
}
func (r resourceHandler) Destroy(Context) error {
return nil
}
func Test_App_Routes_Resource(t *testing.T) {
r := require.New(t)
a := New(Options{})
r.Nil(a.root)
a.GET("/foo", voidHandler)
a.Resource("/r", resourceHandler{})
routes := a.Routes()
r.Len(routes, 6)
route := routes[0]
r.Equal("GET", route.Method)
r.Equal("/foo/", route.Path)
r.NotZero(route.HandlerName)
for k, v := range routes {
if k > 0 {
r.Equal("resourceHandler", v.ResourceName)
}
}
}
func Test_App_VirtualHost(t *testing.T) {
r := require.New(t)
a1 := New(Options{})
r.Nil(a1.root)
h1 := a1.VirtualHost("www.example.com")
h1.GET("/foo", voidHandler)
routes := h1.Routes()
r.Len(routes, 1)
route := routes[0]
r.Equal("GET", route.Method)
r.Equal("/foo/", route.Path)
r.NotZero(route.HandlerName)
// With Regular Expressions
a2 := New(Options{})
r.Nil(a1.root)
h2 := a2.VirtualHost("{subdomain}.example.com")
h2.GET("/foo", voidHandler)
h2.GET("/foo/{id}", voidHandler).Name("fooID")
rh := h2.RouteHelpers()
routes = h2.Routes()
r.Len(routes, 2)
r.Equal("GET", routes[0].Method)
r.Equal("/foo/", routes[0].Path)
r.NotZero(routes[0].HandlerName)
r.Equal("GET", routes[1].Method)
r.Equal("/foo/{id}/", routes[1].Path)
r.NotZero(routes[1].HandlerName)
f, ok := rh["fooPath"]
r.True(ok)
x, err := f(map[string]interface{}{
"subdomain": "test",
})
r.NoError(err)
r.Equal("http://test.example.com/foo/", string(x))
f, ok = rh["fooIDPath"]
r.True(ok)
x, err = f(map[string]interface{}{
"subdomain": "test",
"id": 1,
})
r.NoError(err)
r.Equal("http://test.example.com/foo/1/", string(x))
}