Sfoglia il codice sorgente

Added test verifying download request can be resumed with resume data.

Christian Noon 9 anni fa
parent
commit
acc7361c2e
1 ha cambiato i file con 61 aggiunte e 0 eliminazioni
  1. 61 0
      Tests/DownloadTests.swift

+ 61 - 0
Tests/DownloadTests.swift

@@ -453,4 +453,65 @@ class DownloadResumeDataTestCase: BaseTestCase {
 
         XCTAssertEqual(response?.resumeData, download.resumeData)
     }
+
+    func testThatCancelledDownloadCanBeResumedWithResumeData() {
+        // Given
+        let expectation1 = self.expectation(description: "Download should be cancelled")
+        var cancelled = false
+
+        var response1: DownloadResponse<Data>?
+
+        // When
+        let download = Alamofire.download(urlString)
+        download.downloadProgress { progress in
+            guard !cancelled else { return }
+
+            if progress.fractionCompleted > 0.4 {
+                download.cancel()
+                cancelled = true
+            }
+        }
+        download.responseData { resp in
+            response1 = resp
+            expectation1.fulfill()
+        }
+
+        waitForExpectations(timeout: timeout, handler: nil)
+
+        guard let resumeData = download.resumeData else {
+            XCTFail("resumeData should not be nil")
+            return
+        }
+
+        let expectation2 = self.expectation(description: "Download should complete")
+
+        var progressValues: [Double] = []
+        var response2: DownloadResponse<Data>?
+
+        Alamofire.download(resumingWith: resumeData)
+            .downloadProgress { progress in
+                progressValues.append(progress.fractionCompleted)
+            }
+            .responseData { resp in
+                response2 = resp
+                expectation2.fulfill()
+            }
+
+        waitForExpectations(timeout: timeout, handler: nil)
+
+        // Then
+        XCTAssertNotNil(response1?.request)
+        XCTAssertNotNil(response1?.response)
+        XCTAssertNil(response1?.destinationURL)
+        XCTAssertEqual(response1?.result.isFailure, true)
+        XCTAssertNotNil(response1?.result.error)
+
+        XCTAssertNotNil(response2?.response)
+        XCTAssertNotNil(response2?.temporaryURL)
+        XCTAssertNil(response2?.destinationURL)
+        XCTAssertEqual(response2?.result.isSuccess, true)
+        XCTAssertNil(response2?.result.error)
+
+        progressValues.forEach { XCTAssertGreaterThanOrEqual($0, 0.4) }
+    }
 }