-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexpect_body_json.go
145 lines (127 loc) · 3.97 KB
/
expect_body_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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package hit
import (
"reflect"
"golang.org/x/xerrors"
"github.com/Eun/go-hit/internal/minitest"
)
// IExpectBodyJSON provides assertions on the http response json body.
type IExpectBodyJSON interface {
// Equal expects the json body to be equal to the specified value.
//
// given the following response: { "ID": 10, "Name": "Joe", "Roles": ["Admin", "User"] }
// Usage:
// Expect().Body().JSON().Equal(map[string]interface{}{"ID": 10, "Name": "Joe", "Roles": []string{"Admin", "User"}})
Equal(data interface{}) IStep
// NotEqual expects the json body to be not equal to the specified value.
//
// see Equal() for usage and examples
NotEqual(data ...interface{}) IStep
// Contains expects the json body to contain the specified value.
//
// given the following response: { "ID": 10, "Name": "Joe", "Roles": ["Admin", "User"] }
// Usage:
// Expect().Body().JSON().Contains("ID")
Contains(data ...interface{}) IStep
// NotContains expects the json body to not contain the specified value.
//
// see Contains() for usage and examples
NotContains(data ...interface{}) IStep
// JQ runs an jq expression on the JSON body, the result can be asserted
//
// given the following response: { "ID": 10, "Name": "Joe", "Roles": ["Admin", "User"] }
// Usage:
// Expect().Body().JSON().JQ(".Name").Equal("Joe")
// Expect().Body().JSON().JQ(".Roles").Equal([]string{"Admin", "User"})
// Expect().Body().JSON().JQ(".Roles.[0]").Equal("Admin")
JQ(expression ...string) IExpectBodyJSONJQ
// Len provides assertions to the json body size
//
// Usage:
// Expect().Body().JSON().Len().Equal(10)
Len() IExpectInt
}
type expectBodyJSON struct {
expectBody IExpectBody
cleanPath callPath
}
func newExpectBodyJSON(expectBody IExpectBody, cleanPath callPath) IExpectBodyJSON {
return &expectBodyJSON{
expectBody: expectBody,
cleanPath: cleanPath,
}
}
func (v *expectBodyJSON) Equal(data interface{}) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: ExpectStep,
CallPath: v.cleanPath.Push("Equal", []interface{}{data}),
Exec: func(hit *hitImpl) error {
var obj interface{}
if err := hit.Response().body.JSON().Decode(&obj); err != nil {
return err
}
return minitest.Equal(obj, data)
},
}
}
func (v *expectBodyJSON) NotEqual(data ...interface{}) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: ExpectStep,
CallPath: v.cleanPath.Push("NotEqual", data),
Exec: func(hit *hitImpl) error {
var obj interface{}
if err := hit.Response().body.JSON().Decode(&obj); err != nil {
return err
}
return minitest.NotEqual(obj, data...)
},
}
}
func (v *expectBodyJSON) Contains(data ...interface{}) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: ExpectStep,
CallPath: v.cleanPath.Push("Contains", data),
Exec: func(hit *hitImpl) error {
var obj interface{}
if err := hit.Response().body.JSON().Decode(&obj); err != nil {
return err
}
return minitest.Contains(obj, data...)
},
}
}
func (v *expectBodyJSON) NotContains(data ...interface{}) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: ExpectStep,
CallPath: v.cleanPath.Push("NotContains", data),
Exec: func(hit *hitImpl) error {
var obj interface{}
if err := hit.Response().body.JSON().Decode(&obj); err != nil {
return err
}
return minitest.NotContains(obj, data...)
},
}
}
func (v *expectBodyJSON) JQ(expression ...string) IExpectBodyJSONJQ {
return newExpectBodyJSONJQ(v.expectBody, v.cleanPath.Push("JQ", stringSliceToInterfaceSlice(expression)), expression)
}
func (v *expectBodyJSON) Len() IExpectInt {
return newExpectInt(v.cleanPath.Push("Len", nil), func(hit Hit) int {
var obj interface{}
hit.Response().body.JSON().MustDecode(&obj)
if obj == nil {
return 0
}
rv := reflect.ValueOf(obj)
switch rv.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
return rv.Len()
default:
panic(xerrors.Errorf("cannot get len for %#v", obj))
}
})
}