@@ -195,9 +195,9 @@ class URLSessionInstrumentationTests: XCTestCase {
195195 }
196196
197197 public func testConfigurationCallbacksCalledWhenForbidden( ) throws {
198- if #available ( watchOS 3 . 0 , * ) {
198+ # if os (watchOS)
199199 throw XCTSkip ( " Implementation needs to be updated for watchOS to make this test pass " )
200- }
200+ #endif
201201
202202 let request = URLRequest ( url: URL ( string: " http://localhost:33333/forbidden " ) !)
203203 let task = URLSession . shared. dataTask ( with: request) { data, _, _ in
@@ -388,4 +388,171 @@ class URLSessionInstrumentationTests: XCTestCase {
388388 XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
389389 XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
390390 }
391+
392+
393+
394+ #if swift(>=5.5.2)
395+ @available ( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * )
396+ public func testConfigurationCallbacksCalledWhenSuccessAsync( ) async throws {
397+ let request = URLRequest ( url: URL ( string: " http://localhost:33333/success " ) !)
398+
399+ let ( data, _) = try await URLSession . shared. data ( for: request)
400+ let string = String ( decoding: data, as: UTF8 . self)
401+ print ( string)
402+
403+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. shouldInstrumentCalled)
404+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. nameSpanCalled)
405+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. spanCustomizationCalled)
406+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. shouldInjectTracingHeadersCalled)
407+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
408+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. receivedResponseCalled)
409+ }
410+
411+ @available ( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * )
412+ public func testConfigurationCallbacksCalledWhenForbiddenAsync( ) async throws {
413+ #if os(watchOS)
414+ throw XCTSkip ( " Implementation needs to be updated for watchOS to make this test pass " )
415+ #endif
416+ let request = URLRequest ( url: URL ( string: " http://localhost:33333/forbidden " ) !)
417+ let ( data, _) = try await URLSession . shared. data ( for: request)
418+ let string = String ( decoding: data, as: UTF8 . self)
419+ print ( string)
420+
421+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. shouldInstrumentCalled)
422+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. nameSpanCalled)
423+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. spanCustomizationCalled)
424+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. shouldInjectTracingHeadersCalled)
425+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
426+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. receivedResponseCalled)
427+ XCTAssertFalse ( URLSessionInstrumentationTests . checker. receivedErrorCalled)
428+ }
429+
430+ @available ( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * )
431+ public func testConfigurationCallbacksCalledWhenErrorAsync( ) async throws {
432+ let request = URLRequest ( url: URL ( string: " http://localhost:33333/error " ) !)
433+
434+ do {
435+ _ = try await URLSession . shared. data ( for: request)
436+ } catch {
437+ }
438+
439+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. shouldInstrumentCalled)
440+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. nameSpanCalled)
441+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. spanCustomizationCalled)
442+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. shouldInjectTracingHeadersCalled)
443+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
444+ XCTAssertFalse ( URLSessionInstrumentationTests . checker. receivedResponseCalled)
445+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. receivedErrorCalled)
446+ }
447+
448+ @available ( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * )
449+ public func testDataTaskWithRequestBlockAsync( ) async throws {
450+ let request = URLRequest ( url: URL ( string: " http://localhost:33333/success " ) !)
451+
452+ _ = try await URLSession . shared. data ( for: request)
453+
454+ XCTAssertEqual ( 0 , URLSessionInstrumentationTests . instrumentation. startedRequestSpans. count)
455+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
456+ XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
457+ }
458+
459+ @available ( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * )
460+ public func testDataTaskWithUrlBlockAsync( ) async throws {
461+ let url = URL ( string: " http://localhost:33333/success " ) !
462+
463+ _ = try await URLSession . shared. data ( from: url)
464+
465+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
466+ XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
467+ }
468+
469+ @available ( macOS 12 , iOS 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
470+ public func testDownloadTaskWithUrlBlockAsync( ) async throws {
471+ let url = URL ( string: " http://localhost:33333/success " ) !
472+
473+ _ = try await URLSession . shared. download ( from: url)
474+
475+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
476+ XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
477+ }
478+
479+ @available ( macOS 12 , iOS 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
480+ public func testDownloadTaskWithRequestBlockAsync( ) async throws {
481+ let url = URL ( string: " http://localhost:33333/success " ) !
482+ let request = URLRequest ( url: url)
483+ _ = try await URLSession . shared. download ( for: request)
484+
485+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
486+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. receivedResponseCalled)
487+ XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
488+ }
489+
490+ @available ( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * )
491+ public func testUploadTaskWithRequestBlockAsync( ) async throws {
492+ let url = URL ( string: " http://localhost:33333/success " ) !
493+ let request = URLRequest ( url: url)
494+ _ = try await URLSession . shared. upload ( for: request, from: Data ( ) )
495+
496+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
497+ XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
498+ }
499+
500+ @available ( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * )
501+ public func
502+ testDataTaskWithRequestDelegateAsync( ) async throws {
503+ let request = URLRequest ( url: URL ( string: " http://localhost:33333/success " ) !)
504+
505+ let session = URLSession ( configuration: URLSessionConfiguration . default, delegate: sessionDelegate, delegateQueue: nil )
506+ _ = try await session. data ( for: request)
507+
508+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
509+ XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
510+ }
511+
512+ @available ( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * )
513+ public func testDataTaskWithUrlDelegateAsync( ) async throws {
514+ let url = URL ( string: " http://localhost:33333/success " ) !
515+
516+ let session = URLSession ( configuration: URLSessionConfiguration . default, delegate: sessionDelegate, delegateQueue: nil )
517+ _ = try await session. data ( from: url)
518+
519+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
520+ XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
521+ }
522+
523+ @available ( macOS 12 , iOS 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
524+ public func testDownloadTaskWithUrlDelegateAsync( ) async throws {
525+ let url = URL ( string: " http://localhost:33333/success " ) !
526+
527+ _ = try await URLSession . shared. download ( from: url, delegate: sessionDelegate)
528+
529+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
530+ XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
531+ }
532+
533+ @available ( macOS 12 , iOS 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
534+ public func testDownloadTaskWithRequestDelegateAsync( ) async throws {
535+ let url = URL ( string: " http://localhost:33333/success " ) !
536+ let request = URLRequest ( url: url)
537+
538+ let session = URLSession ( configuration: URLSessionConfiguration . default, delegate: sessionDelegate, delegateQueue: nil )
539+ _ = try await session. download ( for: request)
540+
541+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
542+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. receivedResponseCalled)
543+ XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
544+ }
545+
546+ @available ( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * )
547+ public func testUploadTaskWithRequestDelegateAsync( ) async throws {
548+ let url = URL ( string: " http://localhost:33333/success " ) !
549+ let request = URLRequest ( url: url)
550+ let session = URLSession ( configuration: URLSessionConfiguration . default, delegate: sessionDelegate, delegateQueue: nil )
551+ _ = try await session. upload ( for: request, from: Data ( ) )
552+
553+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled)
554+ XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] )
555+ }
556+
557+ #endif
391558}
0 commit comments