Skip to content

Instantly share code, notes, and snippets.

@Andrea-Scuderi
Last active September 3, 2019 11:24
Show Gist options
  • Save Andrea-Scuderi/15e8e13e31bd562e5c607b63a54d60d5 to your computer and use it in GitHub Desktop.
Save Andrea-Scuderi/15e8e13e31bd562e5c607b63a54d60d5 to your computer and use it in GitHub Desktop.
URLProtocolMock
import Foundation
//References:
// --: https://www.hackingwithswift.com/articles/153/how-to-test-ios-networking-code-the-easy-way
// --: https://nshipster.com/nsurlprotocol/
@objc class URLProtocolMock: URLProtocol {
// this dictionary maps URLs to test data
static var testURLs = [URL?: Data]()
static var response: URLResponse?
static var error: Error?
// say we want to handle all types of request
override class func canInit(with request: URLRequest) -> Bool {
return true
}
override class func canInit(with task: URLSessionTask) -> Bool {
return true
}
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
return request
}
override func startLoading() {
// if we have a valid URL…
if let url = request.url {
// …and if we have test data for that URL…
if let data = URLProtocolMock.testURLs[url] {
// …load it immediately.
self.client?.urlProtocol(self, didLoad: data)
}
}
// …and we return our response if defined…
if let response = URLProtocolMock.response {
self.client?.urlProtocol(self,
didReceive: response,
cacheStoragePolicy: .notAllowed)
}
// …and we return our error if defined…
if let error = URLProtocolMock.error {
self.client?.urlProtocol(self, didFailWithError: error)
}
// mark that we've finished
self.client?.urlProtocolDidFinishLoading(self)
}
// this method is required but doesn't need to do anything
override func stopLoading() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment