Skip to content

Commit

Permalink
WebSocket testing support (#70)
Browse files Browse the repository at this point in the history
* Add TestClientProtocol.ws

* Use AHC for tests, live is currently causing the tests to hang

* Use HummingbirdWSTesting in tests

* Fix TLS test

* Remove unused code

* Use testing framework in extension tests
  • Loading branch information
adam-fowler authored Jul 8, 2024
1 parent f6f7357 commit 356b319
Show file tree
Hide file tree
Showing 4 changed files with 556 additions and 500 deletions.
8 changes: 7 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ let package = Package(
.library(name: "HummingbirdWebSocket", targets: ["HummingbirdWebSocket"]),
.library(name: "HummingbirdWSClient", targets: ["HummingbirdWSClient"]),
.library(name: "HummingbirdWSCompression", targets: ["HummingbirdWSCompression"]),
.library(name: "HummingbirdWSTesting", targets: ["HummingbirdWSTesting"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-rc.1"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-rc.2"),
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.62.0"),
Expand Down Expand Up @@ -50,10 +51,15 @@ let package = Package(
.byName(name: "HummingbirdWSCore"),
.product(name: "CompressNIO", package: "compress-nio"),
]),
.target(name: "HummingbirdWSTesting", dependencies: [
.byName(name: "HummingbirdWSClient"),
.product(name: "HummingbirdTesting", package: "hummingbird"),
]),
.testTarget(name: "HummingbirdWebSocketTests", dependencies: [
.byName(name: "HummingbirdWebSocket"),
.byName(name: "HummingbirdWSClient"),
.byName(name: "HummingbirdWSCompression"),
.byName(name: "HummingbirdWSTesting"),
.product(name: "Hummingbird", package: "hummingbird"),
.product(name: "HummingbirdTesting", package: "hummingbird"),
.product(name: "HummingbirdTLS", package: "hummingbird"),
Expand Down
44 changes: 44 additions & 0 deletions Sources/HummingbirdWSTesting/TestClient+ws.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2023-2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import HummingbirdTesting
import HummingbirdWSClient
import Logging
import NIOSSL

extension TestClientProtocol {
/// Test WebSocket endpoint
/// - Parameters:
/// - path: Endpoint path
/// - configuration: WebSocket client configuration
/// - logger: Logger
/// - handler: WebSocket handler
/// - Returns: WebSocket close frame
@discardableResult public func ws(
_ path: String,
configuration: WebSocketClientConfiguration = .init(),
logger: Logger = Logger(label: "TestClient"),
handler: @escaping WebSocketDataHandler<WebSocketClient.Context>
) async throws -> WebSocketCloseFrame? {
guard let port else {
preconditionFailure("Cannot test WebSockets without a live server. Use `.live` or `.ahc` to test WebSockets")
}
return try await WebSocketClient.connect(
url: "ws://localhost:\(port)\(path)",
configuration: configuration,
logger: logger,
handler: handler
)
}
}
Loading

0 comments on commit 356b319

Please sign in to comment.