Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure Blob and File type are included in FormData #1467

Merged
merged 1 commit into from May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/fetch/formdata.js
Expand Up @@ -243,8 +243,8 @@ function makeEntry (name, value, filename) {
// object, representing the same bytes, whose name attribute value is "blob".
if (isBlobLike(value) && !isFileLike(value)) {
value = value instanceof Blob
? new File([value], 'blob')
: new FileLike(value, 'blob')
? new File([value], 'blob', value)
: new FileLike(value, 'blob', value)
}

// 4. If value is (now) a File object and filename is given, then set value to a
Expand All @@ -256,8 +256,8 @@ function makeEntry (name, value, filename) {
// creating one more File instance doesn't make much sense....
if (isFileLike(value) && filename != null) {
value = value instanceof File
? new File([value], filename)
: new FileLike(value, filename)
? new File([value], filename, value)
: new FileLike(value, filename, value)
}

// 5. Set entry’s value to value.
Expand Down
9 changes: 6 additions & 3 deletions test/fetch/formdata.js
Expand Up @@ -83,13 +83,14 @@ test('arg validation', (t) => {

test('append file', (t) => {
const form = new FormData()
form.set('asd', new File([], 'asd1'), 'asd2')
form.set('asd', new File([], 'asd1', { type: 'text/plain' }), 'asd2')
form.append('asd2', new File([], 'asd1'), 'asd2')

t.equal(form.has('asd'), true)
t.equal(form.has('asd2'), true)
t.equal(form.get('asd').name, 'asd2')
t.equal(form.get('asd2').name, 'asd2')
t.equal(form.get('asd').type, 'text/plain')
form.delete('asd')
t.equal(form.get('asd'), null)
t.equal(form.has('asd2'), true)
Expand All @@ -100,9 +101,10 @@ test('append file', (t) => {

test('append blob', async (t) => {
const form = new FormData()
form.set('asd', new Blob(['asd1']))
form.set('asd', new Blob(['asd1'], { type: 'text/plain' }))

t.equal(form.has('asd'), true)
t.equal(form.get('asd').type, 'text/plain')
t.equal(await form.get('asd').text(), 'asd1')
form.delete('asd')
t.equal(form.get('asd'), null)
Expand All @@ -112,9 +114,10 @@ test('append blob', async (t) => {

test('append third-party blob', async (t) => {
const form = new FormData()
form.set('asd', new ThirdPartyBlob(['asd1']))
form.set('asd', new ThirdPartyBlob(['asd1'], { type: 'text/plain' }))

t.equal(form.has('asd'), true)
t.equal(form.get('asd').type, 'text/plain')
t.equal(await form.get('asd').text(), 'asd1')
form.delete('asd')
t.equal(form.get('asd'), null)
Expand Down