forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
207 lines (180 loc) · 5.12 KB
/
context_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
package goa_test
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/goadesign/goa"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"golang.org/x/net/context"
"gopkg.in/inconshreveable/log15.v2"
)
var _ = Describe("Context", func() {
var logger log15.Logger
var ctx *goa.Context
BeforeEach(func() {
gctx := context.Background()
ctx = goa.NewContext(gctx, goa.New("test"), nil, nil, nil)
ctx.Logger = logger
})
Describe("SetValue", func() {
key := "answer"
val := 42
BeforeEach(func() {
ctx.SetValue(key, val)
})
It("sets the value in the context.Context", func() {
v := ctx.Value(key)
Ω(v).Should(Equal(val))
})
})
Describe("SetResponseWriter", func() {
var rw http.ResponseWriter
BeforeEach(func() {
rw = &TestResponseWriter{Status: 42}
})
It("sets the response writer and returns the previous one", func() {
rwo := ctx.SetResponseWriter(rw)
Ω(rwo).Should(BeNil())
rwo = ctx.SetResponseWriter(&TestResponseWriter{Status: 43})
Ω(rwo).ShouldNot(BeNil())
Ω(rwo).Should(BeAssignableToTypeOf(&TestResponseWriter{}))
trw := rwo.(*TestResponseWriter)
Ω(trw.Status).Should(Equal(42))
})
})
Describe("Request", func() {
It("returns nil if not initialized", func() {
Ω(ctx.Request()).Should(BeNil())
})
})
Describe("Header", func() {
It("returns nil if not initialized", func() {
Ω(ctx.Header()).Should(BeNil())
})
})
Describe("ResponseStatus", func() {
It("returns 0 if not initialized", func() {
Ω(ctx.ResponseStatus()).Should(Equal(0))
})
})
Describe("ResponseLength", func() {
It("returns 0 if not initialized", func() {
Ω(ctx.ResponseLength()).Should(Equal(0))
})
})
Describe("Get", func() {
It(`returns "", false if not initialized`, func() {
p := ctx.Get("foo")
Ω(p).Should(Equal(""))
})
})
Describe("GetMany", func() {
It("returns nil if not initialized", func() {
Ω(ctx.GetMany("foo")).Should(BeNil())
})
})
Describe("RawPayload", func() {
It("returns nil if not initialized", func() {
Ω(ctx.RawPayload()).Should(BeNil())
})
})
Context("with a request response", func() {
const appName = "foo"
var app goa.Service
const resName = "res"
const actName = "act"
var handler, unmarshaler goa.Handler
const reqBody = `"body"`
const respStatus = 200
var respContent = []byte("response")
var handleFunc goa.HandleFunc
var rw http.ResponseWriter
var request *http.Request
var params url.Values
BeforeEach(func() {
app = goa.New(appName)
app.SetDecoder(&goa.JSONFactory{}, "", true, "*/*")
app.SetEncoder(&goa.JSONFactory{}, "", true, "*/*")
handler = func(c *goa.Context) error {
ctx = c
c.RespondBytes(respStatus, respContent)
return nil
}
unmarshaler = func(c *goa.Context) error {
ctx = c
if req := c.Request(); req != nil {
var payload interface{}
err := c.Service().DecodeRequest(ctx, &payload)
if err != nil {
return err
}
c.SetPayload(payload)
}
return nil
}
var err error
reader := strings.NewReader(reqBody)
request, err = http.NewRequest("POST", "/foo?filters=one&filters=two&filters=three", reader)
request.Header.Set("Content-Type", "application/json")
Ω(err).ShouldNot(HaveOccurred())
rw = new(TestResponseWriter)
params = url.Values{"id": []string{"42"}, "filters": []string{"one", "two", "three"}}
})
JustBeforeEach(func() {
ctrl := app.NewController(resName)
handleFunc = ctrl.HandleFunc(actName, handler, unmarshaler)
handleFunc(rw, request, params)
})
Describe("RespondBytes", func() {
It("sets the context fields", func() {
Ω(ctx.Request()).Should(Equal(request))
Ω(ctx.Header()).Should(Equal(rw.Header()))
Ω(ctx.ResponseStatus()).Should(Equal(respStatus))
Ω(ctx.ResponseLength()).Should(Equal(len(respContent)))
p := ctx.Get("id")
Ω(p).Should(Equal("42"))
ps := ctx.GetMany("filters")
Ω(ps).Should(Equal([]string{"one", "two", "three"}))
var payload string
err := json.Unmarshal([]byte(reqBody), &payload)
Ω(err).ShouldNot(HaveOccurred())
Ω(ctx.RawPayload()).Should(Equal(payload))
})
})
Context("Respond", func() {
BeforeEach(func() {
handler = func(c *goa.Context) error {
ctx = c
c.Respond(respStatus, string(respContent))
return nil
}
})
It("sets the context response fields with the JSON", func() {
Ω(ctx.ResponseStatus()).Should(Equal(respStatus))
Ω(ctx.ResponseLength()).Should(Equal(len(respContent) + 3)) // quotes and newline
})
})
Context("BadRequest", func() {
err := fmt.Errorf("boom")
var badReq = &goa.BadRequestError{Actual: err}
BeforeEach(func() {
var err2 error
request, err2 = http.NewRequest("POST", "/foo?filters=one&filters=two&filters=three", nil)
Ω(err2).ShouldNot(HaveOccurred())
handler = func(c *goa.Context) error {
ctx = c
c.BadRequest(badReq)
return nil
}
})
It("responds with 400 and the error body", func() {
Ω(ctx.ResponseStatus()).Should(Equal(400))
tw := rw.(*TestResponseWriter)
Ω(string(tw.Body)).Should(ContainSubstring(err.Error()))
})
})
})
})