From f0454d11c2e8e7560dccc881b324c250a870d3e6 Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Sun, 10 Oct 2021 20:08:43 +0000 Subject: [PATCH] onerror: Goes back to normal end request routine after onError finishes --- flaska.mjs | 1 - test/flaska.out.test.mjs | 17 ++++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/flaska.mjs b/flaska.mjs index eafbbc0..2c68663 100644 --- a/flaska.mjs +++ b/flaska.mjs @@ -458,7 +458,6 @@ export class Flaska { requestEnd(err, ctx) { if (err) { this._onerror(err, ctx) - return ctx.res.end() } if (ctx.req.complete) { return diff --git a/test/flaska.out.test.mjs b/test/flaska.out.test.mjs index 96f25c2..9bc9475 100644 --- a/test/flaska.out.test.mjs +++ b/test/flaska.out.test.mjs @@ -9,10 +9,21 @@ t.describe('#requestEnd()', function() { t.test('calls onerror correctly on error', function(cb) { const assertError = new Error('test') const assertBody = { a: 1 } - let onFinish = function() { + const assertStatus = 501 + // Calculated manually just in case + const assertBodyLength = 7 + let onFinish = function(body) { try { - assert.strictEqual(ctx.status, 501) + assert.strictEqual(ctx.status, assertStatus) assert.strictEqual(ctx.body, assertBody) + assert.strictEqual(ctx.res.statusCode, assertStatus) + assert.strictEqual(ctx.res.setHeader.callCount, 2) + assert.strictEqual(ctx.res.setHeader.firstCall[0], 'Content-Type') + assert.strictEqual(ctx.res.setHeader.firstCall[1], 'application/json; charset=utf-8') + assert.strictEqual(ctx.res.setHeader.secondCall[0], 'Content-Length') + assert.strictEqual(ctx.res.setHeader.secondCall[1], assertBodyLength) + assert.ok(body) + assert.strictEqual(body, '{"a":1}') cb() } catch (err) { cb(err) } } @@ -22,7 +33,7 @@ t.describe('#requestEnd()', function() { flaska.onerror(function(err, inctx) { assert.strictEqual(err, assertError) assert.strictEqual(inctx, ctx) - inctx.status = 501 + inctx.status = assertStatus inctx.body = assertBody })