diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/background-fetch/fetch-uploads.https.window.js | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/background-fetch/fetch-uploads.https.window.js')
-rw-r--r-- | testing/web-platform/tests/background-fetch/fetch-uploads.https.window.js | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/testing/web-platform/tests/background-fetch/fetch-uploads.https.window.js b/testing/web-platform/tests/background-fetch/fetch-uploads.https.window.js new file mode 100644 index 0000000000..f93f88a6ba --- /dev/null +++ b/testing/web-platform/tests/background-fetch/fetch-uploads.https.window.js @@ -0,0 +1,64 @@ +// META: script=/service-workers/service-worker/resources/test-helpers.sub.js +// META: script=resources/utils.js +'use strict'; + +// Covers basic functionality provided by BackgroundFetchManager.fetch(). +// Specifically, when `fetch` contains request uploads. +// https://wicg.github.io/background-fetch/#background-fetch-manager-fetch + +backgroundFetchTest(async (test, backgroundFetch) => { + const uploadData = 'Background Fetch!'; + const request = + new Request('resources/upload.py', {method: 'POST', body: uploadData}); + + const registration = await backgroundFetch.fetch(uniqueId(), request); + assert_equals(registration.uploadTotal, uploadData.length); + + const {type, eventRegistration, results} = await getMessageFromServiceWorker(); + assert_equals(type, 'backgroundfetchsuccess'); + assert_equals(results.length, 1); + assert_equals(eventRegistration.result, 'success'); + assert_equals(eventRegistration.failureReason, ''); + assert_equals(eventRegistration.uploaded, uploadData.length); + assert_equals(results[0].text, uploadData); +}, 'Fetch with an upload should work'); + +backgroundFetchTest(async (test, backgroundFetch) => { + const uploadData = 'Background Fetch!'; + const uploadRequest = + new Request('resources/upload.py', {method: 'POST', body: uploadData}); + + const registration = await backgroundFetch.fetch( + uniqueId(), + [uploadRequest, '/common/slow.py']); + + const uploaded = await new Promise(resolve => { + registration.onprogress = event => { + if (event.target.downloaded === 0) + return; + // If a progress event with downloaded bytes was received, then + // everything was uploaded. + resolve(event.target.uploaded); + }; + }); + + assert_equals(uploaded, uploadData.length); +}, 'Progress event includes uploaded bytes'); + +backgroundFetchTest(async (test, backgroundFetch) => { + const uploadRequest1 = + new Request('resources/upload.py', {method: 'POST', body: 'upload1'}); + const uploadRequest2 = + new Request('resources/upload.py', {method: 'POST', body: 'upload2'}); + + await backgroundFetch.fetch(uniqueId(), [uploadRequest1, uploadRequest2]); + + const {type, eventRegistration, results} = await getMessageFromServiceWorker(); + assert_equals(type, 'backgroundfetchsuccess'); + assert_equals(results.length, 2); + assert_equals(eventRegistration.result, 'success'); + assert_equals(eventRegistration.failureReason, ''); + + assert_array_equals([results[0].text, results[1].text].sort(), + ['upload1', 'upload2']); +}, 'Duplicate upload requests work and can be distinguished.'); |