@datadog/mobile-react-native 1.0.0-rc2 → 1.0.0-rc5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/DatadogSDKReactNative.podspec +1 -1
  2. package/README.md +8 -121
  3. package/android/build.gradle +2 -3
  4. package/android/src/main/java/com/datadog/reactnative/DdRum.kt +25 -24
  5. package/android/src/main/java/com/datadog/reactnative/DdTrace.kt +6 -6
  6. package/ios/Sources/DdRum.m +10 -9
  7. package/ios/Sources/DdRum.swift +24 -24
  8. package/ios/Sources/DdTrace.m +2 -2
  9. package/ios/Sources/DdTrace.swift +6 -6
  10. package/ios/Sources/QueueHolder.swift +1 -1
  11. package/lib/commonjs/DdSdkReactNative.js +63 -0
  12. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  13. package/lib/commonjs/DdSdkReactNativeConfiguration.js +9 -1
  14. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  15. package/lib/commonjs/InternalLog.js +51 -0
  16. package/lib/commonjs/InternalLog.js.map +1 -0
  17. package/lib/commonjs/ProxyConfiguration.js +53 -0
  18. package/lib/commonjs/ProxyConfiguration.js.map +1 -0
  19. package/lib/commonjs/SdkVerbosity.js +26 -0
  20. package/lib/commonjs/SdkVerbosity.js.map +1 -0
  21. package/lib/commonjs/Timer.js +99 -0
  22. package/lib/commonjs/Timer.js.map +1 -0
  23. package/lib/commonjs/foundation.js +102 -45
  24. package/lib/commonjs/foundation.js.map +1 -1
  25. package/lib/commonjs/index.js +28 -6
  26. package/lib/commonjs/index.js.map +1 -1
  27. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js +6 -2
  28. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  29. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +61 -14
  30. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  31. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +110 -17
  32. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  33. package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js +16 -4
  34. package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
  35. package/lib/commonjs/rum/instrumentation/DdRumXhr.js +4 -0
  36. package/lib/commonjs/rum/instrumentation/NoOpEventsInterceptor.js +1 -1
  37. package/lib/commonjs/rum/instrumentation/NoOpEventsInterceptor.js.map +1 -1
  38. package/lib/commonjs/types.js.map +1 -1
  39. package/lib/commonjs/version.js +10 -0
  40. package/lib/commonjs/version.js.map +1 -0
  41. package/lib/module/DdSdkReactNative.js +54 -1
  42. package/lib/module/DdSdkReactNative.js.map +1 -1
  43. package/lib/module/DdSdkReactNativeConfiguration.js +9 -1
  44. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  45. package/lib/module/InternalLog.js +46 -0
  46. package/lib/module/InternalLog.js.map +1 -0
  47. package/lib/module/ProxyConfiguration.js +46 -0
  48. package/lib/module/ProxyConfiguration.js.map +1 -0
  49. package/lib/module/SdkVerbosity.js +18 -0
  50. package/lib/module/SdkVerbosity.js.map +1 -0
  51. package/lib/module/Timer.js +90 -0
  52. package/lib/module/Timer.js.map +1 -0
  53. package/lib/module/foundation.js +69 -44
  54. package/lib/module/foundation.js.map +1 -1
  55. package/lib/module/index.js +3 -1
  56. package/lib/module/index.js.map +1 -1
  57. package/lib/module/rum/instrumentation/DdEventsInterceptor.js +5 -1
  58. package/lib/module/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  59. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +56 -14
  60. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  61. package/lib/module/rum/instrumentation/DdRumResourceTracking.js +99 -17
  62. package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  63. package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js +12 -4
  64. package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
  65. package/lib/module/rum/instrumentation/DdRumXhr.js +1 -1
  66. package/lib/module/rum/instrumentation/NoOpEventsInterceptor.js +1 -1
  67. package/lib/module/rum/instrumentation/NoOpEventsInterceptor.js.map +1 -1
  68. package/lib/module/types.js.map +1 -1
  69. package/lib/module/version.js +3 -0
  70. package/lib/module/version.js.map +1 -0
  71. package/lib/typescript/DdSdkReactNative.d.ts +9 -0
  72. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +5 -2
  73. package/lib/typescript/InternalLog.d.ts +6 -0
  74. package/lib/typescript/ProxyConfiguration.d.ts +56 -0
  75. package/lib/typescript/SdkVerbosity.d.ts +9 -0
  76. package/lib/typescript/Timer.d.ts +20 -0
  77. package/lib/typescript/index.d.ts +3 -1
  78. package/lib/typescript/rum/instrumentation/DdRumErrorTracking.d.ts +2 -0
  79. package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +4 -0
  80. package/lib/typescript/rum/instrumentation/DdRumXhr.d.ts +2 -2
  81. package/lib/typescript/types.d.ts +12 -11
  82. package/lib/typescript/version.d.ts +1 -0
  83. package/package.json +3 -3
  84. package/src/DdSdkReactNative.tsx +57 -8
  85. package/src/DdSdkReactNativeConfiguration.tsx +6 -3
  86. package/src/InternalLog.tsx +48 -0
  87. package/src/ProxyConfiguration.tsx +37 -0
  88. package/src/{NativeSdkVerbosity.tsx → SdkVerbosity.tsx} +2 -2
  89. package/src/Timer.ts +96 -0
  90. package/src/__tests__/DdRumWrapper.test.tsx +91 -0
  91. package/src/__tests__/DdSdkReactNative.test.tsx +213 -13
  92. package/src/__tests__/InternalLog.test.tsx +382 -0
  93. package/src/__tests__/Timer.test.ts +145 -0
  94. package/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx +155 -12
  95. package/src/__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx +270 -26
  96. package/src/foundation.tsx +43 -33
  97. package/src/index.tsx +4 -2
  98. package/src/rum/instrumentation/DdRumErrorTracking.tsx +68 -27
  99. package/src/rum/instrumentation/DdRumResourceTracking.tsx +96 -22
  100. package/src/rum/instrumentation/DdRumUserInteractionTracking.tsx +4 -0
  101. package/src/rum/instrumentation/DdRumXhr.tsx +6 -5
  102. package/src/types.tsx +12 -11
  103. package/src/version.ts +2 -0
  104. package/lib/commonjs/NativeSdkVerbosity.js +0 -26
  105. package/lib/commonjs/NativeSdkVerbosity.js.map +0 -1
  106. package/lib/module/NativeSdkVerbosity.js +0 -18
  107. package/lib/module/NativeSdkVerbosity.js.map +0 -1
  108. package/lib/typescript/NativeSdkVerbosity.d.ts +0 -9
