Skip to content
File

Blob: src/workerd/api/tests/navigator-beacon-test.js

javascript35 lines
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
4import { ok, strictEqual } from 'node:assert';
5 
6const enc = new TextEncoder();
7 
8// This one returns false with no error thrown because we're not in an IoContext
9ok(!navigator.sendBeacon('http://example.org', 'does not work'));
10 
11export 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 
29export default {
30 async fetch(req, env) {
31 strictEqual(await req.text(), 'beacon');
32 return new Response(null, { status: 204 });
33 },
34};