github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/not_found_test.go (about) 1 package buffalo 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "testing" 7 8 "github.com/gobuffalo/httptest" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func Test_App_Dev_NotFound(t *testing.T) { 13 r := require.New(t) 14 15 a := New(Options{}) 16 a.Env = "development" 17 a.GET("/foo", func(c Context) error { return nil }) 18 19 w := httptest.New(a) 20 res := w.HTML("/bad").Get() 21 22 body := res.Body.String() 23 r.Contains(body, "404 - ERROR!") 24 r.Contains(body, "/foo") 25 r.Equal(http.StatusNotFound, res.Code) 26 } 27 28 func Test_App_Dev_NotFound_JSON(t *testing.T) { 29 r := require.New(t) 30 31 a := New(Options{}) 32 a.Env = "development" 33 a.GET("/foo", func(c Context) error { return nil }) 34 35 w := httptest.New(a) 36 res := w.JSON("/bad").Get() 37 r.Equal(http.StatusNotFound, res.Code) 38 39 jb := map[string]interface{}{} 40 err := json.NewDecoder(res.Body).Decode(&jb) 41 r.NoError(err) 42 r.Equal(float64(http.StatusNotFound), jb["code"]) 43 } 44 45 func Test_App_Override_NotFound(t *testing.T) { 46 r := require.New(t) 47 48 a := New(Options{}) 49 a.ErrorHandlers[http.StatusNotFound] = func(status int, err error, c Context) error { 50 c.Response().WriteHeader(http.StatusNotFound) 51 c.Response().Write([]byte("oops!!!")) 52 return nil 53 } 54 a.GET("/foo", func(c Context) error { return nil }) 55 56 w := httptest.New(a) 57 res := w.HTML("/bad").Get() 58 r.Equal(http.StatusNotFound, res.Code) 59 60 body := res.Body.String() 61 r.Equal(body, "oops!!!") 62 r.NotContains(body, "/foo") 63 }