mirror of
https://github.com/bitwarden/ios.git
synced 2025-12-12 18:30:41 -06:00
39 lines
1.1 KiB
Swift
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() {}
|
|
}
|