Browse Source

extend userInfo-dict of .DidComplete-Notification to contain responseData of DataTasks (#2427)

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

* ResponseBodyInNotification: fix unittests sometimes failing on travis ci
Sven Driemecker 7 years ago
parent
commit
491cf2f255
3 changed files with 71 additions and 1 deletions
  1. 3 0
      Source/Notifications.swift
  2. 7 1
      Source/SessionDelegate.swift
  3. 61 0
      Tests/SessionDelegateTests.swift

+ 3 - 0
Source/Notifications.swift

@@ -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"
     }
 }

+ 7 - 1
Source/SessionDelegate.swift

@@ -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

+ 61 - 0
Tests/SessionDelegateTests.swift

@@ -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)
+    }
 }