forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrappers_test.go
102 lines (80 loc) · 1.93 KB
/
wrappers_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package buffalo
import (
"net/http"
"testing"
"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/httptest"
"github.com/stretchr/testify/require"
)
func Test_WrapHandlerFunc(t *testing.T) {
r := require.New(t)
a := New(Options{})
a.GET("/foo", WrapHandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("hello"))
}))
w := httptest.New(a)
res := w.HTML("/foo").Get()
r.Equal("hello", res.Body.String())
}
func Test_WrapHandler(t *testing.T) {
r := require.New(t)
a := New(Options{})
a.GET("/foo", WrapHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("hello"))
})))
w := httptest.New(a)
res := w.HTML("/foo").Get()
r.Equal("hello", res.Body.String())
}
func Test_WrapBuffaloHandler(t *testing.T) {
r := require.New(t)
tt := []struct {
verb string
path string
status int
}{
{"GET", "/", 1},
{"GET", "/foo", 2},
{"POST", "/", 3},
{"POST", "/foo", 4},
}
for _, x := range tt {
bf := func(c Context) error {
req := c.Request()
return c.Render(x.status, render.String(req.Method+req.URL.Path))
}
h := WrapBuffaloHandler(bf)
r.NotNil(h)
req := httptest.NewRequest(x.verb, x.path, nil)
res := httptest.NewRecorder()
h.ServeHTTP(res, req)
r.Equal(x.status, res.Code)
r.Contains(res.Body.String(), x.verb+x.path)
}
}
func Test_WrapBuffaloHandlerFunc(t *testing.T) {
r := require.New(t)
tt := []struct {
verb string
path string
status int
}{
{"GET", "/", 1},
{"GET", "/foo", 2},
{"POST", "/", 3},
{"POST", "/foo", 4},
}
for _, x := range tt {
bf := func(c Context) error {
req := c.Request()
return c.Render(x.status, render.String(req.Method+req.URL.Path))
}
h := WrapBuffaloHandlerFunc(bf)
r.NotNil(h)
req := httptest.NewRequest(x.verb, x.path, nil)
res := httptest.NewRecorder()
h(res, req)
r.Equal(x.status, res.Code)
r.Contains(res.Body.String(), x.verb+x.path)
}
}