Skip to content

[test] Migrate unit tests to Swift Testing #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.8
// swift-tools-version:6.0

import PackageDescription

Expand Down
26 changes: 26 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// swift-tools-version:5.10

import PackageDescription

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

let package = Package(
name: "swift-aws-lambda-events",
platforms: [.macOS(.v14)],
products: [
.library(name: "AWSLambdaEvents", targets: ["AWSLambdaEvents"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.0.0"),
],
targets: [
.target(
name: "AWSLambdaEvents",
dependencies: [
.product(name: "HTTPTypes", package: "swift-http-types")
],
swiftSettings: swiftSettings
)
]
)
26 changes: 26 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// swift-tools-version:5.8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is standard to just support the last three versions of swift. So you should only need to support 5.10.

Does [email protected] work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Will update to 5.10 before merging.

Yes it works, it is picked up for any 5.x version

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the file name, you must always provide the major version component and you can optionally provide the minor and patch version components for more granular control.


import PackageDescription

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

let package = Package(
name: "swift-aws-lambda-events",
platforms: [.macOS(.v14)],
products: [
.library(name: "AWSLambdaEvents", targets: ["AWSLambdaEvents"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.0.0"),
],
targets: [
.target(
name: "AWSLambdaEvents",
dependencies: [
.product(name: "HTTPTypes", package: "swift-http-types")
],
swiftSettings: swiftSettings
)
]
)
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)
}
}
}
Loading