github.com/jasonish/buffalo@v0.8.2-0.20170413145823-bacbdd415f1b/router_test.go (about) 1 package buffalo 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "net/http/httptest" 8 "path/filepath" 9 "testing" 10 11 "github.com/gobuffalo/buffalo/render" 12 "github.com/markbates/willie" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func testApp() *App { 17 a := New(Options{}) 18 a.Redirect(301, "/foo", "/bar") 19 a.GET("/bar", func(c Context) error { 20 return c.Render(200, render.String("bar")) 21 }) 22 23 rt := a.Group("/router/tests") 24 25 h := func(c Context) error { 26 return c.Render(200, render.String(c.Request().Method+"|"+c.Value("current_path").(string))) 27 } 28 29 rt.GET("/", h) 30 rt.POST("/", h) 31 rt.PUT("/", h) 32 rt.DELETE("/", h) 33 rt.OPTIONS("/", h) 34 rt.PATCH("/", h) 35 return a 36 } 37 38 func Test_Router(t *testing.T) { 39 r := require.New(t) 40 41 table := []string{ 42 "GET", 43 "POST", 44 "PUT", 45 "DELETE", 46 "OPTIONS", 47 "PATCH", 48 } 49 50 ts := httptest.NewServer(testApp()) 51 defer ts.Close() 52 53 for _, v := range table { 54 req, err := http.NewRequest(v, fmt.Sprintf("%s/router/tests", ts.URL), nil) 55 r.NoError(err) 56 res, err := http.DefaultClient.Do(req) 57 r.NoError(err) 58 b, _ := ioutil.ReadAll(res.Body) 59 r.Equal(fmt.Sprintf("%s|/router/tests", v), string(b)) 60 } 61 } 62 63 func Test_Router_Group(t *testing.T) { 64 r := require.New(t) 65 66 a := testApp() 67 g := a.Group("/api/v1") 68 g.GET("/users", func(c Context) error { 69 return c.Render(201, nil) 70 }) 71 72 w := willie.New(a) 73 res := w.Request("/api/v1/users").Get() 74 r.Equal(201, res.Code) 75 } 76 77 func Test_Router_Group_on_Group(t *testing.T) { 78 r := require.New(t) 79 80 a := testApp() 81 g := a.Group("/api/v1") 82 g.GET("/users", func(c Context) error { 83 return c.Render(201, nil) 84 }) 85 f := g.Group("/foo") 86 f.GET("/bar", func(c Context) error { 87 return c.Render(420, nil) 88 }) 89 90 w := willie.New(a) 91 res := w.Request("/api/v1/foo/bar").Get() 92 r.Equal(420, res.Code) 93 } 94 95 func Test_Router_Group_Middleware(t *testing.T) { 96 r := require.New(t) 97 98 a := testApp() 99 a.Use(func(h Handler) Handler { return h }) 100 r.Len(a.Middleware.stack, 1) 101 102 g := a.Group("/api/v1") 103 r.Len(a.Middleware.stack, 1) 104 r.Len(g.Middleware.stack, 1) 105 106 g.Use(func(h Handler) Handler { return h }) 107 r.Len(a.Middleware.stack, 1) 108 r.Len(g.Middleware.stack, 2) 109 } 110 111 func Test_Router_Redirect(t *testing.T) { 112 r := require.New(t) 113 w := willie.New(testApp()) 114 res := w.Request("/foo").Get() 115 r.Equal(301, res.Code) 116 r.Equal("/bar", res.Location()) 117 } 118 119 func Test_Router_ServeFiles(t *testing.T) { 120 r := require.New(t) 121 122 tmpFile, err := ioutil.TempFile("", "assets") 123 r.NoError(err) 124 125 af := []byte("hi") 126 _, err = tmpFile.Write(af) 127 r.NoError(err) 128 129 a := New(Options{}) 130 a.ServeFiles("/assets", http.Dir(filepath.Dir(tmpFile.Name()))) 131 132 w := willie.New(a) 133 res := w.Request("/assets/%s", filepath.Base(tmpFile.Name())).Get() 134 135 r.Equal(200, res.Code) 136 r.Equal(af, res.Body.Bytes()) 137 } 138 139 func Test_Resource(t *testing.T) { 140 r := require.New(t) 141 142 type trs struct { 143 Method string 144 Path string 145 Result string 146 } 147 148 tests := []trs{ 149 { 150 Method: "GET", 151 Path: "", 152 Result: "list", 153 }, 154 { 155 Method: "GET", 156 Path: "/new", 157 Result: "new", 158 }, 159 { 160 Method: "GET", 161 Path: "/1", 162 Result: "show 1", 163 }, 164 { 165 Method: "GET", 166 Path: "/1/edit", 167 Result: "edit 1", 168 }, 169 { 170 Method: "POST", 171 Path: "", 172 Result: "create", 173 }, 174 { 175 Method: "PUT", 176 Path: "/1", 177 Result: "update 1", 178 }, 179 { 180 Method: "DELETE", 181 Path: "/1", 182 Result: "destroy 1", 183 }, 184 } 185 186 a := Automatic(Options{}) 187 a.Resource("/users", &userResource{}) 188 a.Resource("/api/v1/users", &userResource{}) 189 190 ts := httptest.NewServer(a) 191 defer ts.Close() 192 193 c := http.Client{} 194 for _, path := range []string{"/users", "/api/v1/users"} { 195 for _, test := range tests { 196 u := ts.URL + path + test.Path 197 req, err := http.NewRequest(test.Method, u, nil) 198 r.NoError(err) 199 res, err := c.Do(req) 200 r.NoError(err) 201 b, err := ioutil.ReadAll(res.Body) 202 r.NoError(err) 203 r.Equal(test.Result, string(b)) 204 } 205 } 206 207 } 208 209 type userResource struct{} 210 211 func (u *userResource) List(c Context) error { 212 return c.Render(200, render.String("list")) 213 } 214 215 func (u *userResource) Show(c Context) error { 216 return c.Render(200, render.String(`show <%=params["user_id"] %>`)) 217 } 218 219 func (u *userResource) New(c Context) error { 220 return c.Render(200, render.String("new")) 221 } 222 223 func (u *userResource) Create(c Context) error { 224 return c.Render(200, render.String("create")) 225 } 226 227 func (u *userResource) Edit(c Context) error { 228 return c.Render(200, render.String(`edit <%=params["user_id"] %>`)) 229 } 230 231 func (u *userResource) Update(c Context) error { 232 return c.Render(200, render.String(`update <%=params["user_id"] %>`)) 233 } 234 235 func (u *userResource) Destroy(c Context) error { 236 return c.Render(200, render.String(`destroy <%=params["user_id"] %>`)) 237 }