Created
January 30, 2019 03:22
-
-
Save charlieInDen/bb7eb6dbe6339134b76d7458f43f6209 to your computer and use it in GitHub Desktop.
URL loading system
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
class MockURLProtocol: URLProtocol { | |
static var requestHandler: ((URLRequest) throws -> (HTTPURLResponse, Data))? | |
override func startLoading() { | |
guard let handler = MockURLProtocol.requestHandler else { | |
XCTFail("Received unexpected request with no handler set") | |
return | |
} | |
do { | |
let (response, data) = try handler(request) | |
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) | |
client?.urlProtocol(self, didLoad: data) | |
client?.urlProtocolDidFinishLoading(self) | |
}catch { | |
client?.urlProtocol(self, didFailWithError: error) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment