summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPNTests/OutgoingConnectionProxyTests.swift
diff options
context:
space:
mode:
authorJon Petersson <jon.petersson@kvadrat.se>2023-11-16 11:55:14 +0100
committerJon Petersson <jon.petersson@kvadrat.se>2023-11-20 09:58:30 +0100
commitdc746f1352d62c8be188d0c82ebcf2740add663c (patch)
tree86470b537fb032376e95dbf374a5638373a52856 /ios/MullvadVPNTests/OutgoingConnectionProxyTests.swift
parent20a9bfb30fd7e8afc881405caab3e1ee737cafd1 (diff)
downloadmullvadvpn-dc746f1352d62c8be188d0c82ebcf2740add663c.tar.xz
mullvadvpn-dc746f1352d62c8be188d0c82ebcf2740add663c.zip
Refactor URLSession out of outgoing connection proxy tests
Diffstat (limited to 'ios/MullvadVPNTests/OutgoingConnectionProxyTests.swift')
-rw-r--r--ios/MullvadVPNTests/OutgoingConnectionProxyTests.swift80
1 files changed, 23 insertions, 57 deletions
diff --git a/ios/MullvadVPNTests/OutgoingConnectionProxyTests.swift b/ios/MullvadVPNTests/OutgoingConnectionProxyTests.swift
index afa8ada0fa..2ffdbdf92d 100644
--- a/ios/MullvadVPNTests/OutgoingConnectionProxyTests.swift
+++ b/ios/MullvadVPNTests/OutgoingConnectionProxyTests.swift
@@ -9,51 +9,27 @@ import MullvadREST
import XCTest
final class OutgoingConnectionProxyTests: XCTestCase {
- private var outgoingConnectionProxy: OutgoingConnectionProxy!
private var mockIPV6ConnectionData: Data!
private var mockIPV4ConnectionData: Data!
private let encoder = JSONEncoder()
override func setUpWithError() throws {
- outgoingConnectionProxy = OutgoingConnectionProxy(urlSession: .mock)
mockIPV4ConnectionData = try encoder.encode(IPV4ConnectionData.mock)
mockIPV6ConnectionData = try encoder.encode(IPV6ConnectionData.mock)
}
override func tearDownWithError() throws {
- outgoingConnectionProxy = nil
mockIPV4ConnectionData.removeAll()
mockIPV6ConnectionData.removeAll()
}
- func testNoInternetConnection() async throws {
- let noIPv4Expectation = expectation(description: "Did not receive IPv4")
- let error = URLError(URLError.notConnectedToInternet)
-
- MockURLProtocol.error = error
- MockURLProtocol.requestHandler = nil
-
- await XCTAssertThrowsErrorAsync(try await outgoingConnectionProxy.getIPV4(retryStrategy: .noRetry)) { error in
- noIPv4Expectation.fulfill()
- XCTAssertEqual((error as? URLError)?.code, .notConnectedToInternet)
- }
- await fulfillment(of: [noIPv4Expectation], timeout: 1)
- }
-
func testSuccessGettingIPV4() async throws {
let iPv4Expectation = expectation(description: "Did receive IPv4")
- MockURLProtocol.error = nil
- MockURLProtocol.requestHandler = { _ in
- let response = HTTPURLResponse(
- url: URL(string: "https://ipv4.am.i.mullvad.net/json")!,
- statusCode: 200,
- httpVersion: nil,
- headerFields: ["Content-Type": "application/json"]
- )!
- return (response, self.mockIPV4ConnectionData)
- }
+ let outgoingConnectionProxy = OutgoingConnectionProxy(urlSession: URLSessionStub(
+ response: (mockIPV4ConnectionData, createHTTPURLResponse(ip: .v4, statusCode: 200))
+ ))
let result = try await outgoingConnectionProxy.getIPV4(retryStrategy: .noRetry)
@@ -66,16 +42,9 @@ final class OutgoingConnectionProxyTests: XCTestCase {
func testFailureGettingIPV4() async throws {
let noIPv4Expectation = expectation(description: "Did not receive IPv4")
- MockURLProtocol.error = nil
- MockURLProtocol.requestHandler = { _ in
- let response = HTTPURLResponse(
- url: URL(string: "https://ipv4.am.i.mullvad.net/json")!,
- statusCode: 503,
- httpVersion: nil,
- headerFields: ["Content-Type": "application/json"]
- )!
- return (response, Data())
- }
+ let outgoingConnectionProxy = OutgoingConnectionProxy(urlSession: URLSessionStub(
+ response: (Data(), createHTTPURLResponse(ip: .v4, statusCode: 503))
+ ))
await XCTAssertThrowsErrorAsync(try await outgoingConnectionProxy.getIPV4(retryStrategy: .noRetry)) { _ in
noIPv4Expectation.fulfill()
@@ -86,16 +55,9 @@ final class OutgoingConnectionProxyTests: XCTestCase {
func testSuccessGettingIPV6() async throws {
let ipv6Expectation = expectation(description: "Did receive IPv6")
- MockURLProtocol.error = nil
- MockURLProtocol.requestHandler = { _ in
- let response = HTTPURLResponse(
- url: URL(string: "https://ipv6.am.i.mullvad.net/json")!,
- statusCode: 200,
- httpVersion: nil,
- headerFields: ["Content-Type": "application/json"]
- )!
- return (response, self.mockIPV6ConnectionData)
- }
+ let outgoingConnectionProxy = OutgoingConnectionProxy(urlSession: URLSessionStub(
+ response: (mockIPV6ConnectionData, createHTTPURLResponse(ip: .v6, statusCode: 200))
+ ))
let result = try await outgoingConnectionProxy.getIPV6(retryStrategy: .noRetry)
@@ -108,16 +70,9 @@ final class OutgoingConnectionProxyTests: XCTestCase {
func testFailureGettingIPV6() async throws {
let noIPv6Expectation = expectation(description: "Did not receive IPv6")
- MockURLProtocol.error = nil
- MockURLProtocol.requestHandler = { _ in
- let response = HTTPURLResponse(
- url: URL(string: "https://ipv6.am.i.mullvad.net/json")!,
- statusCode: 404,
- httpVersion: nil,
- headerFields: ["Content-Type": "application/json"]
- )!
- return (response, Data())
- }
+ let outgoingConnectionProxy = OutgoingConnectionProxy(urlSession: URLSessionStub(
+ response: (mockIPV6ConnectionData, createHTTPURLResponse(ip: .v6, statusCode: 404))
+ ))
await XCTAssertThrowsErrorAsync(try await outgoingConnectionProxy.getIPV6(retryStrategy: .noRetry)) { _ in
noIPv6Expectation.fulfill()
@@ -125,3 +80,14 @@ final class OutgoingConnectionProxyTests: XCTestCase {
await fulfillment(of: [noIPv6Expectation], timeout: 1)
}
}
+
+extension OutgoingConnectionProxyTests {
+ private func createHTTPURLResponse(ip: OutgoingConnectionProxy.ExitIPVersion, statusCode: Int) -> HTTPURLResponse {
+ return HTTPURLResponse(
+ url: URL(string: "https://\(ip.host)/json")!,
+ statusCode: statusCode,
+ httpVersion: nil,
+ headerFields: ["Content-Type": "application/json"]
+ )!
+ }
+}