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