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?
SuperTest is a Node. js library that helps developers test APIs. It extends another library called superagent, a JavaScript HTTP client for Node. js and the browser. Developers can use SuperTest as a standalone library or with JavaScript testing frameworks like Mocha or Jest.
Jest provides you with multiple layers on top of Jasmine. What is SuperTest? *A library for testing node. js HTTP servers *. It is a super-agent driven library for testing node.
Either the problem has been fixed in the libraries, or there is a bug in some other part of your code. Your example runs fine, and gives
when trying to download file
File found!
✓ should respond with "200 OK"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With