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();
});
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()
: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:
To find the answer to this I mostly relied on inspecting the superagent test suite. :)
Existing answers didn't work for me. What I ended up doing was: