From 8db66f416abc0c2ac93a5440c9675cf55da8d537 Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Sun, 29 Sep 2024 05:02:39 +0000 Subject: [PATCH] Minor refactor of tests and eltro --- package.json | 9 +-- test/flaska.body.test.mjs | 0 test/flaska.formidable.test.mjs | 0 test/http.body.json.test.mjs | 128 ++++++++++++++++++++++++++++++++ test/http.test.mjs | 94 +---------------------- 5 files changed, 133 insertions(+), 98 deletions(-) delete mode 100644 test/flaska.body.test.mjs delete mode 100644 test/flaska.formidable.test.mjs create mode 100644 test/http.body.json.test.mjs diff --git a/package.json b/package.json index d3bc283..e5fbd56 100644 --- a/package.json +++ b/package.json @@ -5,17 +5,14 @@ "main": "flaska.mjs", "scripts": { "test": "eltro", - "test:watch": "npm-watch test" + "test:watch": "eltro -r dot -w test" }, "watch": { "test": { "patterns": [ - "test/*", - "flaska.mjs" + "./" ], - "extensions": "mjs", - "quiet": true, - "inherit": true + "extensions": "mjs" } }, "type": "module", diff --git a/test/flaska.body.test.mjs b/test/flaska.body.test.mjs deleted file mode 100644 index e69de29..0000000 diff --git a/test/flaska.formidable.test.mjs b/test/flaska.formidable.test.mjs deleted file mode 100644 index e69de29..0000000 diff --git a/test/http.body.json.test.mjs b/test/http.body.json.test.mjs new file mode 100644 index 0000000..a29d2b9 --- /dev/null +++ b/test/http.body.json.test.mjs @@ -0,0 +1,128 @@ +import { Eltro as t, assert, stub } from 'eltro' +import { setTimeout } from 'timers/promises' +import { Flaska, JsonHandler } from '../flaska.mjs' +import Client from './client.mjs' + +const port = 51024 +const log = { + fatal: stub(), + error: stub(), + warn: stub(), + info: stub(), + debug: stub(), + trace: stub(), + log: stub(), +} +const flaska = new Flaska({ log }) +const client = new Client(port) + +let reqBody = null + +flaska.after(function(ctx) { + if (ctx.aborted) return + ctx.log.info(ctx.status) +}) + +flaska.post('/json', JsonHandler(), function(ctx) { + ctx.body = { success: true } + reqBody = ctx.req.body +}) +flaska.post('/json/slow', JsonHandler(), async function(ctx) { + await setTimeout(300) + ctx.body = { success: true } + ctx.status = 201 + reqBody = ctx.req.body +}) + +function reset() { + log.fatal.reset() + log.error.reset() + log.warn.reset() + log.info.reset() + log.debug.reset() + log.trace.reset() + log.log.reset() +} + +t.before(function() { + return flaska.listenAsync(port) +}) + +t.describe('/json', function() { + t.beforeEach(function() { + log.info.reset() + }) + t.test('should return success and store body', async function() { + const assertBody = { a: '' } + for (let i = 0; i < 1010; i++) { + assertBody.a += 'aaaaaaaaaa' + } + reqBody = null + let body = await client.post('/json', assertBody) + assert.deepEqual(body, { success: true }) + assert.deepStrictEqual(reqBody, assertBody) + assert.strictEqual(log.info.callCount, 1) + assert.strictEqual(log.info.firstCall[0], 200) + }) + + t.test('should return and log correctly', async function() { + const assertBody = { a: '' } + for (let i = 0; i < 1010; i++) { + assertBody.a += 'aaaaaaaaaa' + } + reqBody = null + let body = await client.post('/json/slow', assertBody) + assert.deepEqual(body, { success: true }) + assert.deepStrictEqual(reqBody, assertBody) + assert.strictEqual(log.info.callCount, 1) + assert.strictEqual(log.info.firstCall[0], 201) + }) + + t.test('should fail if body is too big', async function() { + reset() + + const assertBody = { a: '' } + for (let i = 0; i < 10300; i++) { + assertBody.a += 'aaaaaaaaaa' + } + + let err = await assert.isRejected(client.post('/json', assertBody)) + + assert.strictEqual(err.body.status, 413) + assert.ok(log.error.called) + assert.match(log.error.firstCall[0].message, /10240/) + assert.strictEqual(log.error.firstCall[0].status, 413) + }) + + t.test('should fail if not a valid json', async function() { + reset() + + let err = await assert.isRejected(client.customRequest('POST', '/json', 'XXXXX')) + + assert.strictEqual(err.body.status, 400) + assert.match(err.body.message, /invalid json/i) + assert.match(err.body.message, /token[^X]+X/i) + assert.strictEqual(err.body.request, 'XXXXX') + assert.strictEqual(log.error.callCount, 1) + assert.match(log.error.firstCall[0].message, /invalid json/i) + assert.match(log.error.firstCall[0].message, /token[^X]+X/i) + }) + + t.test('should handle incomplete requests correctly', async function() { + reset() + + let req = await client.customRequest('POST', '/json', 'aaaa', { returnRequest: true }) + + req.write('part1') + + await setTimeout(20) + + req.destroy() + + await setTimeout(20) + + assert.strictEqual(log.error.callCount, 0) + assert.strictEqual(log.info.callCount, 0) + // assert.strictEqual(log.info.firstCall[0].message, 'aborted') + }) +}) diff --git a/test/http.test.mjs b/test/http.test.mjs index 2d3f52d..ec59e1c 100644 --- a/test/http.test.mjs +++ b/test/http.test.mjs @@ -4,7 +4,7 @@ import fs from 'fs/promises' import formidable from 'formidable' import { Eltro as t, assert, stub } from 'eltro' import { setTimeout } from 'timers/promises' -import { Flaska, JsonHandler, FormidableHandler, FileResponse } from '../flaska.mjs' +import { Flaska, FormidableHandler, FileResponse } from '../flaska.mjs' import Client from './client.mjs' const port = 51024 @@ -20,7 +20,6 @@ const log = { const flaska = new Flaska({ log }) const client = new Client(port) -let reqBody = null let file = null let uploaded = [] @@ -32,16 +31,6 @@ flaska.after(function(ctx) { flaska.get('/', function(ctx) { ctx.body = { status: true } }) -flaska.post('/json', JsonHandler(), function(ctx) { - ctx.body = { success: true } - reqBody = ctx.req.body -}) -flaska.post('/json/slow', JsonHandler(), async function(ctx) { - await setTimeout(300) - ctx.body = { success: true } - ctx.status = 201 - reqBody = ctx.req.body -}) flaska.get('/timeout', function(ctx) { return new Promise(function() {}) }) @@ -96,90 +85,11 @@ t.describe('/', function() { }) }) -t.describe('/json', function() { - t.beforeEach(function() { - log.info.reset() - }) - t.test('should return success and store body', async function() { - const assertBody = { a: '' } - for (let i = 0; i < 1010; i++) { - assertBody.a += 'aaaaaaaaaa' - } - reqBody = null - let body = await client.post('/json', assertBody) - assert.deepEqual(body, { success: true }) - assert.deepStrictEqual(reqBody, assertBody) - assert.strictEqual(log.info.callCount, 1) - assert.strictEqual(log.info.firstCall[0], 200) - }) - - t.test('should return and log correctly', async function() { - const assertBody = { a: '' } - for (let i = 0; i < 1010; i++) { - assertBody.a += 'aaaaaaaaaa' - } - reqBody = null - let body = await client.post('/json/slow', assertBody) - assert.deepEqual(body, { success: true }) - assert.deepStrictEqual(reqBody, assertBody) - assert.strictEqual(log.info.callCount, 1) - assert.strictEqual(log.info.firstCall[0], 201) - }) - - t.test('should fail if body is too big', async function() { - reset() - - const assertBody = { a: '' } - for (let i = 0; i < 10300; i++) { - assertBody.a += 'aaaaaaaaaa' - } - - let err = await assert.isRejected(client.post('/json', assertBody)) - - assert.strictEqual(err.body.status, 413) - assert.ok(log.error.called) - assert.match(log.error.firstCall[0].message, /10240/) - assert.strictEqual(log.error.firstCall[0].status, 413) - }) - - t.test('should fail if not a valid json', async function() { - reset() - - let err = await assert.isRejected(client.customRequest('POST', '/json', 'XXXXX')) - - assert.strictEqual(err.body.status, 400) - assert.match(err.body.message, /invalid json/i) - assert.match(err.body.message, /token[^X]+X/i) - assert.strictEqual(err.body.request, 'XXXXX') - assert.strictEqual(log.error.callCount, 1) - assert.match(log.error.firstCall[0].message, /invalid json/i) - assert.match(log.error.firstCall[0].message, /token[^X]+X/i) - }) - - t.test('should handle incomplete requests correctly', async function() { - reset() - - let req = await client.customRequest('POST', '/json', 'aaaa', { returnRequest: true }) - - req.write('part1') - - await setTimeout(20) - - req.destroy() - - await setTimeout(20) - - assert.strictEqual(log.error.callCount, 0) - assert.strictEqual(log.info.callCount, 0) - // assert.strictEqual(log.info.firstCall[0].message, 'aborted') - }) -}) - t.describe('/timeout', function() { t.test('server should handle timeout', async function() { reset() - let err = await assert.isRejected(client.customRequest('GET', '/timeout', JSON.stringify({}), { timeout: 20 })) + let err = await assert.isRejected(client.customRequest('GET', '/timeout', null, { timeout: 20 })) assert.match(err.message, /timed out/) assert.notOk(log.error.called)