Skip to content

Commit

Permalink
Removes reserved keyspaces in line with CHADR-066 - https://ably.atla…
Browse files Browse the repository at this point in the history
  • Loading branch information
umair-ably committed Nov 14, 2024
1 parent 5e4a862 commit f7f19cc
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 71 deletions.
12 changes: 0 additions & 12 deletions Sources/AblyChat/ChatAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,10 @@ internal final class ChatAPI: Sendable {
// (CHA-M3b) A message may be sent without metadata or headers. When these are not specified by the user, they must be omitted from the REST payload.
if let metadata = params.metadata {
body["metadata"] = metadata

// (CHA-M3c) metadata must not contain the key ably-chat. This is reserved for future internal use. If this key is present, the send call shall terminate by throwing an ErrorInfo with code 40001.
if metadata.contains(where: { $0.key == "ably-chat" }) {
throw ARTErrorInfo.create(withCode: 40001, message: "metadata must not contain the key `ably-chat`")
}
}

if let headers = params.headers {
body["headers"] = headers

// (CHA-M3d) headers must not contain a key prefixed with ably-chat. This is reserved for future internal use. If this key is present, the send call shall terminate by throwing an ErrorInfo with code 40001.
if headers.keys.contains(where: { keyString in
keyString.hasPrefix("ably-chat")
}) {
throw ARTErrorInfo.create(withCode: 40001, message: "headers must not contain any key with a prefix of `ably-chat`")
}
}

let response: SendMessageResponse = try await makeRequest(endpoint, method: "POST", body: body)
Expand Down
59 changes: 0 additions & 59 deletions Tests/AblyChatTests/ChatAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,6 @@ import Testing
struct ChatAPITests {
// MARK: sendMessage Tests

// @spec CHA-M3c
@Test
func sendMessage_whenMetadataHasAblyChatAsKey_throws40001() async {
// Given
let realtime = MockRealtime.create()
let chatAPI = ChatAPI(realtime: realtime)
let roomId = "basketball::$chat::$chatMessages"
let expectedError = ARTErrorInfo.create(withCode: 40001, message: "metadata must not contain the key `ably-chat`")

await #expect(
performing: {
// When
try await chatAPI.sendMessage(roomId: roomId, params: .init(text: "hello", metadata: ["ably-chat": .null]))
}, throws: { error in
// Then
error as? ARTErrorInfo == expectedError
}
)
}

// @specOneOf(1/2) CHA-M3d
@Test
func sendMessage_whenHeadersHasAnyKeyWithPrefixOfAblyChat_throws40001() async {
// Given
let realtime = MockRealtime.create {
(MockHTTPPaginatedResponse.successSendMessage, nil)
}
let chatAPI = ChatAPI(realtime: realtime)
let roomId = "basketball::$chat::$chatMessages"
let expectedError = ARTErrorInfo.create(withCode: 40001, message: "headers must not contain any key with a prefix of `ably-chat`")

await #expect(
performing: {
// When
try await chatAPI.sendMessage(roomId: roomId, params: .init(text: "hello", headers: ["ably-chat123": .null]))
}, throws: { error in
// then
error as? ARTErrorInfo == expectedError
}
)
}

// @specOneOf(2/2) CHA-M3d
@Test
func sendMessage_whenHeadersHasAnyKeyWithSuffixOfAblyChat_doesNotThrowAnyError() async {
// Given
let realtime = MockRealtime.create {
(MockHTTPPaginatedResponse.successSendMessage, nil)
}
let chatAPI = ChatAPI(realtime: realtime)
let roomId = "basketball::$chat::$chatMessages"

// Then
await #expect(throws: Never.self, performing: {
// When
try await chatAPI.sendMessage(roomId: roomId, params: .init(text: "hello", headers: ["123ably-chat": .null]))
})
}

@Test
func sendMessage_whenSendMessageReturnsNoItems_throwsNoItemInResponse() async {
// Given
Expand Down

0 comments on commit f7f19cc

Please sign in to comment.