Skip to content

Commit

Permalink
Add AHC testing client
Browse files Browse the repository at this point in the history
Also merge HummingbirdCoreXCT with HummingbirdXCT
  • Loading branch information
adam-fowler committed Dec 20, 2023
1 parent c3e102b commit 2e8de97
Show file tree
Hide file tree
Showing 14 changed files with 647 additions and 115 deletions.
9 changes: 1 addition & 8 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,9 @@ let package = Package(
]),
.target(name: "HummingbirdXCT", dependencies: [
.byName(name: "Hummingbird"),
.byName(name: "HummingbirdCoreXCT"),
.product(name: "HTTPTypes", package: "swift-http-types"),
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOPosix", package: "swift-nio"),
]),
.target(name: "HummingbirdCoreXCT", dependencies: [
.product(name: "HTTPTypes", package: "swift-http-types"),
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOConcurrencyHelpers", package: "swift-nio"),
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "NIOHTTPTypes", package: "swift-nio-extras"),
.product(name: "NIOHTTPTypesHTTP1", package: "swift-nio-extras"),
.product(name: "NIOPosix", package: "swift-nio"),
Expand Down Expand Up @@ -124,7 +117,7 @@ let package = Package(
.byName(name: "HummingbirdCore"),
.byName(name: "HummingbirdHTTP2"),
.byName(name: "HummingbirdTLS"),
.byName(name: "HummingbirdCoreXCT"),
.byName(name: "HummingbirdXCT"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
],
resources: [.process("Certificates")]
Expand Down
62 changes: 35 additions & 27 deletions Sources/HummingbirdXCT/Application+XCT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,36 @@ import Hummingbird
import HummingbirdCore
import NIOCore

/// Type of test framework
public struct XCTLiveTestingSetup {
/// Sets up a live server and execute tests using a HTTP client.
public static let live = XCTLiveTestingSetup()
public enum XCTScheme: String {
case http
case https
}

public struct XCTRouterTestingSetup {
/// Type of test framework
public struct XCTTestingSetup {
enum Internal {
case router
case live
case ahc(XCTScheme)
}

let value: Internal

/// Test writing requests directly to router.
public static let router = XCTRouterTestingSetup()
public static var router: XCTTestingSetup { .init(value: .router) }
/// Sets up a live server and execute tests using a HTTP client.
public static var live: XCTTestingSetup { .init(value: .live) }
/// Sets up a live server and execute tests using a HTTP client.
public static func ahc(_ scheme: XCTScheme) -> XCTTestingSetup { .init(value: .ahc(scheme)) }

static func ~= (lhs: XCTTestingSetup, rhs: XCTTestingSetup) -> Bool {
switch (lhs.value, rhs.value) {
case (.router, .router): true
case (.live, .live): true
case (.ahc(let scheme1), .ahc(let scheme2)): scheme1 == scheme2
default: false
}
}
}

/// Extends `HBApplication` to support testing of applications
Expand Down Expand Up @@ -58,29 +79,16 @@ extension HBApplicationProtocol where Responder.Context: HBRequestContext {
/// - testing: indicates which type of testing framework we want
/// - configuration: configuration of application
public func test<Value>(
_: XCTLiveTestingSetup,
_ test: @escaping @Sendable (any HBXCTClientProtocol) async throws -> Value
) async throws -> Value {
let app: any HBXCTApplication
app = HBXCTLive(app: self)
return try await app.run(test)
}
}

extension HBApplicationProtocol where Responder.Context: HBBaseRequestContext {
// MARK: Initialization

/// Creates a version of `HBApplication` that can be used for testing code
///
/// - Parameters:
/// - testing: indicates which type of testing framework we want
/// - configuration: configuration of application
public func test<Value>(
_: XCTRouterTestingSetup,
_ testingSetup: XCTTestingSetup,
_ test: @escaping @Sendable (any HBXCTClientProtocol) async throws -> Value
) async throws -> Value {
let app: any HBXCTApplication
app = try await HBXCTRouter(app: self)
let app: any HBXCTApplication = switch testingSetup {
case .router: try await HBXCTRouter(app: self)
case .live: HBXCTLive(app: self)
case .ahc(.http): HBXCTAsyncHTTPClient(app: self, scheme: .http)
case .ahc(.https): HBXCTAsyncHTTPClient(app: self, scheme: .https)
default: preconditionFailure("XCTTestingSetup not supported")
}
return try await app.run(test)
}
}
Loading

0 comments on commit 2e8de97

Please sign in to comment.