-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
static.go
624 lines (579 loc) · 15.9 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package hit
import (
"context"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/Eun/go-hit/internal/misc"
"golang.org/x/xerrors"
"github.com/Eun/go-hit/errortrace"
urlpkg "net/url"
)
//nolint:gochecknoglobals // ett is used to initialize the errortrace, we always want to ignore some packages, so using
// a global would help to avoid unnecessary initializations.
var ett *errortrace.Template
//nolint:gochecknoinits // ett is used to initialize the errortrace, we always want to ignore some packages, so using
// a global would help to avoid unnecessary initializations.
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().Body().String("Hello World"),
// )
//
// MustDo(
// Post("https://example.com"),
// Send().Body().Int(8),
// )
func Send() ISend {
return newSend(newCallPath("Send", nil))
}
// Expect provides assertions for the response data, e.g. for body or headers.
//
// Examples:
// MustDo(
// Get("https://example.com"),
// Expect().Body().String().Equal("Hello World"),
// )
//
// MustDo(
// Get("https://example.com"),
// Expect().Body().String().Contains("Hello World"),
// )
func Expect() IExpect {
return newExpect(newCallPath("Expect", nil))
}
// Debug prints the current Request and Response.
//
// Examples:
// MustDo(
// Get("https://example.com"),
// Debug(),
// )
//
// MustDo(
// Get("https://example.com"),
// Debug().Request().Headers(),
// Debug().Response().Headers("Content-Type"),
// )
func Debug() IDebug {
return newDebug(newCallPath("Debug", nil), nil)
}
// Fdebug prints the current Request and Response to the specified writer.
//
// Examples:
// MustDo(
// Get("https://example.com"),
// Fdebug(os.Stderr),
// )
//
// MustDo(
// Get("https://example.com"),
// Debug().Request().Headers(),
// Debug().Response().Headers("Content-Type"),
// )
func Fdebug(w io.Writer) IDebug {
return newDebug(newCallPath("Fdebug", nil), w)
}
// Store stores the current Request or Response.
//
// Examples:
// var body string
// MustDo(
// Get("https://example.com"),
// Store().Response().Body().String().In(&body),
// )
//
// var headers http.Header
// MustDo(
// Get("https://example.com"),
// Store().Response().Headers().In(&headers),
// )
// var contentType string
// MustDo(
// Get("https://example.com"),
// Store().Response().Headers("Content-Type").In(&contentType),
// )
func Store() IStore {
return newStore()
}
// HTTPClient sets the client for the request.
//
// Example:
// client := http.DefaultClient
// MustDo(
// Get("https://example.com"),
// HTTPClient(client),
// )
func HTTPClient(client *http.Client) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: requestCreateStep,
CallPath: newCallPath("HTTPClient", nil),
Exec: func(hit *hitImpl) error {
return hit.SetHTTPClient(client)
},
}
}
// BaseURL sets the base url for each Connect, Delete, Get, Head, Post, Options, Put, Trace or Method.
//
// Examples:
// MustDo(
// BaseURL("https://example.com/"),
// Get("index.html"),
// )
//
// MustDo(
// BaseURL("https://%s.%s/", "example", "com"),
// Get("index.html"),
// )
func BaseURL(url string, a ...interface{}) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: requestCreateStep,
CallPath: newCallPath("BaseURL", nil),
Exec: func(hit *hitImpl) error {
if len(a) > 0 {
hit.baseURL = fmt.Sprintf(url, a...)
return nil
}
hit.baseURL = url
return nil
},
}
}
// Request provides methods to set request parameters.
//
// Example:
// request, _ := http.NewRequest(http.MethodGet, "https://example.com", nil)
// MustDo(
// Request().Set(request),
// )
func Request() IRequest {
return newRequest(newCallPath("Request", nil))
}
// Method sets 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", method, url, a...)
}
func makeMethodStep(fnName, method, url string, a ...interface{}) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: requestCreateStep,
CallPath: newCallPath(fnName, nil),
Exec: func(hit *hitImpl) error {
hit.request.Method = method
u := misc.MakeURL(hit.baseURL, url, a...)
if u == "" {
hit.request.URL = new(urlpkg.URL)
return nil
}
var err error
hit.request.URL, err = urlpkg.Parse(u)
if err != nil {
return err
}
return nil
},
}
}
// Connect sets the the method to CONNECT and the specified url.
func Connect(url string, a ...interface{}) IStep {
return makeMethodStep("Connect", http.MethodConnect, url, a...)
}
// Delete sets the the method to DELETE and the specified 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("Delete", http.MethodDelete, url, a...)
}
// Get sets the the method to GET and the specified url.
//
// Examples:
// MustDo(
// Get("https://example.com"),
// )
//
// MustDo(
// Get("https://%s/%s", "example.com", "index.html"),
// Expect().Status().Equal(http.StatusOK),
// Expect().Body().String().Equal("Hello World"),
// )
//
func Get(url string, a ...interface{}) IStep {
return makeMethodStep("Get", http.MethodGet, url, a...)
}
// Head sets the the method to HEAD and the specified 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("Head", http.MethodHead, url, a...)
}
// Post sets the the method to POST and the specified url.
//
// Examples:
// MustDo(
// Post("https://example.com"),
// )
//
// MustDo(
// Post("https://%s.%s", "example", "com"),
// Expect().Status().Equal(http.StatusOK),
// Send().Body().String("Hello World"),
// )
//
func Post(url string, a ...interface{}) IStep {
return makeMethodStep("Post", http.MethodPost, url, a...)
}
// Options sets the the method to OPTIONS and the specified 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("Options", http.MethodOptions, url, a...)
}
// Put sets the the method to PUT and the specified url.
//
// Examples:
// MustDo(
// Put("https://example.com"),
// )
//
// MustDo(
// Put("https://%s,%s", "example", "com"),
// )
//
func Put(url string, a ...interface{}) IStep {
return makeMethodStep("Put", http.MethodPut, url, a...)
}
// Trace sets the the method to TRACE and the specified 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("Trace", http.MethodTrace, url, a...)
}
// Test runs the specified steps and calls t.FailNow() if any error occurs during execution.
func Test(t TestingT, steps ...IStep) {
if err := Do(steps...); err != nil {
_, _ = os.Stderr.WriteString(err.Error())
t.FailNow()
}
}
// do func that ensures we always return an *ErrorTrace.
func do(steps ...IStep) *Error {
hit := &hitImpl{
client: http.DefaultClient,
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.request = newHTTPRequest(hit, nil)
hit.request.Header = map[string][]string{
// remove some standard headers
"User-Agent": {""},
}
hit.state = requestCreateStep
if err := hit.runSteps(requestCreateStep); err != nil {
return err
}
// user did not specify a request with Request().Set()
if hit.request.Method == "" || hit.request.URL == nil || hit.request.URL.Host == "" {
return wrapError(hit, xerrors.New("unable to create a request: did you called Post(), Get(), ...?"))
}
if hit.request.URL.Scheme == "" {
hit.request.URL.Scheme = "https"
}
hit.state = BeforeSendStep
if err := hit.runSteps(BeforeSendStep); err != nil {
return err
}
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 wrapError(hit, xerrors.Errorf("unable to perform request: %w", err))
}
hit.request.Request.ContentLength, err = hit.request.Body().Length()
if err != nil {
return wrapError(hit, xerrors.Errorf("unable to get body length: %w", err))
}
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 wrapError(hit, err)
}
}
if hit.response.Response.Body != nil {
if err := hit.response.Response.Body.Close(); err != nil {
return wrapError(hit, err)
}
}
return nil
}
// Do runs the specified steps and returns error if something was wrong.
func Do(steps ...IStep) error {
if err := do(steps...); err != nil {
return err
}
return nil
}
// 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().Equal(http.StatusOK),
// Expect().Body().String().Equal("Hello World"),
// ),
// )
func CombineSteps(steps ...IStep) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: combineStep,
CallPath: newCallPath("CombineSteps", nil),
Exec: func(hit *hitImpl) 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: requestCreateStep,
CallPath: newCallPath("Description", nil),
Exec: func(hit *hitImpl) error {
hit.SetDescription(description)
return nil
},
}
}
// Clear can be used to remove previous steps.
//
// Usage:
// Clear().Send() // will remove all steps chained to Send()
// // e.g Send().Body().String("Hello World")
// Clear().Send().Body() // will remove all steps chained to Send().Body()
// // e.g Send().Body().String("Hello World")
// Clear().Send().Body().String("Hello World") // will remove all Send().Body().String("Hello World")
// // steps
// Clear().Expect().Body() // will remove all steps chained to Expect().Body()
// // e.g. Expect().Body().Equal("Hello World")
// Clear().Expect().Body().String().Equal("Hello World") // will remove all
// // Expect().Body().String().Equal("Hello World") steps
//
// Example:
// MustDo(
// Get("https://example.com"),
// Expect().Status().Equal(http.StatusNotFound),
// Expect().Body().String().Contains("Not found!"),
// Clear().Expect(),
// Expect().Status().Equal(http.StatusOK),
// Expect().Body().String().Contains("Hello World"),
// )
func Clear() IClear {
return newClear(newCallPath("Clear", nil))
}
// Custom can be used to run custom logic during various steps.
//
// Example:
// MustDo(
// Get("https://example.com"),
// Custom(ExpectStep, func(hit Hit) error {
// if hit.Response().Body().MustString() != "Hello World" {
// return errors.New("Expected Hello World")
// }
// return nil
// }),
// )
func Custom(when StepTime, exec Callback) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: when,
CallPath: newCallPath("Custom", []interface{}{when, exec}),
Exec: func(hit *hitImpl) error {
return exec(hit)
},
}
}
// Return stops the current execution of hit, resulting in ignoring all future steps.
//
// Example:
// MustDo(
// Get("https://example.com"),
// Return(),
// Expect().Body().String().Equal("Hello World"), // will never be executed
// )
func Return() IStep {
return &hitStep{
Trace: ett.Prepare(),
When: cleanStep,
CallPath: nil,
Exec: func(hit *hitImpl) error {
currentStep := hit.CurrentStep()
removeAllFollowing := false
var stepsToRemove []IStep
for _, step := range hit.Steps() {
if step == currentStep {
removeAllFollowing = true
continue
}
if removeAllFollowing {
stepsToRemove = append(stepsToRemove, step)
}
}
hit.RemoveSteps(stepsToRemove...)
return nil
},
}
}
// Context sets the context for the request.
//
// Example:
// ctx, cancel := context.WithTimeout(context.Background(), time.Second)
// defer cancel()
// MustDo(
// Context(ctx),
// Get("https://example.com"),
// Return(),
// Expect().Body().String().Equal("Hello World"), // will never be executed
// )
func Context(ctx context.Context) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: requestCreateStep,
CallPath: nil,
Exec: func(hit *hitImpl) error {
hit.request.Request = hit.request.Request.WithContext(ctx)
return nil
},
}
}
// JoinURL joins the specified parts to one url.
//
// Example:
// JoinURL("https://example.com", "folder", "file") // will return "https://example.com/folder/file"
// JoinURL("https://example.com", "index.html") // will return "https://example.com/index.html"
// JoinURL("https://", "example.com", "index.html") // will return "https://example.com/index.html"
// JoinURL("example.com", "index.html") // will return "example.com/index.html"
// MustDo(
// Get(JoinURL("https://example.com", "index.html")),
// )
func JoinURL(parts ...string) string {
if len(parts) == 0 {
return ""
}
notEmptyParts := make([]string, 0, len(parts))
for _, part := range parts {
if part == "" {
continue
}
notEmptyParts = append(notEmptyParts, part)
}
if len(notEmptyParts) == 0 {
return ""
}
u, err := urlpkg.Parse(strings.Join(notEmptyParts, "/"))
if err != nil {
return ""
}
// replace all "double slashes"
for {
old := u.Path
u.Path = strings.ReplaceAll(u.Path, "//", "/")
if old == u.Path {
break
}
}
return strings.TrimRight(strings.ReplaceAll(u.String(), ":///", "://"), "/")
}