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 t
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. :)