-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmiddleware_test.go
74 lines (60 loc) · 1.65 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
package goacors_test
import (
"net/http"
"net/url"
"github.com/goadesign/goa"
"context"
)
// Helper that sets up a "working" service
func newService(logger goa.LogAdapter) *goa.Service {
service := goa.New("test")
service.Encoder.Register(goa.NewJSONEncoder, "*/*")
service.Decoder.Register(goa.NewJSONDecoder, "*/*")
service.WithLogger(logger)
return service
}
// Creates a test context
func newContext(service *goa.Service, rw http.ResponseWriter, req *http.Request, params url.Values) context.Context {
ctrl := service.NewController("test")
return goa.NewContext(ctrl.Context, rw, req, params)
}
type logEntry struct {
Msg string
Data []interface{}
}
type testLogger struct {
Context []interface{}
InfoEntries []logEntry
ErrorEntries []logEntry
}
func (t *testLogger) Info(msg string, data ...interface{}) {
e := logEntry{msg, append(t.Context, data...)}
t.InfoEntries = append(t.InfoEntries, e)
}
func (t *testLogger) Error(msg string, data ...interface{}) {
e := logEntry{msg, append(t.Context, data...)}
t.ErrorEntries = append(t.ErrorEntries, e)
}
func (t *testLogger) New(data ...interface{}) goa.LogAdapter {
t.Context = append(t.Context, data...)
return t
}
type testResponseWriter struct {
ParentHeader http.Header
Body []byte
Status int
}
func newTestResponseWriter() *testResponseWriter {
h := make(http.Header)
return &testResponseWriter{ParentHeader: h}
}
func (t *testResponseWriter) Header() http.Header {
return t.ParentHeader
}
func (t *testResponseWriter) Write(b []byte) (int, error) {
t.Body = append(t.Body, b...)
return len(b), nil
}
func (t *testResponseWriter) WriteHeader(s int) {
t.Status = s
}