github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/render/auto_test.go (about) 1 package render_test 2 3 import ( 4 "net/http" 5 "strings" 6 "testing" 7 8 "github.com/gobuffalo/buffalo" 9 "github.com/gobuffalo/buffalo/render" 10 "github.com/gobuffalo/httptest" 11 "github.com/gobuffalo/packd" 12 "github.com/stretchr/testify/require" 13 ) 14 15 type Car struct { 16 ID int 17 Name string 18 } 19 20 type Cars []Car 21 22 func Test_Auto_DefaultContentType(t *testing.T) { 23 r := require.New(t) 24 25 re := render.New(render.Options{ 26 DefaultContentType: "application/json", 27 }) 28 29 app := buffalo.New(buffalo.Options{}) 30 app.GET("/cars", func(c buffalo.Context) error { 31 return c.Render(http.StatusOK, re.Auto(c, []string{"Honda", "Toyota", "Ford", "Chevy"})) 32 }) 33 34 res := httptest.NewRecorder() 35 req := httptest.NewRequest("GET", "/cars", nil) 36 app.ServeHTTP(res, req) 37 38 r.Equal(`["Honda","Toyota","Ford","Chevy"]`, strings.TrimSpace(res.Body.String())) 39 } 40 41 func Test_Auto_JSON(t *testing.T) { 42 r := require.New(t) 43 44 re := render.New(render.Options{}) 45 app := buffalo.New(buffalo.Options{}) 46 app.GET("/cars", func(c buffalo.Context) error { 47 return c.Render(http.StatusOK, re.Auto(c, []string{"Honda", "Toyota", "Ford", "Chevy"})) 48 }) 49 50 w := httptest.New(app) 51 52 res := w.JSON("/cars").Get() 53 r.Equal(`["Honda","Toyota","Ford","Chevy"]`, strings.TrimSpace(res.Body.String())) 54 } 55 56 func Test_Auto_XML(t *testing.T) { 57 r := require.New(t) 58 59 re := render.New(render.Options{}) 60 app := buffalo.New(buffalo.Options{}) 61 app.GET("/cars", func(c buffalo.Context) error { 62 return c.Render(http.StatusOK, re.Auto(c, []string{"Honda", "Toyota", "Ford", "Chevy"})) 63 }) 64 65 w := httptest.New(app) 66 67 res := w.XML("/cars").Get() 68 r.Equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<string>Honda</string>\n<string>Toyota</string>\n<string>Ford</string>\n<string>Chevy</string>", strings.TrimSpace(res.Body.String())) 69 } 70 71 func Test_Auto_HTML_List(t *testing.T) { 72 r := require.New(t) 73 74 box := packd.NewMemoryBox() 75 err := box.AddString("cars/index.html", "INDEX: <%= len(cars) %>") 76 r.NoError(err) 77 78 re := render.New(render.Options{ 79 TemplatesBox: box, 80 }) 81 82 app := buffalo.New(buffalo.Options{}) 83 app.GET("/cars", func(c buffalo.Context) error { 84 return c.Render(http.StatusOK, re.Auto(c, Cars{ 85 {Name: "Ford"}, 86 {Name: "Chevy"}, 87 })) 88 }) 89 90 w := httptest.New(app) 91 res := w.HTML("/cars").Get() 92 93 r.Contains(res.Body.String(), "INDEX: 2") 94 } 95 96 func Test_Auto_HTML_List_Plural(t *testing.T) { 97 r := require.New(t) 98 99 type Person struct { 100 Name string 101 } 102 103 type People []Person 104 105 box := packd.NewMemoryBox() 106 err := box.AddString("people/index.html", "INDEX: <%= len(people) %>") 107 r.NoError(err) 108 109 re := render.New(render.Options{ 110 TemplatesBox: box, 111 }) 112 113 app := buffalo.New(buffalo.Options{}) 114 app.GET("/people", func(c buffalo.Context) error { 115 return c.Render(http.StatusOK, re.Auto(c, People{ 116 Person{Name: "Ford"}, 117 Person{Name: "Chevy"}, 118 })) 119 }) 120 121 w := httptest.New(app) 122 res := w.HTML("/people").Get() 123 124 r.Contains(res.Body.String(), "INDEX: 2") 125 } 126 127 func Test_Auto_HTML_Show(t *testing.T) { 128 r := require.New(t) 129 130 box := packd.NewMemoryBox() 131 err := box.AddString("cars/show.html", "Show: <%= car.Name %>") 132 r.NoError(err) 133 134 re := render.New(render.Options{ 135 TemplatesBox: box, 136 }) 137 138 app := buffalo.New(buffalo.Options{}) 139 app.GET("/cars/{id}", func(c buffalo.Context) error { 140 return c.Render(http.StatusOK, re.Auto(c, Car{Name: "Honda"})) 141 }) 142 143 w := httptest.New(app) 144 res := w.HTML("/cars/1").Get() 145 r.Contains(res.Body.String(), "Show: Honda") 146 r.NoError(err) 147 } 148 149 func Test_Auto_HTML_New(t *testing.T) { 150 r := require.New(t) 151 152 box := packd.NewMemoryBox() 153 err := box.AddString("cars/new.html", "New: <%= car.Name %>") 154 r.NoError(err) 155 156 re := render.New(render.Options{ 157 TemplatesBox: box, 158 }) 159 160 app := buffalo.New(buffalo.Options{}) 161 app.GET("/cars/new", func(c buffalo.Context) error { 162 return c.Render(http.StatusOK, re.Auto(c, Car{Name: "Honda"})) 163 }) 164 165 w := httptest.New(app) 166 res := w.HTML("/cars/new").Get() 167 r.Contains(res.Body.String(), "New: Honda") 168 } 169 170 func Test_Auto_HTML_Create(t *testing.T) { 171 r := require.New(t) 172 173 box := packd.NewMemoryBox() 174 err := box.AddString("cars/new.html", "New: <%= car.Name %>") 175 r.NoError(err) 176 177 re := render.New(render.Options{ 178 TemplatesBox: box, 179 }) 180 181 app := buffalo.New(buffalo.Options{}) 182 app.POST("/cars", func(c buffalo.Context) error { 183 return c.Render(http.StatusCreated, re.Auto(c, Car{Name: "Honda"})) 184 }) 185 186 w := httptest.New(app) 187 res := w.HTML("/cars").Post(nil) 188 r.Contains(res.Body.String(), "New: Honda") 189 } 190 191 func Test_Auto_HTML_Create_Redirect(t *testing.T) { 192 r := require.New(t) 193 194 app := buffalo.New(buffalo.Options{}) 195 app.POST("/cars", func(c buffalo.Context) error { 196 return c.Render(http.StatusCreated, render.Auto(c, Car{ 197 ID: 1, 198 Name: "Honda", 199 })) 200 }) 201 202 w := httptest.New(app) 203 res := w.HTML("/cars").Post(nil) 204 r.Equal("/cars/1", res.Location()) 205 r.Equal(http.StatusFound, res.Code) 206 } 207 208 func Test_Auto_HTML_Create_Redirect_Error(t *testing.T) { 209 r := require.New(t) 210 211 box := packd.NewMemoryBox() 212 err := box.AddString("cars/new.html", "Create: <%= car.Name %>") 213 r.NoError(err) 214 215 re := render.New(render.Options{ 216 TemplatesBox: box, 217 }) 218 219 app := buffalo.New(buffalo.Options{}) 220 app.POST("/cars", func(c buffalo.Context) error { 221 b := Car{ 222 Name: "Honda", 223 } 224 return c.Render(http.StatusUnprocessableEntity, re.Auto(c, b)) 225 }) 226 227 w := httptest.New(app) 228 res := w.HTML("/cars").Post(nil) 229 r.Equal(http.StatusUnprocessableEntity, res.Code) 230 r.Contains(res.Body.String(), "Create: Honda") 231 } 232 233 func Test_Auto_HTML_Create_Nested_Redirect(t *testing.T) { 234 r := require.New(t) 235 236 app := buffalo.New(buffalo.Options{}) 237 admin := app.Group("/admin") 238 admin.POST("/cars", func(c buffalo.Context) error { 239 return c.Render(http.StatusCreated, render.Auto(c, Car{ 240 ID: 1, 241 Name: "Honda", 242 })) 243 }) 244 245 w := httptest.New(app) 246 res := w.HTML("/admin/cars").Post(nil) 247 r.Equal("/admin/cars/1", res.Location()) 248 r.Equal(http.StatusFound, res.Code) 249 } 250 251 func Test_Auto_HTML_Destroy_Nested_Redirect(t *testing.T) { 252 r := require.New(t) 253 254 app := buffalo.New(buffalo.Options{}) 255 admin := app.Group("/admin") 256 admin.DELETE("/cars", func(c buffalo.Context) error { 257 return c.Render(http.StatusOK, render.Auto(c, Car{ 258 ID: 1, 259 Name: "Honda", 260 })) 261 }) 262 263 w := httptest.New(app) 264 res := w.HTML("/admin/cars").Delete() 265 r.Equal("/admin/cars", res.Location()) 266 r.Equal(http.StatusFound, res.Code) 267 } 268 269 func Test_Auto_HTML_Edit(t *testing.T) { 270 r := require.New(t) 271 272 box := packd.NewMemoryBox() 273 err := box.AddString("cars/edit.html", "Edit: <%= car.Name %>") 274 r.NoError(err) 275 276 re := render.New(render.Options{ 277 TemplatesBox: box, 278 }) 279 280 app := buffalo.New(buffalo.Options{}) 281 app.GET("/cars/{id}/edit", func(c buffalo.Context) error { 282 return c.Render(http.StatusOK, re.Auto(c, Car{Name: "Honda"})) 283 }) 284 285 w := httptest.New(app) 286 res := w.HTML("/cars/1/edit").Get() 287 r.Contains(res.Body.String(), "Edit: Honda") 288 } 289 290 func Test_Auto_HTML_Update(t *testing.T) { 291 r := require.New(t) 292 293 box := packd.NewMemoryBox() 294 err := box.AddString("cars/edit.html", "Update: <%= car.Name %>") 295 r.NoError(err) 296 297 re := render.New(render.Options{ 298 TemplatesBox: box, 299 }) 300 301 app := buffalo.New(buffalo.Options{}) 302 app.PUT("/cars/{id}", func(c buffalo.Context) error { 303 return c.Render(http.StatusOK, re.Auto(c, Car{Name: "Honda"})) 304 }) 305 306 w := httptest.New(app) 307 res := w.HTML("/cars/1").Put(nil) 308 309 r.Contains(res.Body.String(), "Update: Honda") 310 } 311 312 func Test_Auto_HTML_Update_Redirect(t *testing.T) { 313 r := require.New(t) 314 315 app := buffalo.New(buffalo.Options{}) 316 app.PUT("/cars/{id}", func(c buffalo.Context) error { 317 b := Car{ 318 ID: 1, 319 Name: "Honda", 320 } 321 return c.Render(http.StatusOK, render.Auto(c, b)) 322 }) 323 324 w := httptest.New(app) 325 res := w.HTML("/cars/1").Put(nil) 326 r.Equal("/cars/1", res.Location()) 327 r.Equal(http.StatusFound, res.Code) 328 } 329 330 func Test_Auto_HTML_Update_Redirect_Error(t *testing.T) { 331 r := require.New(t) 332 333 box := packd.NewMemoryBox() 334 err := box.AddString("cars/edit.html", "Update: <%= car.Name %>") 335 r.NoError(err) 336 337 re := render.New(render.Options{ 338 TemplatesBox: box, 339 }) 340 341 app := buffalo.New(buffalo.Options{}) 342 app.PUT("/cars/{id}", func(c buffalo.Context) error { 343 b := Car{ 344 ID: 1, 345 Name: "Honda", 346 } 347 return c.Render(http.StatusUnprocessableEntity, re.Auto(c, b)) 348 }) 349 350 w := httptest.New(app) 351 res := w.HTML("/cars/1").Put(nil) 352 r.Equal(http.StatusUnprocessableEntity, res.Code) 353 r.Contains(res.Body.String(), "Update: Honda") 354 } 355 356 func Test_Auto_HTML_Destroy_Redirect(t *testing.T) { 357 r := require.New(t) 358 359 app := buffalo.New(buffalo.Options{}) 360 app.DELETE("/cars/{id}", func(c buffalo.Context) error { 361 b := Car{ 362 ID: 1, 363 Name: "Honda", 364 } 365 return c.Render(http.StatusOK, render.Auto(c, b)) 366 }) 367 368 w := httptest.New(app) 369 res := w.HTML("/cars/1").Delete() 370 r.Equal("/cars/", res.Location()) 371 r.Equal(http.StatusFound, res.Code) 372 } 373 374 func Test_Auto_HTML_List_Plural_MultiWord(t *testing.T) { 375 r := require.New(t) 376 377 type RoomProvider struct { 378 Name string 379 } 380 381 type RoomProviders []RoomProvider 382 383 box := packd.NewMemoryBox() 384 err := box.AddString("room_providers/index.html", "INDEX: <%= len(roomProviders) %>") 385 r.NoError(err) 386 387 re := render.New(render.Options{ 388 TemplatesBox: box, 389 }) 390 391 app := buffalo.New(buffalo.Options{}) 392 app.GET("/room_providers", func(c buffalo.Context) error { 393 return c.Render(http.StatusOK, re.Auto(c, RoomProviders{ 394 RoomProvider{Name: "Ford"}, 395 RoomProvider{Name: "Chevy"}, 396 })) 397 }) 398 399 w := httptest.New(app) 400 res := w.HTML("/room_providers").Get() 401 402 r.Contains(res.Body.String(), "INDEX: 2") 403 } 404 405 func Test_Auto_HTML_List_Plural_MultiWord_Dashed(t *testing.T) { 406 r := require.New(t) 407 408 type RoomProvider struct { 409 Name string 410 } 411 412 type RoomProviders []RoomProvider 413 414 box := packd.NewMemoryBox() 415 err := box.AddString("room_providers/index.html", "INDEX: <%= len(roomProviders) %>") 416 r.NoError(err) 417 418 re := render.New(render.Options{ 419 TemplatesBox: box, 420 }) 421 422 app := buffalo.New(buffalo.Options{}) 423 app.GET("/room-providers", func(c buffalo.Context) error { 424 return c.Render(http.StatusOK, re.Auto(c, RoomProviders{ 425 RoomProvider{Name: "Ford"}, 426 RoomProvider{Name: "Chevy"}, 427 })) 428 }) 429 430 w := httptest.New(app) 431 res := w.HTML("/room-providers").Get() 432 433 r.Contains(res.Body.String(), "INDEX: 2") 434 } 435 436 func Test_Auto_HTML_Show_MultiWord_Dashed(t *testing.T) { 437 r := require.New(t) 438 439 type RoomProvider struct { 440 ID int 441 Name string 442 } 443 444 box := packd.NewMemoryBox() 445 err := box.AddString("room_providers/show.html", "SHOW: <%= roomProvider.Name %>") 446 r.NoError(err) 447 448 re := render.New(render.Options{ 449 TemplatesBox: box, 450 }) 451 452 app := buffalo.New(buffalo.Options{}) 453 app.GET("/room-providers/{id}", func(c buffalo.Context) error { 454 return c.Render(http.StatusOK, re.Auto(c, RoomProvider{ID: 1, Name: "Ford"})) 455 }) 456 457 w := httptest.New(app) 458 res := w.HTML("/room-providers/1").Get() 459 460 r.Contains(res.Body.String(), "SHOW: Ford") 461 }