@@ -14,9 +14,7 @@ jest.mock('../../../foundation', () => {
14
14
  DdRum: {
15
15
  // eslint-disable-next-line @typescript-eslint/no-empty-function
16
16
  addError: jest.fn().mockImplementation(() => {
17
- return new Promise<void>((resolve, reject) => {
18
- resolve()
19
- })
17
+ return Promise.resolve();
20
18
  })
21
19
  },
22
20
  };
@@ -53,12 +51,36 @@ afterEach(() => {
53
51
  console.error = originalConsoleError
54
52
  })
55
53
 
54
+ it('M intercept and send a RUM event W onGlobalError() {no message}', async () => {
55
+ // GIVEN
56
+ DdRumErrorTracking.startTracking();
57
+ const is_fatal = Math.random() < 0.5;
58
+ const error = {
59
+ stack: ["doSomething() at ./path/to/file.js:67:3"]
60
+ };
61
+
62
+ // WHEN
63
+ DdRumErrorTracking.onGlobalError(error, is_fatal);
64
+ await flushPromises();
65
+
66
+ // THEN
67
+ expect(DdRum.addError.mock.calls.length).toBe(1);
68
+ expect(DdRum.addError.mock.calls[0][0]).toBe(String(error));
69
+ expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
70
+ expect(DdRum.addError.mock.calls[0][2]).toBe("doSomething() at ./path/to/file.js:67:3");
71
+ const attributes = DdRum.addError.mock.calls[0][3];
72
+ expect(attributes["_dd.error.raw"]).toStrictEqual(error);
73
+ expect(attributes["_dd.error.is_crash"]).toStrictEqual(is_fatal);
74
+ expect(baseErrorHandlerCalled).toStrictEqual(true);
75
+ })
56
76
 
57
77
  it('M intercept and send a RUM event W onGlobalError() {empty stack trace}', async () => {
58
78
  // GIVEN
59
79
  DdRumErrorTracking.startTracking();
60
80
  const is_fatal = Math.random() < 0.5;
61
- const error = new Error('Something bad happened');
81
+ const error = {
82
+ message: "Something bad happened"
83
+ };
62
84
 
63
85
  // WHEN
64
86
  DdRumErrorTracking.onGlobalError(error, is_fatal);
@@ -66,7 +88,7 @@ it('M intercept and send a RUM event W onGlobalError() {empty stack trace}', asy
66
88
 
67
89
  // THEN
68
90
  expect(DdRum.addError.mock.calls.length).toBe(1);
69
- expect(DdRum.addError.mock.calls[0][0]).toBe(String(error));
91
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Something bad happened");
70
92
  expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
71
93
  expect(DdRum.addError.mock.calls[0][2]).toBe("");
72
94
  const attributes = DdRum.addError.mock.calls[0][3];
@@ -75,6 +97,28 @@ it('M intercept and send a RUM event W onGlobalError() {empty stack trace}', asy
75
97
  expect(baseErrorHandlerCalled).toStrictEqual(true);
76
98
  })
77
99
 
100
+ it('M intercept and send a RUM event W onGlobalError() {Error object}', async () => {
101
+ // GIVEN
102
+ DdRumErrorTracking.startTracking();
103
+ const is_fatal = Math.random() < 0.5;
104
+ const error = new Error('Something bad happened');
105
+
106
+ // WHEN
107
+ DdRumErrorTracking.onGlobalError(error, is_fatal);
108
+ await flushPromises();
109
+
110
+ // THEN
111
+ expect(DdRum.addError.mock.calls.length).toBe(1);
112
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Something bad happened");
113
+ expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
114
+ expect(DdRum.addError.mock.calls[0][2]).toContain("Error: Something bad happened");
115
+ expect(DdRum.addError.mock.calls[0][2]).toContain("/packages/core/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx");
116
+ const attributes = DdRum.addError.mock.calls[0][3];
117
+ expect(attributes["_dd.error.raw"]).toStrictEqual(error);
118
+ expect(attributes["_dd.error.is_crash"]).toStrictEqual(is_fatal);
119
+ expect(baseErrorHandlerCalled).toStrictEqual(true);
120
+ })
121
+
78
122
 
79
123
  it('M intercept and send a RUM event W onGlobalError() {with source file info}', async () => {
80
124
  // GIVEN
@@ -93,7 +137,7 @@ it('M intercept and send a RUM event W onGlobalError() {with source file info}',
93
137
 
94
138
  // THEN
95
139
  expect(DdRum.addError.mock.calls.length).toBe(1);
96
- expect(DdRum.addError.mock.calls[0][0]).toBe(String(error));
140
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Something bad happened");
97
141
  expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
98
142
  expect(DdRum.addError.mock.calls[0][2]).toBe("at ./path/to/file.js:1038:57");
99
143
  const attributes = DdRum.addError.mock.calls[0][3];
@@ -118,7 +162,55 @@ it('M intercept and send a RUM event W onGlobalError() {with component stack}',
118
162
 
119
163
  // THEN
120
164
  expect(DdRum.addError.mock.calls.length).toBe(1);
121
- expect(DdRum.addError.mock.calls[0][0]).toBe(String(error));
165
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Something bad happened");
166
+ expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
167
+ expect(DdRum.addError.mock.calls[0][2]).toBe("doSomething() at ./path/to/file.js:67:3,nestedCall() at ./path/to/file.js:1064:9,root() at ./path/to/index.js:10:1");
168
+ const attributes = DdRum.addError.mock.calls[0][3];
169
+ expect(attributes["_dd.error.raw"]).toStrictEqual(error);
170
+ expect(attributes["_dd.error.is_crash"]).toStrictEqual(is_fatal);
171
+ expect(baseErrorHandlerCalled).toStrictEqual(true);
172
+ })
173
+
174
+ it('M intercept and send a RUM event W onGlobalError() {with stack}', async () => {
175
+ // GIVEN
176
+ DdRumErrorTracking.startTracking();
177
+ const is_fatal = Math.random() < 0.5;
178
+ const error = {
179
+ stack: ["doSomething() at ./path/to/file.js:67:3", "nestedCall() at ./path/to/file.js:1064:9", "root() at ./path/to/index.js:10:1"],
180
+ message: "Something bad happened"
181
+ };
182
+
183
+ // WHEN
184
+ DdRumErrorTracking.onGlobalError(error, is_fatal);
185
+ await flushPromises();
186
+
187
+ // THEN
188
+ expect(DdRum.addError.mock.calls.length).toBe(1);
189
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Something bad happened");
190
+ expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
191
+ expect(DdRum.addError.mock.calls[0][2]).toBe("doSomething() at ./path/to/file.js:67:3,nestedCall() at ./path/to/file.js:1064:9,root() at ./path/to/index.js:10:1");
192
+ const attributes = DdRum.addError.mock.calls[0][3];
193
+ expect(attributes["_dd.error.raw"]).toStrictEqual(error);
194
+ expect(attributes["_dd.error.is_crash"]).toStrictEqual(is_fatal);
195
+ expect(baseErrorHandlerCalled).toStrictEqual(true);
196
+ })
197
+
198
+ it('M intercept and send a RUM event W onGlobalError() {with stacktrace}', async () => {
199
+ // GIVEN
200
+ DdRumErrorTracking.startTracking();
201
+ const is_fatal = Math.random() < 0.5;
202
+ const error = {
203
+ stacktrace: ["doSomething() at ./path/to/file.js:67:3", "nestedCall() at ./path/to/file.js:1064:9", "root() at ./path/to/index.js:10:1"],
204
+ message: "Something bad happened"
205
+ };
206
+
207
+ // WHEN
208
+ DdRumErrorTracking.onGlobalError(error, is_fatal);
209
+ await flushPromises();
210
+
211
+ // THEN
212
+ expect(DdRum.addError.mock.calls.length).toBe(1);
213
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Something bad happened");
122
214
  expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
123
215
  expect(DdRum.addError.mock.calls[0][2]).toBe("doSomething() at ./path/to/file.js:67:3,nestedCall() at ./path/to/file.js:1064:9,root() at ./path/to/index.js:10:1");
124
216
  const attributes = DdRum.addError.mock.calls[0][3];
@@ -127,15 +219,45 @@ it('M intercept and send a RUM event W onGlobalError() {with component stack}',
127
219
  expect(baseErrorHandlerCalled).toStrictEqual(true);
128
220
  })
129
221
 
222
+ it('M not report error in console handler W onGlobalError() {with console reporting handler}', async () => {
223
+ // GIVEN
224
+ const consoleReportingErrorHandler = jest.fn((error, isFatal)=> {
225
+ console.error(error.message);
226
+ baseErrorHandler(error, isFatal);
227
+ });
228
+ ErrorUtils.setGlobalHandler(consoleReportingErrorHandler);
229
+ DdRumErrorTracking.startTracking();
230
+ const is_fatal = Math.random() < 0.5;
231
+ const error = {
232
+ componentStack: ["doSomething() at ./path/to/file.js:67:3", "nestedCall() at ./path/to/file.js:1064:9", "root() at ./path/to/index.js:10:1"],
233
+ message: "Something bad happened"
234
+ };
235
+
236
+ // WHEN
237
+ DdRumErrorTracking.onGlobalError(error, is_fatal);
238
+ await flushPromises();
239
+
240
+ // THEN
241
+ expect(DdRum.addError.mock.calls.length).toBe(1);
242
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Something bad happened");
243
+ expect(DdRum.addError.mock.calls[0][1]).toBe("SOURCE");
244
+ expect(DdRum.addError.mock.calls[0][2]).toBe("doSomething() at ./path/to/file.js:67:3,nestedCall() at ./path/to/file.js:1064:9,root() at ./path/to/index.js:10:1");
245
+ const attributes = DdRum.addError.mock.calls[0][3];
246
+ expect(attributes["_dd.error.raw"]).toStrictEqual(error);
247
+ expect(attributes["_dd.error.is_crash"]).toStrictEqual(is_fatal);
248
+ expect(consoleReportingErrorHandler).toBeCalledTimes(1);
249
+ expect(baseConsoleErrorCalled).toStrictEqual(false);
250
+ })
251
+
130
252
  it('M intercept and send a RUM event W onConsole() {Error with source file info}', async () => {
131
253
  // GIVEN
132
254
  DdRumErrorTracking.startTracking();
133
- const message= "Something bad happened";
255
+ const message= "Oops I did it again!";
134
256
  const error = {
135
257
  sourceURL: "./path/to/file.js",
136
258
  line: 1038,
137
259
  column: 57,
138
- message: message
260
+ message: "Something bad happened"
139
261
  };
140
262
 
141
263
  // WHEN
@@ -144,7 +266,7 @@ it('M intercept and send a RUM event W onConsole() {Error with source file info}
144
266
 
145
267
  // THEN
146
268
  expect(DdRum.addError.mock.calls.length).toBe(1);
147
- expect(DdRum.addError.mock.calls[0][0]).toBe(message + " " + JSON.stringify(error));
269
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Oops I did it again! Something bad happened");
148
270
  expect(DdRum.addError.mock.calls[0][1]).toBe("CONSOLE");
149
271
  expect(DdRum.addError.mock.calls[0][2]).toBe("at ./path/to/file.js:1038:57");
150
272
  expect(DdRum.addError.mock.calls[0][3]).toBeUndefined()
@@ -154,7 +276,7 @@ it('M intercept and send a RUM event W onConsole() {Error with source file info}
154
276
  it('M intercept and send a RUM event W onConsole() {Error with component stack}', async () => {
155
277
  // GIVEN
156
278
  DdRumErrorTracking.startTracking();
157
- const message= "Something bad happened";
279
+ const message= "Oops I did it again!";
158
280
  const error = {
159
281
  componentStack: ["doSomething() at ./path/to/file.js:67:3", "nestedCall() at ./path/to/file.js:1064:9", "root() at ./path/to/index.js:10:1"],
160
282
  message: "Something bad happened"
@@ -166,7 +288,7 @@ it('M intercept and send a RUM event W onConsole() {Error with component stack}'
166
288
 
167
289
  // THEN
168
290
  expect(DdRum.addError.mock.calls.length).toBe(1);
169
- expect(DdRum.addError.mock.calls[0][0]).toBe(message + " " + JSON.stringify(error));
291
+ expect(DdRum.addError.mock.calls[0][0]).toBe("Oops I did it again! Something bad happened");
170
292
  expect(DdRum.addError.mock.calls[0][1]).toBe("CONSOLE");
171
293
  expect(DdRum.addError.mock.calls[0][2]).toBe("doSomething() at ./path/to/file.js:67:3,nestedCall() at ./path/to/file.js:1064:9,root() at ./path/to/index.js:10:1");
172
294
  expect(DdRum.addError.mock.calls[0][3]).toBeUndefined()
@@ -190,3 +312,24 @@ it('M intercept and send a RUM event W onConsole() {message only}', async () =>
190
312
  expect(DdRum.addError.mock.calls[0][3]).toBeUndefined()
191
313
  expect(baseConsoleErrorCalled).toStrictEqual(true);
192
314
  })
315
+
316
+ describe.each(
317
+ [[undefined], [null], [true], [1], ['message'], [() => { }], [{}], [['a']]]
318
+ )('console calls with different message types', (message) => {
319
+ it(`M intercept and send a RUM event W onConsole() { message has ${typeof (message)} type }`, async () => {
320
+ // GIVEN
321
+ DdRumErrorTracking.startTracking();
322
+
323
+ // WHEN
324
+ DdRumErrorTracking.onConsoleError(message);
325
+ await flushPromises();
326
+
327
+ // THEN
328
+ expect(DdRum.addError.mock.calls.length).toBe(1);
329
+ expect(DdRum.addError.mock.calls[0][0]).toBe(message == undefined ? "Unknown Error" : String(message));
330
+ expect(DdRum.addError.mock.calls[0][1]).toBe("CONSOLE");
331
+ expect(DdRum.addError.mock.calls[0][2]).toBe("");
332
+ expect(DdRum.addError.mock.calls[0][3]).toBeUndefined()
333
+ expect(baseConsoleErrorCalled).toStrictEqual(true);
334
+ })
335
+ })
@@ -4,7 +4,16 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import { DdRumResourceTracking, PARENT_ID_HEADER_KEY, TRACE_ID_HEADER_KEY, ORIGIN_RUM, ORIGIN_HEADER_KEY } from '../../../rum/instrumentation/DdRumResourceTracking'
7
+ import {
8
+ DdRumResourceTracking,
9
+ PARENT_ID_HEADER_KEY,
10
+ TRACE_ID_HEADER_KEY,
11
+ ORIGIN_RUM,
12
+ ORIGIN_HEADER_KEY,
13
+ SAMPLING_PRIORITY_HEADER_KEY,
14
+ SAMPLED_HEADER_KEY,
15
+ calculateResponseSize
16
+ } from '../../../rum/instrumentation/DdRumResourceTracking'
8
17
  import { DdRum } from '../../../index';
9
18
  import { Platform } from 'react-native';
10
19
 
@@ -13,23 +22,21 @@ jest.useFakeTimers()
13
22
  jest.mock('../../../foundation', () => {
14
23
  return {
15
24
  DdRum: {
16
- // eslint-disable-next-line @typescript-eslint/no-empty-function
17
- startResource: jest.fn().mockImplementation(() => {
18
- return new Promise<void>((resolve, reject) => {
19
- resolve()
20
- })
25
+ startResource: jest.fn().mockResolvedValue(() => {
26
+ return Promise.resolve()
21
27
  }),
22
28
 
23
- // eslint-disable-next-line @typescript-eslint/no-empty-function
24
- stopResource: jest.fn().mockImplementation(() => {
25
- return new Promise<void>((resolve, reject) => {
26
- resolve()
27
- })
29
+ stopResource: jest.fn().mockResolvedValue(() => {
30
+ return Promise.resolve()
28
31
  })
29
32
  },
30
33
  };
31
34
  });
32
35
 
36
+ function randomInt(max: number): number {
37
+ return Math.floor(Math.random() * max)
38
+ }
39
+
33
40
  class XMLHttpRequest {
34
41
 
35
42
  static readonly UNSENT = 0;
@@ -38,10 +45,12 @@ class XMLHttpRequest {
38
45
  static readonly LOADING = 3;
39
46
  static readonly DONE = 4;
40
47
 
41
- public response: string | undefined = undefined
42
- public status: number | undefined = undefined
43
- public readyState: number = XMLHttpRequest.UNSENT
44
- public headers: Map<String, String> = new Map()
48
+ public response?: any;
49
+ public responseType?: string;
50
+ public status: number = 0;
51
+ public readyState: number = XMLHttpRequest.UNSENT;
52
+ public requestHeaders: Map<String, String> = new Map();
53
+ public responseHeaders: Map<String, String> = new Map();
45
54
 
46
55
  constructor() {
47
56
 
@@ -70,15 +79,26 @@ class XMLHttpRequest {
70
79
  this.onreadystatechange()
71
80
  }
72
81
 
73
- complete(status: number, response?: string) {
82
+ complete(status: number, response?: any, responseType?: string) {
74
83
  this.response = response
84
+ if (response) {
85
+ this.responseType = responseType ?? 'text'
86
+ }
75
87
  this.status = status
76
88
  this.readyState = XMLHttpRequest.DONE
77
89
  this.onreadystatechange()
78
90
  }
79
91
 
80
92
  setRequestHeader(header: string, value: string): void {
81
- this.headers[header] = value
93
+ this.requestHeaders[header] = value
94
+ }
95
+
96
+ setResponseHeader(header: string, value: string): void {
97
+ this.responseHeaders[header] = value
98
+ }
99
+
100
+ getResponseHeader(header: string): string | null {
101
+ return this.responseHeaders[header];
82
102
  }
83
103
  }
84
104
 
@@ -128,6 +148,7 @@ it('M intercept XHR request W startTracking() + XHR.open() + XHR.send()', async
128
148
  expect(DdRum.stopResource.mock.calls[0][0]).toBe(DdRum.startResource.mock.calls[0][0]);
129
149
  expect(DdRum.stopResource.mock.calls[0][1]).toBe(200);
130
150
  expect(DdRum.stopResource.mock.calls[0][2]).toBe('xhr');
151
+ expect(DdRum.stopResource.mock.calls[0][3]).toBeGreaterThan(0);
131
152
 
132
153
  expect(xhr.originalOpenCalled).toBe(true);
133
154
  expect(xhr.originalSendCalled).toBe(true);
@@ -157,6 +178,7 @@ it('M intercept failing XHR request W startTracking() + XHR.open() + XHR.send()'
157
178
  expect(DdRum.stopResource.mock.calls[0][0]).toBe(DdRum.startResource.mock.calls[0][0]);
158
179
  expect(DdRum.stopResource.mock.calls[0][1]).toBe(500);
159
180
  expect(DdRum.stopResource.mock.calls[0][2]).toBe('xhr');
181
+ expect(DdRum.stopResource.mock.calls[0][3]).toBeGreaterThan(0);
160
182
 
161
183
  expect(xhr.originalOpenCalled).toBe(true);
162
184
  expect(xhr.originalSendCalled).toBe(true);
@@ -186,6 +208,7 @@ it('M intercept aborted XHR request W startTracking() + XHR.open() + XHR.send()
186
208
  expect(DdRum.stopResource.mock.calls[0][0]).toBe(DdRum.startResource.mock.calls[0][0]);
187
209
  expect(DdRum.stopResource.mock.calls[0][1]).toBe(0);
188
210
  expect(DdRum.stopResource.mock.calls[0][2]).toBe('xhr');
211
+ expect(DdRum.stopResource.mock.calls[0][3]).toBe(-1);
189
212
 
190
213
  expect(xhr.originalOpenCalled).toBe(true);
191
214
  expect(xhr.originalSendCalled).toBe(true);
@@ -208,7 +231,7 @@ it('M add the span id in the request headers W startTracking() + XHR.open() + XH
208
231
  await flushPromises();
209
232
 
210
233
  // THEN
211
- const spanId = xhr.headers[PARENT_ID_HEADER_KEY];
234
+ const spanId = xhr.requestHeaders[PARENT_ID_HEADER_KEY];
212
235
  expect(spanId).toBeDefined();
213
236
  expect(spanId).toMatch(/[1-9].+/);
214
237
  })
@@ -228,7 +251,7 @@ it('M add the trace id in the request headers W startTracking() + XHR.open() + X
228
251
  await flushPromises();
229
252
 
230
253
  // THEN
231
- const traceId = xhr.headers[TRACE_ID_HEADER_KEY];
254
+ const traceId = xhr.requestHeaders[TRACE_ID_HEADER_KEY];
232
255
  expect(traceId).toBeDefined();
233
256
  expect(traceId).toMatch(/[1-9].+/);
234
257
  })
@@ -248,8 +271,8 @@ it('M generate different ids for spanId and traceId in request headers', async (
248
271
  await flushPromises();
249
272
 
250
273
  // THEN
251
- const traceId = xhr.headers[TRACE_ID_HEADER_KEY];
252
- const spanId = xhr.headers[PARENT_ID_HEADER_KEY];
274
+ const traceId = xhr.requestHeaders[TRACE_ID_HEADER_KEY];
275
+ const spanId = xhr.requestHeaders[PARENT_ID_HEADER_KEY];
253
276
  expect(traceId !== spanId).toBeTruthy()
254
277
  })
255
278
 
@@ -268,7 +291,43 @@ it('M add origin as RUM in the request headers W startTracking() + XHR.open() +
268
291
  await flushPromises();
269
292
 
270
293
  // THEN
271
- expect(xhr.headers[ORIGIN_HEADER_KEY]).toBe(ORIGIN_RUM)
294
+ expect(xhr.requestHeaders[ORIGIN_HEADER_KEY]).toBe(ORIGIN_RUM)
295
+ })
296
+
297
+ it('M force the agent to keep the request generated trace W startTracking() + XHR.open() + XHR.send()', async () => {
298
+ // GIVEN
299
+ let method = "GET"
300
+ let url = "https://api.example.com/v2/user"
301
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
302
+
303
+ // WHEN
304
+ const xhr = new XMLHttpRequest();
305
+ xhr.open(method, url);
306
+ xhr.send();
307
+ xhr.notifyResponseArrived();
308
+ xhr.complete(200, 'ok');
309
+ await flushPromises();
310
+
311
+ // THEN
312
+ expect(xhr.requestHeaders[SAMPLING_PRIORITY_HEADER_KEY]).toBe("1")
313
+ })
314
+
315
+ it('M mark the request generated trace for sampling W startTracking() + XHR.open() + XHR.send()', async () => {
316
+ // GIVEN
317
+ let method = "GET"
318
+ let url = "https://api.example.com/v2/user"
319
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
320
+
321
+ // WHEN
322
+ const xhr = new XMLHttpRequest();
323
+ xhr.open(method, url);
324
+ xhr.send();
325
+ xhr.notifyResponseArrived();
326
+ xhr.complete(200, 'ok');
327
+ await flushPromises();
328
+
329
+ // THEN
330
+ expect(xhr.requestHeaders[SAMPLED_HEADER_KEY]).toBe("1")
272
331
  })
273
332
 
274
333
  it('M add the span id as resource attributes W startTracking() + XHR.open() + XHR.send()', async () => {
@@ -351,7 +410,7 @@ describe.each([['android'], ['ios']])('timings test', (platform) => {
351
410
  await flushPromises();
352
411
 
353
412
  // THEN
354
- const timings = DdRum.stopResource.mock.calls[0][3]["_dd.resource_timings"];
413
+ const timings = DdRum.stopResource.mock.calls[0][4]["_dd.resource_timings"];
355
414
 
356
415
  if (Platform.OS === 'ios') {
357
416
  expect(timings['firstByte']['startTime']).toBeGreaterThan(0)
@@ -391,7 +450,7 @@ describe.each([['android'], ['ios']])('timings test', (platform) => {
391
450
  await flushPromises();
392
451
 
393
452
  // THEN
394
- const timings = DdRum.stopResource.mock.calls[0][3]["_dd.resource_timings"];
453
+ const timings = DdRum.stopResource.mock.calls[0][4]["_dd.resource_timings"];
395
454
 
396
455
  if (Platform.OS === 'ios') {
397
456
  expect(timings['firstByte']['startTime']).toBeGreaterThan(0)
@@ -427,7 +486,192 @@ it('M not generate resource timings W startTracking() + XHR.open() + XHR.send()
427
486
  await flushPromises();
428
487
 
429
488
  // THEN
430
- const attributes = DdRum.stopResource.mock.calls[0][3];
489
+ const attributes = DdRum.stopResource.mock.calls[0][4];
431
490
 
432
491
  expect(attributes['_dd.resource_timings']).toBeNull()
433
- })
492
+ })
493
+
494
+ describe.each([
495
+ 'blob',
496
+ 'arraybuffer',
497
+ 'text',
498
+ '',
499
+ 'json'
500
+ ].map((responseType) => {
501
+ const xhr = new XMLHttpRequest();
502
+ xhr.readyState = XMLHttpRequest.DONE;
503
+ xhr.responseType = responseType;
504
+ xhr.response = {};
505
+
506
+ const contentLength = randomInt(1_000_000_000)
507
+ xhr.setResponseHeader('Content-Length', contentLength.toString());
508
+ return [xhr, responseType, contentLength]
509
+ }))('Response size from response header', (xhr, responseType, expectedSize) => {
510
+ if (responseType === '') {
511
+ responseType = '_empty_'
512
+ }
513
+ it(`M calculate response size W calculateResponseSize(), responseType=${responseType}`, () => {
514
+ // WHEN
515
+ // @ts-ignore
516
+ const size = calculateResponseSize(xhr);
517
+
518
+ // THEN
519
+ expect(size).toEqual(expectedSize);
520
+ })
521
+ })
522
+
523
+ it('M calculate response size W calculateResponseSize() { responseType=blob }', () => {
524
+ // GIVEN
525
+ const xhr = new XMLHttpRequest();
526
+ xhr.readyState = XMLHttpRequest.DONE;
527
+ xhr.responseType = 'blob';
528
+
529
+ const expectedSize = randomInt(1_000_000)
530
+ xhr.response = { get size() { return expectedSize } }
531
+
532
+ // WHEN
533
+ // @ts-ignore
534
+ const size = calculateResponseSize(xhr);
535
+
536
+ // THEN
537
+ expect(size).toEqual(expectedSize);
538
+ })
539
+
540
+ it('M calculate response size W calculateResponseSize() { responseType=arraybuffer }', () => {
541
+ // GIVEN
542
+ const xhr = new XMLHttpRequest();
543
+ xhr.readyState = XMLHttpRequest.DONE;
544
+ xhr.responseType = 'arraybuffer';
545
+
546
+ const expectedSize = randomInt(100_000);
547
+ xhr.response = new ArrayBuffer(expectedSize);
548
+
549
+ // WHEN
550
+ // @ts-ignore
551
+ const size = calculateResponseSize(xhr);
552
+
553
+ // THEN
554
+ expect(size).toEqual(expectedSize);
555
+ })
556
+
557
+ it('M calculate response size W calculateResponseSize() { responseType=text }', () => {
558
+ // GIVEN
559
+ const xhr = new XMLHttpRequest();
560
+ xhr.readyState = XMLHttpRequest.DONE;
561
+ xhr.responseType = 'text';
562
+
563
+ // size per char is 24, but in bytes it is 33.
564
+ const expectedSize = 33;
565
+ xhr.response = "{\"foo\": \"bar+úñïçôδè ℓ\"}";
566
+
567
+ // WHEN
568
+ // @ts-ignore
569
+ const size = calculateResponseSize(xhr);
570
+
571
+ // THEN
572
+ expect(size).toEqual(expectedSize);
573
+ })
574
+
575
+ it('M calculate response size W calculateResponseSize() { responseType=_empty_ }', () => {
576
+ // GIVEN
577
+ const xhr = new XMLHttpRequest();
578
+ xhr.readyState = XMLHttpRequest.DONE;
579
+ xhr.responseType = '';
580
+
581
+ // size per char is 24, but in bytes it is 33.
582
+ const expectedSize = 33;
583
+ xhr.response = "{\"foo\": \"bar+úñïçôδè ℓ\"}";
584
+
585
+ // WHEN
586
+ // @ts-ignore
587
+ const size = calculateResponseSize(xhr);
588
+
589
+ // THEN
590
+ expect(size).toEqual(expectedSize);
591
+ })
592
+
593
+ it('M calculate response size W calculateResponseSize() { responseType=json }', () => {
594
+ // GIVEN
595
+ const xhr = new XMLHttpRequest();
596
+ xhr.readyState = XMLHttpRequest.DONE;
597
+ xhr.responseType = 'json';
598
+
599
+ const expectedSize = 24;
600
+ xhr.response = { "foo": { "bar": "foobar" } }
601
+
602
+ // WHEN
603
+ // @ts-ignore
604
+ const size = calculateResponseSize(xhr);
605
+
606
+ // THEN
607
+ expect(size).toEqual(expectedSize);
608
+ })
609
+
610
+ it('M not calculate response size W calculateResponseSize() { responseType=document }', () => {
611
+ // GIVEN
612
+ const xhr = new XMLHttpRequest();
613
+ xhr.readyState = XMLHttpRequest.DONE;
614
+ // document type is not supported by RN, so there are no classes to handle it
615
+ xhr.responseType = 'document';
616
+ xhr.response = {};
617
+
618
+ // WHEN
619
+ // @ts-ignore
620
+ const size = calculateResponseSize(xhr);
621
+
622
+ // THEN
623
+ expect(size).toEqual(-1);
624
+ })
625
+
626
+ it('M return 0 W calculateResponseSize() { error is thrown }', () => {
627
+ // GIVEN
628
+ const consoleErrorMock = jest.spyOn(console, 'error').mockImplementation()
629
+ const xhr = new XMLHttpRequest();
630
+ xhr.readyState = XMLHttpRequest.DONE;
631
+ xhr.responseType = 'blob';
632
+ xhr.response = {
633
+ get size() { throw new Error() }
634
+ };
635
+
636
+ // WHEN
637
+ // @ts-ignore
638
+ const size = calculateResponseSize(xhr);
639
+
640
+ // THEN
641
+ expect(size).toEqual(-1);
642
+ expect(consoleErrorMock).toHaveBeenCalled();
643
+
644
+ consoleErrorMock.mockRestore();
645
+ })
646
+
647
+ it('M return 0 W calculateResponseSize() { size is not a number }', () => {
648
+ // GIVEN
649
+ const xhr = new XMLHttpRequest();
650
+ xhr.readyState = XMLHttpRequest.DONE;
651
+ xhr.responseType = 'blob';
652
+
653
+ // we pass empty object, so that .size property is missing, we will get undefined
654
+ xhr.response = {};
655
+
656
+ // WHEN
657
+ // @ts-ignore
658
+ const size = calculateResponseSize(xhr);
659
+
660
+ // THEN
661
+ expect(size).toEqual(-1);
662
+ })
663
+
664
+ it('M return 0 W calculateResponseSize() { no response }', () => {
665
+ // GIVEN
666
+ const xhr = new XMLHttpRequest();
667
+ xhr.readyState = XMLHttpRequest.DONE;
668
+ xhr.responseType = 'blob';
669
+ xhr.response = null;
670
+
671
+ // WHEN
672
+ // @ts-ignore
673
+ const size = calculateResponseSize(xhr);
674
+
675
+ // THEN
676
+ expect(size).toEqual(-1);
677
+ })