@@ -35,6 +35,23 @@ class URLSessionInstrumentationTests: XCTestCase {
35
35
semaphore. signal ( )
36
36
}
37
37
}
38
+
39
+ class CountingSessionDelegate : NSObject , URLSessionDelegate , URLSessionDataDelegate {
40
+ var callCount : Int = 0
41
+
42
+ func urlSession( _ session: URLSession , didBecomeInvalidWithError error: Error ? ) {
43
+ callCount += 1
44
+
45
+ }
46
+
47
+ func urlSession( _ session: URLSession , task: URLSessionTask , didCompleteWithError error: Error ? ) {
48
+ callCount += 1
49
+ }
50
+
51
+ func urlSession( _ session: URLSession , task: URLSessionTask , didFinishCollecting metrics: URLSessionTaskMetrics ) {
52
+ callCount += 1
53
+ }
54
+ }
38
55
39
56
static var requestCopy : URLRequest !
40
57
static var responseCopy : HTTPURLResponse !
@@ -594,9 +611,27 @@ class URLSessionInstrumentationTests: XCTestCase {
594
611
testDataTaskWithRequestDelegateAsync( ) async throws {
595
612
let request = URLRequest ( url: URL ( string: " http://localhost:33333/success " ) !)
596
613
597
- let session = URLSession ( configuration: URLSessionConfiguration . default, delegate: sessionDelegate, delegateQueue: nil )
598
- _ = try await session. data ( for: request)
614
+ let delegate = CountingSessionDelegate ( )
615
+
616
+ let session = URLSession ( configuration: URLSessionConfiguration . default, delegate: delegate, delegateQueue: nil )
617
+ let _ = try await session. data ( for: request)
618
+
619
+ XCTAssertEqual ( 1 , delegate. callCount)
620
+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
621
+ XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
622
+ }
623
+
624
+ @available ( macOS 10 . 15 , iOS 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
625
+ public func
626
+ testDataTaskWithTaskDelegateAsync( ) async throws {
627
+ let request = URLRequest ( url: URL ( string: " http://localhost:33333/success " ) !)
628
+
629
+ let delegate = CountingSessionDelegate ( )
630
+
631
+ let session = URLSession ( configuration: URLSessionConfiguration . default)
632
+ let _ = try await session. data ( for: request, delegate: delegate)
599
633
634
+ XCTAssertEqual ( 1 , delegate. callCount)
600
635
XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
601
636
XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
602
637
}
@@ -623,7 +658,7 @@ class URLSessionInstrumentationTests: XCTestCase {
623
658
}
624
659
625
660
@available ( macOS 12 , iOS 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
626
- public func testDownloadTaskWithRequestDelegateAsync ( ) async throws {
661
+ public func testDownloadTaskWithSessionDelegateAsync ( ) async throws {
627
662
let url = URL ( string: " http://localhost:33333/success " ) !
628
663
let request = URLRequest ( url: url)
629
664
@@ -634,9 +669,21 @@ class URLSessionInstrumentationTests: XCTestCase {
634
669
XCTAssertTrue ( URLSessionInstrumentationTests . checker. receivedResponseCalled)
635
670
XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
636
671
}
672
+
673
+ @available ( macOS 12 , iOS 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
674
+ public func testDownloadTaskWithRequestDelegateAsync( ) async throws {
675
+ let url = URL ( string: " http://localhost:33333/success " ) !
676
+ let request = URLRequest ( url: url)
677
+
678
+ let session = URLSession ( configuration: URLSessionConfiguration . default)
679
+ _ = try await session. download ( for: request, delegate: sessionDelegate)
680
+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
681
+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. receivedResponseCalled)
682
+ XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
683
+ }
637
684
638
685
@available ( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * )
639
- public func testUploadTaskWithRequestDelegateAsync ( ) async throws {
686
+ public func testUploadTaskWithSessionDelegateAsync ( ) async throws {
640
687
let url = URL ( string: " http://localhost:33333/success " ) !
641
688
let request = URLRequest ( url: url)
642
689
let session = URLSession ( configuration: URLSessionConfiguration . default, delegate: sessionDelegate, delegateQueue: nil )
@@ -645,6 +692,17 @@ class URLSessionInstrumentationTests: XCTestCase {
645
692
XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
646
693
XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
647
694
}
695
+
696
+ @available ( macOS 10 . 15 , iOS 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
697
+ public func testUploadTaskWithRequestDelegateAsync( ) async throws {
698
+ let url = URL ( string: " http://localhost:33333/success " ) !
699
+ let request = URLRequest ( url: url)
700
+ let session = URLSession ( configuration: URLSessionConfiguration . default)
701
+ _ = try await session. upload ( for: request, from: Data ( ) , delegate: sessionDelegate)
702
+
703
+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
704
+ XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
705
+ }
648
706
649
707
public func testNonInstrumentedRequestCompletes( ) {
650
708
let request = URLRequest ( url: URL ( string: " http://localhost:33333/dontinstrument " ) !)
0 commit comments