Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECO-5104] Removes reserved keyspaces in line with CHADR-066 #111

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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