summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadRESTTests/RequestExecutorTests.swift
blob: bd5daf052949e0ed54f1e58484775dcf39af0623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
//  RequestExecutorTests.swift
//  MullvadRESTTests
//
//  Created by pronebird on 25/08/2023.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import XCTest

@testable import MullvadMockData
@testable import MullvadREST
@testable import MullvadTypes

@MainActor
final class RequestExecutorTests: XCTestCase {
    let addressCache = REST.AddressCache(canWriteToCache: false, fileCache: MemoryCache())
    nonisolated(unsafe) var timerServerProxy: TimeServerProxy!

    override func setUp() {
        super.setUp()

        let transportProvider = REST.AnyTransportProvider {
            AnyTransport {
                Response(delay: 1, statusCode: 200, value: TimeResponse(dateTime: Date()))
            }
        }

        let apiTransportProvider = REST.AnyAPITransportProvider {
            APITransportStub()
        }

        let proxyFactory = REST.ProxyFactory.makeProxyFactory(
            transportProvider: transportProvider,
            apiTransportProvider: apiTransportProvider,
            addressCache: addressCache
        )
        timerServerProxy = TimeServerProxy(configuration: proxyFactory.configuration)
    }

    func testExecuteAsync() async throws {
        _ = try await timerServerProxy.getDateTime().execute()
    }

    func testExecuteWithCompletionBlock() throws {
        let expectation = self.expectation(description: "Wait for request to complete.")

        _ = timerServerProxy.getDateTime().execute { result in
            XCTAssertTrue(result.isSuccess)
            expectation.fulfill()
        }

        waitForExpectations(timeout: .UnitTest.timeout)
    }

    func testCancelAsyncExecution() async throws {
        let task = Task {
            return try await timerServerProxy.getDateTime().execute()
        }
        task.cancel()

        do {
            _ = try await task.value
            XCTFail("Should always throw OperationError.cancelled")
        } catch {
            XCTAssertTrue(error.isOperationCancellationError)
        }
    }

    func testCancelExecutionWithCompletionBlock() {
        let expectation = self.expectation(description: "Wait for request to complete.")

        let cancellationToken = timerServerProxy.getDateTime().execute { result in
            let isCancellationError = result.error?.isOperationCancellationError ?? false
            XCTAssertTrue(isCancellationError)
            expectation.fulfill()
        }

        cancellationToken.cancel()

        waitForExpectations(timeout: .UnitTest.timeout)
    }
}

extension RequestExecutorTests {
    final class APITransportStub: APITransportProtocol, Sendable {
        public var name: String {
            "app-transport-dummy"
        }

        public func sendRequest(
            _ request: APIRequest,
            completion: @escaping @Sendable (ProxyAPIResponse) -> Void
        ) -> Cancellable {
            AnyCancellable()
        }
    }
}