@datadog/mobile-react-native 1.0.0-rc9 → 1.1.1

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 (112) hide show
  1. package/DatadogSDKReactNative.podspec +2 -1
  2. package/android/build.gradle +73 -3
  3. package/android/gradle.properties +1 -0
  4. package/android/src/main/kotlin/com/datadog/reactnative/DatadogSDKWrapper.kt +54 -0
  5. package/android/src/main/kotlin/com/datadog/reactnative/DatadogWrapper.kt +86 -0
  6. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdLogs.kt +27 -11
  7. package/android/src/main/kotlin/com/datadog/reactnative/DdRum.kt +305 -0
  8. package/android/src/main/kotlin/com/datadog/reactnative/DdSdk.kt +280 -0
  9. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdSdkBridgeExt.kt +2 -2
  10. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfiguration.kt +29 -0
  11. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdSdkConfigurationExt.kt +2 -4
  12. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdSdkReactNativePackage.kt +3 -0
  13. package/android/src/main/kotlin/com/datadog/reactnative/DdTrace.kt +86 -0
  14. package/android/src/main/kotlin/com/datadog/reactnative/GlobalState.kt +32 -0
  15. package/android/src/main/kotlin/com/datadog/reactnative/NoOpViewTrackingStrategy.kt +23 -0
  16. package/android/src/main/kotlin/com/datadog/reactnative/ProxyAuthenticator.kt +60 -0
  17. package/android/src/test/kotlin/com/datadog/reactnative/DdLogsTest.kt +170 -0
  18. package/android/src/test/kotlin/com/datadog/reactnative/DdRumTest.kt +408 -0
  19. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +1547 -0
  20. package/android/src/test/kotlin/com/datadog/reactnative/DdTraceTest.kt +306 -0
  21. package/android/src/test/kotlin/com/datadog/reactnative/GlobalStateTest.kt +118 -0
  22. package/android/src/test/kotlin/com/datadog/reactnative/ProxyAuthenticatorTest.kt +123 -0
  23. package/android/src/test/kotlin/com/datadog/tools/unit/DdSdkConfigurationExt.kt +33 -0
  24. package/android/src/test/kotlin/com/datadog/tools/unit/GenericAssert.kt +43 -0
  25. package/android/src/test/kotlin/com/datadog/tools/unit/MapExt.kt +22 -0
  26. package/android/src/test/kotlin/com/datadog/tools/unit/ReflectUtils.kt +267 -0
  27. package/android/src/test/kotlin/com/datadog/tools/unit/forge/BaseConfigurator.kt +26 -0
  28. package/android/src/test/kotlin/com/datadog/tools/unit/forge/DdSdkConfigurationForgeryFactory.kt +33 -0
  29. package/android/src/test/kotlin/com/datadog/tools/unit/forge/PackageInfoForgeryFactory.kt +20 -0
  30. package/android/src/test/kotlin/com/datadog/tools/unit/forge/Throwable.kt +31 -0
  31. package/android/src/test/kotlin/com/datadog/tools/unit/forge/ThrowableForgeryFactory.kt +21 -0
  32. package/android/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker +1 -0
  33. package/datadog-sourcemaps.gradle +137 -0
  34. package/ios/Sources/AnyEncodable.swift +138 -0
  35. package/ios/Sources/Attributes.swift +101 -0
  36. package/ios/Sources/DdLogs.swift +35 -11
  37. package/ios/Sources/DdRum.m +3 -1
  38. package/ios/Sources/DdRum.swift +184 -22
  39. package/ios/Sources/DdSdk.swift +190 -10
  40. package/ios/Sources/DdSdkConfiguration.swift +51 -0
  41. package/ios/Sources/DdTrace.swift +41 -6
  42. package/ios/Sources/GlobalState.swift +28 -0
  43. package/ios/Sources/QueueHolder.swift +10 -0
  44. package/ios/Sources/RNDdSdkConfiguration.swift +5 -6
  45. package/lib/commonjs/DdSdkReactNative.js +14 -0
  46. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  47. package/lib/commonjs/DdSdkReactNativeConfiguration.js +24 -0
  48. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  49. package/lib/commonjs/foundation.js +36 -2
  50. package/lib/commonjs/foundation.js.map +1 -1
  51. package/lib/commonjs/index.js +14 -0
  52. package/lib/commonjs/index.js.map +1 -1
  53. package/lib/commonjs/nativeModulesTypes.js +36 -0
  54. package/lib/commonjs/nativeModulesTypes.js.map +1 -0
  55. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js +4 -17
  56. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  57. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +4 -4
  58. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  59. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +7 -2
  60. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -1
  61. package/lib/commonjs/types.js +26 -1
  62. package/lib/commonjs/types.js.map +1 -1
  63. package/lib/commonjs/version.js +1 -1
  64. package/lib/commonjs/version.js.map +1 -1
  65. package/lib/module/DdSdkReactNative.js +14 -0
  66. package/lib/module/DdSdkReactNative.js.map +1 -1
  67. package/lib/module/DdSdkReactNativeConfiguration.js +24 -0
  68. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  69. package/lib/module/foundation.js +36 -2
  70. package/lib/module/foundation.js.map +1 -1
  71. package/lib/module/index.js +2 -1
  72. package/lib/module/index.js.map +1 -1
  73. package/lib/module/nativeModulesTypes.js +26 -0
  74. package/lib/module/nativeModulesTypes.js.map +1 -0
  75. package/lib/module/rum/instrumentation/DdEventsInterceptor.js +2 -14
  76. package/lib/module/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  77. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +3 -4
  78. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  79. package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +7 -2
  80. package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -1
  81. package/lib/module/types.js +23 -0
  82. package/lib/module/types.js.map +1 -1
  83. package/lib/module/version.js +1 -1
  84. package/lib/module/version.js.map +1 -1
  85. package/lib/typescript/DdSdkReactNative.d.ts +2 -0
  86. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +20 -0
  87. package/lib/typescript/foundation.d.ts +3 -2
  88. package/lib/typescript/index.d.ts +2 -1
  89. package/lib/typescript/nativeModulesTypes.d.ts +168 -0
  90. package/lib/typescript/rum/instrumentation/DdEventsInterceptor.d.ts +0 -15
  91. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +2 -1
  92. package/lib/typescript/types.d.ts +42 -7
  93. package/lib/typescript/version.d.ts +1 -1
  94. package/package.json +5 -4
  95. package/src/DdSdkReactNative.tsx +20 -0
  96. package/src/DdSdkReactNativeConfiguration.tsx +20 -0
  97. package/src/__tests__/DdSdkReactNative.test.tsx +76 -0
  98. package/src/__tests__/foundation.test.ts +79 -0
  99. package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +8 -8
  100. package/src/foundation.tsx +40 -11
  101. package/src/index.tsx +3 -0
  102. package/src/nativeModulesTypes.ts +258 -0
  103. package/src/rum/instrumentation/DdEventsInterceptor.tsx +2 -17
  104. package/src/rum/instrumentation/DdRumErrorTracking.tsx +3 -4
  105. package/src/rum/instrumentation/resourceTracking/domain/__tests__/firstPartyHosts.test.ts +22 -0
  106. package/src/rum/instrumentation/resourceTracking/domain/firstPartyHosts.ts +8 -1
  107. package/src/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.ts +2 -1
  108. package/src/types.tsx +62 -7
  109. package/src/version.ts +1 -1
  110. package/android/src/main/java/com/datadog/reactnative/DdRum.kt +0 -142
  111. package/android/src/main/java/com/datadog/reactnative/DdSdk.kt +0 -67
  112. package/android/src/main/java/com/datadog/reactnative/DdTrace.kt +0 -51
