Skip to content

Commit 138d021

Browse files
authored
Add Lambda Function URL HTTP request and response types (#436)
* Add Lambda Function URL HTTP request and response types * naming update, remove fields documented to always be * update testdata * updated testdata
1 parent bc1ec47 commit 138d021

4 files changed

+175
-0
lines changed

events/lambda_function_urls.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
package events
4+
5+
// LambdaFunctionURLRequest contains data coming from the HTTP request to a Lambda Function URL.
6+
type LambdaFunctionURLRequest struct {
7+
Version string `json:"version"` // Version is expected to be `"2.0"`
8+
RawPath string `json:"rawPath"`
9+
RawQueryString string `json:"rawQueryString"`
10+
Cookies []string `json:"cookies,omitempty"`
11+
Headers map[string]string `json:"headers"`
12+
QueryStringParameters map[string]string `json:"queryStringParameters,omitempty"`
13+
RequestContext LambdaFunctionURLRequestContext `json:"requestContext"`
14+
Body string `json:"body,omitempty"`
15+
IsBase64Encoded bool `json:"isBase64Encoded"`
16+
}
17+
18+
// LambdaFunctionURLRequestContext contains the information to identify the AWS account and resources invoking the Lambda function.
19+
type LambdaFunctionURLRequestContext struct {
20+
AccountID string `json:"accountId"`
21+
RequestID string `json:"requestId"`
22+
Authorizer *LambdaFunctionURLRequestContextAuthorizerDescription `json:"authorizer,omitempty"`
23+
APIID string `json:"apiId"` // APIID is the Lambda URL ID
24+
DomainName string `json:"domainName"` // DomainName is of the format `"<url-id>.lambda-url.<region>.on.aws"`
25+
DomainPrefix string `json:"domainPrefix"` // DomainPrefix is the Lambda URL ID
26+
Time string `json:"time"`
27+
TimeEpoch int64 `json:"timeEpoch"`
28+
HTTP LambdaFunctionURLRequestContextHTTPDescription `json:"http"`
29+
}
30+
31+
// LambdaFunctionURLRequestContextAuthorizerDescription contains authorizer information for the request context.
32+
type LambdaFunctionURLRequestContextAuthorizerDescription struct {
33+
IAM *LambdaFunctionURLRequestContextAuthorizerIAMDescription `json:"iam,omitempty"`
34+
}
35+
36+
// LambdaFunctionURLRequestContextAuthorizerIAMDescription contains IAM information for the request context.
37+
type LambdaFunctionURLRequestContextAuthorizerIAMDescription struct {
38+
AccessKey string `json:"accessKey"`
39+
AccountID string `json:"accountId"`
40+
CallerID string `json:"callerId"`
41+
UserARN string `json:"userArn"`
42+
UserID string `json:"userId"`
43+
}
44+
45+
// LambdaFunctionURLRequestContextHTTPDescription contains HTTP information for the request context.
46+
type LambdaFunctionURLRequestContextHTTPDescription struct {
47+
Method string `json:"method"`
48+
Path string `json:"path"`
49+
Protocol string `json:"protocol"`
50+
SourceIP string `json:"sourceIp"`
51+
UserAgent string `json:"userAgent"`
52+
}
53+
54+
// LambdaFunctionURLResponse configures the HTTP response to be returned by Lambda Function URL for the request.
55+
type LambdaFunctionURLResponse struct {
56+
StatusCode int `json:"statusCode"`
57+
Headers map[string]string `json:"headers"`
58+
Body string `json:"body"`
59+
IsBase64Encoded bool `json:"isBase64Encoded"`
60+
Cookies []string `json:"cookies"`
61+
}

events/lambda_function_urls_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
package events
4+
5+
import (
6+
"encoding/json"
7+
"io/ioutil"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestLambdaFunctionURLResponseMarshaling(t *testing.T) {
14+
15+
// read json from file
16+
inputJSON, err := ioutil.ReadFile("./testdata/lambda-urls-response.json")
17+
if err != nil {
18+
t.Errorf("could not open test file. details: %v", err)
19+
}
20+
21+
// de-serialize into Go object
22+
var inputEvent LambdaFunctionURLResponse
23+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
24+
t.Errorf("could not unmarshal event. details: %v", err)
25+
}
26+
27+
// serialize to json
28+
outputJSON, err := json.Marshal(inputEvent)
29+
if err != nil {
30+
t.Errorf("could not marshal event. details: %v", err)
31+
}
32+
33+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
34+
}
35+
36+
func TestLambdaFunctionURLRequestMarshaling(t *testing.T) {
37+
38+
// read json from file
39+
inputJSON, err := ioutil.ReadFile("./testdata/lambda-urls-request.json")
40+
if err != nil {
41+
t.Errorf("could not open test file. details: %v", err)
42+
}
43+
44+
// de-serialize into Go object
45+
var inputEvent LambdaFunctionURLRequest
46+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
47+
t.Errorf("could not unmarshal event. details: %v", err)
48+
}
49+
50+
// serialize to json
51+
outputJSON, err := json.Marshal(inputEvent)
52+
if err != nil {
53+
t.Errorf("could not marshal event. details: %v", err)
54+
}
55+
56+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
57+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"version": "2.0",
3+
"rawPath": "/my/path",
4+
"rawQueryString": "parameter1=value1&parameter1=value2&parameter2=value",
5+
"cookies": [
6+
"cookie1",
7+
"cookie2"
8+
],
9+
"headers": {
10+
"header1": "value1",
11+
"header2": "value1,value2"
12+
},
13+
"queryStringParameters": {
14+
"parameter1": "value1,value2",
15+
"parameter2": "value"
16+
},
17+
"requestContext": {
18+
"accountId": "123456789012",
19+
"apiId": "<urlid>",
20+
"authorizer": {
21+
"iam": {
22+
"accessKey": "AKIA...",
23+
"accountId": "111122223333",
24+
"callerId": "AIDA...",
25+
"userArn": "arn:aws:iam::111122223333:user/example-user",
26+
"userId": "AIDA..."
27+
}
28+
},
29+
"domainName": "<url-id>.lambda-url.us-west-2.on.aws",
30+
"domainPrefix": "<url-id>",
31+
"http": {
32+
"method": "POST",
33+
"path": "/my/path",
34+
"protocol": "HTTP/1.1",
35+
"sourceIp": "123.123.123.123",
36+
"userAgent": "agent"
37+
},
38+
"requestId": "id",
39+
"time": "12/Mar/2020:19:03:58 +0000",
40+
"timeEpoch": 1583348638390
41+
},
42+
"body": "Hello from client!",
43+
"isBase64Encoded": false
44+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"statusCode": 201,
3+
"headers": {
4+
"Content-Type": "application/json",
5+
"My-Custom-Header": "Custom Value"
6+
},
7+
"body": "{ \"message\": \"Hello, world!\" }",
8+
"cookies": [
9+
"Cookie_1=Value1; Expires=21 Oct 2021 07:48 GMT",
10+
"Cookie_2=Value2; Max-Age=78000"
11+
],
12+
"isBase64Encoded": false
13+
}

0 commit comments

Comments
 (0)