@@ -7,6 +7,7 @@ chai.use(chaiAsPromised);
7
7
const expect = chai . expect ;
8
8
import { createMockedConnectionString , createMockedKeyValue , createMockedTokenCredential , mockAppConfigurationClientListConfigurationSettings , restoreMocks , sleepInMs } from "./utils/testHelper" ;
9
9
import { load } from "./exportedApi" ;
10
+
10
11
class HttpRequestHeadersPolicy {
11
12
headers : any ;
12
13
name : string ;
@@ -148,4 +149,200 @@ describe("request tracing", function () {
148
149
149
150
restoreMocks ( ) ;
150
151
} ) ;
152
+
153
+ describe ( "request tracing in Web Worker environment" , ( ) => {
154
+ let originalNavigator ;
155
+ let originalWorkerNavigator ;
156
+ let originalWorkerGlobalScope ;
157
+ let originalImportScripts ;
158
+
159
+ before ( ( ) => {
160
+ // Save the original values to restore them later
161
+ originalNavigator = ( global as any ) . navigator ;
162
+ originalWorkerNavigator = ( global as any ) . WorkerNavigator ;
163
+ originalWorkerGlobalScope = ( global as any ) . WorkerGlobalScope ;
164
+ originalImportScripts = ( global as any ) . importScripts ;
165
+ } ) ;
166
+
167
+ afterEach ( ( ) => {
168
+ // Restore the original values after each test
169
+ ( global as any ) . navigator = originalNavigator ;
170
+ ( global as any ) . WorkerNavigator = originalWorkerNavigator ;
171
+ ( global as any ) . WorkerGlobalScope = originalWorkerGlobalScope ;
172
+ ( global as any ) . importScripts = originalImportScripts ;
173
+ } ) ;
174
+
175
+ it ( "should identify WebWorker environment" , async ( ) => {
176
+ ( global as any ) . WorkerNavigator = function WorkerNavigator ( ) { } ;
177
+ ( global as any ) . navigator = new ( global as any ) . WorkerNavigator ( ) ;
178
+ ( global as any ) . WorkerGlobalScope = function WorkerGlobalScope ( ) { } ;
179
+ ( global as any ) . importScripts = function importScripts ( ) { } ;
180
+
181
+ try {
182
+ await load ( createMockedConnectionString ( fakeEndpoint ) , { clientOptions } ) ;
183
+ } catch ( e ) { /* empty */ }
184
+ expect ( headerPolicy . headers ) . not . undefined ;
185
+ const correlationContext = headerPolicy . headers . get ( "Correlation-Context" ) ;
186
+ expect ( correlationContext ) . not . undefined ;
187
+ expect ( correlationContext . includes ( "Host=WebWorker" ) ) . eq ( true ) ;
188
+ } ) ;
189
+
190
+ it ( "is not WebWorker when WorkerNavigator is undefined" , async ( ) => {
191
+ ( global as any ) . navigator = { userAgent : "node.js" } as any ; // Mock navigator
192
+ ( global as any ) . WorkerNavigator = undefined ;
193
+ ( global as any ) . WorkerGlobalScope = function WorkerGlobalScope ( ) { } ;
194
+ ( global as any ) . importScripts = function importScripts ( ) { } ;
195
+
196
+ try {
197
+ await load ( createMockedConnectionString ( fakeEndpoint ) , { clientOptions } ) ;
198
+ } catch ( e ) { /* empty */ }
199
+ expect ( headerPolicy . headers ) . not . undefined ;
200
+ const correlationContext = headerPolicy . headers . get ( "Correlation-Context" ) ;
201
+ expect ( correlationContext ) . not . undefined ;
202
+ expect ( correlationContext . includes ( "Host=WebWorker" ) ) . eq ( false ) ;
203
+ } ) ;
204
+
205
+ it ( "is not WebWorker when navigator is not an instance of WorkerNavigator" , async ( ) => {
206
+ ( global as any ) . navigator = { userAgent : "node.js" } as any ; // Mock navigator but not an instance of WorkerNavigator
207
+ ( global as any ) . WorkerNavigator = function WorkerNavigator ( ) { } ;
208
+ ( global as any ) . WorkerGlobalScope = function WorkerGlobalScope ( ) { } ;
209
+ ( global as any ) . importScripts = function importScripts ( ) { } ;
210
+
211
+ try {
212
+ await load ( createMockedConnectionString ( fakeEndpoint ) , { clientOptions } ) ;
213
+ } catch ( e ) { /* empty */ }
214
+ expect ( headerPolicy . headers ) . not . undefined ;
215
+ const correlationContext = headerPolicy . headers . get ( "Correlation-Context" ) ;
216
+ expect ( correlationContext ) . not . undefined ;
217
+ expect ( correlationContext . includes ( "Host=WebWorker" ) ) . eq ( false ) ;
218
+ } ) ;
219
+
220
+ it ( "is not WebWorker when WorkerGlobalScope is undefined" , async ( ) => {
221
+ ( global as any ) . WorkerNavigator = function WorkerNavigator ( ) { } ;
222
+ ( global as any ) . navigator = new ( global as any ) . WorkerNavigator ( ) ;
223
+ ( global as any ) . WorkerGlobalScope = undefined ;
224
+ ( global as any ) . importScripts = function importScripts ( ) { } ;
225
+
226
+ try {
227
+ await load ( createMockedConnectionString ( fakeEndpoint ) , { clientOptions } ) ;
228
+ } catch ( e ) { /* empty */ }
229
+ expect ( headerPolicy . headers ) . not . undefined ;
230
+ const correlationContext = headerPolicy . headers . get ( "Correlation-Context" ) ;
231
+ expect ( correlationContext ) . not . undefined ;
232
+ expect ( correlationContext . includes ( "Host=WebWorker" ) ) . eq ( false ) ;
233
+ } ) ;
234
+
235
+ it ( "is not WebWorker when importScripts is undefined" , async ( ) => {
236
+ ( global as any ) . WorkerNavigator = function WorkerNavigator ( ) { } ;
237
+ ( global as any ) . navigator = new ( global as any ) . WorkerNavigator ( ) ;
238
+ ( global as any ) . WorkerGlobalScope = function WorkerGlobalScope ( ) { } ;
239
+ ( global as any ) . importScripts = undefined ;
240
+
241
+ try {
242
+ await load ( createMockedConnectionString ( fakeEndpoint ) , { clientOptions } ) ;
243
+ } catch ( e ) { /* empty */ }
244
+ expect ( headerPolicy . headers ) . not . undefined ;
245
+ const correlationContext = headerPolicy . headers . get ( "Correlation-Context" ) ;
246
+ expect ( correlationContext ) . not . undefined ;
247
+ expect ( correlationContext . includes ( "Host=WebWorker" ) ) . eq ( false ) ;
248
+ } ) ;
249
+ } ) ;
250
+
251
+ describe ( "request tracing in Web Browser environment" , ( ) => {
252
+ let originalWindowType ;
253
+ let originalWindowObject ;
254
+ let originalDocumentType ;
255
+ let originalDocumentObject ;
256
+
257
+ before ( ( ) => {
258
+ // Save the original values to restore them later
259
+ originalWindowType = ( global as any ) . Window ;
260
+ originalWindowObject = ( global as any ) . window ;
261
+ originalDocumentType = ( global as any ) . Document ;
262
+ originalDocumentObject = ( global as any ) . document ;
263
+ } ) ;
264
+
265
+ afterEach ( ( ) => {
266
+ // Restore the original values after each test
267
+ ( global as any ) . Window = originalWindowType ;
268
+ ( global as any ) . window = originalWindowObject ;
269
+ ( global as any ) . Document = originalDocumentType ;
270
+ ( global as any ) . document = originalDocumentObject ;
271
+ } ) ;
272
+
273
+ it ( "should identify Web environment" , async ( ) => {
274
+ ( global as any ) . Window = function Window ( ) { } ;
275
+ ( global as any ) . window = new ( global as any ) . Window ( ) ;
276
+ ( global as any ) . Document = function Document ( ) { } ;
277
+ ( global as any ) . document = new ( global as any ) . Document ( ) ;
278
+
279
+ try {
280
+ await load ( createMockedConnectionString ( fakeEndpoint ) , { clientOptions } ) ;
281
+ } catch ( e ) { /* empty */ }
282
+ expect ( headerPolicy . headers ) . not . undefined ;
283
+ const correlationContext = headerPolicy . headers . get ( "Correlation-Context" ) ;
284
+ expect ( correlationContext ) . not . undefined ;
285
+ expect ( correlationContext . includes ( "Host=Web" ) ) . eq ( true ) ;
286
+ } ) ;
287
+
288
+ it ( "is not Web when document is undefined" , async ( ) => {
289
+ ( global as any ) . Window = function Window ( ) { } ;
290
+ ( global as any ) . window = new ( global as any ) . Window ( ) ;
291
+ ( global as any ) . Document = function Document ( ) { } ;
292
+ ( global as any ) . document = undefined ; // not an instance of Document
293
+
294
+ try {
295
+ await load ( createMockedConnectionString ( fakeEndpoint ) , { clientOptions } ) ;
296
+ } catch ( e ) { /* empty */ }
297
+ expect ( headerPolicy . headers ) . not . undefined ;
298
+ const correlationContext = headerPolicy . headers . get ( "Correlation-Context" ) ;
299
+ expect ( correlationContext ) . not . undefined ;
300
+ expect ( correlationContext . includes ( "Host=Web" ) ) . eq ( false ) ;
301
+ } ) ;
302
+
303
+ it ( "is not Web when document is not instance of Document" , async ( ) => {
304
+ ( global as any ) . Window = function Window ( ) { } ;
305
+ ( global as any ) . window = new ( global as any ) . Window ( ) ;
306
+ ( global as any ) . Document = function Document ( ) { } ;
307
+ ( global as any ) . document = { } ; // Not an instance of Document
308
+
309
+ try {
310
+ await load ( createMockedConnectionString ( fakeEndpoint ) , { clientOptions } ) ;
311
+ } catch ( e ) { /* empty */ }
312
+ expect ( headerPolicy . headers ) . not . undefined ;
313
+ const correlationContext = headerPolicy . headers . get ( "Correlation-Context" ) ;
314
+ expect ( correlationContext ) . not . undefined ;
315
+ expect ( correlationContext . includes ( "Host=Web" ) ) . eq ( false ) ;
316
+ } ) ;
317
+
318
+ it ( "is not Web when window is undefined" , async ( ) => {
319
+ ( global as any ) . Window = function Window ( ) { } ;
320
+ ( global as any ) . window = undefined ; // not an instance of Window
321
+ ( global as any ) . Document = function Document ( ) { } ;
322
+ ( global as any ) . document = new ( global as any ) . Document ( ) ;
323
+
324
+ try {
325
+ await load ( createMockedConnectionString ( fakeEndpoint ) , { clientOptions } ) ;
326
+ } catch ( e ) { /* empty */ }
327
+ expect ( headerPolicy . headers ) . not . undefined ;
328
+ const correlationContext = headerPolicy . headers . get ( "Correlation-Context" ) ;
329
+ expect ( correlationContext ) . not . undefined ;
330
+ expect ( correlationContext . includes ( "Host=Web" ) ) . eq ( false ) ;
331
+ } ) ;
332
+
333
+ it ( "is not Web when window is not instance of Window" , async ( ) => {
334
+ ( global as any ) . Window = function Window ( ) { } ;
335
+ ( global as any ) . window = { } ; // not an instance of Window
336
+ ( global as any ) . Document = function Document ( ) { } ;
337
+ ( global as any ) . document = new ( global as any ) . Document ( ) ;
338
+
339
+ try {
340
+ await load ( createMockedConnectionString ( fakeEndpoint ) , { clientOptions } ) ;
341
+ } catch ( e ) { /* empty */ }
342
+ expect ( headerPolicy . headers ) . not . undefined ;
343
+ const correlationContext = headerPolicy . headers . get ( "Correlation-Context" ) ;
344
+ expect ( correlationContext ) . not . undefined ;
345
+ expect ( correlationContext . includes ( "Host=Web" ) ) . eq ( false ) ;
346
+ } ) ;
347
+ } ) ;
151
348
} ) ;
0 commit comments