1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import Observation
import XCTest
@testable import OldFella
final class AvatarTestProtocol: URLProtocol {
static let calls = FeedCallCounter()
override static func canInit(with request: URLRequest) -> Bool { true }
override static func canonicalRequest(for request: URLRequest) -> URLRequest { request }
override func startLoading() {
_ = Self.calls.next()
let url = request.url!
let response = HTTPURLResponse(
url: url, statusCode: url.path == "/error" ? 503 : 200, httpVersion: "HTTP/1.1",
headerFields: ["Cache-Control": "max-age=3600"])!
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
let data = UIGraphicsImageRenderer(size: CGSize(width: 2, height: 2)).image { _ in
UIColor.red.setFill()
UIRectFill(CGRect(x: 0, y: 0, width: 2, height: 2))
}.pngData()!
client?.urlProtocol(self, didLoad: data)
client?.urlProtocolDidFinishLoading(self)
}
override func stopLoading() {}
}