Skip to content

Commit f18b722

Browse files
committed
Add routeKey and stage and others to Lambda event and update tests
1 parent 697c451 commit f18b722

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"os"
1111
"strings"
12+
"time"
1213

1314
"github.com/aws/aws-lambda-go/events"
1415
)
@@ -53,19 +54,26 @@ func handler(w http.ResponseWriter, r *http.Request) {
5354
}
5455
}
5556

57+
// See. https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html#urls-payloads
5658
func buildLambdaEvent(r *http.Request) (*events.APIGatewayV2HTTPRequest, error) {
5759
bodyBytes, _ := io.ReadAll(r.Body)
5860
body := string(bodyBytes)
5961

6062
return &events.APIGatewayV2HTTPRequest{
6163
Version: "2.0",
64+
RouteKey: "$default",
6265
RawPath: r.URL.Path,
6366
RawQueryString: r.URL.RawQuery,
6467
Headers: joinValuesWithComma(r.Header),
6568
QueryStringParameters: joinValuesWithComma(r.URL.Query()),
6669
Body: body,
6770
IsBase64Encoded: false,
6871
RequestContext: events.APIGatewayV2HTTPRequestContext{
72+
RouteKey: "$default",
73+
Stage: "$default",
74+
Time: time.Now().Format("02/Jan/2006:15:04:05 -0700"),
75+
TimeEpoch: time.Now().Unix(),
76+
DomainName: r.Host,
6977
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
7078
Method: r.Method,
7179
Path: r.URL.Path,

main_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ func TestHandler(t *testing.T) {
8484
t.Errorf("expected version to be '2.0', got %v", requestedBodyJSON["version"])
8585
}
8686

87+
if requestedBodyJSON["routeKey"] != "$default" {
88+
t.Errorf("expected routeKey to be '$default', got %v", requestedBodyJSON["routeKey"])
89+
}
90+
8791
if requestedBodyJSON["rawPath"] != "/foo/bar" {
8892
t.Errorf("expected rawPath to be '/foo/bar', got %v", requestedBodyJSON["rawPath"])
8993
}
@@ -114,6 +118,26 @@ func TestHandler(t *testing.T) {
114118
}
115119

116120
requestContext := requestedBodyJSON["requestContext"].(map[string]interface{})
121+
if requestContext["routeKey"] != "$default" {
122+
t.Errorf("expected routeKey to be '$default', got %v", requestContext["routeKey"])
123+
}
124+
125+
if requestContext["stage"] != "$default" {
126+
t.Errorf("expected stage to be '$default', got %v", requestContext["stage"])
127+
}
128+
129+
if _, ok := requestContext["time"].(string); !ok {
130+
t.Errorf("expected time to be a string, got %v", requestContext["time"])
131+
}
132+
133+
if _, ok := requestContext["timeEpoch"].(float64); !ok {
134+
t.Errorf("expected timeEpoch to be a float64, got %v", requestContext["timeEpoch"])
135+
}
136+
137+
if requestContext["domainName"] != req.Host {
138+
t.Errorf("expected domainName to be '%s', got %v", req.Host, requestContext["domainName"])
139+
}
140+
117141
httpContext := requestContext["http"].(map[string]interface{})
118142

119143
if httpContext["method"] != "POST" {
@@ -131,4 +155,5 @@ func TestHandler(t *testing.T) {
131155
if httpContext["userAgent"] != "Go-http-client/1.1" {
132156
t.Errorf("expected userAgent to be 'Go-http-client/1.1', got %v", httpContext["userAgent"])
133157
}
158+
134159
}

test/e2e.test.mjs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test("E2E Test for GET Request", async () => {
1212
message: "Hello from Lambda!",
1313
event: {
1414
version: "2.0",
15-
routeKey: "",
15+
routeKey: "$default",
1616
rawPath: "/foo/bar",
1717
rawQueryString: "testkey=testvalue",
1818
headers: expect.objectContaining({
@@ -23,14 +23,12 @@ test("E2E Test for GET Request", async () => {
2323
testkey: "testvalue",
2424
},
2525
requestContext: {
26-
routeKey: "",
27-
accountId: "",
28-
stage: "",
29-
requestId: expect.any(String),
30-
apiId: "",
31-
domainName: "",
32-
domainPrefix: "",
33-
time: expect.any(String),
26+
routeKey: "$default",
27+
stage: "$default",
28+
domainName: "localhost:8080",
29+
time: expect.stringMatching(
30+
/^\d{2}\/[A-Za-z]{3}\/\d{4}:\d{2}:\d{2}:\d{2} [-+]\d{4}$/
31+
),
3432
timeEpoch: expect.any(Number),
3533
http: expect.objectContaining({
3634
method: "GET",
@@ -78,7 +76,7 @@ test("E2E Test for POST Request", async () => {
7876
message: "Hello from Lambda!",
7977
event: {
8078
version: "2.0",
81-
routeKey: "",
79+
routeKey: "$default",
8280
rawPath: "/foo/bar",
8381
rawQueryString: "",
8482
headers: expect.objectContaining({
@@ -88,14 +86,12 @@ test("E2E Test for POST Request", async () => {
8886
"User-Agent": expect.any(String),
8987
}),
9088
requestContext: {
91-
routeKey: "",
92-
accountId: "",
93-
stage: "",
94-
requestId: expect.any(String),
95-
apiId: "",
96-
domainName: "",
97-
domainPrefix: "",
98-
time: expect.any(String),
89+
routeKey: "$default",
90+
stage: "$default",
91+
domainName: "localhost:8080",
92+
time: expect.stringMatching(
93+
/^\d{2}\/[A-Za-z]{3}\/\d{4}:\d{2}:\d{2}:\d{2} [-+]\d{4}$/
94+
),
9995
timeEpoch: expect.any(Number),
10096
http: expect.objectContaining({
10197
method: "POST",

0 commit comments

Comments
 (0)