I have an app built on Express.js and I'd like to test the file upload functionality. I'm trying to reproduce the object parsed to req.files (when using express.bodyParser middleware). How can I do this?
See Question&Answers more detail:osI have an app built on Express.js and I'd like to test the file upload functionality. I'm trying to reproduce the object parsed to req.files (when using express.bodyParser middleware). How can I do this?
See Question&Answers more detail:osHere is an example of how you would do it with supertest module.
var should = require('should'),
supertest = require('supertest');
var request = supertest('localhost:3000');
describe('upload', function() {
it('a file', function(done) {
request.post('/your/endpoint')
.field('extra_info', '{"in":"case you want to send json along with your file"}')
.attach('image', 'path/to/file.jpg')
.end(function(err, res) {
res.should.have.status(200); // 'success' status
done();
});
});
});