@@ -0,0 +1,306 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ package com.datadog.reactnative
8
+
9
+ import com.datadog.tools.unit.toReadableMap
10
+ import com.facebook.react.bridge.Promise
11
+ import com.facebook.react.bridge.ReactApplicationContext
12
+ import com.nhaarman.mockitokotlin2.any
13
+ import com.nhaarman.mockitokotlin2.doAnswer
14
+ import com.nhaarman.mockitokotlin2.doReturn
15
+ import com.nhaarman.mockitokotlin2.mock
16
+ import com.nhaarman.mockitokotlin2.never
17
+ import com.nhaarman.mockitokotlin2.times
18
+ import com.nhaarman.mockitokotlin2.verify
19
+ import com.nhaarman.mockitokotlin2.verifyNoMoreInteractions
20
+ import com.nhaarman.mockitokotlin2.whenever
21
+ import fr.xgouchet.elmyr.annotation.AdvancedForgery
22
+ import fr.xgouchet.elmyr.annotation.DoubleForgery
23
+ import fr.xgouchet.elmyr.annotation.LongForgery
24
+ import fr.xgouchet.elmyr.annotation.MapForgery
25
+ import fr.xgouchet.elmyr.annotation.StringForgery
26
+ import fr.xgouchet.elmyr.annotation.StringForgeryType
27
+ import fr.xgouchet.elmyr.junit5.ForgeExtension
28
+ import io.opentracing.Span
29
+ import io.opentracing.SpanContext
30
+ import io.opentracing.Tracer
31
+ import org.assertj.core.api.Assertions.assertThat
32
+ import org.junit.jupiter.api.AfterEach
33
+ import org.junit.jupiter.api.Assumptions.assumeTrue
34
+ import org.junit.jupiter.api.BeforeEach
35
+ import org.junit.jupiter.api.Test
36
+ import org.junit.jupiter.api.extension.ExtendWith
37
+ import org.junit.jupiter.api.extension.Extensions
38
+ import org.mockito.Mock
39
+ import org.mockito.junit.jupiter.MockitoExtension
40
+ import org.mockito.junit.jupiter.MockitoSettings
41
+ import org.mockito.quality.Strictness
42
+
43
+ @Extensions(
44
+ ExtendWith(MockitoExtension::class),
45
+ ExtendWith(ForgeExtension::class)
46
+ )
47
+ @MockitoSettings(strictness = Strictness.LENIENT)
48
+ internal class DdTraceTest {
49
+
50
+ lateinit var testedTrace: DdTrace
51
+
52
+ @Mock
53
+ lateinit var mockTracer: Tracer
54
+
55
+ @Mock
56
+ lateinit var mockSpanBuilder: Tracer.SpanBuilder
57
+
58
+ @Mock
59
+ lateinit var mockSpanContext: SpanContext
60
+
61
+ @Mock
62
+ lateinit var mockSpan: Span
63
+
64
+ @StringForgery
65
+ lateinit var fakeOperation: String
66
+
67
+ @DoubleForgery(1000000000000.0, 2000000000000.0)
68
+ var fakeTimestamp: Double = 0.0
69
+
70
+ @StringForgery(type = StringForgeryType.HEXADECIMAL)
71
+ lateinit var fakeSpanId: String
72
+
73
+ @StringForgery(type = StringForgeryType.HEXADECIMAL)
74
+ lateinit var fakeTraceId: String
75
+
76
+ @MapForgery(
77
+ key = AdvancedForgery(string = [StringForgery()]),
78
+ value = AdvancedForgery(string = [StringForgery(StringForgeryType.HEXADECIMAL)])
79
+ )
80
+ lateinit var fakeContext: Map<String, String>
81
+
82
+ @MapForgery(
83
+ key = AdvancedForgery(string = [StringForgery()]),
84
+ value = AdvancedForgery(string = [StringForgery(StringForgeryType.HEXADECIMAL)])
85
+ )
86
+ lateinit var fakeGlobalState: Map<String, String>
87
+
88
+ var lastResolvedValue: Any? = null
89
+ private var mockPromise = mock<Promise>().apply {
90
+ whenever(resolve(any())) doAnswer {
91
+ lastResolvedValue = it.arguments[0]
92
+ }
93
+ }
94
+
95
+ @Mock
96
+ lateinit var mockReactContext: ReactApplicationContext
97
+
98
+ @BeforeEach
99
+ fun `set up`() {
100
+ whenever(mockTracer.buildSpan(fakeOperation)) doReturn mockSpanBuilder
101
+ whenever(mockSpanBuilder.withStartTimestamp(fakeTimestamp.toLong() * 1000)) doReturn mockSpanBuilder
102
+ whenever(mockSpanBuilder.start()) doReturn mockSpan
103
+ whenever(mockSpan.context()) doReturn mockSpanContext
104
+ whenever(mockSpanContext.toSpanId()) doReturn fakeSpanId
105
+ whenever(mockSpanContext.toTraceId()) doReturn fakeTraceId
106
+
107
+ testedTrace = DdTrace(mockReactContext, tracerProvider = { mockTracer })
108
+ }
109
+
110
+ @AfterEach
111
+ fun `tear down`() {
112
+ GlobalState.globalAttributes.clear()
113
+ }
114
+
115
+ @Test
116
+ fun `M start a span W startSpan() `() {
117
+ // When
118
+ testedTrace.startSpan(
119
+ fakeOperation,
120
+ fakeContext.toReadableMap(),
121
+ fakeTimestamp,
122
+ mockPromise
123
+ )
124
+
125
+ // Then
126
+ assertThat(lastResolvedValue).isEqualTo(fakeSpanId)
127
+ }
128
+
129
+ @Test
130
+ fun `M start and stop span W startSpan() + finishSpan()`(
131
+ @LongForgery(100L, 2000L) duration: Long,
132
+ @MapForgery(
133
+ key = AdvancedForgery(string = [StringForgery()]),
134
+ value = AdvancedForgery(string = [StringForgery(StringForgeryType.HEXADECIMAL)])
135
+ ) context: Map<String, String>
136
+ ) {
137
+ // Given
138
+ val endTimestamp = fakeTimestamp + duration
139
+
140
+ // When
141
+ testedTrace.startSpan(
142
+ fakeOperation,
143
+ fakeContext.toReadableMap(),
144
+ fakeTimestamp,
145
+ mockPromise
146
+ )
147
+ val id = lastResolvedValue
148
+ testedTrace.finishSpan(id as String, fakeContext.toReadableMap(), endTimestamp, mockPromise)
149
+
150
+ // Then
151
+ assertThat(id).isEqualTo(fakeSpanId)
152
+ verify(mockSpan).finish(endTimestamp.toLong() * 1000)
153
+ }
154
+
155
+ @Test
156
+ fun `M do nothing W startSpan() + finishSpan() with unknown id`(
157
+ @LongForgery(100L, 2000L) duration: Long,
158
+ @StringForgery(type = StringForgeryType.HEXADECIMAL) otherSpanId: String
159
+ ) {
160
+ // Given
161
+ assumeTrue(otherSpanId != fakeSpanId)
162
+ val endTimestamp = fakeTimestamp + duration
163
+
164
+ // When
165
+ testedTrace.startSpan(
166
+ fakeOperation,
167
+ fakeContext.toReadableMap(),
168
+ fakeTimestamp,
169
+ mockPromise
170
+ )
171
+ val id = lastResolvedValue
172
+ testedTrace.finishSpan(otherSpanId, fakeContext.toReadableMap(), endTimestamp, mockPromise)
173
+
174
+ // Then
175
+ assertThat(id).isEqualTo(fakeSpanId)
176
+ verify(mockSpan, never()).finish(any())
177
+ }
178
+
179
+ @Test
180
+ fun `M start and stop span with context on start W startSpan() + finishSpan()`(
181
+ @LongForgery(100L, 2000L) duration: Long
182
+ ) {
183
+ // Given
184
+ val endTimestamp = fakeTimestamp + duration
185
+
186
+ // When
187
+ testedTrace.startSpan(
188
+ fakeOperation,
189
+ fakeContext.toReadableMap(),
190
+ fakeTimestamp,
191
+ mockPromise
192
+ )
193
+ val id = lastResolvedValue
194
+ testedTrace.finishSpan(
195
+ id as String,
196
+ emptyMap<String, String>().toReadableMap(),
197
+ endTimestamp,
198
+ mockPromise
199
+ )
200
+
201
+ // Then
202
+ assertThat(id).isEqualTo(fakeSpanId)
203
+ verify(mockSpan).context()
204
+ verify(mockSpan).finish(endTimestamp.toLong() * 1000)
205
+ fakeContext.forEach {
206
+ verify(mockSpan).setTag(it.key, it.value)
207
+ }
208
+ verifyNoMoreInteractions(mockSpan)
209
+ }
210
+
211
+ @Test
212
+ fun `M start and stop span with context on finish W startSpan() + finishSpan()`(
213
+ @LongForgery(100L, 2000L) duration: Long
214
+ ) {
215
+ // Given
216
+ val endTimestamp = fakeTimestamp + duration
217
+
218
+ // When
219
+ testedTrace.startSpan(
220
+ fakeOperation,
221
+ emptyMap<String, String>().toReadableMap(),
222
+ fakeTimestamp,
223
+ mockPromise
224
+ )
225
+ val id = lastResolvedValue
226
+ testedTrace.finishSpan(id as String, fakeContext.toReadableMap(), endTimestamp, mockPromise)
227
+
228
+ // Then
229
+ assertThat(id).isEqualTo(fakeSpanId)
230
+ verify(mockSpan).context()
231
+ verify(mockSpan).finish(endTimestamp.toLong() * 1000)
232
+ fakeContext.forEach {
233
+ verify(mockSpan).setTag(it.key, it.value)
234
+ }
235
+ verifyNoMoreInteractions(mockSpan)
236
+ }
237
+
238
+ @Test
239
+ fun `M start and stop span with global state on start W startSpan() + finishSpan()`(
240
+ @LongForgery(100L, 2000L) duration: Long
241
+ ) {
242
+ // Given
243
+ val endTimestamp = fakeTimestamp + duration
244
+
245
+ // When
246
+ fakeGlobalState.forEach { (k, v) ->
247
+ GlobalState.addAttribute(k, v)
248
+ }
249
+ testedTrace.startSpan(
250
+ fakeOperation,
251
+ fakeContext.toReadableMap(),
252
+ fakeTimestamp,
253
+ mockPromise
254
+ )
255
+ val id = lastResolvedValue
256
+ testedTrace.finishSpan(
257
+ id as String,
258
+ emptyMap<String, String>().toReadableMap(),
259
+ endTimestamp,
260
+ mockPromise
261
+ )
262
+
263
+ // Then
264
+ assertThat(id).isEqualTo(fakeSpanId)
265
+ verify(mockSpan).context()
266
+ verify(mockSpan).finish(endTimestamp.toLong() * 1000)
267
+ fakeContext.forEach {
268
+ verify(mockSpan).setTag(it.key, it.value)
269
+ }
270
+ fakeGlobalState.forEach {
271
+ verify(mockSpan, times(2)).setTag(it.key, it.value)
272
+ }
273
+ verifyNoMoreInteractions(mockSpan)
274
+ }
275
+
276
+ @Test
277
+ fun `M start and stop span with global state on finish W startSpan() + finishSpan()`(
278
+ @LongForgery(100L, 2000L) duration: Long
279
+ ) {
280
+ // Given
281
+ val endTimestamp = fakeTimestamp + duration
282
+ val expectedAttributes = fakeContext + fakeGlobalState
283
+
284
+ // When
285
+ testedTrace.startSpan(
286
+ fakeOperation,
287
+ emptyMap<String, String>().toReadableMap(),
288
+ fakeTimestamp,
289
+ mockPromise
290
+ )
291
+ val id = lastResolvedValue
292
+ fakeGlobalState.forEach { (k, v) ->
293
+ GlobalState.addAttribute(k, v)
294
+ }
295
+ testedTrace.finishSpan(id as String, fakeContext.toReadableMap(), endTimestamp, mockPromise)
296
+
297
+ // Then
298
+ assertThat(id).isEqualTo(fakeSpanId)
299
+ verify(mockSpan).context()
300
+ verify(mockSpan).finish(endTimestamp.toLong() * 1000)
301
+ expectedAttributes.forEach {
302
+ verify(mockSpan).setTag(it.key, it.value)
303
+ }
304
+ verifyNoMoreInteractions(mockSpan)
305
+ }
306
+ }
@@ -0,0 +1,118 @@
1
+ package com.datadog.reactnative
2
+
3
+ import fr.xgouchet.elmyr.annotation.IntForgery
4
+ import fr.xgouchet.elmyr.annotation.StringForgery
5
+ import fr.xgouchet.elmyr.junit5.ForgeExtension
6
+ import org.assertj.core.api.Assertions.assertThat
7
+ import org.junit.jupiter.api.AfterEach
8
+ import org.junit.jupiter.api.Test
9
+ import org.junit.jupiter.api.extension.ExtendWith
10
+ import org.junit.jupiter.api.extension.Extensions
11
+ import org.mockito.junit.jupiter.MockitoExtension
12
+ import org.mockito.junit.jupiter.MockitoSettings
13
+ import org.mockito.quality.Strictness
14
+
15
+ @Extensions(
16
+ ExtendWith(MockitoExtension::class),
17
+ ExtendWith(ForgeExtension::class)
18
+ )
19
+ @MockitoSettings(strictness = Strictness.LENIENT)
20
+ internal class GlobalStateTest {
21
+
22
+ @AfterEach
23
+ fun `tear down`() {
24
+ GlobalState.globalAttributes.clear()
25
+ }
26
+
27
+ @Test
28
+ fun `𝕄 store attribute 𝕎 addAttribute() {String}`(
29
+ @StringForgery key: String,
30
+ @StringForgery value: String
31
+ ) {
32
+ // When
33
+ GlobalState.addAttribute(key, value)
34
+
35
+ // Then
36
+ assertThat(GlobalState.globalAttributes)
37
+ .hasSize(1)
38
+ .containsEntry(key, value)
39
+ }
40
+
41
+ @Test
42
+ fun `𝕄 store attribute 𝕎 addAttribute() {Int}`(
43
+ @StringForgery key: String,
44
+ @IntForgery value: Int
45
+ ) {
46
+ // When
47
+ GlobalState.addAttribute(key, value)
48
+
49
+ // Then
50
+ assertThat(GlobalState.globalAttributes)
51
+ .hasSize(1)
52
+ .containsEntry(key, value)
53
+ }
54
+
55
+ @Test
56
+ fun `𝕄 remove String attribute 𝕎 addAttribute() {null value}`(
57
+ @StringForgery key: String,
58
+ @StringForgery value: String
59
+ ) {
60
+ // Given
61
+ GlobalState.addAttribute(key, value)
62
+
63
+ // When
64
+ GlobalState.addAttribute(key, null)
65
+
66
+ // Then
67
+ assertThat(GlobalState.globalAttributes)
68
+ .isEmpty()
69
+ }
70
+
71
+ @Test
72
+ fun `𝕄 remove Int attribute 𝕎 addAttribute() {null value}`(
73
+ @StringForgery key: String,
74
+ @IntForgery value: Int
75
+ ) {
76
+ // Given
77
+ GlobalState.addAttribute(key, value)
78
+
79
+ // When
80
+ GlobalState.addAttribute(key, null)
81
+
82
+ // Then
83
+ assertThat(GlobalState.globalAttributes)
84
+ .isEmpty()
85
+ }
86
+
87
+ @Test
88
+ fun `𝕄 remove String attribute 𝕎 removeAttribute()`(
89
+ @StringForgery key: String,
90
+ @StringForgery value: String
91
+ ) {
92
+ // Given
93
+ GlobalState.addAttribute(key, value)
94
+
95
+ // When
96
+ GlobalState.removeAttribute(key)
97
+
98
+ // Then
99
+ assertThat(GlobalState.globalAttributes)
100
+ .isEmpty()
101
+ }
102
+
103
+ @Test
104
+ fun `𝕄 remove Int attribute 𝕎 removeAttribute()`(
105
+ @StringForgery key: String,
106
+ @IntForgery value: Int
107
+ ) {
108
+ // Given
109
+ GlobalState.addAttribute(key, value)
110
+
111
+ // When
112
+ GlobalState.removeAttribute(key)
113
+
114
+ // Then
115
+ assertThat(GlobalState.globalAttributes)
116
+ .isEmpty()
117
+ }
118
+ }
@@ -0,0 +1,123 @@
1
+ package com.datadog.reactnative
2
+
3
+ import fr.xgouchet.elmyr.Forge
4
+ import fr.xgouchet.elmyr.annotation.StringForgery
5
+ import fr.xgouchet.elmyr.junit5.ForgeExtension
6
+ import okhttp3.Credentials
7
+ import okhttp3.Protocol
8
+ import okhttp3.Request
9
+ import okhttp3.Response
10
+ import org.assertj.core.api.Assertions.assertThat
11
+ import org.junit.jupiter.api.BeforeEach
12
+ import org.junit.jupiter.api.Test
13
+ import org.junit.jupiter.api.extension.ExtendWith
14
+ import org.junit.jupiter.api.extension.Extensions
15
+ import org.mockito.junit.jupiter.MockitoExtension
16
+ import org.mockito.junit.jupiter.MockitoSettings
17
+ import org.mockito.quality.Strictness
18
+
19
+ @Extensions(
20
+ ExtendWith(MockitoExtension::class),
21
+ ExtendWith(ForgeExtension::class)
22
+ )
23
+ @MockitoSettings(strictness = Strictness.LENIENT)
24
+ internal class ProxyAuthenticatorTest {
25
+
26
+ @StringForgery
27
+ lateinit var fakeUsername: String
28
+
29
+ @StringForgery
30
+ lateinit var fakePassword: String
31
+
32
+ private lateinit var testedAuthenticator: ProxyAuthenticator
33
+
34
+ @BeforeEach
35
+ fun `set up`() {
36
+ testedAuthenticator = ProxyAuthenticator(fakeUsername, fakePassword)
37
+ }
38
+
39
+ @Test
40
+ fun `M return null W not supported response code`(forge: Forge) {
41
+ // Given
42
+ val response = forge.proxyResponse(
43
+ statusCode =
44
+ forge.anElementFrom(
45
+ forge.anInt(min = 100, max = 407),
46
+ forge.anInt(min = 408, max = 600)
47
+ )
48
+ )
49
+
50
+ // When
51
+ val request = testedAuthenticator.authenticate(null, response)
52
+
53
+ // Then
54
+ assertThat(request).isNull()
55
+ }
56
+
57
+ @Test
58
+ fun `M return null W no known challenges { no Proxy-Authenticate header }`(forge: Forge) {
59
+ // Given
60
+ val response = forge.proxyResponse(statusCode = 407)
61
+
62
+ // When
63
+ val request = testedAuthenticator.authenticate(null, response)
64
+
65
+ // Then
66
+ assertThat(request).isNull()
67
+ }
68
+
69
+ @Test
70
+ fun `M return null W no known challenges { all unsupported }`(forge: Forge) {
71
+ // Given
72
+ val response = forge.proxyResponse(
73
+ statusCode = 407,
74
+ headers = mapOf("Proxy-Authenticate" to forge.anAlphabeticalString())
75
+ )
76
+
77
+ // When
78
+ val request = testedAuthenticator.authenticate(null, response)
79
+
80
+ // Then
81
+ assertThat(request).isNull()
82
+ }
83
+
84
+ @Test
85
+ fun `M return request with credentials W supported challenge`(forge: Forge) {
86
+ // Given
87
+ val response = forge.proxyResponse(
88
+ statusCode = 407,
89
+ headers = mapOf(
90
+ "Proxy-Authenticate" to forge.anElementFrom(
91
+ "OkHttp-Preemptive",
92
+ "Basic"
93
+ )
94
+ )
95
+ )
96
+
97
+ // When
98
+ val request = testedAuthenticator.authenticate(null, response)
99
+
100
+ // Then
101
+ assertThat(request).isNotNull
102
+ assertThat(request?.header("Proxy-Authorization")).isEqualTo(
103
+ Credentials.basic(fakeUsername, fakePassword)
104
+ )
105
+ }
106
+
107
+ private fun Forge.proxyResponse(
108
+ statusCode: Int,
109
+ headers: Map<String, String> = emptyMap()
110
+ ): Response {
111
+ return Response.Builder()
112
+ .request(Request.Builder().url(aStringMatching("http(s?)://[a-z]+\\.com/\\w+")).build())
113
+ .protocol(aValueFrom(Protocol::class.java))
114
+ .code(statusCode)
115
+ .apply {
116
+ headers.forEach {
117
+ header(it.key, it.value)
118
+ }
119
+ }
120
+ .message(anAlphabeticalString())
121
+ .build()
122
+ }
123
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ package com.datadog.tools.unit
8
+
9
+ import com.datadog.reactnative.DdSdkConfiguration
10
+ import com.facebook.react.bridge.ReadableMap
11
+
12
+ fun DdSdkConfiguration.toReadableJavaOnlyMap(): ReadableMap {
13
+ val map = mutableMapOf<String, Any?>()
14
+ map["clientToken"] = clientToken
15
+ map["env"] = env
16
+ applicationId?.let { map.put("applicationId", it) }
17
+ map["nativeCrashReportEnabled"] = if (nativeCrashReportEnabled == null) {
18
+ false
19
+ } else {
20
+ nativeCrashReportEnabled
21
+ }
22
+ if (sampleRate != null) {
23
+ map["sampleRate"] = sampleRate
24
+ } else {
25
+ // we have to put something, because ReadableMap.asDdSdkConfiguration() will call
26
+ // ReadableMap#getDouble which doesn't allow having null value
27
+ map["sampleRate"] = 100f
28
+ }
29
+ site?.let { map.put("site", it) }
30
+ trackingConsent?.let { map.put("trackingConsent", it) }
31
+ additionalConfig?.let { map.put("additionalConfig", it.toReadableMap()) }
32
+ return map.toReadableMap()
33
+ }
@@ -0,0 +1,43 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ package com.datadog.tools.unit
8
+
9
+ import org.assertj.core.api.AbstractAssert
10
+ import org.assertj.core.api.Assertions.assertThat
11
+
12
+ class GenericAssert(actual: Any) :
13
+ AbstractAssert<GenericAssert, Any>(actual, GenericAssert::class.java) {
14
+
15
+ fun hasField(name: String, nestedAssert: (GenericAssert) -> Unit = {}): GenericAssert {
16
+ val field: Any? = actual.getFieldValue(name)
17
+ assertThat(field)
18
+ .overridingErrorMessage(
19
+ "Expecting object to have a non null field named $name, but field was null"
20
+ )
21
+ .isNotNull()
22
+ nestedAssert(GenericAssert(field!!))
23
+ return this
24
+ }
25
+
26
+ fun <F> hasFieldEqualTo(name: String, expected: F): GenericAssert {
27
+ val field: Any? = actual.getFieldValue(name)
28
+ assertThat(field).isEqualTo(expected)
29
+ return this
30
+ }
31
+
32
+ fun isInstanceOf(expectedClassName: String): GenericAssert {
33
+ val className = actual.javaClass.canonicalName!!
34
+ assertThat(className).isEqualTo(expectedClassName)
35
+ return this
36
+ }
37
+
38
+ companion object {
39
+ fun assertThat(actual: Any): GenericAssert {
40
+ return GenericAssert(actual)
41
+ }
42
+ }
43
+ }
@@ -0,0 +1,22 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ package com.datadog.tools.unit
8
+
9
+ import com.facebook.react.bridge.JavaOnlyMap
10
+ import com.facebook.react.bridge.ReadableMap
11
+
12
+ fun Map<*, *>.toReadableMap(): ReadableMap {
13
+ val keysAndValues = mutableListOf<Any?>()
14
+
15
+ entries.forEach {
16
+ keysAndValues.add(it.key)
17
+ keysAndValues.add(it.value)
18
+ }
19
+
20
+ // this FB implementation is not backed by Android-specific .so library, so ok for unit tests
21
+ return JavaOnlyMap.of(*keysAndValues.toTypedArray())
22
+ }