Skip to content

Commit 6f14be9

Browse files
committed
test: revert broadcast with HTTP test
1 parent c5d15e3 commit 6f14be9

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

Sources/Helpers/Codable.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import ConcurrencyExtras
99
import Foundation
10+
import XCTestDynamicOverlay
1011

1112
extension JSONDecoder {
1213
/// Default `JSONDecoder` for decoding types from Supabase.
@@ -21,7 +22,8 @@ extension JSONDecoder {
2122
}
2223

2324
throw DecodingError.dataCorruptedError(
24-
in: container, debugDescription: "Invalid date format: \(string)"
25+
in: container,
26+
debugDescription: "Invalid date format: \(string)"
2527
)
2628
}
2729
return decoder
@@ -36,6 +38,13 @@ extension JSONEncoder {
3638
let string = date.iso8601String
3739
try container.encode(string)
3840
}
41+
42+
#if DEBUG
43+
if isTesting {
44+
encoder.outputFormatting = [.sortedKeys]
45+
}
46+
#endif
47+
3948
return encoder
4049
}
4150
}

Sources/Realtime/RealtimeChannelV2.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public final class RealtimeChannelV2: Sendable, RealtimeChannelProtocol {
295295
}
296296
headers[.authorization] = "Bearer \(accessToken)"
297297

298-
let body = try await JSONEncoder().encode(
298+
let body = try await JSONEncoder.supabase().encode(
299299
BroadcastMessagePayload(
300300
messages: [
301301
BroadcastMessagePayload.Message(
@@ -347,13 +347,17 @@ public final class RealtimeChannelV2: Sendable, RealtimeChannelProtocol {
347347
@MainActor
348348
public func broadcast(event: String, message: JSONObject) async {
349349
if status != .subscribed {
350-
reportIssue(
351-
"""
352-
Realtime broadcast() is automatically falling back to REST API.
353-
This behavior will be deprecated in the future.
354-
Please use httpSend() explicitly for REST delivery.
355-
"""
356-
)
350+
// Properly expecting issues during tests isn't working as expected, I think because the reportIssue is usually triggered inside an unstructured Task
351+
// because of this I'm disabling issue reporting during tests, so we can use it only for advising developers when running their applications.
352+
if !isTesting {
353+
reportIssue(
354+
"""
355+
Realtime broadcast() is automatically falling back to REST API.
356+
This behavior will be deprecated in the future.
357+
Please use httpSend() explicitly for REST delivery.
358+
"""
359+
)
360+
}
357361

358362
var headers: HTTPFields = [.contentType: "application/json"]
359363
if let apiKey = socket.options.apikey {
@@ -369,7 +373,7 @@ public final class RealtimeChannelV2: Sendable, RealtimeChannelProtocol {
369373
url: socket.broadcastURL,
370374
method: .post,
371375
headers: headers,
372-
body: JSONEncoder().encode(
376+
body: JSONEncoder.supabase().encode(
373377
BroadcastMessagePayload(
374378
messages: [
375379
BroadcastMessagePayload.Message(

Tests/RealtimeTests/RealtimeTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,41 @@ final class RealtimeTests: XCTestCase {
575575
XCTAssertEqual(heartbeatStatuses.value, [.sent, .timeout])
576576
}
577577

578+
func testBroadcastWithHTTP() async throws {
579+
await http.when {
580+
$0.url.path.hasSuffix("broadcast")
581+
} return: { _ in
582+
HTTPResponse(
583+
data: "{}".data(using: .utf8)!,
584+
response: HTTPURLResponse(
585+
url: self.sut.broadcastURL,
586+
statusCode: 200,
587+
httpVersion: nil,
588+
headerFields: nil
589+
)!
590+
)
591+
}
592+
593+
let channel = sut.channel("public:messages") {
594+
$0.broadcast.acknowledgeBroadcasts = true
595+
}
596+
597+
try await channel.broadcast(event: "test", message: ["value": 42])
598+
599+
let request = await http.receivedRequests.last
600+
assertInlineSnapshot(of: request?.urlRequest, as: .curl) {
601+
#"""
602+
curl \
603+
--request POST \
604+
--header "Authorization: Bearer custom.access.token" \
605+
--header "Content-Type: application/json" \
606+
--header "apiKey: anon.api.key" \
607+
--data "{\"messages\":[{\"event\":\"test\",\"payload\":{\"value\":42},\"private\":false,\"topic\":\"realtime:public:messages\"}]}" \
608+
"http://localhost:54321/realtime/v1/api/broadcast"
609+
"""#
610+
}
611+
}
612+
578613
func testSetAuth() async {
579614
let validToken =
580615
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjY0MDkyMjExMjAwfQ.GfiEKLl36X8YWcatHg31jRbilovlGecfUKnOyXMSX9c"

0 commit comments

Comments
 (0)