Procházet zdrojové kódy

Throw an error in MultipartUpload when copying a file to a temporary path fails. (#3306)

* Fix to throw an error in MultipartUpload when copying a file to a temporary path fails.

* Update UploadMultipartFormDataTest for nonexistent file.
sudo.park před 5 roky
rodič
revize
54b77cef4c
3 změnil soubory, kde provedl 75 přidání a 49 odebrání
  1. 1 0
      Source/MultipartUpload.swift
  2. 25 25
      Tests/CombineTests.swift
  3. 49 24
      Tests/UploadTests.swift

+ 1 - 0
Source/MultipartUpload.swift

@@ -67,6 +67,7 @@ final class MultipartUpload {
             } catch {
                 // Cleanup after attempted write if it fails.
                 try? fileManager.removeItem(at: fileURL)
+                throw error
             }
 
             uploadable = .file(fileURL, shouldRemove: true)

+ 25 - 25
Tests/CombineTests.swift

@@ -306,14 +306,14 @@ final class DataRequestCombineTests: CombineTestCase {
             AF.request(URLRequest.makeHTTPBinRequest())
                 .publishDecodable(type: HTTPBinResponse.self, queue: queue)
                 .sink(receiveCompletion: { _ in
-                    dispatchPrecondition(condition: .onQueue(queue))
-                    completionReceived.fulfill()
-                },
+                          dispatchPrecondition(condition: .onQueue(queue))
+                          completionReceived.fulfill()
+                      },
                       receiveValue: {
-                    dispatchPrecondition(condition: .onQueue(queue))
-                    response = $0
-                    responseReceived.fulfill()
-                })
+                          dispatchPrecondition(condition: .onQueue(queue))
+                          response = $0
+                          responseReceived.fulfill()
+                      })
         }
 
         waitForExpectations(timeout: timeout)
@@ -812,18 +812,18 @@ final class DataStreamRequestCombineTests: CombineTestCase {
             AF.streamRequest(URLRequest.makeHTTPBinRequest())
                 .publishDecodable(type: HTTPBinResponse.self, queue: queue)
                 .sink(receiveCompletion: { _ in
-                    dispatchPrecondition(condition: .onQueue(queue))
-                    completionReceived.fulfill()
-                },
+                          dispatchPrecondition(condition: .onQueue(queue))
+                          completionReceived.fulfill()
+                      },
                       receiveValue: { stream in
-                    dispatchPrecondition(condition: .onQueue(queue))
-                    switch stream.event {
-                    case let .stream(value):
-                        result = value
-                    case .complete:
-                        responseReceived.fulfill()
-                    }
-                })
+                          dispatchPrecondition(condition: .onQueue(queue))
+                          switch stream.event {
+                          case let .stream(value):
+                              result = value
+                          case .complete:
+                              responseReceived.fulfill()
+                          }
+                      })
         }
 
         waitForExpectations(timeout: timeout)
@@ -1236,14 +1236,14 @@ final class DownloadRequestCombineTests: CombineTestCase {
             AF.download(URLRequest.makeHTTPBinRequest())
                 .publishDecodable(type: HTTPBinResponse.self, queue: queue)
                 .sink(receiveCompletion: { _ in
-                    dispatchPrecondition(condition: .onQueue(queue))
-                    completionReceived.fulfill()
-                },
+                          dispatchPrecondition(condition: .onQueue(queue))
+                          completionReceived.fulfill()
+                      },
                       receiveValue: {
-                    dispatchPrecondition(condition: .onQueue(queue))
-                    response = $0
-                    responseReceived.fulfill()
-                })
+                          dispatchPrecondition(condition: .onQueue(queue))
+                          response = $0
+                          responseReceived.fulfill()
+                      })
         }
 
         waitForExpectations(timeout: timeout)

+ 49 - 24
Tests/UploadTests.swift

