StorageGetMetadataTests.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 StorageGetMetadataTests: 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, "GET")
  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 ref = storage().reference(withPath: "object")
  35. do {
  36. let _ = try await ref.getMetadata()
  37. } catch {
  38. // All testing is in test block.
  39. }
  40. }
  41. func testSuccessfulFetch() async {
  42. await StorageFetcherService.shared.updateTestBlock(successBlock())
  43. let ref = storage().reference(withPath: "object")
  44. do {
  45. let _ = try await ref.getMetadata()
  46. } catch {
  47. // All testing is in test block.
  48. }
  49. }
  50. func testSuccessfulFetchWithEmulator() async {
  51. let storage = self.storage()
  52. storage.useEmulator(withHost: "localhost", port: 8080)
  53. let testBlock = successBlock(
  54. withURL: URL(string: "http://localhost:8080/v0/b/bucket/o/object")!
  55. )
  56. await StorageFetcherService.shared.updateTestBlock(testBlock)
  57. let ref = storage.reference(withPath: "object")
  58. do {
  59. let _ = try await ref.getMetadata()
  60. } catch {
  61. // All testing is in test block.
  62. }
  63. }
  64. func testUnsuccessfulFetchUnauthenticated() async {
  65. let storage = storage()
  66. await StorageFetcherService.shared.updateTestBlock(unauthenticatedBlock())
  67. let path = objectPath()
  68. let ref = StorageReference(storage: storage, path: path)
  69. do {
  70. let _ = try await ref.getMetadata()
  71. } catch {
  72. XCTAssertEqual((error as NSError).code, StorageErrorCode.unauthenticated.rawValue)
  73. }
  74. }
  75. func testUnsuccessfulFetchUnauthorized() async {
  76. let storage = storage()
  77. await StorageFetcherService.shared.updateTestBlock(unauthorizedBlock())
  78. let path = objectPath()
  79. let ref = StorageReference(storage: storage, path: path)
  80. do {
  81. let _ = try await ref.getMetadata()
  82. } catch {
  83. XCTAssertEqual((error as NSError).code, StorageErrorCode.unauthorized.rawValue)
  84. }
  85. }
  86. func testUnsuccessfulFetchObjectDoesntExist() async {
  87. let storage = storage()
  88. await StorageFetcherService.shared.updateTestBlock(notFoundBlock())
  89. let path = objectPath()
  90. let ref = StorageReference(storage: storage, path: path)
  91. do {
  92. let _ = try await ref.getMetadata()
  93. } catch {
  94. XCTAssertEqual((error as NSError).code, StorageErrorCode.objectNotFound.rawValue)
  95. }
  96. }
  97. func testUnsuccessfulFetchBadJSON() async {
  98. await StorageFetcherService.shared.updateTestBlock(invalidJSONBlock())
  99. let path = objectPath()
  100. let ref = StorageReference(storage: storage(), path: path)
  101. do {
  102. let _ = try await ref.getMetadata()
  103. } catch {
  104. XCTAssertEqual((error as NSError).code, StorageErrorCode.unknown.rawValue)
  105. }
  106. }
  107. }