-
Notifications
You must be signed in to change notification settings - Fork 40
/
apigw_v2_test.go
242 lines (219 loc) · 6.29 KB
/
apigw_v2_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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package algnhsa
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"testing"
"github.com/aws/aws-lambda-go/events"
)
var apiGatewayV2TestEvent = `{
"version": "2.0",
"routeKey": "$default",
"rawPath": "/my/path",
"rawQueryString": "parameter1=value1¶meter1=value2¶meter2=value",
"cookies": [
"cookie1",
"cookie2"
],
"headers": {
"header1": "value1",
"header2": "value1,value2"
},
"queryStringParameters": {
"parameter1": "value1,value2",
"parameter2": "value"
},
"requestContext": {
"accountId": "123456789012",
"apiId": "api-id",
"authentication": {
"clientCert": {
"clientCertPem": "CERT_CONTENT",
"subjectDN": "www.example.com",
"issuerDN": "Example issuer",
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
"validity": {
"notBefore": "May 28 12:30:02 2019 GMT",
"notAfter": "Aug 5 09:36:04 2021 GMT"
}
}
},
"authorizer": {
"jwt": {
"claims": {
"claim1": "value1",
"claim2": "value2"
},
"scopes": [
"scope1",
"scope2"
]
}
},
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"http": {
"method": "POST",
"path": "/my/path",
"protocol": "HTTP/1.1",
"sourceIp": "IP",
"userAgent": "agent"
},
"requestId": "id",
"routeKey": "$default",
"stage": "$default",
"time": "12/Mar/2020:19:03:58 +0000",
"timeEpoch": 1583348638390
},
"body": "Hello from Lambda",
"pathParameters": {
"parameter1": "value1",
"proxy": "/my/path2"
},
"isBase64Encoded": false,
"stageVariables": {
"stageVariable1": "value1",
"stageVariable2": "value2"
}
}
`
var expectedApiGatewayV2Dump = RequestDebugDump{
Method: "POST",
URL: struct {
Path string
RawPath string
}{
Path: "/my/path",
RawPath: "",
},
RequestURI: "/my/path?parameter1=value1¶meter1=value2¶meter2=value",
Host: "",
RemoteAddr: "IP",
Header: map[string][]string{
"Header1": {"value1"},
"Header2": {"value1,value2"},
"Cookie": {"cookie1", "cookie2"},
},
Form: map[string][]string{
"parameter1": {"value1", "value2"},
"parameter2": {"value"},
},
Body: "Hello from Lambda",
}
func dumpAPIGatewayV2(payload []byte, opts Options) (RequestDebugDump, error) {
lh := lambdaHandler{
httpHandler: http.HandlerFunc(RequestDebugDumpHandler),
opts: &opts,
}
responseBytes, err := lh.Invoke(context.Background(), payload)
if err != nil {
return RequestDebugDump{}, err
}
var r events.APIGatewayV2HTTPResponse
if err := json.Unmarshal(responseBytes, &r); err != nil {
return RequestDebugDump{}, err
}
if r.StatusCode != 200 {
return RequestDebugDump{}, errors.New("expected status code 200")
}
var dump RequestDebugDump
if err := json.Unmarshal([]byte(r.Body), &dump); err != nil {
return RequestDebugDump{}, err
}
if dump.APIGatewayV2Request.RequestContext.HTTP.Method != "POST" {
fmt.Printf("%+v\n", dump)
return RequestDebugDump{}, errors.New("expected method POST")
}
dump.APIGatewayV2Request = nil
return dump, nil
}
func TestAPIGatewayV2Base(t *testing.T) {
asrt := assert.New(t)
dump, err := dumpAPIGatewayV2([]byte(apiGatewayV2TestEvent), Options{})
asrt.NoError(err)
asrt.Equal(expectedApiGatewayV2Dump, dump)
}
func TestAPIGatewayV2ProxyPath(t *testing.T) {
asrt := assert.New(t)
dump, err := dumpAPIGatewayV2([]byte(apiGatewayV2TestEvent), Options{UseProxyPath: true})
asrt.NoError(err)
expected := expectedApiGatewayV2Dump
expected.RequestURI = "/my/path2?parameter1=value1¶meter1=value2¶meter2=value"
expected.URL.Path = "/my/path2"
asrt.Equal(expected, dump)
}
func TestAPIGatewayV2Base64BodyRequest(t *testing.T) {
asrt := assert.New(t)
event := events.APIGatewayV2HTTPRequest{}
asrt.NoError(json.Unmarshal([]byte(apiGatewayV2TestEvent), &event))
event.IsBase64Encoded = true
event.Body = "SGVsbG8gZnJvbSBMYW1iZGE="
encodedEvent, err := json.Marshal(event)
asrt.NoError(err)
dump, err := dumpAPIGatewayV2(encodedEvent, Options{})
asrt.NoError(err)
asrt.Equal(expectedApiGatewayV2Dump, dump)
}
func TestAPIGatewayV2URLEncoding(t *testing.T) {
asrt := assert.New(t)
event := events.APIGatewayV2HTTPRequest{}
asrt.NoError(json.Unmarshal([]byte(apiGatewayV2TestEvent), &event))
event.RawPath = "/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82"
event.RawQueryString = "parameter1=value1¶meter1=value2¶meter2=%D1%82%D0%B5%D1%81%D1%82%22"
event.QueryStringParameters["parameter2"] = "тест\""
encodedEvent, err := json.Marshal(event)
asrt.NoError(err)
dump, err := dumpAPIGatewayV2(encodedEvent, Options{})
asrt.NoError(err)
expected := expectedApiGatewayV2Dump
expected.RequestURI = "/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82?parameter1=value1¶meter1=value2¶meter2=%D1%82%D0%B5%D1%81%D1%82%22"
expected.URL.Path = "/привет"
expected.Form = map[string][]string{
"parameter1": {"value1", "value2"},
"parameter2": {"тест\""},
}
asrt.Equal(expected, dump)
}
func TestAPIGatewayV2ResponseHeaders(t *testing.T) {
asrt := assert.New(t)
handler := func(w http.ResponseWriter, r *http.Request) {
header := w.Header()
header.Add("X-Foo", "1")
header.Add("X-Bar", "2")
header.Add("X-Bar", "3")
header.Add("Set-Cookie", "cookie1")
header.Add("Set-Cookie", "cookie2")
w.WriteHeader(404)
io.WriteString(w, "FOO")
}
lh := lambdaHandler{
httpHandler: http.HandlerFunc(handler),
opts: &Options{},
}
responseBytes, err := lh.Invoke(context.Background(), []byte(apiGatewayV2TestEvent))
asrt.NoError(err)
var r events.APIGatewayV2HTTPResponse
err = json.Unmarshal(responseBytes, &r)
asrt.NoError(err)
asrt.Equal(404, r.StatusCode)
asrt.Equal("FOO", r.Body)
expectedHeaders := map[string]string{
"X-Foo": "1",
"X-Bar": "2,3",
}
asrt.Equal(expectedHeaders, r.Headers)
asrt.Equal([]string{"cookie1", "cookie2"}, r.Cookies)
}
func TestAPIGatewayV2Base64BodyResponseAll(t *testing.T) {
testBodyResponseAll(t, apiGatewayV2TestEvent)
}
func TestAPIGatewayV2Base64BodyResponseNoMatch(t *testing.T) {
testBase64BodyResponseNoMatch(t, apiGatewayV2TestEvent)
}
func TestAPIGatewayV2Base64BodyResponseMatch(t *testing.T) {
testBase64BodyResponseMatch(t, apiGatewayV2TestEvent)
}