Skip to content

Commit

Permalink
Add testRouterContextUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Mar 20, 2024
1 parent 2719f8e commit 8f6164f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Tests/HummingbirdWebSocketTests/WebSocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,43 @@ final class HummingbirdWebSocketTests: XCTestCase {
} catch let error as WebSocketClientError where error == .webSocketUpgradeFailed {}
}

/// Test context from router is passed through to web socket
func testRouterContextUpdate() async throws {
struct MyRequestContext: WebSocketRequestContext {
var coreContext: CoreRequestContext
var webSocket: WebSocketRouterContext<MyRequestContext>
var name: String

init(channel: Channel, logger: Logger) {
self.coreContext = .init(allocator: channel.allocator, logger: logger)
self.webSocket = .init()
self.name = ""
}
}
struct MyMiddleware: RouterMiddleware {
func handle(_ request: Request, context: MyRequestContext, next: (Request, MyRequestContext) async throws -> Response) async throws -> Response {
var context = context
context.name = "Roger Moore"
return try await next(request, context)
}
}
let router = Router(context: MyRequestContext.self)
router.middlewares.add(MyMiddleware())
router.ws("/ws") { _, _ in
return .upgrade([:])
} handle: { _, outbound, context in
try await outbound.write(.text(context.name))
}
do {
try await self.testClientAndServerWithRouter(webSocketRouter: router, uri: "localhost:8080") { port, logger in
try WebSocketClient(url: .init("ws://localhost:\(port)/ws"), logger: logger) { inbound, _, _ in
let text = await inbound.first { _ in true }
XCTAssertEqual(text, .text("Roger Moore"))
}
}
} catch let error as WebSocketClientError where error == .webSocketUpgradeFailed {}
}

func testHTTPRequest() async throws {
let router = Router(context: BasicWebSocketRequestContext.self)
router.ws("/ws") { _, _ in
Expand Down

0 comments on commit 8f6164f

Please sign in to comment.