import Foundation /// A mock `URLProtocol` used to mock networking requests using `URLSession`. /// class MockURLProtocol: URLProtocol { override class func canInit(with request: URLRequest) -> Bool { guard let url = request.url, URLProtocolMocking.response(for: url) != nil else { return false } return true } override class func canonicalRequest(for request: URLRequest) -> URLRequest { request } override func startLoading() { guard let url = request.url, let result = URLProtocolMocking.response(for: url) else { return } switch result { case let .success((response, data)): client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) client?.urlProtocol(self, didLoad: data) case let .failure(error): client?.urlProtocol(self, didFailWithError: error) } client?.urlProtocolDidFinishLoading(self) } override func stopLoading() {} }