Skip to content

Commit

Permalink
extend userInfo-dict of .DidComplete-Notification to contain response…
Browse files Browse the repository at this point in the history
…Data of DataTasks (Alamofire#2427)

* extend userInfo-dict of .DidComplete-Notification to contain responseData of DataTasks

* ResponseBodyInNotification: fix unittests sometimes failing on travis ci
  • Loading branch information
svendr authored and jshier committed Mar 5, 2018
1 parent 4d30762 commit 491cf2f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Source/Notifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@ extension Notification {
public struct Key {
/// User info dictionary key representing the `URLSessionTask` associated with the notification.
public static let Task = "org.alamofire.notification.key.task"

/// User info dictionary key representing the responseData associated with the notification.
public static let ResponseData = "org.alamofire.notification.key.responseData"
}
}
8 changes: 7 additions & 1 deletion Source/SessionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,16 @@ extension SessionDelegate: URLSessionTaskDelegate {

strongSelf[task]?.delegate.urlSession(session, task: task, didCompleteWithError: error)

var userInfo: [String: Any] = [Notification.Key.Task: task]

if let data = (strongSelf[task]?.delegate as? DataTaskDelegate)?.data {
userInfo[Notification.Key.ResponseData] = data
}

NotificationCenter.default.post(
name: Notification.Name.Task.DidComplete,
object: strongSelf,
userInfo: [Notification.Key.Task: task]
userInfo: userInfo
)

strongSelf[task] = nil
Expand Down
61 changes: 61 additions & 0 deletions Tests/SessionDelegateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,65 @@ class SessionDelegateTestCase: BaseTestCase {
XCTAssertTrue(overrideClosureCalled)
XCTAssertEqual(response?.statusCode, 200)
}

func testThatDidCompleteNotificationIsCalledWithResponseDataForDataTasks() {
// Given
var notificationCalledWithResponseData = false
var response: HTTPURLResponse?

let expectation = self.expectation(forNotification: Notification.Name.Task.DidComplete.rawValue, object: nil) { notif -> Bool in

// check that we are handling notif for a dataTask
guard let task = notif.userInfo?[Notification.Key.Task] as? URLSessionDataTask else {
return false
}

response = task.response as? HTTPURLResponse

// check that responseData are set in userInfo-dict and it's not empty
if let responseData = notif.userInfo?[Notification.Key.ResponseData] as? Data {
notificationCalledWithResponseData = responseData.count > 0
}

return notificationCalledWithResponseData
}

// When
manager.request("https://httpbin.org/get").responseJSON { resp in }

wait(for: [expectation], timeout: timeout)

// Then
XCTAssertTrue(notificationCalledWithResponseData)
XCTAssertEqual(response?.statusCode, 200)
}

func testThatDidCompleteNotificationIsntCalledForDownloadTasks() {
// Given
var notificationCalledWithNilResponseData = false
var response: HTTPURLResponse?

let expectation = self.expectation(forNotification: Notification.Name.Task.DidComplete.rawValue, object: nil) { notif -> Bool in

// check that we are handling notif for a downloadTask
guard let task = notif.userInfo?[Notification.Key.Task] as? URLSessionDownloadTask else {
return false
}

response = task.response as? HTTPURLResponse

// check that responseData are NOT set in userInfo-dict
notificationCalledWithNilResponseData = notif.userInfo?[Notification.Key.ResponseData] == nil
return notificationCalledWithNilResponseData
}

// When
manager.download("https://httpbin.org/get").response {resp in }

wait(for: [expectation], timeout: timeout)

// Then
XCTAssertTrue(notificationCalledWithNilResponseData)
XCTAssertEqual(response?.statusCode, 200)
}
}

0 comments on commit 491cf2f

Please sign in to comment.