|
0 |
package awslambda
|
|
1 |
|
|
2 |
import (
|
|
3 |
"context"
|
|
4 |
"encoding/json"
|
|
5 |
"fmt"
|
|
6 |
"testing"
|
|
7 |
|
|
8 |
"github.com/aws/aws-lambda-go/events"
|
|
9 |
"github.com/go-kit/kit/endpoint"
|
|
10 |
"github.com/go-kit/kit/log"
|
|
11 |
)
|
|
12 |
|
|
13 |
type key int
|
|
14 |
|
|
15 |
const (
|
|
16 |
KeyBeforeOne key = iota
|
|
17 |
KeyBeforeTwo key = iota
|
|
18 |
KeyAfterOne key = iota
|
|
19 |
KeyEncMode key = iota
|
|
20 |
)
|
|
21 |
|
|
22 |
func TestDefaultErrorEncoder(t *testing.T) {
|
|
23 |
ctx := context.Background()
|
|
24 |
rootErr := fmt.Errorf("root")
|
|
25 |
b, err := DefaultErrorEncoder(ctx, rootErr)
|
|
26 |
if b != nil {
|
|
27 |
t.Fatalf("DefaultErrorEncoder should return nil as []byte")
|
|
28 |
}
|
|
29 |
if err != rootErr {
|
|
30 |
t.Fatalf("DefaultErrorEncoder expects return back the given error.")
|
|
31 |
}
|
|
32 |
}
|
|
33 |
|
|
34 |
func TestInvokeHappyPath(t *testing.T) {
|
|
35 |
svc := serviceTest01{}
|
|
36 |
|
|
37 |
helloHandler := NewHandler(
|
|
38 |
makeTest01HelloEndpoint(svc),
|
|
39 |
decodeHelloRequestWithTwoBefores,
|
|
40 |
encodeResponse,
|
|
41 |
HandlerErrorLogger(log.NewNopLogger()),
|
|
42 |
HandlerBefore(func(
|
|
43 |
ctx context.Context,
|
|
44 |
payload []byte,
|
|
45 |
) context.Context {
|
|
46 |
ctx = context.WithValue(ctx, KeyBeforeOne, "bef1")
|
|
47 |
return ctx
|
|
48 |
}),
|
|
49 |
HandlerBefore(func(
|
|
50 |
ctx context.Context,
|
|
51 |
payload []byte,
|
|
52 |
) context.Context {
|
|
53 |
ctx = context.WithValue(ctx, KeyBeforeTwo, "bef2")
|
|
54 |
return ctx
|
|
55 |
}),
|
|
56 |
HandlerAfter(func(
|
|
57 |
ctx context.Context,
|
|
58 |
response interface{},
|
|
59 |
) context.Context {
|
|
60 |
ctx = context.WithValue(ctx, KeyAfterOne, "af1")
|
|
61 |
return ctx
|
|
62 |
}),
|
|
63 |
HandlerAfter(func(
|
|
64 |
ctx context.Context,
|
|
65 |
response interface{},
|
|
66 |
) context.Context {
|
|
67 |
if _, ok := ctx.Value(KeyAfterOne).(string); !ok {
|
|
68 |
t.Fatalf("Value was not set properly during multi HandlerAfter")
|
|
69 |
}
|
|
70 |
return ctx
|
|
71 |
}),
|
|
72 |
HandlerFinalizer(func(
|
|
73 |
_ context.Context,
|
|
74 |
resp []byte,
|
|
75 |
_ error,
|
|
76 |
) {
|
|
77 |
apigwResp := events.APIGatewayProxyResponse{}
|
|
78 |
err := json.Unmarshal(resp, &apigwResp)
|
|
79 |
if err != nil {
|
|
80 |
t.Fatalf("Should have no error, but got: %+v", err)
|
|
81 |
}
|
|
82 |
|
|
83 |
response := helloResponse{}
|
|
84 |
err = json.Unmarshal([]byte(apigwResp.Body), &response)
|
|
85 |
if err != nil {
|
|
86 |
t.Fatalf("Should have no error, but got: %+v", err)
|
|
87 |
}
|
|
88 |
|
|
89 |
expectedGreeting := "hello john doe bef1 bef2"
|
|
90 |
if response.Greeting != expectedGreeting {
|
|
91 |
t.Fatalf(
|
|
92 |
"Expect: %s, Actual: %s", expectedGreeting, response.Greeting)
|
|
93 |
}
|
|
94 |
}),
|
|
95 |
)
|
|
96 |
|
|
97 |
ctx := context.Background()
|
|
98 |
req, _ := json.Marshal(events.APIGatewayProxyRequest{
|
|
99 |
Body: `{"name":"john doe"}`,
|
|
100 |
})
|
|
101 |
resp, err := helloHandler.Invoke(ctx, req)
|
|
102 |
|
|
103 |
if err != nil {
|
|
104 |
t.Fatalf("Should have no error, but got: %+v", err)
|
|
105 |
}
|
|
106 |
|
|
107 |
apigwResp := events.APIGatewayProxyResponse{}
|
|
108 |
err = json.Unmarshal(resp, &apigwResp)
|
|
109 |
if err != nil {
|
|
110 |
t.Fatalf("Should have no error, but got: %+v", err)
|
|
111 |
}
|
|
112 |
|
|
113 |
response := helloResponse{}
|
|
114 |
err = json.Unmarshal([]byte(apigwResp.Body), &response)
|
|
115 |
if err != nil {
|
|
116 |
t.Fatalf("Should have no error, but got: %+v", err)
|
|
117 |
}
|
|
118 |
|
|
119 |
expectedGreeting := "hello john doe bef1 bef2"
|
|
120 |
if response.Greeting != expectedGreeting {
|
|
121 |
t.Fatalf(
|
|
122 |
"Expect: %s, Actual: %s", expectedGreeting, response.Greeting)
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
func TestInvokeFailDecode(t *testing.T) {
|
|
127 |
svc := serviceTest01{}
|
|
128 |
|
|
129 |
helloHandler := NewHandler(
|
|
130 |
makeTest01HelloEndpoint(svc),
|
|
131 |
decodeHelloRequestWithTwoBefores,
|
|
132 |
encodeResponse,
|
|
133 |
HandlerErrorEncoder(func(
|
|
134 |
ctx context.Context,
|
|
135 |
err error,
|
|
136 |
) ([]byte, error) {
|
|
137 |
apigwResp := events.APIGatewayProxyResponse{}
|
|
138 |
apigwResp.Body = `{"error":"yes"}`
|
|
139 |
apigwResp.StatusCode = 500
|
|
140 |
resp, err := json.Marshal(apigwResp)
|
|
141 |
return resp, err
|
|
142 |
}),
|
|
143 |
)
|
|
144 |
|
|
145 |
ctx := context.Background()
|
|
146 |
req, _ := json.Marshal(events.APIGatewayProxyRequest{
|
|
147 |
Body: `{"name":"john doe"}`,
|
|
148 |
})
|
|
149 |
resp, err := helloHandler.Invoke(ctx, req)
|
|
150 |
|
|
151 |
if err != nil {
|
|
152 |
t.Fatalf("Should have no error, but got: %+v", err)
|
|
153 |
}
|
|
154 |
|
|
155 |
apigwResp := events.APIGatewayProxyResponse{}
|
|
156 |
json.Unmarshal(resp, &apigwResp)
|
|
157 |
if apigwResp.StatusCode != 500 {
|
|
158 |
t.Fatalf("Expect status code of 500, instead of %d", apigwResp.StatusCode)
|
|
159 |
}
|
|
160 |
}
|
|
161 |
|
|
162 |
func TestInvokeFailEndpoint(t *testing.T) {
|
|
163 |
svc := serviceTest01{}
|
|
164 |
|
|
165 |
helloHandler := NewHandler(
|
|
166 |
makeTest01FailEndpoint(svc),
|
|
167 |
decodeHelloRequestWithTwoBefores,
|
|
168 |
encodeResponse,
|
|
169 |
HandlerBefore(func(
|
|
170 |
ctx context.Context,
|
|
171 |
payload []byte,
|
|
172 |
) context.Context {
|
|
173 |
ctx = context.WithValue(ctx, KeyBeforeOne, "bef1")
|
|
174 |
return ctx
|
|
175 |
}),
|
|
176 |
HandlerBefore(func(
|
|
177 |
ctx context.Context,
|
|
178 |
payload []byte,
|
|
179 |
) context.Context {
|
|
180 |
ctx = context.WithValue(ctx, KeyBeforeTwo, "bef2")
|
|
181 |
return ctx
|
|
182 |
}),
|
|
183 |
HandlerErrorEncoder(func(
|
|
184 |
ctx context.Context,
|
|
185 |
err error,
|
|
186 |
) ([]byte, error) {
|
|
187 |
apigwResp := events.APIGatewayProxyResponse{}
|
|
188 |
apigwResp.Body = `{"error":"yes"}`
|
|
189 |
apigwResp.StatusCode = 500
|
|
190 |
resp, err := json.Marshal(apigwResp)
|
|
191 |
return resp, err
|
|
192 |
}),
|
|
193 |
)
|
|
194 |
|
|
195 |
ctx := context.Background()
|
|
196 |
req, _ := json.Marshal(events.APIGatewayProxyRequest{
|
|
197 |
Body: `{"name":"john doe"}`,
|
|
198 |
})
|
|
199 |
resp, err := helloHandler.Invoke(ctx, req)
|
|
200 |
|
|
201 |
if err != nil {
|
|
202 |
t.Fatalf("Should have no error, but got: %+v", err)
|
|
203 |
}
|
|
204 |
|
|
205 |
apigwResp := events.APIGatewayProxyResponse{}
|
|
206 |
json.Unmarshal(resp, &apigwResp)
|
|
207 |
if apigwResp.StatusCode != 500 {
|
|
208 |
t.Fatalf("Expect status code of 500, instead of %d", apigwResp.StatusCode)
|
|
209 |
}
|
|
210 |
}
|
|
211 |
|
|
212 |
func TestInvokeFailEncode(t *testing.T) {
|
|
213 |
svc := serviceTest01{}
|
|
214 |
|
|
215 |
helloHandler := NewHandler(
|
|
216 |
makeTest01HelloEndpoint(svc),
|
|
217 |
decodeHelloRequestWithTwoBefores,
|
|
218 |
encodeResponse,
|
|
219 |
HandlerBefore(func(
|
|
220 |
ctx context.Context,
|
|
221 |
payload []byte,
|
|
222 |
) context.Context {
|
|
223 |
ctx = context.WithValue(ctx, KeyBeforeOne, "bef1")
|
|
224 |
return ctx
|
|
225 |
}),
|
|
226 |
HandlerBefore(func(
|
|
227 |
ctx context.Context,
|
|
228 |
payload []byte,
|
|
229 |
) context.Context {
|
|
230 |
ctx = context.WithValue(ctx, KeyBeforeTwo, "bef2")
|
|
231 |
return ctx
|
|
232 |
}),
|
|
233 |
HandlerAfter(func(
|
|
234 |
ctx context.Context,
|
|
235 |
response interface{},
|
|
236 |
) context.Context {
|
|
237 |
ctx = context.WithValue(ctx, KeyEncMode, "fail_encode")
|
|
238 |
return ctx
|
|
239 |
}),
|
|
240 |
HandlerErrorEncoder(func(
|
|
241 |
ctx context.Context,
|
|
242 |
err error,
|
|
243 |
) ([]byte, error) {
|
|
244 |
// convert error into proper APIGateway response.
|
|
245 |
apigwResp := events.APIGatewayProxyResponse{}
|
|
246 |
apigwResp.Body = `{"error":"yes"}`
|
|
247 |
apigwResp.StatusCode = 500
|
|
248 |
resp, err := json.Marshal(apigwResp)
|
|
249 |
return resp, err
|
|
250 |
}),
|
|
251 |
)
|
|
252 |
|
|
253 |
ctx := context.Background()
|
|
254 |
req, _ := json.Marshal(events.APIGatewayProxyRequest{
|
|
255 |
Body: `{"name":"john doe"}`,
|
|
256 |
})
|
|
257 |
resp, err := helloHandler.Invoke(ctx, req)
|
|
258 |
|
|
259 |
if err != nil {
|
|
260 |
t.Fatalf("Should have no error, but got: %+v", err)
|
|
261 |
}
|
|
262 |
|
|
263 |
apigwResp := events.APIGatewayProxyResponse{}
|
|
264 |
json.Unmarshal(resp, &apigwResp)
|
|
265 |
if apigwResp.StatusCode != 500 {
|
|
266 |
t.Fatalf("Expect status code of 500, instead of %d", apigwResp.StatusCode)
|
|
267 |
}
|
|
268 |
}
|
|
269 |
|
|
270 |
func decodeHelloRequestWithTwoBefores(
|
|
271 |
ctx context.Context, req []byte,
|
|
272 |
) (interface{}, error) {
|
|
273 |
apigwReq := events.APIGatewayProxyRequest{}
|
|
274 |
err := json.Unmarshal([]byte(req), &apigwReq)
|
|
275 |
if err != nil {
|
|
276 |
return apigwReq, err
|
|
277 |
}
|
|
278 |
|
|
279 |
request := helloRequest{}
|
|
280 |
err = json.Unmarshal([]byte(apigwReq.Body), &request)
|
|
281 |
if err != nil {
|
|
282 |
return request, err
|
|
283 |
}
|
|
284 |
|
|
285 |
valOne, ok := ctx.Value(KeyBeforeOne).(string)
|
|
286 |
if !ok {
|
|
287 |
return request, fmt.Errorf(
|
|
288 |
"Value was not set properly when multiple HandlerBefores are used")
|
|
289 |
}
|
|
290 |
|
|
291 |
valTwo, ok := ctx.Value(KeyBeforeTwo).(string)
|
|
292 |
if !ok {
|
|
293 |
return request, fmt.Errorf(
|
|
294 |
"Value was not set properly when multiple HandlerBefores are used")
|
|
295 |
}
|
|
296 |
|
|
297 |
request.Name += " " + valOne + " " + valTwo
|
|
298 |
return request, err
|
|
299 |
}
|
|
300 |
|
|
301 |
func encodeResponse(
|
|
302 |
ctx context.Context, response interface{},
|
|
303 |
) ([]byte, error) {
|
|
304 |
apigwResp := events.APIGatewayProxyResponse{}
|
|
305 |
|
|
306 |
mode, ok := ctx.Value(KeyEncMode).(string)
|
|
307 |
if ok && mode == "fail_encode" {
|
|
308 |
return nil, fmt.Errorf("fail encoding")
|
|
309 |
}
|
|
310 |
|
|
311 |
respByte, err := json.Marshal(response)
|
|
312 |
if err != nil {
|
|
313 |
return nil, err
|
|
314 |
}
|
|
315 |
|
|
316 |
apigwResp.Body = string(respByte)
|
|
317 |
apigwResp.StatusCode = 200
|
|
318 |
|
|
319 |
resp, err := json.Marshal(apigwResp)
|
|
320 |
return resp, err
|
|
321 |
}
|
|
322 |
|
|
323 |
type helloRequest struct {
|
|
324 |
Name string `json:"name"`
|
|
325 |
}
|
|
326 |
|
|
327 |
type helloResponse struct {
|
|
328 |
Greeting string `json:"greeting"`
|
|
329 |
}
|
|
330 |
|
|
331 |
func makeTest01HelloEndpoint(svc serviceTest01) endpoint.Endpoint {
|
|
332 |
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
333 |
req := request.(helloRequest)
|
|
334 |
greeting := svc.hello(req.Name)
|
|
335 |
return helloResponse{greeting}, nil
|
|
336 |
}
|
|
337 |
}
|
|
338 |
|
|
339 |
func makeTest01FailEndpoint(_ serviceTest01) endpoint.Endpoint {
|
|
340 |
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
341 |
return nil, fmt.Errorf("test error endpoint")
|
|
342 |
}
|
|
343 |
}
|
|
344 |
|
|
345 |
type serviceTest01 struct{}
|
|
346 |
|
|
347 |
func (ts *serviceTest01) hello(name string) string {
|
|
348 |
return fmt.Sprintf("hello %s", name)
|
|
349 |
}
|