forked from rehearse/rehearse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stub_handler_test.go
292 lines (237 loc) · 7.22 KB
/
stub_handler_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
package main
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"testing"
)
func init() {
log.SetOutput(ioutil.Discard)
}
type processedResponse struct {
statusCode int
header http.Header
body []byte
}
func mustEncodeStub(t *testing.T, stub Stub) (buf *bytes.Buffer) {
buf = new(bytes.Buffer)
encoder := json.NewEncoder(buf)
if err := encoder.Encode(stub); err != nil {
t.Fatalf("Unable to JSON encode: %#v", stub)
}
return
}
func mustPost(t *testing.T, url string, body io.Reader) (pr *processedResponse) {
resp, err := http.Post(url, "application/json", body)
if err != nil {
t.Fatalf("Unable to POST: %v", err)
}
pr = &processedResponse{statusCode: resp.StatusCode, header: resp.Header}
pr.body, err = ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
return
}
func mustGet(t *testing.T, url string) (pr *processedResponse) {
resp, err := http.Get(url)
if err != nil {
t.Fatal(err)
}
pr = &processedResponse{statusCode: resp.StatusCode, header: resp.Header}
pr.body, err = ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
return
}
func TestStubEndpoint(t *testing.T) {
handler := NewStubHandler()
ts := httptest.NewServer(handler)
stub := Stub{Method: "GET", Path: "/foo", Body: `{"foo":"bar"}`}
postBody := mustEncodeStub(t, stub)
mustPost(t, ts.URL+"/stubs", postBody)
resp := mustGet(t, ts.URL+"/foo")
if resp.statusCode != 200 {
t.Errorf("Status was incorrect, expected 200, got: %v", resp.statusCode)
}
if string(resp.body) != `{"foo":"bar"}` {
t.Errorf("Unexpected response body: %s", string(resp.body))
}
}
func TestStubEndpointWithQuerystring(t *testing.T) {
handler := NewStubHandler()
ts := httptest.NewServer(handler)
stub := Stub{Method: "GET", Path: "/foo", Body: `{"foo":"bar"}`}
stubWithQuery := Stub{Method: "GET", Path: "/foo?bar=baz", Body: `{"baz":"bur"}`}
postBody := mustEncodeStub(t, stub)
mustPost(t, ts.URL+"/stubs", postBody)
postBody = mustEncodeStub(t, stubWithQuery)
mustPost(t, ts.URL+"/stubs", postBody)
resp := mustGet(t, ts.URL+"/foo?bar=baz")
if resp.statusCode != 200 {
t.Errorf("Status was incorrect, expected 200, got: %v", resp.statusCode)
}
if string(resp.body) != `{"baz":"bur"}` {
t.Errorf("Unexpected response body: %s", string(resp.body))
}
}
func TestStubEndpointWithQuerystringOrdering(t *testing.T) {
handler := NewStubHandler()
ts := httptest.NewServer(handler)
stub := Stub{Method: "GET", Path: "/foo?bar=baz&baz=bar", Body: `{"foo":"bar"}`}
postBody := mustEncodeStub(t, stub)
mustPost(t, ts.URL+"/stubs", postBody)
resp := mustGet(t, ts.URL+"/foo?baz=bar&bar=baz")
if resp.statusCode != 200 {
t.Errorf("Status was incorrect, expected 200, got: %v", resp.statusCode)
}
if string(resp.body) != `{"foo":"bar"}` {
t.Errorf("Unexpected response body: %s", string(resp.body))
}
}
func TestStubEndpointWithMultipleMethods(t *testing.T) {
handler := NewStubHandler()
ts := httptest.NewServer(handler)
stub := Stub{Method: "GET", Path: "/foo", Body: `get response`}
postBody := mustEncodeStub(t, stub)
mustPost(t, ts.URL+"/stubs", postBody)
stub = Stub{Method: "POST", Path: "/foo", Body: `post response`}
postBody = mustEncodeStub(t, stub)
mustPost(t, ts.URL+"/stubs", postBody)
resp := mustGet(t, ts.URL+"/foo")
if resp.statusCode != 200 {
t.Errorf("Status was incorrect, expected 200, got: %v", resp.statusCode)
}
if string(resp.body) != `get response` {
t.Errorf("Unexpected response body: %s", string(resp.body))
}
resp = mustPost(t, ts.URL+"/foo", &bytes.Buffer{})
if resp.statusCode != 200 {
t.Errorf("Status was incorrect, expected 200, got: %v", resp.statusCode)
}
if string(resp.body) != `post response` {
t.Errorf("Unexpected response body: %s", string(resp.body))
}
}
func TestStubEndpointWithHeaders(t *testing.T) {
handler := NewStubHandler()
ts := httptest.NewServer(handler)
stub := Stub{Method: "GET", Path: "/foo", Body: `get response`, Status: 404}
postBody := mustEncodeStub(t, stub)
mustPost(t, ts.URL+"/stubs", postBody)
resp := mustGet(t, ts.URL+"/foo")
if resp.statusCode != 404 {
t.Errorf("Status was incorrect, expected 404, got: %v", resp.statusCode)
}
}
func TestGetStubs(t *testing.T) {
var stubs []Stub
handler := NewStubHandler()
ts := httptest.NewServer(handler)
stub := Stub{Method: "GET", Path: "/foo", Body: `{"foo":"bar"}`}
postBody := mustEncodeStub(t, stub)
mustPost(t, ts.URL+"/stubs", postBody)
resp := mustGet(t, ts.URL+"/stubs")
decoder := json.NewDecoder(bytes.NewBuffer(resp.body))
decoder.Decode(&stubs)
if len(stubs) != 1 {
t.Errorf("Expected one stub, got %d.", len(stubs))
}
}
func TestClear(t *testing.T) {
var stubs []Stub
handler := NewStubHandler()
ts := httptest.NewServer(handler)
stub := Stub{Method: "GET", Path: "/foo", Body: `{"foo":"bar"}`}
postBody := mustEncodeStub(t, stub)
mustPost(t, ts.URL+"/stubs", postBody)
resp := mustGet(t, ts.URL+"/stubs")
decoder := json.NewDecoder(bytes.NewBuffer(resp.body))
decoder.Decode(&stubs)
if len(stubs) != 1 {
t.Errorf("Expected one stub, got %d.", len(stubs))
}
client := http.Client{}
req, err := http.NewRequest("DELETE", ts.URL+"/stubs", bytes.NewBuffer([]byte("")))
client.Do(req)
if err != nil {
t.Error(err)
}
resp = mustGet(t, ts.URL+"/stubs")
decoder = json.NewDecoder(bytes.NewBuffer(resp.body))
decoder.Decode(&stubs)
if len(stubs) != 0 {
t.Errorf("Expected zero stubs, got %d.", len(stubs))
}
}
func TestStubMalformedRequest(t *testing.T) {
handler := NewStubHandler()
ts := httptest.NewServer(handler)
postBody := bytes.NewBuffer([]byte("not JSON"))
resp, err := http.Post(ts.URL+"/stubs", "application/json", postBody)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 400 {
t.Fatalf("Status was incorrect, expected 400, got: %v", resp.StatusCode)
}
}
func TestUnhandledStub(t *testing.T) {
handler := NewStubHandler()
ts := httptest.NewServer(handler)
resp, err := http.Get(ts.URL + "/bar")
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 404 {
t.Fatalf("Status was incorrect, expected 404, got: %v", resp.StatusCode)
}
}
func TestStubFallback(t *testing.T) {
handler := NewStubHandler()
handler.fallbackHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
_, err := io.WriteString(w, "fallback")
if err != nil {
t.Fatal(err)
}
})
ts := httptest.NewServer(handler)
resp := mustGet(t, ts.URL+"/foo/bar")
if resp.statusCode != 200 {
t.Errorf("Status was incorrect, expected 200, got: %v", resp.statusCode)
}
if string(resp.body) != "fallback" {
t.Errorf("Unexpected response body: %#v", string(resp.body))
}
}
func TestLoad(t *testing.T) {
handler := NewStubHandler()
buff := new(bytes.Buffer)
json := `
[
{ "method": "GET", "path": "/foo", "body": "bar"},
{ "method": "GET", "path": "/baz", "body": "quz"}
]
`
buff.WriteString(json)
err := handler.load(buff)
if err != nil {
t.Fatalf("Unexpected error during load: %v", err)
}
ts := httptest.NewServer(handler)
resp := mustGet(t, ts.URL+"/foo")
if string(resp.body) != "bar" {
t.Errorf("Unexpected response body: %#v", string(resp.body))
}
resp = mustGet(t, ts.URL+"/baz")
if string(resp.body) != "quz" {
t.Errorf("Unexpected response body: %#v", string(resp.body))
}
}