-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathFoundationMocks.swift
658 lines (547 loc) · 21.6 KB
/
FoundationMocks.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-2020 Datadog, Inc.
*/
import Foundation
/*
A collection of mocks for different `Foundation` types. The convention we use is to extend
types with static factory function prefixed with "mock". For example:
```
extension URL {
static func mockAny() -> URL {
// ...
}
}
extension URLSession {
static func mockDeliverySuccess(data: Data, response: HTTPURLResponse) -> URLSessionMock {
// ...
}
}
```
Other conventions to follow:
* Use the name `mockAny()` to name functions that return any value of given type.
* Use descriptive function and parameter names for functions that configure the object for particular scenario.
* Always use the minimal set of parameters which is required by given mock scenario.
*/
// MARK: - Basic types
protocol AnyMockable {
static func mockAny() -> Self
}
protocol RandomMockable {
static func mockRandom() -> Self
}
extension Data: AnyMockable, RandomMockable {
static func mockAny() -> Data {
return .mock(ofSize: 256)
}
static func mockRepeating(byte: UInt8, times count: Int) -> Data {
return Data(repeating: byte, count: count)
}
static func mock<Size>(ofSize size: Size) -> Data where Size: BinaryInteger {
return mockRepeating(byte: .mockRandom(), times: Int(size))
}
static func mockRandom<Size>(ofSize size: Size) -> Data where Size: BinaryInteger {
let count = Int(size)
let bytes = UnsafeMutablePointer<UInt8>.allocate(capacity: count)
defer { bytes.deallocate() }
let status = SecRandomCopyBytes(kSecRandomDefault, count, bytes)
guard status == errSecSuccess else {
fatalError("Failed to generate random data")
}
return Data(bytes: bytes, count: count)
}
static func mockRandom() -> Data {
return mockRandom(ofSize: Int.mockRandom(min: 16, max: 512))
}
}
extension Optional: AnyMockable where Wrapped: AnyMockable {
static func mockAny() -> Self {
return .some(.mockAny())
}
}
extension Optional: RandomMockable where Wrapped: RandomMockable {
static func mockRandom() -> Self {
return .some(.mockRandom())
}
}
extension Array where Element == Data {
/// Returns chunks of mocked data. Accumulative size of all chunks equals `totalSize`.
static func mockChunksOf(totalSize: UInt64, maxChunkSize: UInt64) -> [Data] {
var chunks: [Data] = []
var bytesWritten: UInt64 = 0
while bytesWritten < totalSize {
let bytesLeft = totalSize - bytesWritten
var nextChunkSize: UInt64 = bytesLeft > Int.max ? UInt64(Int.max) : bytesLeft // prevents `Int` overflow
nextChunkSize = nextChunkSize > maxChunkSize ? maxChunkSize : nextChunkSize // caps the next chunk to its max size
chunks.append(.mockRepeating(byte: 0x1, times: Int(nextChunkSize)))
bytesWritten += UInt64(nextChunkSize)
}
return chunks
}
}
extension Array {
func randomElements() -> [Element] {
return compactMap { Bool.random() ? $0 : nil }
}
}
extension Array: RandomMockable where Element: RandomMockable {
static func mockRandom() -> [Element] {
return Array(repeating: .mockRandom(), count: 10)
}
}
extension Dictionary: AnyMockable where Key: AnyMockable, Value: AnyMockable {
static func mockAny() -> Dictionary {
return [Key.mockAny(): Value.mockAny()]
}
}
extension Dictionary: RandomMockable where Key: RandomMockable, Value: RandomMockable {
static func mockRandom() -> Dictionary {
return [Key.mockRandom(): Value.mockRandom()]
}
}
extension Date: AnyMockable {
static func mockAny() -> Date {
return Date(timeIntervalSinceReferenceDate: 1)
}
static func mockRandomInThePast() -> Date {
return Date(timeIntervalSinceReferenceDate: TimeInterval.mockRandomInThePast())
}
static func mockSpecificUTCGregorianDate(year: Int, month: Int, day: Int, hour: Int, minute: Int = 0, second: Int = 0) -> Date {
var dateComponents = DateComponents()
dateComponents.year = year
dateComponents.month = month
dateComponents.day = day
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.second = second
dateComponents.timeZone = .UTC
dateComponents.calendar = .gregorian
return dateComponents.date!
}
static func mockDecember15th2019At10AMUTC(addingTimeInterval timeInterval: TimeInterval = 0) -> Date {
return mockSpecificUTCGregorianDate(year: 2_019, month: 12, day: 15, hour: 10)
.addingTimeInterval(timeInterval)
}
}
extension TimeZone: AnyMockable {
static var UTC: TimeZone { TimeZone(abbreviation: "UTC")! }
static var EET: TimeZone { TimeZone(abbreviation: "EET")! }
static func mockAny() -> TimeZone { .EET }
}
extension Calendar {
static var gregorian: Calendar {
return Calendar(identifier: .gregorian)
}
}
extension URL: AnyMockable, RandomMockable {
static func mockAny() -> URL {
return URL(string: "https://www.datadoghq.com")!
}
static func mockWith(pathComponent: String) -> URL {
return URL(string: "https://www.foo.com/")!.appendingPathComponent(pathComponent)
}
static func mockWith(url: String, queryParams: [URLQueryItem]) -> URL {
var urlComponents = URLComponents(string: url)
urlComponents!.queryItems = queryParams
return urlComponents!.url!
}
static func mockRandom() -> URL {
return URL(string: "https://www.foo.com/")!
.appendingPathComponent(
.mockRandom(
among: .alphanumerics,
length: 32
)
)
}
static func mockRandomPath(containing subpathComponents: [String] = []) -> URL {
let count: Int = .mockRandom(min: 2, max: 10)
var components: [String] = (0..<count).map { _ in
.mockRandom(
among: .alphanumerics,
length: .mockRandom(min: 3, max: 10)
)
}
components.insert(contentsOf: subpathComponents, at: .random(in: 0..<count))
return URL(fileURLWithPath: "/\(components.joined(separator: "/"))")
}
}
extension UUID: AnyMockable, RandomMockable {
static func mockAny() -> UUID {
return UUID()
}
static func mockRandom() -> UUID {
return UUID()
}
}
extension String: AnyMockable, RandomMockable {
static func mockAny() -> String {
return "abc"
}
static func mockRandom() -> String {
return mockRandom(length: 10)
}
static func mockRandom(length: Int) -> String {
return mockRandom(
among: .alphanumerics + " ",
length: length
)
}
static func mockRandom(among characters: String, length: Int = 10) -> String {
return String((0..<length).map { _ in characters.randomElement()! })
}
static func mockRepeating(character: Character, times: Int) -> String {
let characters = (0..<times).map { _ in character }
return String(characters)
}
static let alphanumerics = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
static let decimalDigits = "0123456789"
}
extension Character: AnyMockable, RandomMockable {
static func mockAny() -> Character { "c" }
static func mockRandom() -> Character {
String.alphanumerics.randomElement()!
}
}
extension Bool: RandomMockable {
static func mockRandom() -> Bool {
return .random()
}
}
extension Range where Bound == Int {
static func mockRandomBetween(min: Int, max: Int) -> Range<Int> {
let bound1: Int = .mockRandom(min: min, max: max)
let bound2: Int = .mockRandom(min: min, max: max)
return bound1 < bound2 ? bound1..<bound2 : bound2..<bound1
}
}
extension FixedWidthInteger where Self: RandomMockable {
static func mockRandom() -> Self {
return .random(in: min...max)
}
static func mockRandom(min: Self = .min, max: Self = .max, otherThan values: Set<Self> = []) -> Self {
var random: Self = .random(in: min...max)
while values.contains(random) { random = .random(in: min...max) }
return random
}
}
extension ExpressibleByIntegerLiteral where Self: AnyMockable {
static func mockAny() -> Self { 0 }
}
extension UInt: AnyMockable, RandomMockable { }
extension UInt8: AnyMockable, RandomMockable { }
extension UInt16: AnyMockable, RandomMockable { }
extension UInt32: AnyMockable, RandomMockable { }
extension UInt64: AnyMockable, RandomMockable { }
extension Int: AnyMockable, RandomMockable { }
extension Int8: AnyMockable, RandomMockable { }
extension Int16: AnyMockable, RandomMockable { }
extension Int32: AnyMockable, RandomMockable { }
extension Int64: AnyMockable, RandomMockable { }
extension UInt64 {
static func mockRandom(otherThan value: UInt64) -> UInt64 {
var random: UInt64 = .mockRandom()
while random == value { random = .mockRandom() }
return random
}
}
extension Bool: AnyMockable {
static func mockAny() -> Bool {
return false
}
}
extension Float: AnyMockable {
static func mockAny() -> Float {
return 0
}
}
extension Double: AnyMockable, RandomMockable {
static func mockAny() -> Double {
return 0
}
static func mockRandom() -> Double {
return mockRandom(min: 0, max: .greatestFiniteMagnitude)
}
static func mockRandom(min: Double, max: Double) -> Double {
return .random(in: min...max)
}
}
extension TimeInterval {
static let distantFuture = TimeInterval(integerLiteral: .max)
static func mockRandomInThePast() -> TimeInterval {
return random(in: 0..<Date().timeIntervalSinceReferenceDate)
}
}
struct ErrorMock: Error, CustomStringConvertible {
let description: String
init(_ description: String = "") {
self.description = description
}
}
struct FailingEncodableMock: Encodable {
let errorMessage: String
func encode(to encoder: Encoder) throws {
throw ErrorMock(errorMessage)
}
}
class BundleMock: Bundle {
// swiftlint:disable identifier_name
fileprivate var _bundlePath: String = .mockAny()
fileprivate var _bundleIdentifier: String? = nil
fileprivate var _CFBundleVersion: String? = nil
fileprivate var _CFBundleShortVersionString: String? = nil
fileprivate var _CFBundleExecutable: String? = nil
// swiftlint:enable identifier_name
override var bundlePath: String { _bundlePath }
override var bundleIdentifier: String? { _bundleIdentifier }
override func object(forInfoDictionaryKey key: String) -> Any? {
switch key {
case "CFBundleVersion": return _CFBundleVersion
case "CFBundleShortVersionString": return _CFBundleShortVersionString
case "CFBundleExecutable": return _CFBundleExecutable
default: return super.object(forInfoDictionaryKey: key)
}
}
}
extension Bundle {
static func mockAny() -> Bundle {
return mockWith()
}
static func mockWith(
bundlePath: String = .mockAny(),
bundleIdentifier: String? = .mockAny(),
CFBundleVersion: String? = .mockAny(),
CFBundleShortVersionString: String? = .mockAny(),
CFBundleExecutable: String? = .mockAny()
) -> Bundle {
let mock = BundleMock()
mock._bundlePath = bundlePath
mock._bundleIdentifier = bundleIdentifier
mock._CFBundleVersion = CFBundleVersion
mock._CFBundleShortVersionString = CFBundleShortVersionString
mock._CFBundleExecutable = CFBundleExecutable
return mock
}
}
// MARK: - HTTP
extension URLResponse {
static func mockAny() -> HTTPURLResponse {
return .mockResponseWith(statusCode: 200)
}
static func mockResponseWith(statusCode: Int) -> HTTPURLResponse {
return HTTPURLResponse(url: .mockAny(), statusCode: statusCode, httpVersion: nil, headerFields: nil)!
}
static func mockWith(
statusCode: Int = 200,
mimeType: String? = "application/json"
) -> HTTPURLResponse {
let headers: [String: String] = (mimeType == nil) ? [:] : ["Content-Type": "\(mimeType!)"]
return HTTPURLResponse(
url: .mockAny(),
statusCode: statusCode,
httpVersion: nil,
headerFields: headers
)!
}
}
extension URLRequest: AnyMockable {
static func mockAny() -> URLRequest {
return URLRequest(url: .mockAny())
}
static func mockWith(httpMethod: String) -> URLRequest {
var request = URLRequest(url: .mockAny())
request.httpMethod = httpMethod
return request
}
static func mockWith(url: String, queryParams: [URLQueryItem], httpMethod: String) -> URLRequest {
let url: URL = .mockWith(url: url, queryParams: queryParams)
var request = URLRequest(url: url)
request.httpMethod = httpMethod
return request
}
}
// MARK: - Process
class ProcessInfoMock: ProcessInfo {
private var _isLowPowerModeEnabled: Bool
private var _arguments: [String]
init(isLowPowerModeEnabled: Bool = .mockAny(), arguments: [String] = []) {
_isLowPowerModeEnabled = isLowPowerModeEnabled
_arguments = arguments
}
override var isLowPowerModeEnabled: Bool { _isLowPowerModeEnabled }
override var arguments: [String] { _arguments }
}
// MARK: - URLSession
extension URLSession {
static func mockAny() -> URLSession {
return .shared
}
}
extension URLSessionTask {
static func mockAny() -> URLSessionTask {
return URLSessionTaskMock(request: .mockAny(), response: .mockAny())
}
static func mockWith(request: URLRequest, response: HTTPURLResponse) -> URLSessionTask {
return URLSessionTaskMock(request: request, response: response)
}
}
extension URLSessionTaskMetrics {
static func mockAny() -> URLSessionTaskMetrics {
return URLSessionTaskMetrics()
}
@available(iOS 13, *)
static func mockWith(
taskInterval: DateInterval = .init(start: Date(), duration: 1),
transactionMetrics: [URLSessionTaskTransactionMetrics] = []
) -> URLSessionTaskMetrics {
return URLSessionTaskMetricsMock(
taskInterval: taskInterval,
transactionMetrics: transactionMetrics
)
}
}
extension URLSessionTaskTransactionMetrics {
static func mockAny() -> URLSessionTaskTransactionMetrics {
return URLSessionTaskTransactionMetrics()
}
/// Mocks `URLSessionTaskTransactionMetrics` by spreading out detailed values between `start` and `end`.
@available(iOS 13, *)
static func mockBySpreadingDetailsBetween(
start: Date,
end: Date,
resourceFetchType: URLSessionTaskMetrics.ResourceFetchType = .networkLoad
) -> URLSessionTaskTransactionMetrics {
let spread = end.timeIntervalSince(start)
let fetchStartDate = start
let domainLookupStartDate = start.addingTimeInterval(spread * 0.05) // 5%
let domainLookupEndDate = start.addingTimeInterval(spread * 0.15) // 15%
let connectStartDate = start.addingTimeInterval(spread * 0.15) // 15%
let secureConnectionStartDate = start.addingTimeInterval(spread * 0.20) // 20%
let secureConnectionEndDate = start.addingTimeInterval(spread * 0.35) // 35%
let connectEndDate = secureConnectionEndDate
let requestStartDate = start.addingTimeInterval(spread * 0.40) // 40%
let responseStartDate = start.addingTimeInterval(spread * 0.50) // 50%
let responseEndDate = end
let countOfResponseBodyBytesAfterDecoding: Int64 = .random(in: 512..<1_024)
return URLSessionTaskTransactionMetricsMock(
resourceFetchType: resourceFetchType,
fetchStartDate: fetchStartDate,
domainLookupStartDate: domainLookupStartDate,
domainLookupEndDate: domainLookupEndDate,
connectStartDate: connectStartDate,
connectEndDate: connectEndDate,
secureConnectionStartDate: secureConnectionStartDate,
secureConnectionEndDate: secureConnectionEndDate,
requestStartDate: requestStartDate,
responseStartDate: responseStartDate,
responseEndDate: responseEndDate,
countOfResponseBodyBytesAfterDecoding: countOfResponseBodyBytesAfterDecoding
)
}
@available(iOS 13, *)
static func mockWith(
resourceFetchType: URLSessionTaskMetrics.ResourceFetchType = .networkLoad,
fetchStartDate: Date? = nil,
domainLookupStartDate: Date? = nil,
domainLookupEndDate: Date? = nil,
connectStartDate: Date? = nil,
connectEndDate: Date? = nil,
secureConnectionStartDate: Date? = nil,
secureConnectionEndDate: Date? = nil,
requestStartDate: Date? = nil,
responseStartDate: Date? = nil,
responseEndDate: Date? = nil,
countOfResponseBodyBytesAfterDecoding: Int64 = 0
) -> URLSessionTaskTransactionMetrics {
return URLSessionTaskTransactionMetricsMock(
resourceFetchType: resourceFetchType,
fetchStartDate: fetchStartDate,
domainLookupStartDate: domainLookupStartDate,
domainLookupEndDate: domainLookupEndDate,
connectStartDate: connectStartDate,
connectEndDate: connectEndDate,
secureConnectionStartDate: secureConnectionStartDate,
secureConnectionEndDate: secureConnectionEndDate,
requestStartDate: requestStartDate,
responseStartDate: responseStartDate,
responseEndDate: responseEndDate,
countOfResponseBodyBytesAfterDecoding: countOfResponseBodyBytesAfterDecoding
)
}
}
private class URLSessionTaskMock: URLSessionTask {
private let _originalRequest: URLRequest
override var originalRequest: URLRequest? { _originalRequest }
private let _response: URLResponse
override var response: URLResponse? { _response }
init(request: URLRequest, response: HTTPURLResponse) {
self._originalRequest = request
self._response = response
}
}
@available(iOS 13, *) // We can't rely on subclassing the `URLSessionTaskMetrics` prior to iOS 13.0
private class URLSessionTaskMetricsMock: URLSessionTaskMetrics {
private let _taskInterval: DateInterval
override var taskInterval: DateInterval { _taskInterval }
private let _transactionMetrics: [URLSessionTaskTransactionMetrics]
override var transactionMetrics: [URLSessionTaskTransactionMetrics] { _transactionMetrics }
init(taskInterval: DateInterval, transactionMetrics: [URLSessionTaskTransactionMetrics]) {
self._taskInterval = taskInterval
self._transactionMetrics = transactionMetrics
}
}
@available(iOS 13, *) // We can't rely on subclassing the `URLSessionTaskTransactionMetrics` prior to iOS 13.0
private class URLSessionTaskTransactionMetricsMock: URLSessionTaskTransactionMetrics {
private let _resourceFetchType: URLSessionTaskMetrics.ResourceFetchType
override var resourceFetchType: URLSessionTaskMetrics.ResourceFetchType { _resourceFetchType }
private let _fetchStartDate: Date?
override var fetchStartDate: Date? { _fetchStartDate }
private let _domainLookupStartDate: Date?
override var domainLookupStartDate: Date? { _domainLookupStartDate }
private let _domainLookupEndDate: Date?
override var domainLookupEndDate: Date? { _domainLookupEndDate }
private let _connectStartDate: Date?
override var connectStartDate: Date? { _connectStartDate }
private let _connectEndDate: Date?
override var connectEndDate: Date? { _connectEndDate }
private let _secureConnectionStartDate: Date?
override var secureConnectionStartDate: Date? { _secureConnectionStartDate }
private let _secureConnectionEndDate: Date?
override var secureConnectionEndDate: Date? { _secureConnectionEndDate }
private let _requestStartDate: Date?
override var requestStartDate: Date? { _requestStartDate }
private let _responseStartDate: Date?
override var responseStartDate: Date? { _responseStartDate }
private let _responseEndDate: Date?
override var responseEndDate: Date? { _responseEndDate }
private let _countOfResponseBodyBytesAfterDecoding: Int64
override var countOfResponseBodyBytesAfterDecoding: Int64 { _countOfResponseBodyBytesAfterDecoding }
init(
resourceFetchType: URLSessionTaskMetrics.ResourceFetchType,
fetchStartDate: Date?,
domainLookupStartDate: Date?,
domainLookupEndDate: Date?,
connectStartDate: Date?,
connectEndDate: Date?,
secureConnectionStartDate: Date?,
secureConnectionEndDate: Date?,
requestStartDate: Date?,
responseStartDate: Date?,
responseEndDate: Date?,
countOfResponseBodyBytesAfterDecoding: Int64
) {
self._resourceFetchType = resourceFetchType
self._fetchStartDate = fetchStartDate
self._domainLookupStartDate = domainLookupStartDate
self._domainLookupEndDate = domainLookupEndDate
self._connectStartDate = connectStartDate
self._connectEndDate = connectEndDate
self._secureConnectionStartDate = secureConnectionStartDate
self._secureConnectionEndDate = secureConnectionEndDate
self._requestStartDate = requestStartDate
self._responseStartDate = responseStartDate
self._responseEndDate = responseEndDate
self._countOfResponseBodyBytesAfterDecoding = countOfResponseBodyBytesAfterDecoding
}
}