Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing file download, res.files returns empty object #126

Closed
ajmas opened this issue Nov 10, 2016 · 3 comments
Closed

Testing file download, res.files returns empty object #126

ajmas opened this issue Nov 10, 2016 · 3 comments

Comments

@ajmas
Copy link

ajmas commented Nov 10, 2016

Originally asked here: http://stackoverflow.com/questions/40517088/using-mocha-chai-to-ensure-rest-api-serves-up-a-file/

I am wanting to download the response file in a test, but the req.files value is an empty object. Displays as {} when displayed with console. The superagent docs suggest that res.files should provide a file.

Note the endpoint works when testing with 'wget'.

The code I am using for my test

it('Should get a file', (done) => {
    chai.request(url)
        .get('/api/exercise/1?token=' + token)
        .end(function(err, res) {
            if (err) { done(err); }
            res.should.have.status(200);

            // Check the headers for type and size
            res.should.have.header('content-type');
            res.header['content-type'].should.be.equal('application/pdf');
            res.should.have.header('content-length');
            const size = fs.statSync(filepath).size.toString();
            res.header['content-length'].should.be.equal(size);

            // TODO get access to saved file and do tests on it
            //      tried 'res.files' but it returns {}                
        });
});

Is there something I am missing?

@ajmas ajmas changed the title Testing file download Testing file download, res.files returns empty object Nov 10, 2016
@meeber
Copy link
Contributor

meeber commented Nov 10, 2016

Not sure about the file property in this case since that's for multipart.

Dunno if there's a better way but looks like this is relevant: http://stackoverflow.com/questions/13573315/read-response-output-buffer-stream-with-supertest-superagent-on-node-js-server

Using the strategy in that article, I was able to test the md5 of some random pdf on the internets:

"use strict";

const chai = require("chai");
const chaiHttp = require("chai-http");
const md5 = require("md5");

const expect = chai.expect;

chai.use(chaiHttp);

const binaryParser = function (res, cb) {
  res.setEncoding("binary");
  res.data = "";
  res.on("data", function (chunk) {
    res.data += chunk;
  });
  res.on("end", function () {
    cb(null, new Buffer(res.data, "binary"));
  });
};

it("merp", function (done) {
  chai
    .request("www.cbu.edu.zm/downloads/")
    .get("pdf-sample.pdf")
    .buffer()
    .parse(binaryParser)
    .end(function (err, res) {
      if (err) done(err);

      expect(res).to.have.status(200);
      expect(md5(res.body)).to.equal("fa7d7e650b2cec68f302b31ba28235d8");

      done();
    });
});

@ajmas
Copy link
Author

ajmas commented Nov 11, 2016

Thanks. I have updated the question on stackoverflow, based on your solution.

@spoonscen
Copy link

meeber you saved my life.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants