github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/route_mappings_test.go (about) 1 package buffalo 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 func Test_App_Routes_without_Root(t *testing.T) { 10 r := require.New(t) 11 12 a := New(Options{}) 13 r.Nil(a.root) 14 15 a.GET("/foo", voidHandler) 16 17 routes := a.Routes() 18 r.Len(routes, 1) 19 route := routes[0] 20 r.Equal("GET", route.Method) 21 r.Equal("/foo/", route.Path) 22 r.NotZero(route.HandlerName) 23 } 24 25 func Test_App_Routes_with_Root(t *testing.T) { 26 r := require.New(t) 27 28 a := New(Options{}) 29 r.Nil(a.root) 30 31 g := a.Group("/api/v1") 32 g.GET("/foo", voidHandler) 33 34 routes := a.Routes() 35 r.Len(routes, 1) 36 route := routes[0] 37 r.Equal("GET", route.Method) 38 r.Equal("/api/v1/foo/", route.Path) 39 r.NotZero(route.HandlerName) 40 41 r.Equal(a.Routes(), g.Routes()) 42 } 43 44 func Test_App_RouteName(t *testing.T) { 45 r := require.New(t) 46 47 a := New(Options{}) 48 49 cases := map[string]string{ 50 "cool": "coolPath", 51 "coolPath": "coolPath", 52 "coco_path": "cocoPath", 53 "ouch_something_cool": "ouchSomethingCoolPath", 54 } 55 56 ri := a.GET("/something", voidHandler) 57 for k, v := range cases { 58 ri.Name(k) 59 r.Equal(ri.PathName, v) 60 } 61 62 } 63 64 func Test_RouteList_Lookup(t *testing.T) { 65 r := require.New(t) 66 67 a := New(Options{}) 68 r.Nil(a.root) 69 70 a.GET("/foo", voidHandler) 71 a.GET("/test", voidHandler) 72 73 routes := a.Routes() 74 for _, route := range routes { 75 lRoute, err := routes.Lookup(route.PathName) 76 r.NoError(err) 77 r.Equal(lRoute, route) 78 } 79 lRoute, err := routes.Lookup("a") 80 r.Error(err) 81 r.Nil(lRoute) 82 83 } 84 85 func Test_App_RouteHelpers(t *testing.T) { 86 r := require.New(t) 87 88 a := New(Options{}) 89 r.Nil(a.root) 90 91 a.GET("/foo", voidHandler) 92 a.GET("/test/{id}", voidHandler) 93 94 rh := a.RouteHelpers() 95 96 r.Len(rh, 2) 97 98 f, ok := rh["fooPath"] 99 r.True(ok) 100 x, err := f(map[string]interface{}{}) 101 r.NoError(err) 102 r.Equal("/foo/", string(x)) 103 104 f, ok = rh["testPath"] 105 r.True(ok) 106 x, err = f(map[string]interface{}{ 107 "id": 1, 108 }) 109 r.NoError(err) 110 r.Equal("/test/1/", string(x)) 111 } 112 113 type resourceHandler struct{} 114 115 func (r resourceHandler) List(Context) error { 116 return nil 117 } 118 119 func (r resourceHandler) Show(Context) error { 120 return nil 121 } 122 123 func (r resourceHandler) Create(Context) error { 124 return nil 125 } 126 127 func (r resourceHandler) Update(Context) error { 128 return nil 129 } 130 131 func (r resourceHandler) Destroy(Context) error { 132 return nil 133 } 134 135 func Test_App_Routes_Resource(t *testing.T) { 136 r := require.New(t) 137 138 a := New(Options{}) 139 r.Nil(a.root) 140 141 a.GET("/foo", voidHandler) 142 a.Resource("/r", resourceHandler{}) 143 144 routes := a.Routes() 145 r.Len(routes, 6) 146 route := routes[0] 147 r.Equal("GET", route.Method) 148 r.Equal("/foo/", route.Path) 149 r.NotZero(route.HandlerName) 150 151 for k, v := range routes { 152 if k > 0 { 153 r.Equal("resourceHandler", v.ResourceName) 154 } 155 } 156 }