-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
61 lines (52 loc) · 1.46 KB
/
example_test.go
File metadata and controls
61 lines (52 loc) · 1.46 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
package testcase_test
import (
"context"
"net/http"
"testing"
"time"
"github.com/go-tk/testcase"
"github.com/stretchr/testify/assert"
)
func TestExample(t *testing.T) {
type C struct { // C for context
// prepare
ctx context.Context
url string
// check
resp *http.Response
err error
}
tc := testcase.New(func(t *testing.T, c *C) {
c.ctx = context.Background() // default
testcase.Callback(t, "PREPARE")
req, _ := http.NewRequestWithContext(c.ctx, "GET", c.url, nil)
c.resp, c.err = http.DefaultClient.Do(req)
testcase.Callback(t, "CHECK")
})
// CASE-1: http client gets https://httpbin.org/delay/60 with timeout 100ms
// should return with deadline exceeded error.
tc.WithTag("delay-60").
WithCallback("PREPARE", func(t *testing.T, c *C) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
t.Cleanup(cancel)
c.ctx = ctx
c.url = "https://httpbin.org/delay/60"
}).
WithCallback("CHECK", func(t *testing.T, c *C) {
assert.ErrorIs(t, c.err, context.DeadlineExceeded)
}).
RunParallel(t)
// CASE-2: http client gets https://httpbin.org/status/201
// should respond with the status code 201.
tc.WithTag("status-201").
WithCallback("PREPARE", func(t *testing.T, c *C) {
c.url = "https://httpbin.org/status/201"
}).
WithCallback("CHECK", func(t *testing.T, c *C) {
if c.err != nil {
t.Fatal(c.err)
}
assert.Equal(t, c.resp.StatusCode, 201)
}).
RunParallel(t)
}