-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMockURLProtocol.swift
39 lines (29 loc) · 971 Bytes
/
MockURLProtocol.swift
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// MockURLProtocol.swift
// OpenMarketTests
//
// Created by Ryan-Son on 2021/09/04.
//
import Nimble
final class MockURLProtocol: URLProtocol {
static var loadingHandler: ((URLRequest) -> (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.loadingHandler else {
fail("Loading handler가 설정되지 않았습니다.")
return
}
let (response, data) = handler(request)
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
if let data = data {
client?.urlProtocol(self, didLoad: data)
}
client?.urlProtocolDidFinishLoading(self)
}
override func stopLoading() { }
}