diff --git a/.gitignore b/.gitignore index 38602882..7c1f9422 100644 --- a/.gitignore +++ b/.gitignore @@ -19,5 +19,6 @@ node_modules *.gz -# Coveralls +# Coverage +.nyc_output coverage diff --git a/test/disk-storage.js b/test/disk-storage.js index bc923ab5..43559bde 100644 --- a/test/disk-storage.js +++ b/test/disk-storage.js @@ -5,6 +5,7 @@ var deepEqual = require('deep-equal') var fs = require('fs') var path = require('path') +var os = require('os') var util = require('./_util') var multer = require('../') var temp = require('fs-temp') @@ -185,4 +186,60 @@ describe('Disk Storage', function () { done() }) }) + + it('should use default destination when none is provided', function (done) { + var storage = multer.diskStorage({}) + var upload = multer({ storage: storage }) + var parser = upload.single('file') + var form = new FormData() + + form.append('file', util.file('small0.dat')) + + util.submitForm(parser, form, (err, req) => { + assert.ifError(err) + + // Verify that the file was stored in the system's temporary directory + assert.strictEqual(path.dirname(req.file.path), os.tmpdir()) + + done() + }) + }) + + it('should handle error in getDestination', function (done) { + var storage = multer.diskStorage({ + destination: function (req, file, cb) { + cb(new Error('Test getDestination error')) + } + }) + var upload = multer({ storage: storage }) + var parser = upload.single('file') + var form = new FormData() + + form.append('file', util.file('small0.dat')) + + util.submitForm(parser, form, function (err, req) { + assert(err) + assert.strictEqual(err.message, 'Test getDestination error') + done() + }) + }) + + it('should handle error in getFilename', function (done) { + var storage = multer.diskStorage({ + filename: function (req, file, cb) { + cb(new Error('Test getFilename error')) + } + }) + var upload = multer({ storage: storage }) + var parser = upload.single('file') + var form = new FormData() + + form.append('file', util.file('small0.dat')) + + util.submitForm(parser, form, function (err, req) { + assert(err) + assert.strictEqual(err.message, 'Test getFilename error') + done() + }) + }) }) diff --git a/test/error-handling.js b/test/error-handling.js index c2b1f973..85b961f0 100644 --- a/test/error-handling.js +++ b/test/error-handling.js @@ -277,4 +277,20 @@ describe('Error Handling', function () { done() }) }) + + it('should throw TypeError if options is not an object', function () { + assert.throws(() => { + multer(null) + }, { + name: 'TypeError', + message: 'Expected object for argument options' + }) + + assert.throws(() => { + multer('invalid') + }, { + name: 'TypeError', + message: 'Expected object for argument options' + }) + }) })