Skip to content

Commit

Permalink
adding tests for .mv() using Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
richardgirges committed Aug 28, 2017
1 parent 1d13af9 commit e5e7225
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
71 changes: 71 additions & 0 deletions test/multipartUploads.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
17 changes: 17 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down

0 comments on commit e5e7225

Please sign in to comment.