From b6d8ae5231c8a4b85668f82c98b5c12cfb8cc37b Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Wed, 6 Mar 2024 10:14:14 +0000 Subject: [PATCH] XCT -> Testing --- Package.swift | 4 +-- .../APIGatewayLambda.swift | 2 +- .../APIGatewayV2Lambda.swift | 2 +- .../HBXCTLambda.swift | 12 ++++---- .../Lambda+Testing.swift} | 10 +++---- .../LambdaEvent.swift | 2 +- .../HummingbirdLambdaTests/LambdaTests.swift | 28 +++++++++---------- 7 files changed, 30 insertions(+), 30 deletions(-) rename Sources/{HummingbirdLambdaXCT => HummingbirdLambdaTesting}/APIGatewayLambda.swift (98%) rename Sources/{HummingbirdLambdaXCT => HummingbirdLambdaTesting}/APIGatewayV2Lambda.swift (98%) rename Sources/{HummingbirdLambdaXCT => HummingbirdLambdaTesting}/HBXCTLambda.swift (85%) rename Sources/{HummingbirdLambdaXCT/Lambda+XCT.swift => HummingbirdLambdaTesting/Lambda+Testing.swift} (80%) rename Sources/{HummingbirdLambdaXCT => HummingbirdLambdaTesting}/LambdaEvent.swift (94%) diff --git a/Package.swift b/Package.swift index 96978c0..d9ed4a6 100644 --- a/Package.swift +++ b/Package.swift @@ -25,7 +25,7 @@ let package = Package( .product(name: "ExtrasBase64", package: "swift-extras-base64"), .product(name: "Hummingbird", package: "hummingbird"), ]), - .target(name: "HummingbirdLambdaXCT", dependencies: [ + .target(name: "HummingbirdLambdaTesting", dependencies: [ .byName(name: "HummingbirdLambda"), ]), .executableTarget(name: "HBLambdaTest", dependencies: [ @@ -33,7 +33,7 @@ let package = Package( ]), .testTarget(name: "HummingbirdLambdaTests", dependencies: [ .byName(name: "HummingbirdLambda"), - .byName(name: "HummingbirdLambdaXCT"), + .byName(name: "HummingbirdLambdaTesting"), .product(name: "NIOPosix", package: "swift-nio"), ]), ] diff --git a/Sources/HummingbirdLambdaXCT/APIGatewayLambda.swift b/Sources/HummingbirdLambdaTesting/APIGatewayLambda.swift similarity index 98% rename from Sources/HummingbirdLambdaXCT/APIGatewayLambda.swift rename to Sources/HummingbirdLambdaTesting/APIGatewayLambda.swift index ea9dc69..fef033d 100644 --- a/Sources/HummingbirdLambdaXCT/APIGatewayLambda.swift +++ b/Sources/HummingbirdLambdaTesting/APIGatewayLambda.swift @@ -18,7 +18,7 @@ import HTTPTypes import HummingbirdCore import NIOCore -extension APIGatewayRequest: XCTLambdaEvent { +extension APIGatewayRequest: LambdaTestableEvent { /// Construct APIGateway Event from uri, method, headers and body public init(uri: String, method: HTTPRequest.Method, headers: HTTPFields, body: ByteBuffer?) throws { let base64Body = body.map { "\"\(String(base64Encoding: $0.readableBytesView))\"" } ?? "null" diff --git a/Sources/HummingbirdLambdaXCT/APIGatewayV2Lambda.swift b/Sources/HummingbirdLambdaTesting/APIGatewayV2Lambda.swift similarity index 98% rename from Sources/HummingbirdLambdaXCT/APIGatewayV2Lambda.swift rename to Sources/HummingbirdLambdaTesting/APIGatewayV2Lambda.swift index fccb980..4436d36 100644 --- a/Sources/HummingbirdLambdaXCT/APIGatewayV2Lambda.swift +++ b/Sources/HummingbirdLambdaTesting/APIGatewayV2Lambda.swift @@ -18,7 +18,7 @@ import HTTPTypes import HummingbirdCore import NIOCore -extension APIGatewayV2Request: XCTLambdaEvent { +extension APIGatewayV2Request: LambdaTestableEvent { /// Construct APIGatewayV2 Event from uri, method, headers and body public init(uri: String, method: HTTPRequest.Method, headers: HTTPFields, body: ByteBuffer?) throws { let base64Body = body.map { "\"\(String(base64Encoding: $0.readableBytesView))\"" } ?? "null" diff --git a/Sources/HummingbirdLambdaXCT/HBXCTLambda.swift b/Sources/HummingbirdLambdaTesting/HBXCTLambda.swift similarity index 85% rename from Sources/HummingbirdLambdaXCT/HBXCTLambda.swift rename to Sources/HummingbirdLambdaTesting/HBXCTLambda.swift index 6f16e36..f82871f 100644 --- a/Sources/HummingbirdLambdaXCT/HBXCTLambda.swift +++ b/Sources/HummingbirdLambdaTesting/HBXCTLambda.swift @@ -21,12 +21,12 @@ import Logging import NIOCore import NIOPosix -class HBXCTLambda where Lambda.Event: XCTLambdaEvent { +class HBLambdaTestFramework where Lambda.Event: LambdaTestableEvent { let context: LambdaContext var terminator: LambdaTerminator init(logLevel: Logger.Level) { - var logger = Logger(label: "HBXCTLambda") + var logger = Logger(label: "HBTestLambda") logger.logLevel = logLevel self.context = .init( requestID: UUID().uuidString, @@ -51,9 +51,9 @@ class HBXCTLambda where Lambda.Event: XCTLambdaEvent { ) } - func run(_ test: @escaping @Sendable (HBXCTLambdaClient) async throws -> Value) async throws -> Value { + func run(_ test: @escaping @Sendable (HBLambdaTestClient) async throws -> Value) async throws -> Value { let handler = try await HBLambdaHandler(context: self.initializationContext) - let value = try await test(HBXCTLambdaClient(handler: handler, context: context)) + let value = try await test(HBLambdaTestClient(handler: handler, context: context)) try await self.terminator.terminate(eventLoop: self.context.eventLoop).get() self.terminator = .init() return value @@ -61,7 +61,7 @@ class HBXCTLambda where Lambda.Event: XCTLambdaEvent { } /// Client used to send requests to lambda test framework -public struct HBXCTLambdaClient where Lambda.Event: XCTLambdaEvent { +public struct HBLambdaTestClient where Lambda.Event: LambdaTestableEvent { let handler: HBLambdaHandler let context: LambdaContext @@ -79,7 +79,7 @@ public struct HBXCTLambdaClient where Lambda.Event: XCTLambdaE /// - body: Request body /// - testCallback: closure to call on response returned by test framework /// - Returns: Return value of test closure - @discardableResult public func XCTExecute( + @discardableResult public func execute( uri: String, method: HTTPRequest.Method, headers: HTTPFields = [:], diff --git a/Sources/HummingbirdLambdaXCT/Lambda+XCT.swift b/Sources/HummingbirdLambdaTesting/Lambda+Testing.swift similarity index 80% rename from Sources/HummingbirdLambdaXCT/Lambda+XCT.swift rename to Sources/HummingbirdLambdaTesting/Lambda+Testing.swift index c8f918e..427a994 100644 --- a/Sources/HummingbirdLambdaXCT/Lambda+XCT.swift +++ b/Sources/HummingbirdLambdaTesting/Lambda+Testing.swift @@ -15,11 +15,11 @@ import HummingbirdLambda import Logging -extension HBLambda where Event: XCTLambdaEvent { +extension HBLambda where Event: LambdaTestableEvent { /// Test `HBLambda` /// /// The `test` closure uses the provided test client to make calls to the - /// lambda via `XCTExecute`. You can verify the contents of the output + /// lambda via `execute`. You can verify the contents of the output /// event returned. /// /// The example below is using the `.router` framework to test @@ -36,16 +36,16 @@ extension HBLambda where Event: XCTLambdaEvent { /// } /// } /// try await HelloLambda.test { client in - /// try await client.XCTExecute(uri: "/hello", method: .get) { response in + /// try await client.execute(uri: "/hello", method: .get) { response in /// XCTAssertEqual(response.body, "Hello") /// } /// } /// ``` public static func test( logLevel: Logger.Level = .debug, - _ test: @escaping @Sendable (HBXCTLambdaClient) async throws -> Value + _ test: @escaping @Sendable (HBLambdaTestClient) async throws -> Value ) async throws -> Value { - let lambda = HBXCTLambda(logLevel: logLevel) + let lambda = HBLambdaTestFramework(logLevel: logLevel) return try await lambda.run(test) } } diff --git a/Sources/HummingbirdLambdaXCT/LambdaEvent.swift b/Sources/HummingbirdLambdaTesting/LambdaEvent.swift similarity index 94% rename from Sources/HummingbirdLambdaXCT/LambdaEvent.swift rename to Sources/HummingbirdLambdaTesting/LambdaEvent.swift index cf914fe..560de1c 100644 --- a/Sources/HummingbirdLambdaXCT/LambdaEvent.swift +++ b/Sources/HummingbirdLambdaTesting/LambdaEvent.swift @@ -16,6 +16,6 @@ import Foundation import HTTPTypes import NIOCore -public protocol XCTLambdaEvent { +public protocol LambdaTestableEvent { init(uri: String, method: HTTPRequest.Method, headers: HTTPFields, body: ByteBuffer?) throws } diff --git a/Tests/HummingbirdLambdaTests/LambdaTests.swift b/Tests/HummingbirdLambdaTests/LambdaTests.swift index ec16309..2aa1d00 100644 --- a/Tests/HummingbirdLambdaTests/LambdaTests.swift +++ b/Tests/HummingbirdLambdaTests/LambdaTests.swift @@ -15,7 +15,7 @@ import AWSLambdaEvents @testable import AWSLambdaRuntimeCore @testable import HummingbirdLambda -import HummingbirdLambdaXCT +import HummingbirdLambdaTesting import Logging import NIOCore import NIOPosix @@ -37,7 +37,7 @@ final class LambdaTests: XCTestCase { } } try await HelloLambda.test { client in - try await client.XCTExecute(uri: "/hello", method: .get) { response in + try await client.execute(uri: "/hello", method: .get) { response in XCTAssertEqual(response.body, "Hello") XCTAssertEqual(response.statusCode, .ok) XCTAssertEqual(response.headers?["Content-Type"], "text/plain; charset=utf-8") @@ -60,7 +60,7 @@ final class LambdaTests: XCTestCase { } try await HelloLambda.test { client in let body = ByteBuffer(bytes: (0...255).map { _ in UInt8.random(in: 0...255) }) - try await client.XCTExecute(uri: "/", method: .post, body: body) { response in + try await client.execute(uri: "/", method: .post, body: body) { response in XCTAssertEqual(response.isBase64Encoded, true) XCTAssertEqual(response.body, String(base64Encoding: body.readableBytesView)) } @@ -88,12 +88,12 @@ final class LambdaTests: XCTestCase { } } try await HelloLambda.test { client in - try await client.XCTExecute(uri: "/", method: .post, headers: [.userAgent: "HBXCT/2.0", .acceptLanguage: "en"]) { response in + try await client.execute(uri: "/", method: .post, headers: [.userAgent: "HBXCT/2.0", .acceptLanguage: "en"]) { response in XCTAssertEqual(response.statusCode, .ok) } var headers: HTTPFields = [.userAgent: "HBXCT/2.0", .acceptLanguage: "en"] headers[values: .acceptLanguage].append("fr") - try await client.XCTExecute(uri: "/multi", method: .post, headers: headers) { response in + try await client.execute(uri: "/multi", method: .post, headers: headers) { response in XCTAssertEqual(response.statusCode, .ok) } } @@ -118,10 +118,10 @@ final class LambdaTests: XCTestCase { } } try await HelloLambda.test { client in - try await client.XCTExecute(uri: "/?foo=bar", method: .post) { response in + try await client.execute(uri: "/?foo=bar", method: .post) { response in XCTAssertEqual(response.statusCode, .ok) } - try await client.XCTExecute(uri: "/multi?foo=bar1&foo=bar2", method: .post) { response in + try await client.execute(uri: "/multi?foo=bar1&foo=bar2", method: .post) { response in XCTAssertEqual(response.statusCode, .ok) } } @@ -142,7 +142,7 @@ final class LambdaTests: XCTestCase { } } try await HelloLambda.test { client in - try await client.XCTExecute(uri: "/", method: .post) { response in + try await client.execute(uri: "/", method: .post) { response in XCTAssertEqual(response.statusCode, .badRequest) XCTAssertEqual(response.body, HelloLambda.body) XCTAssertEqual(response.headers?["Content-Length"], HelloLambda.body.utf8.count.description) @@ -170,7 +170,7 @@ final class LambdaTests: XCTestCase { } } try await HelloLambda.test { client in - try await client.XCTExecute(uri: "/", method: .post) { response in + try await client.execute(uri: "/", method: .post) { response in XCTAssertEqual(response.statusCode, .ok) XCTAssertEqual(response.headers?["Content-Type"], "application/json; charset=utf-8") XCTAssertEqual(response.body, #"{"response":"hello"}"#) @@ -193,7 +193,7 @@ final class LambdaTests: XCTestCase { } try await HelloLambda.test { client in let body = ByteBuffer(bytes: (0...255).map { _ in UInt8.random(in: 0...255) }) - try await client.XCTExecute(uri: "/", method: .post, headers: [.userAgent: "HBXCT/2.0"], body: body) { response in + try await client.execute(uri: "/", method: .post, headers: [.userAgent: "HBXCT/2.0"], body: body) { response in XCTAssertEqual(response.isBase64Encoded, true) XCTAssertEqual(response.body, String(base64Encoding: body.readableBytesView)) } @@ -221,12 +221,12 @@ final class LambdaTests: XCTestCase { } } try await HelloLambda.test { client in - try await client.XCTExecute(uri: "/", method: .post, headers: [.userAgent: "HBXCT/2.0", .acceptLanguage: "en"]) { response in + try await client.execute(uri: "/", method: .post, headers: [.userAgent: "HBXCT/2.0", .acceptLanguage: "en"]) { response in XCTAssertEqual(response.statusCode, .ok) } var headers: HTTPFields = [.userAgent: "HBXCT/2.0", .acceptLanguage: "en"] headers[values: .acceptLanguage].append("fr") - try await client.XCTExecute(uri: "/multi", method: .post, headers: headers) { response in + try await client.execute(uri: "/multi", method: .post, headers: headers) { response in XCTAssertEqual(response.statusCode, .ok) } } @@ -251,10 +251,10 @@ final class LambdaTests: XCTestCase { } } try await HelloLambda.test { client in - try await client.XCTExecute(uri: "/?foo=bar", method: .post) { response in + try await client.execute(uri: "/?foo=bar", method: .post) { response in XCTAssertEqual(response.statusCode, .ok) } - try await client.XCTExecute(uri: "/multi?foo=bar1&foo=bar2", method: .post) { response in + try await client.execute(uri: "/multi?foo=bar1&foo=bar2", method: .post) { response in XCTAssertEqual(response.statusCode, .ok) } }