StorageListTests.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 StorageListTests: 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 testValidatesInput() {
  38. let expectation = self.expectation(description: #function)
  39. expectation.expectedFulfillmentCount = 4
  40. let errorBlock = { (result: StorageListResult?, error: Error?) in
  41. XCTAssertNil(result)
  42. XCTAssertNotNil(error)
  43. let nsError = error! as NSError
  44. XCTAssertEqual(nsError.domain, "FIRStorageErrorDomain")
  45. XCTAssertEqual(nsError.code, StorageErrorCode.invalidArgument.rawValue)
  46. expectation.fulfill()
  47. }
  48. let ref = storage().reference(withPath: "object")
  49. ref.list(maxResults: 0, completion: errorBlock)
  50. ref.list(maxResults: 1001, completion: errorBlock)
  51. ref.list(maxResults: 0, pageToken: "foo", completion: errorBlock)
  52. ref.list(maxResults: 1001, pageToken: "foo", completion: errorBlock)
  53. waitForExpectation(test: self)
  54. }
  55. func testListAllCallbackOnlyCalledOnce() {
  56. let expectation = self.expectation(description: #function)
  57. expectation.expectedFulfillmentCount = 1
  58. let errorBlock = { (result: StorageListResult?, error: Error?) in
  59. XCTAssertNil(result)
  60. XCTAssertNotNil(error)
  61. let nsError = error! as NSError
  62. XCTAssertEqual(nsError.domain, "FIRStorageErrorDomain")
  63. XCTAssertEqual(nsError.code, StorageErrorCode.unknown.rawValue)
  64. expectation.fulfill()
  65. }
  66. let ref = storage().reference(withPath: "object")
  67. ref.listAll(completion: errorBlock)
  68. waitForExpectation(test: self)
  69. }
  70. func testDefaultList() {
  71. let expectation = self.expectation(description: #function)
  72. fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
  73. response: GTMSessionFetcherTestResponse) in
  74. let url = fetcher.request!.url!
  75. XCTAssertEqual(url.scheme, "https")
  76. XCTAssertEqual(url.host, "firebasestorage.googleapis.com")
  77. XCTAssertEqual(url.port, 443)
  78. XCTAssertEqual(url.path, "/v0/b/bucket/o")
  79. let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)!.queryItems!
  80. XCTAssertEqual(queryItems.count, 2)
  81. for item in queryItems {
  82. switch item.name {
  83. case "prefix": XCTAssertEqual(item.value, "object/")
  84. case "delimiter": XCTAssertEqual(item.value, "/")
  85. default: XCTFail("Unexpected URLComponent Query Item")
  86. }
  87. }
  88. XCTAssertEqual(fetcher.request?.httpMethod, "GET")
  89. let httpResponse = HTTPURLResponse(url: (fetcher.request?.url)!,
  90. statusCode: 200,
  91. httpVersion: "HTTP/1.1",
  92. headerFields: nil)
  93. response(httpResponse, nil, nil)
  94. }
  95. let path = objectPath()
  96. let ref = StorageReference(storage: storage(), path: path)
  97. StorageListTask.listTask(
  98. reference: ref,
  99. fetcherService: fetcherService!.self,
  100. queue: dispatchQueue!.self,
  101. pageSize: nil,
  102. previousPageToken: nil
  103. ) { result, error in
  104. expectation.fulfill()
  105. }
  106. waitForExpectation(test: self)
  107. }
  108. func testDefaultListWithEmulator() {
  109. let expectation = self.expectation(description: #function)
  110. let storage = self.storage()
  111. storage.useEmulator(withHost: "localhost", port: 8080)
  112. fetcherService?.allowLocalhostRequest = true
  113. fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
  114. response: GTMSessionFetcherTestResponse) in
  115. let url = fetcher.request!.url!
  116. XCTAssertEqual(url.scheme, "http")
  117. XCTAssertEqual(url.host, "localhost")
  118. XCTAssertEqual(url.port, 8080)
  119. XCTAssertEqual(url.path, "/v0/b/bucket/o")
  120. let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)!.queryItems!
  121. XCTAssertEqual(queryItems.count, 2)
  122. for item in queryItems {
  123. switch item.name {
  124. case "prefix": XCTAssertEqual(item.value, "object/")
  125. case "delimiter": XCTAssertEqual(item.value, "/")
  126. default: XCTFail("Unexpected URLComponent Query Item")
  127. }
  128. }
  129. XCTAssertEqual(fetcher.request?.httpMethod, "GET")
  130. let httpResponse = HTTPURLResponse(url: (fetcher.request?.url)!,
  131. statusCode: 200,
  132. httpVersion: "HTTP/1.1",
  133. headerFields: nil)
  134. response(httpResponse, "{}".data(using: .utf8), nil)
  135. }
  136. let path = objectPath()
  137. let ref = StorageReference(storage: storage, path: path)
  138. StorageListTask.listTask(
  139. reference: ref,
  140. fetcherService: fetcherService!.self,
  141. queue: dispatchQueue!.self,
  142. pageSize: nil,
  143. previousPageToken: nil
  144. ) { result, error in
  145. XCTAssertNil(error)
  146. expectation.fulfill()
  147. }
  148. waitForExpectation(test: self)
  149. }
  150. func testListWithPageSizeAndPageToken() {
  151. let expectation = self.expectation(description: #function)
  152. fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
  153. response: GTMSessionFetcherTestResponse) in
  154. let url = fetcher.request!.url!
  155. XCTAssertEqual(url.scheme, "https")
  156. XCTAssertEqual(url.host, "firebasestorage.googleapis.com")
  157. XCTAssertEqual(url.port, 443)
  158. XCTAssertEqual(url.path, "/v0/b/bucket/o")
  159. let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)!.queryItems!
  160. XCTAssertEqual(queryItems.count, 4)
  161. for item in queryItems {
  162. switch item.name {
  163. case "prefix": XCTAssertEqual(item.value, "object/")
  164. case "delimiter": XCTAssertEqual(item.value, "/")
  165. case "pageToken": XCTAssertEqual(item.value, "foo")
  166. case "maxResults": XCTAssertEqual(item.value, "42")
  167. default: XCTFail("Unexpected URLComponent Query Item")
  168. }
  169. }
  170. XCTAssertEqual(fetcher.request?.httpMethod, "GET")
  171. let httpResponse = HTTPURLResponse(url: (fetcher.request?.url)!,
  172. statusCode: 200,
  173. httpVersion: "HTTP/1.1",
  174. headerFields: nil)
  175. response(httpResponse, nil, nil)
  176. }
  177. let path = objectPath()
  178. let ref = StorageReference(storage: storage(), path: path)
  179. StorageListTask.listTask(
  180. reference: ref,
  181. fetcherService: fetcherService!.self,
  182. queue: dispatchQueue!.self,
  183. pageSize: 42,
  184. previousPageToken: "foo"
  185. ) { result, error in
  186. expectation.fulfill()
  187. }
  188. waitForExpectation(test: self)
  189. }
  190. func testPercentEncodesPlusToken() {
  191. let expectation = self.expectation(description: #function)
  192. fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
  193. response: GTMSessionFetcherTestResponse) in
  194. let url = fetcher.request!.url!
  195. XCTAssertEqual(url.scheme, "https")
  196. XCTAssertEqual(url.host, "firebasestorage.googleapis.com")
  197. XCTAssertEqual(url.port, 443)
  198. XCTAssertEqual(url.path, "/v0/b/bucket/o")
  199. let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)!.queryItems!
  200. XCTAssertEqual(queryItems.count, 2)
  201. for item in queryItems {
  202. switch item.name {
  203. case "prefix": XCTAssertEqual(item.value, "+foo/")
  204. case "delimiter": XCTAssertEqual(item.value, "/")
  205. default: XCTFail("Unexpected URLComponent Query Item")
  206. }
  207. }
  208. XCTAssertEqual(fetcher.request?.httpMethod, "GET")
  209. let httpResponse = HTTPURLResponse(url: (fetcher.request?.url)!,
  210. statusCode: 200,
  211. httpVersion: "HTTP/1.1",
  212. headerFields: nil)
  213. response(httpResponse, nil, nil)
  214. }
  215. let storage = storage()
  216. let ref = storage.reference(withPath: "+foo")
  217. StorageListTask.listTask(
  218. reference: ref,
  219. fetcherService: fetcherService!.self,
  220. queue: dispatchQueue!.self,
  221. pageSize: nil,
  222. previousPageToken: nil
  223. ) { result, error in
  224. expectation.fulfill()
  225. }
  226. waitForExpectation(test: self)
  227. }
  228. func testListWithResponse() throws {
  229. let expectation = self.expectation(description: #function)
  230. let jsonString = "{\n" +
  231. " \"prefixes\": [\n" +
  232. " \"object/prefixWithoutSlash\",\n" +
  233. " \"object/prefixWithSlash/\"\n" +
  234. " ],\n" +
  235. " \"items\": [\n" +
  236. " {\n" +
  237. " \"name\": \"object/data1.dat\",\n" +
  238. " \"bucket\": \"bucket.appspot.com\"\n" +
  239. " },\n" +
  240. " {\n" +
  241. " \"name\": \"object/data2.dat\",\n" +
  242. " \"bucket\": \"bucket.appspot.com\"\n" +
  243. " },\n" +
  244. " ],\n" +
  245. " \"nextPageToken\": \"foo\"" +
  246. "}"
  247. let responseData = try XCTUnwrap(jsonString.data(using: .utf8))
  248. fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
  249. response: GTMSessionFetcherTestResponse) in
  250. let httpResponse = HTTPURLResponse(url: (fetcher.request?.url)!,
  251. statusCode: 200,
  252. httpVersion: "HTTP/1.1",
  253. headerFields: nil)
  254. response(httpResponse, responseData, nil)
  255. }
  256. let storage = storage()
  257. let ref = storage.reference(withPath: "object")
  258. StorageListTask.listTask(
  259. reference: ref,
  260. fetcherService: fetcherService!.self,
  261. queue: dispatchQueue!.self,
  262. pageSize: nil,
  263. previousPageToken: nil
  264. ) { result, error in
  265. XCTAssertNotNil(result)
  266. XCTAssertNil(error)
  267. XCTAssertEqual(result?.items, [ref.child("data1.dat"), ref.child("data2.dat")])
  268. XCTAssertEqual(
  269. result?.prefixes,
  270. [ref.child("prefixWithoutSlash"), ref.child("prefixWithSlash")]
  271. )
  272. XCTAssertEqual(result?.pageToken, "foo")
  273. expectation.fulfill()
  274. }
  275. waitForExpectation(test: self)
  276. }
  277. func testListWithErrorResponse() throws {
  278. let expectation = self.expectation(description: #function)
  279. let error = NSError(domain: "com.google.firebase.storage", code: 404)
  280. fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
  281. response: GTMSessionFetcherTestResponse) in
  282. let httpResponse = HTTPURLResponse(url: (fetcher.request?.url)!,
  283. statusCode: 403,
  284. httpVersion: "HTTP/1.1",
  285. headerFields: nil)
  286. response(httpResponse, nil, error)
  287. }
  288. let storage = storage()
  289. let ref = storage.reference(withPath: "object")
  290. StorageListTask.listTask(
  291. reference: ref,
  292. fetcherService: fetcherService!.self,
  293. queue: dispatchQueue!.self,
  294. pageSize: nil,
  295. previousPageToken: nil
  296. ) { result, error in
  297. XCTAssertNotNil(error)
  298. XCTAssertNil(result)
  299. XCTAssertEqual((error as? NSError)!.domain, "FIRStorageErrorDomain")
  300. XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.objectNotFound.rawValue)
  301. expectation.fulfill()
  302. }
  303. waitForExpectation(test: self)
  304. }
  305. }