Skip to content

Commit a8c815e

Browse files
authored
feature: Create LambdaProxyEvent Type (#18)
feature: Create LambdaProxyEvent Type
1 parent 7487b2c commit a8c815e

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
/// LambdaGatewayProxyEvent contains data coming from the new HTTP API Gateway Proxy
16+
public struct LambdaGatewayProxyEvent: Decodable {
17+
public struct RequestContext: Decodable {
18+
/// Authorizer contains authorizer information for the request context.
19+
public struct Authorizer: Codable {
20+
public let claims: [String: String]?
21+
public let scopes: [String]?
22+
}
23+
24+
public let accountID: String
25+
public let apiID: String
26+
public let domainName: String
27+
public let domainPrefix: String
28+
public let stage: String
29+
public let requestID: String
30+
31+
public let httpMethod: HTTPMethod
32+
public let authorizer: Authorizer?
33+
34+
public let resourcePath: String?
35+
public let path: String?
36+
37+
/// The request time in format: 23/Apr/2020:11:08:18 +0000
38+
public let requestTime: String?
39+
public let requestTimeEpoch: UInt64
40+
41+
enum CodingKeys: String, CodingKey {
42+
case accountID = "accountId"
43+
case apiID = "apiId"
44+
case domainName
45+
case domainPrefix
46+
case stage
47+
48+
case httpMethod
49+
case authorizer
50+
51+
case requestID = "requestId"
52+
53+
case resourcePath
54+
case path
55+
56+
case requestTime
57+
case requestTimeEpoch
58+
}
59+
}
60+
61+
public let resource: String
62+
public let path: String
63+
public let httpMethod: String
64+
public let stageVariables: [String: String]?
65+
66+
public let cookies: [String]?
67+
public let headers: HTTPHeaders
68+
public let queryStringParameters: [String: String]?
69+
public let pathParameters: [String: String]?
70+
71+
public let requestContext: RequestContext
72+
73+
public let body: String?
74+
public let isBase64Encoded: Bool
75+
}
76+
77+
#if swift(>=5.6)
78+
extension LambdaGatewayProxyEvent: Sendable {}
79+
extension LambdaGatewayProxyEvent.RequestContext: Sendable {}
80+
extension LambdaGatewayProxyEvent.RequestContext.Authorizer: Sendable {}
81+
#endif
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
@testable import AWSLambdaEvents
16+
import XCTest
17+
18+
class LambdaGatewayProxyEventTests: XCTestCase {
19+
static let exampleLambdaProxyEvent = """
20+
{
21+
"resource": "/hello",
22+
"path": "/hello",
23+
"httpMethod": "GET",
24+
"headers": {
25+
"x-forwarded-proto":"https",
26+
"x-forwarded-for":"91.64.117.86",
27+
"x-forwarded-port":"443",
28+
"authorization":"Bearer abc123",
29+
"host":"hello.test.com",
30+
"x-amzn-trace-id":"Root=1-5ea3263d-07c5d5ddfd0788bed7dad831",
31+
"user-agent":"Paw/3.1.10 (Macintosh; OS X/10.15.4) GCDHTTPRequest",
32+
"content-length":"0"
33+
},
34+
"multiValueHeaders": {
35+
"header1": [
36+
"value1"
37+
],
38+
"header2": [
39+
"value1",
40+
"value2"
41+
]
42+
},
43+
"queryStringParameters": {
44+
"foo":"bar"
45+
},
46+
"multiValueQueryStringParameters": {
47+
"parameter1": [
48+
"value1",
49+
"value2"
50+
],
51+
"parameter2": [
52+
"value"
53+
]
54+
},
55+
"requestContext": {
56+
"accountId": "123456789012",
57+
"apiId":"pb5dg6g3rg",
58+
"authorizer": {
59+
"scopes":[
60+
"hello"
61+
],
62+
"claims":{
63+
"aud":"customers",
64+
"iss":"https://hello.test.com/",
65+
"iat":"1587749276",
66+
"exp":"1587756476"
67+
}
68+
},
69+
"domainName": "id.execute-api.us-east-1.amazonaws.com",
70+
"domainPrefix": "id",
71+
"extendedRequestId": "request-id",
72+
"httpMethod": "GET",
73+
"identity": {
74+
"accessKey": null,
75+
"accountId": null,
76+
"caller": null,
77+
"cognitoAuthenticationProvider": null,
78+
"cognitoAuthenticationType": null,
79+
"cognitoIdentityId": null,
80+
"cognitoIdentityPoolId": null,
81+
"principalOrgId": null,
82+
"sourceIp":"91.64.117.86",
83+
"user": null,
84+
"userAgent":"Paw/3.1.10 (Macintosh; OS X/10.15.4) GCDHTTPRequest",
85+
"userArn": null,
86+
"clientCert": {
87+
"clientCertPem": "CERT_CONTENT",
88+
"subjectDN": "www.example.com",
89+
"issuerDN": "Example issuer",
90+
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
91+
"validity": {
92+
"notBefore": "May 28 12:30:02 2019 GMT",
93+
"notAfter": "Aug 5 09:36:04 2021 GMT"
94+
}
95+
}
96+
},
97+
"path": "/hello",
98+
"protocol": "HTTP/1.1",
99+
"requestId":"LgLpnibOFiAEPCA=",
100+
"requestTime": "04/Mar/2020:19:15:17 +0000",
101+
"requestTimeEpoch": 1587750461466,
102+
"resourceId": null,
103+
"resourcePath": "/my/path",
104+
"stage": "$default"
105+
},
106+
"pathParameters": null,
107+
"stageVariables":{
108+
"foo":"bar"
109+
},
110+
"body": "Hello from Lambda!",
111+
"isBase64Encoded": false
112+
}
113+
"""
114+
115+
// MARK: - Request -
116+
117+
// MARK: Decoding
118+
119+
func testRequestDecodingExampleProxyRequest() {
120+
let data = LambdaGatewayProxyEventTests.exampleLambdaProxyEvent.data(using: .utf8)!
121+
var req: LambdaGatewayProxyEvent?
122+
XCTAssertNoThrow(req = try JSONDecoder().decode(LambdaGatewayProxyEvent.self, from: data))
123+
124+
XCTAssertEqual(req?.path, "/hello")
125+
XCTAssertEqual(req?.requestContext.httpMethod, .GET)
126+
XCTAssertEqual(req?.queryStringParameters?.count, 1)
127+
XCTAssertEqual(req?.headers.count, 8)
128+
XCTAssertNotNil(req?.body)
129+
}
130+
}

0 commit comments

Comments
 (0)