@@ -280,9 +280,9 @@ class UploadMultipartFormDataTestCase: BaseTestCase {
 
         // When
         AF.upload(multipartFormData: { multipartFormData in
-            multipartFormData.append(uploadData, withName: "upload_data")
-            formData = multipartFormData
-        },
+                      multipartFormData.append(uploadData, withName: "upload_data")
+                      formData = multipartFormData
+                  },
                   to: urlString)
             .response { resp in
                 response = resp
@@ -351,9 +351,9 @@ class UploadMultipartFormDataTestCase: BaseTestCase {
 
         // When
         AF.upload(multipartFormData: { multipartFormData in
-            multipartFormData.append(frenchData, withName: "french")
-            multipartFormData.append(japaneseData, withName: "japanese")
-        },
+                      multipartFormData.append(frenchData, withName: "french")
+                      multipartFormData.append(japaneseData, withName: "japanese")
+                  },
                   to: urlString)
             .response { resp in
                 response = resp
@@ -388,9 +388,9 @@ class UploadMultipartFormDataTestCase: BaseTestCase {
 
         // When
         let request = AF.upload(multipartFormData: { multipartFormData in
-            multipartFormData.append(frenchData, withName: "french")
-            multipartFormData.append(japaneseData, withName: "japanese")
-        },
+                                    multipartFormData.append(frenchData, withName: "french")
+                                    multipartFormData.append(japaneseData, withName: "japanese")
+                                },
                                 to: urlString)
             .response { resp in
                 response = resp
@@ -420,9 +420,9 @@ class UploadMultipartFormDataTestCase: BaseTestCase {
 
         // When
         let request = AF.upload(multipartFormData: { multipartFormData in
-            multipartFormData.append(uploadData, withName: "upload_data")
-            formData = multipartFormData
-        },
+                                    multipartFormData.append(uploadData, withName: "upload_data")
+                                    formData = multipartFormData
+                                },
                                 to: urlString)
             .response { resp in
                 response = resp
@@ -458,9 +458,9 @@ class UploadMultipartFormDataTestCase: BaseTestCase {
 
         // When
         let request = AF.upload(multipartFormData: { multipartFormData in
-            multipartFormData.append(frenchData, withName: "french")
-            multipartFormData.append(japaneseData, withName: "japanese")
-        },
+                                    multipartFormData.append(frenchData, withName: "french")
+                                    multipartFormData.append(japaneseData, withName: "japanese")
+                                },
                                 to: urlString,
                                 usingThreshold: 0).response { resp in
             response = resp
@@ -490,9 +490,9 @@ class UploadMultipartFormDataTestCase: BaseTestCase {
 
         // When
         let request = AF.upload(multipartFormData: { multipartFormData in
-            multipartFormData.append(uploadData, withName: "upload_data")
-            formData = multipartFormData
-        },
+                                    multipartFormData.append(uploadData, withName: "upload_data")
+                                    formData = multipartFormData
+                                },
                                 to: urlString,
                                 usingThreshold: 0).response { resp in
             response = resp
@@ -519,6 +519,31 @@ class UploadMultipartFormDataTestCase: BaseTestCase {
         }
     }
 
+    func testThatUploadingMultipartFormDataWithNonexistentFileThrowsAnError() {
+        // Given
+        let urlString = URL.makeHTTPBinURL(path: "post")
+        let imageURL = URL(fileURLWithPath: "does_not_exist.jpg")
+
+        let expectation = self.expectation(description: "multipart form data upload from nonexistent file should fail")
+        var response: DataResponse<Data?, AFError>?
+
+        // When
+        let request = AF.upload(multipartFormData: { multipartFormData in
+                                    multipartFormData.append(imageURL, withName: "upload_file")
+                                },
+                                to: urlString,
+                                usingThreshold: 0).response { resp in
+            response = resp
+            expectation.fulfill()
+        }
+
+        waitForExpectations(timeout: timeout)
+
+        // Then
+        XCTAssertNil(request.uploadable)
+        XCTAssertTrue(response?.result.isSuccess == false)
+    }
+
     #if os(macOS)
     func disabled_testThatUploadingMultipartFormDataOnBackgroundSessionWritesDataToFileToAvoidCrash() {
         // Given
@@ -542,9 +567,9 @@ class UploadMultipartFormDataTestCase: BaseTestCase {
 
         // When
         let upload = manager.upload(multipartFormData: { multipartFormData in
-            multipartFormData.append(french, withName: "french")
-            multipartFormData.append(japanese, withName: "japanese")
-        },
+                                        multipartFormData.append(french, withName: "french")
+                                        multipartFormData.append(japanese, withName: "japanese")
+                                    },
                                     to: urlString)
             .response { defaultResponse in
                 request = defaultResponse.request
@@ -589,9 +614,9 @@ class UploadMultipartFormDataTestCase: BaseTestCase {
 
         // When
         AF.upload(multipartFormData: { multipartFormData in
-            multipartFormData.append(loremData1, withName: "lorem1")
-            multipartFormData.append(loremData2, withName: "lorem2")
-        },
+                      multipartFormData.append(loremData1, withName: "lorem1")
+                      multipartFormData.append(loremData2, withName: "lorem2")
+                  },
                   to: urlString,
                   usingThreshold: streamFromDisk ? 0 : 100_000_000)
             .uploadProgress { progress in