From b8f816494b75ff5f9ff7f8471d2b6f37114d7552 Mon Sep 17 00:00:00 2001 From: Guilherme Souza Date: Tue, 16 Jul 2024 13:29:50 -0300 Subject: [PATCH] test: fix failing tests (#467) --- Sources/TestHelpers/HTTPClientMock.swift | 39 +++++++------------ Tests/AuthTests/SessionManagerTests.swift | 12 ++++-- .../FunctionsTests/FunctionsClientTests.swift | 35 +++++++++-------- 3 files changed, 40 insertions(+), 46 deletions(-) diff --git a/Sources/TestHelpers/HTTPClientMock.swift b/Sources/TestHelpers/HTTPClientMock.swift index 0d77c178..43eb6da0 100644 --- a/Sources/TestHelpers/HTTPClientMock.swift +++ b/Sources/TestHelpers/HTTPClientMock.swift @@ -10,22 +10,16 @@ import Foundation import Helpers import XCTestDynamicOverlay -package final class HTTPClientMock: HTTPClientType { +package actor HTTPClientMock: HTTPClientType { package struct MockNotFound: Error {} - private let mocks: LockIsolated < [@Sendable (HTTPRequest) async throws -> HTTPResponse?]> = .init([]) - private let _receivedRequests = LockIsolated<[HTTPRequest]>([]) - private let _returnedResponses = LockIsolated<[Result]>([]) - + private var mocks = [@Sendable (HTTPRequest) async throws -> HTTPResponse?]() + /// Requests received by this client in order. - package var receivedRequests: [HTTPRequest] { - _receivedRequests.value - } + package var receivedRequests: [HTTPRequest] = [] /// Responses returned by this client in order. - package var returnedResponses: [Result] { - _returnedResponses.value - } + package var returnedResponses: [Result] = [] package init() {} @@ -34,14 +28,11 @@ package final class HTTPClientMock: HTTPClientType { _ request: @escaping @Sendable (HTTPRequest) -> Bool, return response: @escaping @Sendable (HTTPRequest) async throws -> HTTPResponse ) -> Self { - mocks.withValue { - $0.append { r in - if request(r) { - return try await response(r) - } - - return nil + mocks.append { r in + if request(r) { + return try await response(r) } + return nil } return self } @@ -54,20 +45,16 @@ package final class HTTPClientMock: HTTPClientType { } package func send(_ request: HTTPRequest) async throws -> HTTPResponse { - _receivedRequests.withValue { $0.append(request) } + receivedRequests.append(request) - for mock in mocks.value { + for mock in mocks{ do { if let response = try await mock(request) { - _returnedResponses.withValue { - $0.append(.success(response)) - } + returnedResponses.append(.success(response)) return response } } catch { - _returnedResponses.withValue { - $0.append(.failure(error)) - } + returnedResponses.append(.failure(error)) throw error } } diff --git a/Tests/AuthTests/SessionManagerTests.swift b/Tests/AuthTests/SessionManagerTests.swift index 69ce7753..1edee03a 100644 --- a/Tests/AuthTests/SessionManagerTests.swift +++ b/Tests/AuthTests/SessionManagerTests.swift @@ -31,7 +31,7 @@ final class SessionManagerTests: XCTestCase { configuration: .init( url: clientURL, localStorage: InMemoryLocalStorage(), - logger: nil, + logger: TestLogger(), autoRefreshToken: false ), http: http, @@ -72,7 +72,7 @@ final class SessionManagerTests: XCTestCase { let (refreshSessionStream, refreshSessionContinuation) = AsyncStream.makeStream() - http.when( + await http.when( { $0.url.path.contains("/token") }, return: { _ in refreshSessionCallCount.withValue { $0 += 1 } @@ -83,7 +83,7 @@ final class SessionManagerTests: XCTestCase { // Fire N tasks and call sut.session() let tasks = (0 ..< 10).map { _ in - Task.detached { [weak self] in + Task { [weak self] in try await self?.sut.session() } } @@ -106,3 +106,9 @@ final class SessionManagerTests: XCTestCase { } } } + +struct TestLogger: SupabaseLogger { + func log(message: SupabaseLogMessage) { + print(message.description) + } +} diff --git a/Tests/FunctionsTests/FunctionsClientTests.swift b/Tests/FunctionsTests/FunctionsClientTests.swift index c55ff9de..70e305b3 100644 --- a/Tests/FunctionsTests/FunctionsClientTests.swift +++ b/Tests/FunctionsTests/FunctionsClientTests.swift @@ -29,7 +29,7 @@ final class FunctionsClientTests: XCTestCase { func testInvoke() async throws { let url = URL(string: "http://localhost:5432/functions/v1/hello_world")! - let http = HTTPClientMock() + let http = await HTTPClientMock() .when { $0.url.pathComponents.contains("hello_world") } return: { _ in @@ -49,7 +49,7 @@ final class FunctionsClientTests: XCTestCase { options: .init(headers: ["X-Custom-Key": "value"], body: body) ) - let request = http.receivedRequests.last + let request = await http.receivedRequests.last XCTAssertEqual(request?.url, url) XCTAssertEqual(request?.method, .post) @@ -59,7 +59,7 @@ final class FunctionsClientTests: XCTestCase { } func testInvokeWithCustomMethod() async throws { - let http = HTTPClientMock().any { _ in try .stub(body: Empty()) } + let http = await HTTPClientMock().any { _ in try .stub(body: Empty()) } let sut = FunctionsClient( url: url, @@ -70,12 +70,12 @@ final class FunctionsClientTests: XCTestCase { try await sut.invoke("hello-world", options: .init(method: .delete)) - let request = http.receivedRequests.last + let request = await http.receivedRequests.last XCTAssertEqual(request?.method, .delete) } func testInvokeWithQuery() async throws { - let http = HTTPClientMock().any { _ in try .stub(body: Empty()) } + let http = await HTTPClientMock().any { _ in try .stub(body: Empty()) } let sut = FunctionsClient( url: url, @@ -91,12 +91,12 @@ final class FunctionsClientTests: XCTestCase { ) ) - let request = http.receivedRequests.last + let request = await http.receivedRequests.last XCTAssertEqual(request?.urlRequest.url?.query, "key=value") } func testInvokeWithRegionDefinedInClient() async throws { - let http = HTTPClientMock() + let http = await HTTPClientMock() .any { _ in try .stub(body: Empty()) } let sut = FunctionsClient( @@ -108,11 +108,12 @@ final class FunctionsClientTests: XCTestCase { try await sut.invoke("hello-world") - XCTAssertEqual(http.receivedRequests.last?.headers["x-region"], "ca-central-1") + let request = await http.receivedRequests.last + XCTAssertEqual(request?.headers["x-region"], "ca-central-1") } func testInvokeWithRegion() async throws { - let http = HTTPClientMock() + let http = await HTTPClientMock() .any { _ in try .stub(body: Empty()) } let sut = FunctionsClient( @@ -124,11 +125,12 @@ final class FunctionsClientTests: XCTestCase { try await sut.invoke("hello-world", options: .init(region: .caCentral1)) - XCTAssertEqual(http.receivedRequests.last?.headers["x-region"], "ca-central-1") + let request = await http.receivedRequests.last + XCTAssertEqual(request?.headers["x-region"], "ca-central-1") } func testInvokeWithoutRegion() async throws { - let http = HTTPClientMock() + let http = await HTTPClientMock() .any { _ in try .stub(body: Empty()) } let sut = FunctionsClient( @@ -140,11 +142,12 @@ final class FunctionsClientTests: XCTestCase { try await sut.invoke("hello-world") - XCTAssertNil(http.receivedRequests.last?.headers["x-region"]) + let request = await http.receivedRequests.last + XCTAssertNil(request?.headers["x-region"]) } func testInvoke_shouldThrow_URLError_badServerResponse() async { - let sut = FunctionsClient( + let sut = await FunctionsClient( url: url, headers: ["Apikey": apiKey], region: nil, @@ -162,7 +165,7 @@ final class FunctionsClientTests: XCTestCase { } func testInvoke_shouldThrow_FunctionsError_httpError() async { - let sut = FunctionsClient( + let sut = await FunctionsClient( url: url, headers: ["Apikey": apiKey], region: nil, @@ -180,9 +183,7 @@ final class FunctionsClientTests: XCTestCase { } func testInvoke_shouldThrow_FunctionsError_relayError() async { - let url = URL(string: "http://localhost:5432/functions/v1/hello_world")! - - let sut = FunctionsClient( + let sut = await FunctionsClient( url: self.url, headers: ["Apikey": apiKey], region: nil,