Return error when no file is uploaded

This commit is contained in:
Loay 2016-10-25 10:03:05 -04:00
parent a1c7aa2992
commit f944fa2fd7
3 changed files with 43 additions and 4 deletions

View File

@ -41,7 +41,7 @@ exports.upload = function(provider, req, res, options, cb) {
form.handlePart = function(part) { form.handlePart = function(part) {
var self = this; var self = this;
if (part.filename === undefined) { if (part.filename === undefined || part.filename === '') {
var value = ''; var value = '';
var decoder = new StringDecoder(this.encoding); var decoder = new StringDecoder(this.encoding);
@ -187,11 +187,20 @@ exports.upload = function(provider, req, res, options, cb) {
}; };
form.parse(req, function(err, _fields, _files) { form.parse(req, function(err, _fields, _files) {
cb = cb || function() {};
if (err) { if (err) {
console.error(err); console.error(err);
return cb(err);
} }
// eslint-disable-next-line no-unused-expressions
cb && cb(err, {files: files, fields: fields}); if (Object.keys(_files).length === 0) {
err = new Error('No file content uploaded');
err.statusCode = 400; // DO NOT MODIFY res.status directly!
return cb(err);
}
cb(null, {files: files, fields: fields});
}); });
}; };

View File

@ -21,7 +21,8 @@
"loopback": "^3.0.0", "loopback": "^3.0.0",
"mkdirp": "^0.5.0", "mkdirp": "^0.5.0",
"mocha": "^2.1.0", "mocha": "^2.1.0",
"supertest": "^0.15.0" "supertest": "^0.15.0",
"semver": "^5.3.0"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -7,6 +7,7 @@
var request = require('supertest'); var request = require('supertest');
var loopback = require('loopback'); var loopback = require('loopback');
var assert = require('assert'); var assert = require('assert');
var semver = require('semver');
var app = loopback(); var app = loopback();
var path = require('path'); var path = require('path');
@ -213,6 +214,34 @@ describe('storage service', function() {
}); });
}); });
it('returns error when no file is provided to upload', function(done) {
if (semver.gt(process.versions.node, '4.0.0')) {
request('http://localhost:' + app.get('port'))
.post('/imageContainers/album1/upload')
.set('Accept', 'application/json')
.set('Connection', 'keep-alive')
.expect('Content-Type', /json/)
.expect(400, function(err, res) {
var indexOfMsg =
res.body.error.message.toLowerCase().indexOf('no file');
assert.notEqual(indexOfMsg, -1,
'Error message does not contain \"no file\"');
done(err);
});
} else {
request('http://localhost:' + app.get('port'))
.post('/imageContainers/album1/upload')
.set('Accept', 'application/json')
.set('Connection', 'keep-alive')
.expect('Content-Type', /json/)
.expect(500, function(err, res) {
assert.equal(res.body.error.message,
'bad content-type header, no content-type');
done(err);
});
}
});
it('should get file by name', function(done) { it('should get file by name', function(done) {
request('http://localhost:' + app.get('port')) request('http://localhost:' + app.get('port'))
.get('/containers/album1/files/test.jpg') .get('/containers/album1/files/test.jpg')