beacon-basic.https.window.js (3584B)
1 // META: timeout=long 2 // META: script=/common/utils.js 3 // META: script=beacon-common.sub.js 4 5 'use strict'; 6 7 // TODO(yhirano): Check the sec-fetch-mode request header once WebKit supports 8 // the feature. 9 10 parallelPromiseTest(async (t) => { 11 const iframe = document.createElement('iframe'); 12 document.body.appendChild(iframe); 13 t.add_cleanup(() => iframe.remove()); 14 15 const id = token(); 16 const url = `/beacon/resources/beacon.py?cmd=store&id=${id}`; 17 assert_true(iframe.contentWindow.navigator.sendBeacon(url)); 18 iframe.remove(); 19 20 const result = await waitForResult(id); 21 assert_equals(result.type, '(missing)', 'content-type'); 22 }, `simple case: with no payload`); 23 24 parallelPromiseTest(async (t) => { 25 const iframe = document.createElement('iframe'); 26 document.body.appendChild(iframe); 27 t.add_cleanup(() => iframe.remove()); 28 29 const id = token(); 30 const url = `/beacon/resources/beacon.py?cmd=store&id=${id}`; 31 assert_true(iframe.contentWindow.navigator.sendBeacon(url, null)); 32 iframe.remove(); 33 34 const result = await waitForResult(id); 35 assert_equals(result.type, '(missing)', 'content-type'); 36 }, `simple case: with null payload`); 37 38 for (const size of [EMPTY, SMALL, LARGE, MAX]) { 39 for (const type of [STRING, ARRAYBUFFER, FORM, BLOB]) { 40 if (size === MAX && type === FORM) { 41 // It is difficult to estimate the size of a form accurately, so we cannot 42 // test this case. 43 continue; 44 } 45 parallelPromiseTest(async (t) => { 46 const iframe = document.createElement('iframe'); 47 document.body.appendChild(iframe); 48 t.add_cleanup(() => iframe.remove()); 49 50 const payload = makePayload(size, type); 51 const id = token(); 52 const url = `/beacon/resources/beacon.py?cmd=store&id=${id}`; 53 assert_true(iframe.contentWindow.navigator.sendBeacon(url, payload)); 54 iframe.remove(); 55 56 const result = await waitForResult(id); 57 if (getContentType(type) === null) { 58 assert_equals(result.type, '(missing)', 'content-type'); 59 } else { 60 assert_true(result.type.includes(getContentType(type)), 'content-type'); 61 } 62 }, `simple case: type = ${type} and size = ${size}`); 63 } 64 } 65 66 for (const type of [STRING, ARRAYBUFFER, FORM, BLOB]) { 67 parallelPromiseTest(async (t) => { 68 const iframe = document.createElement('iframe'); 69 document.body.appendChild(iframe); 70 t.add_cleanup(() => iframe.remove()); 71 72 const payload = makePayload(TOOLARGE, type); 73 const id = token(); 74 const url = `/beacon/resources/beacon.py?cmd=store&id=${id}`; 75 assert_false(iframe.contentWindow.navigator.sendBeacon(url, payload)); 76 }, `Too large payload should be rejected: type = ${type}`); 77 } 78 79 for (const type of [STRING, ARRAYBUFFER, BLOB]) { 80 parallelPromiseTest(async (t) => { 81 const iframe = document.createElement('iframe'); 82 document.body.appendChild(iframe); 83 t.add_cleanup(() => iframe.remove()); 84 85 assert_true(iframe.contentWindow.navigator.sendBeacon( 86 `/beacon/resources/beacon.py?cmd=store&id=${token()}`, 87 makePayload(MAX, type))); 88 assert_true(iframe.contentWindow.navigator.sendBeacon( 89 `/beacon/resources/beacon.py?cmd=store&id=${token()}`, '')); 90 assert_false(iframe.contentWindow.navigator.sendBeacon( 91 `/beacon/resources/beacon.py?cmd=store&id=${token()}`, 'x')); 92 }, `Payload size restriction should be accumulated: type = ${type}`); 93 } 94 95 test(() => { 96 assert_throws_js( 97 TypeError, () => navigator.sendBeacon('...', new ReadableStream())); 98 }, 'sendBeacon() with a stream does not work due to the keepalive flag being set');