File
Blob: src/workerd/api/tests/navigator-beacon-test.js
| 1 | // Copyright (c) 2023 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | import { ok, strictEqual } from 'node:assert'; |
| 5 | |
| 6 | const enc = new TextEncoder(); |
| 7 | |
| 8 | // This one returns false with no error thrown because we're not in an IoContext |
| 9 | ok(!navigator.sendBeacon('http://example.org', 'does not work')); |
| 10 | |
| 11 | export const navigatorBeaconTest = { |
| 12 | async test(ctrl, env, ctx) { |
| 13 | ok(navigator.sendBeacon('http://example.org', 'beacon')); |
| 14 | ok( |
| 15 | navigator.sendBeacon( |
| 16 | 'http://example.org', |
| 17 | new ReadableStream({ |
| 18 | start(c) { |
| 19 | c.enqueue(enc.encode('beacon')); |
| 20 | c.close(); |
| 21 | }, |
| 22 | }) |
| 23 | ) |
| 24 | ); |
| 25 | ok(navigator.sendBeacon('http://example.org', enc.encode('beacon'))); |
| 26 | }, |
| 27 | }; |
| 28 | |
| 29 | export default { |
| 30 | async fetch(req, env) { |
| 31 | strictEqual(await req.text(), 'beacon'); |
| 32 | return new Response(null, { status: 204 }); |
| 33 | }, |
| 34 | }; |