Skip to content

Commit

Permalink
Add support for supplying additional headers
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Apr 13, 2023
1 parent 5f48b71 commit 01537b4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Sources/HummingbirdWSClient/WebSocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum HBWebSocketClient {
/// - configuration: Configuration of connection
/// - eventLoop: eventLoop to run connection on
/// - Returns: EventLoopFuture which will be fulfilled with `HBWebSocket` once connection is made
public static func connect(url: HBURL, configuration: Configuration, on eventLoop: EventLoop) -> EventLoopFuture<HBWebSocket> {
public static func connect(url: HBURL, headers: HTTPHeaders = [:], configuration: Configuration, on eventLoop: EventLoop) -> EventLoopFuture<HBWebSocket> {
let wsPromise = eventLoop.makePromise(of: HBWebSocket.self)
do {
let url = try SplitURL(url: url)
Expand All @@ -38,7 +38,7 @@ public enum HBWebSocketClient {
.channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
.channelOption(ChannelOptions.socket(IPPROTO_TCP, TCP_NODELAY), value: 1)
.channelInitializer { channel in
return Self.setupChannelForWebsockets(url: url, channel: channel, wsPromise: wsPromise, on: eventLoop)
return Self.setupChannelForWebsockets(url: url, headers: headers, channel: channel, wsPromise: wsPromise, on: eventLoop)
}
.connect(host: url.host, port: url.port)
.cascadeFailure(to: wsPromise)
Expand Down Expand Up @@ -66,6 +66,7 @@ public enum HBWebSocketClient {
/// setup for channel for websocket. Create initial HTTP request and include upgrade for when it is successful
static func setupChannelForWebsockets(
url: SplitURL,
headers: HTTPHeaders,
channel: Channel,
wsPromise: EventLoopPromise<HBWebSocket>,
on eventLoop: EventLoop
Expand All @@ -78,6 +79,7 @@ public enum HBWebSocketClient {
do {
httpHandler = try WebSocketInitialRequestHandler(
url: url,
headers: headers,
upgradePromise: upgradePromise
)
} catch {
Expand Down
26 changes: 26 additions & 0 deletions Tests/HummingbirdWebSocketTests/WebSocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,32 @@ final class HummingbirdWebSocketTests: XCTestCase {
let wsFuture = HBWebSocketClient.connect(url: "ws://localhost:8080/test?connect", configuration: .init(), on: eventLoop)
_ = try wsFuture.wait()
}

func testAdditionalHeaders() throws {
let app = HBApplication(configuration: .init(address: .hostname(port: 8080)))
// add HTTP to WebSocket upgrade
app.ws.addUpgrade()
// on websocket connect.
app.ws.on(
"/test",
shouldUpgrade: { request in
guard request.headers["Sec-WebSocket-Extensions"].first == "foo" else { return request.failure(HBHTTPError(.badRequest)) }
return request.success(nil)
},
onUpgrade: { _, _ in }
)
try app.start()
defer { app.stop() }

let eventLoop = app.eventLoopGroup.next()
let wsFuture = HBWebSocketClient.connect(
url: "ws://localhost:8080/test",
headers: ["Sec-WebSocket-Extensions": "foo"],
configuration: .init(),
on: eventLoop
)
_ = try wsFuture.wait()
}
}

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
Expand Down

0 comments on commit 01537b4

Please sign in to comment.