-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
38 lines (33 loc) · 1.06 KB
/
json.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
package ginsrvmock
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/antlinker/ginsrvmock/internal/json"
"github.com/stretchr/testify/assert"
)
// JSONAssert json断言器
type JSONAssert struct {
Resp *httptest.ResponseRecorder
t *testing.T
}
// Assert 断言操作
// wantcode 想要获得的返回http状态码
// wantdata 想要获得的返回数据,首先wantdata会被json编码,然后在被解码为map[string]interface{}
// wanthead 只有第一个参数生效,用来比较返回的http头信息中是否包含 wanthead 包含的所有信息
func (a JSONAssert) Assert(wantcode int, wantdata interface{}, wanthead ...http.Header) {
assert.Equal(a.t, wantcode, a.Resp.Code)
if wantdata != nil {
buff := a.Resp.Body.Bytes()
tmp, err := json.Marshal(wantdata)
assert.Empty(a.t, err)
assert.JSONEq(a.t, string(tmp), string(buff))
}
if len(wanthead) == 0 || wanthead[0] == nil {
return
}
for key := range wanthead[0] {
wantheadvalue := wanthead[0].Get(key)
assert.Equal(a.t, wantheadvalue, a.Resp.HeaderMap.Get(key))
}
}