-
Notifications
You must be signed in to change notification settings - Fork 3
/
content_type_test.go
49 lines (38 loc) · 1.13 KB
/
content_type_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
package contenttype_test
import (
"testing"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/httptest"
contenttype "github.com/gobuffalo/mw-contenttype"
"github.com/stretchr/testify/require"
)
func ctApp() *buffalo.App {
h := func(c buffalo.Context) error {
return c.Render(200, render.String(c.Request().Header.Get("Content-Type")))
}
a := buffalo.New(buffalo.Options{})
a.GET("/set", contenttype.Set("application/json")(h))
a.GET("/add", contenttype.Add("application/json")(h))
return a
}
func Test_SetContentType(t *testing.T) {
r := require.New(t)
w := httptest.New(ctApp())
res := w.HTML("/set").Get()
r.Equal("application/json", res.Body.String())
req := w.HTML("/set")
req.Headers["Content-Type"] = "text/plain"
res = req.Get()
r.Equal("application/json", res.Body.String())
}
func Test_AddContentType(t *testing.T) {
r := require.New(t)
w := httptest.New(ctApp())
res := w.HTML("/add").Get()
r.Equal("application/json", res.Body.String())
req := w.HTML("/add")
req.Headers["Content-Type"] = "text/plain"
res = req.Get()
r.Equal("text/plain", res.Body.String())
}