-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponse_json_test.go
90 lines (82 loc) · 2.7 KB
/
response_json_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
package webfmwk
import (
"net/http"
"testing"
"github.com/burgesQ/gommon/webtest"
"github.com/stretchr/testify/require"
)
func TestJSONResponse(t *testing.T) {
var (
s, e = InitServer(CheckIsUp())
ret = struct {
Message string `json:"message"`
}{"nul"}
tests = map[string]struct {
fn func(c Context, ret interface{}) error
expectedOP int
}{
"blob": {expectedOP: http.StatusOK, fn: func(c Context, ret interface{}) error {
return c.JSONBlob(http.StatusOK, []byte(hBody))
}},
"ok": {expectedOP: http.StatusOK, fn: func(c Context, ret interface{}) error {
return c.JSONOk(ret)
}},
"created": {expectedOP: http.StatusCreated, fn: func(c Context, ret interface{}) error {
return c.JSONCreated(ret)
}},
"accepted": {expectedOP: http.StatusAccepted, fn: func(c Context, ret interface{}) error {
return c.JSONAccepted(ret)
}},
"noContent": {expectedOP: http.StatusNoContent, fn: func(c Context, ret interface{}) error {
return c.JSONNoContent()
}},
"badRequest": {expectedOP: http.StatusBadRequest, fn: func(c Context, ret interface{}) error {
return c.JSONBadRequest(ret)
}},
"unauthorized": {expectedOP: http.StatusUnauthorized, fn: func(c Context, ret interface{}) error {
return c.JSONUnauthorized(ret)
}},
"forbidden": {expectedOP: http.StatusForbidden, fn: func(c Context, ret interface{}) error {
return c.JSONForbidden(ret)
}},
"notFound": {expectedOP: http.StatusNotFound, fn: func(c Context, ret interface{}) error {
return c.JSONNotFound(ret)
}},
"conflict": {expectedOP: http.StatusConflict, fn: func(c Context, ret interface{}) error {
return c.JSONConflict(ret)
}},
"unprocessable": {expectedOP: http.StatusUnprocessableEntity, fn: func(c Context, ret interface{}) error {
return c.JSONUnprocessable(ret)
}},
"internalError": {expectedOP: http.StatusInternalServerError, fn: func(c Context, ret interface{}) error {
return c.JSONInternalError(ret)
}},
"notImplemented": {expectedOP: http.StatusNotImplemented, fn: func(c Context, ret interface{}) error {
return c.JSONNotImplemented(ret)
}},
}
)
require.Nil(t, e)
t.Cleanup(func() { require.Nil(t, s.ShutdownAndWait()) })
// load custom endpoints
for n, t := range tests {
fn := t.fn
s.GET("/"+n, func(c Context) error {
return fn(c, ret)
})
}
go s.Start(_testPort)
<-s.isReady
for name, test := range tests {
t.Run(name, func(t *testing.T) {
webtest.RequestAndTestAPI(t, _testAddr+"/"+name,
func(t *testing.T, resp *http.Response) {
t.Helper()
if test.expectedOP != http.StatusNoContent {
webtest.Body(t, hBody, resp)
}
webtest.StatusCode(t, test.expectedOP, resp)
})
})
}
}