Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Feb 18, 2025
1 parent 11398b6 commit 965e517
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
22 changes: 13 additions & 9 deletions Sources/Realtime/RealtimeClientV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import Helpers
public typealias JSONObject = Helpers.JSONObject

/// Factory function for returning a new WebSocket connection.
typealias WebSocketTransport = @Sendable () async throws -> any WebSocket
typealias WebSocketTransport = @Sendable (_ url: URL, _ headers: [String: String]) async throws ->
any WebSocket

public final class RealtimeClientV2: Sendable {
struct MutableState {
Expand Down Expand Up @@ -93,15 +94,11 @@ public final class RealtimeClientV2: Sendable {
self.init(
url: url,
options: options,
wsTransport: {
wsTransport: { url, headers in
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = options.headers.dictionary
configuration.httpAdditionalHeaders = headers
return try await URLSessionWebSocket.connect(
to: Self.realtimeWebSocketURL(
baseURL: Self.realtimeBaseURL(url: url),
apikey: options.apikey,
logLevel: options.logLevel
),
to: url,
configuration: configuration
)
},
Expand Down Expand Up @@ -173,7 +170,14 @@ public final class RealtimeClientV2: Sendable {
status = .connecting

do {
let conn = try await wsTransport()
let conn = try await wsTransport(
Self.realtimeWebSocketURL(
baseURL: Self.realtimeBaseURL(url: url),
apikey: options.apikey,
logLevel: options.logLevel
),
options.headers.dictionary
)
mutableState.withValue { $0.conn = conn }
onConnected(reconnect: reconnect)
} catch {
Expand Down
30 changes: 27 additions & 3 deletions Tests/RealtimeTests/RealtimeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import XCTest

@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
final class RealtimeTests: XCTestCase {
let url = URL(string: "https://localhost:54321/realtime/v1")!
let url = URL(string: "http://localhost:54321/realtime/v1")!
let apiKey = "anon.api.key"

override func invokeTest() {
Expand Down Expand Up @@ -49,7 +49,7 @@ final class RealtimeTests: XCTestCase {
"custom.access.token"
}
),
wsTransport: { self.client },
wsTransport: { _, _ in self.client },
http: http
)
}
Expand All @@ -60,6 +60,30 @@ final class RealtimeTests: XCTestCase {
super.tearDown()
}

func test_transport() async {
let client = RealtimeClientV2(
url: url,
options: RealtimeClientOptions(
headers: ["apikey": apiKey],
logLevel: .warn,
accessToken: {
"custom.access.token"
}
),
wsTransport: { url, headers in
assertInlineSnapshot(of: url, as: .description) {
"""
ws://localhost:54321/realtime/v1/websocket?apikey=anon.api.key&vsn=1.0.0&log_level=warn
"""
}
return FakeWebSocket.fakes().0
},
http: http
)

await client.connect()
}

func testBehavior() async throws {
let channel = sut.channel("public:messages")
var subscriptions: Set<ObservationToken> = []
Expand Down Expand Up @@ -352,7 +376,7 @@ final class RealtimeTests: XCTestCase {
let request = await http.receivedRequests.last
assertInlineSnapshot(of: request?.urlRequest, as: .raw(pretty: true)) {
"""
POST https://localhost:54321/realtime/v1/api/broadcast
POST http://localhost:54321/realtime/v1/api/broadcast
Authorization: Bearer custom.access.token
Content-Type: application/json
apiKey: anon.api.key
Expand Down
2 changes: 1 addition & 1 deletion Tests/RealtimeTests/_PushTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class _PushTests: XCTestCase {
options: RealtimeClientOptions(
headers: ["apiKey": "apikey"]
),
wsTransport: { client },
wsTransport: { _, _ in client },
http: HTTPClientMock()
)
}
Expand Down

0 comments on commit 965e517

Please sign in to comment.