From b3fab948bd72b993c2d9f98e01ddcd1b0578b6de Mon Sep 17 00:00:00 2001 From: Yan Zhang <2351748+Eskibear@users.noreply.github.com> Date: Thu, 25 Jul 2024 10:09:58 +0800 Subject: [PATCH 1/2] Fix reference error in navigator detection --- src/requestTracing/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/requestTracing/utils.ts b/src/requestTracing/utils.ts index 61dda15d..20d19313 100644 --- a/src/requestTracing/utils.ts +++ b/src/requestTracing/utils.ts @@ -159,7 +159,7 @@ function isWebWorker() { // https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope const workerGlobalScopeDefined = typeof WorkerGlobalScope !== "undefined"; // https://developer.mozilla.org/en-US/docs/Web/API/WorkerNavigator - const isNavigatorDefinedAsExpected = typeof navigator === "object" && typeof WorkerNavigator !== "function" && navigator instanceof WorkerNavigator; + const isNavigatorDefinedAsExpected = typeof navigator === "object" && typeof WorkerNavigator === "function" && navigator instanceof WorkerNavigator; // https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#importing_scripts_and_libraries const importScriptsAsGlobalFunction = typeof importScripts === "function"; From 76f30d5de518ed44f9af82c755bbd08f86a2cc66 Mon Sep 17 00:00:00 2001 From: Yan Zhang Date: Thu, 25 Jul 2024 11:19:51 +0800 Subject: [PATCH 2/2] add test cases mocking web/webworker env --- test/requestTracing.test.ts | 197 ++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) diff --git a/test/requestTracing.test.ts b/test/requestTracing.test.ts index 5a01fb04..cdaa7ca3 100644 --- a/test/requestTracing.test.ts +++ b/test/requestTracing.test.ts @@ -7,6 +7,7 @@ chai.use(chaiAsPromised); const expect = chai.expect; import { createMockedConnectionString, createMockedKeyValue, createMockedTokenCredential, mockAppConfigurationClientListConfigurationSettings, restoreMocks, sleepInMs } from "./utils/testHelper"; import { load } from "./exportedApi"; + class HttpRequestHeadersPolicy { headers: any; name: string; @@ -148,4 +149,200 @@ describe("request tracing", function () { restoreMocks(); }); + + describe("request tracing in Web Worker environment", () => { + let originalNavigator; + let originalWorkerNavigator; + let originalWorkerGlobalScope; + let originalImportScripts; + + before(() => { + // Save the original values to restore them later + originalNavigator = (global as any).navigator; + originalWorkerNavigator = (global as any).WorkerNavigator; + originalWorkerGlobalScope = (global as any).WorkerGlobalScope; + originalImportScripts = (global as any).importScripts; + }); + + afterEach(() => { + // Restore the original values after each test + (global as any).navigator = originalNavigator; + (global as any).WorkerNavigator = originalWorkerNavigator; + (global as any).WorkerGlobalScope = originalWorkerGlobalScope; + (global as any).importScripts = originalImportScripts; + }); + + it("should identify WebWorker environment", async () => { + (global as any).WorkerNavigator = function WorkerNavigator() { }; + (global as any).navigator = new (global as any).WorkerNavigator(); + (global as any).WorkerGlobalScope = function WorkerGlobalScope() { }; + (global as any).importScripts = function importScripts() { }; + + try { + await load(createMockedConnectionString(fakeEndpoint), { clientOptions }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext).not.undefined; + expect(correlationContext.includes("Host=WebWorker")).eq(true); + }); + + it("is not WebWorker when WorkerNavigator is undefined", async () => { + (global as any).navigator = { userAgent: "node.js" } as any; // Mock navigator + (global as any).WorkerNavigator = undefined; + (global as any).WorkerGlobalScope = function WorkerGlobalScope() { }; + (global as any).importScripts = function importScripts() { }; + + try { + await load(createMockedConnectionString(fakeEndpoint), { clientOptions }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext).not.undefined; + expect(correlationContext.includes("Host=WebWorker")).eq(false); + }); + + it("is not WebWorker when navigator is not an instance of WorkerNavigator", async () => { + (global as any).navigator = { userAgent: "node.js" } as any; // Mock navigator but not an instance of WorkerNavigator + (global as any).WorkerNavigator = function WorkerNavigator() { }; + (global as any).WorkerGlobalScope = function WorkerGlobalScope() { }; + (global as any).importScripts = function importScripts() { }; + + try { + await load(createMockedConnectionString(fakeEndpoint), { clientOptions }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext).not.undefined; + expect(correlationContext.includes("Host=WebWorker")).eq(false); + }); + + it("is not WebWorker when WorkerGlobalScope is undefined", async () => { + (global as any).WorkerNavigator = function WorkerNavigator() { }; + (global as any).navigator = new (global as any).WorkerNavigator(); + (global as any).WorkerGlobalScope = undefined; + (global as any).importScripts = function importScripts() { }; + + try { + await load(createMockedConnectionString(fakeEndpoint), { clientOptions }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext).not.undefined; + expect(correlationContext.includes("Host=WebWorker")).eq(false); + }); + + it("is not WebWorker when importScripts is undefined", async () => { + (global as any).WorkerNavigator = function WorkerNavigator() { }; + (global as any).navigator = new (global as any).WorkerNavigator(); + (global as any).WorkerGlobalScope = function WorkerGlobalScope() { }; + (global as any).importScripts = undefined; + + try { + await load(createMockedConnectionString(fakeEndpoint), { clientOptions }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext).not.undefined; + expect(correlationContext.includes("Host=WebWorker")).eq(false); + }); + }); + + describe("request tracing in Web Browser environment", () => { + let originalWindowType; + let originalWindowObject; + let originalDocumentType; + let originalDocumentObject; + + before(() => { + // Save the original values to restore them later + originalWindowType = (global as any).Window; + originalWindowObject = (global as any).window; + originalDocumentType = (global as any).Document; + originalDocumentObject = (global as any).document; + }); + + afterEach(() => { + // Restore the original values after each test + (global as any).Window = originalWindowType; + (global as any).window = originalWindowObject; + (global as any).Document = originalDocumentType; + (global as any).document = originalDocumentObject; + }); + + it("should identify Web environment", async () => { + (global as any).Window = function Window() { }; + (global as any).window = new (global as any).Window(); + (global as any).Document = function Document() { }; + (global as any).document = new (global as any).Document(); + + try { + await load(createMockedConnectionString(fakeEndpoint), { clientOptions }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext).not.undefined; + expect(correlationContext.includes("Host=Web")).eq(true); + }); + + it("is not Web when document is undefined", async () => { + (global as any).Window = function Window() { }; + (global as any).window = new (global as any).Window(); + (global as any).Document = function Document() { }; + (global as any).document = undefined; // not an instance of Document + + try { + await load(createMockedConnectionString(fakeEndpoint), { clientOptions }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext).not.undefined; + expect(correlationContext.includes("Host=Web")).eq(false); + }); + + it("is not Web when document is not instance of Document", async () => { + (global as any).Window = function Window() { }; + (global as any).window = new (global as any).Window(); + (global as any).Document = function Document() { }; + (global as any).document = {}; // Not an instance of Document + + try { + await load(createMockedConnectionString(fakeEndpoint), { clientOptions }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext).not.undefined; + expect(correlationContext.includes("Host=Web")).eq(false); + }); + + it("is not Web when window is undefined", async () => { + (global as any).Window = function Window() { }; + (global as any).window = undefined; // not an instance of Window + (global as any).Document = function Document() { }; + (global as any).document = new (global as any).Document(); + + try { + await load(createMockedConnectionString(fakeEndpoint), { clientOptions }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext).not.undefined; + expect(correlationContext.includes("Host=Web")).eq(false); + }); + + it("is not Web when window is not instance of Window", async () => { + (global as any).Window = function Window() { }; + (global as any).window = {}; // not an instance of Window + (global as any).Document = function Document() { }; + (global as any).document = new (global as any).Document(); + + try { + await load(createMockedConnectionString(fakeEndpoint), { clientOptions }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext).not.undefined; + expect(correlationContext.includes("Host=Web")).eq(false); + }); + }); }); \ No newline at end of file