Skip to content

Commit

Permalink
XCT -> Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Mar 6, 2024
1 parent 95e9ece commit b6d8ae5
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Expand Up @@ -25,15 +25,15 @@ 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: [
.byName(name: "HummingbirdLambda"),
]),
.testTarget(name: "HummingbirdLambdaTests", dependencies: [
.byName(name: "HummingbirdLambda"),
.byName(name: "HummingbirdLambdaXCT"),
.byName(name: "HummingbirdLambdaTesting"),
.product(name: "NIOPosix", package: "swift-nio"),
]),
]
Expand Down
Expand Up @@ -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"
Expand Down
Expand Up @@ -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"
Expand Down
Expand Up @@ -21,12 +21,12 @@ import Logging
import NIOCore
import NIOPosix

class HBXCTLambda<Lambda: HBLambda> where Lambda.Event: XCTLambdaEvent {
class HBLambdaTestFramework<Lambda: HBLambda> 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,
Expand All @@ -51,17 +51,17 @@ class HBXCTLambda<Lambda: HBLambda> where Lambda.Event: XCTLambdaEvent {
)
}

func run<Value>(_ test: @escaping @Sendable (HBXCTLambdaClient<Lambda>) async throws -> Value) async throws -> Value {
func run<Value>(_ test: @escaping @Sendable (HBLambdaTestClient<Lambda>) async throws -> Value) async throws -> Value {
let handler = try await HBLambdaHandler<Lambda>(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
}
}

/// Client used to send requests to lambda test framework
public struct HBXCTLambdaClient<Lambda: HBLambda> where Lambda.Event: XCTLambdaEvent {
public struct HBLambdaTestClient<Lambda: HBLambda> where Lambda.Event: LambdaTestableEvent {
let handler: HBLambdaHandler<Lambda>
let context: LambdaContext

Expand All @@ -79,7 +79,7 @@ public struct HBXCTLambdaClient<Lambda: HBLambda> 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<Return>(
@discardableResult public func execute<Return>(
uri: String,
method: HTTPRequest.Method,
headers: HTTPFields = [:],
Expand Down
Expand Up @@ -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
Expand All @@ -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<Value>(
logLevel: Logger.Level = .debug,
_ test: @escaping @Sendable (HBXCTLambdaClient<Self>) async throws -> Value
_ test: @escaping @Sendable (HBLambdaTestClient<Self>) async throws -> Value
) async throws -> Value {
let lambda = HBXCTLambda<Self>(logLevel: logLevel)
let lambda = HBLambdaTestFramework<Self>(logLevel: logLevel)
return try await lambda.run(test)
}
}
Expand Up @@ -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
}
28 changes: 14 additions & 14 deletions Tests/HummingbirdLambdaTests/LambdaTests.swift
Expand Up @@ -15,7 +15,7 @@
import AWSLambdaEvents
@testable import AWSLambdaRuntimeCore
@testable import HummingbirdLambda
import HummingbirdLambdaXCT
import HummingbirdLambdaTesting
import Logging
import NIOCore
import NIOPosix
Expand All @@ -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")
Expand All @@ -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))
}
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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"}"#)
Expand All @@ -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))
}
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}
Expand Down

0 comments on commit b6d8ae5

Please sign in to comment.