Created
May 25, 2019 00:31
-
-
Save laevandus/f5a7b7a075d07ff053105716ac2fe17d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class TestURLProtocol: URLProtocol { | |
override class func canInit(with request: URLRequest) -> Bool { | |
return true | |
} | |
override class func canonicalRequest(for request: URLRequest) -> URLRequest { | |
return request | |
} | |
static var loadingHandler: ((URLRequest) -> (HTTPURLResponse, Data?, Error?))? | |
override func startLoading() { | |
guard let handler = TestURLProtocol.loadingHandler else { | |
XCTFail("Loading handler is not set.") | |
return | |
} | |
let (response, data, error) = handler(request) | |
if let data = data { | |
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) | |
client?.urlProtocol(self, didLoad: data) | |
client?.urlProtocolDidFinishLoading(self) | |
} | |
else { | |
client?.urlProtocol(self, didFailWithError: error!) | |
} | |
} | |
override func stopLoading() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment