forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
not_found_test.go
63 lines (49 loc) · 1.37 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
63
package buffalo
import (
"encoding/json"
"net/http"
"testing"
"github.com/gobuffalo/httptest"
"github.com/stretchr/testify/require"
)
func Test_App_Dev_NotFound(t *testing.T) {
r := require.New(t)
a := New(Options{})
a.Env = "development"
a.GET("/foo", func(c Context) error { return nil })
w := httptest.New(a)
res := w.HTML("/bad").Get()
body := res.Body.String()
r.Contains(body, "404 - ERROR!")
r.Contains(body, "/foo")
r.Equal(http.StatusNotFound, res.Code)
}
func Test_App_Dev_NotFound_JSON(t *testing.T) {
r := require.New(t)
a := New(Options{})
a.Env = "development"
a.GET("/foo", func(c Context) error { return nil })
w := httptest.New(a)
res := w.JSON("/bad").Get()
r.Equal(http.StatusNotFound, res.Code)
jb := map[string]interface{}{}
err := json.NewDecoder(res.Body).Decode(&jb)
r.NoError(err)
r.Equal(float64(http.StatusNotFound), jb["code"])
}
func Test_App_Override_NotFound(t *testing.T) {
r := require.New(t)
a := New(Options{})
a.ErrorHandlers[http.StatusNotFound] = func(status int, err error, c Context) error {
c.Response().WriteHeader(http.StatusNotFound)
c.Response().Write([]byte("oops!!!"))
return nil
}
a.GET("/foo", func(c Context) error { return nil })
w := httptest.New(a)
res := w.HTML("/bad").Get()
r.Equal(http.StatusNotFound, res.Code)
body := res.Body.String()
r.Equal(body, "oops!!!")
r.NotContains(body, "/foo")
}