forked from Eun/go-hit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.go
467 lines (436 loc) · 11.4 KB
/
static.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package hit
import (
"net/http"
"os"
"io"
"fmt"
"github.com/Eun/go-hit/errortrace"
"github.com/Eun/go-hit/internal"
)
//nolint:gochecknoglobals
var ett *errortrace.ErrorTraceTemplate
//nolint:gochecknoinits
func init() {
ett = errortrace.New(
"testing",
"runtime",
errortrace.IgnorePackage(Do),
)
}
// Send sends the specified data as the body payload
//
// Examples:
// MustDo(
// Post("https://example.com"),
// Send("Hello World"),
// )
//
// MustDo(
// Post("https://example.com"),
// Send().Body("Hello World")
// )
func Send(data ...interface{}) ISend {
return newSend(newClearPath("Send", data), data)
}
// Expect expects the body to be equal the specified value, omit the parameter to get more options
//
// Examples:
// MustDo(
// Get("https://example.com"),
// Expect().Body().Contains("Hello World")
// )
//
// MustDo(
// Get("https://example.com"),
// Expect("Hello World"),
// )
func Expect(data ...interface{}) IExpect {
return newExpect(newClearPath("Expect", data), data)
}
// Debug prints the current Request and Response to hit.Stdout(), you can filter the output based on expressions
//
// Examples:
// MustDo(
// Get("https://example.com"),
// Debug(),
// )
//
// MustDo(
// Get("https://example.com"),
// Debug("Response.Headers"),
// )
func Debug(expression ...string) IStep {
return newDebug(expression)
}
// HTTPClient sets the client for the request
//
// Example:
// var client http.Client
// MustDo(
// Get("https://example.com"),
// HTTPClient(&client),
// )
func HTTPClient(client *http.Client) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: BeforeSendStep,
ClearPath: nil, // not clearable
Exec: func(hit Hit) error {
hit.SetHTTPClient(client)
return nil
},
}
}
// Stdout sets the output to the specified writer
//
// Example:
// MustDo(
// Get("https://example.com"),
// Stdout(os.Stderr),
// Debug(),
// )
func Stdout(w io.Writer) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: BeforeSendStep,
ClearPath: nil, // not clearable
Exec: func(hit Hit) error {
hit.SetStdout(w)
return nil
},
}
}
// BaseURL sets the base url for each Connect, Delete, Get, Head, Post, Options, Put, Trace or Method
//
// Examples:
// MustDo(
// BaseURL("https://example.com")
// )
//
// MustDo(
// BaseURL("https://%s/%s", "example.com", "index.html")
// )
func BaseURL(url string, a ...interface{}) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: BeforeSendStep,
ClearPath: nil, // not clearable
Exec: func(hit Hit) error {
hit.SetBaseURL(url, a...)
return nil
},
}
}
// Request creates a new Hit instance with an existing http request
//
// Example:
// request, _ := http.NewRequest(http.MethodGet, "https://example.com", nil)
// MustDo(
// Request(request),
// )
func Request(request *http.Request) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: BeforeSendStep,
ClearPath: nil, // not clearable
Exec: func(hit Hit) error {
hit.SetRequest(request)
return nil
},
}
}
// Method creates a new Hit instance with the specified method and url
//
// Examples:
// MustDo(
// Method(http.MethodGet, "https://example.com"),
// )
//
// MustDo(
// Method(http.MethodGet, "https://%s/%s", "example.com", "index.html"),
// )
func Method(method, url string, a ...interface{}) IStep {
return makeMethodStep(method, url, a...)
}
func makeMethodStep(method, url string, a ...interface{}) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: BeforeSendStep,
ClearPath: nil, // not clearable
Exec: func(hit Hit) error {
request, err := http.NewRequest(method, internal.MakeURL(hit.BaseURL(), url, a...), nil)
if err != nil {
return err
}
// remove some standard headers
request.Header.Set("User-Agent", "")
hit.SetRequest(request)
return nil
},
}
}
// Connect creates a new Hit instance with CONNECT as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Connect("https://example.com"),
// )
//
// MustDo(
// Connect("https://%s/%s", "example.com", "index.html"),
// )
func Connect(url string, a ...interface{}) IStep {
return makeMethodStep(http.MethodConnect, url, a...)
}
// Delete creates a new Hit instance with DELETE as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Delete("https://example.com"),
// )
//
// MustDo(
// Delete("https://%s/%s", "example.com", "index.html"),
// )
//
func Delete(url string, a ...interface{}) IStep {
return makeMethodStep(http.MethodDelete, url, a...)
}
// Get creates a new Hit instance with GET as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Get("https://example.com"),
// )
//
// MustDo(
// Get("https://%s/%s", "example.com", "index.html"),
// )
//
func Get(url string, a ...interface{}) IStep {
return makeMethodStep(http.MethodGet, url, a...)
}
// Head creates a new Hit instance with HEAD as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Head("https://example.com"),
// )
//
// MustDo(
// Head("https://%s/%s", "example.com", "index.html"),
// )
//
func Head(url string, a ...interface{}) IStep {
return makeMethodStep(http.MethodHead, url, a...)
}
// Post creates a new Hit instance with POST as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Post("https://example.com"),
// )
//
// MustDo(
// Post("https://%s/%s", "example.com", "index.html"),
// )
//
func Post(url string, a ...interface{}) IStep {
return makeMethodStep(http.MethodPost, url, a...)
}
// Options creates a new Hit instance with OPTIONS as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Options("https://example.com"),
// )
//
// MustDo(
// Options("https://%s/%s", "example.com", "index.html"),
// )
//
func Options(url string, a ...interface{}) IStep {
return makeMethodStep(http.MethodOptions, url, a...)
}
// Put creates a new Hit instance with PUT as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Put("https://example.com"),
// )
//
// MustDo(
// Put("https://%s/%s", "example.com", "index.html"),
// )
//
func Put(url string, a ...interface{}) IStep {
return makeMethodStep(http.MethodPut, url, a...)
}
// Trace creates a new Hit instance with TRACE as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Trace("https://example.com"),
// )
//
// MustDo(
// Trace("https://%s/%s", "example.com", "index.html"),
// )
//
func Trace(url string, a ...interface{}) IStep {
return makeMethodStep(http.MethodTrace, url, a...)
}
// Test runs the specified steps and calls t.Error() if any error occurs during execution
func Test(t TestingT, steps ...IStep) {
if err := Do(steps...); err != nil {
if _, ok := err.(errortrace.ErrorTraceError); !ok {
os.Stderr.WriteString(ett.Format("", err.Error()).Error())
t.FailNow()
}
os.Stderr.WriteString(err.Error())
t.FailNow()
}
}
// Do runs the specified steps and returns error if something was wrong
func Do(steps ...IStep) error {
hit := &defaultInstance{
client: http.DefaultClient,
stdout: os.Stdout,
steps: steps,
state: CombineStep,
}
if err := hit.runSteps(CombineStep); err != nil {
return err
}
hit.state = CleanStep
if err := hit.runSteps(CleanStep); err != nil {
return err
}
hit.state = BeforeSendStep
if err := hit.runSteps(BeforeSendStep); err != nil {
return err
}
if hit.request == nil {
return fmt.Errorf("unable to perform request: no request set, did you called Post(), Get(), ...?")
}
hit.state = SendStep
if err := hit.runSteps(SendStep); err != nil {
return err
}
hit.state = AfterSendStep
if err := hit.runSteps(AfterSendStep); err != nil {
return err
}
hit.request.Request.Body = hit.request.Body().Reader()
res, err := hit.client.Do(hit.request.Request)
if err != nil {
return fmt.Errorf("unable to perform request: %s", err.Error())
}
hit.response = newHTTPResponse(hit, res)
hit.state = BeforeExpectStep
if err := hit.runSteps(BeforeExpectStep); err != nil {
return err
}
hit.state = ExpectStep
if err := hit.runSteps(ExpectStep); err != nil {
return err
}
hit.state = AfterExpectStep
if err := hit.runSteps(AfterExpectStep); err != nil {
return err
}
if hit.request.Request.Body != nil {
if err := hit.request.Request.Body.Close(); err != nil {
return err
}
}
return err
}
// MustDo runs the specified steps and panics with the error if something was wrong
func MustDo(steps ...IStep) {
if err := Do(steps...); err != nil {
panic(err)
}
}
// CombineSteps combines multiple steps to one
//
// Example:
// MustDo(
// Get("https://example.com"),
// CombineSteps(
// Expect().Status(http.StatusOK),
// Expect().Body("Hello World"),
// ),
// )
func CombineSteps(steps ...IStep) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: CombineStep,
ClearPath: nil, // not clearable
Exec: func(hit Hit) error {
hit.InsertSteps(steps...)
return nil
},
}
}
// Description sets a custom description for this test.
// The description will be printed in an error case
//
// Example:
// MustDo(
// Description("Check if example.com is available"),
// Get("https://example.com"),
// )
func Description(description string) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: BeforeSendStep,
ClearPath: nil, // not clearable
Exec: func(hit Hit) error {
hit.SetDescription(description)
return nil
},
}
}
// Clear can be used to remove previous steps.
//
// Usage:
// Clear().Send("Hello World") // will remove all Send("Hello World") steps
// Clear().Send().Body("Hello World") // will remove all Send().Body("Hello World") steps
// Clear().Expect().Body() // will remove all Expect().Body() steps and all chained steps to Body() e.g. Expect().Body().Equal("Hello World")
// Clear().Expect().Body("Hello World") // will remove all Expect().Body("Hello World") steps
//
// Example:
// MustDo(
// Post("https://example.com"),
// Expect().Status(http.StatusOK),
// Expect().Body().Contains("Welcome to example.com"),
// Clear().Expect(),
// Expect().Status(http.NotFound),
// Expect().Body().Contains("Not found!"),
// )
func Clear() IClear {
return newClear()
}
// Custom can be used to run custom logic during various steps.
//
// Example:
// MustDo(
// Post("https://example.com"),
// Custom(ExpectStep, func(hit Hit) {
// if hit.Response().Body().String() != "Hello Earth" {
// panic("Expected Hello Earth")
// }
// }),
// )
func Custom(when StepTime, exec Callback) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: when,
ClearPath: newClearPath("Custom", []interface{}{when, exec}),
Exec: func(hit Hit) error {
exec(hit)
return nil
},
}
}