forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_mappings_test.go
156 lines (117 loc) · 2.66 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
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)
}
}
}