StorageReferenceTests.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Combine
  15. @testable import FirebaseStorage
  16. import Foundation
  17. import XCTest
  18. class StorageReferenceTests: XCTestCase {
  19. let expectationTimeout: TimeInterval = 2
  20. override class func setUp() {
  21. FirebaseApp.configureForTests()
  22. }
  23. override class func tearDown() {
  24. FirebaseApp.app()?.delete { success in
  25. if success {
  26. print("Shut down app successfully.")
  27. } else {
  28. print("💥 There was a problem when shutting down the app..")
  29. }
  30. }
  31. }
  32. var storage: Storage?
  33. override func setUp() {
  34. let app = FirebaseApp.appForStorageUnitTestsWithName(name: "App")
  35. storage = Storage.storage(app: app)
  36. }
  37. func testReferenceWithNonExistentFileFails() {
  38. // Given
  39. var cancellables = Set<AnyCancellable>()
  40. let putFileExpectation = expectation(description: "Put file expectation")
  41. let tempFileURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
  42. let tempFilePath = tempFileURL.appendingPathComponent("temp.data").absoluteString
  43. let ref = storage?.reference(withPath: tempFilePath)
  44. let dummyFileURL = URL(fileURLWithPath: "some_non_existing-folder/file.data")
  45. // When
  46. ref?.putFile(from: dummyFileURL, metadata: nil)
  47. .sink { completion in
  48. if case let .failure(error) = completion {
  49. putFileExpectation.fulfill()
  50. XCTAssertTrue(String(describing: error).starts(with: "unknown"))
  51. }
  52. } receiveValue: { metadata in
  53. XCTFail("💥 result unexpected")
  54. }
  55. .store(in: &cancellables)
  56. // then
  57. wait(for: [putFileExpectation], timeout: expectationTimeout)
  58. }
  59. }