Skip to content

Commit

Permalink
handle -> onUpgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Mar 22, 2024
1 parent 8f6164f commit 0d0cb03
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Sources/HummingbirdWebSocket/Server/WebSocketRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ 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<Context>
onUpgrade handler: @escaping WebSocketDataHandler<Context>
) -> Self where Context: WebSocketRequestContext {
return on(path, method: .get) { request, context -> Response in
let result = try await shouldUpgrade(request, context)
switch result {
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)
}
}
Expand All @@ -98,7 +98,7 @@ public struct WebSocketUpgradeMiddleware<Context: WebSocketRequestContext>: Rout
/// - handle: WebSocket handler
public init(
shouldUpgrade: @Sendable @escaping (Request, Context) async throws -> RouterShouldUpgrade = { _, _ in .upgrade([:]) },
handler: @escaping WebSocketDataHandler<Context>
onUpgrade handler: @escaping WebSocketDataHandler<Context>
) {
self.shouldUpgrade = shouldUpgrade
self.handler = handler
Expand Down
12 changes: 6 additions & 6 deletions Tests/HummingbirdWebSocketTests/WebSocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) }
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 0d0cb03

Please sign in to comment.