Skip to content

Commit

Permalink
Default HTTPFields to [:] in upgrade case (#52)
Browse files Browse the repository at this point in the history
* Default HTTPFields to [:] in upgrade case

* Swift format
  • Loading branch information
adam-fowler authored Apr 6, 2024
1 parent bff34ca commit 496cad4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import NIOWebSocket
/// Should HTTP channel upgrade to WebSocket
public enum ShouldUpgradeResult<Value: Sendable>: Sendable {
case dontUpgrade
case upgrade(HTTPFields, Value)
case upgrade(HTTPFields = [:], Value)

/// Map upgrade result to difference type
func map<Result>(_ map: (HTTPFields, Value) throws -> (HTTPFields, Result)) rethrows -> ShouldUpgradeResult<Result> {
Expand Down
2 changes: 1 addition & 1 deletion Sources/HummingbirdWebSocket/Server/WebSocketRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public struct BasicWebSocketRequestContext: WebSocketRequestContext {
/// WebSocket upgrade or not
public enum RouterShouldUpgrade: Sendable {
case dontUpgrade
case upgrade(HTTPFields)
case upgrade(HTTPFields = [:])
}

extension RouterMethods {
Expand Down
16 changes: 8 additions & 8 deletions Tests/HummingbirdWebSocketTests/WebSocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ final class HummingbirdWebSocketTests: XCTestCase {
let app = Application(
router: router,
server: .http1WebSocketUpgrade { _, _, _ in
return .upgrade([:]) { _, outbound, _ in
return .upgrade { _, outbound, _ in
try await outbound.write(.text("Hello"))
}
},
Expand Down Expand Up @@ -367,12 +367,12 @@ final class HummingbirdWebSocketTests: XCTestCase {
func testRouteSelection() async throws {
let router = Router(context: BasicWebSocketRequestContext.self)
router.ws("/ws1") { _, _ in
return .upgrade([:])
return .upgrade()
} onUpgrade: { _, outbound, _ in
try await outbound.write(.text("One"))
}
router.ws("/ws2") { _, _ in
return .upgrade([:])
return .upgrade()
} onUpgrade: { _, outbound, _ in
try await outbound.write(.text("Two"))
}
Expand All @@ -396,7 +396,7 @@ final class HummingbirdWebSocketTests: XCTestCase {
let router = Router(context: BasicWebSocketRequestContext.self)
router.ws("/ws") { request, _ in
guard request.uri.queryParameters["test"] != nil else { return .dontUpgrade }
return .upgrade([:])
return .upgrade()
} onUpgrade: { _, outbound, context in
guard let test = context.request.uri.queryParameters["test"] else { return }
try await outbound.write(.text(String(test)))
Expand All @@ -414,7 +414,7 @@ final class HummingbirdWebSocketTests: XCTestCase {
let router = Router(context: BasicWebSocketRequestContext.self)
router.group("/ws")
.add(middleware: WebSocketUpgradeMiddleware { _, _ in
return .upgrade([:])
return .upgrade()
} onUpgrade: { _, outbound, _ in
try await outbound.write(.text("One"))
})
Expand All @@ -429,7 +429,7 @@ final class HummingbirdWebSocketTests: XCTestCase {
func testRouteSelectionFail() async throws {
let router = Router(context: BasicWebSocketRequestContext.self)
router.ws("/ws") { _, _ in
return .upgrade([:])
return .upgrade()
} onUpgrade: { _, outbound, _ in
try await outbound.write(.text("One"))
}
Expand Down Expand Up @@ -463,7 +463,7 @@ final class HummingbirdWebSocketTests: XCTestCase {
let router = Router(context: MyRequestContext.self)
router.middlewares.add(MyMiddleware())
router.ws("/ws") { _, _ in
return .upgrade([:])
return .upgrade()
} onUpgrade: { _, outbound, context in
try await outbound.write(.text(context.requestContext.name))
}
Expand All @@ -480,7 +480,7 @@ final class HummingbirdWebSocketTests: XCTestCase {
func testHTTPRequest() async throws {
let router = Router(context: BasicWebSocketRequestContext.self)
router.ws("/ws") { _, _ in
return .upgrade([:])
return .upgrade()
} onUpgrade: { _, outbound, _ in
try await outbound.write(.text("Hello"))
}
Expand Down

0 comments on commit 496cad4

Please sign in to comment.