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