瀏覽代碼

Fix crash when request download with fileURL (#3318)

* Fix crash when download local file

* Clean up tests.

Co-authored-by: Jon Shier <jon@jonshier.com>
hyotak.yun 5 年之前
父節點
當前提交
9e6f3265c3
共有 2 個文件被更改,包括 70 次插入0 次删除
  1. 5 0
      Source/URLRequest+Alamofire.swift
  2. 65 0
      Tests/RequestTests.swift

+ 5 - 0
Source/URLRequest+Alamofire.swift

@@ -32,6 +32,11 @@ public extension URLRequest {
     }
 
     func validate() throws {
+        if let url = url, url.isFileURL {
+            // This should become another urlRequestValidationFailed error in Alamofire 6.
+            throw AFError.invalidURL(url: url)
+        }
+
         if method == .get, let bodyData = httpBody {
             throw AFError.urlRequestValidationFailed(reason: .bodyDataInGETRequest(bodyData))
         }

+ 65 - 0
Tests/RequestTests.swift

@@ -1181,3 +1181,68 @@ final class RequestLifetimeTests: BaseTestCase {
         XCTAssertNotNil(task)
     }
 }
+
+// MARK: -
+
+class RequestInvalidURLTestCase: BaseTestCase {
+    #if !SWIFT_PACKAGE
+    func testThatDataRequestWithFileURLThrowsError() {
+        // Given
+        let fileURL = url(forResource: "valid_data", withExtension: "json")
+        let expectation = self.expectation(description: "Request should fail with invalid URL error.")
+        var response: DataResponse<Data?, AFError>?
+
+        // When
+        AF.request(fileURL)
+            .response { resp in
+                response = resp
+                expectation.fulfill()
+            }
+
+        waitForExpectations(timeout: timeout)
+
+        // Then
+        XCTAssertEqual(response?.error?.isInvalidURLError, true)
+    }
+    
+    func testThatDownloadRequestWithFileURLThrowsError() {
+        // Given
+        let fileURL = url(forResource: "valid_data", withExtension: "json")
+        let expectation = self.expectation(description: "Request should fail with invalid URL error.")
+        var response: DownloadResponse<URL?, AFError>?
+
+        // When
+        AF.download(fileURL)
+            .response { resp in
+                response = resp
+                expectation.fulfill()
+            }
+
+        waitForExpectations(timeout: timeout)
+
+        // Then
+        XCTAssertEqual(response?.error?.isInvalidURLError, true)
+    }
+    
+    func testThatDataStreamRequestWithFileURLThrowsError() {
+        // Given
+        let fileURL = url(forResource: "valid_data", withExtension: "json")
+        let expectation = self.expectation(description: "Request should fail with invalid URL error.")
+        var response: DataStreamRequest.Completion?
+
+        // When
+        AF.streamRequest(fileURL)
+            .responseStream { stream in
+                guard case let .complete(completion) = stream.event else { return }
+                
+                response = completion
+                expectation.fulfill()
+            }
+
+        waitForExpectations(timeout: timeout)
+
+        // Then
+        XCTAssertEqual(response?.error?.isInvalidURLError, true)
+    }
+    #endif
+}