diff --git a/extensions/applicationinsights-dependencies-js/Tests/Unit/src/ajax.tests.ts b/extensions/applicationinsights-dependencies-js/Tests/Unit/src/ajax.tests.ts index ea6fbacd4..78a5bb969 100644 --- a/extensions/applicationinsights-dependencies-js/Tests/Unit/src/ajax.tests.ts +++ b/extensions/applicationinsights-dependencies-js/Tests/Unit/src/ajax.tests.ts @@ -1909,6 +1909,51 @@ export class AjaxTests extends AITestClass { } }); + this.testCase({ + name: "Fetch: fetch keeps custom headers when correlation headers are discarded by dependencyListeners", + test: () => { + hookFetch((resolve) => { + AITestClass.orgSetTimeout(function() { + resolve(); + }, 0); + }); + + + this._ajax = new AjaxMonitor(); + this._ajax.addDependencyListener((details: IDependencyListenerDetails) => { + return false; + }); + let appInsightsCore = new AppInsightsCore(); + let coreConfig = { + instrumentationKey: "", + disableFetchTracking: false, + disableAjaxTracking: true + }; + appInsightsCore.initialize(coreConfig, [this._ajax, new TestChannelPlugin()]); + let fetchSpy = this.sandbox.spy(window, "fetch"); + + // Setup + let headers = new Headers(); + headers.append('My-Header', 'Header field'); + let init = { + method: 'get', + headers: headers + }; + const url = 'https://httpbin.org/status/200'; + + let headerSpy = this.sandbox.spy(this._ajax, "includeCorrelationHeaders"); + + // Act + Assert.ok(fetchSpy.notCalled); + fetch(url, init); + + // Assert + Assert.ok(fetchSpy.calledOnce); + Assert.ok(headerSpy.calledOnce); + Assert.deepEqual(init, headerSpy.returnValue || headerSpy.returnValues[0]); + } + }); + this.testCaseAsync({ name: "Fetch: should create and pass a traceparent header if ai and w3c is enabled with custom headers", stepDelay: 10, diff --git a/extensions/applicationinsights-dependencies-js/src/ajax.ts b/extensions/applicationinsights-dependencies-js/src/ajax.ts index 1fa9628e2..0f0e2b896 100644 --- a/extensions/applicationinsights-dependencies-js/src/ajax.ts +++ b/extensions/applicationinsights-dependencies-js/src/ajax.ts @@ -466,8 +466,6 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IAjaxMonitorPlug init.headers = headers; } - - return init; } else if (xhr) { // XHR if (correlationIdCanIncludeCorrelationHeader(_extensionConfig, ajaxData.getAbsoluteUrl(), currentWindowHost)) { if (_isUsingAIHeaders) { @@ -512,12 +510,10 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IAjaxMonitorPlug } } } - - return xhr; } } - return undefined; + return xhr || init; } _self.trackDependencyDataInternal = (dependency: IDependencyTelemetry, properties?: { [key: string]: any }, systemProperties?: { [key: string]: any }) => {