12345678910111213141516171819202122232425262728293031323334353637383940 |
- //
- // MockURLProtocol.swift
- // LiveLikeGiphyChallenge
- //
- // Created by Irina Smokvarska on 2/1/22.
- //
- import XCTest
- @testable import LiveLikeGiphyChallenge
- //MARK: MockURLProtocol
- class MockURLProtocol: URLProtocol {
- static var requestHandler: ((URLRequest) throws -> (HTTPURLResponse, Data))?
-
- override class func canInit(with request: URLRequest) -> Bool {
- return true
- }
-
- override class func canonicalRequest(for request: URLRequest) -> URLRequest {
- return request
- }
-
- 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)
- }
- }
-
- override func stopLoading() {
- }
- }
|