From 0d0cb03f00b74c1fe2a4301e791315f4497156e8 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Fri, 22 Mar 2024 10:24:26 +0000 Subject: [PATCH] handle -> onUpgrade --- .../Server/WebSocketRouter.swift | 8 ++++---- Tests/HummingbirdWebSocketTests/WebSocketTests.swift | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/HummingbirdWebSocket/Server/WebSocketRouter.swift b/Sources/HummingbirdWebSocket/Server/WebSocketRouter.swift index 2c60583..54e1570 100644 --- a/Sources/HummingbirdWebSocket/Server/WebSocketRouter.swift +++ b/Sources/HummingbirdWebSocket/Server/WebSocketRouter.swift @@ -65,11 +65,11 @@ extension RouterMethods { /// - Parameters: /// - path: Path to match /// - shouldUpgrade: Should request be upgraded - /// - handle: WebSocket channel handler + /// - handler: WebSocket channel handler @discardableResult public func ws( _ path: String = "", shouldUpgrade: @Sendable @escaping (Request, Context) async throws -> RouterShouldUpgrade = { _, _ in .upgrade([:]) }, - handle: @escaping WebSocketDataHandler + onUpgrade handler: @escaping WebSocketDataHandler ) -> Self where Context: WebSocketRequestContext { return on(path, method: .get) { request, context -> Response in let result = try await shouldUpgrade(request, context) @@ -77,7 +77,7 @@ extension RouterMethods { case .dontUpgrade: return .init(status: .methodNotAllowed) case .upgrade(let headers): - context.webSocket.handler.withLockedValue { $0 = WebSocketRouterContext.Value(context: context, handler: handle) } + context.webSocket.handler.withLockedValue { $0 = WebSocketRouterContext.Value(context: context, handler: handler) } return .init(status: .ok, headers: headers) } } @@ -98,7 +98,7 @@ public struct WebSocketUpgradeMiddleware: Rout /// - handle: WebSocket handler public init( shouldUpgrade: @Sendable @escaping (Request, Context) async throws -> RouterShouldUpgrade = { _, _ in .upgrade([:]) }, - handler: @escaping WebSocketDataHandler + onUpgrade handler: @escaping WebSocketDataHandler ) { self.shouldUpgrade = shouldUpgrade self.handler = handler diff --git a/Tests/HummingbirdWebSocketTests/WebSocketTests.swift b/Tests/HummingbirdWebSocketTests/WebSocketTests.swift index 6de603d..b7666bf 100644 --- a/Tests/HummingbirdWebSocketTests/WebSocketTests.swift +++ b/Tests/HummingbirdWebSocketTests/WebSocketTests.swift @@ -393,12 +393,12 @@ final class HummingbirdWebSocketTests: XCTestCase { let router = Router(context: BasicWebSocketRequestContext.self) router.ws("/ws1") { _, _ in return .upgrade([:]) - } handle: { _, outbound, _ in + } onUpgrade: { _, outbound, _ in try await outbound.write(.text("One")) } router.ws("/ws2") { _, _ in return .upgrade([:]) - } handle: { _, outbound, _ in + } onUpgrade: { _, outbound, _ in try await outbound.write(.text("Two")) } try await self.testClientAndServerWithRouter(webSocketRouter: router, uri: "localhost:8080") { port, logger in @@ -422,7 +422,7 @@ final class HummingbirdWebSocketTests: XCTestCase { router.group("/ws") .add(middleware: WebSocketUpgradeMiddleware { _, _ in return .upgrade([:]) - } handler: { _, outbound, _ in + } onUpgrade: { _, outbound, _ in try await outbound.write(.text("One")) }) .get { _, _ -> Response in return .init(status: .ok) } @@ -437,7 +437,7 @@ final class HummingbirdWebSocketTests: XCTestCase { let router = Router(context: BasicWebSocketRequestContext.self) router.ws("/ws") { _, _ in return .upgrade([:]) - } handle: { _, outbound, _ in + } onUpgrade: { _, outbound, _ in try await outbound.write(.text("One")) } do { @@ -471,7 +471,7 @@ final class HummingbirdWebSocketTests: XCTestCase { router.middlewares.add(MyMiddleware()) router.ws("/ws") { _, _ in return .upgrade([:]) - } handle: { _, outbound, context in + } onUpgrade: { _, outbound, context in try await outbound.write(.text(context.name)) } do { @@ -488,7 +488,7 @@ final class HummingbirdWebSocketTests: XCTestCase { let router = Router(context: BasicWebSocketRequestContext.self) router.ws("/ws") { _, _ in return .upgrade([:]) - } handle: { _, outbound, _ in + } onUpgrade: { _, outbound, _ in try await outbound.write(.text("Hello")) } router.get("/http") { _, _ in