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

399 Fixed bug 'AuthHeaders ignored for sendOnClose' #453

Merged
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
19 changes: 17 additions & 2 deletions build/UserALEWebExtension/background.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/UserALEWebExtension/background.js.map

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions build/UserALEWebExtension/content.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/UserALEWebExtension/content.js.map

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions build/UserALEWebExtension/options.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/UserALEWebExtension/options.js.map

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions build/userale-2.4.0.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/userale-2.4.0.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/userale-2.4.0.min.js

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions src/sendLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,27 @@ export function sendOnClose(
config: Configuration,
): void {
window.addEventListener("pagehide", function () {
if (config.on && logs.length > 0) {
navigator.sendBeacon(config.url, JSON.stringify(logs));
if (!config.on) {
return;
}

if (logs.length > 0) {
const headers: HeadersInit = new Headers();
headers.set("Content-Type", "applicaiton/json;charset=UTF-8");

if (config.authHeader) {
headers.set("Authorization", config.authHeader.toString());
}

fetch(config.url, {
keepalive: true,
method: "POST",
headers: headers,
body: JSON.stringify(logs),
}).catch((error) => {
console.error(error);
});

logs.splice(0); // clear log queue
}
});
Expand Down
30 changes: 17 additions & 13 deletions test/sendLogs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,38 @@ describe("sendLogs", () => {
done();
});

it("sends logs on page exit with navigator", () => {
const sendBeaconSpy = jest.fn();
Object.defineProperty(global, "navigator", {
value: { sendBeacon: sendBeaconSpy },
writable: true,
});
it("sends logs on page exit with fetch", () => {
const fetchSpy = jest.fn().mockImplementation(() =>
Promise.resolve({
ok: true,
foo: "bar",
}),
);
global.fetch = fetchSpy;

config.update({ on: true, url: "test" });
sendOnClose([], config);
config.update({ on: true, url: "test" });
sendOnClose([{ foo: "bar" }], config);
global.window.dispatchEvent(new window.CustomEvent("pagehide"));

expect(sendBeaconSpy).toHaveBeenCalledTimes(1);
expect(fetchSpy).toHaveBeenCalledTimes(1);
});

it("does not send logs on page exit when config is off", () => {
const sendBeaconSpy = jest.fn();
Object.defineProperty(global, "navigator", {
value: { sendBeacon: sendBeaconSpy },
writable: true,
});
const fetchSpy = jest.fn().mockImplementation(() =>
Promise.resolve({
ok: true,
foo: "bar",
}),
);
global.fetch = fetchSpy;

config.update({ on: false, url: "test" });
sendOnClose([{ foo: "bar" }], config);
global.window.dispatchEvent(new window.CustomEvent("pagehide"));

expect(sendBeaconSpy).not.toHaveBeenCalled();
expect(fetchSpy).not.toHaveBeenCalled();
});

it("sends logs with proper auth header when using registerAuthCallback", (done) => {
Expand Down
Loading