forked from goadesign/goa
/
service_test.go
220 lines (182 loc) · 4.75 KB
/
service_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
package goa_test
import (
"fmt"
"net/http"
"github.com/julienschmidt/httprouter"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/raphael/goa"
)
var _ = Describe("Application", func() {
const appName = "foo"
var s goa.Service
BeforeEach(func() {
s = goa.New(appName)
})
Describe("New", func() {
It("creates an application", func() {
Ω(s).ShouldNot(BeNil())
})
It("initializes the application fields", func() {
Ω(s.Name()).Should(Equal(appName))
Ω(s).Should(BeAssignableToTypeOf(&goa.Application{}))
app, _ := s.(*goa.Application)
Ω(app.Name()).Should(Equal(appName))
Ω(app.Logger).ShouldNot(BeNil())
Ω(app.Router).ShouldNot(BeNil())
})
})
Describe("Use", func() {
Context("with a valid middleware", func() {
var m goa.Middleware
BeforeEach(func() {
m = goa.RequestID()
})
JustBeforeEach(func() {
s.Use(m)
})
It("adds the middleware", func() {
ctrl := s.NewController("test")
Ω(ctrl.MiddlewareChain()).Should(HaveLen(1))
Ω(ctrl.MiddlewareChain()[0]).Should(BeAssignableToTypeOf(goa.RequestID()))
})
})
})
Describe("NewHTTPRouterHandle", func() {
const resName = "res"
const actName = "act"
var handler goa.Handler
const respStatus = 200
var respContent = []byte("response")
var httpHandle httprouter.Handle
var ctx *goa.Context
JustBeforeEach(func() {
ctrl := s.NewController("test")
httpHandle = ctrl.NewHTTPRouterHandle(actName, handler)
})
BeforeEach(func() {
handler = func(c *goa.Context) error {
ctx = c
c.Respond(respStatus, respContent)
return nil
}
})
It("creates a handle", func() {
Ω(httpHandle).ShouldNot(BeNil())
})
Context("with a request", func() {
var rw http.ResponseWriter
var r *http.Request
var p httprouter.Params
BeforeEach(func() {
var err error
r, err = http.NewRequest("GET", "/foo", nil)
Ω(err).ShouldNot(HaveOccurred())
rw = new(TestResponseWriter)
id := httprouter.Param{Key: "id", Value: "42"}
query := httprouter.Param{Key: "sort", Value: "asc"}
p = httprouter.Params{id, query}
})
JustBeforeEach(func() {
httpHandle(rw, r, p)
})
It("creates a handle that handles the request", func() {
i, ok := ctx.Get("id")
Ω(ok).Should(BeTrue())
Ω(i).Should(Equal("42"))
s, ok := ctx.Get("sort")
Ω(ok).Should(BeTrue())
Ω(s).Should(Equal("asc"))
tw := rw.(*TestResponseWriter)
Ω(tw.Status).Should(Equal(respStatus))
Ω(tw.Body).Should(Equal(respContent))
})
Context("and middleware", func() {
middlewareCalled := false
BeforeEach(func() {
s.Use(TMiddleware(&middlewareCalled))
})
It("calls the middleware", func() {
Ω(middlewareCalled).Should(BeTrue())
})
})
Context("and a middleware chain", func() {
middlewareCalled := false
secondCalled := false
BeforeEach(func() {
s.Use(TMiddleware(&middlewareCalled))
s.Use(SecondMiddleware(&middlewareCalled, &secondCalled))
})
It("calls the middleware in the right order", func() {
Ω(middlewareCalled).Should(BeTrue())
Ω(secondCalled).Should(BeTrue())
})
})
Context("with a handler that fails", func() {
errorHandlerCalled := false
BeforeEach(func() {
s.SetErrorHandler(TErrorHandler(&errorHandlerCalled))
})
Context("by returning an error", func() {
BeforeEach(func() {
handler = func(ctx *goa.Context) error {
return fmt.Errorf("boom")
}
})
It("triggers the error handler", func() {
Ω(errorHandlerCalled).Should(BeTrue())
})
})
Context("by not handling the request", func() {
BeforeEach(func() {
handler = func(ctx *goa.Context) error {
return nil
}
})
It("triggers the error handler", func() {
Ω(errorHandlerCalled).Should(BeTrue())
})
})
})
})
})
})
func TErrorHandler(witness *bool) goa.ErrorHandler {
return func(ctx *goa.Context, err error) {
*witness = true
}
}
func TMiddleware(witness *bool) goa.Middleware {
return func(h goa.Handler) goa.Handler {
return func(ctx *goa.Context) error {
*witness = true
return h(ctx)
}
}
}
func SecondMiddleware(witness1, witness2 *bool) goa.Middleware {
return func(h goa.Handler) goa.Handler {
return func(ctx *goa.Context) error {
if !*witness1 {
panic("middleware called in wrong order")
}
*witness2 = true
return h(ctx)
}
}
}
type TestResponseWriter struct {
ParentHeader http.Header
Body []byte
Status int
}
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
}