diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bd8e60f63f..0c4628792d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). # Upcoming +## StreamChat +### 🐞 Fixed +- Allow injecting a custom URLSessionConfiguration in ChatClientConfig [#2756](https://github.com/GetStream/stream-chat-swift/pull/2756) + ## StreamChatUI ### 🔄 Changed - Make record button in composer, visible depending on the channel's capabilities. [#2758](https://github.com/GetStream/stream-chat-swift/pull/2758) diff --git a/Sources/StreamChat/ChatClient.swift b/Sources/StreamChat/ChatClient.swift index 0c9ebdfbd93..9679df8d7be 100644 --- a/Sources/StreamChat/ChatClient.swift +++ b/Sources/StreamChat/ChatClient.swift @@ -243,7 +243,7 @@ public class ChatClient { /// The default configuration of URLSession to be used for both the `APIClient` and `WebSocketClient`. It contains all /// required header auth parameters to make a successful request. private var urlSessionConfiguration: URLSessionConfiguration { - let configuration = URLSessionConfiguration.default + let configuration = config.urlSessionConfiguration configuration.waitsForConnectivity = false configuration.httpAdditionalHeaders = sessionHeaders configuration.timeoutIntervalForRequest = config.timeoutIntervalForRequest diff --git a/Sources/StreamChat/Config/ChatClientConfig.swift b/Sources/StreamChat/Config/ChatClientConfig.swift index c1232336061..197ad7f29a1 100644 --- a/Sources/StreamChat/Config/ChatClientConfig.swift +++ b/Sources/StreamChat/Config/ChatClientConfig.swift @@ -186,8 +186,14 @@ public struct ChatClientConfig { /// whenever a new channel is created,/updated the SDK will try to /// match the channel list filter automatically. public var isChannelAutomaticFilteringEnabled: Bool = true - - public init(apiKey: APIKey) { + + /// The `URLSessionConfiguration` being used as default configuration for the `APIClient` and + /// `WebSocketClient` + public var urlSessionConfiguration: URLSessionConfiguration = .default + + public init( + apiKey: APIKey + ) { self.apiKey = apiKey isClientInActiveMode = !Bundle.main.isAppExtension } diff --git a/Tests/StreamChatTests/StreamChat/ChatClient_Tests.swift b/Tests/StreamChatTests/StreamChat/ChatClient_Tests.swift index cf1de401d42..d089f3b9bfd 100644 --- a/Tests/StreamChatTests/StreamChat/ChatClient_Tests.swift +++ b/Tests/StreamChatTests/StreamChat/ChatClient_Tests.swift @@ -328,6 +328,24 @@ final class ChatClient_Tests: XCTestCase { XCTAssertEqual(client.activeChannelControllers.count, 0) XCTAssertEqual(client.activeChannelListControllers.count, 0) } + + + func test_apiClient_usesInjectedURLSessionConfiguration() { + // configure a URLSessionConfiguration with a URLProtocol class + var urlSessionConfiguration = URLSessionConfiguration.default + URLProtocol_Mock.startTestSession(with: &urlSessionConfiguration) + + // initialise a ChatClient with a custom URLSessionConfiguration, + // which is used instead of `URLSessionConfiguration.default` + var chatClientConfig = ChatClientConfig() + chatClientConfig.urlSessionConfiguration = urlSessionConfiguration + let chatClient = ChatClient(config: chatClientConfig) + + // make sure the `apiClient` is initialised using the injected + // `URLSessionConfiguration` + XCTAssertTrue(chatClient.apiClient.session.configuration.protocolClasses? + .contains(where: { $0 is URLProtocol_Mock.Type }) ?? false) + } // MARK: - Background workers tests