Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request upload: test synchronous feature detect #36048

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 70 additions & 9 deletions fetch/api/basic/request-upload.h2.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@

const duplex = "half";

function testUpload(desc, url, method, createBody, expectedBody) {
async function assertUpload(url, method, createBody, expectedBody) {
const requestInit = {method};
const body = createBody();
if (body) {
requestInit["body"] = body;
requestInit.duplex = "half";
}
const resp = await fetch(url, requestInit);
const text = await resp.text();
assert_equals(text, expectedBody);
}

function testUpload(desc, url, method, createBody, expectedBody) {
promise_test(async () => {
const body = createBody();
if (body) {
requestInit["body"] = body;
requestInit.duplex = "half";
}
const resp = await fetch(url, requestInit);
const text = await resp.text();
assert_equals(text, expectedBody);
await assertUpload(url, method, createBody, expectedBody);
}, desc);
}

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

test(() => {
let duplexAccessed = false;

const request = new Request("", {
body: new ReadableStream(),
method: "POST",
get duplex() {
duplexAccessed = true;
return "half";
},
});

assert_equals(
request.headers.get("Content-Type"),
null,
`Request should not have a content-type set`
);
assert_true(duplexAccessed, `duplex dictionary property should be accessed`);
}, "Synchronous feature detect");

// The asserts the synchronousFeatureDetect isn't broken by a partial implementation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// The asserts the synchronousFeatureDetect isn't broken by a partial implementation.
// This asserts the synchronousFeatureDetect isn't broken by a partial implementation.

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot about this nit... Maybe fix in a follow-up?

// An earlier feature detect was broken by Safari implementing streaming bodies as part of Request,
// but it failed when passed to fetch().
// This tests ensures that UAs must not implement RequestInit.duplex and streaming request bodies without also implementing the fetch() parts.
promise_test(async () => {
let duplexAccessed = false;

const request = new Request("", {
body: new ReadableStream(),
method: "POST",
get duplex() {
duplexAccessed = true;
return "half";
},
});

const supported =
request.headers.get("Content-Type") === null && duplexAccessed;

// If the feature detect fails, assume the browser is being truthful (other tests pick up broken cases here)
if (!supported) return false;

await assertUpload(
url,
"POST",
() =>
new ReadableStream({
start: (controller) => {
const encoder = new TextEncoder();
controller.enqueue(encoder.encode("Test"));
controller.close();
},
}),
"Test"
);
}, "Synchronous feature detect fails if feature unsupported");

promise_test(async (t) => {
const body = createStream(["hello"]);
const method = "POST";
Expand Down