@@ -8,6 +8,7 @@ import XCTest
8
8
import TestUtilities
9
9
import DatadogInternal
10
10
11
+ @testable import DatadogRUM
11
12
@testable import DatadogTrace
12
13
@testable import DatadogCore
13
14
@@ -50,7 +51,7 @@ class NetworkInstrumentationIntegrationTests: XCTestCase {
50
51
core. flushAndTearDown ( )
51
52
core = nil
52
53
}
53
-
54
+
54
55
func testParentSpanPropagation( ) throws {
55
56
let expectation = expectation ( description: " request completes " )
56
57
// Given
@@ -88,4 +89,115 @@ class NetworkInstrumentationIntegrationTests: XCTestCase {
88
89
89
90
class MockDelegate : NSObject , URLSessionDataDelegate {
90
91
}
92
+
93
+ func testResourceAttributesProvider_givenURLSessionDataTaskRequest( ) {
94
+ core = DatadogCoreProxy (
95
+ context: . mockWith(
96
+ env: " test " ,
97
+ version: " 1.1.1 " ,
98
+ serverTimeOffset: 123
99
+ )
100
+ )
101
+
102
+ let providerExpectation = expectation ( description: " provider called " )
103
+ var providerDataCount = 0
104
+ RUM . enable (
105
+ with: . init(
106
+ applicationID: . mockAny( ) ,
107
+ urlSessionTracking: . init(
108
+ resourceAttributesProvider: { req, resp, data, err in
109
+ XCTAssertNotNil ( data)
110
+ XCTAssertTrue ( data!. count > 0 )
111
+ providerDataCount = data!. count
112
+ providerExpectation. fulfill ( )
113
+ return [ : ]
114
+ } )
115
+ ) ,
116
+ in: core
117
+ )
118
+
119
+ URLSessionInstrumentation . enable (
120
+ with: . init(
121
+ delegateClass: InstrumentedSessionDelegate . self
122
+ ) ,
123
+ in: core
124
+ )
125
+
126
+ let session = URLSession (
127
+ configuration: . ephemeral,
128
+ delegate: InstrumentedSessionDelegate ( ) ,
129
+ delegateQueue: nil
130
+ )
131
+ var request = URLRequest ( url: URL ( string: " https://www.datadoghq.com/ " ) !)
132
+ request. httpMethod = " GET "
133
+
134
+ let task = session. dataTask ( with: request)
135
+ task. resume ( )
136
+
137
+ wait ( for: [ providerExpectation] , timeout: 10 )
138
+ XCTAssertTrue ( providerDataCount > 0 )
139
+ }
140
+
141
+ func testResourceAttributesProvider_givenURLSessionDataTaskRequestWithCompletionHandler( ) {
142
+ core = DatadogCoreProxy (
143
+ context: . mockWith(
144
+ env: " test " ,
145
+ version: " 1.1.1 " ,
146
+ serverTimeOffset: 123
147
+ )
148
+ )
149
+
150
+ let providerExpectation = expectation ( description: " provider called " )
151
+ var providerDataCount = 0
152
+ var providerData : Data ?
153
+ RUM . enable (
154
+ with: . init(
155
+ applicationID: . mockAny( ) ,
156
+ urlSessionTracking: . init(
157
+ resourceAttributesProvider: { req, resp, data, err in
158
+ XCTAssertNotNil ( data)
159
+ XCTAssertTrue ( data!. count > 0 )
160
+ providerDataCount = data!. count
161
+ data. map { providerData = $0 }
162
+ providerExpectation. fulfill ( )
163
+ return [ : ]
164
+ } )
165
+ ) ,
166
+ in: core
167
+ )
168
+
169
+ URLSessionInstrumentation . enable (
170
+ with: . init(
171
+ delegateClass: InstrumentedSessionDelegate . self
172
+ ) ,
173
+ in: core
174
+ )
175
+
176
+ let session = URLSession (
177
+ configuration: . ephemeral,
178
+ delegate: InstrumentedSessionDelegate ( ) ,
179
+ delegateQueue: nil
180
+ )
181
+ var request = URLRequest ( url: URL ( string: " https://www.datadoghq.com/ " ) !)
182
+ request. httpMethod = " GET "
183
+
184
+ let taskExpectation = self . expectation ( description: " task completed " )
185
+ var taskDataCount = 0
186
+ var taskData : Data ?
187
+ let task = session. dataTask ( with: request) { data, _, _ in
188
+ XCTAssertNotNil ( data)
189
+ XCTAssertTrue ( data!. count > 0 )
190
+ taskDataCount = data!. count
191
+ data. map { taskData = $0 }
192
+ taskExpectation. fulfill ( )
193
+ }
194
+ task. resume ( )
195
+
196
+ wait ( for: [ providerExpectation, taskExpectation] , timeout: 10 )
197
+ XCTAssertEqual ( providerDataCount, taskDataCount)
198
+ XCTAssertEqual ( providerData, taskData)
199
+ }
200
+
201
+ class InstrumentedSessionDelegate : NSObject , URLSessionDataDelegate {
202
+ }
91
203
}
0 commit comments