-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
expect_body_json_jq.go
167 lines (150 loc) · 5.04 KB
/
expect_body_json_jq.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package hit
import (
"reflect"
"golang.org/x/xerrors"
"github.com/Eun/go-hit/internal/minitest"
)
// IExpectBodyJSONJQ provides assertions on the http response json body.
type IExpectBodyJSONJQ 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().JQ(".").Equal(map[string]interface{}{"ID": 10, "Name": "Joe", "Roles": []string{"Admin", "User"}})
// Expect().Body().JSON().JQ(".Name").Equal("Joe")
// Expect().Body().JSON().JQ(".Roles").Equal([]string{"Admin", "User"})
// Expect().Body().JSON().JQ(".Roles.[0]").Equal("Admin")
//
// Example:
// // given the following response: { "ID": 10, "Name": "Joe", "Roles": ["Admin", "User"] }
// MustDo(
// Get("https://example.com/json"),
// Expect().Body().JSON().JQ(".").Equal(map[string]interface{}{"ID": 10, "Name": "Joe", "Roles": []string{"Admin", "User"}}),
// Expect().Body().JSON().JQ(".Name").Equal("Joe"),
// Expect().Body().JSON().JQ(".Roles").Equal([]string{"Admin", "User"}),
// Expect().Body().JSON().JQ(".Roles.[0]").Equal("Admin"),
// )
Equal(data interface{}) IStep
// NotEqual expects the json body to be equal to the specified value.
//
// see Equal() for usage and examples
NotEqual(data ...interface{}) IStep
// Contains 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().JQ(".").Contains("ID")
// Expect().Body().JSON().JQ(".Name").Contains("J")
// Expect().Body().JSON().JQ(".Roles").Contains("Admin")
//
// Example:
// // given the following response: { "ID": 10, "Name": "Joe", "Roles": ["Admin", "User"] }
// MustDo(
// Get("https://example.com/json"),
// Expect().Body().JSON().JQ(".").Contains("ID"),
// Expect().Body().JSON().JQ(".Name").Contains("J"),
// Expect().Body().JSON().JQ(".Roles").Contains("Admin"),
// )
Contains(data ...interface{}) IStep
// NotContains expects the json body to be equal to the specified value.
//
// see Contains() for usage and examples
NotContains(data ...interface{}) IStep
// Len provides assertions to the json object size
//
// Usage:
// Expect().Body().JSON().JQ(".Name").Len().Equal(10)
Len() IExpectInt
// JQ runs an additional jq expression
JQ(expression ...string) IExpectBodyJSONJQ
}
type expectBodyJSONJQ struct {
expectBody IExpectBody
cleanPath callPath
expression []string
}
func newExpectBodyJSONJQ(expectBody IExpectBody, cleanPath callPath, expression []string) IExpectBodyJSONJQ {
return &expectBodyJSONJQ{
expectBody: expectBody,
cleanPath: cleanPath,
expression: expression,
}
}
func (v *expectBodyJSONJQ) 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().JQ(&obj, v.expression...); err != nil {
return err
}
return minitest.Equal(obj, data)
},
}
}
func (v *expectBodyJSONJQ) 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().JQ(&obj, v.expression...); err != nil {
return err
}
return minitest.NotEqual(obj, data...)
},
}
}
func (v *expectBodyJSONJQ) 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().JQ(&obj, v.expression...); err != nil {
return err
}
return minitest.Contains(obj, data...)
},
}
}
func (v *expectBodyJSONJQ) 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().JQ(&obj, v.expression...); err != nil {
return err
}
return minitest.NotContains(obj, data...)
},
}
}
func (v *expectBodyJSONJQ) Len() IExpectInt {
return newExpectInt(v.cleanPath.Push("Len", nil), func(hit Hit) int {
var obj interface{}
hit.Response().body.JSON().MustJQ(&obj, v.expression...)
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))
}
})
}
func (v *expectBodyJSONJQ) JQ(expression ...string) IExpectBodyJSONJQ {
return newExpectBodyJSONJQ(
v.expectBody,
v.cleanPath.Push("JQ", stringSliceToInterfaceSlice(expression)), append(v.expression, expression...),
)
}