| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- // Copyright 2022 Google LLC
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- import Foundation
- @testable import FirebaseStorage
- import GTMSessionFetcherCore
- import XCTest
- class StorageListTests: StorageTestHelpers {
- var fetcherService: GTMSessionFetcherService?
- var dispatchQueue: DispatchQueue?
- override func setUp() {
- super.setUp()
- fetcherService = GTMSessionFetcherService()
- fetcherService?.authorizer = StorageTokenAuthorizer(
- googleAppID: "dummyAppID",
- fetcherService: fetcherService!,
- authProvider: nil,
- appCheck: nil
- )
- dispatchQueue = DispatchQueue(label: "Test dispatch queue")
- }
- override func tearDown() {
- fetcherService = nil
- super.tearDown()
- }
- func testValidatesInput() {
- let expectation = self.expectation(description: #function)
- expectation.expectedFulfillmentCount = 4
- let errorBlock = { (result: StorageListResult?, error: Error?) in
- XCTAssertNil(result)
- XCTAssertNotNil(error)
- let nsError = error! as NSError
- XCTAssertEqual(nsError.domain, "FIRStorageErrorDomain")
- XCTAssertEqual(nsError.code, StorageErrorCode.invalidArgument.rawValue)
- expectation.fulfill()
- }
- let ref = storage().reference(withPath: "object")
- ref.list(maxResults: 0, completion: errorBlock)
- ref.list(maxResults: 1001, completion: errorBlock)
- ref.list(maxResults: 0, pageToken: "foo", completion: errorBlock)
- ref.list(maxResults: 1001, pageToken: "foo", completion: errorBlock)
- waitForExpectation(test: self)
- }
- func testListAllCallbackOnlyCalledOnce() {
- let expectation = self.expectation(description: #function)
- expectation.expectedFulfillmentCount = 1
- let errorBlock = { (result: StorageListResult?, error: Error?) in
- XCTAssertNil(result)
- XCTAssertNotNil(error)
- let nsError = error! as NSError
- XCTAssertEqual(nsError.domain, "FIRStorageErrorDomain")
- XCTAssertEqual(nsError.code, StorageErrorCode.unknown.rawValue)
- expectation.fulfill()
- }
- let ref = storage().reference(withPath: "object")
- ref.listAll(completion: errorBlock)
- waitForExpectation(test: self)
- }
- func testDefaultList() {
- let expectation = self.expectation(description: #function)
- fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
- response: GTMSessionFetcherTestResponse) in
- let url = fetcher.request!.url!
- XCTAssertEqual(url.scheme, "https")
- XCTAssertEqual(url.host, "firebasestorage.googleapis.com")
- XCTAssertEqual(url.port, 443)
- XCTAssertEqual(url.path, "/v0/b/bucket/o")
- let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)!.queryItems!
- XCTAssertEqual(queryItems.count, 2)
- for item in queryItems {
- switch item.name {
- case "prefix": XCTAssertEqual(item.value, "object/")
- case "delimiter": XCTAssertEqual(item.value, "/")
- default: XCTFail("Unexpected URLComponent Query Item")
- }
- }
- XCTAssertEqual(fetcher.request?.httpMethod, "GET")
- let httpResponse = HTTPURLResponse(url: (fetcher.request?.url)!,
- statusCode: 200,
- httpVersion: "HTTP/1.1",
- headerFields: nil)
- response(httpResponse, nil, nil)
- }
- let path = objectPath()
- let ref = StorageReference(storage: storage(), path: path)
- let task = StorageListTask(
- reference: ref,
- fetcherService: fetcherService!.self,
- queue: dispatchQueue!.self,
- pageSize: nil,
- previousPageToken: nil
- ) { result, error in
- expectation.fulfill()
- }
- task.enqueue()
- waitForExpectation(test: self)
- }
- func testDefaultListWithEmulator() {
- let expectation = self.expectation(description: #function)
- let storage = self.storage()
- storage.useEmulator(withHost: "localhost", port: 8080)
- fetcherService?.allowLocalhostRequest = true
- fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
- response: GTMSessionFetcherTestResponse) in
- let url = fetcher.request!.url!
- XCTAssertEqual(url.scheme, "http")
- XCTAssertEqual(url.host, "localhost")
- XCTAssertEqual(url.port, 8080)
- XCTAssertEqual(url.path, "/v0/b/bucket/o")
- let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)!.queryItems!
- XCTAssertEqual(queryItems.count, 2)
- for item in queryItems {
- switch item.name {
- case "prefix": XCTAssertEqual(item.value, "object/")
- case "delimiter": XCTAssertEqual(item.value, "/")
- default: XCTFail("Unexpected URLComponent Query Item")
- }
- }
- XCTAssertEqual(fetcher.request?.httpMethod, "GET")
- let httpResponse = HTTPURLResponse(url: (fetcher.request?.url)!,
- statusCode: 200,
- httpVersion: "HTTP/1.1",
- headerFields: nil)
- response(httpResponse, "{}".data(using: .utf8), nil)
- }
- let path = objectPath()
- let ref = StorageReference(storage: storage, path: path)
- let task = StorageListTask(
- reference: ref,
- fetcherService: fetcherService!.self,
- queue: dispatchQueue!.self,
- pageSize: nil,
- previousPageToken: nil
- ) { result, error in
- XCTAssertNil(error)
- expectation.fulfill()
- }
- task.enqueue()
- waitForExpectation(test: self)
- }
- func testListWithPageSizeAndPageToken() {
- let expectation = self.expectation(description: #function)
- fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
- response: GTMSessionFetcherTestResponse) in
- let url = fetcher.request!.url!
- XCTAssertEqual(url.scheme, "https")
- XCTAssertEqual(url.host, "firebasestorage.googleapis.com")
- XCTAssertEqual(url.port, 443)
- XCTAssertEqual(url.path, "/v0/b/bucket/o")
- let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)!.queryItems!
- XCTAssertEqual(queryItems.count, 4)
- for item in queryItems {
- switch item.name {
- case "prefix": XCTAssertEqual(item.value, "object/")
- case "delimiter": XCTAssertEqual(item.value, "/")
- case "pageToken": XCTAssertEqual(item.value, "foo")
- case "maxResults": XCTAssertEqual(item.value, "42")
- default: XCTFail("Unexpected URLComponent Query Item")
- }
- }
- XCTAssertEqual(fetcher.request?.httpMethod, "GET")
- let httpResponse = HTTPURLResponse(url: (fetcher.request?.url)!,
- statusCode: 200,
- httpVersion: "HTTP/1.1",
- headerFields: nil)
- response(httpResponse, nil, nil)
- }
- let path = objectPath()
- let ref = StorageReference(storage: storage(), path: path)
- let task = StorageListTask(
- reference: ref,
- fetcherService: fetcherService!.self,
- queue: dispatchQueue!.self,
- pageSize: 42,
- previousPageToken: "foo"
- ) { result, error in
- expectation.fulfill()
- }
- task.enqueue()
- waitForExpectation(test: self)
- }
- func testPercentEncodesPlusToken() {
- let expectation = self.expectation(description: #function)
- fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
- response: GTMSessionFetcherTestResponse) in
- let url = fetcher.request!.url!
- XCTAssertEqual(url.scheme, "https")
- XCTAssertEqual(url.host, "firebasestorage.googleapis.com")
- XCTAssertEqual(url.port, 443)
- XCTAssertEqual(url.path, "/v0/b/bucket/o")
- let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)!.queryItems!
- XCTAssertEqual(queryItems.count, 2)
- for item in queryItems {
- switch item.name {
- case "prefix": XCTAssertEqual(item.value, "+foo/")
- case "delimiter": XCTAssertEqual(item.value, "/")
- default: XCTFail("Unexpected URLComponent Query Item")
- }
- }
- XCTAssertEqual(fetcher.request?.httpMethod, "GET")
- let httpResponse = HTTPURLResponse(url: (fetcher.request?.url)!,
- statusCode: 200,
- httpVersion: "HTTP/1.1",
- headerFields: nil)
- response(httpResponse, nil, nil)
- }
- let storage = storage()
- let ref = storage.reference(withPath: "+foo")
- let task = StorageListTask(
- reference: ref,
- fetcherService: fetcherService!.self,
- queue: dispatchQueue!.self,
- pageSize: nil,
- previousPageToken: nil
- ) { result, error in
- expectation.fulfill()
- }
- task.enqueue()
- waitForExpectation(test: self)
- }
- func testListWithResponse() throws {
- let expectation = self.expectation(description: #function)
- let jsonString = "{\n" +
- " \"prefixes\": [\n" +
- " \"object/prefixWithoutSlash\",\n" +
- " \"object/prefixWithSlash/\"\n" +
- " ],\n" +
- " \"items\": [\n" +
- " {\n" +
- " \"name\": \"object/data1.dat\",\n" +
- " \"bucket\": \"bucket.appspot.com\"\n" +
- " },\n" +
- " {\n" +
- " \"name\": \"object/data2.dat\",\n" +
- " \"bucket\": \"bucket.appspot.com\"\n" +
- " },\n" +
- " ],\n" +
- " \"nextPageToken\": \"foo\"" +
- "}"
- let responseData = try XCTUnwrap(jsonString.data(using: .utf8))
- fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
- response: GTMSessionFetcherTestResponse) in
- let httpResponse = HTTPURLResponse(url: (fetcher.request?.url)!,
- statusCode: 200,
- httpVersion: "HTTP/1.1",
- headerFields: nil)
- response(httpResponse, responseData, nil)
- }
- let storage = storage()
- let ref = storage.reference(withPath: "object")
- let task = StorageListTask(
- reference: ref,
- fetcherService: fetcherService!.self,
- queue: dispatchQueue!.self,
- pageSize: nil,
- previousPageToken: nil
- ) { result, error in
- XCTAssertNotNil(result)
- XCTAssertNil(error)
- XCTAssertEqual(result?.items, [ref.child("data1.dat"), ref.child("data2.dat")])
- XCTAssertEqual(
- result?.prefixes,
- [ref.child("prefixWithoutSlash"), ref.child("prefixWithSlash")]
- )
- XCTAssertEqual(result?.pageToken, "foo")
- expectation.fulfill()
- }
- task.enqueue()
- waitForExpectation(test: self)
- }
- func testListWithErrorResponse() throws {
- let expectation = self.expectation(description: #function)
- let error = NSError(domain: "com.google.firebase.storage", code: 404)
- fetcherService?.testBlock = { (fetcher: GTMSessionFetcher,
- response: GTMSessionFetcherTestResponse) in
- let httpResponse = HTTPURLResponse(url: (fetcher.request?.url)!,
- statusCode: 403,
- httpVersion: "HTTP/1.1",
- headerFields: nil)
- response(httpResponse, nil, error)
- }
- let storage = storage()
- let ref = storage.reference(withPath: "object")
- let task = StorageListTask(
- reference: ref,
- fetcherService: fetcherService!.self,
- queue: dispatchQueue!.self,
- pageSize: nil,
- previousPageToken: nil
- ) { result, error in
- XCTAssertNotNil(error)
- XCTAssertNil(result)
- XCTAssertEqual(error!.domain, "FIRStorageErrorDomain")
- XCTAssertEqual(error!.code, StorageErrorCode.objectNotFound.rawValue)
- expectation.fulfill()
- }
- task.enqueue()
- waitForExpectation(test: self)
- }
- }
|