StorageDeleteTests.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2022 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. @testable import FirebaseStorage
  15. import Foundation
  16. import GTMSessionFetcherCore
  17. import XCTest
  18. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  19. class StorageDeleteTests: StorageTestHelpers {
  20. func testFetcherConfiguration() async {
  21. let testBlock = { (fetcher: GTMSessionFetcher!,
  22. response: GTMSessionFetcherTestResponse) in
  23. XCTAssertEqual(fetcher.request?.url, self.objectURL())
  24. XCTAssertEqual(fetcher.request?.httpMethod, "DELETE")
  25. let httpResponse = HTTPURLResponse(
  26. url: (fetcher.request?.url)!,
  27. statusCode: 200,
  28. httpVersion: "HTTP/1.1",
  29. headerFields: nil
  30. )
  31. response(httpResponse, nil, nil)
  32. }
  33. await StorageFetcherService.shared.updateTestBlock(testBlock)
  34. let path = objectPath()
  35. let ref = StorageReference(storage: storage(), path: path)
  36. do {
  37. let _ = try await ref.delete()
  38. } catch {
  39. // All testing is in test block.
  40. }
  41. }
  42. func testSuccessfulFetch() async {
  43. await StorageFetcherService.shared.updateTestBlock(successBlock())
  44. let path = objectPath()
  45. let ref = StorageReference(storage: storage(), path: path)
  46. do {
  47. let _ = try await ref.delete()
  48. } catch {
  49. // All testing is in test block.
  50. }
  51. }
  52. func testSuccessfulFetchWithEmulator() async {
  53. let storage = self.storage()
  54. storage.useEmulator(withHost: "localhost", port: 8080)
  55. let testBlock = successBlock(
  56. withURL: URL(string: "http://localhost:8080/v0/b/bucket/o/object")!
  57. )
  58. await StorageFetcherService.shared.updateTestBlock(successBlock())
  59. let path = objectPath()
  60. let ref = StorageReference(storage: storage, path: path)
  61. do {
  62. let _ = try await ref.delete()
  63. } catch {
  64. // All testing is in test block.
  65. }
  66. }
  67. func testUnsuccessfulFetchUnauthenticated() async {
  68. let storage = storage()
  69. await StorageFetcherService.shared.updateTestBlock(unauthenticatedBlock())
  70. let path = objectPath()
  71. let ref = StorageReference(storage: storage, path: path)
  72. do {
  73. try await ref.delete()
  74. } catch {
  75. XCTAssertEqual((error as NSError).code, StorageErrorCode.unauthenticated.rawValue)
  76. }
  77. }
  78. func testUnsuccessfulFetchUnauthorized() async {
  79. let storage = storage()
  80. await StorageFetcherService.shared.updateTestBlock(unauthorizedBlock())
  81. let path = objectPath()
  82. let ref = StorageReference(storage: storage, path: path)
  83. do {
  84. try await ref.delete()
  85. } catch {
  86. XCTAssertEqual((error as NSError).code, StorageErrorCode.unauthorized.rawValue)
  87. }
  88. }
  89. func testUnsuccessfulFetchObjectDoesntExist() async {
  90. let storage = storage()
  91. await StorageFetcherService.shared.updateTestBlock(notFoundBlock())
  92. let path = objectPath()
  93. let ref = StorageReference(storage: storage, path: path)
  94. do {
  95. try await ref.delete()
  96. } catch {
  97. XCTAssertEqual((error as NSError).code, StorageErrorCode.objectNotFound.rawValue)
  98. }
  99. }
  100. }