Skip to content

Commit ab2ab49

Browse files
authored
Add error code testing after cancel (#10484)
1 parent 0b1cb06 commit ab2ab49

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

FirebaseStorage/Tests/Integration/StorageIntegration.swift

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import FirebaseAuth
1616
import FirebaseCore
17-
import FirebaseStorage
17+
@testable import FirebaseStorage
1818
import XCTest
1919

2020
/**
@@ -475,6 +475,42 @@ class StorageResultTests: StorageIntegrationCommon {
475475
waitForExpectations()
476476
}
477477

478+
func testCancelErrorCode() throws {
479+
let expectation = self.expectation(description: #function)
480+
let ref = storage.reference(withPath: "ios/public/helloworld")
481+
let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
482+
let fileURL = tmpDirURL.appendingPathComponent("hello.txt")
483+
let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
484+
485+
ref.putData(data) { result in
486+
switch result {
487+
case .success:
488+
let task = ref.write(toFile: fileURL)
489+
task.cancel()
490+
491+
task.observe(StorageTaskStatus.success) { snapshot in
492+
XCTFail("Error processing success snapshot")
493+
expectation.fulfill()
494+
}
495+
496+
task.observe(StorageTaskStatus.failure) { snapshot in
497+
let expected = "User cancelled the upload/download."
498+
if let error = snapshot.error {
499+
let errorDescription = error.localizedDescription
500+
XCTAssertEqual(errorDescription, expected)
501+
let code = (error as NSError).code
502+
XCTAssertEqual(code, StorageErrorCode.cancelled.rawValue)
503+
}
504+
expectation.fulfill()
505+
}
506+
case let .failure(error):
507+
XCTFail("Unexpected error \(error) from putData")
508+
expectation.fulfill()
509+
}
510+
}
511+
waitForExpectations()
512+
}
513+
478514
private func assertMetadata(actualMetadata: StorageMetadata,
479515
expectedContentType: String,
480516
expectedCustomMetadata: [String: String]) {

FirebaseStorage/Tests/ObjCIntegration/FIRStorageIntegrationTests.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ - (void)testCancelDownload {
624624
XCTAssertTrue([[snapshot description] containsString:@"State: Failed"]);
625625
if (!fulfilled) {
626626
fulfilled = YES;
627+
XCTAssertEqual(snapshot.error.code, FIRStorageErrorCodeCancelled);
627628
[expectation fulfill];
628629
}
629630
}];

0 commit comments

Comments
 (0)