StorageGetMetadataTests.swift 6.1 KB

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