-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest.go
More file actions
334 lines (275 loc) · 7.03 KB
/
request.go
File metadata and controls
334 lines (275 loc) · 7.03 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// SPDX-FileCopyrightText: 2025 Greenbone AG
//
// SPDX-License-Identifier: AGPL-3.0-or-later
package httpassert
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// nolint:interfacebloat
// Request interface provides fluent HTTP request building.
type Request interface {
Get(path string) Request
Getf(format string, a ...interface{}) Request
Post(path string) Request
Postf(format string, a ...interface{}) Request
Put(path string) Request
Putf(format string, a ...interface{}) Request
Delete(path string) Request
Deletef(format string, a ...interface{}) Request
Options(path string) Request
Optionsf(format string, a ...interface{}) Request
Patch(path string) Request
Patchf(format string, a ...interface{}) Request
Perform(verb string, path string) Request
Performf(verb string, path string, a ...interface{}) Request
AuthHeader(header string) Request
Headers(headers map[string]string) Request
Header(key, value string) Request
AuthJwt(jwt string) Request
ContentType(string) Request
Content(string) Request
JsonContent(string) Request
JsonContentObject(any) Request
ContentFile(string) Request
JsonContentFile(path string) Request
Expect() Response
ExpectEventually(check func(r Response), timeout time.Duration, interval time.Duration) Response
}
type request struct {
t *testing.T
router http.Handler
method string
url string
contentType string
body string
headers map[string]string
response *httptest.ResponseRecorder
}
func (m *request) Perform(verb string, path string) Request {
switch verb {
case "Post", http.MethodPost:
return m.Post(path)
case "Put", http.MethodPut:
return m.Put(path)
case "Get", http.MethodGet:
return m.Get(path)
case "Delete", http.MethodDelete:
return m.Delete(path)
case "Options", http.MethodOptions:
return m.Options(path)
case "Patch", http.MethodPatch:
return m.Patch(path)
default:
m.t.Fatalf("unknown verb: %s", verb)
return m
}
}
func (m *request) Performf(verb string, path string, a ...interface{}) Request {
switch verb {
case "Post", http.MethodPost:
return m.Postf(path, a...)
case "Put", http.MethodPut:
return m.Putf(path, a...)
case "Get", http.MethodGet:
return m.Getf(path, a...)
case "Delete", http.MethodDelete:
return m.Deletef(path, a...)
case "Options", http.MethodOptions:
return m.Optionsf(path, a...)
case "Patch", http.MethodPatch:
return m.Patchf(path, a...)
default:
m.t.Fatalf("unknown verb: %s", verb)
return m
}
}
// responseImpl implements Response.
type responseImpl struct {
t *testing.T
response *httptest.ResponseRecorder
request *request
}
// New returns a new Request instance for the given router.
func New(t *testing.T, router http.Handler) Request {
return &request{
t: t,
router: router,
headers: map[string]string{},
}
}
func (m *request) Post(path string) Request {
m.method = http.MethodPost
m.url = path
return m
}
func (m *request) Postf(format string, a ...interface{}) Request {
m.method = http.MethodPost
m.url = fmt.Sprintf(format, a...)
return m
}
func (m *request) Put(path string) Request {
m.method = http.MethodPut
m.url = path
return m
}
func (m *request) Putf(format string, a ...interface{}) Request {
m.method = http.MethodPut
m.url = fmt.Sprintf(format, a...)
return m
}
func (m *request) Get(path string) Request {
m.method = http.MethodGet
m.url = path
return m
}
func (m *request) Getf(format string, a ...interface{}) Request {
m.method = http.MethodGet
m.url = fmt.Sprintf(format, a...)
return m
}
func (m *request) Options(path string) Request {
m.method = http.MethodOptions
m.url = path
return m
}
func (m *request) Optionsf(format string, a ...interface{}) Request {
m.method = http.MethodOptions
m.url = fmt.Sprintf(format, a...)
return m
}
func (m *request) Delete(path string) Request {
m.method = http.MethodDelete
m.url = path
return m
}
func (m *request) Deletef(format string, a ...interface{}) Request {
m.method = http.MethodDelete
m.url = fmt.Sprintf(format, a...)
return m
}
func (m *request) Patch(path string) Request {
m.method = http.MethodPatch
m.url = path
return m
}
func (m *request) Patchf(format string, a ...interface{}) Request {
m.method = http.MethodPatch
m.url = fmt.Sprintf(format, a...)
return m
}
func (m *request) AuthHeader(header string) Request {
m.headers["Authorization"] = header
return m
}
func (m *request) Headers(headers map[string]string) Request {
m.headers = headers
return m
}
func (m *request) Header(key, value string) Request {
m.headers[key] = value
return m
}
func (m *request) AuthJwt(jwt string) Request {
m.AuthHeader("bearer " + jwt)
return m
}
func (m *request) ContentType(ct string) Request {
m.contentType = ct
return m
}
func (m *request) Content(body string) Request {
m.body = body
return m
}
func (m *request) JsonContent(body string) Request {
m.ContentType("application/json")
m.Content(body)
return m
}
func (m *request) JsonContentObject(obj any) Request {
marshal, err := json.Marshal(obj)
require.NoError(m.t, err)
m.JsonContent(string(marshal))
return m
}
func (m *request) ContentFile(path string) Request {
content, err := os.ReadFile(path)
if err != nil {
assert.Fail(m.t, err.Error())
}
m.body = string(content)
return m
}
func (m *request) JsonContentFile(path string) Request {
m.ContentType("application/json")
m.ContentFile(path)
return m
}
func (m *request) Expect() Response {
if m.router == nil {
assert.Fail(m.t, "Request router is nil")
}
req, err := http.NewRequest(m.method, m.url, bytes.NewBufferString(m.body))
if err != nil {
assert.Fail(m.t, err.Error())
}
if m.contentType != "" {
req.Header.Set("Content-Type", m.contentType)
}
for k, v := range m.headers {
req.Header.Set(k, v)
}
rec := httptest.NewRecorder()
m.router.ServeHTTP(rec, req)
m.response = rec
return &responseImpl{t: m.t, response: rec, request: m}
}
func (m *request) ExpectEventually(assertions func(resp Response), timeout, interval time.Duration) Response {
if m.router == nil {
assert.Fail(m.t, "Request router is nil")
}
deadline := time.Now().Add(timeout)
var lastResp *responseImpl
for {
req, err := http.NewRequest(m.method, m.url, bytes.NewBufferString(m.body))
if err != nil {
assert.Fail(m.t, err.Error())
break
}
if m.contentType != "" {
req.Header.Set("Content-Type", m.contentType)
}
for k, v := range m.headers {
req.Header.Set(k, v)
}
rec := httptest.NewRecorder()
m.router.ServeHTTP(rec, req)
m.response = rec
resp := &responseImpl{t: m.t, response: rec, request: m}
lastResp = resp
sandbox := &testing.T{}
assertions(&responseImpl{t: sandbox, response: rec, request: m})
if sandbox.Failed() {
resp.Log()
}
if !sandbox.Failed() {
return resp
}
if time.Now().After(deadline) {
assert.Fail(m.t, fmt.Sprintf("ExpectEventually: condition not met within %v", timeout))
resp.Log()
return resp
}
time.Sleep(interval)
}
return lastResp
}