Skip to content

Commit

Permalink
finish test for transports
Browse files Browse the repository at this point in the history
  • Loading branch information
siyuniu-ms committed Nov 16, 2023
1 parent 8ff933a commit af86a1d
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1151,8 +1151,202 @@ export class SenderTests extends AITestClass {
}
});

this.testCase({
name: 'Transport Type: isBeaconApiDisabled is true and User provide beacon in transports, we should still block beacon',
test: () => {
let window = getGlobalInst("window");
let fakeXMLHttpRequest = (window as any).XMLHttpRequest;
let fetchstub = this.sandbox.stub((window as any), "fetch");

let sendBeaconCalled = false;
this.hookSendBeacon((url: string) => {
sendBeaconCalled = true;
return false;
});

let config = {
endpointUrl: "https//: test",
emitLineDelimitedJson: false,
maxBatchInterval: 15000,
maxBatchSizeInBytes: 102400,
disableTelemetry: false,
enableSessionStorageBuffer: true,
isRetryDisabled: false,
isBeaconApiDisabled: true,
disableXhr: false,
onunloadDisableFetch: false,
onunloadDisableBeacon: false,
instrumentationKey:"key",
namePrefix: "",
samplingPercentage: 100,
customHeaders: [{header:"header",value:"val" }],
convertUndefined: "",
eventsLimitInMem: 10000,
transports: [TransportType.Beacon]
} as ISenderConfig;

const sender = new Sender();
const cr = new AppInsightsCore();
var coreConfig = {
instrumentationKey: "",
extensionConfig: {[sender.identifier]: config}
};

cr.initialize(coreConfig, [sender]);

this.onDone(() => {
sender.teardown();
});

const telemetryItem: ITelemetryItem = {
name: 'fake item',
iKey: 'iKey',
baseType: 'some type',
baseData: {}
};

QUnit.assert.ok(isBeaconApiSupported(), "Beacon API is supported");
QUnit.assert.equal(false, sendBeaconCalled, "Beacon API was not called before");
QUnit.assert.equal(0, this._getXhrRequests().length, "xhr sender was not called before");

try {
sender.processTelemetry(telemetryItem, null);
sender.flush();
} catch(e) {
QUnit.assert.ok(false);
}

QUnit.assert.equal(false, sendBeaconCalled, "Beacon API is blocked, Beacon API should not be called");
QUnit.assert.equal(1, this._getXhrRequests().length, "xhr sender is called");
QUnit.assert.ok(!fetchstub.called, "fetch sender is not called");
// store it back
(window as any).XMLHttpRequest = fakeXMLHttpRequest;
}
});
this.testCase({
name: 'Transport Type: isBeaconApiDisabled is false and User provide beacon in transports, we should pick beacon',
useFakeTimers: true,
test: () => {
let sendBeaconCalled = false;
this.hookSendBeacon((url: string) => {
sendBeaconCalled = true;
return true;
});
let config = {
isBeaconApiDisabled: false,
disableXhr: false,
transports: [TransportType.Beacon]
} as ISenderConfig;

const sender = new Sender();
const cr = new AppInsightsCore();
var coreConfig = {
instrumentationKey: "",
extensionConfig: {[sender.identifier]: config}
};

cr.initialize(coreConfig, [sender]);

this.onDone(() => {
sender.teardown();
});
const telemetryItem: ITelemetryItem = {
name: 'fake item',
iKey: 'iKey',
baseType: 'some type',
baseData: {}
};

QUnit.assert.ok(isBeaconApiSupported(), "Beacon API is supported");
QUnit.assert.equal(false, sendBeaconCalled, "Beacon API was not called before");
QUnit.assert.equal(0, this._getXhrRequests().length, "xhr sender was not called before");

try {
sender.processTelemetry(telemetryItem, null);
sender.flush();
} catch(e) {
QUnit.assert.ok(false);
}

this.clock.tick(15000);

QUnit.assert.equal(0, this._getXhrRequests().length, "xhr sender is not called when Beacon API is enabled");
QUnit.assert.equal(true, sendBeaconCalled, "Beacon API is enabled, Beacon API is called");
}
});

// this.testCase({
// name: 'Transport Type: isBeaconApiDisabled is false and User provide beacon in transports, we should pick beacon',
// test: () => {
// let window = getGlobalInst("window");
// let fakeXMLHttpRequest = (window as any).XMLHttpRequest;
// let fetchstub = this.sandbox.stub((window as any), "fetch");

