forked from goadesign/goa
/
middleware_test.go
355 lines (309 loc) · 9.51 KB
/
middleware_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
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
package goa_test
import (
"fmt"
"net/http"
"regexp"
"strings"
"time"
"gopkg.in/inconshreveable/log15.v2"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/raphael/goa"
)
var _ = Describe("NewMiddleware", func() {
var input interface{}
var middleware goa.Middleware
var mErr error
JustBeforeEach(func() {
middleware, mErr = goa.NewMiddleware(input)
})
Context("using a goa Middleware", func() {
var goaMiddleware goa.Middleware
BeforeEach(func() {
goaMiddleware = func(h goa.Handler) goa.Handler { return h }
input = goaMiddleware
})
It("returns the middleware", func() {
Ω(fmt.Sprintf("%#v", middleware)).Should(Equal(fmt.Sprintf("%#v", goaMiddleware)))
Ω(mErr).ShouldNot(HaveOccurred())
})
})
Context("using a goa middleware func", func() {
var goaMiddlewareFunc func(goa.Handler) goa.Handler
BeforeEach(func() {
goaMiddlewareFunc = func(h goa.Handler) goa.Handler { return h }
input = goaMiddlewareFunc
})
It("returns the middleware", func() {
Ω(fmt.Sprintf("%#v", middleware)).Should(Equal(fmt.Sprintf("%#v", goa.Middleware(goaMiddlewareFunc))))
Ω(mErr).ShouldNot(HaveOccurred())
})
})
Context("with a context", func() {
var ctx *goa.Context
BeforeEach(func() {
req, err := http.NewRequest("GET", "/goo", nil)
Ω(err).ShouldNot(HaveOccurred())
rw := new(TestResponseWriter)
params := map[string]string{"foo": "bar"}
query := map[string][]string{"filter": []string{"one"}}
ctx = goa.NewContext(nil, req, rw, params, query, nil)
Ω(ctx.ResponseStatus()).Should(Equal(0))
})
Context("using a goa handler", func() {
BeforeEach(func() {
var goaHandler goa.Handler = func(ctx *goa.Context) error {
ctx.JSON(200, "ok")
return nil
}
input = goaHandler
})
It("wraps it in a middleware", func() {
Ω(mErr).ShouldNot(HaveOccurred())
h := func(ctx *goa.Context) error { return nil }
Ω(middleware(h)(ctx)).ShouldNot(HaveOccurred())
Ω(ctx.ResponseStatus()).Should(Equal(200))
})
})
Context("using a goa handler func", func() {
BeforeEach(func() {
input = func(ctx *goa.Context) error {
ctx.JSON(200, "ok")
return nil
}
})
It("wraps it in a middleware", func() {
Ω(mErr).ShouldNot(HaveOccurred())
h := func(ctx *goa.Context) error { return nil }
Ω(middleware(h)(ctx)).ShouldNot(HaveOccurred())
Ω(ctx.ResponseStatus()).Should(Equal(200))
})
})
Context("using a http middleware func", func() {
BeforeEach(func() {
input = func(h http.Handler) http.Handler { return h }
})
It("wraps it in a middleware", func() {
Ω(mErr).ShouldNot(HaveOccurred())
h := func(c *goa.Context) error { c.JSON(200, "ok"); return nil }
Ω(middleware(h)(ctx)).ShouldNot(HaveOccurred())
Ω(ctx.ResponseStatus()).Should(Equal(200))
})
})
Context("using a http handler", func() {
BeforeEach(func() {
var httpHandler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
w.WriteHeader(200)
})
input = httpHandler
})
It("wraps it in a middleware", func() {
Ω(mErr).ShouldNot(HaveOccurred())
h := func(ctx *goa.Context) error { return nil }
Ω(middleware(h)(ctx)).ShouldNot(HaveOccurred())
Ω(ctx.ResponseStatus()).Should(Equal(200))
})
})
Context("using a http handler func", func() {
BeforeEach(func() {
input = func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
w.WriteHeader(200)
}
})
It("wraps it in a middleware", func() {
Ω(mErr).ShouldNot(HaveOccurred())
h := func(ctx *goa.Context) error { return nil }
Ω(middleware(h)(ctx)).ShouldNot(HaveOccurred())
Ω(ctx.ResponseStatus()).Should(Equal(200))
})
})
})
})
var _ = Describe("LogRequest", func() {
var handler *testHandler
var ctx *goa.Context
params := map[string]string{"param": "value"}
query := map[string][]string{"query": []string{"qvalue"}}
payload := map[string]interface{}{"payload": 42}
BeforeEach(func() {
req, err := http.NewRequest("POST", "/goo", strings.NewReader(`{"payload":42}`))
Ω(err).ShouldNot(HaveOccurred())
rw := new(TestResponseWriter)
ctx = goa.NewContext(nil, req, rw, params, query, payload)
handler = new(testHandler)
logger := log15.New("test", "test")
logger.SetHandler(handler)
ctx.Logger = logger
})
It("logs requests", func() {
h := func(ctx *goa.Context) error {
ctx.JSON(200, "ok")
return nil
}
lg := goa.LogRequest()(h)
Ω(lg(ctx)).ShouldNot(HaveOccurred())
Ω(handler.Records).Should(HaveLen(5))
Ω(handler.Records[0].Ctx).Should(HaveLen(6))
Ω(handler.Records[0].Ctx[4]).Should(Equal("POST"))
Ω(handler.Records[0].Ctx[5]).Should(Equal("/goo"))
Ω(handler.Records[1].Ctx).Should(HaveLen(6))
Ω(handler.Records[1].Ctx[4]).Should(Equal("param"))
Ω(handler.Records[1].Ctx[5]).Should(Equal("value"))
Ω(handler.Records[2].Ctx).Should(HaveLen(6))
Ω(handler.Records[2].Ctx[4]).Should(Equal("query"))
Ω(handler.Records[2].Ctx[5]).Should(Equal([]string{"qvalue"}))
Ω(handler.Records[3].Ctx).Should(HaveLen(6))
Ω(handler.Records[3].Ctx[4]).Should(Equal("payload"))
Ω(handler.Records[3].Ctx[5]).Should(Equal(42))
Ω(handler.Records[4].Ctx).Should(HaveLen(10))
Ω(handler.Records[4].Ctx[4]).Should(Equal("status"))
Ω(handler.Records[4].Ctx[5]).Should(Equal(200))
Ω(handler.Records[4].Ctx[6]).Should(Equal("bytes"))
Ω(handler.Records[4].Ctx[7]).Should(Equal(4))
Ω(handler.Records[4].Ctx[8]).Should(Equal("time"))
})
})
var _ = Describe("RequestID", func() {
const reqID = "request id"
var ctx *goa.Context
BeforeEach(func() {
req, err := http.NewRequest("GET", "/goo", nil)
Ω(err).ShouldNot(HaveOccurred())
req.Header.Set("X-Request-Id", reqID)
ctx = goa.NewContext(nil, req, new(TestResponseWriter), nil, nil, nil)
})
It("sets the request ID in the context", func() {
h := func(ctx *goa.Context) error {
ctx.JSON(200, "ok")
return nil
}
rg := goa.RequestID()(h)
Ω(rg(ctx)).ShouldNot(HaveOccurred())
Ω(ctx.Value(goa.ReqIDKey)).Should(Equal(reqID))
})
})
var _ = Describe("Recover", func() {
It("recovers", func() {
h := func(ctx *goa.Context) error {
panic("boom")
}
rg := goa.Recover()(h)
err := rg(goa.NewContext(nil, nil, nil, nil, nil, nil))
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(Equal("panic: boom"))
})
})
var _ = Describe("Timeout", func() {
It("sets a deadline", func() {
h := func(ctx *goa.Context) error {
ctx.JSON(200, "ok")
return nil
}
t := goa.Timeout(time.Duration(1))(h)
ctx := goa.NewContext(nil, nil, nil, nil, nil, nil)
err := t(ctx)
Ω(err).ShouldNot(HaveOccurred())
_, ok := ctx.Deadline()
Ω(ok).Should(BeTrue())
})
})
var _ = Describe("RequireHeader", func() {
var handler *testHandler
var ctx *goa.Context
var req *http.Request
params := map[string]string{"param": "value"}
query := map[string][]string{"query": []string{"qvalue"}}
payload := map[string]interface{}{"payload": 42}
headerName := "Some-Header"
BeforeEach(func() {
var err error
req, err = http.NewRequest("POST", "/foo/bar", strings.NewReader(`{"payload":42}`))
Ω(err).ShouldNot(HaveOccurred())
rw := new(TestResponseWriter)
ctx = goa.NewContext(nil, req, rw, params, query, payload)
handler = new(testHandler)
logger := log15.New("test", "test")
logger.SetHandler(handler)
ctx.Logger = logger
})
It("matches a header value", func() {
req.Header.Set(headerName, "some value")
h := func(ctx *goa.Context) error {
ctx.JSON(http.StatusOK, "ok")
return nil
}
t := goa.RequireHeader(
regexp.MustCompile("^/foo"),
headerName,
regexp.MustCompile("^some value$"),
http.StatusUnauthorized)(h)
err := t(ctx)
Ω(err).ShouldNot(HaveOccurred())
Ω(ctx.ResponseStatus()).Should(Equal(http.StatusOK))
})
It("responds with failure on mismatch", func() {
req.Header.Set(headerName, "some other value")
h := func(ctx *goa.Context) error {
panic("unreachable")
}
t := goa.RequireHeader(
regexp.MustCompile("^/foo"),
headerName,
regexp.MustCompile("^some value$"),
http.StatusUnauthorized)(h)
err := t(ctx)
Ω(err).ShouldNot(HaveOccurred())
Ω(ctx.ResponseStatus()).Should(Equal(http.StatusUnauthorized))
})
It("responds with failure when header is missing", func() {
h := func(ctx *goa.Context) error {
panic("unreachable")
}
t := goa.RequireHeader(
regexp.MustCompile("^/foo"),
headerName,
regexp.MustCompile("^some value$"),
http.StatusUnauthorized)(h)
err := t(ctx)
Ω(err).ShouldNot(HaveOccurred())
Ω(ctx.ResponseStatus()).Should(Equal(http.StatusUnauthorized))
})
It("passes through for a non-matching path", func() {
req.Header.Set(headerName, "bogus")
h := func(ctx *goa.Context) error {
ctx.JSON(http.StatusOK, "ok")
return nil
}
t := goa.RequireHeader(
regexp.MustCompile("^/baz"),
headerName,
regexp.MustCompile("^some value$"),
http.StatusUnauthorized)(h)
err := t(ctx)
Ω(err).ShouldNot(HaveOccurred())
Ω(ctx.ResponseStatus()).Should(Equal(http.StatusOK))
})
It("matches value for a nil path pattern", func() {
req.Header.Set(headerName, "bogus")
h := func(ctx *goa.Context) error {
panic("unreachable")
}
t := goa.RequireHeader(
nil,
headerName,
regexp.MustCompile("^some value$"),
http.StatusNotFound)(h)
err := t(ctx)
Ω(err).ShouldNot(HaveOccurred())
Ω(ctx.ResponseStatus()).Should(Equal(http.StatusNotFound))
})
})
type testHandler struct {
Records []*log15.Record
}
func (t *testHandler) Log(r *log15.Record) error {
t.Records = append(t.Records, r)
return nil
}