I'm testing my API endpoints with supertest, and it works great, but i can't figure out how to test if a file download is successful.
In my routes file i have defined the endpoint to be:
app.get('/api/attachment/:id/file', attachment.getFile);
and the function getFile()
looks something like this:
exports.getFile = function(req, res, next) {
Attachment.getById(req.params.id, function(err, att) {
[...]
if (att) {
console.log('File found!');
return res.download(att.getPath(), att.name);
}
Then, in my test file, I try the following:
describe('when trying to download file', function() {
it('should respond with "200 OK"', function(done) {
request(url)
.get('/api/attachment/' + attachment._id + '/file');
.expect(200)
.end(function(err, res) {
if (err) {
return done(err);
}
return done();
});
});
});
I know for sure that the file is found, because it logs out File found!
. It also works fine if i try manually, but for some reason, mocha returns Error: expected 200 "OK", got 404 "Not Found"
.
I've experimented with different mime-types and supertest .set("Accept-Encoding": "*")
, but nothing works.
Anyone know how to do this?