|
1
|
|
|
package http |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"errors" |
|
5
|
|
|
"io" |
|
6
|
|
|
"io/ioutil" |
|
7
|
|
|
"net/http" |
|
8
|
|
|
"testing" |
|
9
|
|
|
"time" |
|
10
|
|
|
|
|
11
|
|
|
"github.com/stretchr/testify/assert" |
|
12
|
|
|
) |
|
13
|
|
|
|
|
14
|
|
|
func TestRequest(t *testing.T) { |
|
15
|
|
|
req := &Request{ |
|
16
|
|
|
Method: "GET", |
|
17
|
|
|
Protocol: "http", |
|
18
|
|
|
Host: "www.aliyun.com", |
|
19
|
|
|
Path: "/", |
|
20
|
|
|
} |
|
21
|
|
|
assert.Equal(t, "GET http://www.aliyun.com/", req.BuildRequestURL()) |
|
22
|
|
|
// With query |
|
23
|
|
|
req = &Request{ |
|
24
|
|
|
Method: "GET", |
|
25
|
|
|
Protocol: "http", |
|
26
|
|
|
Host: "www.aliyun.com", |
|
27
|
|
|
Path: "/", |
|
28
|
|
|
Queries: map[string]string{ |
|
29
|
|
|
"spm": "test", |
|
30
|
|
|
}, |
|
31
|
|
|
} |
|
32
|
|
|
assert.Equal(t, "GET http://www.aliyun.com/?spm=test", req.BuildRequestURL()) |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
func TestDoGet(t *testing.T) { |
|
36
|
|
|
req := &Request{ |
|
37
|
|
|
Method: "GET", |
|
38
|
|
|
Protocol: "http", |
|
39
|
|
|
Host: "www.aliyun.com", |
|
40
|
|
|
Path: "/", |
|
41
|
|
|
} |
|
42
|
|
|
res, err := Do(req) |
|
43
|
|
|
assert.Nil(t, err) |
|
44
|
|
|
assert.NotNil(t, res) |
|
45
|
|
|
assert.Equal(t, 200, res.StatusCode) |
|
46
|
|
|
assert.Equal(t, "text/html; charset=utf-8", res.Headers["Content-Type"]) |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
func TestDoPost(t *testing.T) { |
|
50
|
|
|
req := &Request{ |
|
51
|
|
|
Method: "POST", |
|
52
|
|
|
Protocol: "http", |
|
53
|
|
|
Host: "www.aliyun.com", |
|
54
|
|
|
Path: "/", |
|
55
|
|
|
Form: map[string]string{ |
|
56
|
|
|
"URL": "HI", |
|
57
|
|
|
}, |
|
58
|
|
|
Headers: map[string]string{ |
|
59
|
|
|
"Accept-Language": "zh", |
|
60
|
|
|
}, |
|
61
|
|
|
} |
|
62
|
|
|
res, err := Do(req) |
|
63
|
|
|
assert.Nil(t, err) |
|
64
|
|
|
assert.NotNil(t, res) |
|
65
|
|
|
assert.Equal(t, 200, res.StatusCode) |
|
66
|
|
|
assert.Equal(t, "text/html; charset=utf-8", res.Headers["Content-Type"]) |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
type errorReader struct { |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
func (r *errorReader) Read(p []byte) (n int, err error) { |
|
73
|
|
|
err = errors.New("read failed") |
|
74
|
|
|
return |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
func TestDoWithError(t *testing.T) { |
|
78
|
|
|
originNewRequest := newRequest |
|
79
|
|
|
defer func() { newRequest = originNewRequest }() |
|
80
|
|
|
|
|
81
|
|
|
// case 1: mock new http request failed |
|
82
|
|
|
newRequest = func(method, url string, body io.Reader) (*http.Request, error) { |
|
83
|
|
|
return nil, errors.New("new http request failed") |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
req := &Request{ |
|
87
|
|
|
Method: "POST", |
|
88
|
|
|
Protocol: "http", |
|
89
|
|
|
Host: "www.aliyun.com", |
|
90
|
|
|
Path: "/", |
|
91
|
|
|
Form: map[string]string{ |
|
92
|
|
|
"URL": "HI", |
|
93
|
|
|
}, |
|
94
|
|
|
Headers: map[string]string{ |
|
95
|
|
|
"Accept-Language": "zh", |
|
96
|
|
|
}, |
|
97
|
|
|
} |
|
98
|
|
|
_, err := Do(req) |
|
99
|
|
|
assert.EqualError(t, err, "new http request failed") |
|
100
|
|
|
|
|
101
|
|
|
// reset new request |
|
102
|
|
|
newRequest = originNewRequest |
|
103
|
|
|
|
|
104
|
|
|
// case 2: server error |
|
105
|
|
|
originDo := hookDo |
|
106
|
|
|
defer func() { hookDo = originDo }() |
|
107
|
|
|
hookDo = func(fn do) do { |
|
108
|
|
|
return func(req *http.Request) (res *http.Response, err error) { |
|
109
|
|
|
err = errors.New("mock server error") |
|
110
|
|
|
return |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
_, err = Do(req) |
|
114
|
|
|
assert.EqualError(t, err, "mock server error") |
|
115
|
|
|
|
|
116
|
|
|
// case 4: mock read response error |
|
117
|
|
|
hookDo = func(fn do) do { |
|
118
|
|
|
return func(req *http.Request) (res *http.Response, err error) { |
|
119
|
|
|
res = &http.Response{ |
|
120
|
|
|
Proto: "HTTP/1.1", |
|
121
|
|
|
ProtoMajor: 1, |
|
122
|
|
|
ProtoMinor: 1, |
|
123
|
|
|
Header: map[string][]string{}, |
|
124
|
|
|
StatusCode: 200, |
|
125
|
|
|
Status: "200 " + http.StatusText(200), |
|
126
|
|
|
} |
|
127
|
|
|
res.Body = ioutil.NopCloser(&errorReader{}) |
|
128
|
|
|
return |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
_, err = Do(req) |
|
133
|
|
|
assert.EqualError(t, err, "read failed") |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
func TestDoWithProxy(t *testing.T) { |
|
137
|
|
|
req := &Request{ |
|
138
|
|
|
Method: "POST", |
|
139
|
|
|
Protocol: "http", |
|
140
|
|
|
Host: "www.aliyun.com", |
|
141
|
|
|
Path: "/", |
|
142
|
|
|
Form: map[string]string{ |
|
143
|
|
|
"URL": "HI", |
|
144
|
|
|
}, |
|
145
|
|
|
Headers: map[string]string{ |
|
146
|
|
|
"Accept-Language": "zh", |
|
147
|
|
|
}, |
|
148
|
|
|
Proxy: "http://localhost:9999/", |
|
149
|
|
|
} |
|
150
|
|
|
_, err := Do(req) |
|
151
|
|
|
assert.Contains(t, err.Error(), "proxyconnect tcp: dial tcp") |
|
152
|
|
|
assert.Contains(t, err.Error(), "connect: connection refused") |
|
153
|
|
|
|
|
154
|
|
|
// invalid proxy url |
|
155
|
|
|
req.Proxy = string([]byte{0x7f}) |
|
156
|
|
|
_, err = Do(req) |
|
157
|
|
|
assert.Contains(t, err.Error(), "net/url: invalid control character in URL") |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
func TestDoWithConnectTimeout(t *testing.T) { |
|
161
|
|
|
req := &Request{ |
|
162
|
|
|
Method: "POST", |
|
163
|
|
|
Protocol: "http", |
|
164
|
|
|
Host: "www.aliyun.com", |
|
165
|
|
|
Path: "/", |
|
166
|
|
|
Form: map[string]string{ |
|
167
|
|
|
"URL": "HI", |
|
168
|
|
|
}, |
|
169
|
|
|
Headers: map[string]string{ |
|
170
|
|
|
"Accept-Language": "zh", |
|
171
|
|
|
}, |
|
172
|
|
|
ConnectTimeout: 1 * time.Nanosecond, |
|
173
|
|
|
} |
|
174
|
|
|
_, err := Do(req) |
|
175
|
|
|
assert.Contains(t, err.Error(), "dial tcp: ") |
|
176
|
|
|
assert.Contains(t, err.Error(), "i/o timeout") |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
func TestDoWithReadTimeout(t *testing.T) { |
|
180
|
|
|
req := &Request{ |
|
181
|
|
|
Method: "POST", |
|
182
|
|
|
Protocol: "http", |
|
183
|
|
|
Host: "www.aliyun.com", |
|
184
|
|
|
Path: "/", |
|
185
|
|
|
ReadTimeout: 1 * time.Nanosecond, |
|
186
|
|
|
} |
|
187
|
|
|
_, err := Do(req) |
|
188
|
|
|
assert.Contains(t, err.Error(), "(Client.Timeout exceeded while awaiting headers)") |
|
189
|
|
|
} |
|
190
|
|
|
|