Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
with:
linux_5_9_enabled: false
linux_5_10_enabled: false
linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error"
linux_nightly_next_arguments_override: "--explicit-target-dependency-import-check error"
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error"

swift-6-language-mode:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ xcuserdata
Package.resolved
.serverless
.devcontainer
.amazonq
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// swift-tools-version:5.8
// swift-tools-version:6.0

import PackageDescription

let swiftSettings: [SwiftSetting] = [.enableExperimentalFeature("StrictConcurrency=complete")]

let package = Package(
name: "swift-aws-lambda-events",
platforms: [.macOS(.v15)],
products: [
.library(name: "AWSLambdaEvents", targets: ["AWSLambdaEvents"])
],
Expand Down
22 changes: 12 additions & 10 deletions Tests/AWSLambdaEventsTests/ALBTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
//
//===----------------------------------------------------------------------===//

import XCTest
import Foundation
import Testing

@testable import AWSLambdaEvents

class ALBTests: XCTestCase {
@Suite
struct ALBTests {
static let exampleSingleValueHeadersEventBody = """
{
"requestContext":{
Expand Down Expand Up @@ -45,21 +47,21 @@ class ALBTests: XCTestCase {
}
"""

func testRequestWithSingleValueHeadersEvent() {
@Test func requestWithSingleValueHeadersEvent() {
let data = ALBTests.exampleSingleValueHeadersEventBody.data(using: .utf8)!
do {
let decoder = JSONDecoder()

let event = try decoder.decode(ALBTargetGroupRequest.self, from: data)

XCTAssertEqual(event.httpMethod, .get)
XCTAssertEqual(event.body, "")
XCTAssertEqual(event.isBase64Encoded, false)
XCTAssertEqual(event.headers?.count, 11)
XCTAssertEqual(event.path, "/")
XCTAssertEqual(event.queryStringParameters, [:])
#expect(event.httpMethod == .get)
#expect(event.body == "")
#expect(event.isBase64Encoded == false)
#expect(event.headers?.count == 11)
#expect(event.path == "/")
#expect(event.queryStringParameters == [:])
} catch {
XCTFail("Unexpected error: \(error)")
Issue.record("Unexpected error: \(error)")
}
}
}
7 changes: 2 additions & 5 deletions Tests/AWSLambdaEventsTests/APIGateway+EncodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ struct APIGatewayEncodableResponseTests {
#expect(throws: Never.self) {
try response = APIGatewayV2Response(statusCode: .ok, encodableBody: businessResponse)
}
try #require(response?.body != nil)

// when
let body = response?.body?.data(using: .utf8)
try #require(body != nil)
let body = try #require(response?.body?.data(using: .utf8))

#expect(throws: Never.self) {
let encodedBody = try JSONDecoder().decode(BusinessResponse.self, from: body!)
let encodedBody = try JSONDecoder().decode(BusinessResponse.self, from: body)

// then
#expect(encodedBody == businessResponse)
Expand All @@ -63,7 +61,6 @@ struct APIGatewayEncodableResponseTests {

// when
let body = response?.body?.data(using: .utf8)
try #require(body != nil)

#expect(throws: Never.self) {
let encodedBody = try JSONDecoder().decode(BusinessResponse.self, from: body!)
Expand Down
36 changes: 18 additions & 18 deletions Tests/AWSLambdaEventsTests/APIGateway+V2IAMTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
//
//===----------------------------------------------------------------------===//

import XCTest
import Foundation
import Testing

@testable import AWSLambdaEvents

class APIGatewayV2IAMTests: XCTestCase {
@Suite
struct APIGatewayV2IAMTests {
static let getEventWithIAM = """
{
"version": "2.0",
Expand Down Expand Up @@ -133,30 +135,28 @@ class APIGatewayV2IAMTests: XCTestCase {

// MARK: Decoding

func testRequestDecodingGetRequestWithIAM() {
@Test func requestDecodingGetRequestWithIAM() throws {
let data = APIGatewayV2IAMTests.getEventWithIAM.data(using: .utf8)!
var req: APIGatewayV2Request?
XCTAssertNoThrow(req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data))
let req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)

XCTAssertEqual(req?.rawPath, "/hello")
XCTAssertEqual(req?.context.authorizer?.iam?.accessKey, "ASIA-redacted")
XCTAssertEqual(req?.context.authorizer?.iam?.accountId, "012345678912")
XCTAssertNil(req?.body)
#expect(req.rawPath == "/hello")
#expect(req.context.authorizer?.iam?.accessKey == "ASIA-redacted")
#expect(req.context.authorizer?.iam?.accountId == "012345678912")
#expect(req.body == nil)
}

func testRequestDecodingGetRequestWithIAMWithCognito() {
@Test func requestDecodingGetRequestWithIAMWithCognito() throws {
let data = APIGatewayV2IAMTests.getEventWithIAMAndCognito.data(using: .utf8)!
var req: APIGatewayV2Request?
XCTAssertNoThrow(req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data))
let req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)

XCTAssertEqual(req?.rawPath, "/hello")
XCTAssertEqual(req?.context.authorizer?.iam?.accessKey, "ASIA-redacted")
XCTAssertEqual(req?.context.authorizer?.iam?.accountId, "012345678912")
#expect(req.rawPath == "/hello")
#expect(req.context.authorizer?.iam?.accessKey == "ASIA-redacted")
#expect(req.context.authorizer?.iam?.accountId == "012345678912")

// test the cognito identity part
XCTAssertEqual(req?.context.authorizer?.iam?.cognitoIdentity?.identityId, "us-east-1:68bc0ecd-9d5e--redacted")
XCTAssertEqual(req?.context.authorizer?.iam?.cognitoIdentity?.amr?.count, 3)
#expect(req.context.authorizer?.iam?.cognitoIdentity?.identityId == "us-east-1:68bc0ecd-9d5e--redacted")
#expect(req.context.authorizer?.iam?.cognitoIdentity?.amr?.count == 3)

XCTAssertNil(req?.body)
#expect(req.body == nil)
}
}
45 changes: 24 additions & 21 deletions Tests/AWSLambdaEventsTests/APIGateway+V2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
//
//===----------------------------------------------------------------------===//

import XCTest
import Foundation
import Testing

@testable import AWSLambdaEvents

class APIGatewayV2Tests: XCTestCase {
@Suite
struct APIGatewayV2Tests {
static let exampleGetEventBody = """
{
"routeKey":"GET /hello",
Expand Down Expand Up @@ -189,36 +191,37 @@ class APIGatewayV2Tests: XCTestCase {

// MARK: Decoding

func testRequestDecodingExampleGetRequest() {
@Test func requestDecodingExampleGetRequest() throws {
let data = APIGatewayV2Tests.exampleGetEventBody.data(using: .utf8)!
var req: APIGatewayV2Request?
XCTAssertNoThrow(req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data))
let req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)

XCTAssertEqual(req?.rawPath, "/hello")
XCTAssertEqual(req?.context.http.method, .get)
XCTAssertEqual(req?.queryStringParameters.count, 1)
XCTAssertEqual(req?.rawQueryString, "foo=bar")
XCTAssertEqual(req?.headers.count, 8)
XCTAssertEqual(req?.context.authorizer?.jwt?.claims?["aud"], "customers")
#expect(req.rawPath == "/hello")
#expect(req.context.http.method == .get)
#expect(req.queryStringParameters.count == 1)
#expect(req.rawQueryString == "foo=bar")
#expect(req.headers.count == 8)
#expect(req.context.authorizer?.jwt?.claims?["aud"] == "customers")

XCTAssertNil(req?.body)
#expect(req.body == nil)
}

func testDecodingRequestClientCert() throws {
@Test func decodingRequestClientCert() throws {
let data = APIGatewayV2Tests.fullExamplePayload.data(using: .utf8)!
let request = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
let clientCert = request.context.authentication?.clientCert

XCTAssertEqual(clientCert?.clientCertPem, "CERT_CONTENT")
XCTAssertEqual(clientCert?.subjectDN, "www.example.com")
XCTAssertEqual(clientCert?.issuerDN, "Example issuer")
XCTAssertEqual(clientCert?.serialNumber, "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1")
XCTAssertEqual(clientCert?.validity.notBefore, "May 28 12:30:02 2019 GMT")
XCTAssertEqual(clientCert?.validity.notAfter, "Aug 5 09:36:04 2021 GMT")
#expect(clientCert?.clientCertPem == "CERT_CONTENT")
#expect(clientCert?.subjectDN == "www.example.com")
#expect(clientCert?.issuerDN == "Example issuer")
#expect(clientCert?.serialNumber == "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1")
#expect(clientCert?.validity.notBefore == "May 28 12:30:02 2019 GMT")
#expect(clientCert?.validity.notAfter == "Aug 5 09:36:04 2021 GMT")
}

func testDecodingNilCollections() {
@Test func decodingNilCollections() throws {
let data = APIGatewayV2Tests.exampleGetEventBodyNilHeaders.data(using: .utf8)!
XCTAssertNoThrow(_ = try JSONDecoder().decode(APIGatewayV2Request.self, from: data))
#expect(throws: Never.self) {
_ = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
}
}
}
106 changes: 45 additions & 61 deletions Tests/AWSLambdaEventsTests/APIGatewayLambdaAuthorizerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
//
//===----------------------------------------------------------------------===//

import XCTest
import Foundation
import Testing

@testable import AWSLambdaEvents

class APIGatewayLambdaAuthorizerTests: XCTestCase {
@Suite
struct APIGatewayLambdaAuthorizerTests {
static let getEventWithLambdaAuthorizer = """
{
"version": "2.0",
Expand Down Expand Up @@ -139,52 +141,45 @@ class APIGatewayLambdaAuthorizerTests: XCTestCase {

// MARK: Decoding

func testRequestDecodingGetRequestWithLambdaAuthorizer() {
@Test func requestDecodingGetRequestWithLambdaAuthorizer() throws {
let data = APIGatewayLambdaAuthorizerTests.getEventWithLambdaAuthorizer.data(using: .utf8)!
var req: APIGatewayV2Request?
XCTAssertNoThrow(req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data))
let req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)

XCTAssertEqual(req?.rawPath, "/hello")
XCTAssertEqual(req?.context.authorizer?.lambda?.count, 2)
XCTAssertEqual(req?.context.authorizer?.lambda?["abc1"], "xyz1")
XCTAssertEqual(req?.context.authorizer?.lambda?["abc2"], "xyz2")
XCTAssertNil(req?.body)
#expect(req.rawPath == "/hello")
#expect(req.context.authorizer?.lambda?.count == 2)
#expect(req.context.authorizer?.lambda?["abc1"] == "xyz1")
#expect(req.context.authorizer?.lambda?["abc2"] == "xyz2")
#expect(req.body == nil)
}

func testLambdaAuthorizerRequestRequestDecoding() {
@Test func lambdaAuthorizerRequestRequestDecoding() throws {
let data = APIGatewayLambdaAuthorizerTests.lambdaAuthorizerRequest.data(using: .utf8)!
var req: APIGatewayLambdaAuthorizerRequest?
XCTAssertNoThrow(req = try JSONDecoder().decode(APIGatewayLambdaAuthorizerRequest.self, from: data))
let req = try JSONDecoder().decode(APIGatewayLambdaAuthorizerRequest.self, from: data)

XCTAssertEqual(req?.rawPath, "/dev/applications")
XCTAssertEqual(req?.version, "2.0")
#expect(req.rawPath == "/dev/applications")
#expect(req.version == "2.0")
}

// MARK: Encoding

func testDecodingLambdaAuthorizerSimpleResponse() {
@Test func decodingLambdaAuthorizerSimpleResponse() throws {
var resp = APIGatewayLambdaAuthorizerSimpleResponse(
isAuthorized: true,
context: ["abc1": "xyz1", "abc2": "xyz2"]
)

var data: Data?
XCTAssertNoThrow(data = try JSONEncoder().encode(resp))
let data = try #require(try? JSONEncoder().encode(resp))
let stringData = try #require(String(data: data, encoding: .utf8))
let newData = try #require(stringData.data(using: .utf8))

var stringData: String?
XCTAssertNoThrow(stringData = try String(data: XCTUnwrap(data), encoding: .utf8))
resp = try JSONDecoder().decode(APIGatewayLambdaAuthorizerSimpleResponse.self, from: newData)

data = stringData?.data(using: .utf8)
XCTAssertNoThrow(
resp = try JSONDecoder().decode(APIGatewayLambdaAuthorizerSimpleResponse.self, from: XCTUnwrap(data))
)

XCTAssertEqual(resp.isAuthorized, true)
XCTAssertEqual(resp.context?.count, 2)
XCTAssertEqual(resp.context?["abc1"], "xyz1")
#expect(resp.isAuthorized == true)
#expect(resp.context?.count == 2)
#expect(resp.context?["abc1"] == "xyz1")
}

func testDecodingLambdaAuthorizerPolicyResponse() {
@Test func decodingLambdaAuthorizerPolicyResponse() throws {
let statement = APIGatewayLambdaAuthorizerPolicyResponse.PolicyDocument.Statement(
action: "s3:getObject",
effect: .allow,
Expand All @@ -197,25 +192,20 @@ class APIGatewayLambdaAuthorizerTests: XCTestCase {
context: ["abc1": "xyz1", "abc2": "xyz2"]
)

var data: Data?
XCTAssertNoThrow(data = try JSONEncoder().encode(resp))

var stringData: String?
XCTAssertNoThrow(stringData = try String(data: XCTUnwrap(data), encoding: .utf8))
let data = try #require(try? JSONEncoder().encode(resp))
let stringData = try #require(String(data: data, encoding: .utf8))
let newData = try #require(stringData.data(using: .utf8))

data = stringData?.data(using: .utf8)
XCTAssertNoThrow(
resp = try JSONDecoder().decode(APIGatewayLambdaAuthorizerPolicyResponse.self, from: XCTUnwrap(data))
)
resp = try JSONDecoder().decode(APIGatewayLambdaAuthorizerPolicyResponse.self, from: newData)

XCTAssertEqual(resp.principalId, "John Appleseed")
XCTAssertEqual(resp.policyDocument.statement.count, 1)
XCTAssertEqual(resp.policyDocument.statement[0].action, ["s3:getObject"])
XCTAssertEqual(resp.context?.count, 2)
XCTAssertEqual(resp.context?["abc1"], "xyz1")
#expect(resp.principalId == "John Appleseed")
#expect(resp.policyDocument.statement.count == 1)
#expect(resp.policyDocument.statement[0].action == ["s3:getObject"])
#expect(resp.context?.count == 2)
#expect(resp.context?["abc1"] == "xyz1")
}

func testDecodingLambdaAuthorizerPolicyResponseWithMultipleResources() {
@Test func decodingLambdaAuthorizerPolicyResponseWithMultipleResources() throws {
let statement = APIGatewayLambdaAuthorizerPolicyResponse.PolicyDocument.Statement(
action: ["execute-api:Invoke"],
effect: .allow,
Expand All @@ -231,28 +221,22 @@ class APIGatewayLambdaAuthorizerTests: XCTestCase {
context: ["abc1": "xyz1", "abc2": "xyz2"]
)

var data: Data?
XCTAssertNoThrow(data = try JSONEncoder().encode(resp))

var stringData: String?
XCTAssertNoThrow(stringData = try String(data: XCTUnwrap(data), encoding: .utf8))
let data = try #require(try? JSONEncoder().encode(resp))
let stringData = try #require(try? String(data: data, encoding: .utf8))
let newData = try #require(stringData.data(using: .utf8))

data = stringData?.data(using: .utf8)
XCTAssertNoThrow(
resp = try JSONDecoder().decode(APIGatewayLambdaAuthorizerPolicyResponse.self, from: XCTUnwrap(data))
)
resp = try JSONDecoder().decode(APIGatewayLambdaAuthorizerPolicyResponse.self, from: newData)

XCTAssertEqual(resp.principalId, "John Appleseed")
XCTAssertEqual(resp.policyDocument.statement.count, 1)
XCTAssertEqual(resp.policyDocument.statement[0].action, ["execute-api:Invoke"])
XCTAssertEqual(
resp.policyDocument.statement[0].resource,
[
#expect(resp.principalId == "John Appleseed")
#expect(resp.policyDocument.statement.count == 1)
#expect(resp.policyDocument.statement[0].action == ["execute-api:Invoke"])
#expect(
resp.policyDocument.statement[0].resource == [
"arn:aws:execute-api:*:*:*/*/GET/v1/user/0123",
"arn:aws:execute-api:*:*:*/*/POST/v1/user",
]
)
XCTAssertEqual(resp.context?.count, 2)
XCTAssertEqual(resp.context?["abc1"], "xyz1")
#expect(resp.context?.count == 2)
#expect(resp.context?["abc1"] == "xyz1")
}
}
Loading