@fastify/formbody
Advanced tools
Comparing version 8.0.0 to 8.0.1
{ | ||
"name": "@fastify/formbody", | ||
"version": "8.0.0", | ||
"version": "8.0.1", | ||
"description": "A module for Fastify to parse x-www-form-urlencoded bodies", | ||
@@ -11,3 +11,3 @@ "main": "formbody.js", | ||
"test": "npm run test:unit && npm run test:typescript", | ||
"test:unit": "tap", | ||
"test:unit": "c8 --100 node --test", | ||
"test:typescript": "tsd" | ||
@@ -36,3 +36,3 @@ }, | ||
"@types/node": "^22.0.0", | ||
"fastify": "^5.0.0-alpha.4", | ||
"fastify": "^5.0.0", | ||
"form-auto-content": "^3.2.1", | ||
@@ -42,3 +42,3 @@ "qs": "^6.12.0", | ||
"standard": "^17.1.0", | ||
"tap": "^18.7.1", | ||
"c8": "^10.1.2", | ||
"tsd": "^0.31.0" | ||
@@ -45,0 +45,0 @@ }, |
'use strict' | ||
const tap = require('tap') | ||
const test = tap.test | ||
const test = require('node:test') | ||
const Fastify = require('fastify') | ||
@@ -10,7 +9,6 @@ const plugin = require('../') | ||
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) => { | ||
@@ -20,19 +18,19 @@ 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) => { | ||
@@ -42,17 +40,17 @@ 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() | ||
@@ -62,3 +60,3 @@ | ||
reply.send = function mockSend (arg) { | ||
t.fail('reply.send() was called multiple times') | ||
t.assert.fail('reply.send() was called multiple times') | ||
} | ||
@@ -68,3 +66,3 @@ setTimeout(next, 1) | ||
fastify.register(plugin, { bodyLimit: 10 }) | ||
await fastify.register(plugin, { bodyLimit: 10 }) | ||
fastify.post('/limited', (req, res) => { | ||
@@ -74,26 +72,28 @@ 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) => { | ||
@@ -103,17 +103,17 @@ 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 }) | ||
@@ -126,28 +126,30 @@ | ||
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() | ||
@@ -160,16 +162,16 @@ | ||
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() | ||
@@ -183,12 +185,13 @@ | ||
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' }) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14554
212
12