github.com/gotstago/buffalo@v0.9.5/not_found_test.go (about)

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