Skip to content

Commit 09e8f7b

Browse files
authoredAug 8, 2022
Minor refactor (#126)
* disable package lock generation move typings to types folder use lint in ci workflow remove unused deps set tap configurations in .taprc * remove request package add forma-auto-content use fastify.inject instead of requests * update version to 7.0.1
1 parent ba4c189 commit 09e8f7b

File tree

7 files changed

+29
-66
lines changed

7 files changed

+29
-66
lines changed
 

‎.github/workflows/ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ jobs:
1515
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
1616
with:
1717
license-check: true
18+
lint: true

‎.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

‎.taprc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
files:
2+
- test/**/*.test.js

‎package.json

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
{
22
"name": "@fastify/formbody",
3-
"version": "7.0.0",
3+
"version": "7.0.1",
44
"description": "A module for Fastify to parse x-www-form-urlencoded bodies",
55
"main": "formbody.js",
66
"scripts": {
77
"codecov": "codecov",
88
"lint": "standard | snazzy",
9-
"lint:ci": "standard",
10-
"test": "standard && tap --100 \"test/**/*.test.js\" && tsd",
11-
"test:ci": "tap --100 \"test/**/*.test.js\"",
12-
"typescript": "tsd"
9+
"test": "npm run test:unit && npm run test:typescript",
10+
"test:unit": "tap",
11+
"test:typescript": "tsd"
1312
},
1413
"precommit": [
1514
"lint",
@@ -32,20 +31,18 @@
3231
"devDependencies": {
3332
"@fastify/pre-commit": "^2.0.2",
3433
"@types/node": "^18.0.0",
35-
"codecov": "^3.7.2",
3634
"fastify": "^4.0.0-rc.2",
35+
"form-auto-content": "^2.2.0",
3736
"qs": "^6.5.1",
38-
"request": "^2.88.0",
3937
"snazzy": "^9.0.0",
4038
"standard": "^17.0.0",
4139
"tap": "^16.0.0",
42-
"tsd": "^0.22.0",
43-
"typescript": "^4.0.2"
40+
"tsd": "^0.22.0"
4441
},
4542
"dependencies": {
4643
"fastify-plugin": "^4.0.0"
4744
},
48-
"types": "formbody.d.ts",
45+
"types": "types/formbody.d.ts",
4946
"publishConfig": {
5047
"access": "public"
5148
}

‎test/integration.test.js

+15-53
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
const tap = require('tap')
44
const test = tap.test
55
const Fastify = require('fastify')
6-
const request = require('request')
76
const plugin = require('../')
87
const qs = require('qs')
8+
const formAutoContent = require('form-auto-content')
99

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

23-
const reqOpts = {
24-
method: 'POST',
25-
baseUrl: 'http://localhost:' + fastify.server.address().port
26-
}
27-
const req = request.defaults(reqOpts)
28-
req({ uri: '/test1', form: { foo: 'foo' } }, (err, response, body) => {
23+
fastify.inject({ path: '/test1', method: 'POST', ...formAutoContent({ foo: 'foo' }) }, (err, response) => {
2924
t.error(err)
3025
t.equal(response.statusCode, 200)
31-
t.same(JSON.parse(body), { foo: 'foo', message: 'done' })
26+
t.same(JSON.parse(response.body), { foo: 'foo', message: 'done' })
3227
})
3328
})
3429
})
@@ -46,16 +41,11 @@ test('cannot exceed body limit', (t) => {
4641
if (err) tap.error(err)
4742
fastify.server.unref()
4843

49-
const reqOpts = {
50-
method: 'POST',
51-
baseUrl: 'http://localhost:' + fastify.server.address().port
52-
}
53-
const req = request.defaults(reqOpts)
5444
const payload = require('crypto').randomBytes(128).toString('hex')
55-
req({ uri: '/limited', form: { foo: payload } }, (err, response, body) => {
45+
fastify.inject({ path: '/limited', method: 'POST', ...formAutoContent({ foo: payload }) }, (err, response) => {
5646
t.error(err)
5747
t.equal(response.statusCode, 413)
58-
t.equal(JSON.parse(body).message, 'Request body is too large')
48+
t.equal(JSON.parse(response.body).message, 'Request body is too large')
5949
})
6050
})
6151
})
@@ -80,25 +70,17 @@ test('cannot exceed body limit when Content-Length is not available', (t) => {
8070
if (err) tap.error(err)
8171
fastify.server.unref()
8272

83-
const reqOpts = {
84-
method: 'POST',
85-
baseUrl: 'http://localhost:' + fastify.server.address().port,
86-
headers: {
87-
'content-type': 'application/x-www-form-urlencoded'
88-
}
89-
}
90-
const req = request.defaults(reqOpts)
9173
let sent = false
9274
const payload = require('stream').Readable({
9375
read: function () {
9476
this.push(sent ? null : Buffer.alloc(70000, 'a'))
9577
sent = true
9678
}
9779
})
98-
req({ uri: '/limited', body: payload }, (err, response, body) => {
80+
fastify.inject({ path: '/limited', method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: payload }, (err, response) => {
9981
t.error(err)
10082
t.equal(response.statusCode, 413)
101-
t.equal(JSON.parse(body).message, 'Request body is too large')
83+
t.equal(JSON.parse(response.body).message, 'Request body is too large')
10284
})
10385
})
10486
})
@@ -116,16 +98,11 @@ test('cannot exceed body limit set on Fastify instance', (t) => {
11698
if (err) tap.error(err)
11799
fastify.server.unref()
118100

119-
const reqOpts = {
120-
method: 'POST',
121-
baseUrl: 'http://localhost:' + fastify.server.address().port
122-
}
123-
const req = request.defaults(reqOpts)
124101
const payload = require('crypto').randomBytes(128).toString('hex')
125-
req({ uri: '/limited', form: { foo: payload } }, (err, response, body) => {
102+
fastify.inject({ path: '/limited', method: 'POST', ...formAutoContent({ foo: payload }) }, (err, response) => {
126103
t.error(err)
127104
t.equal(response.statusCode, 413)
128-
t.equal(JSON.parse(body).message, 'Request body is too large')
105+
t.equal(JSON.parse(response.body).message, 'Request body is too large')
129106
})
130107
})
131108
})
@@ -143,16 +120,11 @@ test('plugin bodyLimit should overwrite Fastify instance bodyLimit', (t) => {
143120
if (err) tap.error(err)
144121
fastify.server.unref()
145122

146-
const reqOpts = {
147-
method: 'POST',
148-
baseUrl: 'http://localhost:' + fastify.server.address().port
149-
}
150-
const req = request.defaults(reqOpts)
151123
const payload = require('crypto').randomBytes(128).toString('hex')
152-
req({ uri: '/limited', form: { foo: payload } }, (err, response, body) => {
124+
fastify.inject({ path: '/limited', method: 'POST', ...formAutoContent({ foo: payload }) }, (err, response) => {
153125
t.error(err)
154126
t.equal(response.statusCode, 413)
155-
t.equal(JSON.parse(body).message, 'Request body is too large')
127+
t.equal(JSON.parse(response.body).message, 'Request body is too large')
156128
})
157129
})
158130
})
@@ -181,15 +153,10 @@ test('plugin should not parse nested objects by default', (t) => {
181153
if (err) tap.error(err)
182154
fastify.server.unref()
183155

184-
const reqOpts = {
185-
method: 'POST',
186-
baseUrl: 'http://localhost:' + fastify.server.address().port
187-
}
188-
const req = request.defaults(reqOpts)
189-
req({ uri: '/test1', form: { 'foo[one]': 'foo', 'foo[two]': 'bar' } }, (err, response, body) => {
156+
fastify.inject({ path: '/test1', method: 'POST', ...formAutoContent({ 'foo[one]': 'foo', 'foo[two]': 'bar' }) }, (err, response) => {
190157
t.error(err)
191158
t.equal(response.statusCode, 200)
192-
t.same(JSON.parse(body), { 'foo[one]': 'foo', 'foo[two]': 'bar', message: 'done' })
159+
t.same(JSON.parse(response.body), { 'foo[one]': 'foo', 'foo[two]': 'bar', message: 'done' })
193160
})
194161
})
195162
})
@@ -208,15 +175,10 @@ test('plugin should allow providing custom parser as option', (t) => {
208175
if (err) tap.error(err)
209176
fastify.server.unref()
210177

211-
const reqOpts = {
212-
method: 'POST',
213-
baseUrl: 'http://localhost:' + fastify.server.address().port
214-
}
215-
const req = request.defaults(reqOpts)
216-
req({ uri: '/test1', form: { 'foo[one]': 'foo', 'foo[two]': 'bar' } }, (err, response, body) => {
178+
fastify.inject({ path: '/test1', method: 'POST', ...formAutoContent({ 'foo[one]': 'foo', 'foo[two]': 'bar' }) }, (err, response) => {
217179
t.error(err)
218180
t.equal(response.statusCode, 200)
219-
t.same(JSON.parse(body), { foo: { one: 'foo', two: 'bar' }, message: 'done' })
181+
t.same(JSON.parse(response.body), { foo: { one: 'foo', two: 'bar' }, message: 'done' })
220182
})
221183
})
222184
})

‎formbody.d.ts ‎types/formbody.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { FastifyPlugin } from 'fastify'
1+
import { FastifyPluginCallback } from 'fastify'
22

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

8-
declare const formBodyPlugin: FastifyPlugin<FormBodyPluginOptions>
8+
declare const formBodyPlugin: FastifyPluginCallback<FormBodyPluginOptions>;
99

1010
export default formBodyPlugin

‎formbody.test-d.ts ‎types/formbody.test-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fastify from 'fastify'
22
import querystring from 'querystring'
3-
import formBodyPlugin, { FormBodyPluginOptions } from './formbody'
3+
import formBodyPlugin, { FormBodyPluginOptions } from '..'
44

55
const app = fastify()
66
app.register(formBodyPlugin)

0 commit comments

Comments
 (0)