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 ImageTestProtocol: URLProtocol {
override static func canInit(with request: URLRequest) -> Bool { true }
override static func canonicalRequest(for request: URLRequest) -> URLRequest { request }
override func startLoading() {
guard let url = request.url, url.path != "/wait" else { return }
let response = HTTPURLResponse(
url: url, statusCode: url.path == "/error" ? 503 : 200, httpVersion: "HTTP/1.1", headerFields: nil)!
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
if url.path == "/gif" {
client?.urlProtocol(self, didLoad: Data("GIF89a".utf8))
client?.urlProtocol(self, didLoad: Data(repeating: 0, count: GIFDownload.byteLimit))
} else {
client?.urlProtocol(self, didLoad: Data(repeating: 0, count: 1024))
}
client?.urlProtocolDidFinishLoading(self)
}
override func stopLoading() {}
}