Reputation: 28786
I'm adding integration tests to a legacy code-base prior to refactoring. For this case, uploading a file.
The Test:
it('uploads a photo at the specified index', done => {
chai.request(server.instance)
.post('/profile/photo/0')
.set('Access-Token', `${token}`)
.set('API-Key', testConfig.apiKey)
.field({contentId: 'foobar'})
.attach('file', fs.readFileSync(__dirname + '/logo.png'), 'file')
.end((err, res) => {
console.log(JSON.stringify(res.body))
res.should.have.status(200)
done()
})
})
The endpoint being tested works ok in production. But to get the test to pass, I have to comment out the following lines in make-middleware.js
of the multer
module:
if (!includeFile) {
// appender.removePlaceholder(placeholder)
// return fileStream.resume()
}
Being inexperienced with node, I must've missed some configuration or something. How can I make my test pass (without modifying an external module's code)?
Upvotes: 3
Views: 643
Reputation: 5564
multer
uses busboy
to get its job done (get / stream files). The lines you've commented just stops the stream :
fileStream.resume()
in this code is equivalent of stream.resume()
in busboy
code, so it just discards the stream :
(from busboy
documentation) :
you should always handle the
stream
no matter if you care about the file contents or not (e.g. you can simply just dostream.resume();
if you want to discard the contents)
But Multer is not supposed to behave that way !
It only behave that way if you pass to Multer a custom fileFilter
with a callback that sets includeFile
to false.
Otherwise, if you don't have fileFilter
option, Multer uses the following default fileFilter
(that does nothing) :
function allowAll (req, file, cb) {
cb(null, true)
}
And as you can see, the second parameter of the callback is true
, which is includeFile
.
So, you can check your custom fileFilter
if you have one and if you don't, it could be an unexpected side effect and I whish you good luck with it !
Hope it helps,
Best regards
Upvotes: 1