github.com/bscott/buffalo@v0.11.1/render/auto_test.go (about) 1 package render_test 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 "testing" 8 9 "github.com/gobuffalo/buffalo" 10 "github.com/gobuffalo/buffalo/render" 11 "github.com/gobuffalo/packr" 12 "github.com/markbates/willie" 13 "github.com/stretchr/testify/require" 14 ) 15 16 type Car struct { 17 ID int 18 Name string 19 } 20 21 type Cars []Car 22 23 func Test_Auto_JSON(t *testing.T) { 24 r := require.New(t) 25 26 app := buffalo.New(buffalo.Options{}) 27 app.GET("/cars", func(c buffalo.Context) error { 28 return c.Render(200, render.Auto(c, []string{"Honda", "Toyota", "Ford", "Chevy"})) 29 }) 30 31 w := willie.New(app) 32 33 res := w.JSON("/cars").Get() 34 r.Equal(`["Honda","Toyota","Ford","Chevy"]`, strings.TrimSpace(res.Body.String())) 35 } 36 37 func Test_Auto_XML(t *testing.T) { 38 r := require.New(t) 39 40 app := buffalo.New(buffalo.Options{}) 41 app.GET("/cars", func(c buffalo.Context) error { 42 return c.Render(200, render.Auto(c, []string{"Honda", "Toyota", "Ford", "Chevy"})) 43 }) 44 45 w := willie.New(app) 46 47 res := w.XML("/cars").Get() 48 r.Equal("<string>Honda</string>\n<string>Toyota</string>\n<string>Ford</string>\n<string>Chevy</string>", strings.TrimSpace(res.Body.String())) 49 } 50 51 func Test_Auto_HTML_List(t *testing.T) { 52 r := require.New(t) 53 54 err := withHTMLFile("cars/index.html", "INDEX: <%= len(cars) %>", func(e *render.Engine) { 55 app := buffalo.New(buffalo.Options{}) 56 app.GET("/cars", func(c buffalo.Context) error { 57 return c.Render(200, e.Auto(c, Cars{ 58 {Name: "Ford"}, 59 {Name: "Chevy"}, 60 })) 61 }) 62 63 w := willie.New(app) 64 res := w.HTML("/cars").Get() 65 66 r.Contains(res.Body.String(), "INDEX: 2") 67 }) 68 r.NoError(err) 69 } 70 71 func Test_Auto_HTML_Show(t *testing.T) { 72 r := require.New(t) 73 74 err := withHTMLFile("cars/show.html", "Show: <%= car.Name %>", func(e *render.Engine) { 75 app := buffalo.New(buffalo.Options{}) 76 app.GET("/cars/{id}", func(c buffalo.Context) error { 77 return c.Render(200, e.Auto(c, Car{Name: "Honda"})) 78 }) 79 80 w := willie.New(app) 81 res := w.HTML("/cars/1").Get() 82 r.Contains(res.Body.String(), "Show: Honda") 83 }) 84 r.NoError(err) 85 } 86 87 func Test_Auto_HTML_New(t *testing.T) { 88 r := require.New(t) 89 90 err := withHTMLFile("cars/new.html", "New: <%= car.Name %>", func(e *render.Engine) { 91 app := buffalo.New(buffalo.Options{}) 92 app.GET("/cars/new", func(c buffalo.Context) error { 93 return c.Render(200, e.Auto(c, Car{Name: "Honda"})) 94 }) 95 96 w := willie.New(app) 97 res := w.HTML("/cars/new").Get() 98 r.Contains(res.Body.String(), "New: Honda") 99 }) 100 r.NoError(err) 101 } 102 103 func Test_Auto_HTML_Create(t *testing.T) { 104 r := require.New(t) 105 106 err := withHTMLFile("cars/new.html", "New: <%= car.Name %>", func(e *render.Engine) { 107 app := buffalo.New(buffalo.Options{}) 108 app.POST("/cars", func(c buffalo.Context) error { 109 return c.Render(201, e.Auto(c, Car{Name: "Honda"})) 110 }) 111 112 w := willie.New(app) 113 res := w.HTML("/cars").Post(nil) 114 r.Contains(res.Body.String(), "New: Honda") 115 }) 116 r.NoError(err) 117 } 118 119 func Test_Auto_HTML_Create_Redirect(t *testing.T) { 120 r := require.New(t) 121 122 app := buffalo.New(buffalo.Options{}) 123 app.POST("/cars", func(c buffalo.Context) error { 124 return c.Render(201, render.Auto(c, Car{ 125 ID: 1, 126 Name: "Honda", 127 })) 128 }) 129 130 w := willie.New(app) 131 res := w.HTML("/cars").Post(nil) 132 r.Equal("/cars/1", res.Location()) 133 r.Equal(302, res.Code) 134 } 135 136 func Test_Auto_HTML_Create_Redirect_Error(t *testing.T) { 137 r := require.New(t) 138 139 err := withHTMLFile("cars/new.html", "Create: <%= car.Name %>", func(e *render.Engine) { 140 app := buffalo.New(buffalo.Options{}) 141 app.POST("/cars", func(c buffalo.Context) error { 142 b := Car{ 143 Name: "Honda", 144 } 145 return c.Render(422, e.Auto(c, b)) 146 }) 147 148 w := willie.New(app) 149 res := w.HTML("/cars").Post(nil) 150 r.Equal(422, res.Code) 151 r.Contains(res.Body.String(), "Create: Honda") 152 }) 153 r.NoError(err) 154 } 155 156 func Test_Auto_HTML_Edit(t *testing.T) { 157 r := require.New(t) 158 159 err := withHTMLFile("cars/edit.html", "Edit: <%= car.Name %>", func(e *render.Engine) { 160 app := buffalo.New(buffalo.Options{}) 161 app.GET("/cars/{id}/edit", func(c buffalo.Context) error { 162 return c.Render(200, e.Auto(c, Car{Name: "Honda"})) 163 }) 164 165 w := willie.New(app) 166 res := w.HTML("/cars/1/edit").Get() 167 r.Contains(res.Body.String(), "Edit: Honda") 168 }) 169 r.NoError(err) 170 } 171 172 func Test_Auto_HTML_Update(t *testing.T) { 173 r := require.New(t) 174 175 err := withHTMLFile("cars/edit.html", "Update: <%= car.Name %>", func(e *render.Engine) { 176 app := buffalo.New(buffalo.Options{}) 177 app.PUT("/cars/{id}", func(c buffalo.Context) error { 178 return c.Render(200, e.Auto(c, Car{Name: "Honda"})) 179 }) 180 181 w := willie.New(app) 182 res := w.HTML("/cars/1").Put(nil) 183 184 r.Contains(res.Body.String(), "Update: Honda") 185 }) 186 r.NoError(err) 187 } 188 189 func Test_Auto_HTML_Update_Redirect(t *testing.T) { 190 r := require.New(t) 191 192 app := buffalo.New(buffalo.Options{}) 193 app.PUT("/cars/{id}", func(c buffalo.Context) error { 194 b := Car{ 195 ID: 1, 196 Name: "Honda", 197 } 198 return c.Render(200, render.Auto(c, b)) 199 }) 200 201 w := willie.New(app) 202 res := w.HTML("/cars/1").Put(nil) 203 r.Equal("/cars/1", res.Location()) 204 r.Equal(302, res.Code) 205 } 206 207 func Test_Auto_HTML_Update_Redirect_Error(t *testing.T) { 208 r := require.New(t) 209 210 err := withHTMLFile("cars/edit.html", "Update: <%= car.Name %>", func(e *render.Engine) { 211 app := buffalo.New(buffalo.Options{}) 212 app.PUT("/cars/{id}", func(c buffalo.Context) error { 213 b := Car{ 214 ID: 1, 215 Name: "Honda", 216 } 217 return c.Render(422, e.Auto(c, b)) 218 }) 219 220 w := willie.New(app) 221 res := w.HTML("/cars/1").Put(nil) 222 r.Equal(422, res.Code) 223 r.Contains(res.Body.String(), "Update: Honda") 224 }) 225 r.NoError(err) 226 } 227 228 func Test_Auto_HTML_Destroy_Redirect(t *testing.T) { 229 r := require.New(t) 230 231 app := buffalo.New(buffalo.Options{}) 232 app.DELETE("/cars/{id}", func(c buffalo.Context) error { 233 b := Car{ 234 ID: 1, 235 Name: "Honda", 236 } 237 return c.Render(200, render.Auto(c, b)) 238 }) 239 240 w := willie.New(app) 241 res := w.HTML("/cars/1").Delete() 242 r.Equal("/cars", res.Location()) 243 r.Equal(302, res.Code) 244 } 245 246 func withHTMLFile(name string, contents string, fn func(*render.Engine)) error { 247 tmpDir := filepath.Join(os.TempDir(), filepath.Dir(name)) 248 err := os.MkdirAll(tmpDir, 0766) 249 if err != nil { 250 return err 251 } 252 defer os.Remove(tmpDir) 253 254 tmpFile, err := os.Create(filepath.Join(tmpDir, filepath.Base(name))) 255 if err != nil { 256 return err 257 } 258 defer os.Remove(tmpFile.Name()) 259 260 _, err = tmpFile.Write([]byte(contents)) 261 if err != nil { 262 return err 263 } 264 265 e := render.New(render.Options{ 266 TemplatesBox: packr.NewBox(os.TempDir()), 267 }) 268 269 fn(e) 270 return nil 271 }