We want to send compressed body by gzip HTTP request in chai-http in mocha.
let chai = require('chai');
let chaiHttp = require('chai-http');
const zlib = require('zlib');
chai.use(chaiHttp);
object = {
"content": {
"data": {
"key": "testval"
}
}
};
const objStr = JSON.stringify(object);
const objBuf = Buffer.from(objStr, "utf-8");
const bodyContent = zlib.gzipSync(objBuf);
const bodyLen = Buffer.byteLength(bodyContent, 'utf-8');
chai.request("http://serverurl")
.post('/path')
.set('Content-Type', 'application/json')
.set('Content-Encoding', 'gzip')
.set('Content-Length', bodyLen)
.set('Accept-Encoding', 'gzip')
.send(bodyContent)
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(200);
done();
});
However, we met the error Error: incorrect header check at Zlib.zlibOnError on the server-side. Is there anything am I missing?
Looks like it's superagent, it's breaking the encoding, because it stringifies everything that is not a string and has
application/json, so instead of a Buffer:it's sending a JSON:
breaking the encoding, causing the error.
It happens here:
Adding
!Buffer.isBuffer(data)or something there might work.Until then, try a workaround, for example with
http: