StorageGetMetadataTests.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. var fetcherService: GTMSessionFetcherService?
  21. var dispatchQueue: DispatchQueue?
  22. override func setUp() {
  23. super.setUp()
  24. fetcherService = GTMSessionFetcherService()
  25. fetcherService?.authorizer = StorageTokenAuthorizer(
  26. googleAppID: "dummyAppID",
  27. fetcherService: fetcherService!,
  28. authProvider: nil,
  29. appCheck: nil
  30. )
  31. dispatchQueue = DispatchQueue(label: "Test dispatch queue")
  32. }
  33. override func tearDown() {
  34. fetcherService = nil
  35. super.tearDown()
  36. }
  37. func testFetcherConfiguration() {
  38. let expectation = self.expectation(description: #function)
  39. fetcherService!.testBlock = { (fetcher: GTMSessionFetcher!,
  40. response: GTMSessionFetcherTestResponse) in
  41. XCTAssertEqual(fetcher.request?.url, self.objectURL())
  42. XCTAssertEqual(fetcher.request?.httpMethod, "GET")
  43. let httpResponse = HTTPURLResponse(
  44. url: (fetcher.request?.url)!,
  45. statusCode: 200,
  46. httpVersion: "HTTP/1.1",
  47. headerFields: nil
  48. )
  49. response(httpResponse, nil, nil)
  50. }
  51. let path = objectPath()
  52. let ref = StorageReference(storage: storage(), path: path)
  53. StorageGetMetadataTask.getMetadataTask(
  54. reference: ref,
  55. fetcherService: fetcherService!.self,
  56. queue: dispatchQueue!.self
  57. ) { metadata, error in
  58. expectation.fulfill()
  59. }
  60. waitForExpectation(test: self)
  61. }
  62. func testSuccessfulFetch() {
  63. let expectation = self.expectation(description: #function)
  64. fetcherService!.testBlock = { (fetcher: GTMSessionFetcher!,
  65. response: GTMSessionFetcherTestResponse) in
  66. XCTAssertEqual(fetcher.request?.url, self.objectURL())
  67. XCTAssertEqual(fetcher.request?.httpMethod, "GET")
  68. let httpResponse = HTTPURLResponse(
  69. url: (fetcher.request?.url)!,
  70. statusCode: 200,
  71. httpVersion: "HTTP/1.1",
  72. headerFields: nil
  73. )
  74. response(httpResponse, nil, nil)
  75. }
  76. let path = objectPath()
  77. let ref = StorageReference(storage: storage(), path: path)
  78. StorageGetMetadataTask.getMetadataTask(
  79. reference: ref,
  80. fetcherService: fetcherService!.self,
  81. queue: dispatchQueue!.self
  82. ) { metadata, error in
  83. expectation.fulfill()
  84. }
  85. waitForExpectation(test: self)
  86. }
  87. func testSuccessfulFetchWithEmulator() {
  88. let expectation = self.expectation(description: #function)
  89. let storage = self.storage()
  90. storage.useEmulator(withHost: "localhost", port: 8080)
  91. fetcherService?.allowLocalhostRequest = true
  92. fetcherService!
  93. .testBlock = successBlock(
  94. withURL: URL(string: "http://localhost:8080/v0/b/bucket/o/object")!
  95. )
  96. let path = objectPath()
  97. let ref = StorageReference(storage: storage, path: path)
  98. StorageGetMetadataTask.getMetadataTask(
  99. reference: ref,
  100. fetcherService: fetcherService!.self,
  101. queue: dispatchQueue!.self
  102. ) { metadata, error in
  103. expectation.fulfill()
  104. }
  105. waitForExpectation(test: self)
  106. }
  107. func testUnsuccessfulFetchUnauthenticated() {
  108. let expectation = self.expectation(description: #function)
  109. fetcherService!.testBlock = unauthenticatedBlock()
  110. let path = objectPath()
  111. let ref = StorageReference(storage: storage(), path: path)
  112. StorageGetMetadataTask.getMetadataTask(
  113. reference: ref,
  114. fetcherService: fetcherService!.self,
  115. queue: dispatchQueue!.self
  116. ) { metadata, error in
  117. XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.unauthenticated.rawValue)
  118. expectation.fulfill()
  119. }
  120. waitForExpectation(test: self)
  121. }
  122. func testUnsuccessfulFetchUnauthorized() {
  123. let expectation = self.expectation(description: #function)
  124. fetcherService!.testBlock = unauthorizedBlock()
  125. let path = objectPath()
  126. let ref = StorageReference(storage: storage(), path: path)
  127. StorageGetMetadataTask.getMetadataTask(
  128. reference: ref,
  129. fetcherService: fetcherService!.self,
  130. queue: dispatchQueue!.self
  131. ) { metadata, error in
  132. XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.unauthorized.rawValue)
  133. expectation.fulfill()
  134. }
  135. waitForExpectation(test: self)
  136. }
  137. func testUnsuccessfulFetchObjectDoesntExist() {
  138. let expectation = self.expectation(description: #function)
  139. fetcherService!.testBlock = notFoundBlock()
  140. let path = objectPath()
  141. let ref = StorageReference(storage: storage(), path: path)
  142. StorageGetMetadataTask.getMetadataTask(
  143. reference: ref,
  144. fetcherService: fetcherService!.self,
  145. queue: dispatchQueue!.self
  146. ) { metadata, error in
  147. XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.objectNotFound.rawValue)
  148. expectation.fulfill()
  149. }
  150. waitForExpectation(test: self)
  151. }
  152. func testUnsuccessfulFetchBadJSON() {
  153. let expectation = self.expectation(description: #function)
  154. fetcherService!.testBlock = invalidJSONBlock()
  155. let path = objectPath()
  156. let ref = StorageReference(storage: storage(), path: path)
  157. StorageGetMetadataTask.getMetadataTask(
  158. reference: ref,
  159. fetcherService: fetcherService!.self,
  160. queue: dispatchQueue!.self
  161. ) { metadata, error in
  162. XCTAssertNil(metadata)
  163. let nsError = try! XCTUnwrap(error as? NSError)
  164. XCTAssertEqual(nsError.code, StorageErrorCode.unknown.rawValue)
  165. expectation.fulfill()
  166. }
  167. waitForExpectation(test: self)
  168. }
  169. }