-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhandlers_test.go
250 lines (195 loc) · 5.77 KB
/
handlers_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
package main_test
import (
. "github.com/cloudfoundry-samples/pong_matcher_go"
. "github.com/cloudfoundry-samples/pong_matcher_go/domain"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
)
func stubbedMatchRequestRetrieval(success bool) MatchRequestRetriever {
return func(uuid string) (bool, MatchRequest, error) {
mr := MatchRequest{}
return success, mr, nil
}
}
func stubbedMatchRetrieval(success bool) MatchRetriever {
return func(uuid string) (bool, Match) {
m := Match{}
return success, m
}
}
var _ = Describe("Request handlers", func() {
wellFormedJson := func() io.Reader { return strings.NewReader(`{"player": "some-player"}`) }
blankBody := func() io.Reader { return strings.NewReader("") }
Describe("getting a match request", func() {
Context("when a match request is found", func() {
It("responds with 200", func() {
handle := GetMatchRequestHandler(stubbedMatchRequestRetrieval(true))
resp := httptest.NewRecorder()
req, err := http.NewRequest(
"GET", "/match_requests/foo", nil,
)
handle(resp, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Code).To(Equal(200))
})
})
Context("when a match request is not found", func() {
It("responds with 404", func() {
handle := GetMatchRequestHandler(stubbedMatchRequestRetrieval(false))
resp := httptest.NewRecorder()
req, err := http.NewRequest(
"GET", "/match_requests/foo", nil,
)
handle(resp, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Code).To(Equal(404))
})
})
Context("when selecting the match request produces an error", func() {
errorFetch := func(uuid string) (bool, MatchRequest, error) {
return false, MatchRequest{}, errors.New("Bad stuff")
}
handle := GetMatchRequestHandler(errorFetch)
It("responds with 500", func() {
resp := httptest.NewRecorder()
req, err := http.NewRequest(
"GET", "/match_requests/foo", nil,
)
handle(resp, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Code).To(Equal(500))
})
})
})
Describe("creating a match request", func() {
Context("when persistence works", func() {
nullPersist := func(mr MatchRequest) error { return nil }
handle := CreateMatchRequestHandler(nullPersist)
Context("with a well-formed body", func() {
It("responds with 200", func() {
resp := httptest.NewRecorder()
req, err := http.NewRequest(
"PUT", "/match_requests/foo", wellFormedJson(),
)
handle(resp, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Code).To(Equal(200))
})
})
Context("without a body", func() {
It("responds with 400", func() {
resp := httptest.NewRecorder()
req, err := http.NewRequest(
"PUT", "/match_requests/foo", blankBody(),
)
handle(resp, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Code).To(Equal(400))
})
})
})
Context("when persistence fails", func() {
errorPersist := func(mr MatchRequest) error {
return errors.New("Bad stuff")
}
handle := CreateMatchRequestHandler(errorPersist)
It("responds with 500", func() {
resp := httptest.NewRecorder()
req, err := http.NewRequest(
"PUT", "/match_requests/foo", wellFormedJson(),
)
handle(resp, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Code).To(Equal(500))
})
})
})
Describe("getting a match", func() {
Context("when a match is found", func() {
It("responds with 200", func() {
handle := MatchHandler(stubbedMatchRetrieval(true))
resp := httptest.NewRecorder()
req, err := http.NewRequest(
"GET", "/matches/foo", nil,
)
handle(resp, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Code).To(Equal(200))
})
})
Context("when a match is not found", func() {
It("responds with 404", func() {
handle := MatchHandler(stubbedMatchRetrieval(false))
resp := httptest.NewRecorder()
req, err := http.NewRequest(
"GET", "/matches/foo", nil,
)
handle(resp, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Code).To(Equal(404))
})
})
})
Describe("posting results of a match", func() {
nullPersist := func(r Result) error { return nil }
Context("with a well-formed body", func() {
handle := ResultsHandler(nullPersist)
It("responds with 201", func() {
resp := httptest.NewRecorder()
req, err := http.NewRequest(
"POST", "/results", wellFormedJson(),
)
handle(resp, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Code).To(Equal(201))
})
})
Context("without a body", func() {
handle := ResultsHandler(nullPersist)
It("responds with 400", func() {
resp := httptest.NewRecorder()
req, err := http.NewRequest(
"POST", "/results", blankBody(),
)
handle(resp, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Code).To(Equal(400))
})
})
Context("when persistence produces an error", func() {
errorPersist := func(r Result) error {
return errors.New("Bad stuff")
}
handle := ResultsHandler(errorPersist)
It("responds with 500", func() {
resp := httptest.NewRecorder()
req, err := http.NewRequest(
"POST", "/results", wellFormedJson(),
)
handle(resp, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Code).To(Equal(500))
})
})
})
Describe("wiping the database", func() {
Context("when deletion produces an error", func() {
errorDelete := func() error {
return errors.New("Bad stuff")
}
handle := AllHandler(errorDelete)
It("responds with 500", func() {
resp := httptest.NewRecorder()
req, err := http.NewRequest("DELETE", "/all", nil)
handle(resp, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Code).To(Equal(500))
})
})
})
})