forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_mappings_test.go
111 lines (83 loc) · 1.91 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
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))
}