swift-libs/Sources/Communications/Classes/MockURLProtocol.swift
Javier Cicchelli c4d09cd808 [Setup] Non Apple platforms (#13)
This PR contains the work done to address the issue #12, with regards to provide basic support of non Apple platforms.

To provide further details about the work done:
- [x] flattened the folder structure, especially now that the idea to filter folders based on platform is being discarded;
- [x] implemented precompiler processors to filter out platform-specific source code;
- [x] updated the `Package` file to provide basic support for non-Apple platforms;
- [x] added and also improved some targets to the `Makefile` file to smooth the current development workflows;
- [x] updated the `.gitignore` file with references to the `.vscode` folder and the `.env` file;
- [x] updated the Swift tools version to v5.7.

Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Reviewed-on: #13
2023-04-28 17:37:09 +00:00

121 lines
3.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftLibs open source project
//
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
// Licensed under the EUPL 1.2 or later.
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
//
//===----------------------------------------------------------------------===//
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
import Foundation
/// This class overrides the `URLProtocol` protocol used by the `URLSession` to handle the loading of protocol-specific URL data so it is possible to mock URL response for testing purposes.
public class MockURLProtocol: URLProtocol {
// MARK: Properties
/// The dictionary in which the mock requests with its respective mock responses will be injected.
public static var mockData: [MockURLRequest: MockURLResponse] = [:]
// MARK: Functions
public override class func canInit(with task: URLSessionTask) -> Bool {
true
}
public override class func canInit(with request: URLRequest) -> Bool {
true
}
public override class func canonicalRequest(for request: URLRequest) -> URLRequest {
request
}
public override func startLoading() {
guard
let httpMethod = request.httpMethod,
let method = HTTPRequestMethod(rawValue: httpMethod),
let url = request.url,
let response = Self.mockData[.init(
method: method,
url: url
)]
else {
client?.urlProtocolDidFinishLoading(self)
return
}
if let data = response.data {
client?.urlProtocol(self, didLoad: data)
}
if let httpResponse = HTTPURLResponse(
url: url,
statusCode: response.status.rawValue,
httpVersion: nil,
headerFields: response.headers
) {
client?.urlProtocol(
self,
didReceive: httpResponse,
cacheStoragePolicy: .allowedInMemoryOnly
)
}
client?.urlProtocolDidFinishLoading(self)
}
public override func stopLoading() {}
}
// MARK: - Structs
/// This model includes the data to be injected into an specific URL at the time of mocking its request.
public struct MockURLRequest: Hashable {
// MARK: Properties
public let method: HTTPRequestMethod
public let url: URL
// MARK: Initialisers
public init(
method: HTTPRequestMethod,
url: URL
) {
self.method = method
self.url = url
}
}
/// This model includes the data to be injected into an specific URL at the time of mocking its response.
public struct MockURLResponse {
// MARK: Properties
public let status: HTTPResponseCode
public let headers: [String: String]
public let data: Data?
// MARK: Initialisers
public init(
status: HTTPResponseCode,
headers: [String : String] = [:],
data: Data? = nil
) {
self.status = status
self.headers = headers
self.data = data
}
}
#endif