-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
46 lines (33 loc) · 977 Bytes
/
utils_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
package kid
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWrapHandler(t *testing.T) {
k1 := New()
k2 := New()
k2.Get("/test", func(c *Context) {
c.JSONByte(http.StatusOK, []byte(`{"status": "ok"}`))
})
k1.Get("/test", WrapHandler(k2))
req := httptest.NewRequest(http.MethodGet, "/test", nil)
res := httptest.NewRecorder()
k1.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
assert.Equal(t, `{"status": "ok"}`, res.Body.String())
}
func TestWrapHandlerFunc(t *testing.T) {
k := New()
k.Get("/test", WrapHandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"status": "ok"}`))
assert.NoError(t, err)
}))
req := httptest.NewRequest(http.MethodGet, "/test", nil)
res := httptest.NewRecorder()
k.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
assert.Equal(t, `{"status": "ok"}`, res.Body.String())
}