This repository was archived by the owner on Sep 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMockNetworking.swift
75 lines (62 loc) · 2.23 KB
/
MockNetworking.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/
import Foundation
class MockTask: URLSessionDataTask {
private let data_: Data?
private let urlResponse_: URLResponse?
private let error_: Error?
var internalState: URLSessionTask.State = .suspended
override var state: URLSessionTask.State {
internalState
}
override func cancel() {
internalState = .canceling
}
var completionHandler: ((Data?, URLResponse?, Error?) -> Void)?
init(data: Data?, urlResponse: URLResponse?, error: Error?) {
data_ = data
urlResponse_ = urlResponse
error_ = error
}
override func resume() {
internalState = .running
DispatchQueue.global().asyncAfter(deadline: .now() + 0.1) { [weak self] in
guard let self = self else { return }
if self.internalState != .canceling {
self.internalState = .completed
self.completionHandler?(self.data_, self.urlResponse_, self.error_)
}
}
}
}
class MockUrlCache: URLCache {
var response: CachedURLResponse?
init(response: CachedURLResponse) {
self.response = response
super.init(memoryCapacity: 1, diskCapacity: 1, diskPath: "")
}
override func cachedResponse(for _: URLRequest) -> CachedURLResponse? {
response
}
}
class MockSession: URLSession {
let handler: (URLRequest) -> (Data?, URLResponse?, Error?)
var requests: [URLRequest] = []
init(handler: @escaping ((URLRequest) -> (Data?, URLResponse?, Error?))) {
self.handler = handler
}
override func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
requests.append(request)
let (data, urlResponse, error) = handler(request)
let task = MockTask(data: data, urlResponse: urlResponse, error: error)
task.completionHandler = completionHandler
return task
}
}