StorageListTests.swift 12 KB

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