🌊
【XCTest】URLProtocolを用いてURLSessionをモックする
後日執筆予定
final class CustomURLProtocol: URLProtocol {
static var targets: [URL: (error: Error?, data: Data?, response: HTTPURLResponse?)] = .init()
override class func canInit(with request: URLRequest) -> Bool { true }
override class func canonicalRequest(for request: URLRequest) -> URLRequest { request }
override func startLoading() {
if let url = request.url {
if let (error, data, response) = Self.targets[url] {
if let response {
self.client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
}
if let data {
self.client?.urlProtocol(self, didLoad: data)
}
if let error {
self.client?.urlProtocol(self, didFailWithError: error)
}
}
}
self.client?.urlProtocolDidFinishLoading(self)
}
override func stopLoading() {}
}
let config = URLSessionConfiguration.ephemeral
config.protocolClasses = [CustomURLProtocol.self]
let urlSession = URLSession(configuration: config)
URLSession, URLProtocol, URLProtocolClient
Discussion