Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/StreamChat/ChatClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +246 to 249
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One downside here is that customers won't be able to override the waitsForConnectivity or the httpAdditionalHeaders. But for now, I think this approach is simple enough, and it should work for 99.99% of use cases. If we see the need, we can add this feature later 👍

Expand Down
10 changes: 8 additions & 2 deletions Sources/StreamChat/Config/ChatClientConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 18 additions & 0 deletions Tests/StreamChatTests/StreamChat/ChatClient_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down