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
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();
});
});