* Return all body data from a request.
* @param {http.ClientRequest} req
* @param {Number=} maximumBodySize
+ * @param {Boolean=} toString
*/
- async bodyData(req, maximumBodySize) {
+ async bodyData(req, maximumBodySize, toString = true) {
const _scope = _fileScope('bodyData');
return new Promise((resolve, reject) => {
const body = [];
reject(new ResponseError(Enum.ErrorResponse.RequestEntityTooLarge));
}
});
- req.on('end', () => resolve(Buffer.concat(body).toString()));
+ req.on('end', () => {
+ const bodyBuffer = Buffer.concat(body);
+ resolve(toString ? bodyBuffer.toString() : bodyBuffer);
+ });
req.on('error', (e) => {
this.logger.error(_scope, 'failed', { error: e });
reject(e);
assert.strictEqual(e.statusCode, 413);
}
});
+ it('provides buffer', async function () {
+ const p = dingus.bodyData(res, 0, false);
+ const expected = Buffer.from('bleat');
+ resEvents['data'](expected);
+ resEvents['end']();
+ const result = await p;
+ assert.deepStrictEqual(result, expected);
+ });
}); // bodyData
describe('ingestBody', function () {