Skip to content

Commit 9dd8781

Browse files
jakearchibaldmoz-wptsync-bot
authored andcommitted
Bug 1792178 [wpt PR 36048] - Request upload: test synchronous feature detect, a=testonly
Automatic update from web-platform-tests Request upload: test synchronous feature detect For whatwg/fetch#1493. -- wpt-commits: 9841e79d6f1301738125a2fbfbc6992c7b8ae366 wpt-pr: 36048
1 parent 3abc954 commit 9dd8781

File tree

1 file changed

+70
-9
lines changed

1 file changed

+70
-9
lines changed

testing/web-platform/tests/fetch/api/basic/request-upload.h2.any.js

+70-9
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55

66
const duplex = "half";
77

8-
function testUpload(desc, url, method, createBody, expectedBody) {
8+
async function assertUpload(url, method, createBody, expectedBody) {
99
const requestInit = {method};
10+
const body = createBody();
11+
if (body) {
12+
requestInit["body"] = body;
13+
requestInit.duplex = "half";
14+
}
15+
const resp = await fetch(url, requestInit);
16+
const text = await resp.text();
17+
assert_equals(text, expectedBody);
18+
}
19+
20+
function testUpload(desc, url, method, createBody, expectedBody) {
1021
promise_test(async () => {
11-
const body = createBody();
12-
if (body) {
13-
requestInit["body"] = body;
14-
requestInit.duplex = "half";
15-
}
16-
const resp = await fetch(url, requestInit);
17-
const text = await resp.text();
18-
assert_equals(text, expectedBody);
22+
await assertUpload(url, method, createBody, expectedBody);
1923
}, desc);
2024
}
2125

@@ -98,6 +102,63 @@ promise_test(async (test) => {
98102
assert_equals(await response.text(), 'test', `Response has correct body`);
99103
}, "Feature detect for POST with ReadableStream, using request object");
100104

105+
test(() => {
106+
let duplexAccessed = false;
107+
108+
const request = new Request("", {
109+
body: new ReadableStream(),
110+
method: "POST",
111+
get duplex() {
112+
duplexAccessed = true;
113+
return "half";
114+
},
115+
});
116+
117+
assert_equals(
118+
request.headers.get("Content-Type"),
119+
null,
120+
`Request should not have a content-type set`
121+
);
122+
assert_true(duplexAccessed, `duplex dictionary property should be accessed`);
123+
}, "Synchronous feature detect");
124+
125+
// The asserts the synchronousFeatureDetect isn't broken by a partial implementation.
126+
// An earlier feature detect was broken by Safari implementing streaming bodies as part of Request,
127+
// but it failed when passed to fetch().
128+
// This tests ensures that UAs must not implement RequestInit.duplex and streaming request bodies without also implementing the fetch() parts.
129+
promise_test(async () => {
130+
let duplexAccessed = false;
131+
132+
const request = new Request("", {
133+
body: new ReadableStream(),
134+
method: "POST",
135+
get duplex() {
136+
duplexAccessed = true;
137+
return "half";
138+
},
139+
});
140+
141+
const supported =
142+
request.headers.get("Content-Type") === null && duplexAccessed;
143+
144+
// If the feature detect fails, assume the browser is being truthful (other tests pick up broken cases here)
145+
if (!supported) return false;
146+
147+
await assertUpload(
148+
url,
149+
"POST",
150+
() =>
151+
new ReadableStream({
152+
start: (controller) => {
153+
const encoder = new TextEncoder();
154+
controller.enqueue(encoder.encode("Test"));
155+
controller.close();
156+
},
157+
}),
158+
"Test"
159+
);
160+
}, "Synchronous feature detect fails if feature unsupported");
161+
101162
promise_test(async (t) => {
102163
const body = createStream(["hello"]);
103164
const method = "POST";

0 commit comments

Comments
 (0)