Skip to content

Instantly share code, notes, and snippets.

@charlieInDen
Created January 30, 2019 03:22
Show Gist options
  • Save charlieInDen/bb7eb6dbe6339134b76d7458f43f6209 to your computer and use it in GitHub Desktop.
Save charlieInDen/bb7eb6dbe6339134b76d7458f43f6209 to your computer and use it in GitHub Desktop.
URL loading system
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