StorageUpdateMetadataTests.swift 6.0 KB

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