/**
- * Parse rawBody from ctx as contentType into parsedBody.
- * @param {string} contentType
- * @param {object} ctx
- */
- parseBody(contentType, ctx) {
+ * Parse rawBody as contentType into ctx.parsedBody.
+ * @param {string} contentType
+ * @param {object} ctx
+ * @param {string|buffer}
+ */
+ parseBody(contentType, ctx, rawBody) {
const _scope = _fileScope('parseBody');
+ if (!rawBody) {
+ // 1.2.4 and earlier expected rawBody on context
+ rawBody = ctx.rawBody;
+ }
+
switch (contentType) {
case Enum.ContentType.ApplicationForm:
- ctx.parsedBody = this.querystring.parse(ctx.rawBody);
+ ctx.parsedBody = this.querystring.parse(rawBody);
break;
case Enum.ContentType.ApplicationJson:
try {
- ctx.parsedBody = JSON.parse(ctx.rawBody);
+ ctx.parsedBody = JSON.parse(rawBody);
} catch (e) {
this.logger.debug(_scope, 'JSON parse failed', { requestId: ctx.requestId, error: e });
throw new ResponseError(Enum.ErrorResponse.BadRequest, e.message);
* @param {http.ClientRequest} req
* @param {http.ServerResponse} res
* @param {object} ctx
- */
- async ingestBody(req, res, ctx) {
- ctx.rawBody = await this.bodyData(req);
- const contentType = Dingus.getRequestContentType(req);
- this.parseBody(contentType, ctx);
+ * @param {object}
+ * @param {Boolean} .parseEmptyBody
+ * @param {Boolean} .persistRawBody
+ */
+ async ingestBody(req, res, ctx, { parseEmptyBody = true, persistRawBody = false, maximumBodySize } = {}) {
+ const rawBody = await this.bodyData(req, maximumBodySize);
+ if (persistRawBody) {
+ ctx.rawBody = rawBody;
+ }
+ if (rawBody || parseEmptyBody) {
+ const contentType = Dingus.getRequestContentType(req);
+ this.parseBody(contentType, ctx, rawBody);
+ }
}
}); // bodyData
describe('ingestBody', function () {
- it('covers', async function () {
+ it('ingests json', async function () {
const req = {};
const res = {};
const ctx = {};
sinon.stub(Dingus, 'getRequestContentType').returns(Enum.ContentType.ApplicationJson);
await dingus.ingestBody(req, res, ctx);
assert.deepStrictEqual(ctx.parsedBody, { foo: 'bar' });
+ assert.deepStrictEqual(ctx.rawBody, undefined);
+ });
+ it('persists rawBody', async function () {
+ const req = {};
+ const res = {};
+ const ctx = {};
+ const body = '{"foo":"bar"}';
+ sinon.stub(dingus, 'bodyData').resolves(body);
+ sinon.stub(Dingus, 'getRequestContentType').returns(Enum.ContentType.ApplicationJson);
+ await dingus.ingestBody(req, res, ctx, { persistRawBody: true });
+ assert.deepStrictEqual(ctx.parsedBody, { foo: 'bar' });
+ assert.deepStrictEqual(ctx.rawBody, body);
+ });
+ it('skips parsing empty body', async function () {
+ const req = {};
+ const res = {};
+ const ctx = {};
+ const body = '';
+ sinon.stub(dingus, 'bodyData').resolves(body);
+ sinon.stub(Dingus, 'getRequestContentType').returns(Enum.ContentType.ApplicationJson);
+ sinon.spy(dingus, 'parseBody');
+ await dingus.ingestBody(req, res, ctx, { parseEmptyBody: false });
+ assert.deepStrictEqual(ctx.parsedBody, undefined);
+ assert(dingus.parseBody.notCalled);
});
}); // ingestBody