diff --git a/.taprc b/.taprc deleted file mode 100644 index eb6eb3e..0000000 --- a/.taprc +++ /dev/null @@ -1,2 +0,0 @@ -files: - - test/**/*.test.js diff --git a/package.json b/package.json index 2ac8180..540f7c2 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "codecov": "codecov", "lint": "standard | snazzy", "test": "npm run test:unit && npm run test:typescript", - "test:unit": "tap", + "test:unit": "c8 --100 node --test", "test:typescript": "tsd" }, "precommit": [ @@ -37,7 +37,7 @@ "qs": "^6.12.0", "snazzy": "^9.0.0", "standard": "^17.1.0", - "tap": "^18.7.1", + "c8": "^10.1.2", "tsd": "^0.31.0" }, "dependencies": { diff --git a/test/integration.test.js b/test/integration.test.js index 8997241..4d8a371 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -1,114 +1,114 @@ 'use strict' -const tap = require('tap') -const test = tap.test +const test = require('node:test') const Fastify = require('fastify') const plugin = require('../') const qs = require('qs') const formAutoContent = require('form-auto-content') -test('succes route succeeds', (t) => { - t.plan(3) +test('success route succeeds', async (t) => { const fastify = Fastify() - fastify.register(plugin) + await fastify.register(plugin) fastify.post('/test1', (req, res) => { res.send(Object.assign({}, req.body, { message: 'done' })) }) - fastify.listen({ port: 0 }, (err) => { - if (err) tap.error(err) - fastify.server.unref() + await fastify.listen({ port: 0 }) + fastify.server.unref() - fastify.inject({ path: '/test1', method: 'POST', ...formAutoContent({ foo: 'foo' }) }, (err, response) => { - t.error(err) - t.equal(response.statusCode, 200) - t.same(JSON.parse(response.body), { foo: 'foo', message: 'done' }) - }) + const response = await fastify.inject({ + path: '/test1', + method: 'POST', + ...formAutoContent({ foo: 'foo' }) }) + t.assert.ifError(response.error) + t.assert.strictEqual(response.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(response.body), { foo: 'foo', message: 'done' }) }) -test('cannot exceed body limit', (t) => { - t.plan(3) +test('cannot exceed body limit', async (t) => { const fastify = Fastify() - fastify.register(plugin, { bodyLimit: 10 }) + await fastify.register(plugin, { bodyLimit: 10 }) fastify.post('/limited', (req, res) => { res.send(Object.assign({}, req.body, { message: 'done' })) }) - fastify.listen({ port: 0 }, (err) => { - if (err) tap.error(err) - fastify.server.unref() + await fastify.listen({ port: 0 }) + fastify.server.unref() - const payload = require('node:crypto').randomBytes(128).toString('hex') - fastify.inject({ path: '/limited', method: 'POST', ...formAutoContent({ foo: payload }) }, (err, response) => { - t.error(err) - t.equal(response.statusCode, 413) - t.equal(JSON.parse(response.body).message, 'Request body is too large') - }) + const payload = require('node:crypto').randomBytes(128).toString('hex') + const response = await fastify.inject({ + path: '/limited', + method: 'POST', + ...formAutoContent({ foo: payload }) }) + t.assert.ifError(response.error) + t.assert.strictEqual(response.statusCode, 413) + t.assert.strictEqual(JSON.parse(response.body).message, 'Request body is too large') }) -test('cannot exceed body limit when Content-Length is not available', (t) => { - t.plan(3) +test('cannot exceed body limit when Content-Length is not available', async (t) => { const fastify = Fastify() fastify.addHook('onSend', (request, reply, payload, next) => { reply.send = function mockSend (arg) { - t.fail('reply.send() was called multiple times') + t.assert.fail('reply.send() was called multiple times') } setTimeout(next, 1) }) - fastify.register(plugin, { bodyLimit: 10 }) + await fastify.register(plugin, { bodyLimit: 10 }) fastify.post('/limited', (req, res) => { res.send(Object.assign({}, req.body, { message: 'done' })) }) - fastify.listen({ port: 0 }, (err) => { - if (err) tap.error(err) - fastify.server.unref() + await fastify.listen({ port: 0 }) + fastify.server.unref() - let sent = false - const payload = require('node:stream').Readable({ - read: function () { - this.push(sent ? null : Buffer.alloc(70000, 'a')) - sent = true - } - }) - fastify.inject({ path: '/limited', method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: payload }, (err, response) => { - t.error(err) - t.equal(response.statusCode, 413) - t.equal(JSON.parse(response.body).message, 'Request body is too large') - }) + let sent = false + const payload = require('node:stream').Readable({ + read: function () { + this.push(sent ? null : Buffer.alloc(70000, 'a')) + sent = true + } }) + + const response = await fastify.inject({ + path: '/limited', + method: 'POST', + headers: { 'content-type': 'application/x-www-form-urlencoded' }, + body: payload + }) + t.assert.ifError(response.error) + t.assert.strictEqual(response.statusCode, 413) + t.assert.strictEqual(JSON.parse(response.body).message, 'Request body is too large') }) -test('cannot exceed body limit set on Fastify instance', (t) => { - t.plan(3) +test('cannot exceed body limit set on Fastify instance', async (t) => { const fastify = Fastify({ bodyLimit: 10 }) - fastify.register(plugin) + await fastify.register(plugin) fastify.post('/limited', (req, res) => { res.send(Object.assign({}, req.body, { message: 'done' })) }) - fastify.listen({ port: 0 }, (err) => { - if (err) tap.error(err) - fastify.server.unref() + await fastify.listen({ port: 0 }) + fastify.server.unref() - const payload = require('node:crypto').randomBytes(128).toString('hex') - fastify.inject({ path: '/limited', method: 'POST', ...formAutoContent({ foo: payload }) }, (err, response) => { - t.error(err) - t.equal(response.statusCode, 413) - t.equal(JSON.parse(response.body).message, 'Request body is too large') - }) + const payload = require('node:crypto').randomBytes(128).toString('hex') + const response = await fastify.inject({ + path: '/limited', + method: 'POST', + ...formAutoContent({ foo: payload }) }) + t.assert.ifError(response.error) + t.assert.strictEqual(response.statusCode, 413) + t.assert.strictEqual(JSON.parse(response.body).message, 'Request body is too large') }) -test('plugin bodyLimit should overwrite Fastify instance bodyLimit', (t) => { - t.plan(3) +test('plugin bodyLimit should overwrite Fastify instance bodyLimit', async (t) => { const fastify = Fastify({ bodyLimit: 100000 }) fastify.register(plugin, { bodyLimit: 10 }) @@ -116,32 +116,34 @@ test('plugin bodyLimit should overwrite Fastify instance bodyLimit', (t) => { res.send(Object.assign({}, req.body, { message: 'done' })) }) - fastify.listen({ port: 0 }, (err) => { - if (err) tap.error(err) - fastify.server.unref() + await fastify.listen({ port: 0 }) + fastify.server.unref() - const payload = require('node:crypto').randomBytes(128).toString('hex') - fastify.inject({ path: '/limited', method: 'POST', ...formAutoContent({ foo: payload }) }, (err, response) => { - t.error(err) - t.equal(response.statusCode, 413) - t.equal(JSON.parse(response.body).message, 'Request body is too large') - }) + const payload = require('node:crypto').randomBytes(128).toString('hex') + const response = await fastify.inject({ + path: '/limited', + method: 'POST', + ...formAutoContent({ foo: payload }) }) + t.assert.ifError(response.error) + t.assert.strictEqual(response.statusCode, 413) + t.assert.strictEqual(JSON.parse(response.body).message, 'Request body is too large') }) -test('plugin should throw if opts.parser is not a function', (t) => { - t.plan(2) +test('plugin should throw if opts.parser is not a function', async (t) => { const fastify = Fastify() - fastify.register(plugin, { parser: 'invalid' }) - fastify.listen({ port: 0 }, (err) => { - t.ok(err) - t.match(err.message, /parser must be a function/) + try { + await fastify.register(plugin, { parser: 'invalid' }) + await fastify.listen({ port: 0 }) + } catch (err) { + t.assert.ok(err) + t.assert.match(err.message, /parser must be a function/) + } finally { fastify.server.unref() - }) + } }) -test('plugin should not parse nested objects by default', (t) => { - t.plan(3) +test('plugin should not parse nested objects by default', async (t) => { const fastify = Fastify() fastify.register(plugin) @@ -149,20 +151,20 @@ test('plugin should not parse nested objects by default', (t) => { res.send(Object.assign({}, req.body, { message: 'done' })) }) - fastify.listen({ port: 0 }, (err) => { - if (err) tap.error(err) - fastify.server.unref() + await fastify.listen({ port: 0 }) + fastify.server.unref() - fastify.inject({ path: '/test1', method: 'POST', ...formAutoContent({ 'foo[one]': 'foo', 'foo[two]': 'bar' }) }, (err, response) => { - t.error(err) - t.equal(response.statusCode, 200) - t.same(JSON.parse(response.body), { 'foo[one]': 'foo', 'foo[two]': 'bar', message: 'done' }) - }) + const response = await fastify.inject({ + path: '/test1', + method: 'POST', + ...formAutoContent({ 'foo[one]': 'foo', 'foo[two]': 'bar' }) }) + t.assert.ifError(response.error) + t.assert.strictEqual(response.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(response.body), { 'foo[one]': 'foo', 'foo[two]': 'bar', message: 'done' }) }) -test('plugin should allow providing custom parser as option', (t) => { - t.plan(3) +test('plugin should allow providing custom parser as option', async (t) => { const fastify = Fastify() // this makes sure existing users for <= 4 have upgrade path @@ -171,14 +173,15 @@ test('plugin should allow providing custom parser as option', (t) => { res.send(Object.assign({}, req.body, { message: 'done' })) }) - fastify.listen({ port: 0 }, (err) => { - if (err) tap.error(err) - fastify.server.unref() + await fastify.listen({ port: 0 }) + fastify.server.unref() - fastify.inject({ path: '/test1', method: 'POST', ...formAutoContent({ 'foo[one]': 'foo', 'foo[two]': 'bar' }) }, (err, response) => { - t.error(err) - t.equal(response.statusCode, 200) - t.same(JSON.parse(response.body), { foo: { one: 'foo', two: 'bar' }, message: 'done' }) - }) + const response = await fastify.inject({ + path: '/test1', + method: 'POST', + ...formAutoContent({ 'foo[one]': 'foo', 'foo[two]': 'bar' }) }) + t.assert.ifError(response.error) + t.assert.strictEqual(response.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(response.body), { foo: { one: 'foo', two: 'bar' }, message: 'done' }) })