forked from quii/mockingjay-server
/
compatability_test.go
268 lines (218 loc) · 7.33 KB
/
compatability_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
package main
import (
"fmt"
"github.com/quii/mockingjay-server/mockingjay"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestItMatchesHeaders(t *testing.T) {
yaml := `
---
- name: Endpoint with response headers
request:
uri: /hello
method: GET
response:
code: 200
body: 'ok'
headers:
content-type: text/json
`
checker := NewCompatabilityChecker()
endpoints, err := mockingjay.NewFakeEndpoints([]byte(yaml))
if err != nil {
t.Fatal(err)
}
realServerWithoutHeaders := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "ok")
}))
realServerWithHeaders := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-TYPE", "text/json")
fmt.Fprint(w, "ok")
}))
if checker.CheckCompatability(endpoints, realServerWithoutHeaders.URL) {
t.Error("Checker should've found downstream server to be incompatible as it did not include the response headers we expected")
}
if !checker.CheckCompatability(endpoints, realServerWithHeaders.URL) {
t.Error("Checker shouldve found downstream server to be compatible")
}
}
func TestItMatchesBodiesAsStrings(t *testing.T) {
body := "Chris"
downstreamBody := "Christopher"
realServer := makeFakeDownstreamServer(downstreamBody, noSleep)
checker := NewCompatabilityChecker()
endpoints := makeEndpoints(body)
if !checker.CheckCompatability(endpoints, realServer.URL) {
t.Error("Checker should've found this endpoint to be correct")
}
}
func TestItFailsIncompatibleStrings(t *testing.T) {
body := "Chris"
downstreamBody := "Pixies"
realServer := makeFakeDownstreamServer(downstreamBody, noSleep)
checker := NewCompatabilityChecker()
endpoints := makeEndpoints(body)
if checker.CheckCompatability(endpoints, realServer.URL) {
t.Error("Checker should've found this endpoint to be incompatible")
}
}
func TestItChecksAValidEndpointsJSON(t *testing.T) {
body := `{"foo":"bar"}`
realServer := makeFakeDownstreamServer(body, noSleep)
checker := NewCompatabilityChecker()
endpoints := makeEndpoints(body)
if !checker.CheckCompatability(endpoints, realServer.URL) {
t.Error("Checker should've found this endpoint to be correct")
}
}
func TestItChecksAValidEndpointsXML(t *testing.T) {
body := `<foo><bar>x</bar></foo>`
realServerBody := `<foo><bar>y</bar></foo>`
realServer := makeFakeDownstreamServer(realServerBody, noSleep)
checker := NewCompatabilityChecker()
endpoints := makeEndpoints(body)
if !checker.CheckCompatability(endpoints, realServer.URL) {
t.Error("Checker should've found this endpoint to be correct")
}
}
func TestItFlagsDifferentJSONToBeIncompatible(t *testing.T) {
serverResponseBody := `{"foo": "bar"}`
fakeResponseBody := `{"baz": "boo"}`
realServer := makeFakeDownstreamServer(serverResponseBody, noSleep)
checker := NewCompatabilityChecker()
endpoints := makeEndpoints(fakeResponseBody)
if checker.CheckCompatability(endpoints, realServer.URL) {
t.Error("Checker should've found this endpoint to be incorrect")
}
}
func TestItChecksStatusCodes(t *testing.T) {
body := `{"foo": "bar"}`
realServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(body))
}))
checker := NewCompatabilityChecker()
endpoints := makeEndpoints(body)
if checker.CheckCompatability(endpoints, realServer.URL) {
t.Error("Checker should've found this endpoint to be incorrect")
}
}
func TestIfItsNotJSONItDoesAStringMatch(t *testing.T) {
serverResponseBody := "hello world"
fakeResponseBody := "hello bob"
realServer := makeFakeDownstreamServer(serverResponseBody, noSleep)
checker := NewCompatabilityChecker()
endpoints := makeEndpoints(fakeResponseBody)
if checker.CheckCompatability(endpoints, realServer.URL) {
t.Error("Checker should've found this endpoint to be incorrect")
}
}
func TestIfItsNotJSONItKnowsItsCompatable(t *testing.T) {
body := "hello world"
realServer := makeFakeDownstreamServer(body, noSleep)
checker := NewCompatabilityChecker()
endpoints := makeEndpoints(body)
if !checker.CheckCompatability(endpoints, realServer.URL) {
t.Error("Checker should've found this endpoint to be correct")
}
}
func TestIfItsNotJSONItAllowsAnyBody(t *testing.T) {
body := "hello world"
realServer := makeFakeDownstreamServer(body, noSleep)
checker := NewCompatabilityChecker()
endpoints := makeEndpoints("*")
if !checker.CheckCompatability(endpoints, realServer.URL) {
t.Error("Checker should've found this endpoint to be correct")
}
}
func TestItIsIncompatibleWhenRealServerIsntReachable(t *testing.T) {
body := "doesnt matter"
checker := NewCompatabilityChecker()
endpoints := makeEndpoints(body)
if checker.CheckCompatability(endpoints, "http://localhost:12344") {
t.Error("Checker shouldve found this to be an error as the real server isnt reachable")
}
}
func TestItHandlesBadURLsInConfig(t *testing.T) {
yaml := fmt.Sprintf(yamlFormat, "not a real url", "foobar")
fakeEndPoints, _ := mockingjay.NewFakeEndpoints([]byte(yaml))
checker := NewCompatabilityChecker()
if checker.CheckCompatability(fakeEndPoints, "also not a real url") {
t.Error("Checker should've found that the URL in the YAML cannot be made into a request")
}
}
// https://github.com/quii/mockingjay-server/issues/3
func TestWhitespaceSensitivity(t *testing.T) {
y := `
---
- name: Testing whitespace sensitivity
request:
uri: /hello
method: POST
body: '{"email":"foo@bar.com","password":"xxx"}'
response:
code: 200
body: '{"token": "1234abc"}'
`
fakeEndPoints, _ := mockingjay.NewFakeEndpoints([]byte(y))
checker := NewCompatabilityChecker()
realServer := makeFakeDownstreamServer(`{"token": "1234abc"}`, noSleep)
if !checker.CheckCompatability(fakeEndPoints, realServer.URL) {
t.Error("Checker should've found that the two JSONs are compatible despite having different whitespace")
}
}
// this is panicing in goconvey?
func TestErrorReportingOfEmptyJSONArrays(t *testing.T) {
y := `
---
- name: Testing whitespace sensitivity
request:
uri: /hello
method: POST
body: '{"email":"foo@bar.com","password":"xxx"}'
response:
code: 200
body: '{"stuff": [{"foo":"bar"}]}'
`
fakeEndPoints, err := mockingjay.NewFakeEndpoints([]byte(y))
if err != nil {
t.Fatal(err)
}
checker := NewCompatabilityChecker()
realServer := makeFakeDownstreamServer(`{"stuff":[]}`, noSleep)
if checker.CheckCompatability(fakeEndPoints, realServer.URL) {
t.Error("Checker shouldn't have found it compatible because its got an empty array downstream")
}
}
const noSleep = 1
const defaultRequestURI = "/hello"
const yamlFormat = `
---
- name: Test endpoint
request:
uri: %s
method: GET
response:
code: 200
body: '%s'
`
func makeFakeDownstreamServer(responseBody string, sleepTime time.Duration) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(sleepTime * time.Millisecond)
if r.URL.RequestURI() == defaultRequestURI {
fmt.Fprint(w, responseBody)
} else {
http.Error(w, "Not found", http.StatusNotFound)
}
}))
}
func makeEndpoints(body string) []mockingjay.FakeEndpoint {
e, _ := mockingjay.NewFakeEndpoints([]byte(testYAML(body)))
return e
}
func testYAML(responseBody string) string {
return fmt.Sprintf(yamlFormat, defaultRequestURI, responseBody)
}