5 | 5 |
"fmt"
|
6 | 6 |
"testing"
|
7 | 7 |
|
8 | |
"github.com/aws/aws-lambda-go/events"
|
9 | 8 |
"github.com/go-kit/kit/endpoint"
|
10 | 9 |
"github.com/go-kit/kit/log"
|
11 | 10 |
"github.com/go-kit/kit/transport"
|
|
20 | 19 |
KeyEncMode key = iota
|
21 | 20 |
)
|
22 | 21 |
|
|
22 |
// Created based on github.com/aws/aws-lambda-go@v1.13.3/events.APIGatewayProxyRequest for the purposes of the tests below.
|
|
23 |
type apiGatewayProxyRequest struct {
|
|
24 |
Body string `json:"body"`
|
|
25 |
}
|
|
26 |
|
|
27 |
// Created based on github.com/aws/aws-lambda-go@v1.13.3/events.APIGatewayProxyResponse for the purposes of the tests below.
|
|
28 |
type apiGatewayProxyResponse struct {
|
|
29 |
StatusCode int `json:"statusCode"`
|
|
30 |
Body string `json:"body"`
|
|
31 |
}
|
|
32 |
|
23 | 33 |
func TestDefaultErrorEncoder(t *testing.T) {
|
24 | 34 |
ctx := context.Background()
|
25 | 35 |
rootErr := fmt.Errorf("root")
|
|
75 | 85 |
resp []byte,
|
76 | 86 |
_ error,
|
77 | 87 |
) {
|
78 | |
apigwResp := events.APIGatewayProxyResponse{}
|
|
88 |
apigwResp := apiGatewayProxyResponse{}
|
79 | 89 |
err := json.Unmarshal(resp, &apigwResp)
|
80 | 90 |
if err != nil {
|
81 | 91 |
t.Fatalf("Should have no error, but got: %+v", err)
|
|
96 | 106 |
)
|
97 | 107 |
|
98 | 108 |
ctx := context.Background()
|
99 | |
req, _ := json.Marshal(events.APIGatewayProxyRequest{
|
|
109 |
req, _ := json.Marshal(apiGatewayProxyRequest{
|
100 | 110 |
Body: `{"name":"john doe"}`,
|
101 | 111 |
})
|
102 | 112 |
resp, err := helloHandler.Invoke(ctx, req)
|
|
105 | 115 |
t.Fatalf("Should have no error, but got: %+v", err)
|
106 | 116 |
}
|
107 | 117 |
|
108 | |
apigwResp := events.APIGatewayProxyResponse{}
|
|
118 |
apigwResp := apiGatewayProxyResponse{}
|
109 | 119 |
err = json.Unmarshal(resp, &apigwResp)
|
110 | 120 |
if err != nil {
|
111 | 121 |
t.Fatalf("Should have no error, but got: %+v", err)
|
|
135 | 145 |
ctx context.Context,
|
136 | 146 |
err error,
|
137 | 147 |
) ([]byte, error) {
|
138 | |
apigwResp := events.APIGatewayProxyResponse{}
|
|
148 |
apigwResp := apiGatewayProxyResponse{}
|
139 | 149 |
apigwResp.Body = `{"error":"yes"}`
|
140 | 150 |
apigwResp.StatusCode = 500
|
141 | 151 |
resp, err := json.Marshal(apigwResp)
|
|
144 | 154 |
)
|
145 | 155 |
|
146 | 156 |
ctx := context.Background()
|
147 | |
req, _ := json.Marshal(events.APIGatewayProxyRequest{
|
|
157 |
req, _ := json.Marshal(apiGatewayProxyRequest{
|
148 | 158 |
Body: `{"name":"john doe"}`,
|
149 | 159 |
})
|
150 | 160 |
resp, err := helloHandler.Invoke(ctx, req)
|
|
153 | 163 |
t.Fatalf("Should have no error, but got: %+v", err)
|
154 | 164 |
}
|
155 | 165 |
|
156 | |
apigwResp := events.APIGatewayProxyResponse{}
|
|
166 |
apigwResp := apiGatewayProxyResponse{}
|
157 | 167 |
json.Unmarshal(resp, &apigwResp)
|
158 | 168 |
if apigwResp.StatusCode != 500 {
|
159 | 169 |
t.Fatalf("Expect status code of 500, instead of %d", apigwResp.StatusCode)
|
|
185 | 195 |
ctx context.Context,
|
186 | 196 |
err error,
|
187 | 197 |
) ([]byte, error) {
|
188 | |
apigwResp := events.APIGatewayProxyResponse{}
|
|
198 |
apigwResp := apiGatewayProxyResponse{}
|
189 | 199 |
apigwResp.Body = `{"error":"yes"}`
|
190 | 200 |
apigwResp.StatusCode = 500
|
191 | 201 |
resp, err := json.Marshal(apigwResp)
|
|
194 | 204 |
)
|
195 | 205 |
|
196 | 206 |
ctx := context.Background()
|
197 | |
req, _ := json.Marshal(events.APIGatewayProxyRequest{
|
|
207 |
req, _ := json.Marshal(apiGatewayProxyRequest{
|
198 | 208 |
Body: `{"name":"john doe"}`,
|
199 | 209 |
})
|
200 | 210 |
resp, err := helloHandler.Invoke(ctx, req)
|
|
203 | 213 |
t.Fatalf("Should have no error, but got: %+v", err)
|
204 | 214 |
}
|
205 | 215 |
|
206 | |
apigwResp := events.APIGatewayProxyResponse{}
|
|
216 |
apigwResp := apiGatewayProxyResponse{}
|
207 | 217 |
json.Unmarshal(resp, &apigwResp)
|
208 | 218 |
if apigwResp.StatusCode != 500 {
|
209 | 219 |
t.Fatalf("Expect status code of 500, instead of %d", apigwResp.StatusCode)
|
|
243 | 253 |
err error,
|
244 | 254 |
) ([]byte, error) {
|
245 | 255 |
// convert error into proper APIGateway response.
|
246 | |
apigwResp := events.APIGatewayProxyResponse{}
|
|
256 |
apigwResp := apiGatewayProxyResponse{}
|
247 | 257 |
apigwResp.Body = `{"error":"yes"}`
|
248 | 258 |
apigwResp.StatusCode = 500
|
249 | 259 |
resp, err := json.Marshal(apigwResp)
|
|
252 | 262 |
)
|
253 | 263 |
|
254 | 264 |
ctx := context.Background()
|
255 | |
req, _ := json.Marshal(events.APIGatewayProxyRequest{
|
|
265 |
req, _ := json.Marshal(apiGatewayProxyRequest{
|
256 | 266 |
Body: `{"name":"john doe"}`,
|
257 | 267 |
})
|
258 | 268 |
resp, err := helloHandler.Invoke(ctx, req)
|
|
261 | 271 |
t.Fatalf("Should have no error, but got: %+v", err)
|
262 | 272 |
}
|
263 | 273 |
|
264 | |
apigwResp := events.APIGatewayProxyResponse{}
|
|
274 |
apigwResp := apiGatewayProxyResponse{}
|
265 | 275 |
json.Unmarshal(resp, &apigwResp)
|
266 | 276 |
if apigwResp.StatusCode != 500 {
|
267 | 277 |
t.Fatalf("Expect status code of 500, instead of %d", apigwResp.StatusCode)
|
|
271 | 281 |
func decodeHelloRequestWithTwoBefores(
|
272 | 282 |
ctx context.Context, req []byte,
|
273 | 283 |
) (interface{}, error) {
|
274 | |
apigwReq := events.APIGatewayProxyRequest{}
|
|
284 |
apigwReq := apiGatewayProxyRequest{}
|
275 | 285 |
err := json.Unmarshal([]byte(req), &apigwReq)
|
276 | 286 |
if err != nil {
|
277 | 287 |
return apigwReq, err
|
|
302 | 312 |
func encodeResponse(
|
303 | 313 |
ctx context.Context, response interface{},
|
304 | 314 |
) ([]byte, error) {
|
305 | |
apigwResp := events.APIGatewayProxyResponse{}
|
|
315 |
apigwResp := apiGatewayProxyResponse{}
|
306 | 316 |
|
307 | 317 |
mode, ok := ctx.Value(KeyEncMode).(string)
|
308 | 318 |
if ok && mode == "fail_encode" {
|