I have a post endpoint which uses multer and takes an array of files:
router.post('/api/projects/:id/sessions', upload.array('files', 4), function(req, res, next) {
...
}
In test, I need to send an array of 4 files, I can't figure out how to do this with supertest. Here is my unworking test using a method I saw suggested elsewhere:
it('Add a session', function(done) {
api.post('/api/projects/' + projectId + '/sessions')
.set('X-Auth', tokenA)
.attach('files[0]', './test/files/d.zip')
.attach('files[1]', './test/files/m.csv')
.attach('files[2]', './test/files/o.15o.txt')
.attach('files[3]', './test/files/e.n')
.end(function(err, res) {
expect(res.status).to.equal(201)
done(err)
})
})
This returns a rather long error:
Error: Unexpected field
at makeError (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/lib/make-error.js:12:13)
at wrappedFileFilter (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/index.js:39:19)
at Busboy.<anonymous> (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/lib/make-middleware.js:112:7)
at emitMany (events.js:108:13)
at Busboy.emit (events.js:182:7)
at Busboy.emit (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/lib/main.js:31:35)
at PartStream.<anonymous> (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/lib/types/multipart.js:208:13)
at emitOne (events.js:77:13)
at PartStream.emit (events.js:169:7)
at HeaderParser.<anonymous> (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/Dicer.js:51:16)
at emitOne (events.js:77:13)
at HeaderParser.emit (events.js:169:7)
at HeaderParser._finish (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/HeaderParser.js:68:8)
at SBMH.<anonymous> (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/HeaderParser.js:40:12)
at emitMany (events.js:108:13)
at SBMH.emit (events.js:182:7)
at SBMH._sbmh_feed (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/node_modules/streamsearch/lib/sbmh.js:159:14)
at SBMH.push (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/node_modules/streamsearch/lib/sbmh.js:56:14)
at HeaderParser.push (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/HeaderParser.js:46:19)
at Dicer._oninfo (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/Dicer.js:197:25)
at SBMH.<anonymous> (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/Dicer.js:127:10)
at emitMany (events.js:108:13)
at SBMH.emit (events.js:182:7)
at SBMH._sbmh_feed (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/node_modules/streamsearch/lib/sbmh.js:188:10)
at SBMH.push (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/node_modules/streamsearch/lib/sbmh.js:56:14)
at Dicer._write (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/Dicer.js:109:17)
at doWrite (_stream_writable.js:292:12)
at writeOrBuffer (_stream_writable.js:278:5)
at Dicer.Writable.write (_stream_writable.js:207:11)
at Multipart.write (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/lib/types/multipart.js:285:24)
at Busboy._write (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/lib/main.js:74:16)
at doWrite (_stream_writable.js:292:12)
at writeOrBuffer (_stream_writable.js:278:5)
at Busboy.Writable.write (_stream_writable.js:207:11)
at IncomingMessage.ondata (_stream_readable.js:528:20)
at emitOne (events.js:77:13)
at IncomingMessage.emit (events.js:169:7)
at IncomingMessage.Readable.read (_stream_readable.js:360:10)
at flow (_stream_readable.js:743:26)
at resume_ (_stream_readable.js:723:3)
at doNTCallback2 (node.js:439:9)
at process._tickCallback (node.js:353:17)
Can anyone help me in writing a working test for this POST endpoint? Thanks.
So it turns out if you just attach
files to the same name, they'll be appended to an array and sent as such, so the test below worked fine:
it('Add a session', function(done) {
api.post('/api/projects/' + projectId + '/sessions')
.set('X-Auth', tokenA)
.attach('files', './test/files/d.zip')
.attach('files', './test/files/m.csv')
.attach('files', './test/files/o.15o.txt')
.attach('files', './test/files/e.n')
.end(function(err, res) {
expect(res.status).to.equal(201)
done(err)
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With