Skip to content

Commit

Permalink
Correctly reject in-progress body methods with AbortError
Browse files Browse the repository at this point in the history
Prior to this change, response body methods such as
response.body.arrayBuffer() would reject with a TypeError if the fetch
was aborted. This change makes them correctly reject with an AbortError
instead.

This implements stage #3 of the "Body Cancellation" section of the
design doc:

https://docs.google.com/document/d/1OuoCG2uiijbAwbCw9jaS7tHEO0LBO_4gMNio1ox0qlY/edit#heading=h.fvc7d7q07oio

Bug: 817687
Change-Id: Ifde322d9c22485a8ba9d14fd4ffca65c4fb4745a
  • Loading branch information
ricea authored and chromium-wpt-export-bot committed Mar 19, 2018
1 parent 9b3bb05 commit 2b48a08
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
74 changes: 74 additions & 0 deletions service-workers/cache-storage/script-tests/cache-abort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
if (self.importScripts) {
importScripts('/resources/testharness.js');
importScripts('../resources/test-helpers.js');
importScripts('/common/utils.js');
}

// We perform the same tests on put, add, addAll. Parameterise the tests to
// reduce repetition.
const methodsToTest = {
put: async (cache, request) => {
const response = await fetch(request);
return cache.put(request, response);
},
add: async (cache, request) => cache.add(request),
addAll: async (cache, request) => cache.addAll([request]),
};

for (const method in methodsToTest) {
const perform = methodsToTest[method];

cache_test(async (cache, test) => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
const request = new Request('../resources/simple.txt', { signal });
await promise_rejects(test, 'AbortError', perform(cache, request),
`${method} should reject`);
}, `${method}() on an already-aborted request should reject with AbortError`);

cache_test(async (cache, test) => {
const controller = new AbortController();
const signal = controller.signal;
const request = new Request('../resources/simple.txt', { signal });
const promise = perform(cache, request);
controller.abort();
await promise_rejects(test, 'AbortError', promise,
`${method} should reject`);
}, `${method}() synchronously followed by abort should reject with ` +
`AbortError`);

cache_test(async (cache, test) => {
const controller = new AbortController();
const signal = controller.signal;
const stateKey = token();
const abortKey = token();
const request = new Request(
`../../../fetch/api/resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`,
{ signal });

const promise = perform(cache, request);

// Wait for response to start.
let opened = false;
do {
// Normally only one fetch is needed, but they will be served in reverse
// order sometimes.
const response =
await fetch(`../../../fetch/api/resources/stash-take.py?key=${stateKey}`);
const body = await response.json();
if (body === 'open') opened = true;
} while (!opened);

controller.abort();

await promise_rejects(test, 'AbortError', promise,
`${method} should reject`);

// infinite-slow-response.py doesn't know when to stop.
await fetch(`../../../fetch/api/resources/stash-put.py?key=${abortKey}`);
}, `${method}() followed by abort after headers received should reject ` +
`with AbortError`);
}

done();
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<title>Cache Storage: Abort</title>
<link rel="help" href="https://fetch.spec.whatwg.org/#request-signal">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../service-worker/resources/test-helpers.sub.js"></script>
<script>
service_worker_test('../script-tests/cache-abort.js');
</script>
8 changes: 8 additions & 0 deletions service-workers/cache-storage/window/cache-abort.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<title>Cache Storage: Abort</title>
<link rel="help" href="https://fetch.spec.whatwg.org/#request-signal">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/test-helpers.js"></script>
<script src="/common/utils.js"></script>
<script src="../script-tests/cache-abort.js"></script>
8 changes: 8 additions & 0 deletions service-workers/cache-storage/worker/cache-abort.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<title>>Cache Storage: Abort</title>
<link rel="help" href="https://fetch.spec.whatwg.org/#request-signal">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
fetch_tests_from_worker(new Worker('../script-tests/cache-abort.js'));
</script>

0 comments on commit 2b48a08

Please sign in to comment.