// let sendBeaconCalled = false;
// this.hookSendBeacon((url: string) => {
// sendBeaconCalled = true;
// return false;
// });

// let config = {
// endpointUrl: "https//: test",
// emitLineDelimitedJson: false,
// maxBatchInterval: 15000,
// maxBatchSizeInBytes: 102400,
// disableTelemetry: false,
// enableSessionStorageBuffer: true,
// isRetryDisabled: false,
// isBeaconApiDisabled: false,
// disableXhr: false,
// onunloadDisableFetch: false,
// onunloadDisableBeacon: false,
// instrumentationKey:"key",
// namePrefix: "",
// samplingPercentage: 100,
// customHeaders: [{header:"header",value:"val" }],
// convertUndefined: "",
// eventsLimitInMem: 10000,
// transports: [TransportType.Beacon]
// } as ISenderConfig;

// const sender = new Sender();
// const cr = new AppInsightsCore();
// var coreConfig = {
// instrumentationKey: "",
// extensionConfig: {[sender.identifier]: config}
// };

// cr.initialize(coreConfig, [sender]);

// this.onDone(() => {
// sender.teardown();
// });

// const telemetryItem: ITelemetryItem = {
// name: 'fake item',
// iKey: 'iKey',
// baseType: 'some type',
// baseData: {}
// };

// QUnit.assert.ok(isBeaconApiSupported(), "Beacon API is supported");
// QUnit.assert.equal(false, sendBeaconCalled, "Beacon API was not called before");
// QUnit.assert.equal(0, this._getXhrRequests().length, "xhr sender was not called before");

// try {
// sender.processTelemetry(telemetryItem, null);
// sender.flush();
// } catch(e) {
// QUnit.assert.ok(false);
// }

// QUnit.assert.equal(true, sendBeaconCalled, "Beacon API is enabled, Beacon API is called");
// QUnit.assert.equal(0, this._getXhrRequests().length, "xhr sender is not called");
// QUnit.assert.ok(!fetchstub.called, "fetch sender is not called");
// // store it back
// (window as any).XMLHttpRequest = fakeXMLHttpRequest;
// }
// });


this.testCase({
Expand Down Expand Up @@ -1221,7 +1415,7 @@ export class SenderTests extends AITestClass {
}

QUnit.assert.equal(false, sendBeaconCalled, "Beacon API is disabled, Beacon API is not called");
QUnit.assert.equal(1, this._getXhrRequests().length, "xhr sender is not called");
QUnit.assert.equal(1, this._getXhrRequests().length, "xhr sender is called");
QUnit.assert.ok(!fetchstub.called, "fetch sender is not called");
// store it back
(window as any).XMLHttpRequest = fakeXMLHttpRequest;
Expand Down
9 changes: 8 additions & 1 deletion channels/applicationinsights-channel-js/src/Sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControls {

// Prefix any user requested transport(s) values
let theTransports: TransportType[] = _prependTransports([TransportType.Xhr, TransportType.Fetch], senderConfig.transports);
if (senderConfig.isBeaconApiDisabled){
// remove beacon from theTransports
theTransports = theTransports.filter(transport => transport !== TransportType.Beacon);
}

httpInterface = _getSenderInterface(theTransports, false);

let xhrInterface = { sendPOST: _xhrSender} as IXHROverride;
Expand All @@ -377,6 +382,8 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControls {
}

httpInterface = _alwaysUseCustomSend? customInterface : (httpInterface || customInterface || xhrInterface);



_self._sender = (payload: string[], isAsync: boolean) => {
return _doSend(httpInterface, payload, isAsync);
Expand All @@ -387,7 +394,7 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControls {
_syncUnloadSender = _fetchKeepAliveSender;
}

let syncTransports: TransportType[] = _prependTransports([TransportType.Beacon, TransportType.Xhr], senderConfig.transports);
let syncTransports: TransportType[] = _prependTransports([TransportType.Beacon, TransportType.Xhr], senderConfig.unloadTransports);
syncInterface = _getSenderInterface(syncTransports, true);
syncInterface = _alwaysUseCustomSend? customInterface : (syncInterface || customInterface);

Expand Down

0 comments on commit af86a1d

Please sign in to comment.