Skip to content

Commit

Permalink
Session and task descriptions (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
hishma committed Mar 8, 2024
1 parent b800c46 commit 61e0153
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
1 change: 1 addition & 0 deletions Tests/ApolloInternalTestHelpers/MockURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class MockURLSessionClient: URLSessionClient {
}

public override func sendRequest(_ request: URLRequest,
taskDescription: String? = nil,
rawTaskCompletionHandler: URLSessionClient.RawCompletion? = nil,
completion: @escaping URLSessionClient.Completion) -> URLSessionTask {
self.$lastRequest.mutate { $0 = request }
Expand Down
45 changes: 45 additions & 0 deletions Tests/ApolloTests/Network/URLSessionClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,51 @@ class URLSessionClientTests: XCTestCase {
self.wait(for: [expectation], timeout: 5)
}

func testSessionDescription() {
// Should be nil by default.
XCTAssertNil(client.session.sessionDescription)

// Should set the sessionDescription of the URLSession.
let expected = "test description"
let client2 = URLSessionClient(sessionConfiguration: sessionConfiguration,
sessionDescription: expected)
XCTAssertEqual(expected, client2.session.sessionDescription)

client2.invalidate()
}

func testTaskDescription() {
let url = URL(string: "http://www.test.com/taskDesciption")!

let request = request(for: url,
responseData: nil,
statusCode: -1)

let expectation = self.expectation(description: "Described task completed")
expectation.isInverted = true

let task = self.client.sendRequest(request) { result in
// This shouldn't get hit since we cancel the task immediately
expectation.fulfill()
}
self.client.cancel(task: task)

// Should be nil by default.
XCTAssertNil(task.taskDescription)

let expected = "test task description"
let describedTask = self.client.sendRequest(request,
taskDescription: expected) { result in
// This shouldn't get hit since we cancel the task immediately
expectation.fulfill()
}
self.client.cancel(task: describedTask)

// The returned task should have the provided taskDescription.
XCTAssertEqual(expected, describedTask.taskDescription)

self.wait(for: [expectation], timeout: 5)
}
}

extension URLSessionClientTests: MockRequestProvider {
Expand Down
17 changes: 13 additions & 4 deletions apollo-ios/Sources/Apollo/URLSessionClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ open class URLSessionClient: NSObject, URLSessionDelegate, URLSessionTaskDelegat
/// - Parameters:
/// - sessionConfiguration: The `URLSessionConfiguration` to use to set up the URL session.
/// - callbackQueue: [optional] The `OperationQueue` to tell the URL session to call back to this class on, which will in turn call back to your class. Defaults to `.main`.
/// - sessionDescription: [optional] A human-readable string that you can use for debugging purposes.
public init(sessionConfiguration: URLSessionConfiguration = .default,
callbackQueue: OperationQueue? = .main) {
callbackQueue: OperationQueue? = .main,
sessionDescription: String? = nil) {
super.init()
self.session = URLSession(configuration: sessionConfiguration,
delegate: self,
delegateQueue: callbackQueue)

let session = URLSession(configuration: sessionConfiguration,
delegate: self,
delegateQueue: callbackQueue)
session.sessionDescription = sessionDescription
self.session = session
}

/// Cleans up and invalidates everything related to this session client.
Expand Down Expand Up @@ -112,12 +117,14 @@ open class URLSessionClient: NSObject, URLSessionDelegate, URLSessionTaskDelegat
///
/// - Parameters:
/// - request: The request to perform.
/// - taskDescription: [optional] A description to add to the `URLSessionTask` for debugging purposes.
/// - rawTaskCompletionHandler: [optional] A completion handler to call once the raw task is done, so if an Error requires access to the headers, the user can still access these.
/// - completion: A completion handler to call when the task has either completed successfully or failed.
///
/// - Returns: The created URLSession task, already resumed, because nobody ever remembers to call `resume()`.
@discardableResult
open func sendRequest(_ request: URLRequest,
taskDescription: String? = nil,
rawTaskCompletionHandler: RawCompletion? = nil,
completion: @escaping Completion) -> URLSessionTask {
guard self.hasNotBeenInvalidated else {
Expand All @@ -126,6 +133,8 @@ open class URLSessionClient: NSObject, URLSessionDelegate, URLSessionTaskDelegat
}

let task = self.session.dataTask(with: request)
task.taskDescription = taskDescription

let taskData = TaskData(rawCompletion: rawTaskCompletionHandler,
completionBlock: completion)

Expand Down

0 comments on commit 61e0153

Please sign in to comment.