Skip to content

Commit 88eca8d

Browse files
authored
fix(fetch): allow third-party FormData/Blob bodies (nodejs#1643)
1 parent 0862fba commit 88eca8d

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

lib/fetch/response.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ webidl.converters.XMLHttpRequestBodyInit = function (V) {
519519
}
520520

521521
if (isBlobLike(V)) {
522-
return webidl.converters.Blob(V)
522+
return webidl.converters.Blob(V, { strict: false })
523523
}
524524

525525
if (
@@ -530,8 +530,8 @@ webidl.converters.XMLHttpRequestBodyInit = function (V) {
530530
return webidl.converters.BufferSource(V)
531531
}
532532

533-
if (V instanceof FormData) {
534-
return webidl.converters.FormData(V)
533+
if (util.isFormDataLike(V)) {
534+
return webidl.converters.FormData(V, { strict: false })
535535
}
536536

537537
if (V instanceof URLSearchParams) {

test/fetch/request.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const {
99
fetch
1010
} = require('../../')
1111
const { kState } = require('../../lib/fetch/symbols.js')
12+
const {
13+
Blob: ThirdPartyBlob,
14+
FormData: ThirdPartyFormData
15+
} = require('formdata-node')
1216
const hasSignalReason = !!~process.version.localeCompare('v16.14.0', undefined, { numeric: true })
1317

1418
test('arg validation', async (t) => {
@@ -435,3 +439,23 @@ test('RequestInit.signal option', async (t) => {
435439
signal: false
436440
}), TypeError)
437441
})
442+
443+
test('constructing Request with third party Blob body', async (t) => {
444+
const blob = new ThirdPartyBlob(['text'])
445+
const req = new Request('http://asd', {
446+
method: 'POST',
447+
body: blob
448+
})
449+
t.equal(await req.text(), 'text')
450+
})
451+
test('constructing Request with third party FormData body', async (t) => {
452+
const form = new ThirdPartyFormData()
453+
form.set('key', 'value')
454+
const req = new Request('http://asd', {
455+
method: 'POST',
456+
body: form
457+
})
458+
const contentType = req.headers.get('content-type').split('=')
459+
t.equal(contentType[0], 'multipart/form-data; boundary')
460+
t.ok((await req.text()).startsWith(`--${contentType[1]}`))
461+
})

test/fetch/response.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const {
55
Response
66
} = require('../../')
77
const { ReadableStream } = require('stream/web')
8+
const {
9+
Blob: ThirdPartyBlob,
10+
FormData: ThirdPartyFormData
11+
} = require('formdata-node')
812

913
test('arg validation', async (t) => {
1014
// constructor
@@ -230,3 +234,17 @@ test('constructing a Response with a ReadableStream body', async (t) => {
230234

231235
t.end()
232236
})
237+
238+
test('constructing Response with third party Blob body', async (t) => {
239+
const blob = new ThirdPartyBlob(['text'])
240+
const res = new Response(blob)
241+
t.equal(await res.text(), 'text')
242+
})
243+
test('constructing Response with third party FormData body', async (t) => {
244+
const form = new ThirdPartyFormData()
245+
form.set('key', 'value')
246+
const res = new Response(form)
247+
const contentType = res.headers.get('content-type').split('=')
248+
t.equal(contentType[0], 'multipart/form-data; boundary')
249+
t.ok((await res.text()).startsWith(`--${contentType[1]}`))
250+
})

0 commit comments

Comments
 (0)