Skip to content

Commit c83699c

Browse files
spezambmoffatt
authored andcommitted
ApiGw websocket events (#159)
* apigw websocket event testdata * Add event for ApiGW Websocket integration * typo in struct name * fixed test event * matching apigw testdata * duplicated identidy data * duplicated json field requestTimeEpoch * fixed test request json order * Fixes suggestions from pr: #159 issue: #154 * ExtendedRequestId -> ExtendedRequestID * Change type of Authorizer to interface{} * Update apigw.go changed wrong `Authorizer`
1 parent 044197f commit c83699c

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed

events/apigw.go

+44
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,50 @@ type APIGatewayRequestIdentity struct {
5757
User string `json:"user"`
5858
}
5959

60+
// APIGatewayWebsocketProxyRequest contains data coming from the API Gateway proxy
61+
type APIGatewayWebsocketProxyRequest struct {
62+
Resource string `json:"resource"` // The resource path defined in API Gateway
63+
Path string `json:"path"` // The url path for the caller
64+
HTTPMethod string `json:"httpMethod"`
65+
Headers map[string]string `json:"headers"`
66+
MultiValueHeaders map[string][]string `json:"multiValueHeaders"`
67+
QueryStringParameters map[string]string `json:"queryStringParameters"`
68+
MultiValueQueryStringParameters map[string][]string `json:"multiValueQueryStringParameters"`
69+
PathParameters map[string]string `json:"pathParameters"`
70+
StageVariables map[string]string `json:"stageVariables"`
71+
RequestContext APIGatewayWebsocketProxyRequestContext `json:"requestContext"`
72+
Body string `json:"body"`
73+
IsBase64Encoded bool `json:"isBase64Encoded,omitempty"`
74+
}
75+
76+
// APIGatewayWebsocketProxyRequestContext contains the information to identify
77+
// the AWS account and resources invoking the Lambda function. It also includes
78+
// Cognito identity information for the caller.
79+
type APIGatewayWebsocketProxyRequestContext struct {
80+
AccountID string `json:"accountId"`
81+
ResourceID string `json:"resourceId"`
82+
Stage string `json:"stage"`
83+
RequestID string `json:"requestId"`
84+
Identity APIGatewayRequestIdentity `json:"identity"`
85+
ResourcePath string `json:"resourcePath"`
86+
Authorizer interface{} `json:"authorizer"`
87+
HTTPMethod string `json:"httpMethod"`
88+
APIID string `json:"apiId"` // The API Gateway rest API Id
89+
ConnectedAt int64 `json:"connectedAt"`
90+
ConnectionID string `json:"connectionId"`
91+
DomainName string `json:"domainName"`
92+
Error string `json:"error"`
93+
EventType string `json:"eventType"`
94+
ExtendedRequestID string `json:"extendedRequestId"`
95+
IntegrationLatency string `json:"integrationLatency"`
96+
MessageDirection string `json:"messageDirection"`
97+
MessageID interface{} `json:"messageId"`
98+
RequestTime string `json:"requestTime"`
99+
RequestTimeEpoch int64 `json:"requestTimeEpoch"`
100+
RouteKey string `json:"routeKey"`
101+
Status string `json:"status"`
102+
}
103+
60104
// APIGatewayCustomAuthorizerRequestTypeRequestIdentity contains identity information for the request caller.
61105
type APIGatewayCustomAuthorizerRequestTypeRequestIdentity struct {
62106
APIKey string `json:"apiKey"`

events/apigw_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,33 @@ func TestApiGatewayCustomAuthorizerRequestTypeRequestMalformedJson(t *testing.T)
127127
test.TestMalformedJson(t, APIGatewayCustomAuthorizerRequestTypeRequest{})
128128
}
129129

130+
func TestApiGatewayWebsocketRequestMarshaling(t *testing.T) {
131+
132+
// read json from file
133+
inputJSON, err := ioutil.ReadFile("./testdata/apigw-websocket-request.json")
134+
if err != nil {
135+
t.Errorf("could not open test file. details: %v", err)
136+
}
137+
138+
// de-serialize into Go object
139+
var inputEvent APIGatewayWebsocketProxyRequest
140+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
141+
t.Errorf("could not unmarshal event. details: %v", err)
142+
}
143+
144+
// serialize to json
145+
outputJSON, err := json.Marshal(inputEvent)
146+
if err != nil {
147+
t.Errorf("could not marshal event. details: %v", err)
148+
}
149+
150+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
151+
}
152+
153+
func TestApiGatewayWebsocketRequestMalformedJson(t *testing.T) {
154+
test.TestMalformedJson(t, APIGatewayWebsocketProxyRequest{})
155+
}
156+
130157
func TestApiGatewayCustomAuthorizerResponseMarshaling(t *testing.T) {
131158

132159
// read json from file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
"Host": [
28+
"*.execute-api.eu-central-1.amazonaws.com"
29+
],
30+
"Sec-WebSocket-Extensions": [
31+
"permessage-deflate; client_max_window_bits"
32+
],
33+
"Sec-WebSocket-Key": [
34+
"*"
35+
],
36+
"Sec-WebSocket-Version": [
37+
"13"
38+
],
39+
"X-Amzn-Trace-Id": [
40+
"Root=*"
41+
],
42+
"X-Forwarded-For": [
43+
"*.*.*.*"
44+
],
45+
"X-Forwarded-Port": [
46+
"443"
47+
],
48+
"X-Forwarded-Proto": [
49+
"https"
50+
]
51+
},
52+
"queryStringParameters": {
53+
"name": "me"
54+
},
55+
"multiValueQueryStringParameters": {
56+
"name": ["me"]
57+
},
58+
"pathParameters": {
59+
"proxy": "hello/world"
60+
},
61+
"stageVariables": {
62+
"stageVariableName": "stageVariableValue"
63+
},
64+
"requestContext": {
65+
"accountId": "12345678912",
66+
"resourceId": "roq9wj",
67+
"stage": "testStage",
68+
"requestId": "deef4878-7910-11e6-8f14-25afc3e9ae33",
69+
"identity": {
70+
"cognitoIdentityPoolId": "theCognitoIdentityPoolId",
71+
"accountId": "theAccountId",
72+
"cognitoIdentityId": "theCognitoIdentityId",
73+
"caller": "theCaller",
74+
"apiKey": "theApiKey",
75+
"accessKey": "ANEXAMPLEOFACCESSKEY",
76+
"sourceIp": "192.168.196.186",
77+
"cognitoAuthenticationType": "theCognitoAuthenticationType",
78+
"cognitoAuthenticationProvider": "theCognitoAuthenticationProvider",
79+
"userArn": "theUserArn",
80+
"userAgent": "PostmanRuntime/2.4.5",
81+
"user": "theUser"
82+
},
83+
"resourcePath": "/{proxy+}",
84+
"authorizer": {
85+
"principalId": "admin",
86+
"clientId": 1,
87+
"clientName": "Exata"
88+
},
89+
"httpMethod": "POST",
90+
"apiId": "gy415nuibc",
91+
"connectedAt": 1547230720092,
92+
"connectionId": "TWegAcC4EowCHnA=",
93+
"domainName": "*.execute-api.eu-central-1.amazonaws.com",
94+
"error": "*",
95+
"eventType": "CONNECT",
96+
"extendedRequestId": "TWegAcC4EowCHnA=",
97+
"integrationLatency": "123",
98+
"messageDirection": "IN",
99+
"messageId": null,
100+
"requestTime": "07/Jan/2019:09:20:57 +0000",
101+
"requestTimeEpoch": 0,
102+
"routeKey": "$connect",
103+
"status": "*"
104+
},
105+
"body": "{\r\n\t\"a\": 1\r\n}"
106+
}

0 commit comments

Comments
 (0)