|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import "jsdom-global/register"; |
| 6 | +import assert from "assert"; |
| 7 | +import sinon from "sinon"; |
| 8 | +import nock from "nock"; |
| 9 | +import fetch from "node-fetch"; |
| 10 | + |
| 11 | +import BrowserSendBeaconFallbackUploader from "../../../../src/platform/browser/sendbeacon_fallback_uploader"; |
| 12 | +import { UploadResult, UploadResultStatus } from "../../../../src/core/upload/uploader"; |
| 13 | +import PingRequest from "../../../../src/core/upload/ping_request"; |
| 14 | + |
| 15 | +const sandbox = sinon.createSandbox(); |
| 16 | + |
| 17 | +const MOCK_ENDPOINT = "http://www.example.com"; |
| 18 | + |
| 19 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 20 | +// @ts-ignore |
| 21 | +// eslint-disable-next-line jsdoc/require-jsdoc |
| 22 | +function setGlobalSendBeacon() { |
| 23 | + global.navigator.sendBeacon = (url: string, content: string): boolean => { |
| 24 | + void fetch(url, { |
| 25 | + body: content, |
| 26 | + method: "POST", |
| 27 | + }); |
| 28 | + |
| 29 | + return true; |
| 30 | + }; |
| 31 | +} |
| 32 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 33 | +// @ts-ignore |
| 34 | +global.fetch = fetch; |
| 35 | + |
| 36 | +describe("Uploader/BrowserSendBeaconFallback", function () { |
| 37 | + beforeEach(function() { |
| 38 | + setGlobalSendBeacon(); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(function () { |
| 42 | + sandbox.restore(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("returns the correct status for successful requests", async function () { |
| 46 | + const TEST_PING_CONTENT = {"my-test-value": 40721}; |
| 47 | + for (const status of [200, 400, 500]) { |
| 48 | + nock(MOCK_ENDPOINT).post(/./i, body => { |
| 49 | + return JSON.stringify(body) == JSON.stringify(TEST_PING_CONTENT); |
| 50 | + }).reply(status); |
| 51 | + |
| 52 | + const response = BrowserSendBeaconFallbackUploader.post(MOCK_ENDPOINT, new PingRequest("abc", {}, JSON.stringify(TEST_PING_CONTENT), 1024)); |
| 53 | + // When using sendBeacon, we can't really tell if something was correctly uploaded |
| 54 | + // or not. All we can know is if the request was enqueued, so we always expect 200. |
| 55 | + const expectedResponse = new UploadResult(UploadResultStatus.Success, 200); |
| 56 | + assert.deepStrictEqual( |
| 57 | + await response, |
| 58 | + expectedResponse |
| 59 | + ); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + it("returns the correct status after fallback", async function () { |
| 64 | + const TEST_PING_CONTENT = {"my-test-value": 40721}; |
| 65 | + nock(MOCK_ENDPOINT).post(/./i).reply(200); |
| 66 | + |
| 67 | + // Reset `fetch` to a known state. |
| 68 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 69 | + // @ts-ignore |
| 70 | + global.fetch = fetch; |
| 71 | + |
| 72 | + // Ensure `sendBeacon` fails. |
| 73 | + global.navigator.sendBeacon = () => false; |
| 74 | + |
| 75 | + const response = BrowserSendBeaconFallbackUploader.post(MOCK_ENDPOINT, new PingRequest("abc", {}, JSON.stringify(TEST_PING_CONTENT), 1024)); |
| 76 | + const expectedResponse = new UploadResult(UploadResultStatus.Success, 200); |
| 77 | + assert.deepStrictEqual( |
| 78 | + await response, |
| 79 | + expectedResponse |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it("returns the correct status when both uploads fail", async function () { |
| 84 | + nock(MOCK_ENDPOINT).post(/./i).replyWithError({ |
| 85 | + message: "something awful happened", |
| 86 | + code: "AWFUL_ERROR", |
| 87 | + }); |
| 88 | + |
| 89 | + // Reset `fetch` to a known state. |
| 90 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 91 | + // @ts-ignore |
| 92 | + global.fetch = fetch; |
| 93 | + |
| 94 | + // Ensure `sendBeacon` fails. |
| 95 | + global.navigator.sendBeacon = () => false; |
| 96 | + |
| 97 | + const response = BrowserSendBeaconFallbackUploader.post(MOCK_ENDPOINT, new PingRequest("abc", {}, "{}", 1024)); |
| 98 | + const expectedResponse = new UploadResult(UploadResultStatus.RecoverableFailure); |
| 99 | + assert.deepStrictEqual( |
| 100 | + await response, |
| 101 | + expectedResponse |
| 102 | + ); |
| 103 | + }); |
| 104 | +}); |
0 commit comments