StorageUpdateMetadataTests.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 StorageUpdateMetadataTests: StorageTestHelpers {
  20. var metadata: StorageMetadata!
  21. override func setUp() {
  22. super.setUp()
  23. metadata = StorageMetadata(dictionary: ["bucket": "bucket", "name": "path/to/object"])
  24. }
  25. override func tearDown() {
  26. super.tearDown()
  27. }
  28. func testFetcherConfiguration() async {
  29. let testBlock = { (fetcher: GTMSessionFetcher!,
  30. response: GTMSessionFetcherTestResponse) in
  31. XCTAssertEqual(fetcher.request?.url, self.objectURL())
  32. XCTAssertEqual(fetcher.request?.httpMethod, "PATCH")
  33. let httpResponse = HTTPURLResponse(
  34. url: (fetcher.request?.url)!,
  35. statusCode: 200,
  36. httpVersion: "HTTP/1.1",
  37. headerFields: nil
  38. )
  39. response(httpResponse, nil, nil)
  40. }
  41. await StorageFetcherService.shared.updateTestBlock(testBlock)
  42. let ref = storage().reference(withPath: "object")
  43. do {
  44. let _ = try await ref.updateMetadata(metadata)
  45. } catch {
  46. // All testing is in test block.
  47. }
  48. }
  49. func testSuccessfulFetch() async throws {
  50. let storage = storage()
  51. await StorageFetcherService.shared.updateTestBlock(successBlock(withMetadata: metadata))
  52. let path = objectPath()
  53. let ref = StorageReference(storage: storage, path: path)
  54. let metadata = try await ref.updateMetadata(metadata)
  55. XCTAssertEqual(self.metadata?.bucket, metadata.bucket)
  56. XCTAssertEqual(self.metadata?.name, metadata.name)
  57. }
  58. func testSuccessfulFetchWithEmulator() async {
  59. let storage = self.storage()
  60. storage.useEmulator(withHost: "localhost", port: 8080)
  61. let testBlock = successBlock(
  62. withURL: URL(string: "http://localhost:8080/v0/b/bucket/o/object")!
  63. )
  64. await StorageFetcherService.shared.updateTestBlock(testBlock)
  65. let ref = storage.reference(withPath: "object")
  66. do {
  67. let _ = try await ref.updateMetadata(metadata)
  68. } catch {
  69. // All testing is in test block.
  70. }
  71. }
  72. func testUnsuccessfulFetchUnauthenticated() async {
  73. let storage = storage()
  74. await StorageFetcherService.shared.updateTestBlock(unauthenticatedBlock())
  75. let path = objectPath()
  76. let ref = StorageReference(storage: storage, path: path)
  77. do {
  78. let _ = try await ref.updateMetadata(metadata)
  79. } catch {
  80. XCTAssertEqual((error as NSError).code, StorageErrorCode.unauthenticated.rawValue)
  81. }
  82. }
  83. func testUnsuccessfulFetchUnauthorized() async {
  84. let storage = storage()
  85. await StorageFetcherService.shared.updateTestBlock(unauthorizedBlock())
  86. let path = objectPath()
  87. let ref = StorageReference(storage: storage, path: path)
  88. do {
  89. let _ = try await ref.updateMetadata(metadata)
  90. } catch {
  91. XCTAssertEqual((error as NSError).code, StorageErrorCode.unauthorized.rawValue)
  92. }
  93. }
  94. func testUnsuccessfulFetchObjectDoesntExist() async {
  95. let storage = storage()
  96. await StorageFetcherService.shared.updateTestBlock(notFoundBlock())
  97. let path = objectPath()
  98. let ref = StorageReference(storage: storage, path: path)
  99. do {
  100. let _ = try await ref.updateMetadata(metadata)
  101. } catch {
  102. XCTAssertEqual((error as NSError).code, StorageErrorCode.objectNotFound.rawValue)
  103. }
  104. }
  105. func testUnsuccessfulFetchBadJSON() async {
  106. await StorageFetcherService.shared.updateTestBlock(invalidJSONBlock())
  107. let ref = storage().reference(withPath: "object")
  108. do {
  109. let _ = try await ref.updateMetadata(metadata)
  110. } catch {
  111. XCTAssertEqual((error as NSError).code, StorageErrorCode.unknown.rawValue)
  112. }
  113. }
  114. }