Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactor #126

Merged
merged 3 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ jobs:
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
with:
license-check: true
lint: true
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
2 changes: 2 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
files:
- test/**/*.test.js
17 changes: 7 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
"name": "@fastify/formbody",
"version": "7.0.0",
"version": "7.0.1",
"description": "A module for Fastify to parse x-www-form-urlencoded bodies",
"main": "formbody.js",
"scripts": {
"codecov": "codecov",
"lint": "standard | snazzy",
"lint:ci": "standard",
"test": "standard && tap --100 \"test/**/*.test.js\" && tsd",
"test:ci": "tap --100 \"test/**/*.test.js\"",
"typescript": "tsd"
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "tap",
"test:typescript": "tsd"
},
"precommit": [
"lint",
Expand All @@ -32,20 +31,18 @@
"devDependencies": {
"@fastify/pre-commit": "^2.0.2",
"@types/node": "^18.0.0",
"codecov": "^3.7.2",
"fastify": "^4.0.0-rc.2",
"form-auto-content": "^2.2.0",
"qs": "^6.5.1",
"request": "^2.88.0",
"snazzy": "^9.0.0",
"standard": "^17.0.0",
"tap": "^16.0.0",
"tsd": "^0.22.0",
"typescript": "^4.0.2"
"tsd": "^0.22.0"
},
"dependencies": {
"fastify-plugin": "^4.0.0"
},
"types": "formbody.d.ts",
"types": "types/formbody.d.ts",
"publishConfig": {
"access": "public"
}
Expand Down
68 changes: 15 additions & 53 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
const tap = require('tap')
const test = tap.test
const Fastify = require('fastify')
const request = require('request')
const plugin = require('../')
const qs = require('qs')
const formAutoContent = require('form-auto-content')

test('succes route succeeds', (t) => {
t.plan(3)
Expand All @@ -20,15 +20,10 @@ test('succes route succeeds', (t) => {
if (err) tap.error(err)
fastify.server.unref()

const reqOpts = {
method: 'POST',
baseUrl: 'http://localhost:' + fastify.server.address().port
}
const req = request.defaults(reqOpts)
req({ uri: '/test1', form: { foo: 'foo' } }, (err, response, body) => {
fastify.inject({ path: '/test1', method: 'POST', ...formAutoContent({ foo: 'foo' }) }, (err, response) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), { foo: 'foo', message: 'done' })
t.same(JSON.parse(response.body), { foo: 'foo', message: 'done' })
})
})
})
Expand All @@ -46,16 +41,11 @@ test('cannot exceed body limit', (t) => {
if (err) tap.error(err)
fastify.server.unref()

const reqOpts = {
method: 'POST',
baseUrl: 'http://localhost:' + fastify.server.address().port
}
const req = request.defaults(reqOpts)
const payload = require('crypto').randomBytes(128).toString('hex')
req({ uri: '/limited', form: { foo: payload } }, (err, response, body) => {
fastify.inject({ path: '/limited', method: 'POST', ...formAutoContent({ foo: payload }) }, (err, response) => {
t.error(err)
t.equal(response.statusCode, 413)
t.equal(JSON.parse(body).message, 'Request body is too large')
t.equal(JSON.parse(response.body).message, 'Request body is too large')
})
})
})
Expand All @@ -80,25 +70,17 @@ test('cannot exceed body limit when Content-Length is not available', (t) => {
if (err) tap.error(err)
fastify.server.unref()

const reqOpts = {
method: 'POST',
baseUrl: 'http://localhost:' + fastify.server.address().port,
headers: {
'content-type': 'application/x-www-form-urlencoded'
}
}
const req = request.defaults(reqOpts)
let sent = false
const payload = require('stream').Readable({
read: function () {
this.push(sent ? null : Buffer.alloc(70000, 'a'))
sent = true
}
})
req({ uri: '/limited', body: payload }, (err, response, body) => {
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(body).message, 'Request body is too large')
t.equal(JSON.parse(response.body).message, 'Request body is too large')
})
})
})
Expand All @@ -116,16 +98,11 @@ test('cannot exceed body limit set on Fastify instance', (t) => {
if (err) tap.error(err)
fastify.server.unref()

const reqOpts = {
method: 'POST',
baseUrl: 'http://localhost:' + fastify.server.address().port
}
const req = request.defaults(reqOpts)
const payload = require('crypto').randomBytes(128).toString('hex')
req({ uri: '/limited', form: { foo: payload } }, (err, response, body) => {
fastify.inject({ path: '/limited', method: 'POST', ...formAutoContent({ foo: payload }) }, (err, response) => {
t.error(err)
t.equal(response.statusCode, 413)
t.equal(JSON.parse(body).message, 'Request body is too large')
t.equal(JSON.parse(response.body).message, 'Request body is too large')
})
})
})
Expand All @@ -143,16 +120,11 @@ test('plugin bodyLimit should overwrite Fastify instance bodyLimit', (t) => {
if (err) tap.error(err)
fastify.server.unref()

const reqOpts = {
method: 'POST',
baseUrl: 'http://localhost:' + fastify.server.address().port
}
const req = request.defaults(reqOpts)
const payload = require('crypto').randomBytes(128).toString('hex')
req({ uri: '/limited', form: { foo: payload } }, (err, response, body) => {
fastify.inject({ path: '/limited', method: 'POST', ...formAutoContent({ foo: payload }) }, (err, response) => {
t.error(err)
t.equal(response.statusCode, 413)
t.equal(JSON.parse(body).message, 'Request body is too large')
t.equal(JSON.parse(response.body).message, 'Request body is too large')
})
})
})
Expand Down Expand Up @@ -181,15 +153,10 @@ test('plugin should not parse nested objects by default', (t) => {
if (err) tap.error(err)
fastify.server.unref()

const reqOpts = {
method: 'POST',
baseUrl: 'http://localhost:' + fastify.server.address().port
}
const req = request.defaults(reqOpts)
req({ uri: '/test1', form: { 'foo[one]': 'foo', 'foo[two]': 'bar' } }, (err, response, body) => {
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(body), { 'foo[one]': 'foo', 'foo[two]': 'bar', message: 'done' })
t.same(JSON.parse(response.body), { 'foo[one]': 'foo', 'foo[two]': 'bar', message: 'done' })
})
})
})
Expand All @@ -208,15 +175,10 @@ test('plugin should allow providing custom parser as option', (t) => {
if (err) tap.error(err)
fastify.server.unref()

const reqOpts = {
method: 'POST',
baseUrl: 'http://localhost:' + fastify.server.address().port
}
const req = request.defaults(reqOpts)
req({ uri: '/test1', form: { 'foo[one]': 'foo', 'foo[two]': 'bar' } }, (err, response, body) => {
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(body), { foo: { one: 'foo', two: 'bar' }, message: 'done' })
t.same(JSON.parse(response.body), { foo: { one: 'foo', two: 'bar' }, message: 'done' })
})
})
})
4 changes: 2 additions & 2 deletions formbody.d.ts → types/formbody.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FastifyPlugin } from 'fastify'
import { FastifyPluginCallback } from 'fastify'

export interface FormBodyPluginOptions {
bodyLimit?: number
parser?: (str: string) => Record<string, unknown>
}

declare const formBodyPlugin: FastifyPlugin<FormBodyPluginOptions>
declare const formBodyPlugin: FastifyPluginCallback<FormBodyPluginOptions>;

export default formBodyPlugin
2 changes: 1 addition & 1 deletion formbody.test-d.ts → types/formbody.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fastify from 'fastify'
import querystring from 'querystring'
import formBodyPlugin, { FormBodyPluginOptions } from './formbody'
import formBodyPlugin, { FormBodyPluginOptions } from '..'

const app = fastify()
app.register(formBodyPlugin)
Expand Down