Skip to content

Commit

Permalink
fix(ext/fetch): support empty formdata (denoland#16165)
Browse files Browse the repository at this point in the history
This PR adds support for empty `FormData` parsing in
`Response`/`Request`

```js
new Response(new FormData()).formData()
```

ref: web-platform-tests/wpt#28607
  • Loading branch information
marcosc90 committed Oct 7, 2022
1 parent e136bd8 commit a5d55fe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
14 changes: 12 additions & 2 deletions ext/fetch/21_formdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,19 @@
* @returns {FormData}
*/
parse() {
// Body must be at least 2 boundaries + \r\n + -- on the last boundary.
// To have fields body must be at least 2 boundaries + \r\n + --
// on the last boundary.
if (this.body.length < (this.boundary.length * 2) + 4) {
throw new TypeError("Form data too short to be valid.");
const decodedBody = core.decode(this.body);
const lastBoundary = this.boundary + "--";
// check if it's an empty valid form data
if (
decodedBody === lastBoundary ||
decodedBody === lastBoundary + "\r\n"
) {
return new FormData();
}
throw new TypeError("Unable to parse body as form data.");
}

const formData = new FormData();
Expand Down
20 changes: 4 additions & 16 deletions tools/wpt/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -3077,14 +3077,8 @@
"response-static-json.any.worker.html": true
},
"body": {
"formdata.any.html": [
"Consume empty response.formData() as FormData",
"Consume empty request.formData() as FormData"
],
"formdata.any.worker.html": [
"Consume empty response.formData() as FormData",
"Consume empty request.formData() as FormData"
],
"formdata.any.html": true,
"formdata.any.worker.html": true,
"mime-type.any.html": true,
"mime-type.any.worker.html": true
},
Expand Down Expand Up @@ -3231,14 +3225,8 @@
"response.text() rejects if already aborted",
"Call text() twice on aborted response"
],
"request.any.html": [
"Calling formData() on an aborted request",
"Aborting a request after calling formData()"
],
"request.any.worker.html": [
"Calling formData() on an aborted request",
"Aborting a request after calling formData()"
],
"request.any.html": true,
"request.any.worker.html": true,
"cache.https.any.html": false,
"cache.https.any.worker.html": false
}
Expand Down

0 comments on commit a5d55fe

Please sign in to comment.