github.com/corylanou/buffalo@v0.8.0/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 := Automatic(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 PAGE NOT FOUND")
    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 := Automatic(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("GET", jb["method"])
    42  	r.Equal("/bad", jb["path"])
    43  	r.NotEmpty(jb["routes"])
    44  }
    45  
    46  func Test_App_Prod_NotFound(t *testing.T) {
    47  	r := require.New(t)
    48  
    49  	a := New(Options{Env: "production"})
    50  	a.GET("/foo", func(c Context) error { return nil })
    51  
    52  	w := willie.New(a)
    53  	res := w.Request("/bad").Get()
    54  	r.Equal(404, res.Code)
    55  
    56  	body := res.Body.String()
    57  	r.Equal(body, "404 page not found\n")
    58  	r.NotContains(body, "/foo")
    59  }
    60  
    61  func Test_App_Override_NotFound(t *testing.T) {
    62  	r := require.New(t)
    63  
    64  	a := New(Options{})
    65  	a.ErrorHandlers[404] = func(status int, err error, c Context) error {
    66  		c.Response().WriteHeader(404)
    67  		c.Response().Write([]byte("oops!!!"))
    68  		return nil
    69  	}
    70  	a.GET("/foo", func(c Context) error { return nil })
    71  
    72  	w := willie.New(a)
    73  	res := w.Request("/bad").Get()
    74  	r.Equal(404, res.Code)
    75  
    76  	body := res.Body.String()
    77  	r.Equal(body, "oops!!!")
    78  	r.NotContains(body, "/foo")
    79  }