Skip to content

Commit

Permalink
Bug 1896059 [wpt PR 46198] - Add tests for bytes() method on Body mix…
Browse files Browse the repository at this point in the history
…in, a=testonly

Automatic update from web-platform-tests
Fetch: bytes() method on Body mixin

For whatwg/fetch#1753.
--

wpt-commits: 1b9332c3c8a84d34e38271d29518c204ab2cfb6e
wpt-pr: 46198
  • Loading branch information
bakkot authored and moz-wptsync-bot committed May 21, 2024
1 parent b71cae2 commit 22cf03f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion testing/web-platform/tests/fetch/api/abort/general.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// META: script=/common/get-host-info.sub.js
// META: script=../request/request-error.js

const BODY_METHODS = ['arrayBuffer', 'blob', 'formData', 'json', 'text'];
const BODY_METHODS = ['arrayBuffer', 'blob', 'bytes', 'formData', 'json', 'text'];

const error1 = new Error('error1');
error1.name = 'error1';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<script>
// Duplicating this resource to make service worker scoping simpler.
const SCOPE = '../resources/basic.html';
const BODY_METHODS = ['arrayBuffer', 'blob', 'formData', 'json', 'text'];
const BODY_METHODS = ['arrayBuffer', 'blob', 'bytes', 'formData', 'json', 'text'];

const error1 = new Error('error1');
error1.name = 'error1';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ function checkBodyArrayBuffer(request, expectedBody) {
});
}

function checkBodyBytes(request, expectedBody) {
return request.bytes().then(function(bodyAsUint8Array) {
assert_true(bodyAsUint8Array instanceof Uint8Array);
validateBufferFromString(bodyAsUint8Array.buffer, expectedBody, "Retrieve and verify request's body");
assert_true(request.bodyUsed, "body as bytes: bodyUsed turned true");
});
}

function checkBodyJSON(request, expectedBody) {
return request.json().then(function(bodyAsJSON) {
var strBody = JSON.stringify(bodyAsJSON)
Expand Down Expand Up @@ -58,6 +66,11 @@ function checkRequestBody(body, expected, bodyType) {
assert_false(request.bodyUsed, "bodyUsed is false at init");
return checkBodyArrayBuffer(request, expected);
}, "Consume " + bodyType + " request's body as arrayBuffer");
promise_test(function(test) {
var request = new Request("", {"method": "POST", "body": body });
assert_false(request.bodyUsed, "bodyUsed is false at init");
return checkBodyBytes(request, expected);
}, "Consume " + bodyType + " request's body as bytes");
promise_test(function(test) {
var request = new Request("", {"method": "POST", "body": body });
assert_false(request.bodyUsed, "bodyUsed is false at init");
Expand Down Expand Up @@ -113,6 +126,7 @@ checkBlobResponseBody(blob, textData, "blob", checkBodyBlob);
checkBlobResponseBody(blob, textData, "text", checkBodyText);
checkBlobResponseBody(blob, textData, "json", checkBodyJSON);
checkBlobResponseBody(blob, textData, "arrayBuffer", checkBodyArrayBuffer);
checkBlobResponseBody(blob, textData, "bytes", checkBodyBytes);
checkBlobResponseBody(new Blob([""]), "", "blob (empty blob as input)", checkBodyBlob);

var goodJSONValues = ["null", "1", "true", "\"string\""];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// META: title=realm of Response bytes()

"use strict";

promise_test(async () => {
await new Promise(resolve => {
onload = resolve;
});

let iframe = document.createElement("iframe");
document.body.appendChild(iframe);
iframe.srcdoc = "<!doctype html>";
await new Promise(resolve => {
iframe.onload = resolve;
});

let otherRealm = iframe.contentWindow;

let ab = await window.Response.prototype.bytes.call(new otherRealm.Response(""));

assert_true(ab instanceof otherRealm.Uint8Array, "Uint8Array should be created in receiver's realm");
assert_false(ab instanceof Uint8Array, "Uint8Array should not be created in the bytes() methods's realm");
}, "realm of the Uint8Array from Response bytes()");
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ promise_test(test => {
// test start() errors for all Body reader methods
runRequestPromiseTest(newStreamWithStartError(), 'arrayBuffer', 'ReadableStream start() Error propagates to Response.arrayBuffer() Promise');
runRequestPromiseTest(newStreamWithStartError(), 'blob', 'ReadableStream start() Error propagates to Response.blob() Promise');
runRequestPromiseTest(newStreamWithStartError(), 'bytes', 'ReadableStream start() Error propagates to Response.bytes() Promise');
runRequestPromiseTest(newStreamWithStartError(), 'formData', 'ReadableStream start() Error propagates to Response.formData() Promise');
runRequestPromiseTest(newStreamWithStartError(), 'json', 'ReadableStream start() Error propagates to Response.json() Promise');
runRequestPromiseTest(newStreamWithStartError(), 'text', 'ReadableStream start() Error propagates to Response.text() Promise');

// test pull() errors for all Body reader methods
runRequestPromiseTest(newStreamWithPullError(), 'arrayBuffer', 'ReadableStream pull() Error propagates to Response.arrayBuffer() Promise');
runRequestPromiseTest(newStreamWithPullError(), 'blob', 'ReadableStream pull() Error propagates to Response.blob() Promise');
runRequestPromiseTest(newStreamWithPullError(), 'bytes', 'ReadableStream pull() Error propagates to Response.bytes() Promise');
runRequestPromiseTest(newStreamWithPullError(), 'formData', 'ReadableStream pull() Error propagates to Response.formData() Promise');
runRequestPromiseTest(newStreamWithPullError(), 'json', 'ReadableStream pull() Error propagates to Response.json() Promise');
runRequestPromiseTest(newStreamWithPullError(), 'text', 'ReadableStream pull() Error propagates to Response.text() Promise');
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function runChunkTest(responseReaderMethod, testDescription) {

runChunkTest('arrayBuffer', 'ReadableStream with non-Uint8Array chunk passed to Response.arrayBuffer() causes TypeError');
runChunkTest('blob', 'ReadableStream with non-Uint8Array chunk passed to Response.blob() causes TypeError');
runChunkTest('bytes', 'ReadableStream with non-Uint8Array chunk passed to Response.bytes() causes TypeError');
runChunkTest('formData', 'ReadableStream with non-Uint8Array chunk passed to Response.formData() causes TypeError');
runChunkTest('json', 'ReadableStream with non-Uint8Array chunk passed to Response.json() causes TypeError');
runChunkTest('text', 'ReadableStream with non-Uint8Array chunk passed to Response.text() causes TypeError');

0 comments on commit 22cf03f

Please sign in to comment.