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