Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing if download is successful with supertest

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?

like image 629
Martin Hallén Avatar asked Jul 17 '14 12:07

Martin Hallén


People also ask

What is SuperTest in API testing?

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.

What is SuperTest and 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.


1 Answers

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"
like image 123
bolav Avatar answered Sep 27 '22 18:09

bolav