From 6d4d62e79c302151907ec6daf847259ca3bc250e Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Thu, 11 May 2023 11:12:23 +0000 Subject: [PATCH] test: Add test for buffer support --- test/flaska.out.test.mjs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/flaska.out.test.mjs b/test/flaska.out.test.mjs index a6c1025..d793b04 100644 --- a/test/flaska.out.test.mjs +++ b/test/flaska.out.test.mjs @@ -91,6 +91,38 @@ t.describe('#requestEnd()', function() { flaska.requestEnd(null, ctx) }) + t.test('call res and end correctly when dealing with buffers', function(cb) { + const assertStatus = 202 + // Calculated manually just in case + const assertBodyLength = 11 + const assertBody = Buffer.from('hello world') + let onFinish = cb.finish(function(body) { + assert.strictEqual(ctx.status, assertStatus) + assert.strictEqual(ctx.body, assertBody) + assert.strictEqual(ctx.headers['Content-Type'], 'application/octet-stream') + assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength) + + if (method === 'GET') { + assert.ok(body) + assert.strictEqual(body, assertBody) + } else { + assert.notOk(body) + } + + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + }) + const ctx = createCtx({ + method: method, + status: assertStatus, + }, onFinish) + ctx.body = assertBody + + let flaska = new Flaska({}, fakerHttp, fakeStream) + flaska.requestEnd(null, ctx) + }) + t.test('call res and end correctly when dealing with null and no status override', function(cb) { // Calculated manually just in case const assertBodyLength = 0