-
Notifications
You must be signed in to change notification settings - Fork 559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Lambda Function URL HTTP request and response types #436
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
package events | ||
|
||
// LambdaHTTPRequest contains data coming from the new HTTP API Gateway | ||
type LambdaHTTPRequest struct { | ||
Version string `json:"version"` // Version is expected to always be `"2.0"` | ||
RouteKey string `json:"routeKey"` // RouteKey is expected to always be `"$default"` | ||
RawPath string `json:"rawPath"` | ||
RawQueryString string `json:"rawQueryString"` | ||
Cookies []string `json:"cookies,omitempty"` | ||
Headers map[string]string `json:"headers"` | ||
QueryStringParameters map[string]string `json:"queryStringParameters,omitempty"` | ||
RequestContext LambdaHTTPRequestContext `json:"requestContext"` | ||
Body string `json:"body,omitempty"` | ||
IsBase64Encoded bool `json:"isBase64Encoded"` | ||
} | ||
|
||
// LambdaHTTPRequestContext contains the information to identify the AWS account and resources invoking the Lambda function. | ||
type LambdaHTTPRequestContext struct { | ||
RouteKey string `json:"routeKey"` // RouteKey is expected to always be `"$default"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto, thinking to delete this |
||
AccountID string `json:"accountId"` | ||
Stage string `json:"stage"` // Stage is expected to always be `"$default"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'm going to delete this too, docs are pretty clear that it's a vestigial field. |
||
RequestID string `json:"requestId"` | ||
Authorizer *LambdaHTTPRequestContextAuthorizerDescription `json:"authorizer,omitempty"` | ||
APIID string `json:"apiId"` // APIID is the Lambda URL ID | ||
DomainName string `json:"domainName"` // DomainName is of the format `"<url-id>.lambda-url.<region>.on.aws"` | ||
DomainPrefix string `json:"domainPrefix"` // DomainPrefix is the Lambda URL ID | ||
Time string `json:"time"` | ||
TimeEpoch int64 `json:"timeEpoch"` | ||
HTTP LambdaHTTPRequestContextHTTPDescription `json:"http"` | ||
} | ||
|
||
// LambdaHTTPRequestContextAuthorizerDescription contains authorizer information for the request context. | ||
type LambdaHTTPRequestContextAuthorizerDescription struct { | ||
IAM *LambdaHTTPRequestContextAuthorizerIAMDescription `json:"iam,omitempty"` | ||
} | ||
|
||
// LambdaHTTPRequestContextAuthorizerIAMDescription contains IAM information for the request context. | ||
type LambdaHTTPRequestContextAuthorizerIAMDescription struct { | ||
AccessKey string `json:"accessKey"` | ||
AccountID string `json:"accountId"` | ||
CallerID string `json:"callerId"` | ||
UserARN string `json:"userArn"` | ||
UserID string `json:"userId"` | ||
} | ||
|
||
// LambdaHTTPRequestContextHTTPDescription contains HTTP information for the request context. | ||
type LambdaHTTPRequestContextHTTPDescription struct { | ||
Method string `json:"method"` | ||
Path string `json:"path"` | ||
Protocol string `json:"protocol"` | ||
SourceIP string `json:"sourceIp"` | ||
UserAgent string `json:"userAgent"` | ||
} | ||
|
||
// LambdaHTTPResponse configures the response to be returned by API Gateway V2 for the request | ||
type LambdaHTTPResponse struct { | ||
StatusCode int `json:"statusCode"` | ||
Headers map[string]string `json:"headers"` | ||
Body string `json:"body"` | ||
IsBase64Encoded bool `json:"isBase64Encoded"` | ||
Cookies []string `json:"cookies"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
package events | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLambdaHTTPResponseMarshaling(t *testing.T) { | ||
|
||
// read json from file | ||
inputJSON, err := ioutil.ReadFile("./testdata/lambda-urls-response.json") | ||
if err != nil { | ||
t.Errorf("could not open test file. details: %v", err) | ||
} | ||
|
||
// de-serialize into Go object | ||
var inputEvent LambdaHTTPResponse | ||
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil { | ||
t.Errorf("could not unmarshal event. details: %v", err) | ||
} | ||
|
||
// serialize to json | ||
outputJSON, err := json.Marshal(inputEvent) | ||
if err != nil { | ||
t.Errorf("could not marshal event. details: %v", err) | ||
} | ||
|
||
assert.JSONEq(t, string(inputJSON), string(outputJSON)) | ||
} | ||
|
||
func TestLambdaHTTPRequestMarshaling(t *testing.T) { | ||
|
||
// read json from file | ||
inputJSON, err := ioutil.ReadFile("./testdata/lambda-urls-request.json") | ||
if err != nil { | ||
t.Errorf("could not open test file. details: %v", err) | ||
} | ||
|
||
// de-serialize into Go object | ||
var inputEvent LambdaHTTPRequest | ||
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil { | ||
t.Errorf("could not unmarshal event. details: %v", err) | ||
} | ||
|
||
// serialize to json | ||
outputJSON, err := json.Marshal(inputEvent) | ||
if err != nil { | ||
t.Errorf("could not marshal event. details: %v", err) | ||
} | ||
|
||
assert.JSONEq(t, string(inputJSON), string(outputJSON)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"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": "<urlid>", | ||
"authorizer": { | ||
"iam": { | ||
"accessKey": "AKIA...", | ||
"accountId": "111122223333", | ||
"callerId": "AIDA...", | ||
"userArn": "arn:aws:iam::111122223333:user/example-user", | ||
"userId": "AIDA..." | ||
} | ||
}, | ||
"domainName": "<url-id>.lambda-url.us-west-2.on.aws", | ||
"domainPrefix": "<url-id>", | ||
"http": { | ||
"method": "POST", | ||
"path": "/my/path", | ||
"protocol": "HTTP/1.1", | ||
"sourceIp": "123.123.123.123", | ||
"userAgent": "agent" | ||
}, | ||
"requestId": "id", | ||
"routeKey": "$default", | ||
"stage": "$default", | ||
"time": "12/Mar/2020:19:03:58 +0000", | ||
"timeEpoch": 1583348638390 | ||
}, | ||
"body": "Hello from client!", | ||
"isBase64Encoded": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"statusCode": 201, | ||
"headers": { | ||
"Content-Type": "application/json", | ||
"My-Custom-Header": "Custom Value" | ||
}, | ||
"body": "{ \"message\": \"Hello, world!\" }", | ||
"cookies": [ | ||
"Cookie_1=Value1; Expires=21 Oct 2021 07:48 GMT", | ||
"Cookie_2=Value2; Max-Age=78000" | ||
], | ||
"isBase64Encoded": false | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'm gonna change this to LambdaFunctionURLRequest/Response to align with the feature name in the documentation