Skip to content

Commit

Permalink
Fetch/XHR/Beacon: test locked/disturbed ReadableStream
Browse files Browse the repository at this point in the history
See whatwg/fetch#801 for context.
  • Loading branch information
annevk authored Aug 24, 2018
1 parent 9646393 commit aecdb30
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
3 changes: 3 additions & 0 deletions beacon/beacon-readablestream.window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test(() => {
assert_throws(new TypeError(), () => navigator.sendBeacon("...", new ReadableStream()));
}, "sendBeacon() with a stream does not work due to the keepalive flag being set");
51 changes: 51 additions & 0 deletions fetch/api/request/request-init-stream.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// META: global=window,worker

"use strict";

async function assert_request(input, init) {
assert_throws(new TypeError(), () => new Request(input, init), "new Request()");
assert_throws(new TypeError(), async () => await fetch(input, init), "fetch()");
}

promise_test(async () => {
const stream = new ReadableStream();
stream.getReader();
await assert_request("...", { method:"POST", body: stream });
}, "Constructing a Request with a stream on which getReader() is called");

promise_test(async () => {
const stream = new ReadableStream();
stream.getReader().read();
await assert_request("...", { method:"POST", body: stream });
}, "Constructing a Request with a stream on which read() is called");

promise_test(async () => {
const stream = new ReadableStream({ pull: c => c.enqueue(new Uint8Array()) }),
reader = stream.getReader();
await reader.read();
reader.releaseLock();
await assert_request("...", { method:"POST", body: stream });
}, "Constructing a Request with a stream on which read() and releaseLock() are called");

promise_test(async () => {
const request = new Request("...", { method: "POST", body: "..." });
request.body.getReader();
await assert_request(request);
assert_class_string(new Request(request, { body: "..." }), "Request");
}, "Constructing a Request with a Request on which body.getReader() is called");

promise_test(async () => {
const request = new Request("...", { method: "POST", body: "..." });
request.body.getReader().read();
await assert_request(request);
assert_class_string(new Request(request, { body: "..." }), "Request");
}, "Constructing a Request with a Request on which body.getReader().read() is called");

promise_test(async () => {
const request = new Request("...", { method: "POST", body: "..." }),
reader = request.body.getReader();
await reader.read();
reader.releaseLock();
await assert_request(request);
assert_class_string(new Request(request, { body: "..." }), "Request");
}, "Constructing a Request with a Request on which read() and releaseLock() are called");
23 changes: 23 additions & 0 deletions fetch/api/response/response-from-stream.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// META: global=window,worker

"use strict";

test(() => {
const stream = new ReadableStream();
stream.getReader();
assert_throws(new TypeError(), () => new Response(stream));
}, "Constructing a Response with a stream on which getReader() is called");

test(() => {
const stream = new ReadableStream();
stream.getReader().read();
assert_throws(new TypeError(), () => new Response(stream));
}, "Constructing a Response with a stream on which read() is called");

promise_test(async () => {
const stream = new ReadableStream({ pull: c => c.enqueue(new Uint8Array()) }),
reader = stream.getReader();
await reader.read();
reader.releaseLock();
assert_throws(new TypeError(), () => new Response(stream));
}, "Constructing a Response with a stream on which read() and releaseLock() are called");
27 changes: 27 additions & 0 deletions xhr/send-data-readablestream.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// META: global=window,dedicatedworker,sharedworker

function assert_xhr(stream) {
const client = new XMLHttpRequest();
client.open("POST", "...");
assert_throws(new TypeError(), () => client.send(stream));
}

test(() => {
const stream = new ReadableStream();
stream.getReader();
assert_xhr(stream);
}, "XMLHttpRequest: send() with a stream on which getReader() is called");

test(() => {
const stream = new ReadableStream();
stream.getReader().read();
assert_xhr(stream);
}, "XMLHttpRequest: send() with a stream on which read() is called");

promise_test(async () => {
const stream = new ReadableStream({ pull: c => c.enqueue(new Uint8Array()) }),
reader = stream.getReader();
await reader.read();
reader.releaseLock();
assert_xhr(stream);
}, "XMLHttpRequest: send() with a stream on which read() and releaseLock() are called");

0 comments on commit aecdb30

Please sign in to comment.