Skip to content

Commit

Permalink
test: update test to support node@14 and node@16
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 committed Apr 8, 2024
1 parent 031c36d commit e0406d9
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 10 deletions.
2 changes: 0 additions & 2 deletions lib/form-data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

// TODO: remove coverage ignore when node > 18
/* istanbul ignore file */
const { randomUUID } = require('node:crypto')
const { Readable } = require('node:stream')

Expand Down
2 changes: 0 additions & 2 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ function Request (options) {
let payload = options.payload || options.body || null
let payloadResume = payload && typeof payload.resume === 'function'

// TODO: remove coverage ignore when node > 18
/* istanbul ignore if */
if (isFormDataLike(payload)) {
const stream = formDataToStream(payload)
payload = stream.stream
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"standard": "^17.0.0",
"tap": "^16.0.0",
"tinybench": "^2.5.1",
"tsd": "^0.31.0"
"tsd": "^0.31.0",
"undici": "^5.28.4"
},
"scripts": {
"benchmark": "node benchmark/benchmark.js",
Expand Down
96 changes: 91 additions & 5 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2013,13 +2013,99 @@ test('request that is destroyed does not error', (t) => {
})
})

test('undici / native form-data should be handled correctly', (t) => {
if (parseInt(process.versions.node.split('.', 1)[0], 10) < 18) {
t.pass('Skip because Node version < 18 do not provide FetchAPI')
t.end()
return
test('native form-data should be handled correctly', (t) => {
t.plan(23)

const dispatch = function (req, res) {
let body = ''
t.ok(/multipart\/form-data; boundary=----formdata-[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}(--)?$/.test(req.headers['content-type']), 'proper Content-Type provided')
req.on('data', d => {
body += d
})
req.on('end', () => {
res.end(body)
})
}

const form = new FormData()
form.append('field', 'value')
form.append('blob', new Blob(['value']), '')
form.append('blob-with-type', new Blob(['value'], { type: 'text/plain' }), '')
form.append('blob-with-name', new Blob(['value']), 'file.txt')
form.append('number', 1)

inject(dispatch, {
method: 'POST',
url: 'http://example.com:8080/hello',
payload: form
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)

const regexp = [
// header
/^------formdata-[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}(--)?$/,
// content-disposition
/^Content-Disposition: form-data; name="(.*)"(; filename="(.*)")?$/,
// content-type
/^Content-Type: (.*)$/
]
const readable = Readable.from(res.body.split('\r\n'))
let i = 1
readable.on('data', function (chunk) {
switch (i) {
case 1:
case 5:
case 10:
case 15:
case 20: {
// header
t.ok(regexp[0].test(chunk), 'correct header')
break
}
case 2:
case 6:
case 11:
case 16: {
// content-disposition
t.ok(regexp[1].test(chunk), 'correct content-disposition')
break
}
case 7:
case 12:
case 17: {
// content-type
t.ok(regexp[2].test(chunk), 'correct content-type')
break
}
case 3:
case 8:
case 13:
case 18: {
// empty
t.equal(chunk, '', 'correct space')
break
}
case 4:
case 9:
case 14:
case 19: {
// value
t.equal(chunk, 'value', 'correct value')
break
}
}
i++
})
})
}, { skip: globalThis.FormData == null || globalThis.Blob == null })

// used for testing node@14 / node@16
test('undici form-data should be handled correctly', (t) => {
// inline require to prevent global variable pollution
const { FormData } = require('undici')
const { Blob } = require('node:buffer')

t.plan(23)

const dispatch = function (req, res) {
Expand Down

0 comments on commit e0406d9

Please sign in to comment.