ios/Networking/Tests/NetworkingTests/Support/MockURLProtocol.swift
2023-08-29 10:14:02 -05:00

39 lines
1.1 KiB
Swift

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() {}
}