From e5e7225d8b7522b232c11f74b4c91304fd06b30b Mon Sep 17 00:00:00 2001 From: Richard Girges Date: Mon, 28 Aug 2017 12:13:42 -0700 Subject: [PATCH] adding tests for .mv() using Promise --- test/multipartUploads.spec.js | 71 +++++++++++++++++++++++++++++++++++ test/server.js | 17 +++++++++ 2 files changed, 88 insertions(+) diff --git a/test/multipartUploads.spec.js b/test/multipartUploads.spec.js index ae8296d..d76c6af 100644 --- a/test/multipartUploads.spec.js +++ b/test/multipartUploads.spec.js @@ -102,6 +102,77 @@ describe('Test Single File Upload', function() { }); }); +describe('Test Single File Upload w/ .mv() Promise', function() { + for (let i = 0; i < mockFiles.length; i++) { + let fileName = mockFiles[i]; + + it(`upload ${fileName} with POST w/ .mv() Promise`, function(done) { + let filePath = path.join(fileDir, fileName); + let uploadedFilePath = path.join(uploadDir, fileName); + + clearUploadsDir(); + + request(app) + .post('/upload/single/promise') + .attach('testFile', filePath) + .expect(200) + .end(function(err, res) { + if (err) { + return done(err); + } + + fs.stat(uploadedFilePath, done); + }); + }); + + it(`upload ${fileName} with PUT w/ .mv() Promise`, function(done) { + let filePath = path.join(fileDir, fileName); + let uploadedFilePath = path.join(uploadDir, fileName); + + clearUploadsDir(); + + request(app) + .post('/upload/single/promise') + .attach('testFile', filePath) + .expect(200) + .end(function(err, res) { + if (err) { + return done(err); + } + + fs.stat(uploadedFilePath, done); + }); + }); + } + + it('fail when no files were attached', function(done) { + request(app) + .post('/upload/single') + .expect(400) + .end(done); + }); + + it('fail when using GET', function(done) { + let filePath = path.join(fileDir, mockFiles[0]); + + request(app) + .get('/upload/single') + .attach('testFile', filePath) + .expect(400) + .end(done); + }); + + it('fail when using HEAD', function(done) { + let filePath = path.join(fileDir, mockFiles[0]); + + request(app) + .head('/upload/single') + .attach('testFile', filePath) + .expect(400) + .end(done); + }); +}); + describe('Test Multi-File Upload', function() { it('upload multiple files with POST', function(done) { let upload1 = path.join(fileDir, mockFiles[0]); diff --git a/test/server.js b/test/server.js index 4df141e..ea305c2 100644 --- a/test/server.js +++ b/test/server.js @@ -38,6 +38,23 @@ const setup = function(fileUploadOptions) { }); }); + app.all('/upload/single/promise', function(req, res) { + if (!req.files) { + return res.status(400).send('No files were uploaded.'); + } + + let testFile = req.files.testFile; + let uploadPath = path.join(uploadDir, testFile.name); + + testFile.mv(uploadPath) + .then(() => { + res.send('File uploaded to ' + uploadPath); + }) + .catch((err) => { + res.status(500).send(err); + }); + }); + app.all('/upload/single/withfields', function(req, res) { if (!req.files) { return res.status(400).send('No files were uploaded.');