20

I am trying to write a test that checks whether an API route outputs a ZIP file with the correct contents.

I am using mocha and supertest for testing, and I would like to actually read the output stream/buffer, read the zip file contents and see if the contents are correct.

Any ideas how should I do it? When I try to read res.body, it's just an empty object.

  request(app)
    .get( "/api/v1/orders/download?id[]=1&id=2" )
    .set( "Authorization", authData )
    .expect( 200 )
    .expect( 'Content-Type', /application\/zip/ )
    .end( function (err, res) {
      if (err) return done( err );

      console.log( 'body:', res.body )

      // Write the temp HTML file to filesystem using utf-8 encoding
      var zip = new AdmZip( res.body );
      var zipEntries = zip.getEntries();

      console.log( 'zipentries:', zipEntries );

      zipEntries.forEach(function(zipEntry) {
        console.log(zipEntry.toString()); // outputs zip entries information
      });

      done();
    });
kba
  • 957
  • 8
  • 20
ragulka
  • 4,152
  • 7
  • 41
  • 71

4 Answers4

34

Expanding on @Beau's answer, the following can be used to get any binary response content as a Buffer which you can examine further in request.end():

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

// example mocha test
it('my test', function(done) {
    request(app)
        .get('/path/to/image.png')
        .expect(200)
        .expect('Content-Type', 'image.png')
        .buffer()
        .parse(binaryParser)
        .end(function(err, res) {
            if (err) return done(err);

            // binary response data is in res.body as a buffer
            assert.ok(Buffer.isBuffer(res.body));
            console.log("res=", res.body);

            done();
        });
});
rcoup
  • 4,299
  • 2
  • 26
  • 33
  • 1
    This works great, although I had to add `.buffer()` to the request. – Nate Feb 24 '14 at 21:20
  • With @Nate, from the [docs](http://visionmedia.github.io/superagent/#parsing-response-bodies), "If response buffering is not enabled (.buffer(false)) then the response event will be emitted without waiting for the body parser to finish, so response.body won't be available". – ZachB Jul 20 '16 at 05:13
  • @ZachB so `.buffer().parse(binaryParser)`? – rcoup Jul 20 '16 at 09:19
  • 1
    @rcoup yes, say the docs, but it also seems to work fine without in a quick test. Longer responses might require it...? – ZachB Jul 20 '16 at 18:09
2

I think you'll want to create your own parser for application/zip and use that to get at the actual response data; the JSON parser is here, for example. Once you've got that you can use it by passing it to request.parse; so your test would become:

request(app)
  .get( "/api/v1/orders/download?id[]=1&id=2" )
  .set( "Authorization", authData )
  .expect( 200 )
  .expect( 'Content-Type', /application\/zip/ )
  .parse( function (res, fn) {
    res.data = '';
    res.on( 'data', function (chunk) { res.data += chunk; } );
    res.on( 'end', function () {
      try {
        fn( null, new AdmZip( res.data ) );
      } catch ( err ) {
        fn( err );
      }
    });
  })
  .end( function (err, res) {
    if (err) return done( err );

    console.log( 'body:', res.body )

    // Write the temp HTML file to filesystem using utf-8 encoding
    var zipEntries = res.body.getEntries();

    console.log( 'zipentries:', zipEntries );

    zipEntries.forEach(function(zipEntry) {
      console.log(zipEntry.toString()); // outputs zip entries information
    });

    done();
  });

To find the answer to this I mostly relied on inspecting the superagent test suite. :)

Beau
  • 10,321
  • 7
  • 39
  • 35
0

Existing answers didn't work for me. What I ended up doing was:

// parses response.body buffer into a data object
const parsePDF = response => {
  return new Promise((resolve, reject) => {
    // code that parses response.body as buffer
    // and calls resolve(data); when done
    // or reject(err); on error
  })
};

const binaryParser = require('superagent-binary-parser');

// test snippet
request(app)
    .get('/some/api/returning/pdf')
    .expect(200)
    .expect('content-type', 'application/pdf')
    .parse(binaryParser)
    .buffer()
    .then(parsePDF)
    .then((pdf) => {
      chai.expect(pdf.pages.length).to.be.equal(5);
    })
Edi
  • 591
  • 4
  • 16
0

I just had the same case where I needed to write a test to check the contents of a zip file. This is how I did it.

    it('test', async function () {
        let data = '';
        const res = await request(app)
            .get('/api/some-zip')
            .parse((res, callback) => {
                res.setEncoding('binary');
                res.on('data', function (chunk) {
                    data += chunk;
                });
                res.on('end', function () {
                    callback(null, Buffer.from(data, 'binary'));
                });
            })
        const zip = new AdmZip(res.body)
        const zipEntries = zip.getEntries();

        expect(zipEntries[0].name).to.equal('zipfile1.pdf');
        expect(zipEntries[1].name).to.equal('zipfile2.pdf');
    });

I used async/await syntax

David Storm
  • 180
  • 2
  • 10