Skip to content

Commit 460325b

Browse files
jmnarlochbmoffatt
authored andcommitted
Adds OperationName to API Gateway Proxy Request (#224)
operationName is included by API Gateway for API's that were imported through OpenAPI spec.
1 parent cb75cfe commit 460325b

File tree

3 files changed

+128
-9
lines changed

3 files changed

+128
-9
lines changed

events/apigw.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ type APIGatewayProxyResponse struct {
3030
// APIGatewayProxyRequestContext contains the information to identify the AWS account and resources invoking the
3131
// Lambda function. It also includes Cognito identity information for the caller.
3232
type APIGatewayProxyRequestContext struct {
33-
AccountID string `json:"accountId"`
34-
ResourceID string `json:"resourceId"`
35-
Stage string `json:"stage"`
36-
RequestID string `json:"requestId"`
37-
Identity APIGatewayRequestIdentity `json:"identity"`
38-
ResourcePath string `json:"resourcePath"`
39-
Authorizer map[string]interface{} `json:"authorizer"`
40-
HTTPMethod string `json:"httpMethod"`
41-
APIID string `json:"apiId"` // The API Gateway rest API Id
33+
AccountID string `json:"accountId"`
34+
ResourceID string `json:"resourceId"`
35+
OperationName string `json:"operationName,omitempty"`
36+
Stage string `json:"stage"`
37+
RequestID string `json:"requestId"`
38+
Identity APIGatewayRequestIdentity `json:"identity"`
39+
ResourcePath string `json:"resourcePath"`
40+
Authorizer map[string]interface{} `json:"authorizer"`
41+
HTTPMethod string `json:"httpMethod"`
42+
APIID string `json:"apiId"` // The API Gateway rest API Id
4243
}
4344

4445
// APIGatewayRequestIdentity contains identity information for the request caller.

events/apigw_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,32 @@ func TestApiGatewayCustomAuthorizerResponseMarshaling(t *testing.T) {
180180
func TestApiGatewayCustomAuthorizerResponseMalformedJson(t *testing.T) {
181181
test.TestMalformedJson(t, APIGatewayCustomAuthorizerResponse{})
182182
}
183+
184+
func TestApiGatewayRestApiOpenApiRequestMarshaling(t *testing.T) {
185+
186+
// read json from file
187+
inputJSON, err := ioutil.ReadFile("./testdata/apigw-restapi-openapi-request.json")
188+
if err != nil {
189+
t.Errorf("could not open test file. details: %v", err)
190+
}
191+
192+
// de-serialize into Go object
193+
var inputEvent APIGatewayProxyRequest
194+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
195+
t.Errorf("could not unmarshal event. details: %v", err)
196+
}
197+
198+
// validate request context
199+
requestContext := inputEvent.RequestContext
200+
if requestContext.OperationName != "HelloWorld" {
201+
t.Errorf("could not extract operationName from context: %v", requestContext)
202+
}
203+
204+
// serialize to json
205+
outputJSON, err := json.Marshal(inputEvent)
206+
if err != nil {
207+
t.Errorf("could not marshal event. details: %v", err)
208+
}
209+
210+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
211+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{
2+
"resource": "/{proxy+}",
3+
"path": "/hello/world",
4+
"httpMethod": "POST",
5+
"headers": {
6+
"Accept": "*/*",
7+
"Accept-Encoding": "gzip, deflate",
8+
"cache-control": "no-cache",
9+
"CloudFront-Forwarded-Proto": "https",
10+
"CloudFront-Is-Desktop-Viewer": "true",
11+
"CloudFront-Is-Mobile-Viewer": "false",
12+
"CloudFront-Is-SmartTV-Viewer": "false",
13+
"CloudFront-Is-Tablet-Viewer": "false",
14+
"CloudFront-Viewer-Country": "US",
15+
"Content-Type": "application/json",
16+
"headerName": "headerValue",
17+
"Host": "gy415nuibc.execute-api.us-east-1.amazonaws.com",
18+
"Postman-Token": "9f583ef0-ed83-4a38-aef3-eb9ce3f7a57f",
19+
"User-Agent": "PostmanRuntime/2.4.5",
20+
"Via": "1.1 d98420743a69852491bbdea73f7680bd.cloudfront.net (CloudFront)",
21+
"X-Amz-Cf-Id": "pn-PWIJc6thYnZm5P0NMgOUglL1DYtl0gdeJky8tqsg8iS_sgsKD1A==",
22+
"X-Forwarded-For": "54.240.196.186, 54.182.214.83",
23+
"X-Forwarded-Port": "443",
24+
"X-Forwarded-Proto": "https"
25+
},
26+
"multiValueHeaders": {
27+
"Accept": ["*/*"],
28+
"Accept-Encoding": ["gzip, deflate"],
29+
"cache-control": ["no-cache"],
30+
"CloudFront-Forwarded-Proto": ["https"],
31+
"CloudFront-Is-Desktop-Viewer": ["true"],
32+
"CloudFront-Is-Mobile-Viewer": ["false"],
33+
"CloudFront-Is-SmartTV-Viewer": ["false"],
34+
"CloudFront-Is-Tablet-Viewer": ["false"],
35+
"CloudFront-Viewer-Country": ["US"],
36+
"Content-Type": ["application/json"],
37+
"headerName": ["headerValue"],
38+
"Host": ["gy415nuibc.execute-api.us-east-1.amazonaws.com"],
39+
"Postman-Token": ["9f583ef0-ed83-4a38-aef3-eb9ce3f7a57f"],
40+
"User-Agent": ["PostmanRuntime/2.4.5"],
41+
"Via": ["1.1 d98420743a69852491bbdea73f7680bd.cloudfront.net (CloudFront)"],
42+
"X-Amz-Cf-Id": ["pn-PWIJc6thYnZm5P0NMgOUglL1DYtl0gdeJky8tqsg8iS_sgsKD1A=="],
43+
"X-Forwarded-For": ["54.240.196.186, 54.182.214.83"],
44+
"X-Forwarded-Port": ["443"],
45+
"X-Forwarded-Proto": ["https"]
46+
},
47+
"queryStringParameters": {
48+
"name": "me"
49+
},
50+
"multiValueQueryStringParameters": {
51+
"name": ["me"]
52+
},
53+
"pathParameters": {
54+
"proxy": "hello/world"
55+
},
56+
"stageVariables": {
57+
"stageVariableName": "stageVariableValue"
58+
},
59+
"requestContext": {
60+
"accountId": "12345678912",
61+
"resourceId": "roq9wj",
62+
"operationName": "HelloWorld",
63+
"stage": "testStage",
64+
"requestId": "deef4878-7910-11e6-8f14-25afc3e9ae33",
65+
"identity": {
66+
"cognitoIdentityPoolId": "theCognitoIdentityPoolId",
67+
"accountId": "theAccountId",
68+
"cognitoIdentityId": "theCognitoIdentityId",
69+
"caller": "theCaller",
70+
"apiKey": "theApiKey",
71+
"accessKey": "ANEXAMPLEOFACCESSKEY",
72+
"sourceIp": "192.168.196.186",
73+
"cognitoAuthenticationType": "theCognitoAuthenticationType",
74+
"cognitoAuthenticationProvider": "theCognitoAuthenticationProvider",
75+
"userArn": "theUserArn",
76+
"userAgent": "PostmanRuntime/2.4.5",
77+
"user": "theUser"
78+
},
79+
"authorizer": {
80+
"principalId": "admin",
81+
"clientId": 1,
82+
"clientName": "Exata"
83+
},
84+
"resourcePath": "/{proxy+}",
85+
"httpMethod": "POST",
86+
"apiId": "gy415nuibc"
87+
},
88+
"body": "{\r\n\t\"a\": 1\r\n}"
89+
}

0 commit comments

Comments
 (0)