Skip to content

Commit

Permalink
[Background Fetch] Throw DOMException for responseReady on abort.
Browse files Browse the repository at this point in the history
According to the spec, responseReady in BackgroundFetchRecord should
throw an AbortError DOMException if the fetch was abandoned.

https://wicg.github.io/background-fetch/#create-record-objects (2.4.3)

Change-Id: Ieadf278acd061e05b8822014d0934f050fcac702
Reviewed-on: https://chromium-review.googlesource.com/c/1283692
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Reviewed-by: Peter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#600769}
  • Loading branch information
rayankans authored and chromium-wpt-export-bot committed Oct 18, 2018
1 parent b38f09e commit f3ca7fc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
14 changes: 13 additions & 1 deletion background-fetch/abort.https.window.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ backgroundFetchTest(async (test, backgroundFetch) => {
};
});

}, 'Calling BackgroundFetchRegistration.abort sets the correct fields and responses are still available');
}, 'Calling BackgroundFetchRegistration.abort sets the correct fields and responses are still available');

backgroundFetchTest(async (test, backgroundFetch) => {
const registration = await backgroundFetch.fetch(
uniqueId(), '/serviceworker/resources/slow-response.php');
assert_true(await registration.abort());

const {results} = await getMessageFromServiceWorker();
assert_equals(results.length, 1);
assert_false(results[0].response);
assert_equals(results[0].name, 'AbortError');

}, 'An aborted fetch throws a DOM exception when accessing an incomplete record', 'sw-abort.js');
23 changes: 23 additions & 0 deletions background-fetch/service_workers/sw-abort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
importScripts('sw-helpers.js');

async function getFetchResult(record) {
try {
await record.responseReady;
} catch (e) {
return {
response: false,
name: e.name,
};
}

return {
response: true,
};
}
self.addEventListener('backgroundfetchabort', event => {
event.waitUntil(
event.registration.matchAll()
.then(records =>
Promise.all(records.map(record => getFetchResult(record))))
.then(results => sendMessageToDocument({results})));
});

0 comments on commit f3ca7fc

Please sign in to comment.