Skip to content

Commit 93d2af0

Browse files
authored
Merge pull request #2352 from DataDog/valpertui/add-combine-network-instrumentation-tests
Add Network instrumentation URLSession.dataTaskPublisher test
2 parents a498e1e + 7ab1410 commit 93d2af0

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

DatadogInternal/Tests/NetworkInstrumentation/NetworkInstrumentationFeatureTests.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,47 @@ class NetworkInstrumentationFeatureTests: XCTestCase {
175175
_ = server.waitAndReturnRequests(count: 1)
176176
}
177177

178+
@available(iOS 13.0, tvOS 13.0, *)
179+
func testGivenURLSessionWithCustomDelegate_whenUsingCombineDataFromURL_itNotifiesInterceptor() async throws {
180+
/// Testing only 16.0 or above because 15.0 has ThreadSanitizer issues with async APIs
181+
guard #available(iOS 16, tvOS 16, *) else {
182+
return
183+
}
184+
185+
let notifyInterceptionDidStart = expectation(description: "Notify interception did start")
186+
let notifyInterceptionDidComplete = expectation(description: "Notify intercepion did complete")
187+
let server = ServerMock(
188+
delivery: .success(response: .mockResponseWith(statusCode: 200), data: .mock(ofSize: 10)),
189+
skipIsMainThreadCheck: true
190+
)
191+
192+
handler.onInterceptionDidStart = { _ in notifyInterceptionDidStart.fulfill() }
193+
handler.onInterceptionDidComplete = { _ in notifyInterceptionDidComplete.fulfill() }
194+
195+
// Given
196+
let delegate = MockDelegate()
197+
try URLSessionInstrumentation.enableOrThrow(with: .init(delegateClass: MockDelegate.self), in: core)
198+
let session = server.getInterceptedURLSession(delegate: delegate)
199+
200+
// When
201+
_ = session.dataTaskPublisher(for: URL.mockAny())
202+
.sink(
203+
receiveCompletion: { _ in },
204+
receiveValue: { _ in }
205+
)
206+
// Then
207+
await dd_fulfillment(
208+
for: [
209+
notifyInterceptionDidStart,
210+
notifyInterceptionDidComplete
211+
],
212+
timeout: 5,
213+
enforceOrder: true
214+
)
215+
216+
_ = server.waitAndReturnRequests(count: 1)
217+
}
218+
178219
func testGivenURLSessionWithCustomDelegate_whenUsingCompletionHandlerDataFromURL_itNotifiesInterceptor() throws {
179220
/// Testing only 16.0 or above because 15.0 has ThreadSanitizer issues with async APIs
180221
guard #available(iOS 16, tvOS 16, *) else {

0 commit comments

Comments
 (0)