-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathURLSessionSwizzlerTests.swift
40 lines (31 loc) · 1.47 KB
/
URLSessionSwizzlerTests.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
/*
* 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-Present Datadog, Inc.
*/
import XCTest
@testable import DatadogInternal
class URLSessionSwizzlerTests: XCTestCase {
func testSwizzling_dataTaskWithCompletion() throws {
let didReceive = expectation(description: "didReceive")
didReceive.expectedFulfillmentCount = 2
let didInterceptCompletion = expectation(description: "interceptCompletion")
didInterceptCompletion.expectedFulfillmentCount = 2
let swizzler = URLSessionSwizzler()
try swizzler.swizzle(
interceptCompletionHandler: { _, _, _ in
didInterceptCompletion.fulfill()
}, didReceive: { _, _ in
didReceive.fulfill()
}
)
let session = URLSession(configuration: .default)
let url = URL(string: "https://www.datadoghq.com/")!
session.dataTask(with: url) { _, _, _ in }.resume() // intercepted
session.dataTask(with: URLRequest(url: url)) { _, _, _ in }.resume() // intercepted
swizzler.unswizzle()
session.dataTask(with: url) { _, _, _ in }.resume() // not intercepted
session.dataTask(with: URLRequest(url: url)) { _, _, _ in }.resume() // not intercepted
wait(for: [didReceive, didInterceptCompletion], timeout: 5)
}
}