-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
107 lines (68 loc) · 1.89 KB
/
http_test.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
package http_test
import (
"context"
"net/url"
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/go-waitfor/waitfor"
"github.com/go-waitfor/waitfor-http"
)
func TestUse(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
uStr := "http://api.test.com/"
httpmock.RegisterResponder("GET", uStr,
httpmock.NewStringResponder(200, `OK`))
w := waitfor.New(http.Use())
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
err := w.Test(ctx, []string{uStr})
assert.NoError(t, err)
}
func TestHTTP(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
uStr := "http://api.test.com/"
httpmock.RegisterResponder("GET", uStr,
httpmock.NewStringResponder(200, `OK`))
u, err := url.Parse(uStr)
assert.NoError(t, err)
r, err := http.New(u)
assert.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = r.Test(ctx)
assert.NoError(t, err)
}
func TestHTTP_Failed(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
uStr := "http://foobar"
httpmock.RegisterResponder("GET", uStr,
httpmock.NewStringResponder(404, ``))
u, err := url.Parse(uStr)
assert.NoError(t, err)
r, err := http.New(u)
assert.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = r.Test(ctx)
assert.Error(t, err)
}
func TestHTTPS(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
uStr := "https://api.test.com/"
httpmock.RegisterResponder("GET", uStr,
httpmock.NewStringResponder(200, `OK`))
u, err := url.Parse(uStr)
assert.NoError(t, err)
r, err := http.New(u)
assert.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = r.Test(ctx)
assert.NoError(t, err)
}