Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions Sources/WebSocketKit/WebSocket+Connect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ extension WebSocket {
on eventLoopGroup: EventLoopGroup,
onUpgrade: @Sendable @escaping (WebSocket) -> ()
) -> EventLoopFuture<Void> {
#if os(iOS) || os(tvOS) || os(watchOS) || os(macOS)
let optionalURL: URL?
if #available(iOS 17.0, macOS 14.3, tvOS 17.0, watchOS 10.0, *) {
optionalURL = URL(string: url, encodingInvalidCharacters: false)
} else {
optionalURL = URL(string: url)
}
guard let url = optionalURL else {
return eventLoopGroup.any().makeFailedFuture(WebSocketClient.Error.invalidURL)
}

return self.connect(
to: url,
headers: headers,
configuration: configuration,
on: eventLoopGroup,
onUpgrade: onUpgrade
)
#else
guard let url = URL(string: url) else {
return eventLoopGroup.any().makeFailedFuture(WebSocketClient.Error.invalidURL)
}
Expand All @@ -30,8 +49,9 @@ extension WebSocket {
on: eventLoopGroup,
onUpgrade: onUpgrade
)
#endif
}

/// Establish a WebSocket connection.
///
/// - Parameters:
Expand Down Expand Up @@ -62,7 +82,7 @@ extension WebSocket {
onUpgrade: onUpgrade
)
}

/// Establish a WebSocket connection.
///
/// - Parameters:
Expand Down Expand Up @@ -101,7 +121,7 @@ extension WebSocket {
onUpgrade: onUpgrade
)
}

/// Establish a WebSocket connection via a proxy server.
///
/// - Parameters:
Expand Down Expand Up @@ -152,8 +172,8 @@ extension WebSocket {
onUpgrade: onUpgrade
)
}


/// Description
/// - Parameters:
/// - url: URL for the origin server.
Expand Down
8 changes: 4 additions & 4 deletions Tests/WebSocketKitTests/AsyncWebSocketKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ final class AsyncWebSocketKitTests: XCTestCase {
return XCTFail("couldn't get port from \(String(reflecting: server.localAddress))")
}
try await WebSocket.connect(to: "ws://localhost:\(port)", on: self.elg) { (ws) async in
ws.onPong {
ws.onPong { webSocket, _ in
do {
try await $0.close()
try await webSocket.close()
} catch {
XCTFail("Failed to close websocket: \(String(reflecting: error))")
}
Expand All @@ -118,8 +118,8 @@ final class AsyncWebSocketKitTests: XCTestCase {
}
try await WebSocket.connect(to: "ws://localhost:\(port)", on: self.elg) { (ws) async in
ws.pingInterval = .milliseconds(100)
ws.onPong {
do { try await $0.close() } catch { XCTFail("Failed to close websocket: \(String(reflecting: error))") }
ws.onPong { webSocket, _ in
do { try await webSocket.close() } catch { XCTFail("Failed to close websocket: \(String(reflecting: error))") }
promise.succeed(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/WebSocketKitTests/SSLTestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func generateRSAPrivateKey() -> OpaquePointer {
precondition(generateRC == 1)

let pkey = CNIOBoringSSL_EVP_PKEY_new()!
let assignRC = CNIOBoringSSL_EVP_PKEY_assign(pkey, EVP_PKEY_RSA, rsa)
let assignRC = CNIOBoringSSL_EVP_PKEY_assign_RSA(pkey, rsa)

precondition(assignRC == 1)
return pkey
Expand Down Expand Up @@ -125,7 +125,7 @@ func generateSelfSignedCert(keygenFunction: () -> OpaquePointer = generateRSAPri
NID_commonName,
MBSTRING_UTF8,
UnsafeMutablePointer(mutating: pointer),
CInt(commonName.lengthOfBytes(using: .utf8)),
ossl_ssize_t(commonName.lengthOfBytes(using: .utf8)),
-1,
0)
}
Expand Down
10 changes: 5 additions & 5 deletions Tests/WebSocketKitTests/WebSocketKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ final class WebSocketKitTests: XCTestCase {
try server.close(mode: .all).wait()
}

func testBadURLInWebsocketConnect() async throws {
func testBadURLInWebsocketConnect() throws {
XCTAssertThrowsError(try WebSocket.connect(to: "%w", on: self.elg, onUpgrade: { _ in }).wait()) {
guard case .invalidURL = $0 as? WebSocketClient.Error else {
return XCTFail("Expected .invalidURL but got \(String(reflecting: $0))")
Expand Down Expand Up @@ -516,8 +516,8 @@ final class WebSocketKitTests: XCTestCase {
return XCTFail("couldn't get port from \(String(reflecting: server.localAddress))")
}
WebSocket.connect(to: "ws://localhost:\(port)", on: self.elg) { ws in
ws.onPong {
$0.close(promise: closePromise)
ws.onPong { webSocket, _ in
webSocket.close(promise: closePromise)
promise.succeed()
}
ws.sendPing()
Expand All @@ -536,8 +536,8 @@ final class WebSocketKitTests: XCTestCase {
}
WebSocket.connect(to: "ws://localhost:\(port)", on: self.elg) { ws in
ws.pingInterval = .milliseconds(100)
ws.onPong {
$0.close(promise: closePromise)
ws.onPong { webSocket, _ in
webSocket.close(promise: closePromise)
promise.succeed()
}
}.cascadeFailure(to: closePromise)
Expand Down