From 157c9cd52caea6e19a79d2121d55a0355901f26d Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Tue, 17 Jan 2023 17:44:42 +0000 Subject: [PATCH] Fixes after routerbuilder changes (#8) * Fix after router builder changes * Use separate router for web sockets * Use hummingbird branch feature/run-router-first * Use main branch * Increase autoping test timeout * Use codecov@v3 action * Use hummingbird v1.0.0-rc --- .github/workflows/ci.yml | 4 ++-- Package.swift | 4 ++-- Sources/HummingbirdWebSocket/Application+WebSocket.swift | 6 +++--- Sources/HummingbirdWebSocket/Request+WebSocket.swift | 8 -------- Sources/HummingbirdWebSocket/WebSocketRouterGroup.swift | 8 +++----- Tests/HummingbirdWebSocketTests/WebSocketTests.swift | 2 +- 6 files changed, 11 insertions(+), 21 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ad07890..368adcf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,7 +30,7 @@ jobs: -ignore-filename-regex="\/Tests\/" \ -instr-profile=.build/debug/codecov/default.profdata > info.lcov - name: Upload to codecov.io - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v3 with: file: info.lcov linux: @@ -57,6 +57,6 @@ jobs: -ignore-filename-regex="\/Tests\/" \ -instr-profile .build/debug/codecov/default.profdata > info.lcov - name: Upload to codecov.io - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v3 with: file: info.lcov diff --git a/Package.swift b/Package.swift index 052cd47..7270b7f 100644 --- a/Package.swift +++ b/Package.swift @@ -12,8 +12,8 @@ let package = Package( .library(name: "HummingbirdWSCore", targets: ["HummingbirdWSCore"]), ], dependencies: [ - .package(url: "https://github.com/hummingbird-project/hummingbird-core.git", from: "1.0.0-alpha"), - .package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "1.0.0-alpha"), + .package(url: "https://github.com/hummingbird-project/hummingbird-core.git", from: "1.0.0-rc"), + .package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "1.0.0-rc"), .package(url: "https://github.com/apple/swift-nio.git", from: "2.32.1"), .package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.5.0"), .package(url: "https://github.com/swift-extras/swift-extras-base64.git", from: "0.5.0"), diff --git a/Sources/HummingbirdWebSocket/Application+WebSocket.swift b/Sources/HummingbirdWebSocket/Application+WebSocket.swift index 9601a80..c037dd2 100644 --- a/Sources/HummingbirdWebSocket/Application+WebSocket.swift +++ b/Sources/HummingbirdWebSocket/Application+WebSocket.swift @@ -37,7 +37,7 @@ extension HBApplication { ) request.webSocketTestShouldUpgrade = true return responder.respond(to: request).flatMapThrowing { - if $0.webSocketShouldUpgrade == true { + if $0.status == .ok { return $0.headers } throw HBHTTPError(.badRequest) @@ -54,11 +54,11 @@ extension HBApplication { _ = responder.respond(to: request) } ) - self.routerGroup = .init(router: self.application.router) + self.routerGroup = .init(router: HBRouterBuilder()) self.application.lifecycle.register( label: "WebSockets", start: .sync { - self.responder = self.application.middleware.constructResponder(finalResponder: application.router) + self.responder = self.routerGroup.router.buildRouter() }, shutdown: .sync {} ) diff --git a/Sources/HummingbirdWebSocket/Request+WebSocket.swift b/Sources/HummingbirdWebSocket/Request+WebSocket.swift index 747daf0..f9f88be 100644 --- a/Sources/HummingbirdWebSocket/Request+WebSocket.swift +++ b/Sources/HummingbirdWebSocket/Request+WebSocket.swift @@ -28,11 +28,3 @@ extension HBRequest { set { self.extensions.set(\.webSocketTestShouldUpgrade, value: newValue) } } } - -extension HBResponse { - /// Can we upgrade to a web socket connection? - var webSocketShouldUpgrade: Bool? { - get { self.extensions.get(\.webSocketShouldUpgrade) } - set { self.extensions.set(\.webSocketShouldUpgrade, value: newValue) } - } -} diff --git a/Sources/HummingbirdWebSocket/WebSocketRouterGroup.swift b/Sources/HummingbirdWebSocket/WebSocketRouterGroup.swift index cc7347d..a5c8570 100644 --- a/Sources/HummingbirdWebSocket/WebSocketRouterGroup.swift +++ b/Sources/HummingbirdWebSocket/WebSocketRouterGroup.swift @@ -16,10 +16,10 @@ import Hummingbird /// Router Group for adding WebSocket connections to public struct HBWebSocketRouterGroup { - let router: HBRouter + let router: HBRouterBuilder let middlewares: HBMiddlewareGroup - init(router: HBRouter) { + init(router: HBRouterBuilder) { self.router = router self.middlewares = .init() } @@ -46,9 +46,7 @@ public struct HBWebSocketRouterGroup { return request.body.consumeBody(on: request.eventLoop).flatMap { buffer in request.body = .byteBuffer(buffer) return shouldUpgrade(request).map { headers in - var response = HBResponse(status: .ok, headers: headers ?? [:]) - response.webSocketShouldUpgrade = true - return response + return HBResponse(status: .ok, headers: headers ?? [:]) } } } else if let webSocket = request.webSocket { diff --git a/Tests/HummingbirdWebSocketTests/WebSocketTests.swift b/Tests/HummingbirdWebSocketTests/WebSocketTests.swift index f95297e..fe28be6 100644 --- a/Tests/HummingbirdWebSocketTests/WebSocketTests.swift +++ b/Tests/HummingbirdWebSocketTests/WebSocketTests.swift @@ -242,7 +242,7 @@ final class HummingbirdWebSocketTests: XCTestCase { func testAutoPing() throws { let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1) defer { XCTAssertNoThrow(try elg.syncShutdownGracefully()) } - let promise = TimeoutPromise(eventLoop: elg.next(), timeout: .seconds(10)) + let promise = TimeoutPromise(eventLoop: elg.next(), timeout: .seconds(30)) var count = 0 let app = try self.setupClientAndServer(