Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[main] allow empty string as first parameter to fetch #2152

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,124 @@ export class AjaxTests extends AITestClass {
}]
});

this.testCaseAsync({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cloned the two test cases above this that were testing for null/undefined, but I didn't completely understand what all of the setup/checks were that were necessary.

name: "Fetch: instrumentation handles empty string",
stepDelay: 10,
autoComplete: false,
timeOut: 10000,
steps: [ (testContext) => {
let fetchCalls = hookFetch((resolve) => {
AITestClass.orgSetTimeout(function() {
resolve({
headers: new Headers(),
ok: true,
body: null,
bodyUsed: false,
redirected: false,
status: 200,
statusText: "Hello",
trailer: null,
type: "basic",
url: "https://httpbin.org/status/200"
});
}, 0);
});

this._ajax = new AjaxMonitor();
let dependencyFields = hookTrackDependencyInternal(this._ajax);
let appInsightsCore = new AppInsightsCore();
let coreConfig = { instrumentationKey: "", disableFetchTracking: false };
appInsightsCore.initialize(coreConfig, [this._ajax, new TestChannelPlugin()]);
let fetchSpy = this.sandbox.spy(appInsightsCore, "track")
let throwSpy = this.sandbox.spy(appInsightsCore.logger, "throwInternal");

// Act
Assert.ok(fetchSpy.notCalled, "No fetch called yet");
fetch("", {method: "post", [DisabledPropertyName]: false}).then(() => {
// Assert
Assert.ok(fetchSpy.calledOnce, "createFetchRecord called once after using fetch");
let data = fetchSpy.args[0][0].baseData;
Assert.equal("Fetch", data.type, "request is Fetch type");
Assert.equal(false, throwSpy.called, "We should not have failed internally");
Assert.equal(1, dependencyFields.length, "trackDependencyDataInternal was called");
Assert.ok(dependencyFields[0].dependency.startTime, "startTime was specified before trackDependencyDataInternal was called");
Assert.equal(undefined, dependencyFields[0].sysProperties, "no system properties");

// Assert that the HTTP method was preserved
jorupp marked this conversation as resolved.
Show resolved Hide resolved
Assert.equal(1, fetchCalls.length);
Assert.notEqual(undefined, fetchCalls[0].init, "Has init param");
Assert.equal("post", fetchCalls[0].init?.method, "Has post method");

testContext.testDone();
}, () => {
Assert.ok(false, "fetch failed!");
testContext.testDone();
});
}]
});

this.testCaseAsync({
name: "Fetch: instrumentation handles empty string with traceId",
stepDelay: 10,
autoComplete: false,
timeOut: 10000,
steps: [ (testContext) => {
let fetchCalls = hookFetch((resolve) => {
AITestClass.orgSetTimeout(function() {
resolve({
headers: new Headers(),
ok: true,
body: null,
bodyUsed: false,
redirected: false,
status: 200,
statusText: "Hello",
trailer: null,
type: "basic",
url: "https://httpbin.org/status/200"
});
}, 0);
});

this._ajax = new AjaxMonitor();
let dependencyFields = hookTrackDependencyInternal(this._ajax);
let appInsightsCore = new AppInsightsCore();
let coreConfig = { instrumentationKey: "", disableFetchTracking: false };
appInsightsCore.initialize(coreConfig, [this._ajax, new TestChannelPlugin()]);
let fetchSpy = this.sandbox.spy(appInsightsCore, "track")
let throwSpy = this.sandbox.spy(appInsightsCore.logger, "throwInternal");
let traceCtx = appInsightsCore.getTraceCtx();
let expectedTraceId = generateW3CId();
let expectedSpanId = generateW3CId().substring(0, 16);
traceCtx!.setTraceId(expectedTraceId);
traceCtx!.setSpanId(expectedSpanId);

// Act
Assert.ok(fetchSpy.notCalled, "No fetch called yet");
fetch("", {method: "post", [DisabledPropertyName]: false}).then(() => {
// Assert
Assert.ok(fetchSpy.calledOnce, "createFetchRecord called once after using fetch");
let data = fetchSpy.args[0][0].baseData;
Assert.equal("Fetch", data.type, "request is Fetch type");
Assert.equal(false, throwSpy.called, "We should not have failed internally");
Assert.equal(1, dependencyFields.length, "trackDependencyDataInternal was called");
Assert.ok(dependencyFields[0].dependency.startTime, "startTime was specified before trackDependencyDataInternal was called");
Assert.equal(expectedTraceId, dependencyFields[0].sysProperties!.trace.traceID, "system properties traceId");
Assert.equal(expectedSpanId, dependencyFields[0].sysProperties!.trace.parentID, "system properties spanId");

// Assert that the HTTP method was preserved
jorupp marked this conversation as resolved.
Show resolved Hide resolved
Assert.equal(1, fetchCalls.length);
Assert.notEqual(undefined, fetchCalls[0].init, "Has init param");
Assert.equal("post", fetchCalls[0].init?.method, "Has post method");

testContext.testDone();
}, () => {
Assert.ok(false, "fetch failed!");
testContext.testDone();
});
}]
});


this.testCase({
name: "Fetch: fetch keeps custom headers",
Expand Down
2 changes: 1 addition & 1 deletion extensions/applicationinsights-dependencies-js/src/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu

_processDependencyListeners(_dependencyListeners, _self.core, ajaxData, xhr, input, init);

if (input) { // Fetch
if (input || input === "") { // Fetch
if (correlationIdCanIncludeCorrelationHeader(_extensionConfig, ajaxData.getAbsoluteUrl(), currentWindowHost)) {
if (!init) {
init = {};
Expand Down
Loading