This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathhttp_test.go
304 lines (238 loc) · 7.36 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
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
package checkers
import (
"math"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"fmt"
. "github.com/onsi/gomega"
)
func TestNewHTTP(t *testing.T) {
RegisterTestingT(t)
t.Run("Happy path", func(t *testing.T) {
u, err := url.Parse("http://testing.com/search?q=foo")
Expect(err).ToNot(HaveOccurred())
h, err := NewHTTP(&HTTPConfig{
URL: u,
})
Expect(err).ToNot(HaveOccurred())
Expect(h).ToNot(BeNil())
})
t.Run("Should error with a nil cfg", func(t *testing.T) {
h, err := NewHTTP(nil)
Expect(h).To(BeNil())
Expect(err.Error()).To(ContainSubstring("Passed in config cannot be nil"))
})
t.Run("Should error when prepare fails", func(t *testing.T) {
h, err := NewHTTP(&HTTPConfig{})
Expect(h).To(BeNil())
Expect(err.Error()).To(ContainSubstring("URL cannot be nil"))
})
}
func TestDo(t *testing.T) {
RegisterTestingT(t)
t.Run("Should error with unparsable payload", func(t *testing.T) {
h := &HTTP{
Config: &HTTPConfig{
Payload: math.NaN(),
},
}
res, err := h.do()
Expect(err).To(HaveOccurred())
Expect(res).To(BeNil())
Expect(err.Error()).To(ContainSubstring("error parsing payload"))
})
t.Run("Should error if request can't be created", func(t *testing.T) {
u, _ := url.Parse("http://google.com")
h := &HTTP{
Config: &HTTPConfig{
Payload: "foo",
Method: "bad method",
URL: u,
},
}
res, err := h.do()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Unable to create new HTTP request for HTTPMonitor check"))
Expect(res).To(BeNil())
})
}
func TestPrepare(t *testing.T) {
RegisterTestingT(t)
t.Run("Should error when URL is not set", func(t *testing.T) {
h := &HTTPConfig{}
err := h.prepare()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("URL cannot be nil"))
})
t.Run("Should set appropriate defaults", func(t *testing.T) {
u, _ := url.Parse("http://google.com")
h := &HTTPConfig{URL: u}
err := h.prepare()
Expect(err).ToNot(HaveOccurred())
Expect(h.StatusCode).To(Equal(http.StatusOK))
Expect(h.Method).To(Equal("GET"))
Expect(h.Timeout).To(Equal(defaultHTTPTimeout))
Expect(h.Client.Timeout).To(Equal(h.Timeout))
})
t.Run("Custom http client timeout should be updated", func(t *testing.T) {
u, _ := url.Parse("http://google.com")
h := &HTTPConfig{URL: u, Client: &http.Client{}, Timeout: time.Duration(1) * time.Second}
err := h.prepare()
Expect(err).ToNot(HaveOccurred())
Expect(h.Client.Timeout).To(Equal(time.Duration(1) * time.Second))
})
}
func TestParsePayload(t *testing.T) {
RegisterTestingT(t)
t.Run("Happy path", func(t *testing.T) {
reader, err := parsePayload("test")
Expect(err).ToNot(HaveOccurred())
Expect(reader).ToNot(BeNil())
reader, err = parsePayload([]byte("test"))
Expect(err).ToNot(HaveOccurred())
Expect(reader).ToNot(BeNil())
foo := struct {
Name string
}{
Name: "foo",
}
reader, err = parsePayload(foo)
Expect(err).ToNot(HaveOccurred())
Expect(reader).ToNot(BeNil())
})
t.Run("should return nil if no payload is passed in", func(t *testing.T) {
reader, err := parsePayload(nil)
Expect(err).ToNot(HaveOccurred())
Expect(reader).To(BeNil())
})
t.Run("should error with payload that can't be marshalled to json", func(t *testing.T) {
reader, err := parsePayload(math.NaN())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to marshal json body"))
Expect(reader).To(BeNil())
})
}
func TestHTTPStatus(t *testing.T) {
RegisterTestingT(t)
t.Run("Happy path", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
testURL, err := url.Parse(ts.URL)
Expect(err).ToNot(HaveOccurred())
cfg := &HTTPConfig{
URL: testURL,
}
checker, err := NewHTTP(cfg)
Expect(err).ToNot(HaveOccurred())
data, err := checker.Status()
Expect(err).ToNot(HaveOccurred())
Expect(data).To(BeNil())
})
t.Run("Should return error if HTTP call fails", func(t *testing.T) {
testURL, _ := url.Parse("no-scheme.com")
cfg := &HTTPConfig{
URL: testURL,
}
checker, err := NewHTTP(cfg)
Expect(err).ToNot(HaveOccurred())
data, err := checker.Status()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("unsupported protocol"))
Expect(data).To(BeNil())
})
t.Run("Should return error if expected response status code does not match received status code", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer ts.Close()
testURL, err := url.Parse(ts.URL)
Expect(err).ToNot(HaveOccurred())
cfg := &HTTPConfig{
URL: testURL,
}
checker, err := NewHTTP(cfg)
Expect(err).ToNot(HaveOccurred())
data, err := checker.Status()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("does not match expected status code"))
Expect(data).To(BeNil())
})
t.Run("Should return error if response data does not contain expected data", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("foo"))
}))
defer ts.Close()
testURL, err := url.Parse(ts.URL)
Expect(err).ToNot(HaveOccurred())
cfg := &HTTPConfig{
URL: testURL,
Expect: "bar",
}
checker, err := NewHTTP(cfg)
Expect(err).ToNot(HaveOccurred())
data, err := checker.Status()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("does not contain expected content"))
Expect(data).To(BeNil())
})
t.Run("Should not error if expected data in response is found", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("foo"))
}))
defer ts.Close()
testURL, err := url.Parse(ts.URL)
Expect(err).ToNot(HaveOccurred())
cfg := &HTTPConfig{
URL: testURL,
Expect: "foo",
}
checker, err := NewHTTP(cfg)
Expect(err).ToNot(HaveOccurred())
data, err := checker.Status()
Expect(err).ToNot(HaveOccurred())
Expect(data).To(BeNil())
})
t.Run("Should return error if response body is not readable", func(t *testing.T) {
httpClient := &http.Client{
Transport: newTransport(),
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("foo"))
}))
defer ts.Close()
testURL, err := url.Parse(ts.URL)
Expect(err).ToNot(HaveOccurred())
cfg := &HTTPConfig{
URL: testURL,
Expect: "foo",
Client: httpClient,
}
checker, err := NewHTTP(cfg)
Expect(err).ToNot(HaveOccurred())
data, err := checker.Status()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Unable to read response body to perform content expectancy check"))
Expect(data).To(BeNil())
})
}
type CustomTransport struct{}
func newTransport() *CustomTransport {
return &CustomTransport{}
}
func (c *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: &mockReader{},
}, nil
}
type mockReader struct{}
func (m *mockReader) Read(p []byte) (n int, err error) { return 0, fmt.Errorf("foo") }
func (m *mockReader) Close() error { return nil }