Skip to content

Commit

Permalink
Bug 1775067 [wpt PR 34496] - Add tests for RequestInit.duplex, a=test…
Browse files Browse the repository at this point in the history
…only

Automatic update from web-platform-tests
Fetch: tests for RequestInit's duplex

See whatwg/fetch#1457 for context.
--

wpt-commits: a9b73f530b73d237b0512968715104c96631c22a
wpt-pr: 34496
  • Loading branch information
yutakahirano authored and moz-wptsync-bot committed Jul 5, 2022
1 parent 3e667e1 commit bbdb1c4
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function testUpload(desc, url, method, createBody, expectedBody) {
const body = createBody();
if (body) {
requestInit["body"] = body;
requestInit.duplex = "half";
}
return fetch(url, requestInit).then(function(resp) {
return resp.text().then((text)=> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function testUpload(desc, url, method, createBody, expectedBody) {
const body = createBody();
if (body) {
requestInit["body"] = body;
requestInit.duplex = "half";
}
const resp = await fetch(url, requestInit);
const text = await resp.text();
Expand Down
150 changes: 120 additions & 30 deletions testing/web-platform/tests/fetch/api/request/request-init-stream.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,146 @@

"use strict";

const duplex = "half";
const method = "POST";

test(() => {
const stream = new ReadableStream();
const request = new Request("...", { method:"POST", body: stream });
assert_equals(request.body, stream);
const body = new ReadableStream();
const request = new Request("...", { method, body, duplex });
assert_equals(request.body, body);
}, "Constructing a Request with a stream holds the original object.");

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

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

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

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

promise_test(async (t) => {
test((t) => {
const request = new Request("...", { method: "POST", body: "..." });
request.body.getReader();
await assert_request(t, request);
assert_class_string(new Request(request, { body: "..." }), "Request");
assert_throws_js(TypeError, () => new Request(request));
// This doesn't throw.
new Request(request, { body: "..." });
}, "Constructing a Request with a Request on which body.getReader() is called");

promise_test(async (t) => {
test((t) => {
const request = new Request("...", { method: "POST", body: "..." });
request.body.getReader().read();
await assert_request(t, request);
assert_class_string(new Request(request, { body: "..." }), "Request");
assert_throws_js(TypeError, () => new Request(request));
// This doesn't throw.
new Request(request, { body: "..." });
}, "Constructing a Request with a Request on which body.getReader().read() is called");

promise_test(async (t) => {
const request = new Request("...", { method: "POST", body: "..." }),
reader = request.body.getReader();
promsie_test((t) => {
const request = new Request("...", { method: "POST", body: "..." });
const reader = request.body.getReader();
await reader.read();
reader.releaseLock();
await assert_request(t, request);
assert_class_string(new Request(request, { body: "..." }), "Request");
assert_throws_js(TypeError, () => new Request(request));
// This doesn't throw.
new Request(request, { body: "..." });
}, "Constructing a Request with a Request on which read() and releaseLock() are called");

test((t) => {
new Request("...", { method, body: null });
}, "It is OK to omit .duplex when the body is null.");

test((t) => {
new Request("...", { method, body: "..." });
}, "It is OK to omit .duplex when the body is a string.");

test((t) => {
new Request("...", { method, body: new Uint8Array(3) });
}, "It is OK to omit .duplex when the body is a Uint8Array.");

test((t) => {
new Request("...", { method, body: new Blob([]) });
}, "It is OK to omit .duplex when the body is a Blob.");

test((t) => {
const body = new ReadableStream();
assert_throws_js(TypeError,
() => new Request("...", { method, body }));
}, "It is error to omit .duplex when the body is a ReadableStream.");

test((t) => {
new Request("...", { method, body: null, duplex: "half" });
}, "It is OK to set .duplex = 'half' when the body is null.");

test((t) => {
new Request("...", { method, body: "...", duplex: "half" });
}, "It is OK to set .duplex = 'half' when the body is a string.");

test((t) => {
new Request("...", { method, body: new Uint8Array(3), duplex: "half" });
}, "It is OK to set .duplex = 'half' when the body is a Uint8Array.");

test((t) => {
new Request("...", { method, body: new Blob([]), duplex: "half" });
}, "It is OK to set .duplex = 'half' when the body is a Blob.");

test((t) => {
const body = new ReadableStream();
new Request("...", { method, body, duplex: "half" });
}, "It is OK to set .duplex = 'half' when the body is a ReadableStream.");

test((t) => {
const body = null;
const duplex = "full";
assert_throws_js(TypeError,
() => new Request("...", { method, body, duplex }));
}, "It is error to set .duplex = 'full' when the body is null.");

test((t) => {
const body = "...";
const duplex = "full";
assert_throws_js(TypeError,
() => new Request("...", { method, body, duplex }));
}, "It is error to set .duplex = 'full' when the body is a string.");

test((t) => {
const body = new Uint8Array(3);
const duplex = "full";
assert_throws_js(TypeError,
() => new Request("...", { method, body, duplex }));
}, "It is error to set .duplex = 'full' when the body is a Uint8Array.");

test((t) => {
const body = new Blob([]);
const duplex = "full";
assert_throws_js(TypeError,
() => new Request("...", { method, body, duplex }));
}, "It is error to set .duplex = 'full' when the body is a Blob.");

test((t) => {
const body = new ReadableStream();
const duplex = "full";
assert_throws_js(TypeError,
() => new Request("...", { method, body, duplex }));
}, "It is error to set .duplex = 'full' when the body is a ReadableStream.");

test((t) => {
const body = new ReadableStream();
const duplex = "half";
const req1 = new Request("...", { method, body, duplex });
const req2 = new Request(req1);
}, "It is OK to omit duplex when init.body is not given and input.body is given.");

0 comments on commit bbdb1c4

Please sign in to comment.