diff --git a/FirebaseStorage/Sources/Internal/StorageFetcherService.swift b/FirebaseStorage/Sources/Internal/StorageFetcherService.swift index c7915efaa294..809d258f87d9 100644 --- a/FirebaseStorage/Sources/Internal/StorageFetcherService.swift +++ b/FirebaseStorage/Sources/Internal/StorageFetcherService.swift @@ -23,15 +23,16 @@ import Foundation /// Manage Storage's fetcherService @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *) actor StorageFetcherService { + static let shared = StorageFetcherService() + private var _fetcherService: GTMSessionFetcherService? - func fetcherService(_ storage: Storage) async -> GTMSessionFetcherService { + func service(_ storage: Storage) async -> GTMSessionFetcherService { if let _fetcherService { return _fetcherService } let app = storage.app - if let fetcherService = await FetcherServiceMap.shared.get(appName: app.name, - bucket: storage.storageBucket) { + if let fetcherService = getFromMap(appName: app.name, bucket: storage.storageBucket) { return fetcherService } else { let fetcherService = GTMSessionFetcherService() @@ -51,15 +52,14 @@ actor StorageFetcherService { fetcherService.allowLocalhostRequest = true fetcherService.allowedInsecureSchemes = ["http"] } - await FetcherServiceMap.shared.set(appName: app.name, bucket: storage.storageBucket, - fetcher: fetcherService) + setMap(appName: app.name, bucket: storage.storageBucket, fetcher: fetcherService) return fetcherService } } /// Update the testBlock for unit testing. Save it as a property since this may be called before /// fetcherService is initialized. - func updateTestBlock(_ block: @escaping GTMSessionFetcherTestBlock) { + func updateTestBlock(_ block: GTMSessionFetcherTestBlock?) { testBlock = block if let _fetcherService { _fetcherService.testBlock = testBlock @@ -82,20 +82,13 @@ actor StorageFetcherService { } /// Map of apps to a dictionary of buckets to GTMSessionFetcherService. - private actor FetcherServiceMap { - static let shared = FetcherServiceMap() - - var fetcherServiceMap: [String: [String: GTMSessionFetcherService]] = [:] + private var fetcherServiceMap: [String: [String: GTMSessionFetcherService]] = [:] - func get(appName: String, bucket: String) -> GTMSessionFetcherService? { - return fetcherServiceMap[appName]?[bucket] - } + private func getFromMap(appName: String, bucket: String) -> GTMSessionFetcherService? { + return fetcherServiceMap[appName]?[bucket] + } - func set(appName: String, bucket: String, fetcher: GTMSessionFetcherService) { - if fetcherServiceMap[appName] == nil { - fetcherServiceMap[appName] = [:] - } - fetcherServiceMap[appName]?[bucket] = fetcher - } + private func setMap(appName: String, bucket: String, fetcher: GTMSessionFetcherService) { + fetcherServiceMap[appName, default: [:]][bucket] = fetcher } } diff --git a/FirebaseStorage/Sources/Internal/StorageInternalTask.swift b/FirebaseStorage/Sources/Internal/StorageInternalTask.swift index 82429009e29b..bf2e858cd6e1 100644 --- a/FirebaseStorage/Sources/Internal/StorageInternalTask.swift +++ b/FirebaseStorage/Sources/Internal/StorageInternalTask.swift @@ -38,9 +38,7 @@ class StorageInternalTask: StorageTask { dispatchQueue.async { [self] in self.state = .queueing Task { - let fetcherService = await reference.storage.fetcherService - .fetcherService(reference.storage) - + let fetcherService = await StorageFetcherService.shared.service(reference.storage) var request = request ?? self.baseRequest request.httpMethod = httpMethod request.timeoutInterval = self.reference.storage.maxOperationRetryTime diff --git a/FirebaseStorage/Sources/Storage.swift b/FirebaseStorage/Sources/Storage.swift index c9a33522b031..a6c62853ecbf 100644 --- a/FirebaseStorage/Sources/Storage.swift +++ b/FirebaseStorage/Sources/Storage.swift @@ -249,8 +249,6 @@ import FirebaseCore // MARK: - Internal and Private APIs - let fetcherService = StorageFetcherService() - let dispatchQueue: DispatchQueue init(app: FirebaseApp, bucket: String) { diff --git a/FirebaseStorage/Sources/StorageDownloadTask.swift b/FirebaseStorage/Sources/StorageDownloadTask.swift index 0f0776c777a7..70a2e64629e9 100644 --- a/FirebaseStorage/Sources/StorageDownloadTask.swift +++ b/FirebaseStorage/Sources/StorageDownloadTask.swift @@ -121,7 +121,7 @@ open class StorageDownloadTask: StorageObservableTask, StorageTaskManagement { fetcher = GTMSessionFetcher(downloadResumeData: resumeData) fetcher.comment = "Resuming DownloadTask" } else { - let fetcherService = await reference.storage.fetcherService.fetcherService(reference.storage) + let fetcherService = await StorageFetcherService.shared.service(reference.storage) fetcher = fetcherService.fetcher(with: request) fetcher.comment = "Starting DownloadTask" diff --git a/FirebaseStorage/Sources/StorageUploadTask.swift b/FirebaseStorage/Sources/StorageUploadTask.swift index 5870d4e36893..c1ed2ccfd79a 100644 --- a/FirebaseStorage/Sources/StorageUploadTask.swift +++ b/FirebaseStorage/Sources/StorageUploadTask.swift @@ -59,8 +59,7 @@ import Foundation let bodyData = try? JSONSerialization.data(withJSONObject: dataRepresentation) Task { - let fetcherService = await reference.storage.fetcherService - .fetcherService(reference.storage) + let fetcherService = await StorageFetcherService.shared.service(reference.storage) var request = self.baseRequest request.httpMethod = "POST" request.timeoutInterval = self.reference.storage.maxUploadRetryTime diff --git a/FirebaseStorage/Tests/Unit/StorageAuthorizerTests.swift b/FirebaseStorage/Tests/Unit/StorageAuthorizerTests.swift index 3b340179d96d..f5b16cab63db 100644 --- a/FirebaseStorage/Tests/Unit/StorageAuthorizerTests.swift +++ b/FirebaseStorage/Tests/Unit/StorageAuthorizerTests.swift @@ -23,7 +23,6 @@ class StorageAuthorizerTests: StorageTestHelpers { var appCheckTokenSuccess: FIRAppCheckTokenResultFake! var appCheckTokenError: FIRAppCheckTokenResultFake! var fetcher: GTMSessionFetcher! - var fetcherService: GTMSessionFetcherService! var auth: FIRAuthInteropFake! var appCheck: FIRAppCheckFake! @@ -52,7 +51,6 @@ class StorageAuthorizerTests: StorageTestHelpers { override func tearDown() { fetcher = nil - fetcherService = nil auth = nil appCheck = nil appCheckTokenSuccess = nil diff --git a/FirebaseStorage/Tests/Unit/StorageDeleteTests.swift b/FirebaseStorage/Tests/Unit/StorageDeleteTests.swift index 1f6a2ed4efd3..3a1bc9dd853c 100644 --- a/FirebaseStorage/Tests/Unit/StorageDeleteTests.swift +++ b/FirebaseStorage/Tests/Unit/StorageDeleteTests.swift @@ -19,29 +19,9 @@ import XCTest @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *) class StorageDeleteTests: StorageTestHelpers { - var fetcherService: GTMSessionFetcherService? - var dispatchQueue: DispatchQueue? - - override func setUp() { - super.setUp() - fetcherService = GTMSessionFetcherService() - fetcherService?.authorizer = StorageTokenAuthorizer( - googleAppID: "dummyAppID", - authProvider: nil, - appCheck: nil - ) - dispatchQueue = DispatchQueue(label: "Test dispatch queue") - } - - override func tearDown() { - fetcherService = nil - super.tearDown() - } - - func testFetcherConfiguration() { - let expectation = self.expectation(description: #function) - fetcherService!.testBlock = { (fetcher: GTMSessionFetcher!, - response: GTMSessionFetcherTestResponse) in + func testFetcherConfiguration() async { + let testBlock = { (fetcher: GTMSessionFetcher!, + response: GTMSessionFetcherTestResponse) in XCTAssertEqual(fetcher.request?.url, self.objectURL()) XCTAssertEqual(fetcher.request?.httpMethod, "DELETE") let httpResponse = HTTPURLResponse( @@ -52,67 +32,46 @@ class StorageDeleteTests: StorageTestHelpers { ) response(httpResponse, nil, nil) } + await StorageFetcherService.shared.updateTestBlock(testBlock) let path = objectPath() let ref = StorageReference(storage: storage(), path: path) - StorageDeleteTask.deleteTask( - reference: ref, - queue: dispatchQueue!.self - ) { _, error in - expectation.fulfill() + do { + let _ = try await ref.delete() + } catch { + // All testing is in test block. } - waitForExpectation(test: self) } - func testSuccessfulFetch() { - let expectation = self.expectation(description: #function) - fetcherService!.testBlock = { (fetcher: GTMSessionFetcher!, - response: GTMSessionFetcherTestResponse) in - XCTAssertEqual(fetcher.request?.url, self.objectURL()) - XCTAssertEqual(fetcher.request?.httpMethod, "DELETE") - let httpResponse = HTTPURLResponse( - url: (fetcher.request?.url)!, - statusCode: 200, - httpVersion: "HTTP/1.1", - headerFields: nil - ) - response(httpResponse, nil, nil) - } + func testSuccessfulFetch() async { + await StorageFetcherService.shared.updateTestBlock(successBlock()) let path = objectPath() let ref = StorageReference(storage: storage(), path: path) - StorageDeleteTask.deleteTask( - reference: ref, - queue: dispatchQueue!.self - ) { _, error in - expectation.fulfill() + do { + let _ = try await ref.delete() + } catch { + // All testing is in test block. } - waitForExpectation(test: self) } - func testSuccessfulFetchWithEmulator() { - let expectation = self.expectation(description: #function) + func testSuccessfulFetchWithEmulator() async { let storage = self.storage() storage.useEmulator(withHost: "localhost", port: 8080) - fetcherService?.allowLocalhostRequest = true - - fetcherService! - .testBlock = successBlock( - withURL: URL(string: "http://localhost:8080/v0/b/bucket/o/object")! - ) - + let testBlock = successBlock( + withURL: URL(string: "http://localhost:8080/v0/b/bucket/o/object")! + ) + await StorageFetcherService.shared.updateTestBlock(successBlock()) let path = objectPath() let ref = StorageReference(storage: storage, path: path) - StorageDeleteTask.deleteTask( - reference: ref, - queue: dispatchQueue!.self - ) { _, error in - expectation.fulfill() + do { + let _ = try await ref.delete() + } catch { + // All testing is in test block. } - waitForExpectation(test: self) } func testUnsuccessfulFetchUnauthenticated() async { let storage = storage() - await storage.fetcherService.updateTestBlock(unauthenticatedBlock()) + await StorageFetcherService.shared.updateTestBlock(unauthenticatedBlock()) let path = objectPath() let ref = StorageReference(storage: storage, path: path) do { @@ -124,7 +83,7 @@ class StorageDeleteTests: StorageTestHelpers { func testUnsuccessfulFetchUnauthorized() async { let storage = storage() - await storage.fetcherService.updateTestBlock(unauthorizedBlock()) + await StorageFetcherService.shared.updateTestBlock(unauthorizedBlock()) let path = objectPath() let ref = StorageReference(storage: storage, path: path) do { @@ -136,7 +95,7 @@ class StorageDeleteTests: StorageTestHelpers { func testUnsuccessfulFetchObjectDoesntExist() async { let storage = storage() - await storage.fetcherService.updateTestBlock(notFoundBlock()) + await StorageFetcherService.shared.updateTestBlock(notFoundBlock()) let path = objectPath() let ref = StorageReference(storage: storage, path: path) do { diff --git a/FirebaseStorage/Tests/Unit/StorageGetMetadataTests.swift b/FirebaseStorage/Tests/Unit/StorageGetMetadataTests.swift index a2428d792fc8..96f5e0fe87ff 100644 --- a/FirebaseStorage/Tests/Unit/StorageGetMetadataTests.swift +++ b/FirebaseStorage/Tests/Unit/StorageGetMetadataTests.swift @@ -19,29 +19,9 @@ import XCTest @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *) class StorageGetMetadataTests: StorageTestHelpers { - var fetcherService: GTMSessionFetcherService? - var dispatchQueue: DispatchQueue? - - override func setUp() { - super.setUp() - fetcherService = GTMSessionFetcherService() - fetcherService?.authorizer = StorageTokenAuthorizer( - googleAppID: "dummyAppID", - authProvider: nil, - appCheck: nil - ) - dispatchQueue = DispatchQueue(label: "Test dispatch queue") - } - - override func tearDown() { - fetcherService = nil - super.tearDown() - } - - func testFetcherConfiguration() { - let expectation = self.expectation(description: #function) - fetcherService!.testBlock = { (fetcher: GTMSessionFetcher!, - response: GTMSessionFetcherTestResponse) in + func testFetcherConfiguration() async { + let testBlock = { (fetcher: GTMSessionFetcher!, + response: GTMSessionFetcherTestResponse) in XCTAssertEqual(fetcher.request?.url, self.objectURL()) XCTAssertEqual(fetcher.request?.httpMethod, "GET") let httpResponse = HTTPURLResponse( @@ -52,67 +32,44 @@ class StorageGetMetadataTests: StorageTestHelpers { ) response(httpResponse, nil, nil) } - let path = objectPath() - let ref = StorageReference(storage: storage(), path: path) - StorageGetMetadataTask.getMetadataTask( - reference: ref, - queue: dispatchQueue!.self - ) { metadata, error in - expectation.fulfill() + await StorageFetcherService.shared.updateTestBlock(testBlock) + let ref = storage().reference(withPath: "object") + do { + let _ = try await ref.getMetadata() + } catch { + // All testing is in test block. } - waitForExpectation(test: self) } - func testSuccessfulFetch() { - let expectation = self.expectation(description: #function) - fetcherService!.testBlock = { (fetcher: GTMSessionFetcher!, - response: GTMSessionFetcherTestResponse) in - XCTAssertEqual(fetcher.request?.url, self.objectURL()) - 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) - StorageGetMetadataTask.getMetadataTask( - reference: ref, - queue: dispatchQueue!.self - ) { metadata, error in - expectation.fulfill() + func testSuccessfulFetch() async { + await StorageFetcherService.shared.updateTestBlock(successBlock()) + let ref = storage().reference(withPath: "object") + do { + let _ = try await ref.getMetadata() + } catch { + // All testing is in test block. } - waitForExpectation(test: self) } - func testSuccessfulFetchWithEmulator() { - let expectation = self.expectation(description: #function) + func testSuccessfulFetchWithEmulator() async { let storage = self.storage() storage.useEmulator(withHost: "localhost", port: 8080) - fetcherService?.allowLocalhostRequest = true - fetcherService! - .testBlock = successBlock( - withURL: URL(string: "http://localhost:8080/v0/b/bucket/o/object")! - ) - - let path = objectPath() - let ref = StorageReference(storage: storage, path: path) - StorageGetMetadataTask.getMetadataTask( - reference: ref, - queue: dispatchQueue!.self - ) { metadata, error in - expectation.fulfill() + let testBlock = successBlock( + withURL: URL(string: "http://localhost:8080/v0/b/bucket/o/object")! + ) + await StorageFetcherService.shared.updateTestBlock(testBlock) + let ref = storage.reference(withPath: "object") + do { + let _ = try await ref.getMetadata() + } catch { + // All testing is in test block. } - waitForExpectation(test: self) } func testUnsuccessfulFetchUnauthenticated() async { let storage = storage() - await storage.fetcherService.updateTestBlock(unauthenticatedBlock()) + await StorageFetcherService.shared.updateTestBlock(unauthenticatedBlock()) let path = objectPath() let ref = StorageReference(storage: storage, path: path) do { @@ -124,7 +81,7 @@ class StorageGetMetadataTests: StorageTestHelpers { func testUnsuccessfulFetchUnauthorized() async { let storage = storage() - await storage.fetcherService.updateTestBlock(unauthorizedBlock()) + await StorageFetcherService.shared.updateTestBlock(unauthorizedBlock()) let path = objectPath() let ref = StorageReference(storage: storage, path: path) do { @@ -136,7 +93,7 @@ class StorageGetMetadataTests: StorageTestHelpers { func testUnsuccessfulFetchObjectDoesntExist() async { let storage = storage() - await storage.fetcherService.updateTestBlock(notFoundBlock()) + await StorageFetcherService.shared.updateTestBlock(notFoundBlock()) let path = objectPath() let ref = StorageReference(storage: storage, path: path) do { @@ -146,21 +103,14 @@ class StorageGetMetadataTests: StorageTestHelpers { } } - func testUnsuccessfulFetchBadJSON() { - let expectation = self.expectation(description: #function) - - fetcherService!.testBlock = invalidJSONBlock() + func testUnsuccessfulFetchBadJSON() async { + await StorageFetcherService.shared.updateTestBlock(invalidJSONBlock()) let path = objectPath() let ref = StorageReference(storage: storage(), path: path) - StorageGetMetadataTask.getMetadataTask( - reference: ref, - queue: dispatchQueue!.self - ) { metadata, error in - XCTAssertNil(metadata) - let nsError = try! XCTUnwrap(error as? NSError) - XCTAssertEqual(nsError.code, StorageErrorCode.unknown.rawValue) - expectation.fulfill() + do { + let _ = try await ref.getMetadata() + } catch { + XCTAssertEqual((error as NSError).code, StorageErrorCode.unknown.rawValue) } - waitForExpectation(test: self) } } diff --git a/FirebaseStorage/Tests/Unit/StorageListTests.swift b/FirebaseStorage/Tests/Unit/StorageListTests.swift index 1b03ee62a180..37140f1262b0 100644 --- a/FirebaseStorage/Tests/Unit/StorageListTests.swift +++ b/FirebaseStorage/Tests/Unit/StorageListTests.swift @@ -19,25 +19,6 @@ import XCTest @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *) class StorageListTests: StorageTestHelpers { - var fetcherService: GTMSessionFetcherService? - var dispatchQueue: DispatchQueue? - - override func setUp() { - super.setUp() - fetcherService = GTMSessionFetcherService() - fetcherService?.authorizer = StorageTokenAuthorizer( - googleAppID: "dummyAppID", - 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 @@ -77,22 +58,21 @@ class StorageListTests: StorageTestHelpers { waitForExpectation(test: self) } - func testDefaultList() { - let expectation = self.expectation(description: #function) - - fetcherService?.testBlock = { (fetcher: GTMSessionFetcher, - response: GTMSessionFetcherTestResponse) in + func testDefaultList() async throws { + let 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) + XCTAssertEqual(queryItems.count, 3) for item in queryItems { switch item.name { case "prefix": XCTAssertEqual(item.value, "object/") case "delimiter": XCTAssertEqual(item.value, "/") + case "maxResults": XCTAssertEqual(item.value, "10") default: XCTFail("Unexpected URLComponent Query Item") } } @@ -103,18 +83,13 @@ class StorageListTests: StorageTestHelpers { headerFields: nil) response(httpResponse, nil, nil) } - - let path = objectPath() - let ref = StorageReference(storage: storage(), path: path) - StorageListTask.listTask( - reference: ref, - queue: dispatchQueue!.self, - pageSize: nil, - previousPageToken: nil - ) { result, error in - expectation.fulfill() + await StorageFetcherService.shared.updateTestBlock(testBlock) + let ref = storage().reference(withPath: "object") + do { + let _ = try await ref.list(maxResults: 10) + } catch { + // All testing is in test block. } - waitForExpectation(test: self) } func testDefaultListWithEmulator() async throws { @@ -145,16 +120,16 @@ class StorageListTests: StorageTestHelpers { headerFields: nil) response(httpResponse, "{}".data(using: .utf8), nil) } - await storage.fetcherService.updateTestBlock(testBlock) + await StorageFetcherService.shared.updateTestBlock(testBlock) let ref = storage.reference(withPath: "object") - let _ = try await ref.list(maxResults: 123) + let result = try await ref.list(maxResults: 123) + XCTAssertEqual(result.items, []) } - func testListWithPageSizeAndPageToken() { - let expectation = self.expectation(description: #function) - - fetcherService?.testBlock = { (fetcher: GTMSessionFetcher, - response: GTMSessionFetcherTestResponse) in + func testListWithPageSizeAndPageToken() async throws { + let storage = self.storage() + let testBlock = { (fetcher: GTMSessionFetcher, + response: GTMSessionFetcherTestResponse) in let url = fetcher.request!.url! XCTAssertEqual(url.scheme, "https") XCTAssertEqual(url.host, "firebasestorage.googleapis.com") @@ -179,35 +154,30 @@ class StorageListTests: StorageTestHelpers { response(httpResponse, nil, nil) } - let path = objectPath() - let ref = StorageReference(storage: storage(), path: path) - StorageListTask.listTask( - reference: ref, - queue: dispatchQueue!.self, - pageSize: 42, - previousPageToken: "foo" - ) { result, error in - expectation.fulfill() + await StorageFetcherService.shared.updateTestBlock(testBlock) + let ref = storage.reference(withPath: "object") + do { + let _ = try await ref.list(maxResults: 42, pageToken: "foo") + } catch { + // All testing is in test block. } - waitForExpectation(test: self) } - func testPercentEncodesPlusToken() { - let expectation = self.expectation(description: #function) - - fetcherService?.testBlock = { (fetcher: GTMSessionFetcher, - response: GTMSessionFetcherTestResponse) in + func testPercentEncodesPlusToken() async { + let 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) + XCTAssertEqual(queryItems.count, 3) for item in queryItems { switch item.name { case "prefix": XCTAssertEqual(item.value, "+foo/") case "delimiter": XCTAssertEqual(item.value, "/") + case "maxResults": XCTAssertEqual(item.value, "97") default: XCTFail("Unexpected URLComponent Query Item") } } @@ -219,17 +189,14 @@ class StorageListTests: StorageTestHelpers { response(httpResponse, nil, nil) } + await StorageFetcherService.shared.updateTestBlock(testBlock) let storage = storage() let ref = storage.reference(withPath: "+foo") - StorageListTask.listTask( - reference: ref, - queue: dispatchQueue!.self, - pageSize: nil, - previousPageToken: nil - ) { result, error in - expectation.fulfill() + do { + let _ = try await ref.list(maxResults: 97) + } catch { + // All testing is in test block. } - waitForExpectation(test: self) } func testListWithResponse() async throws { @@ -262,7 +229,7 @@ class StorageListTests: StorageTestHelpers { } let storage = storage() - await storage.fetcherService.updateTestBlock(testBlock) + await StorageFetcherService.shared.updateTestBlock(testBlock) let ref = storage.reference(withPath: "object") let result = try await ref.list(maxResults: 1000) XCTAssertEqual(result.items, [ref.child("data1.dat"), ref.child("data2.dat")]) @@ -286,7 +253,7 @@ class StorageListTests: StorageTestHelpers { } let storage = storage() - await storage.fetcherService.updateTestBlock(testBlock) + await StorageFetcherService.shared.updateTestBlock(testBlock) let ref = storage.reference(withPath: "object") do { let _ = try await ref.list(maxResults: 1000) diff --git a/FirebaseStorage/Tests/Unit/StorageTestHelpers.swift b/FirebaseStorage/Tests/Unit/StorageTestHelpers.swift index 9e1cfeb29515..accda78c684a 100644 --- a/FirebaseStorage/Tests/Unit/StorageTestHelpers.swift +++ b/FirebaseStorage/Tests/Unit/StorageTestHelpers.swift @@ -40,6 +40,9 @@ class StorageTestHelpers: XCTestCase { FirebaseApp.configure(name: appName, options: options) app = FirebaseApp.app(name: appName) } + addTeardownBlock { + await StorageFetcherService.shared.updateTestBlock(nil) + } } func rootReference() -> StorageReference { diff --git a/FirebaseStorage/Tests/Unit/StorageUpdateMetadataTests.swift b/FirebaseStorage/Tests/Unit/StorageUpdateMetadataTests.swift index 7650032d1799..8019ee5bc611 100644 --- a/FirebaseStorage/Tests/Unit/StorageUpdateMetadataTests.swift +++ b/FirebaseStorage/Tests/Unit/StorageUpdateMetadataTests.swift @@ -19,26 +19,20 @@ import XCTest @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *) class StorageUpdateMetadataTests: StorageTestHelpers { - var fetcherService: GTMSessionFetcherService? - var dispatchQueue: DispatchQueue? var metadata: StorageMetadata! override func setUp() { super.setUp() metadata = StorageMetadata(dictionary: ["bucket": "bucket", "name": "path/to/object"]) - fetcherService = GTMSessionFetcherService() - dispatchQueue = DispatchQueue(label: "Test dispatch queue") } override func tearDown() { - fetcherService = nil super.tearDown() } - func testFetcherConfiguration() { - let expectation = self.expectation(description: #function) - fetcherService!.testBlock = { (fetcher: GTMSessionFetcher!, - response: GTMSessionFetcherTestResponse) in + func testFetcherConfiguration() async { + let testBlock = { (fetcher: GTMSessionFetcher!, + response: GTMSessionFetcherTestResponse) in XCTAssertEqual(fetcher.request?.url, self.objectURL()) XCTAssertEqual(fetcher.request?.httpMethod, "PATCH") let httpResponse = HTTPURLResponse( @@ -49,21 +43,18 @@ class StorageUpdateMetadataTests: StorageTestHelpers { ) response(httpResponse, nil, nil) } - let path = objectPath() - let ref = StorageReference(storage: storage(), path: path) - StorageUpdateMetadataTask.updateMetadataTask( - reference: ref, - queue: dispatchQueue!.self, - metadata: metadata! - ) { metadata, error in - expectation.fulfill() + await StorageFetcherService.shared.updateTestBlock(testBlock) + let ref = storage().reference(withPath: "object") + do { + let _ = try await ref.updateMetadata(metadata) + } catch { + // All testing is in test block. } - waitForExpectation(test: self) } func testSuccessfulFetch() async throws { let storage = storage() - await storage.fetcherService.updateTestBlock(successBlock(withMetadata: metadata)) + await StorageFetcherService.shared.updateTestBlock(successBlock(withMetadata: metadata)) let path = objectPath() let ref = StorageReference(storage: storage, path: path) let metadata = try await ref.updateMetadata(metadata) @@ -71,32 +62,25 @@ class StorageUpdateMetadataTests: StorageTestHelpers { XCTAssertEqual(self.metadata?.name, metadata.name) } - func testSuccessfulFetchWithEmulator() { - let expectation = self.expectation(description: #function) + func testSuccessfulFetchWithEmulator() async { let storage = self.storage() storage.useEmulator(withHost: "localhost", port: 8080) - fetcherService?.allowLocalhostRequest = true - fetcherService! - .testBlock = successBlock( - withURL: URL(string: "http://localhost:8080/v0/b/bucket/o/object")! - ) - - let path = objectPath() - let ref = StorageReference(storage: storage, path: path) - StorageUpdateMetadataTask.updateMetadataTask( - reference: ref, - queue: dispatchQueue!.self, - metadata: metadata! - ) { metadata, error in - expectation.fulfill() + let testBlock = successBlock( + withURL: URL(string: "http://localhost:8080/v0/b/bucket/o/object")! + ) + await StorageFetcherService.shared.updateTestBlock(testBlock) + let ref = storage.reference(withPath: "object") + do { + let _ = try await ref.updateMetadata(metadata) + } catch { + // All testing is in test block. } - waitForExpectation(test: self) } func testUnsuccessfulFetchUnauthenticated() async { let storage = storage() - await storage.fetcherService.updateTestBlock(unauthenticatedBlock()) + await StorageFetcherService.shared.updateTestBlock(unauthenticatedBlock()) let path = objectPath() let ref = StorageReference(storage: storage, path: path) do { @@ -108,7 +92,7 @@ class StorageUpdateMetadataTests: StorageTestHelpers { func testUnsuccessfulFetchUnauthorized() async { let storage = storage() - await storage.fetcherService.updateTestBlock(unauthorizedBlock()) + await StorageFetcherService.shared.updateTestBlock(unauthorizedBlock()) let path = objectPath() let ref = StorageReference(storage: storage, path: path) do { @@ -120,7 +104,7 @@ class StorageUpdateMetadataTests: StorageTestHelpers { func testUnsuccessfulFetchObjectDoesntExist() async { let storage = storage() - await storage.fetcherService.updateTestBlock(notFoundBlock()) + await StorageFetcherService.shared.updateTestBlock(notFoundBlock()) let path = objectPath() let ref = StorageReference(storage: storage, path: path) do { @@ -130,20 +114,13 @@ class StorageUpdateMetadataTests: StorageTestHelpers { } } - func testUnsuccessfulFetchBadJSON() { - let expectation = self.expectation(description: #function) - - fetcherService!.testBlock = invalidJSONBlock() - let path = objectPath() - let ref = StorageReference(storage: storage(), path: path) - StorageUpdateMetadataTask.updateMetadataTask( - reference: ref, - queue: dispatchQueue!.self, - metadata: metadata! - ) { metadata, error in - XCTAssertEqual((error as? NSError)!.code, StorageErrorCode.unknown.rawValue) - expectation.fulfill() + func testUnsuccessfulFetchBadJSON() async { + await StorageFetcherService.shared.updateTestBlock(invalidJSONBlock()) + let ref = storage().reference(withPath: "object") + do { + let _ = try await ref.updateMetadata(metadata) + } catch { + XCTAssertEqual((error as NSError).code, StorageErrorCode.unknown.rawValue) } - waitForExpectation(test: self) } }