Skip to content

Commit e7ea461

Browse files
authored
Add transfer-encoding for streaming (#338)
1 parent f60173c commit e7ea461

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module.exports = require('neostandard')({
44
ignores: [
55
...require('neostandard').resolveIgnoresFromGitignore(),
6-
'test/benchmark.js',
6+
'test/benchmark.js'
77
],
88
ts: true
99
})

lib/request.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ function Request (options) {
107107

108108
this.headers = {}
109109
this.rawHeaders = []
110+
110111
const headers = options.headers || {}
111112

112113
for (const field in headers) {
@@ -159,6 +160,7 @@ function Request (options) {
159160
payloadResume = true
160161
// we override the content-type
161162
this.headers['content-type'] = stream.contentType
163+
this.headers['transfer-encoding'] = 'chunked'
162164
}
163165

164166
if (payload && typeof payload !== 'string' && !payloadResume && !Buffer.isBuffer(payload)) {
@@ -206,7 +208,7 @@ function Request (options) {
206208
this.push(null)
207209
})
208210
} else {
209-
// Stream v1 are handled in index.js asynchronously
211+
// Stream v1 are handled in index.js synchronously
210212
this._read = readEverythingElse
211213
}
212214
}
@@ -239,7 +241,6 @@ function readEverythingElse () {
239241
if (this._lightMyRequest.simulate.end !== false) {
240242
this.push(null)
241243
}
242-
243244
return
244245
}
245246

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"form-auto-content": "^3.2.1",
2222
"form-data": "^4.0.0",
2323
"formdata-node": "^6.0.3",
24+
"multer": "^1.4.5-lts.1",
2425
"neostandard": "^0.12.0",
2526
"tinybench": "^3.0.0",
2627
"tsd": "^0.31.0",

test/index.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const zlib = require('node:zlib')
88
const http = require('node:http')
99
const eos = require('end-of-stream')
1010
const express = require('express')
11+
const multer = require('multer')
1112

1213
const inject = require('../index')
1314
const parseURL = require('../lib/parse-url')
@@ -2275,3 +2276,41 @@ test('query method works', (t, done) => {
22752276
done()
22762277
})
22772278
})
2279+
2280+
test('should return the file content', async (t) => {
2281+
const multerMiddleware = multer({
2282+
storage: multer.memoryStorage(),
2283+
limits: {
2284+
fileSize: 1024
2285+
}
2286+
})
2287+
2288+
const app = express()
2289+
2290+
app.use((req, res, next) => {
2291+
if (req.headers['content-type'].indexOf('multipart/form-data') === 0) {
2292+
req.multipart = true
2293+
}
2294+
next()
2295+
})
2296+
2297+
app.post('/hello', multerMiddleware.single('textFile'), (req, res) => {
2298+
res.send(req.file.buffer.toString('utf8'))
2299+
})
2300+
app.use((err, req, res, next) => {
2301+
console.warn(err)
2302+
res.status(500).send('Something was wrong')
2303+
})
2304+
2305+
const formData = new FormData()
2306+
formData.append('textFile', new Blob(['some data']), 'sample.txt')
2307+
2308+
const response = await inject(app, {
2309+
method: 'POST',
2310+
url: 'http://example.com:8080/hello',
2311+
payload: formData
2312+
})
2313+
2314+
t.assert.equal(response.statusCode, 200)
2315+
t.assert.equal(response.payload, 'some data')
2316+
})

0 commit comments

Comments
 (0)