|
|
@@ -712,3 +712,164 @@ class DownloadResponseFlatMapTestCase: BaseTestCase {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+class DownloadResponseMapErrorTestCase: BaseTestCase {
|
|
|
+ func testThatMapErrorTransformsFailureValue() {
|
|
|
+ // Given
|
|
|
+ let urlString = "https://invalid-url-here.org/this/does/not/exist"
|
|
|
+ let expectation = self.expectation(description: "request should not succeed")
|
|
|
+
|
|
|
+ var response: DownloadResponse<Any>?
|
|
|
+
|
|
|
+ // When
|
|
|
+ Alamofire.download(urlString).responseJSON { resp in
|
|
|
+ response = resp.mapError { error in
|
|
|
+ return TestError.error(error: error)
|
|
|
+ }
|
|
|
+
|
|
|
+ expectation.fulfill()
|
|
|
+ }
|
|
|
+
|
|
|
+ waitForExpectations(timeout: timeout, handler: nil)
|
|
|
+
|
|
|
+ // Then
|
|
|
+ XCTAssertNotNil(response?.request)
|
|
|
+ XCTAssertNil(response?.response)
|
|
|
+ XCTAssertNil(response?.temporaryURL)
|
|
|
+ XCTAssertNil(response?.destinationURL)
|
|
|
+ XCTAssertNil(response?.resumeData)
|
|
|
+ XCTAssertNotNil(response?.error)
|
|
|
+ XCTAssertEqual(response?.result.isFailure, true)
|
|
|
+ guard let error = response?.error as? TestError, case .error = error else { XCTFail(); return }
|
|
|
+
|
|
|
+ if #available(iOS 10.0, macOS 10.12, tvOS 10.0, *) {
|
|
|
+ XCTAssertNotNil(response?.metrics)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func testThatMapErrorPreservesSuccessValue() {
|
|
|
+ // Given
|
|
|
+ let urlString = "https://httpbin.org/get"
|
|
|
+ let expectation = self.expectation(description: "request should succeed")
|
|
|
+
|
|
|
+ var response: DownloadResponse<Data>?
|
|
|
+
|
|
|
+ // When
|
|
|
+ Alamofire.download(urlString).responseData { resp in
|
|
|
+ response = resp.mapError { TestError.error(error: $0) }
|
|
|
+ expectation.fulfill()
|
|
|
+ }
|
|
|
+
|
|
|
+ waitForExpectations(timeout: timeout, handler: nil)
|
|
|
+
|
|
|
+ // Then
|
|
|
+ XCTAssertNotNil(response?.request)
|
|
|
+ XCTAssertNotNil(response?.response)
|
|
|
+ XCTAssertNotNil(response?.temporaryURL)
|
|
|
+ XCTAssertNil(response?.destinationURL)
|
|
|
+ XCTAssertNil(response?.resumeData)
|
|
|
+ XCTAssertEqual(response?.result.isSuccess, true)
|
|
|
+
|
|
|
+ if #available(iOS 10.0, macOS 10.12, tvOS 10.0, *) {
|
|
|
+ XCTAssertNotNil(response?.metrics)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: -
|
|
|
+
|
|
|
+class DownloadResponseFlatMapErrorTestCase: BaseTestCase {
|
|
|
+ func testThatFlatMapErrorPreservesSuccessValue() {
|
|
|
+ // Given
|
|
|
+ let urlString = "https://httpbin.org/get"
|
|
|
+ let expectation = self.expectation(description: "request should succeed")
|
|
|
+
|
|
|
+ var response: DownloadResponse<Data>?
|
|
|
+
|
|
|
+ // When
|
|
|
+ Alamofire.download(urlString).responseData { resp in
|
|
|
+ response = resp.flatMapError { TestError.error(error: $0) }
|
|
|
+ expectation.fulfill()
|
|
|
+ }
|
|
|
+
|
|
|
+ waitForExpectations(timeout: timeout, handler: nil)
|
|
|
+
|
|
|
+ // Then
|
|
|
+ XCTAssertNotNil(response?.request)
|
|
|
+ XCTAssertNotNil(response?.response)
|
|
|
+ XCTAssertNotNil(response?.temporaryURL)
|
|
|
+ XCTAssertNil(response?.destinationURL)
|
|
|
+ XCTAssertNil(response?.resumeData)
|
|
|
+ XCTAssertNil(response?.error)
|
|
|
+ XCTAssertEqual(response?.result.isSuccess, true)
|
|
|
+
|
|
|
+ if #available(iOS 10.0, macOS 10.12, tvOS 10.0, *) {
|
|
|
+ XCTAssertNotNil(response?.metrics)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func testThatFlatMapErrorCatchesTransformationError() {
|
|
|
+ // Given
|
|
|
+ let urlString = "https://invalid-url-here.org/this/does/not/exist"
|
|
|
+ let expectation = self.expectation(description: "request should fail")
|
|
|
+
|
|
|
+ var response: DownloadResponse<Data>?
|
|
|
+
|
|
|
+ // When
|
|
|
+ Alamofire.download(urlString).responseData { resp in
|
|
|
+ response = resp.flatMapError { _ in try TransformationError.error.alwaysFails() }
|
|
|
+ expectation.fulfill()
|
|
|
+ }
|
|
|
+
|
|
|
+ waitForExpectations(timeout: timeout, handler: nil)
|
|
|
+
|
|
|
+ // Then
|
|
|
+ XCTAssertNotNil(response?.request)
|
|
|
+ XCTAssertNil(response?.response)
|
|
|
+ XCTAssertNil(response?.temporaryURL)
|
|
|
+ XCTAssertNil(response?.destinationURL)
|
|
|
+ XCTAssertNil(response?.resumeData)
|
|
|
+ XCTAssertNotNil(response?.error)
|
|
|
+ XCTAssertEqual(response?.result.isFailure, true)
|
|
|
+
|
|
|
+ if let error = response?.result.error {
|
|
|
+ XCTAssertTrue(error is TransformationError)
|
|
|
+ } else {
|
|
|
+ XCTFail("flatMapError should catch the transformation error")
|
|
|
+ }
|
|
|
+
|
|
|
+ if #available(iOS 10.0, macOS 10.12, tvOS 10.0, *) {
|
|
|
+ XCTAssertNotNil(response?.metrics)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func testThatFlatMapErrorTransformsError() {
|
|
|
+ // Given
|
|
|
+ let urlString = "https://invalid-url-here.org/this/does/not/exist"
|
|
|
+ let expectation = self.expectation(description: "request should fail")
|
|
|
+
|
|
|
+ var response: DownloadResponse<Data>?
|
|
|
+
|
|
|
+ // When
|
|
|
+ Alamofire.download(urlString).responseData { resp in
|
|
|
+ response = resp.flatMapError { TestError.error(error: $0) }
|
|
|
+ expectation.fulfill()
|
|
|
+ }
|
|
|
+
|
|
|
+ waitForExpectations(timeout: timeout, handler: nil)
|
|
|
+
|
|
|
+ // Then
|
|
|
+ XCTAssertNotNil(response?.request)
|
|
|
+ XCTAssertNil(response?.response)
|
|
|
+ XCTAssertNil(response?.temporaryURL)
|
|
|
+ XCTAssertNil(response?.destinationURL)
|
|
|
+ XCTAssertNil(response?.resumeData)
|
|
|
+ XCTAssertNotNil(response?.error)
|
|
|
+ XCTAssertEqual(response?.result.isFailure, true)
|
|
|
+ guard let error = response?.error as? TestError, case .error = error else { XCTFail(); return }
|
|
|
+
|
|
|
+ if #available(iOS 10.0, macOS 10.12, tvOS 10.0, *) {
|
|
|
+ XCTAssertNotNil(response?.metrics)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|