Skip to content

Commit 1b9332c

Browse files
authored
Fetch: bytes() method on Body mixin
For whatwg/fetch#1753.
1 parent ee22853 commit 1b9332c

File tree

6 files changed

+42
-2
lines changed

6 files changed

+42
-2
lines changed

fetch/api/abort/general.any.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// META: script=/common/get-host-info.sub.js
55
// META: script=../request/request-error.js
66

7-
const BODY_METHODS = ['arrayBuffer', 'blob', 'formData', 'json', 'text'];
7+
const BODY_METHODS = ['arrayBuffer', 'blob', 'bytes', 'formData', 'json', 'text'];
88

99
const error1 = new Error('error1');
1010
error1.name = 'error1';

fetch/api/abort/serviceworker-intercepted.https.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<script>
1212
// Duplicating this resource to make service worker scoping simpler.
1313
const SCOPE = '../resources/basic.html';
14-
const BODY_METHODS = ['arrayBuffer', 'blob', 'formData', 'json', 'text'];
14+
const BODY_METHODS = ['arrayBuffer', 'blob', 'bytes', 'formData', 'json', 'text'];
1515

1616
const error1 = new Error('error1');
1717
error1.name = 'error1';

fetch/api/request/request-consume.any.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ function checkBodyArrayBuffer(request, expectedBody) {
2727
});
2828
}
2929

30+
function checkBodyBytes(request, expectedBody) {
31+
return request.bytes().then(function(bodyAsUint8Array) {
32+
assert_true(bodyAsUint8Array instanceof Uint8Array);
33+
validateBufferFromString(bodyAsUint8Array.buffer, expectedBody, "Retrieve and verify request's body");
34+
assert_true(request.bodyUsed, "body as bytes: bodyUsed turned true");
35+
});
36+
}
37+
3038
function checkBodyJSON(request, expectedBody) {
3139
return request.json().then(function(bodyAsJSON) {
3240
var strBody = JSON.stringify(bodyAsJSON)
@@ -58,6 +66,11 @@ function checkRequestBody(body, expected, bodyType) {
5866
assert_false(request.bodyUsed, "bodyUsed is false at init");
5967
return checkBodyArrayBuffer(request, expected);
6068
}, "Consume " + bodyType + " request's body as arrayBuffer");
69+
promise_test(function(test) {
70+
var request = new Request("", {"method": "POST", "body": body });
71+
assert_false(request.bodyUsed, "bodyUsed is false at init");
72+
return checkBodyBytes(request, expected);
73+
}, "Consume " + bodyType + " request's body as bytes");
6174
promise_test(function(test) {
6275
var request = new Request("", {"method": "POST", "body": body });
6376
assert_false(request.bodyUsed, "bodyUsed is false at init");
@@ -113,6 +126,7 @@ checkBlobResponseBody(blob, textData, "blob", checkBodyBlob);
113126
checkBlobResponseBody(blob, textData, "text", checkBodyText);
114127
checkBlobResponseBody(blob, textData, "json", checkBodyJSON);
115128
checkBlobResponseBody(blob, textData, "arrayBuffer", checkBodyArrayBuffer);
129+
checkBlobResponseBody(blob, textData, "bytes", checkBodyBytes);
116130
checkBlobResponseBody(new Blob([""]), "", "blob (empty blob as input)", checkBodyBlob);
117131

118132
var goodJSONValues = ["null", "1", "true", "\"string\""];
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// META: title=realm of Response bytes()
2+
3+
"use strict";
4+
5+
promise_test(async () => {
6+
await new Promise(resolve => {
7+
onload = resolve;
8+
});
9+
10+
let iframe = document.createElement("iframe");
11+
document.body.appendChild(iframe);
12+
iframe.srcdoc = "<!doctype html>";
13+
await new Promise(resolve => {
14+
iframe.onload = resolve;
15+
});
16+
17+
let otherRealm = iframe.contentWindow;
18+
19+
let ab = await window.Response.prototype.bytes.call(new otherRealm.Response(""));
20+
21+
assert_true(ab instanceof otherRealm.Uint8Array, "Uint8Array should be created in receiver's realm");
22+
assert_false(ab instanceof Uint8Array, "Uint8Array should not be created in the bytes() methods's realm");
23+
}, "realm of the Uint8Array from Response bytes()");

fetch/api/response/response-error-from-stream.any.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ promise_test(test => {
4747
// test start() errors for all Body reader methods
4848
runRequestPromiseTest(newStreamWithStartError(), 'arrayBuffer', 'ReadableStream start() Error propagates to Response.arrayBuffer() Promise');
4949
runRequestPromiseTest(newStreamWithStartError(), 'blob', 'ReadableStream start() Error propagates to Response.blob() Promise');
50+
runRequestPromiseTest(newStreamWithStartError(), 'bytes', 'ReadableStream start() Error propagates to Response.bytes() Promise');
5051
runRequestPromiseTest(newStreamWithStartError(), 'formData', 'ReadableStream start() Error propagates to Response.formData() Promise');
5152
runRequestPromiseTest(newStreamWithStartError(), 'json', 'ReadableStream start() Error propagates to Response.json() Promise');
5253
runRequestPromiseTest(newStreamWithStartError(), 'text', 'ReadableStream start() Error propagates to Response.text() Promise');
5354

5455
// test pull() errors for all Body reader methods
5556
runRequestPromiseTest(newStreamWithPullError(), 'arrayBuffer', 'ReadableStream pull() Error propagates to Response.arrayBuffer() Promise');
5657
runRequestPromiseTest(newStreamWithPullError(), 'blob', 'ReadableStream pull() Error propagates to Response.blob() Promise');
58+
runRequestPromiseTest(newStreamWithPullError(), 'bytes', 'ReadableStream pull() Error propagates to Response.bytes() Promise');
5759
runRequestPromiseTest(newStreamWithPullError(), 'formData', 'ReadableStream pull() Error propagates to Response.formData() Promise');
5860
runRequestPromiseTest(newStreamWithPullError(), 'json', 'ReadableStream pull() Error propagates to Response.json() Promise');
5961
runRequestPromiseTest(newStreamWithPullError(), 'text', 'ReadableStream pull() Error propagates to Response.text() Promise');

fetch/api/response/response-stream-bad-chunk.any.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function runChunkTest(responseReaderMethod, testDescription) {
1919

2020
runChunkTest('arrayBuffer', 'ReadableStream with non-Uint8Array chunk passed to Response.arrayBuffer() causes TypeError');
2121
runChunkTest('blob', 'ReadableStream with non-Uint8Array chunk passed to Response.blob() causes TypeError');
22+
runChunkTest('bytes', 'ReadableStream with non-Uint8Array chunk passed to Response.bytes() causes TypeError');
2223
runChunkTest('formData', 'ReadableStream with non-Uint8Array chunk passed to Response.formData() causes TypeError');
2324
runChunkTest('json', 'ReadableStream with non-Uint8Array chunk passed to Response.json() causes TypeError');
2425
runChunkTest('text', 'ReadableStream with non-Uint8Array chunk passed to Response.text() causes TypeError');

0 commit comments

Comments
 (0)