forked from ivpusic/httpcheck
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tester_body.go
253 lines (222 loc) · 7.06 KB
/
tester_body.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package httpcheck
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
"github.com/itchyny/gojq"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// body //////////////////////////////////////////////////////////
// WithBody adds the []byte data to the body.
func (tt *Tester) WithBody(body []byte) *Tester {
tt.request.Body = io.NopCloser(bytes.NewReader(body))
tt.request.ContentLength = int64(len(body))
return tt
}
// HasBody checks if the body is equal to provided []byte data.
func (tt *Tester) HasBody(expected []byte) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
assert.Equal(tt.t, expected, body)
return tt
}
// MustHasBody checks if the body is equal to provided []byte data.
func (tt *Tester) MustHasBody(expected []byte) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
require.Equal(tt.t, expected, body)
return tt
}
// ContainsBody checks if the body contains provided [] byte data.
func (tt *Tester) ContainsBody(segment []byte) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
if !bytes.Contains(body, segment) {
assert.Fail(tt.t, fmt.Sprintf("%#v does not contain %#v", body, segment))
}
return tt
}
// MustContainsBody checks if the body contains provided [] byte data.
func (tt *Tester) MustContainsBody(segment []byte) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
if !bytes.Contains(body, segment) {
require.Fail(tt.t, fmt.Sprintf("%#v does not contain %#v", body, segment))
}
return tt
}
// NotContainsBody checks if the body does not contain provided [] byte data.
func (tt *Tester) NotContainsBody(segment []byte) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
if bytes.Contains(body, segment) {
assert.Fail(tt.t, fmt.Sprintf("%#v contains %#v", body, segment))
}
return tt
}
// MustNotContainsBody checks if the body does not contain provided [] byte data.
func (tt *Tester) MustNotContainsBody(segment []byte) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
if bytes.Contains(body, segment) {
require.Fail(tt.t, fmt.Sprintf("%#v contains %#v", body, segment))
}
return tt
}
// WithString adds the string to the body.
func (tt *Tester) WithString(body string) *Tester {
tt.request.Body = io.NopCloser(strings.NewReader(body))
tt.request.ContentLength = int64(len(body))
return tt
}
// HasString converts the response to a string type and then compares it with the given string.
func (tt *Tester) HasString(expected string) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
assert.Equal(tt.t, expected, string(body))
return tt
}
// MustHasString converts the response to a string type and then compares it with the given string.
func (tt *Tester) MustHasString(expected string) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
require.Equal(tt.t, expected, string(body))
return tt
}
// ContainsString converts the response to a string type and then checks it containing the given string.
func (tt *Tester) ContainsString(substr string) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
assert.Contains(tt.t, string(body), substr)
return tt
}
// MustContainsString converts the response to a string type and then checks it containing the given string.
func (tt *Tester) MustContainsString(substr string) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
require.Contains(tt.t, string(body), substr)
return tt
}
// NotContainsString converts the response to a string type and then checks if it does not
// contain the given string.
func (tt *Tester) NotContainsString(substr string) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
assert.NotContains(tt.t, string(body), substr)
return tt
}
// MustNotContainsString converts the response to a string type and then checks if it does not
// contain the given string.
func (tt *Tester) MustNotContainsString(substr string) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
require.NotContains(tt.t, string(body), substr)
return tt
}
func (tt *Tester) MatchesJSONQuery(q string) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
var in any
require.NoError(tt.t, json.Unmarshal(body, &in), "failed to unmarshal json: %s", string(body))
jq, err := gojq.Parse(q)
require.NoError(tt.t, err, "failed to parse query %q: %s", q)
it := jq.Run(in)
var detect bool
for {
v, ok := it.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
require.NoError(tt.t, err, "query %q does not match: %s", q, string(body))
}
if v != nil {
detect = true
}
}
assert.True(tt.t, detect, "query %q does not match: %s", q, string(body))
return tt
}
func (tt *Tester) NotMatchesJSONQuery(q string) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
var in any
require.NoError(tt.t, json.Unmarshal(body, &in))
jq, err := gojq.Parse(q)
require.NoError(tt.t, err, "failed to parse query %q: %s", q)
it := jq.Run(in)
var detect bool
for {
v, ok := it.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
require.NoError(tt.t, err, "query %q does not match: %s", q, string(body))
}
if v != nil {
detect = true
}
}
assert.False(tt.t, detect, "query %q does not match: %s", q, string(body))
return tt
}