Skip to content

Commit ae07717

Browse files
committed
Added APIGateway V2
1 parent 59e9d8b commit ae07717

File tree

3 files changed

+231
-35
lines changed

3 files changed

+231
-35
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
extension APIGateway {
16+
public struct V2 {}
17+
}
18+
19+
extension APIGateway.V2 {
20+
/// APIGateway.V2.Request contains data coming from the new HTTP API Gateway
21+
public struct Request: Codable {
22+
/// Context contains the information to identify the AWS account and resources invoking the Lambda function.
23+
public struct Context: Codable {
24+
public struct HTTP: Codable {
25+
public let method: HTTPMethod
26+
public let path: String
27+
public let `protocol`: String
28+
public let sourceIp: String
29+
public let userAgent: String
30+
}
31+
32+
/// Authorizer contains authorizer information for the request context.
33+
public struct Authorizer: Codable {
34+
/// JWT contains JWT authorizer information for the request context.
35+
public struct JWT: Codable {
36+
public let claims: [String: String]
37+
public let scopes: [String]?
38+
}
39+
40+
let jwt: JWT
41+
}
42+
43+
public let accountId: String
44+
public let apiId: String
45+
public let domainName: String
46+
public let domainPrefix: String
47+
public let stage: String
48+
public let requestId: String
49+
50+
public let http: HTTP
51+
public let authorizer: Authorizer?
52+
53+
/// The request time in format: 23/Apr/2020:11:08:18 +0000
54+
public let time: String
55+
public let timeEpoch: UInt64
56+
}
57+
58+
public let version: String
59+
public let routeKey: String
60+
public let rawPath: String
61+
public let rawQueryString: String
62+
63+
public let cookies: [String]?
64+
public let headers: HTTPHeaders
65+
public let queryStringParameters: [String: String]?
66+
public let pathParameters: [String: String]?
67+
68+
public let context: Context
69+
public let stageVariables: [String: String]?
70+
71+
public let body: String?
72+
public let isBase64Encoded: Bool
73+
74+
enum CodingKeys: String, CodingKey {
75+
case version
76+
case routeKey
77+
case rawPath
78+
case rawQueryString
79+
80+
case cookies
81+
case headers
82+
case queryStringParameters
83+
case pathParameters
84+
85+
case context = "requestContext"
86+
case stageVariables
87+
88+
case body
89+
case isBase64Encoded
90+
}
91+
}
92+
}
93+
94+
extension APIGateway.V2 {
95+
public struct Response: Codable {
96+
public let statusCode: HTTPResponseStatus
97+
public let headers: HTTPHeaders?
98+
public let multiValueHeaders: HTTPMultiValueHeaders?
99+
public let body: String?
100+
public let isBase64Encoded: Bool?
101+
public let cookies: [String]?
102+
103+
public init(
104+
statusCode: HTTPResponseStatus,
105+
headers: HTTPHeaders? = nil,
106+
multiValueHeaders: HTTPMultiValueHeaders? = nil,
107+
body: String? = nil,
108+
isBase64Encoded: Bool? = nil,
109+
cookies: [String]? = nil
110+
) {
111+
self.statusCode = statusCode
112+
self.headers = headers
113+
self.multiValueHeaders = multiValueHeaders
114+
self.body = body
115+
self.isBase64Encoded = isBase64Encoded
116+
self.cookies = cookies
117+
}
118+
}
119+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 APIGatewayV2Tests: XCTestCase {
19+
static let exampleGetPayload = """
20+
{
21+
"routeKey":"GET /hello",
22+
"version":"2.0",
23+
"rawPath":"/hello",
24+
"stageVariables":{
25+
"foo":"bar"
26+
},
27+
"requestContext":{
28+
"timeEpoch":1587750461466,
29+
"domainPrefix":"hello",
30+
"authorizer":{
31+
"jwt":{
32+
"scopes":[
33+
"hello"
34+
],
35+
"claims":{
36+
"aud":"customers",
37+
"iss":"https://hello.test.com/",
38+
"iat":"1587749276",
39+
"exp":"1587756476"
40+
}
41+
}
42+
},
43+
"accountId":"0123456789",
44+
"stage":"$default",
45+
"domainName":"hello.test.com",
46+
"apiId":"pb5dg6g3rg",
47+
"requestId":"LgLpnibOFiAEPCA=",
48+
"http":{
49+
"path":"/hello",
50+
"userAgent":"Paw/3.1.10 (Macintosh; OS X/10.15.4) GCDHTTPRequest",
51+
"method":"GET",
52+
"protocol":"HTTP/1.1",
53+
"sourceIp":"91.64.117.86"
54+
},
55+
"time":"24/Apr/2020:17:47:41 +0000"
56+
},
57+
"isBase64Encoded":false,
58+
"rawQueryString":"foo=bar",
59+
"queryStringParameters":{
60+
"foo":"bar"
61+
},
62+
"headers":{
63+
"x-forwarded-proto":"https",
64+
"x-forwarded-for":"91.64.117.86",
65+
"x-forwarded-port":"443",
66+
"authorization":"Bearer abc123",
67+
"host":"hello.test.com",
68+
"x-amzn-trace-id":"Root=1-5ea3263d-07c5d5ddfd0788bed7dad831",
69+
"user-agent":"Paw/3.1.10 (Macintosh; OS X/10.15.4) GCDHTTPRequest",
70+
"content-length":"0"
71+
}
72+
}
73+
"""
74+
75+
// MARK: - Request -
76+
77+
// MARK: Decoding
78+
79+
func testRequestDecodingExampleGetRequest() {
80+
let data = APIGatewayV2Tests.exampleGetPayload.data(using: .utf8)!
81+
var req: APIGateway.V2.Request?
82+
XCTAssertNoThrow(req = try JSONDecoder().decode(APIGateway.V2.Request.self, from: data))
83+
84+
XCTAssertEqual(req?.rawPath, "/hello")
85+
XCTAssertEqual(req?.context.http.method, .GET)
86+
XCTAssertEqual(req?.queryStringParameters?.count, 1)
87+
XCTAssertEqual(req?.rawQueryString, "foo=bar")
88+
XCTAssertEqual(req?.headers.count, 8)
89+
XCTAssertNil(req?.body)
90+
}
91+
92+
}

Tests/AWSLambdaEventsTests/APIGatewayTests.swift

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,21 @@ class APIGatewayTests: XCTestCase {
2929
// MARK: Decoding
3030

3131
func testRequestDecodingExampleGetRequest() {
32-
do {
33-
let data = APIGatewayTests.exampleGetPayload.data(using: .utf8)!
34-
let request = try JSONDecoder().decode(APIGateway.Request.self, from: data)
32+
let data = APIGatewayTests.exampleGetPayload.data(using: .utf8)!
33+
var req: APIGateway.Request?
34+
XCTAssertNoThrow(req = try JSONDecoder().decode(APIGateway.Request.self, from: data))
3535

36-
XCTAssertEqual(request.path, "/test")
37-
XCTAssertEqual(request.httpMethod, .GET)
38-
} catch {
39-
XCTFail("Unexpected error: \(error)")
40-
}
36+
XCTAssertEqual(req?.path, "/test")
37+
XCTAssertEqual(req?.httpMethod, .GET)
4138
}
4239

4340
func testRequestDecodingTodoPostRequest() {
44-
struct Todo: Decodable {
45-
let title: String
46-
}
41+
let data = APIGatewayTests.todoPostPayload.data(using: .utf8)!
42+
var req: APIGateway.Request?
43+
XCTAssertNoThrow(req = try JSONDecoder().decode(APIGateway.Request.self, from: data))
4744

48-
do {
49-
let data = APIGatewayTests.todoPostPayload.data(using: .utf8)!
50-
let request = try JSONDecoder().decode(APIGateway.Request.self, from: data)
51-
52-
XCTAssertEqual(request.path, "/todos")
53-
XCTAssertEqual(request.httpMethod, .POST)
54-
55-
// let todo = try request.decodeBody(Todo.self)
56-
// XCTAssertEqual(todo.title, "a todo")
57-
} catch {
58-
XCTFail("Unexpected error: \(error)")
59-
}
45+
XCTAssertEqual(req?.path, "/todos")
46+
XCTAssertEqual(req?.httpMethod, .POST)
6047
}
6148

6249
// MARK: - Response -
@@ -76,17 +63,15 @@ class APIGatewayTests: XCTestCase {
7663
headers: ["Server": "Test"],
7764
body: "abc123"
7865
)
79-
80-
do {
81-
let data = try JSONEncoder().encode(resp)
82-
let json = try JSONDecoder().decode(JSONResponse.self, from: data)
83-
84-
XCTAssertEqual(json.statusCode, resp.statusCode.code)
85-
XCTAssertEqual(json.body, resp.body)
86-
XCTAssertEqual(json.isBase64Encoded, resp.isBase64Encoded)
87-
XCTAssertEqual(json.headers?["Server"], "Test")
88-
} catch {
89-
XCTFail("unexpected error: \(error)")
90-
}
66+
67+
var data: Data?
68+
XCTAssertNoThrow(data = try JSONEncoder().encode(resp))
69+
var json: JSONResponse?
70+
XCTAssertNoThrow(json = try JSONDecoder().decode(JSONResponse.self, from: XCTUnwrap(data)))
71+
72+
XCTAssertEqual(json?.statusCode, resp.statusCode.code)
73+
XCTAssertEqual(json?.body, resp.body)
74+
XCTAssertEqual(json?.isBase64Encoded, resp.isBase64Encoded)
75+
XCTAssertEqual(json?.headers?["Server"], "Test")
9176
}
9277
}

0 commit comments

Comments
 (0)