From 0723a4338ea62cd75a9f2c9bc7b29e08523a30a9 Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Wed, 26 Jun 2024 11:34:32 +0200 Subject: [PATCH] refactor: Re-arrange code for readability --- .../ConfidenceClientEventsRemote.swift} | 2 +- .../ConfidenceClientOptions.swift | 0 .../ConfidenceClientResolveRemote.swift} | 2 +- .../ConfidenceClients.swift} | 7 ++- Sources/Confidence/Confidence.swift | 4 +- Sources/Confidence/EventSenderEngine.swift | 6 +-- .../ConfidenceAppLifecycleProducer.swift | 0 .../{ => Producers}/ConfidenceProducer.swift | 0 .../ConfidenceScreenTrackProducer.swift} | 8 +-- Sources/Confidence/Sdk.swift | 6 --- .../Confidence/TestHelpers/Extensions.swift | 52 ------------------- .../Confidence/{ => Utils}/Array+Chunks.swift | 0 Sources/Confidence/{ => Utils}/Backport.swift | 0 .../{ => Utils}/BaseUrlMapper.swift | 0 .../BufferedPassthroughSubject.swift | 0 .../CaseIterableDefaultsLast.swift | 0 .../{ => Utils}/ConfidenceValueHash.swift | 0 Sources/Confidence/{ => Utils}/FlagPath.swift | 0 .../{ => Utils}/HttpStatusCode+Error.swift | 0 .../{ => Utils}/PayloadMerger.swift | 0 .../Confidence/{ => Utils}/VisitorUtil.swift | 0 .../ConfidenceFeatureProvider.swift | 16 +++--- .../ConfidenceContextTests.swift | 20 +++---- Tests/ConfidenceTests/ConfidenceTest.swift | 13 +++++ .../EventSenderEngineTest.swift | 4 +- Tests/ConfidenceTests/EventUploaderMock.swift | 2 +- .../Helpers/ConfidenceClientMock.swift | 2 +- .../Helpers}/GrpcStatusCode.swift | 0 .../Helpers/MockedClientURLProtocol.swift | 30 +++++++++++ .../RemoteConfidenceClientTests.swift | 8 +-- .../RemoteResolveConfidenceClientTest.swift | 2 +- 31 files changed, 87 insertions(+), 97 deletions(-) rename Sources/Confidence/{RemoteConfidenceClient.swift => Client/ConfidenceClientEventsRemote.swift} (97%) rename Sources/Confidence/{ => Client}/ConfidenceClientOptions.swift (100%) rename Sources/Confidence/{RemoteResolveConfidenceClient.swift => Client/ConfidenceClientResolveRemote.swift} (98%) rename Sources/Confidence/{ConfidenceClient.swift => Client/ConfidenceClients.swift} (84%) rename Sources/Confidence/{ => Producers}/ConfidenceAppLifecycleProducer.swift (100%) rename Sources/Confidence/{ => Producers}/ConfidenceProducer.swift (100%) rename Sources/Confidence/{ConfidenceScreenTracker.swift => Producers/ConfidenceScreenTrackProducer.swift} (92%) delete mode 100644 Sources/Confidence/Sdk.swift delete mode 100644 Sources/Confidence/TestHelpers/Extensions.swift rename Sources/Confidence/{ => Utils}/Array+Chunks.swift (100%) rename Sources/Confidence/{ => Utils}/Backport.swift (100%) rename Sources/Confidence/{ => Utils}/BaseUrlMapper.swift (100%) rename Sources/Confidence/{ => Utils}/BufferedPassthroughSubject.swift (100%) rename Sources/Confidence/{ => Utils}/CaseIterableDefaultsLast.swift (100%) rename Sources/Confidence/{ => Utils}/ConfidenceValueHash.swift (100%) rename Sources/Confidence/{ => Utils}/FlagPath.swift (100%) rename Sources/Confidence/{ => Utils}/HttpStatusCode+Error.swift (100%) rename Sources/Confidence/{ => Utils}/PayloadMerger.swift (100%) rename Sources/Confidence/{ => Utils}/VisitorUtil.swift (100%) rename {Sources/Confidence/TestHelpers => Tests/ConfidenceTests/Helpers}/GrpcStatusCode.swift (100%) diff --git a/Sources/Confidence/RemoteConfidenceClient.swift b/Sources/Confidence/Client/ConfidenceClientEventsRemote.swift similarity index 97% rename from Sources/Confidence/RemoteConfidenceClient.swift rename to Sources/Confidence/Client/ConfidenceClientEventsRemote.swift index 808c96d0..07f6d0f7 100644 --- a/Sources/Confidence/RemoteConfidenceClient.swift +++ b/Sources/Confidence/Client/ConfidenceClientEventsRemote.swift @@ -1,7 +1,7 @@ import Foundation import os -public class RemoteConfidenceClient: ConfidenceClient { +public class ConfidenceClientEventsRemote: ConfidenceEventsClient { private var options: ConfidenceClientOptions private let metadata: ConfidenceMetadata private var httpClient: HttpClient diff --git a/Sources/Confidence/ConfidenceClientOptions.swift b/Sources/Confidence/Client/ConfidenceClientOptions.swift similarity index 100% rename from Sources/Confidence/ConfidenceClientOptions.swift rename to Sources/Confidence/Client/ConfidenceClientOptions.swift diff --git a/Sources/Confidence/RemoteResolveConfidenceClient.swift b/Sources/Confidence/Client/ConfidenceClientResolveRemote.swift similarity index 98% rename from Sources/Confidence/RemoteResolveConfidenceClient.swift rename to Sources/Confidence/Client/ConfidenceClientResolveRemote.swift index 7521f75a..b64a4ed1 100644 --- a/Sources/Confidence/RemoteResolveConfidenceClient.swift +++ b/Sources/Confidence/Client/ConfidenceClientResolveRemote.swift @@ -1,6 +1,6 @@ import Foundation -public class RemoteConfidenceResolveClient: ConfidenceResolveClient { +public class ConfidenceClientResolveRemote: ConfidenceResolveClient { private let targetingKey = "targeting_key" private var options: ConfidenceClientOptions private let metadata: ConfidenceMetadata diff --git a/Sources/Confidence/ConfidenceClient.swift b/Sources/Confidence/Client/ConfidenceClients.swift similarity index 84% rename from Sources/Confidence/ConfidenceClient.swift rename to Sources/Confidence/Client/ConfidenceClients.swift index 45f91428..74b36010 100644 --- a/Sources/Confidence/ConfidenceClient.swift +++ b/Sources/Confidence/Client/ConfidenceClients.swift @@ -1,6 +1,6 @@ import Foundation -protocol ConfidenceClient { +protocol ConfidenceEventsClient { // Returns true if the batch has been correctly processed by the backend func upload(events: [NetworkEvent]) async throws -> Bool } @@ -21,3 +21,8 @@ public struct ResolvesResult: Codable, Equatable { var resolvedValues: [ResolvedValue] var resolveToken: String? } + +struct Sdk: Codable { + var id: String + var version: String +} diff --git a/Sources/Confidence/Confidence.swift b/Sources/Confidence/Confidence.swift index 35f1d045..26c3571e 100644 --- a/Sources/Confidence/Confidence.swift +++ b/Sources/Confidence/Confidence.swift @@ -326,7 +326,7 @@ extension Confidence { let metadata = ConfidenceMetadata( name: sdkId, version: "0.2.3") // x-release-please-version - let uploader = RemoteConfidenceClient( + let uploader = ConfidenceClientEventsRemote( options: options, metadata: metadata ) @@ -337,7 +337,7 @@ extension Confidence { options: options, metadata: metadata ) - let flagResolver = flagResolver ?? RemoteConfidenceResolveClient( + let flagResolver = flagResolver ?? ConfidenceClientResolveRemote( options: options, applyOnResolve: false, metadata: metadata diff --git a/Sources/Confidence/EventSenderEngine.swift b/Sources/Confidence/EventSenderEngine.swift index 9a83e201..06039bc1 100644 --- a/Sources/Confidence/EventSenderEngine.swift +++ b/Sources/Confidence/EventSenderEngine.swift @@ -20,7 +20,7 @@ final class EventSenderEngineImpl: EventSenderEngine { private let uploadReqChannel = PassthroughSubject() private var cancellables = Set() private let flushPolicies: [FlushPolicy] - private let uploader: ConfidenceClient + private let uploader: ConfidenceEventsClient private let clientSecret: String private let payloadMerger: PayloadMerger = PayloadMergerImpl() private let semaphore = DispatchSemaphore(value: 1) @@ -28,7 +28,7 @@ final class EventSenderEngineImpl: EventSenderEngine { convenience init( clientSecret: String, - uploader: ConfidenceClient, + uploader: ConfidenceEventsClient, storage: EventStorage ) { self.init( @@ -42,7 +42,7 @@ final class EventSenderEngineImpl: EventSenderEngine { init( clientSecret: String, - uploader: ConfidenceClient, + uploader: ConfidenceEventsClient, storage: EventStorage, flushPolicies: [FlushPolicy], writeQueue: DispatchQueue diff --git a/Sources/Confidence/ConfidenceAppLifecycleProducer.swift b/Sources/Confidence/Producers/ConfidenceAppLifecycleProducer.swift similarity index 100% rename from Sources/Confidence/ConfidenceAppLifecycleProducer.swift rename to Sources/Confidence/Producers/ConfidenceAppLifecycleProducer.swift diff --git a/Sources/Confidence/ConfidenceProducer.swift b/Sources/Confidence/Producers/ConfidenceProducer.swift similarity index 100% rename from Sources/Confidence/ConfidenceProducer.swift rename to Sources/Confidence/Producers/ConfidenceProducer.swift diff --git a/Sources/Confidence/ConfidenceScreenTracker.swift b/Sources/Confidence/Producers/ConfidenceScreenTrackProducer.swift similarity index 92% rename from Sources/Confidence/ConfidenceScreenTracker.swift rename to Sources/Confidence/Producers/ConfidenceScreenTrackProducer.swift index 081342f8..4fb89489 100644 --- a/Sources/Confidence/ConfidenceScreenTracker.swift +++ b/Sources/Confidence/Producers/ConfidenceScreenTrackProducer.swift @@ -3,7 +3,7 @@ import Foundation import UIKit import Combine -public class ConfidenceScreenTracker: ConfidenceEventProducer { +public class ConfidenceScreenTrackProducer: ConfidenceEventProducer { private var events = BufferedPassthrough() static let notificationName = Notification.Name(rawValue: "ConfidenceScreenTracker") static let screenName = "screen_name" @@ -70,7 +70,7 @@ extension UIViewController { private func sendNotification(event: String) { var className = String(describing: type(of: self)) .replacingOccurrences(of: "ViewController", with: "") - var message: [String: String] = [ConfidenceScreenTracker.screenName: className] + var message: [String: String] = [ConfidenceScreenTrackProducer.screenName: className] if let trackable = self as? TrackableComponent { className = trackable.trackName() @@ -79,14 +79,14 @@ extension UIViewController { do { let data = try encoder.encode(trackableWithMessage.trackMessage()) let messageString = String(decoding: data, as: UTF8.self) - message.updateValue(messageString, forKey: ConfidenceScreenTracker.messageKey) + message.updateValue(messageString, forKey: ConfidenceScreenTrackProducer.messageKey) } catch { } } } NotificationCenter.default.post( - name: ConfidenceScreenTracker.notificationName, + name: ConfidenceScreenTrackProducer.notificationName, object: self, userInfo: message ) diff --git a/Sources/Confidence/Sdk.swift b/Sources/Confidence/Sdk.swift deleted file mode 100644 index bc615a2b..00000000 --- a/Sources/Confidence/Sdk.swift +++ /dev/null @@ -1,6 +0,0 @@ -import Foundation - -public struct Sdk: Codable { - var id: String - var version: String -} diff --git a/Sources/Confidence/TestHelpers/Extensions.swift b/Sources/Confidence/TestHelpers/Extensions.swift deleted file mode 100644 index d7b11462..00000000 --- a/Sources/Confidence/TestHelpers/Extensions.swift +++ /dev/null @@ -1,52 +0,0 @@ -import Foundation - -extension URLRequest { - public func decodeBody(type: T.Type) -> T? { - guard let bodyStream = self.httpBodyStream else { return nil } - - bodyStream.open() - - let bufferSize: Int = 128 - let buffer = UnsafeMutablePointer.allocate(capacity: bufferSize) - - var data = Data() - while bodyStream.hasBytesAvailable { - let readBytes = bodyStream.read(buffer, maxLength: bufferSize) - data.append(buffer, count: readBytes) - } - - buffer.deallocate() - - bodyStream.close() - - let decoder = JSONDecoder() - decoder.dateDecodingStrategy = .iso8601 - - do { - return try decoder.decode(type, from: data) - } catch { - return nil - } - } -} - -extension [ResolvedValue] { - func toCacheData(context: ConfidenceStruct, resolveToken: String) -> FlagResolution { - return FlagResolution( - context: context, - flags: self, - resolveToken: resolveToken - ) - } -} - -/// Used for testing -public protocol DispatchQueueType { - func async(execute work: @escaping @convention(block) () -> Void) -} - -extension DispatchQueue: DispatchQueueType { - public func async(execute work: @escaping @convention(block) () -> Void) { - async(group: nil, qos: .unspecified, flags: [], execute: work) - } -} diff --git a/Sources/Confidence/Array+Chunks.swift b/Sources/Confidence/Utils/Array+Chunks.swift similarity index 100% rename from Sources/Confidence/Array+Chunks.swift rename to Sources/Confidence/Utils/Array+Chunks.swift diff --git a/Sources/Confidence/Backport.swift b/Sources/Confidence/Utils/Backport.swift similarity index 100% rename from Sources/Confidence/Backport.swift rename to Sources/Confidence/Utils/Backport.swift diff --git a/Sources/Confidence/BaseUrlMapper.swift b/Sources/Confidence/Utils/BaseUrlMapper.swift similarity index 100% rename from Sources/Confidence/BaseUrlMapper.swift rename to Sources/Confidence/Utils/BaseUrlMapper.swift diff --git a/Sources/Confidence/BufferedPassthroughSubject.swift b/Sources/Confidence/Utils/BufferedPassthroughSubject.swift similarity index 100% rename from Sources/Confidence/BufferedPassthroughSubject.swift rename to Sources/Confidence/Utils/BufferedPassthroughSubject.swift diff --git a/Sources/Confidence/CaseIterableDefaultsLast.swift b/Sources/Confidence/Utils/CaseIterableDefaultsLast.swift similarity index 100% rename from Sources/Confidence/CaseIterableDefaultsLast.swift rename to Sources/Confidence/Utils/CaseIterableDefaultsLast.swift diff --git a/Sources/Confidence/ConfidenceValueHash.swift b/Sources/Confidence/Utils/ConfidenceValueHash.swift similarity index 100% rename from Sources/Confidence/ConfidenceValueHash.swift rename to Sources/Confidence/Utils/ConfidenceValueHash.swift diff --git a/Sources/Confidence/FlagPath.swift b/Sources/Confidence/Utils/FlagPath.swift similarity index 100% rename from Sources/Confidence/FlagPath.swift rename to Sources/Confidence/Utils/FlagPath.swift diff --git a/Sources/Confidence/HttpStatusCode+Error.swift b/Sources/Confidence/Utils/HttpStatusCode+Error.swift similarity index 100% rename from Sources/Confidence/HttpStatusCode+Error.swift rename to Sources/Confidence/Utils/HttpStatusCode+Error.swift diff --git a/Sources/Confidence/PayloadMerger.swift b/Sources/Confidence/Utils/PayloadMerger.swift similarity index 100% rename from Sources/Confidence/PayloadMerger.swift rename to Sources/Confidence/Utils/PayloadMerger.swift diff --git a/Sources/Confidence/VisitorUtil.swift b/Sources/Confidence/Utils/VisitorUtil.swift similarity index 100% rename from Sources/Confidence/VisitorUtil.swift rename to Sources/Confidence/Utils/VisitorUtil.swift diff --git a/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift b/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift index c073864a..3af2cc80 100644 --- a/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift +++ b/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift @@ -65,14 +65,6 @@ public class ConfidenceFeatureProvider: FeatureProvider { } } - func shutdown() { - for cancellable in cancellables { - cancellable.cancel() - } - cancellables.removeAll() - currentResolveTask?.cancel() - } - public func onContextSet( oldContext: OpenFeature.EvaluationContext?, newContext: OpenFeature.EvaluationContext @@ -123,6 +115,14 @@ public class ConfidenceFeatureProvider: FeatureProvider { return eventHandler.observe() } + func shutdown() { + for cancellable in cancellables { + cancellable.cancel() + } + cancellables.removeAll() + currentResolveTask?.cancel() + } + private func withLock(callback: @escaping (ConfidenceFeatureProvider) -> Void) { confidenceFeatureProviderQueue.sync { [weak self] in guard let self = self else { diff --git a/Tests/ConfidenceTests/ConfidenceContextTests.swift b/Tests/ConfidenceTests/ConfidenceContextTests.swift index cf65f4db..0dc7a732 100644 --- a/Tests/ConfidenceTests/ConfidenceContextTests.swift +++ b/Tests/ConfidenceTests/ConfidenceContextTests.swift @@ -4,7 +4,7 @@ import XCTest // swiftlint:disable type_body_length final class ConfidenceContextTests: XCTestCase { func testWithContext() { - let client = RemoteConfidenceResolveClient( + let client = ConfidenceClientResolveRemote( options: ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), @@ -30,7 +30,7 @@ final class ConfidenceContextTests: XCTestCase { } func testWithContextUpdateParent() { - let client = RemoteConfidenceResolveClient( + let client = ConfidenceClientResolveRemote( options: ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), @@ -61,7 +61,7 @@ final class ConfidenceContextTests: XCTestCase { } func testUpdateLocalContext() { - let client = RemoteConfidenceResolveClient( + let client = ConfidenceClientResolveRemote( options: ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), @@ -87,7 +87,7 @@ final class ConfidenceContextTests: XCTestCase { } func testUpdateLocalContextWithoutOverride() { - let client = RemoteConfidenceResolveClient( + let client = ConfidenceClientResolveRemote( options: ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), @@ -117,7 +117,7 @@ final class ConfidenceContextTests: XCTestCase { } func testUpdateParentContextWithOverride() { - let client = RemoteConfidenceResolveClient( + let client = ConfidenceClientResolveRemote( options: ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), @@ -147,7 +147,7 @@ final class ConfidenceContextTests: XCTestCase { } func testRemoveContextEntry() { - let client = RemoteConfidenceResolveClient( + let client = ConfidenceClientResolveRemote( options: ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), @@ -171,7 +171,7 @@ final class ConfidenceContextTests: XCTestCase { } func testRemoveContextEntryFromParent() { - let client = RemoteConfidenceResolveClient( + let client = ConfidenceClientResolveRemote( options: ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), @@ -198,7 +198,7 @@ final class ConfidenceContextTests: XCTestCase { } func testRemoveContextEntryFromParentAndChild() { - let client = RemoteConfidenceResolveClient( + let client = ConfidenceClientResolveRemote( options: ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), @@ -228,7 +228,7 @@ final class ConfidenceContextTests: XCTestCase { } func testRemoveContextEntryFromParentAndChildThenUpdate() { - let client = RemoteConfidenceResolveClient( + let client = ConfidenceClientResolveRemote( options: ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), @@ -260,7 +260,7 @@ final class ConfidenceContextTests: XCTestCase { } func testVisitorId() { - let client = RemoteConfidenceResolveClient( + let client = ConfidenceClientResolveRemote( options: ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), diff --git a/Tests/ConfidenceTests/ConfidenceTest.swift b/Tests/ConfidenceTests/ConfidenceTest.swift index 25543438..044f496b 100644 --- a/Tests/ConfidenceTests/ConfidenceTest.swift +++ b/Tests/ConfidenceTests/ConfidenceTest.swift @@ -591,4 +591,17 @@ final class DispatchQueueFake: DispatchQueueType { work() } } + + +/// Used for testing +public protocol DispatchQueueType { + func async(execute work: @escaping @convention(block) () -> Void) +} + +extension DispatchQueue: DispatchQueueType { + public func async(execute work: @escaping @convention(block) () -> Void) { + async(group: nil, qos: .unspecified, flags: [], execute: work) + } +} + // swiftlint:enable type_body_length diff --git a/Tests/ConfidenceTests/EventSenderEngineTest.swift b/Tests/ConfidenceTests/EventSenderEngineTest.swift index c2a78e04..c5842689 100644 --- a/Tests/ConfidenceTests/EventSenderEngineTest.swift +++ b/Tests/ConfidenceTests/EventSenderEngineTest.swift @@ -107,7 +107,7 @@ final class EventSenderEngineTest: XCTestCase { func testRemoveEventsFromStorageOnBadRequest() throws { MockedClientURLProtocol.mockedOperation = .badRequest - let badRequestUploader = RemoteConfidenceClient( + let badRequestUploader = ConfidenceClientEventsRemote( options: ConfidenceClientOptions(credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), metadata: ConfidenceMetadata(name: "", version: "")) @@ -131,7 +131,7 @@ final class EventSenderEngineTest: XCTestCase { func testKeepEventsInStorageForRetry() throws { MockedClientURLProtocol.mockedOperation = .needRetryLater - let retryLaterUploader = RemoteConfidenceClient( + let retryLaterUploader = ConfidenceClientEventsRemote( options: ConfidenceClientOptions(credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), metadata: ConfidenceMetadata(name: "", version: "")) diff --git a/Tests/ConfidenceTests/EventUploaderMock.swift b/Tests/ConfidenceTests/EventUploaderMock.swift index d2048aef..61325e2c 100644 --- a/Tests/ConfidenceTests/EventUploaderMock.swift +++ b/Tests/ConfidenceTests/EventUploaderMock.swift @@ -2,7 +2,7 @@ import Foundation import Combine @testable import Confidence -final class EventUploaderMock: ConfidenceClient { +final class EventUploaderMock: ConfidenceEventsClient { var calledRequest: [NetworkEvent]? let subject: PassthroughSubject = PassthroughSubject() diff --git a/Tests/ConfidenceTests/Helpers/ConfidenceClientMock.swift b/Tests/ConfidenceTests/Helpers/ConfidenceClientMock.swift index b16fbb9d..e0d5dc0c 100644 --- a/Tests/ConfidenceTests/Helpers/ConfidenceClientMock.swift +++ b/Tests/ConfidenceTests/Helpers/ConfidenceClientMock.swift @@ -2,7 +2,7 @@ import Foundation @testable import Confidence -class ConfidenceClientMock: ConfidenceClient { +class ConfidenceClientMock: ConfidenceEventsClient { func upload(events: [NetworkEvent]) async throws -> Bool { return true } diff --git a/Sources/Confidence/TestHelpers/GrpcStatusCode.swift b/Tests/ConfidenceTests/Helpers/GrpcStatusCode.swift similarity index 100% rename from Sources/Confidence/TestHelpers/GrpcStatusCode.swift rename to Tests/ConfidenceTests/Helpers/GrpcStatusCode.swift diff --git a/Tests/ConfidenceTests/Helpers/MockedClientURLProtocol.swift b/Tests/ConfidenceTests/Helpers/MockedClientURLProtocol.swift index 06ccc8b7..f0283ff4 100644 --- a/Tests/ConfidenceTests/Helpers/MockedClientURLProtocol.swift +++ b/Tests/ConfidenceTests/Helpers/MockedClientURLProtocol.swift @@ -113,3 +113,33 @@ class MockedClientURLProtocol: URLProtocol { client?.urlProtocolDidFinishLoading(self) } } + +extension URLRequest { + public func decodeBody(type: T.Type) -> T? { + guard let bodyStream = self.httpBodyStream else { return nil } + + bodyStream.open() + + let bufferSize: Int = 128 + let buffer = UnsafeMutablePointer.allocate(capacity: bufferSize) + + var data = Data() + while bodyStream.hasBytesAvailable { + let readBytes = bodyStream.read(buffer, maxLength: bufferSize) + data.append(buffer, count: readBytes) + } + + buffer.deallocate() + + bodyStream.close() + + let decoder = JSONDecoder() + decoder.dateDecodingStrategy = .iso8601 + + do { + return try decoder.decode(type, from: data) + } catch { + return nil + } + } +} diff --git a/Tests/ConfidenceTests/RemoteConfidenceClientTests.swift b/Tests/ConfidenceTests/RemoteConfidenceClientTests.swift index 2d62d03b..4d687f84 100644 --- a/Tests/ConfidenceTests/RemoteConfidenceClientTests.swift +++ b/Tests/ConfidenceTests/RemoteConfidenceClientTests.swift @@ -10,7 +10,7 @@ class RemoteConfidenceClientTest: XCTestCase { } func testUploadDoesntThrow() async throws { - let client = RemoteConfidenceClient( + let client = ConfidenceClientEventsRemote( options: ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), @@ -27,7 +27,7 @@ class RemoteConfidenceClientTest: XCTestCase { } func testUploadEmptyEventsDoesntThrow() async throws { - let client = RemoteConfidenceClient( + let client = ConfidenceClientEventsRemote( options: ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), @@ -39,7 +39,7 @@ class RemoteConfidenceClientTest: XCTestCase { func testUploadFirstEventFailsDoesntThrow() async throws { MockedClientURLProtocol.mockedOperation = .firstEventFails - let client = RemoteConfidenceClient( + let client = ConfidenceClientEventsRemote( options: ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), @@ -57,7 +57,7 @@ class RemoteConfidenceClientTest: XCTestCase { func testMalformedResponseThrows() async throws { MockedClientURLProtocol.mockedOperation = .malformedResponse - let client = RemoteConfidenceClient( + let client = ConfidenceClientEventsRemote( options: ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: "")), session: MockedClientURLProtocol.mockedSession(), diff --git a/Tests/ConfidenceTests/RemoteResolveConfidenceClientTest.swift b/Tests/ConfidenceTests/RemoteResolveConfidenceClientTest.swift index 16519b27..f93f7bdc 100644 --- a/Tests/ConfidenceTests/RemoteResolveConfidenceClientTest.swift +++ b/Tests/ConfidenceTests/RemoteResolveConfidenceClientTest.swift @@ -25,7 +25,7 @@ class RemoteResolveConfidenceClientTest: XCTestCase { func testResolveMultipleFlagsSucceeds() async throws { let session = MockedResolveClientURLProtocol.mockedSession(flags: flags) - let client = RemoteConfidenceResolveClient( + let client = ConfidenceClientResolveRemote( options: .init(credentials: .clientSecret(secret: "test")), session: session, applyOnResolve: true,