Skip to content

Commit 0dab43c

Browse files
KhafraDevronag
authored andcommitted
wpt: add request-consume-empty.any.js
1 parent ed32418 commit 0dab43c

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

lib/fetch/body.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ function bodyMixinMethods (instance) {
394394

395395
const busboyResolve = new Promise((resolve, reject) => {
396396
busboy.on('finish', resolve)
397-
busboy.on('error', (err) => reject(err))
397+
busboy.on('error', (err) => reject(new TypeError(err)))
398398
})
399399

400400
if (this.body !== null) for await (const chunk of consumeBody(this[kState].body)) busboy.write(chunk)

test/wpt/status/fetch.status.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,11 @@
123123
"Consume blob response's body as blob",
124124
"Consume blob response's body as blob (empty blob as input)"
125125
]
126+
},
127+
"request-consume-empty.any.js": {
128+
"fail": [
129+
"Consume request's body as blob",
130+
"Consume empty FormData request body as text"
131+
]
126132
}
127133
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// META: global=window,worker
2+
// META: title=Request consume empty bodies
3+
4+
function checkBodyText(test, request) {
5+
return request.text().then(function(bodyAsText) {
6+
assert_equals(bodyAsText, "", "Resolved value should be empty");
7+
assert_false(request.bodyUsed);
8+
});
9+
}
10+
11+
function checkBodyBlob(test, request) {
12+
return request.blob().then(function(bodyAsBlob) {
13+
var promise = new Promise(function(resolve, reject) {
14+
var reader = new FileReader();
15+
reader.onload = function(evt) {
16+
resolve(reader.result)
17+
};
18+
reader.onerror = function() {
19+
reject("Blob's reader failed");
20+
};
21+
reader.readAsText(bodyAsBlob);
22+
});
23+
return promise.then(function(body) {
24+
assert_equals(body, "", "Resolved value should be empty");
25+
assert_false(request.bodyUsed);
26+
});
27+
});
28+
}
29+
30+
function checkBodyArrayBuffer(test, request) {
31+
return request.arrayBuffer().then(function(bodyAsArrayBuffer) {
32+
assert_equals(bodyAsArrayBuffer.byteLength, 0, "Resolved value should be empty");
33+
assert_false(request.bodyUsed);
34+
});
35+
}
36+
37+
function checkBodyJSON(test, request) {
38+
return request.json().then(
39+
function(bodyAsJSON) {
40+
assert_unreached("JSON parsing should fail");
41+
},
42+
function() {
43+
assert_false(request.bodyUsed);
44+
});
45+
}
46+
47+
function checkBodyFormData(test, request) {
48+
return request.formData().then(function(bodyAsFormData) {
49+
assert_true(bodyAsFormData instanceof FormData, "Should receive a FormData");
50+
assert_false(request.bodyUsed);
51+
});
52+
}
53+
54+
function checkBodyFormDataError(test, request) {
55+
return promise_rejects_js(test, TypeError, request.formData()).then(function() {
56+
assert_false(request.bodyUsed);
57+
});
58+
}
59+
60+
function checkRequestWithNoBody(bodyType, checkFunction, headers = []) {
61+
promise_test(function(test) {
62+
var request = new Request("", {"method": "POST", "headers": headers});
63+
assert_false(request.bodyUsed);
64+
return checkFunction(test, request);
65+
}, "Consume request's body as " + bodyType);
66+
}
67+
68+
checkRequestWithNoBody("text", checkBodyText);
69+
checkRequestWithNoBody("blob", checkBodyBlob);
70+
checkRequestWithNoBody("arrayBuffer", checkBodyArrayBuffer);
71+
checkRequestWithNoBody("json (error case)", checkBodyJSON);
72+
checkRequestWithNoBody("formData with correct multipart type (error case)", checkBodyFormDataError, [["Content-Type", 'multipart/form-data; boundary="boundary"']]);
73+
checkRequestWithNoBody("formData with correct urlencoded type", checkBodyFormData, [["Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"]]);
74+
checkRequestWithNoBody("formData without correct type (error case)", checkBodyFormDataError);
75+
76+
function checkRequestWithEmptyBody(bodyType, body, asText) {
77+
promise_test(function(test) {
78+
var request = new Request("", {"method": "POST", "body": body});
79+
assert_false(request.bodyUsed, "bodyUsed is false at init");
80+
if (asText) {
81+
return request.text().then(function(bodyAsString) {
82+
assert_equals(bodyAsString.length, 0, "Resolved value should be empty");
83+
assert_true(request.bodyUsed, "bodyUsed is true after being consumed");
84+
});
85+
}
86+
return request.arrayBuffer().then(function(bodyAsArrayBuffer) {
87+
assert_equals(bodyAsArrayBuffer.byteLength, 0, "Resolved value should be empty");
88+
assert_true(request.bodyUsed, "bodyUsed is true after being consumed");
89+
});
90+
}, "Consume empty " + bodyType + " request body as " + (asText ? "text" : "arrayBuffer"));
91+
}
92+
93+
// FIXME: Add BufferSource, FormData and URLSearchParams.
94+
checkRequestWithEmptyBody("blob", new Blob([], { "type" : "text/plain" }), false);
95+
checkRequestWithEmptyBody("text", "", false);
96+
checkRequestWithEmptyBody("blob", new Blob([], { "type" : "text/plain" }), true);
97+
checkRequestWithEmptyBody("text", "", true);
98+
checkRequestWithEmptyBody("URLSearchParams", new URLSearchParams(""), true);
99+
// FIXME: This test assumes that the empty string be returned but it is not clear whether that is right. See https://github.com/web-platform-tests/wpt/pull/3950.
100+
checkRequestWithEmptyBody("FormData", new FormData(), true);
101+
checkRequestWithEmptyBody("ArrayBuffer", new ArrayBuffer(), true);

0 commit comments

Comments
 (0)