generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
executable file
·371 lines (289 loc) · 8.03 KB
/
server.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package httpmock
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"sync"
"github.com/swaggest/assertjson"
"github.com/swaggest/assertjson/json5"
)
// Expectation describes expected request and defines response.
type Expectation struct {
Method string
RequestURI string
RequestHeader map[string]string
RequestCookie map[string]string
RequestBody []byte
Status int
ResponseHeader map[string]string
ResponseBody []byte
// Unlimited enables reusing of this expectation unlimited number of times.
Unlimited bool
// Repeated defines how many times this expectation should be used.
Repeated int
}
// Server serves predefined response for predefined request.
type Server struct {
// OnError is called on expectations mismatch or internal errors, optional.
OnError func(err error)
// OnRequest is called on before every request, optional.
OnRequest func(rw http.ResponseWriter, req *http.Request)
// ErrorResponder allows custom failure responses.
ErrorResponder func(rw http.ResponseWriter, err error)
// DefaultResponseHeaders are added to every response to an expected request.
DefaultResponseHeaders map[string]string
// JSONComparer controls JSON equality check.
JSONComparer assertjson.Comparer
// OnBodyMismatch is called when received body does not match expected, optional.
OnBodyMismatch func(received []byte)
mu sync.Mutex
server *httptest.Server
expectations []Expectation
async []Expectation
}
// NewServer creates mocked server.
func NewServer() (*Server, string) {
m := Server{}
m.server = httptest.NewServer(&m)
m.JSONComparer = assertjson.Comparer{IgnoreDiff: assertjson.IgnoreDiff}
return &m, m.server.URL
}
// Expect adds expected operation.
func (sm *Server) Expect(e Expectation) {
sm.mu.Lock()
defer sm.mu.Unlock()
sm.expectations = append(sm.expectations, e)
}
// ExpectAsync sets non-sequential expectation.
//
// Asynchronous expectations are checked for every incoming request,
// first match is used for response.
// If there are no matches, regular (sequential expectations are used).
func (sm *Server) ExpectAsync(e Expectation) {
sm.mu.Lock()
defer sm.mu.Unlock()
sm.async = append(sm.async, e)
}
// Close closes mocked server.
func (sm *Server) Close() {
sm.server.Close()
}
func (sm *Server) prepareBody(data []byte) ([]byte, error) {
if sm.JSONComparer.Vars == nil {
return data, nil
}
if !json.Valid(data) {
return data, nil
}
for k, v := range sm.JSONComparer.Vars.GetAll() {
j, err := json.Marshal(v)
if err != nil {
return nil, err
}
data = bytes.ReplaceAll(data, []byte(`"`+k+`"`), j)
}
return data, nil
}
func (sm *Server) writeResponse(rw http.ResponseWriter, expectation Expectation) bool {
var err error
if sm.DefaultResponseHeaders != nil {
for k, v := range sm.DefaultResponseHeaders {
rw.Header().Set(k, v)
}
}
if expectation.ResponseHeader != nil {
for k, v := range expectation.ResponseHeader {
rw.Header().Set(k, v)
}
}
if expectation.Status == 0 {
expectation.Status = http.StatusOK
}
rw.WriteHeader(expectation.Status)
expectation.ResponseBody, err = sm.prepareBody(expectation.ResponseBody)
if sm.checkFail(rw, err) {
return false
}
_, err = rw.Write(expectation.ResponseBody)
return !sm.checkFail(rw, err)
}
func (sm *Server) checkAsync(rw http.ResponseWriter, req *http.Request) bool {
for i, expectation := range sm.async {
if err := sm.checkRequest(req, expectation); err != nil {
continue
}
if !sm.writeResponse(rw, expectation) {
return true
}
if expectation.Unlimited {
return true
}
if expectation.Repeated > 0 {
expectation.Repeated--
sm.async[i] = expectation
if expectation.Repeated > 0 {
return true
}
}
// Deleting expectation.
sm.async[i] = sm.async[len(sm.async)-1]
sm.async = sm.async[:len(sm.async)-1]
return true
}
return false
}
// ServeHTTP asserts request expectations and serves mocked response.
func (sm *Server) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
sm.mu.Lock()
defer sm.mu.Unlock()
if sm.OnRequest != nil {
sm.OnRequest(rw, req)
}
if sm.checkAsync(rw, req) {
return
}
if len(sm.expectations) == 0 {
body, err := ioutil.ReadAll(req.Body)
if err == nil && len(body) > 0 {
if sm.OnBodyMismatch != nil {
sm.OnBodyMismatch(body)
}
sm.checkFail(rw, fmt.Errorf("unexpected request received: %s %s, body:\n%s", req.Method,
req.RequestURI, string(body)))
} else {
sm.checkFail(rw, fmt.Errorf("unexpected request received: %s %s", req.Method, req.RequestURI))
}
return
}
expectation := sm.expectations[0]
err := sm.checkRequest(req, expectation)
if sm.checkFail(rw, err) {
return
}
if !sm.writeResponse(rw, expectation) {
return
}
if expectation.Unlimited {
return
}
if expectation.Repeated > 0 {
expectation.Repeated--
sm.expectations[0] = expectation
if expectation.Repeated > 0 {
return
}
}
sm.expectations = sm.expectations[1:]
}
func (sm *Server) checkBody(expected, received []byte) (err error) {
defer func() {
if err != nil && sm.OnBodyMismatch != nil {
sm.OnBodyMismatch(received)
}
}()
if !json5.Valid(expected) || !json5.Valid(received) {
if !bytes.Equal(expected, received) {
return errors.New("unexpected request body")
}
} else {
// Performing JSON comparison for JSON payloads and binary comparison otherwise.
expected, err := json5.Downgrade(expected)
if err != nil {
return err
}
if err = sm.JSONComparer.FailNotEqual(expected, received); err != nil {
return fmt.Errorf("unexpected request body: %w", err)
}
}
return nil
}
func (sm *Server) checkRequest(req *http.Request, expectation Expectation) error {
if expectation.Method != "" && expectation.Method != req.Method {
return fmt.Errorf("method %q expected, %q received", expectation.Method, req.Method)
}
if expectation.RequestURI != "" && expectation.RequestURI != req.RequestURI {
return fmt.Errorf("request uri %q expected, %q received", expectation.RequestURI, req.RequestURI)
}
for k, v := range expectation.RequestHeader {
if req.Header.Get(k) != v {
return fmt.Errorf("header %q with value %q expected, %q received", k, v, req.Header.Get(k))
}
}
for n, v := range expectation.RequestCookie {
c, err := req.Cookie(n)
if err != nil {
return fmt.Errorf("failed to find cookie %q with value %q: %w", n, v, err)
}
if c.Value != v {
return fmt.Errorf("header %q with value %q expected, %q received", n, v, c.Value)
}
}
reqBody, err := ioutil.ReadAll(req.Body)
if err != nil {
return err
}
if expectation.RequestBody == nil {
return nil
}
return sm.checkBody(expectation.RequestBody, reqBody)
}
func (sm *Server) checkFail(rw http.ResponseWriter, err error) bool {
if err == nil {
return false
}
if sm.OnError != nil {
sm.OnError(err)
}
if sm.ErrorResponder != nil {
sm.ErrorResponder(rw, err)
return true
}
rw.WriteHeader(http.StatusInternalServerError)
_, err = rw.Write([]byte(err.Error()))
if err != nil && sm.OnError != nil {
sm.OnError(err)
}
return true
}
// ResetExpectations discards all expectation to reset the state of mock.
func (sm *Server) ResetExpectations() {
sm.mu.Lock()
defer sm.mu.Unlock()
sm.expectations = nil
sm.async = nil
}
// ExpectationsWereMet checks whether all queued expectations
// were met in order. If any of them was not met - an error is returned.
func (sm *Server) ExpectationsWereMet() error {
sm.mu.Lock()
defer sm.mu.Unlock()
var unmet string
for _, e := range sm.expectations {
if e.Unlimited {
continue
}
if e.Method != "" || e.RequestURI != "" {
unmet += ", " + e.Method + " " + e.RequestURI
} else {
unmet += ", response " + string(e.ResponseBody)
}
}
for _, e := range sm.async {
if e.Unlimited {
continue
}
if e.Method != "" || e.RequestURI != "" {
unmet += ", " + e.Method + " " + e.RequestURI
} else {
unmet += ", response " + string(e.ResponseBody)
}
}
if unmet != "" {
return errors.New("there are remaining expectations that were not met: " + unmet[2:])
}
return nil
}