diff --git a/fetch/api/basic/request-upload.any.js b/fetch/api/basic/request-upload.any.js index 24f7c5b432caac..9168aa11541f1c 100644 --- a/fetch/api/basic/request-upload.any.js +++ b/fetch/api/basic/request-upload.any.js @@ -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)=> { diff --git a/fetch/api/basic/request-upload.h2.any.js b/fetch/api/basic/request-upload.h2.any.js index 355a331c9d630c..70d11eca76694b 100644 --- a/fetch/api/basic/request-upload.h2.any.js +++ b/fetch/api/basic/request-upload.h2.any.js @@ -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(); diff --git a/fetch/api/request/request-init-stream.any.js b/fetch/api/request/request-init-stream.any.js index 8c50c4929e75da..ac78a253f7d0df 100644 --- a/fetch/api/request/request-init-stream.any.js +++ b/fetch/api/request/request-init-stream.any.js @@ -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."); +