forked from krakend/krakend-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload_test.go
196 lines (189 loc) · 7.36 KB
/
payload_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
package lambda
import (
"bytes"
"context"
"io"
"net/url"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
"github.com/luraproject/lura/v2/proxy"
)
func TestPayloadExtractorFactory(t *testing.T) {
explosiveBF := func(remote *config.Backend) proxy.Proxy {
t.Error("this backend factory should not been called")
return proxy.NoopProxy
}
bf := func(t *testing.T, expectedPayload string) proxy.BackendFactory {
return BackendFactoryWithInvoker(
logging.NoOp,
explosiveBF,
func(_ *Options) Invoker {
return invoker(func(in *lambda.InvokeInput) (*lambda.InvokeOutput, error) {
if string(in.Payload) != expectedPayload {
t.Errorf("invalid payload in Lambda: expected = %s, got = %s", expectedPayload, in.Payload)
}
return &lambda.InvokeOutput{
Payload: []byte(`{"message":"success"}`),
StatusCode: aws.Int64(200),
}, nil
})
},
)
}
for _, tc := range []struct {
Name string
Method string
Params map[string]string
Body io.ReadCloser
ExpectedPayload string
ExtraConfig map[string]interface{}
}{
{
Name: "get",
Method: "GET",
Params: map[string]string{"first_name": "fooo", "last_name": "bar"},
ExpectedPayload: "{\"first_name\":\"fooo\",\"last_name\":\"bar\"}\n",
},
{
Name: "post",
Method: "POST",
Params: map[string]string{"function": "python37"},
Body: io.NopCloser(bytes.NewBufferString(`{"first_name":"foobar","last_name":"some"}`)),
ExpectedPayload: `{"first_name":"foobar","last_name":"some"}`,
},
{
Name: "AWS API Gateway format empty config",
Params: map[string]string{"function": "python37"},
Body: io.NopCloser(bytes.NewBufferString(`{"first_name":"foobar","last_name":"some"}`)),
ExpectedPayload: `{"first_name":"foobar","last_name":"some"}`,
ExtraConfig: map[string]interface{}{
"aws_api_gateway_format": map[string]interface{}{},
},
},
{
Name: "AWS API Gateway format disabled",
Params: map[string]string{"function": "python37"},
Body: io.NopCloser(bytes.NewBufferString(`{"first_name":"foobar","last_name":"some"}`)),
ExpectedPayload: `{"first_name":"foobar","last_name":"some"}`,
ExtraConfig: map[string]interface{}{
"aws_api_gateway_format": map[string]interface{}{
"enabled": false,
},
},
},
{
Name: "AWS API Gateway v1 format enabled",
Params: map[string]string{"function": "python37"},
Body: io.NopCloser(bytes.NewBufferString(`{"first_name":"foobar","last_name":"some"}`)),
ExpectedPayload: `{"version":"1.0","path":"","httpMethod":"","headers":{},"multiValueHeaders":null,"queryStringParameters":{},"multiValueQueryStringParameters":null,"pathParameters":{"function":"python37"},"requestContext":{"path":"","protocol":"","httpMethod":"","identity":{"userAgent":""}},"body":"{\"first_name\":\"foobar\",\"last_name\":\"some\"}"}`,
ExtraConfig: map[string]interface{}{
"aws_api_gateway_format": map[string]interface{}{
"enabled": true,
},
},
},
} {
t.Run(tc.Name, func(t *testing.T) {
r := &proxy.Request{
Params: tc.Params,
Body: tc.Body,
URL: &url.URL{},
}
extra := map[string]interface{}{}
remote := &config.Backend{
Method: tc.Method,
ExtraConfig: config.ExtraConfig{
Namespace: extra,
},
}
extra["function_name"] = "function_name"
remote.ExtraConfig[Namespace] = tc.ExtraConfig
resp, err := bf(t, tc.ExpectedPayload)(remote)(context.Background(), r)
if err != nil {
t.Errorf("error: %s", err)
}
if m, ok := resp.Data["message"]; !ok || m != "success" {
t.Errorf("unexpected response: %v", resp.Data)
}
})
}
}
func TestFromAwsApiGatewayV1Format(t *testing.T) {
for _, tc := range []struct {
Name string
Method string
URL *url.URL
Body io.ReadCloser
Params map[string]string
Headers map[string][]string
Query map[string][]string
Expected string
}{
{
Name: "simple request",
Method: "GET",
URL: &url.URL{},
Body: io.NopCloser(bytes.NewBufferString(`{}`)),
Expected: `{"version":"1.0","path":"","httpMethod":"GET","headers":{},"multiValueHeaders":null,"queryStringParameters":{},"multiValueQueryStringParameters":null,"pathParameters":null,"requestContext":{"path":"","protocol":"","httpMethod":"GET","identity":{"userAgent":""}},"body":"{}"}`,
},
{
Name: "headers",
URL: &url.URL{},
Headers: map[string][]string{
"header-1": {"header-1-value-1", "header-1-value-2"},
"header-2": {"header-2-value-1", "header-2-value-2"},
},
Body: io.NopCloser(bytes.NewBufferString(``)),
Expected: `{"version":"1.0","path":"","httpMethod":"","headers":{"header-1":"header-1-value-1","header-2":"header-2-value-1"},"multiValueHeaders":{"header-1":["header-1-value-1","header-1-value-2"],"header-2":["header-2-value-1","header-2-value-2"]},"queryStringParameters":{},"multiValueQueryStringParameters":null,"pathParameters":null,"requestContext":{"path":"","protocol":"","httpMethod":"","identity":{"userAgent":""}},"body":""}`,
},
{
Name: "query",
URL: &url.URL{},
Query: map[string][]string{
"query-1": {"query-1-value-1", "query-1-value-2"},
"query-2": {"query-2-value-1", "query-2-value-2"},
},
Body: io.NopCloser(bytes.NewBufferString(``)),
Expected: `{"version":"1.0","path":"","httpMethod":"","headers":{},"multiValueHeaders":null,"queryStringParameters":{"query-1":"query-1-value-1","query-2":"query-2-value-1"},"multiValueQueryStringParameters":{"query-1":["query-1-value-1","query-1-value-2"],"query-2":["query-2-value-1","query-2-value-2"]},"pathParameters":null,"requestContext":{"path":"","protocol":"","httpMethod":"","identity":{"userAgent":""}},"body":""}`,
},
{
Name: "params",
URL: &url.URL{},
Params: map[string]string{
"param1": "param1Value",
},
Body: io.NopCloser(bytes.NewBufferString(``)),
Expected: `{"version":"1.0","path":"","httpMethod":"","headers":{},"multiValueHeaders":null,"queryStringParameters":{},"multiValueQueryStringParameters":null,"pathParameters":{"param1":"param1Value"},"requestContext":{"path":"","protocol":"","httpMethod":"","identity":{"userAgent":""}},"body":""}`,
},
{
Name: "user-agent",
URL: &url.URL{},
Headers: map[string][]string{
"User-Agent": {"user-agent-1", "user-agent-2"},
},
Body: io.NopCloser(bytes.NewBufferString(``)),
Expected: `{"version":"1.0","path":"","httpMethod":"","headers":{"User-Agent":"user-agent-1"},"multiValueHeaders":{"User-Agent":["user-agent-1","user-agent-2"]},"queryStringParameters":{},"multiValueQueryStringParameters":null,"pathParameters":null,"requestContext":{"path":"","protocol":"","httpMethod":"","identity":{"userAgent":"user-agent-1"}},"body":""}`,
},
} {
t.Run(tc.Name, func(t *testing.T) {
r := &proxy.Request{
Params: tc.Params,
Body: tc.Body,
URL: tc.URL,
Headers: tc.Headers,
Query: tc.Query,
Method: tc.Method,
}
payload, err := fromAwsApiGatewayV1Format(r)
if err != nil {
t.Errorf("error: %s", err)
}
if string(payload) != tc.Expected {
t.Errorf("invalid payload: expected = %s, got = %s", tc.Expected, payload)
}
})
}
}