Browse Source

Adding tests for upload and download with progress

Mattt Thompson 11 năm trước cách đây
mục cha
commit
da237ff228
2 tập tin đã thay đổi với 43 bổ sung1 xóa
  1. 22 0
      Tests/DownloadTests.swift
  2. 21 1
      Tests/UploadTests.swift

+ 22 - 0
Tests/DownloadTests.swift

@@ -78,4 +78,26 @@ class AlamofireDownloadResponseTestCase: XCTestCase {
             XCTAssertNil(error, "\(error)")
         }
     }
+
+    func testDownloadRequestWithProgress() {
+        let numberOfLines = 100
+        let URL = "http://httpbin.org/stream/\(numberOfLines)"
+
+        let expectation = expectationWithDescription(URL)
+
+        let destination = Alamofire.Request.suggestedDownloadDestination(directory: searchPathDirectory, domain: searchPathDomain)
+
+        Alamofire.download(.GET, URL, destination)
+                 .progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) -> Void in
+                    expectation.fulfill()
+
+                    XCTAssertGreaterThan(bytesRead, 0, "bytesRead should be > 0")
+                    XCTAssertGreaterThan(totalBytesRead, 0, "totalBytesRead should be > 0")
+                    XCTAssertEqual(totalBytesExpectedToRead, -1, "totalBytesExpectedToRead should be -1")
+                 }
+
+        waitForExpectationsWithTimeout(10) { (error) in
+            XCTAssertNil(error, "\(error)")
+        }
+    }
 }

+ 21 - 1
Tests/UploadTests.swift

@@ -25,7 +25,7 @@ import Alamofire
 import XCTest
 
 class UploadResponseTestCase: XCTestCase {
-    func testDownloadRequest() {
+    func testUploadRequest() {
         let URL = "http://httpbin.org/post"
         let data = "Lorem ipsum dolor sit amet".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
 
@@ -44,4 +44,24 @@ class UploadResponseTestCase: XCTestCase {
             XCTAssertNil(error, "\(error)")
         }
     }
+
+    func testUploadRequestWithProgress() {
+        let URL = "http://httpbin.org/post"
+        let data = "Lorem ipsum dolor sit amet".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
+
+        let expectation = expectationWithDescription(URL)
+
+        Alamofire.upload(.POST, URL, data!)
+                 .progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) -> Void in
+                    expectation.fulfill()
+
+                    XCTAssertGreaterThan(bytesWritten, 0, "bytesWritten should be > 0")
+                    XCTAssertGreaterThan(totalBytesWritten, 0, "totalBytesWritten should be > 0")
+                    XCTAssertGreaterThan(totalBytesExpectedToWrite, 0, "totalBytesExpectedToWrite should be > 0")
+                 }
+
+        waitForExpectationsWithTimeout(10) { (error) in
+            XCTAssertNil(error, "\(error)")
+        }
+    }
 }