Skip to content

Commit 8981e01

Browse files
committed
Add WPTs for RequestInit.duplex
This is for whatwg/fetch#1457.
1 parent bd23d17 commit 8981e01

File tree

3 files changed

+115
-23
lines changed

3 files changed

+115
-23
lines changed

fetch/api/basic/request-upload.any.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function testUpload(desc, url, method, createBody, expectedBody) {
99
const body = createBody();
1010
if (body) {
1111
requestInit["body"] = body;
12+
requestInit.duplex = "half";
1213
}
1314
return fetch(url, requestInit).then(function(resp) {
1415
return resp.text().then((text)=> {

fetch/api/basic/request-upload.h2.any.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function testUpload(desc, url, method, createBody, expectedBody) {
99
const body = createBody();
1010
if (body) {
1111
requestInit["body"] = body;
12+
requestInit.duplex = "half";
1213
}
1314
const resp = await fetch(url, requestInit);
1415
const text = await resp.text();

fetch/api/request/request-init-stream.any.js

+113-23
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,146 @@
22

33
"use strict";
44

5+
const duplex = "half";
6+
const method = "POST";
7+
58
test(() => {
6-
const stream = new ReadableStream();
7-
const request = new Request("...", { method:"POST", body: stream });
9+
const body = new ReadableStream({});
10+
const request = new Request("...", { method, body, duplex });
811
assert_equals(request.body, stream);
912
}, "Constructing a Request with a stream holds the original object.");
1013

11-
async function assert_request(test, input, init) {
12-
assert_throws_js(TypeError, () => new Request(input, init), "new Request()");
13-
await promise_rejects_js(test, TypeError, fetch(input, init), "fetch()");
14-
}
15-
1614
promise_test(async (t) => {
17-
const stream = new ReadableStream();
18-
stream.getReader();
19-
await assert_request(t, "...", { method:"POST", body: stream });
15+
const body = new ReadableStream();
16+
body.getReader();
17+
assert_throws_js(TypeError,
18+
() => new Request("...", { method, body, duplex }));
2019
}, "Constructing a Request with a stream on which getReader() is called");
2120

2221
promise_test(async (t) => {
23-
const stream = new ReadableStream();
22+
const body = new ReadableStream({});
2423
stream.getReader().read();
25-
await assert_request(t, "...", { method:"POST", body: stream });
24+
assert_throws_js(TypeError,
25+
() => new Request("...", { method, body, duplex }));
2626
}, "Constructing a Request with a stream on which read() is called");
2727

2828
promise_test(async (t) => {
29-
const stream = new ReadableStream({ pull: c => c.enqueue(new Uint8Array()) }),
30-
reader = stream.getReader();
29+
const body = new ReadableStream({ pull: c => c.enqueue(new Uint8Array()) });
30+
const reader = body.getReader();
3131
await reader.read();
3232
reader.releaseLock();
33-
await assert_request(t, "...", { method:"POST", body: stream });
33+
assert_throws_js(TypeError,
34+
() => "...", { method, body, duplex });
3435
}, "Constructing a Request with a stream on which read() and releaseLock() are called");
3536

3637
promise_test(async (t) => {
3738
const request = new Request("...", { method: "POST", body: "..." });
3839
request.body.getReader();
39-
await assert_request(t, request);
40-
assert_class_string(new Request(request, { body: "..." }), "Request");
40+
assert_throws_js(TypeError, () => new Request(request));
41+
// This doesn't throw.
42+
const r = new Request(request, { body: "..." });
4143
}, "Constructing a Request with a Request on which body.getReader() is called");
4244

4345
promise_test(async (t) => {
4446
const request = new Request("...", { method: "POST", body: "..." });
4547
request.body.getReader().read();
46-
await assert_request(t, request);
47-
assert_class_string(new Request(request, { body: "..." }), "Request");
48+
assert_throws_js(TypeError, () => new Request(request));
49+
// This doesn't throw.
50+
const r = new Request(request, { body: "..." });
4851
}, "Constructing a Request with a Request on which body.getReader().read() is called");
4952

5053
promise_test(async (t) => {
51-
const request = new Request("...", { method: "POST", body: "..." }),
52-
reader = request.body.getReader();
54+
const request = new Request("...", { method: "POST", body: "..." });
55+
const reader = request.body.getReader();
5356
await reader.read();
5457
reader.releaseLock();
55-
await assert_request(t, request);
56-
assert_class_string(new Request(request, { body: "..." }), "Request");
58+
assert_throws_js(TypeError, () => new Request(request));
59+
// This doesn't throw.
60+
const r = new Request(request, { body: "..." });
5761
}, "Constructing a Request with a Request on which read() and releaseLock() are called");
62+
63+
test((t) => {
64+
new Request("...", { method, body: null });
65+
}, "It is OK to omit .duplex when the body is null.");
66+
67+
test((t) => {
68+
new Request("...", { method, body: "..." });
69+
}, "It is OK to omit .duplex when the body is a string.");
70+
71+
test((t) => {
72+
new Request("...", { method, body: new Uint8Array(3) });
73+
}, "It is OK to omit .duplex when the body is a Uint8Array.");
74+
75+
test((t) => {
76+
new Request("...", { method, body: new Blob([]) });
77+
}, "It is OK to omit .duplex when the body is a Blob.");
78+
79+
test((t) => {
80+
const body = new ReadableStream({});
81+
assert_throws_js(TypeError,
82+
() => new Request("...", { method, body }));
83+
}, "It is error to omit .duplex when the body is a ReadableStream.");
84+
85+
test((t) => {
86+
new Request("...", { method, body: null, duplex: "half" });
87+
}, "It is OK to set .duplex = 'half' when the body is null.");
88+
89+
test((t) => {
90+
new Request("...", { method, body: "...", duplex: "half" });
91+
}, "It is OK to set .duplex = 'half' when the body is a string.");
92+
93+
test((t) => {
94+
new Request("...", { method, body: new Uint8Array(3), duplex: "half" });
95+
}, "It is OK to set .duplex = 'half' when the body is a Uint8Array.");
96+
97+
test((t) => {
98+
new Request("...", { method, body: new Blob([]), duplex: "half" });
99+
}, "It is OK to set .duplex = 'half' when the body is a Blob.");
100+
101+
test((t) => {
102+
const body = new ReadableStream({});
103+
new Request("...", { method, body, duplex: "half" });
104+
}, "It is OK to set .duplex = 'half' when the body is a ReadableStream.");
105+
106+
test((t) => {
107+
const body = null;
108+
const duplex = "full";
109+
assert_throws_js(TypeError,
110+
() => new Request("...", { method, body, duplex }));
111+
}, "It is error to set .duplex = 'full' when the body is null.");
112+
113+
test((t) => {
114+
const body = "...";
115+
const duplex = "full";
116+
assert_throws_js(TypeError,
117+
() => new Request("...", { method, body, duplex }));
118+
}, "It is error to set .duplex = 'full' when the body is a string.");
119+
120+
test((t) => {
121+
const body = new Uint8Array(3);
122+
const duplex = "full";
123+
assert_throws_js(TypeError,
124+
() => new Request("...", { method, body, duplex }));
125+
}, "It is error to set .duplex = 'full' when the body is a Uint8Array.");
126+
127+
test((t) => {
128+
const body = new Blob([]);
129+
const duplex = "full";
130+
assert_throws_js(TypeError,
131+
() => new Request("...", { method, body, duplex }));
132+
}, "It is error to set .duplex = 'full' when the body is a Blob.");
133+
134+
test((t) => {
135+
const body = new ReadableStream({});
136+
const duplex = "full";
137+
assert_throws_js(TypeError,
138+
() => new Request("...", { method, body, duplex }));
139+
}, "It is error to set .duplex = 'full' when the body is a ReadableStream.");
140+
141+
test((t) => {
142+
const body = new ReadableStream({});
143+
const duplex = "half";
144+
const req1 = new Request("...", { method, body, duplex });
145+
const req2 = new Request(req1);
146+
}, "It is OK to omit duplex when init.body is not given and input.body is given.");
147+

0 commit comments

Comments
 (0)