@fastify/express 1.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/nodejs.yml +10 -40
- package/README.md +81 -17
- package/index.d.ts +1 -1
- package/index.js +17 -4
- package/package.json +6 -7
- package/test/404s.test.js +17 -17
- package/test/application.test.js +14 -14
- package/test/basic.test.js +37 -37
- package/test/enhance-request.test.js +12 -12
- package/test/form-data.test.js +153 -0
- package/test/hooks.test.js +3 -3
- package/test/middleware.test.js +32 -32
|
@@ -1,49 +1,19 @@
|
|
|
1
1
|
name: CI
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
on:
|
|
3
4
|
push:
|
|
4
5
|
paths-ignore:
|
|
5
|
-
- docs/**
|
|
6
|
+
- 'docs/**'
|
|
6
7
|
- '*.md'
|
|
7
8
|
pull_request:
|
|
8
9
|
paths-ignore:
|
|
9
|
-
- docs/**
|
|
10
|
+
- 'docs/**'
|
|
10
11
|
- '*.md'
|
|
12
|
+
|
|
11
13
|
jobs:
|
|
12
14
|
test:
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
- 12
|
|
19
|
-
- 14
|
|
20
|
-
- 16
|
|
21
|
-
os:
|
|
22
|
-
- macos-latest
|
|
23
|
-
- ubuntu-latest
|
|
24
|
-
- windows-latest
|
|
25
|
-
steps:
|
|
26
|
-
- uses: actions/checkout@v3
|
|
27
|
-
- name: Use Node.js
|
|
28
|
-
uses: actions/setup-node@v3
|
|
29
|
-
with:
|
|
30
|
-
node-version: ${{ matrix.node-version }}
|
|
31
|
-
- name: Install Dependencies
|
|
32
|
-
run: |
|
|
33
|
-
npm install --ignore-scripts
|
|
34
|
-
- name: Run Tests
|
|
35
|
-
run: |
|
|
36
|
-
npm test
|
|
37
|
-
automerge:
|
|
38
|
-
needs: test
|
|
39
|
-
runs-on: ubuntu-latest
|
|
40
|
-
permissions:
|
|
41
|
-
pull-requests: write
|
|
42
|
-
contents: write
|
|
43
|
-
steps:
|
|
44
|
-
- uses: fastify/github-action-merge-dependabot@v3
|
|
45
|
-
if: >-
|
|
46
|
-
${{ github.actor == 'dependabot[bot]' && github.event_name ==
|
|
47
|
-
'pull_request' }}
|
|
48
|
-
with:
|
|
49
|
-
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
15
|
+
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
package/README.md
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
|
-
# fastify
|
|
1
|
+
# @fastify/express
|
|
2
2
|
|
|
3
3
|

|
|
4
|
-
[](https://snyk.io/test/github/fastify/fastify-express)
|
|
4
|
+
[](https://www.npmjs.com/package/@fastify/express)
|
|
6
5
|
[](https://standardjs.com/)
|
|
7
6
|
|
|
8
7
|
This plugin adds full [Express](http://expressjs.com) compatibility to Fastify, it exposes the same `use` function of Express, and it allows you to use any Express middleware or application.<br/>
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
<table>
|
|
10
|
+
<tbody>
|
|
11
|
+
<tr>
|
|
12
|
+
<td rowspan="2"><b>Note</b></td>
|
|
13
|
+
<td><b>This plugin should not be used as a long-term solution, it aims to help you have a smooth transition from Express to Fastify, but you should migrate your Express specific code to Fastify over time.</b></td>
|
|
14
|
+
</tr>
|
|
15
|
+
<tr>
|
|
16
|
+
<td><b>Since <a href="https://github.com/expressjs/express/issues/2761">Express does not support Node.js core HTTP/2 module</a>, this plugin does not support HTTP/2 too.</b></td>
|
|
17
|
+
</tr>
|
|
18
|
+
<tbody>
|
|
19
|
+
</table>
|
|
12
20
|
|
|
13
21
|
## Install
|
|
14
22
|
```
|
|
15
|
-
npm i fastify
|
|
23
|
+
npm i @fastify/express
|
|
16
24
|
```
|
|
17
25
|
|
|
18
26
|
## Usage
|
|
@@ -22,7 +30,7 @@ const Fastify = require('fastify')
|
|
|
22
30
|
|
|
23
31
|
async function build () {
|
|
24
32
|
const fastify = Fastify()
|
|
25
|
-
await fastify.register(require('fastify
|
|
33
|
+
await fastify.register(require('@fastify/express'))
|
|
26
34
|
// do you know we also have cors support?
|
|
27
35
|
// https://github.com/fastify/fastify-cors
|
|
28
36
|
fastify.use(require('cors')())
|
|
@@ -38,7 +46,7 @@ build()
|
|
|
38
46
|
|
|
39
47
|
### Add a complete application
|
|
40
48
|
|
|
41
|
-
You can register an entire Express application and make it work with Fastify. Remember,
|
|
49
|
+
You can register an entire Express application and make it work with Fastify. Remember, `@fastify/express` is just `express` under the covers and requires the same body parsers as you'd use in `express`.
|
|
42
50
|
|
|
43
51
|
```js
|
|
44
52
|
// index.js
|
|
@@ -76,7 +84,7 @@ router.use('*', (req, res) => {
|
|
|
76
84
|
res.json({ msg: 'not found'})
|
|
77
85
|
})
|
|
78
86
|
|
|
79
|
-
fastify.register(require('fastify
|
|
87
|
+
fastify.register(require('@fastify/express'))
|
|
80
88
|
.after(() => {
|
|
81
89
|
fastify.use(express.urlencoded({extended: false})) // for Postman x-www-form-urlencoded
|
|
82
90
|
fastify.use(express.json())
|
|
@@ -114,7 +122,7 @@ const fastify = require('fastify')()
|
|
|
114
122
|
fastify.register(subsystem)
|
|
115
123
|
|
|
116
124
|
async function subsystem (fastify, opts) {
|
|
117
|
-
await fastify.register(require('fastify
|
|
125
|
+
await fastify.register(require('@fastify/express'))
|
|
118
126
|
fastify.use(require('cors')())
|
|
119
127
|
}
|
|
120
128
|
```
|
|
@@ -124,7 +132,7 @@ async function subsystem (fastify, opts) {
|
|
|
124
132
|
const fastify = require('fastify')()
|
|
125
133
|
|
|
126
134
|
fastify
|
|
127
|
-
.register(require('fastify
|
|
135
|
+
.register(require('@fastify/express'))
|
|
128
136
|
.register(subsystem)
|
|
129
137
|
|
|
130
138
|
async function subsystem (fastify, opts) {
|
|
@@ -141,7 +149,7 @@ Take a look at the [Lifecycle](https://www.fastify.io/docs/latest/Lifecycle/) do
|
|
|
141
149
|
const fastify = require('fastify')()
|
|
142
150
|
|
|
143
151
|
fastify
|
|
144
|
-
.register(require('fastify
|
|
152
|
+
.register(require('@fastify/express'))
|
|
145
153
|
.register(subsystem)
|
|
146
154
|
|
|
147
155
|
async function subsystem (fastify, opts) {
|
|
@@ -170,7 +178,7 @@ const path = require('path')
|
|
|
170
178
|
const serveStatic = require('serve-static')
|
|
171
179
|
|
|
172
180
|
fastify
|
|
173
|
-
.register(require('fastify
|
|
181
|
+
.register(require('@fastify/express'))
|
|
174
182
|
.register(subsystem)
|
|
175
183
|
|
|
176
184
|
async function subsystem (fastify, opts) {
|
|
@@ -189,7 +197,7 @@ async function subsystem (fastify, opts) {
|
|
|
189
197
|
|
|
190
198
|
To use this module with TypeScript, make sure to install `@types/express`.
|
|
191
199
|
|
|
192
|
-
You will need to add `"types": ["fastify
|
|
200
|
+
You will need to add `"types": ["@fastify/express"]` to your tsconfig.json file when using `require` to import the plugin.
|
|
193
201
|
|
|
194
202
|
## Middlewares alternatives
|
|
195
203
|
|
|
@@ -197,9 +205,65 @@ Fastify offers some alternatives to the most commonly used middlewares, followin
|
|
|
197
205
|
|
|
198
206
|
| Express Middleware | Fastify Plugin |
|
|
199
207
|
| ------------- |---------------|
|
|
200
|
-
| [`helmet`](https://github.com/helmetjs/helmet) | [
|
|
201
|
-
| [`cors`](https://github.com/expressjs/cors) | [
|
|
202
|
-
| [`serve-static`](https://github.com/expressjs/serve-static) | [
|
|
208
|
+
| [`helmet`](https://github.com/helmetjs/helmet) | [`@fastify/helmet`](https://github.com/fastify/fastify-helmet) |
|
|
209
|
+
| [`cors`](https://github.com/expressjs/cors) | [`@fastify/cors`](https://github.com/fastify/fastify-cors) |
|
|
210
|
+
| [`serve-static`](https://github.com/expressjs/serve-static) | [`@fastify/static`](https://github.com/fastify/fastify-static) |
|
|
211
|
+
|
|
212
|
+
## Troubleshooting
|
|
213
|
+
|
|
214
|
+
### POST request with body hangs up
|
|
215
|
+
|
|
216
|
+
[body-parser](https://github.com/expressjs/body-parser) library incompatible with `fastify-express`, when you have `fastify` routes and any `express` middlewares.
|
|
217
|
+
Any POST requests with **body**, which `body-parser` will try to parse, will be hangs up.
|
|
218
|
+
|
|
219
|
+
Example application:
|
|
220
|
+
|
|
221
|
+
```js
|
|
222
|
+
const Fastify = require('fastify')
|
|
223
|
+
const Express = require('express')
|
|
224
|
+
const expressPlugin = require('fastify-express')
|
|
225
|
+
const bodyParser = require('body-parser')
|
|
226
|
+
|
|
227
|
+
const fastify = Fastify()
|
|
228
|
+
const express = Express()
|
|
229
|
+
|
|
230
|
+
express.use(bodyParser.urlencoded({ extended: false }))
|
|
231
|
+
|
|
232
|
+
await fastify.register(expressPlugin)
|
|
233
|
+
|
|
234
|
+
fastify.use(express)
|
|
235
|
+
|
|
236
|
+
// this route will never reply
|
|
237
|
+
fastify.post('/hello', (req, reply) => {
|
|
238
|
+
return { hello: 'world' }
|
|
239
|
+
})
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
For this case, you need to remove `body-parser`, install `fastify-formbody` and change `fastify-express` options:
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
```js
|
|
246
|
+
const Fastify = require('fastify')
|
|
247
|
+
const Express = require('express')
|
|
248
|
+
const expressPlugin = require('fastify-express')
|
|
249
|
+
const fastifyFormBody = require('fastify-formbody')
|
|
250
|
+
|
|
251
|
+
const fastify = Fastify()
|
|
252
|
+
const express = Express()
|
|
253
|
+
|
|
254
|
+
await fastify.register(fastifyFormBody)
|
|
255
|
+
await fastify.register(expressPlugin, {
|
|
256
|
+
// run express after `fastify-formbody` logic
|
|
257
|
+
expressHook: 'preHandler'
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
fastify.use(express)
|
|
261
|
+
|
|
262
|
+
// it works!
|
|
263
|
+
fastify.post('/hello', (req, reply) => {
|
|
264
|
+
return { hello: 'world' }
|
|
265
|
+
})
|
|
266
|
+
```
|
|
203
267
|
|
|
204
268
|
## License
|
|
205
269
|
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -6,14 +6,18 @@ const Express = require('express')
|
|
|
6
6
|
const kMiddlewares = Symbol('fastify-express-middlewares')
|
|
7
7
|
|
|
8
8
|
function expressPlugin (fastify, options, next) {
|
|
9
|
+
const {
|
|
10
|
+
expressHook = 'onRequest'
|
|
11
|
+
} = options
|
|
12
|
+
|
|
9
13
|
fastify.decorate('use', use)
|
|
10
14
|
fastify[kMiddlewares] = []
|
|
11
15
|
fastify.decorate('express', Express())
|
|
12
16
|
fastify.express.disable('x-powered-by')
|
|
13
17
|
|
|
14
18
|
fastify
|
|
15
|
-
.addHook(
|
|
16
|
-
.addHook(
|
|
19
|
+
.addHook(expressHook, enhanceRequest)
|
|
20
|
+
.addHook(expressHook, runConnect)
|
|
17
21
|
.addHook('onRegister', onRegister)
|
|
18
22
|
|
|
19
23
|
function use (path, fn) {
|
|
@@ -39,6 +43,15 @@ function expressPlugin (fastify, options, next) {
|
|
|
39
43
|
req.raw.log = req.log
|
|
40
44
|
reply.raw.log = req.log
|
|
41
45
|
|
|
46
|
+
// backward compatibility for body-parser
|
|
47
|
+
if (req.body) {
|
|
48
|
+
req.raw.body = req.body
|
|
49
|
+
}
|
|
50
|
+
// backward compatibility for cookie-parser
|
|
51
|
+
if (req.cookies) {
|
|
52
|
+
req.raw.cookies = req.cookies
|
|
53
|
+
}
|
|
54
|
+
|
|
42
55
|
const originalProtocol = req.raw.protocol
|
|
43
56
|
// Make it lazy as it does a bit of work
|
|
44
57
|
Object.defineProperty(req.raw, 'protocol', {
|
|
@@ -78,6 +91,6 @@ function expressPlugin (fastify, options, next) {
|
|
|
78
91
|
}
|
|
79
92
|
|
|
80
93
|
module.exports = fp(expressPlugin, {
|
|
81
|
-
fastify: '
|
|
82
|
-
name: 'fastify
|
|
94
|
+
fastify: '4.x',
|
|
95
|
+
name: '@fastify/express'
|
|
83
96
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/express",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Express compatibility layer for Fastify",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -27,24 +27,23 @@
|
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@types/express": "^4.17.1",
|
|
29
29
|
"@types/node": "*",
|
|
30
|
+
"body-parser": "^1.20.0",
|
|
30
31
|
"cors": "^2.8.5",
|
|
31
|
-
"fastify": "^
|
|
32
|
+
"fastify": "^4.0.0-rc.3",
|
|
33
|
+
"@fastify/formbody": "^7.0.1",
|
|
32
34
|
"helmet": "^5.0.0",
|
|
33
|
-
"passport": "^0.
|
|
35
|
+
"passport": "^0.6.0",
|
|
34
36
|
"passport-http-bearer": "^1.0.1",
|
|
35
37
|
"serve-static": "^1.14.1",
|
|
36
38
|
"simple-get": "^4.0.0",
|
|
37
39
|
"standard": "^17.0.0",
|
|
38
40
|
"tap": "^16.0.0",
|
|
39
|
-
"tsd": "^0.
|
|
41
|
+
"tsd": "^0.21.0"
|
|
40
42
|
},
|
|
41
43
|
"dependencies": {
|
|
42
44
|
"express": "^4.17.1",
|
|
43
45
|
"fastify-plugin": "^3.0.0"
|
|
44
46
|
},
|
|
45
|
-
"engines": {
|
|
46
|
-
"node": ">=10.0.0"
|
|
47
|
-
},
|
|
48
47
|
"tsd": {
|
|
49
48
|
"directory": "test/types"
|
|
50
49
|
},
|
package/test/404s.test.js
CHANGED
|
@@ -44,9 +44,9 @@ test('run hooks and middleware on default 404', t => {
|
|
|
44
44
|
reply.send({ hello: 'world' })
|
|
45
45
|
})
|
|
46
46
|
|
|
47
|
-
t.
|
|
47
|
+
t.teardown(fastify.close.bind(fastify))
|
|
48
48
|
|
|
49
|
-
fastify.listen(0, err => {
|
|
49
|
+
fastify.listen({ port: 0 }, err => {
|
|
50
50
|
t.error(err)
|
|
51
51
|
|
|
52
52
|
sget({
|
|
@@ -56,7 +56,7 @@ test('run hooks and middleware on default 404', t => {
|
|
|
56
56
|
headers: { 'Content-Type': 'application/json' }
|
|
57
57
|
}, (err, response, body) => {
|
|
58
58
|
t.error(err)
|
|
59
|
-
t.
|
|
59
|
+
t.equal(response.statusCode, 404)
|
|
60
60
|
})
|
|
61
61
|
})
|
|
62
62
|
})
|
|
@@ -101,7 +101,7 @@ test('run non-encapsulated plugin hooks and middleware on default 404', t => {
|
|
|
101
101
|
reply.send({ hello: 'world' })
|
|
102
102
|
})
|
|
103
103
|
|
|
104
|
-
fastify.listen(0, (err, address) => {
|
|
104
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
105
105
|
t.error(err)
|
|
106
106
|
sget({
|
|
107
107
|
method: 'POST',
|
|
@@ -110,7 +110,7 @@ test('run non-encapsulated plugin hooks and middleware on default 404', t => {
|
|
|
110
110
|
headers: { 'Content-Type': 'application/json' }
|
|
111
111
|
}, (err, response, body) => {
|
|
112
112
|
t.error(err)
|
|
113
|
-
t.
|
|
113
|
+
t.equal(response.statusCode, 404)
|
|
114
114
|
})
|
|
115
115
|
})
|
|
116
116
|
})
|
|
@@ -163,15 +163,15 @@ test('run non-encapsulated plugin hooks and middleware on custom 404', t => {
|
|
|
163
163
|
|
|
164
164
|
fastify.register(plugin) // Registering plugin after handler also works
|
|
165
165
|
|
|
166
|
-
fastify.listen(0, (err, address) => {
|
|
166
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
167
167
|
t.error(err)
|
|
168
168
|
sget({
|
|
169
169
|
method: 'GET',
|
|
170
170
|
url: address + '/not-found'
|
|
171
171
|
}, (err, response, body) => {
|
|
172
172
|
t.error(err)
|
|
173
|
-
t.
|
|
174
|
-
t.
|
|
173
|
+
t.equal(body.toString(), 'this was not found')
|
|
174
|
+
t.equal(response.statusCode, 404)
|
|
175
175
|
})
|
|
176
176
|
})
|
|
177
177
|
})
|
|
@@ -243,9 +243,9 @@ test('run hooks and middleware with encapsulated 404', t => {
|
|
|
243
243
|
next()
|
|
244
244
|
}, { prefix: '/test' })
|
|
245
245
|
|
|
246
|
-
t.
|
|
246
|
+
t.teardown(fastify.close.bind(fastify))
|
|
247
247
|
|
|
248
|
-
fastify.listen(0, err => {
|
|
248
|
+
fastify.listen({ port: 0 }, err => {
|
|
249
249
|
t.error(err)
|
|
250
250
|
|
|
251
251
|
sget({
|
|
@@ -255,7 +255,7 @@ test('run hooks and middleware with encapsulated 404', t => {
|
|
|
255
255
|
headers: { 'Content-Type': 'application/json' }
|
|
256
256
|
}, (err, response, body) => {
|
|
257
257
|
t.error(err)
|
|
258
|
-
t.
|
|
258
|
+
t.equal(response.statusCode, 404)
|
|
259
259
|
})
|
|
260
260
|
})
|
|
261
261
|
})
|
|
@@ -277,9 +277,9 @@ test('run middlewares on default 404', t => {
|
|
|
277
277
|
reply.send({ hello: 'world' })
|
|
278
278
|
})
|
|
279
279
|
|
|
280
|
-
t.
|
|
280
|
+
t.teardown(fastify.close.bind(fastify))
|
|
281
281
|
|
|
282
|
-
fastify.listen(0, err => {
|
|
282
|
+
fastify.listen({ port: 0 }, err => {
|
|
283
283
|
t.error(err)
|
|
284
284
|
|
|
285
285
|
sget({
|
|
@@ -289,7 +289,7 @@ test('run middlewares on default 404', t => {
|
|
|
289
289
|
headers: { 'Content-Type': 'application/json' }
|
|
290
290
|
}, (err, response, body) => {
|
|
291
291
|
t.error(err)
|
|
292
|
-
t.
|
|
292
|
+
t.equal(response.statusCode, 404)
|
|
293
293
|
})
|
|
294
294
|
})
|
|
295
295
|
})
|
|
@@ -320,9 +320,9 @@ test('run middlewares with encapsulated 404', t => {
|
|
|
320
320
|
next()
|
|
321
321
|
}, { prefix: '/test' })
|
|
322
322
|
|
|
323
|
-
t.
|
|
323
|
+
t.teardown(fastify.close.bind(fastify))
|
|
324
324
|
|
|
325
|
-
fastify.listen(0, err => {
|
|
325
|
+
fastify.listen({ port: 0 }, err => {
|
|
326
326
|
t.error(err)
|
|
327
327
|
|
|
328
328
|
sget({
|
|
@@ -332,7 +332,7 @@ test('run middlewares with encapsulated 404', t => {
|
|
|
332
332
|
headers: { 'Content-Type': 'application/json' }
|
|
333
333
|
}, (err, response, body) => {
|
|
334
334
|
t.error(err)
|
|
335
|
-
t.
|
|
335
|
+
t.equal(response.statusCode, 404)
|
|
336
336
|
})
|
|
337
337
|
})
|
|
338
338
|
})
|
package/test/application.test.js
CHANGED
|
@@ -26,16 +26,16 @@ test('Register express application', t => {
|
|
|
26
26
|
fastify.register(expressPlugin)
|
|
27
27
|
.after(() => { fastify.use(express) })
|
|
28
28
|
|
|
29
|
-
fastify.listen(0, (err, address) => {
|
|
29
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
30
30
|
t.error(err)
|
|
31
31
|
sget({
|
|
32
32
|
method: 'GET',
|
|
33
33
|
url: address + '/hello'
|
|
34
34
|
}, (err, res, data) => {
|
|
35
35
|
t.error(err)
|
|
36
|
-
t.
|
|
36
|
+
t.equal(res.statusCode, 201)
|
|
37
37
|
t.match(res.headers, { 'x-custom': 'true' })
|
|
38
|
-
t.
|
|
38
|
+
t.same(JSON.parse(data), { hello: 'world' })
|
|
39
39
|
})
|
|
40
40
|
})
|
|
41
41
|
})
|
|
@@ -65,25 +65,25 @@ test('Register express application that uses Router', t => {
|
|
|
65
65
|
fastify.register(expressPlugin)
|
|
66
66
|
.after(() => { fastify.use(router) })
|
|
67
67
|
|
|
68
|
-
fastify.listen(0, (err, address) => {
|
|
68
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
69
69
|
t.error(err)
|
|
70
70
|
sget({
|
|
71
71
|
method: 'GET',
|
|
72
72
|
url: address + '/hello'
|
|
73
73
|
}, (err, res, data) => {
|
|
74
74
|
t.error(err)
|
|
75
|
-
t.
|
|
75
|
+
t.equal(res.statusCode, 201)
|
|
76
76
|
t.match(res.headers, { 'x-custom': 'true' })
|
|
77
|
-
t.
|
|
77
|
+
t.same(JSON.parse(data), { hello: 'world' })
|
|
78
78
|
})
|
|
79
79
|
sget({
|
|
80
80
|
method: 'GET',
|
|
81
81
|
url: address + '/foo'
|
|
82
82
|
}, (err, res, data) => {
|
|
83
83
|
t.error(err)
|
|
84
|
-
t.
|
|
84
|
+
t.equal(res.statusCode, 400)
|
|
85
85
|
t.match(res.headers, { 'x-custom': 'true' })
|
|
86
|
-
t.
|
|
86
|
+
t.same(JSON.parse(data), { foo: 'bar' })
|
|
87
87
|
})
|
|
88
88
|
})
|
|
89
89
|
})
|
|
@@ -104,14 +104,14 @@ test('Should remove x-powered-by header', t => {
|
|
|
104
104
|
.register(expressPlugin)
|
|
105
105
|
.after(() => { fastify.use(router) })
|
|
106
106
|
|
|
107
|
-
fastify.listen(0, (err, address) => {
|
|
107
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
108
108
|
t.error(err)
|
|
109
109
|
sget({
|
|
110
110
|
method: 'GET',
|
|
111
111
|
url: address
|
|
112
112
|
}, (err, res) => {
|
|
113
113
|
t.error(err)
|
|
114
|
-
t.
|
|
114
|
+
t.equal(res.headers['x-powered-by'], undefined)
|
|
115
115
|
})
|
|
116
116
|
})
|
|
117
117
|
})
|
|
@@ -132,14 +132,14 @@ test('Should expose the express app on the fastify instance', t => {
|
|
|
132
132
|
.register(expressPlugin)
|
|
133
133
|
.after(() => { fastify.use(router) })
|
|
134
134
|
|
|
135
|
-
fastify.listen(0, (err, address) => {
|
|
135
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
136
136
|
t.error(err)
|
|
137
137
|
sget({
|
|
138
138
|
method: 'GET',
|
|
139
139
|
url: address
|
|
140
140
|
}, (err, res) => {
|
|
141
141
|
t.error(err)
|
|
142
|
-
t.
|
|
142
|
+
t.equal(fastify.express.disabled('x-powered-by'), true)
|
|
143
143
|
})
|
|
144
144
|
})
|
|
145
145
|
})
|
|
@@ -166,14 +166,14 @@ test('Should flush headers if express handles request', t => {
|
|
|
166
166
|
.register(expressPlugin)
|
|
167
167
|
.after(() => { fastify.use(router) })
|
|
168
168
|
|
|
169
|
-
fastify.listen(0, (err, address) => {
|
|
169
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
170
170
|
t.error(err)
|
|
171
171
|
sget({
|
|
172
172
|
method: 'GET',
|
|
173
173
|
url: address
|
|
174
174
|
}, (err, res) => {
|
|
175
175
|
t.error(err)
|
|
176
|
-
t.
|
|
176
|
+
t.equal(res.headers.foo, 'bar')
|
|
177
177
|
})
|
|
178
178
|
})
|
|
179
179
|
})
|
package/test/basic.test.js
CHANGED
|
@@ -22,7 +22,7 @@ test('Should support connect style middlewares', t => {
|
|
|
22
22
|
return { hello: 'world' }
|
|
23
23
|
})
|
|
24
24
|
|
|
25
|
-
fastify.listen(0, (err, address) => {
|
|
25
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
26
26
|
t.error(err)
|
|
27
27
|
sget({
|
|
28
28
|
method: 'GET',
|
|
@@ -32,7 +32,7 @@ test('Should support connect style middlewares', t => {
|
|
|
32
32
|
t.match(res.headers, {
|
|
33
33
|
'access-control-allow-origin': '*'
|
|
34
34
|
})
|
|
35
|
-
t.
|
|
35
|
+
t.same(JSON.parse(data), { hello: 'world' })
|
|
36
36
|
})
|
|
37
37
|
})
|
|
38
38
|
})
|
|
@@ -49,7 +49,7 @@ test('Should support connect style middlewares (async await)', async t => {
|
|
|
49
49
|
return { hello: 'world' }
|
|
50
50
|
})
|
|
51
51
|
|
|
52
|
-
const address = await fastify.listen(0)
|
|
52
|
+
const address = await fastify.listen({ port: 0 })
|
|
53
53
|
return new Promise((resolve, reject) => {
|
|
54
54
|
sget({
|
|
55
55
|
method: 'GET',
|
|
@@ -59,7 +59,7 @@ test('Should support connect style middlewares (async await)', async t => {
|
|
|
59
59
|
t.match(res.headers, {
|
|
60
60
|
'access-control-allow-origin': '*'
|
|
61
61
|
})
|
|
62
|
-
t.
|
|
62
|
+
t.same(JSON.parse(data), { hello: 'world' })
|
|
63
63
|
resolve()
|
|
64
64
|
})
|
|
65
65
|
})
|
|
@@ -78,7 +78,7 @@ test('Should support connect style middlewares (async await after)', async t =>
|
|
|
78
78
|
return { hello: 'world' }
|
|
79
79
|
})
|
|
80
80
|
|
|
81
|
-
const address = await fastify.listen(0)
|
|
81
|
+
const address = await fastify.listen({ port: 0 })
|
|
82
82
|
return new Promise((resolve, reject) => {
|
|
83
83
|
sget({
|
|
84
84
|
method: 'GET',
|
|
@@ -88,7 +88,7 @@ test('Should support connect style middlewares (async await after)', async t =>
|
|
|
88
88
|
t.match(res.headers, {
|
|
89
89
|
'access-control-allow-origin': '*'
|
|
90
90
|
})
|
|
91
|
-
t.
|
|
91
|
+
t.same(JSON.parse(data), { hello: 'world' })
|
|
92
92
|
resolve()
|
|
93
93
|
})
|
|
94
94
|
})
|
|
@@ -111,7 +111,7 @@ test('Should support per path middlewares', t => {
|
|
|
111
111
|
return { hello: 'world' }
|
|
112
112
|
})
|
|
113
113
|
|
|
114
|
-
fastify.listen(0, (err, address) => {
|
|
114
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
115
115
|
t.error(err)
|
|
116
116
|
sget({
|
|
117
117
|
method: 'GET',
|
|
@@ -139,7 +139,7 @@ test('Should support complex middlewares', t => {
|
|
|
139
139
|
const fastify = Fastify()
|
|
140
140
|
|
|
141
141
|
passport.use(new Strategy((token, cb) => {
|
|
142
|
-
t.
|
|
142
|
+
t.equal(token, '123456789')
|
|
143
143
|
cb(null, { token })
|
|
144
144
|
}))
|
|
145
145
|
|
|
@@ -148,10 +148,10 @@ test('Should support complex middlewares', t => {
|
|
|
148
148
|
.after(() => { fastify.use(passport.authenticate('bearer', { session: false })) })
|
|
149
149
|
fastify
|
|
150
150
|
.get('/', (req, reply) => {
|
|
151
|
-
t.
|
|
151
|
+
t.same(req.raw.user, { token: '123456789' })
|
|
152
152
|
reply.send('ok')
|
|
153
153
|
})
|
|
154
|
-
.listen(0, (err, address) => {
|
|
154
|
+
.listen({ port: 0 }, (err, address) => {
|
|
155
155
|
t.error(err)
|
|
156
156
|
sget({
|
|
157
157
|
method: 'GET',
|
|
@@ -186,7 +186,7 @@ test('Encapsulation support / 1', t => {
|
|
|
186
186
|
reply.send('ok')
|
|
187
187
|
})
|
|
188
188
|
|
|
189
|
-
fastify.listen(0, (err, address) => {
|
|
189
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
190
190
|
t.error(err)
|
|
191
191
|
sget({
|
|
192
192
|
method: 'GET',
|
|
@@ -222,7 +222,7 @@ test('Encapsulation support / 2', t => {
|
|
|
222
222
|
reply.send('ok')
|
|
223
223
|
})
|
|
224
224
|
|
|
225
|
-
fastify.listen(0, (err, address) => {
|
|
225
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
226
226
|
t.error(err)
|
|
227
227
|
sget({
|
|
228
228
|
method: 'GET',
|
|
@@ -260,7 +260,7 @@ test('Encapsulation support / 3', t => {
|
|
|
260
260
|
reply.send('ok')
|
|
261
261
|
})
|
|
262
262
|
|
|
263
|
-
fastify.listen(0, (err, address) => {
|
|
263
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
264
264
|
t.error(err)
|
|
265
265
|
sget({
|
|
266
266
|
method: 'GET',
|
|
@@ -309,7 +309,7 @@ test('Encapsulation support / 4', t => {
|
|
|
309
309
|
reply.send('ok')
|
|
310
310
|
})
|
|
311
311
|
|
|
312
|
-
fastify.listen(0, (err, address) => {
|
|
312
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
313
313
|
t.error(err)
|
|
314
314
|
sget({
|
|
315
315
|
method: 'GET',
|
|
@@ -378,7 +378,7 @@ test('Encapsulation support / 5', t => {
|
|
|
378
378
|
reply.send('ok')
|
|
379
379
|
})
|
|
380
380
|
|
|
381
|
-
fastify.listen(0, (err, address) => {
|
|
381
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
382
382
|
t.error(err)
|
|
383
383
|
sget({
|
|
384
384
|
method: 'GET',
|
|
@@ -449,7 +449,7 @@ test('Middleware chain', t => {
|
|
|
449
449
|
return { hello: 'world' }
|
|
450
450
|
})
|
|
451
451
|
|
|
452
|
-
fastify.listen(0, (err, address) => {
|
|
452
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
453
453
|
t.error(err)
|
|
454
454
|
sget({
|
|
455
455
|
method: 'GET',
|
|
@@ -461,17 +461,17 @@ test('Middleware chain', t => {
|
|
|
461
461
|
})
|
|
462
462
|
|
|
463
463
|
function middleware1 (req, res, next) {
|
|
464
|
-
t.
|
|
464
|
+
t.equal(order.shift(), 1)
|
|
465
465
|
next()
|
|
466
466
|
}
|
|
467
467
|
|
|
468
468
|
function middleware2 (req, res, next) {
|
|
469
|
-
t.
|
|
469
|
+
t.equal(order.shift(), 2)
|
|
470
470
|
next()
|
|
471
471
|
}
|
|
472
472
|
|
|
473
473
|
function middleware3 (req, res, next) {
|
|
474
|
-
t.
|
|
474
|
+
t.equal(order.shift(), 3)
|
|
475
475
|
next()
|
|
476
476
|
}
|
|
477
477
|
})
|
|
@@ -495,32 +495,32 @@ test('Middleware chain (with errors) / 1', t => {
|
|
|
495
495
|
return { hello: 'world' }
|
|
496
496
|
})
|
|
497
497
|
|
|
498
|
-
fastify.listen(0, (err, address) => {
|
|
498
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
499
499
|
t.error(err)
|
|
500
500
|
sget({
|
|
501
501
|
method: 'GET',
|
|
502
502
|
url: address
|
|
503
503
|
}, (err, res, data) => {
|
|
504
504
|
t.error(err)
|
|
505
|
-
t.
|
|
505
|
+
t.equal(res.statusCode, 500)
|
|
506
506
|
fastify.close()
|
|
507
507
|
})
|
|
508
508
|
})
|
|
509
509
|
|
|
510
510
|
function middleware1 (req, res, next) {
|
|
511
|
-
t.
|
|
511
|
+
t.equal(order.shift(), 1)
|
|
512
512
|
next(new Error('middleware1'))
|
|
513
513
|
}
|
|
514
514
|
|
|
515
515
|
function middleware2 (err, req, res, next) {
|
|
516
|
-
t.
|
|
517
|
-
t.
|
|
516
|
+
t.equal(err.message, 'middleware1')
|
|
517
|
+
t.equal(order.shift(), 2)
|
|
518
518
|
next(new Error('middleware2'))
|
|
519
519
|
}
|
|
520
520
|
|
|
521
521
|
function middleware3 (err, req, res, next) {
|
|
522
|
-
t.
|
|
523
|
-
t.
|
|
522
|
+
t.equal(err.message, 'middleware2')
|
|
523
|
+
t.equal(order.shift(), 3)
|
|
524
524
|
next(new Error('kaboom'))
|
|
525
525
|
}
|
|
526
526
|
})
|
|
@@ -532,7 +532,7 @@ test('Middleware chain (with errors) / 2', t => {
|
|
|
532
532
|
const fastify = Fastify()
|
|
533
533
|
|
|
534
534
|
fastify.setErrorHandler((err, req, reply) => {
|
|
535
|
-
t.
|
|
535
|
+
t.equal(err.message, 'middleware2')
|
|
536
536
|
reply.send(err)
|
|
537
537
|
})
|
|
538
538
|
|
|
@@ -549,26 +549,26 @@ test('Middleware chain (with errors) / 2', t => {
|
|
|
549
549
|
return { hello: 'world' }
|
|
550
550
|
})
|
|
551
551
|
|
|
552
|
-
fastify.listen(0, (err, address) => {
|
|
552
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
553
553
|
t.error(err)
|
|
554
554
|
sget({
|
|
555
555
|
method: 'GET',
|
|
556
556
|
url: address
|
|
557
557
|
}, (err, res, data) => {
|
|
558
558
|
t.error(err)
|
|
559
|
-
t.
|
|
559
|
+
t.equal(res.statusCode, 500)
|
|
560
560
|
fastify.close()
|
|
561
561
|
})
|
|
562
562
|
})
|
|
563
563
|
|
|
564
564
|
function middleware1 (req, res, next) {
|
|
565
|
-
t.
|
|
565
|
+
t.equal(order.shift(), 1)
|
|
566
566
|
next(new Error('middleware1'))
|
|
567
567
|
}
|
|
568
568
|
|
|
569
569
|
function middleware2 (err, req, res, next) {
|
|
570
|
-
t.
|
|
571
|
-
t.
|
|
570
|
+
t.equal(err.message, 'middleware1')
|
|
571
|
+
t.equal(order.shift(), 2)
|
|
572
572
|
next(new Error('middleware2'))
|
|
573
573
|
}
|
|
574
574
|
|
|
@@ -615,7 +615,7 @@ test('Send a response from a middleware', t => {
|
|
|
615
615
|
t.fail('We should not be here')
|
|
616
616
|
})
|
|
617
617
|
|
|
618
|
-
fastify.listen(0, (err, address) => {
|
|
618
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
619
619
|
t.error(err)
|
|
620
620
|
sget({
|
|
621
621
|
method: 'GET',
|
|
@@ -623,7 +623,7 @@ test('Send a response from a middleware', t => {
|
|
|
623
623
|
json: true
|
|
624
624
|
}, (err, res, data) => {
|
|
625
625
|
t.error(err)
|
|
626
|
-
t.
|
|
626
|
+
t.same(data, { hello: 'world' })
|
|
627
627
|
fastify.close()
|
|
628
628
|
})
|
|
629
629
|
})
|
|
@@ -657,15 +657,15 @@ test('Should support plugin level prefix', t => {
|
|
|
657
657
|
next()
|
|
658
658
|
}, { prefix: '/hello' })
|
|
659
659
|
|
|
660
|
-
fastify.listen(0, (err, address) => {
|
|
660
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
661
661
|
t.error(err)
|
|
662
662
|
sget({
|
|
663
663
|
method: 'GET',
|
|
664
664
|
url: address + '/hello/world'
|
|
665
665
|
}, (err, res, data) => {
|
|
666
666
|
t.error(err)
|
|
667
|
-
t.
|
|
668
|
-
t.
|
|
667
|
+
t.equal(res.headers['x-foo'], 'bar')
|
|
668
|
+
t.same(JSON.parse(data), { hello: 'world' })
|
|
669
669
|
})
|
|
670
670
|
})
|
|
671
671
|
})
|
|
@@ -14,18 +14,18 @@ test('Should enhance the Node.js core request/response objects', t => {
|
|
|
14
14
|
fastify.register(expressPlugin)
|
|
15
15
|
|
|
16
16
|
fastify.get('/', async (req, reply) => {
|
|
17
|
-
t.
|
|
18
|
-
t.
|
|
19
|
-
t.
|
|
20
|
-
t.
|
|
21
|
-
t.
|
|
22
|
-
t.
|
|
17
|
+
t.equal(req.raw.originalUrl, req.raw.url)
|
|
18
|
+
t.equal(req.raw.id, req.id)
|
|
19
|
+
t.equal(req.raw.hostname, req.hostname)
|
|
20
|
+
t.equal(req.raw.protocol, req.protocol)
|
|
21
|
+
t.equal(req.raw.ip, req.ip)
|
|
22
|
+
t.same(req.raw.ips, req.ips)
|
|
23
23
|
t.ok(req.raw.log)
|
|
24
24
|
t.ok(reply.raw.log)
|
|
25
25
|
return { hello: 'world' }
|
|
26
26
|
})
|
|
27
27
|
|
|
28
|
-
fastify.listen(0, (err, address) => {
|
|
28
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
29
29
|
t.error(err)
|
|
30
30
|
sget({
|
|
31
31
|
method: 'GET',
|
|
@@ -40,19 +40,19 @@ test('trust proxy protocol', (t) => {
|
|
|
40
40
|
t.plan(5)
|
|
41
41
|
const fastify = Fastify({ trustProxy: true })
|
|
42
42
|
|
|
43
|
-
t.
|
|
43
|
+
t.teardown(fastify.close.bind(fastify))
|
|
44
44
|
|
|
45
45
|
fastify.register(expressPlugin).after(() => {
|
|
46
46
|
fastify.use('/', function (req, res) {
|
|
47
|
-
t.
|
|
48
|
-
t.
|
|
49
|
-
t.
|
|
47
|
+
t.equal(req.ip, '1.1.1.1', 'gets ip from x-forwarded-for')
|
|
48
|
+
t.equal(req.hostname, 'example.com', 'gets hostname from x-forwarded-host')
|
|
49
|
+
t.equal(req.protocol, 'lorem', 'gets protocol from x-forwarded-proto')
|
|
50
50
|
|
|
51
51
|
res.sendStatus(200)
|
|
52
52
|
})
|
|
53
53
|
})
|
|
54
54
|
|
|
55
|
-
fastify.listen(0, (err, address) => {
|
|
55
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
56
56
|
t.error(err)
|
|
57
57
|
sget({
|
|
58
58
|
method: 'GET',
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const Fastify = require('fastify')
|
|
5
|
+
const fastifyFormBody = require('@fastify/formbody')
|
|
6
|
+
const Express = require('express')
|
|
7
|
+
const bodyParser = require('body-parser')
|
|
8
|
+
const sget = require('simple-get').concat
|
|
9
|
+
|
|
10
|
+
const expressPlugin = require('../index')
|
|
11
|
+
|
|
12
|
+
test('POST request without form body works', t => {
|
|
13
|
+
t.plan(5)
|
|
14
|
+
const fastify = Fastify()
|
|
15
|
+
const express = Express()
|
|
16
|
+
t.teardown(fastify.close)
|
|
17
|
+
|
|
18
|
+
fastify.register(fastifyFormBody)
|
|
19
|
+
fastify.register(expressPlugin)
|
|
20
|
+
.after(() => {
|
|
21
|
+
express.use(bodyParser.urlencoded({ extended: false }))
|
|
22
|
+
fastify.use(express)
|
|
23
|
+
fastify.use((req, res, next) => {
|
|
24
|
+
// body-parser default value
|
|
25
|
+
t.same(req.body, {})
|
|
26
|
+
next()
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
fastify.post('/hello', (req, reply) => {
|
|
31
|
+
return { hello: 'world' }
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
35
|
+
t.error(err)
|
|
36
|
+
sget({
|
|
37
|
+
method: 'post',
|
|
38
|
+
url: address + '/hello',
|
|
39
|
+
timeout: 100
|
|
40
|
+
}, (err, res, data) => {
|
|
41
|
+
t.error(err)
|
|
42
|
+
t.equal(res.statusCode, 200)
|
|
43
|
+
t.same(JSON.parse(data), { hello: 'world' })
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('POST request with form body and without body-parser works', t => {
|
|
49
|
+
t.plan(5)
|
|
50
|
+
const fastify = Fastify()
|
|
51
|
+
const express = Express()
|
|
52
|
+
t.teardown(fastify.close)
|
|
53
|
+
|
|
54
|
+
fastify.register(fastifyFormBody)
|
|
55
|
+
fastify.register(expressPlugin)
|
|
56
|
+
.after(() => {
|
|
57
|
+
fastify.use(express)
|
|
58
|
+
fastify.use((req, res, next) => {
|
|
59
|
+
// req.body default value
|
|
60
|
+
t.equal(req.body, undefined)
|
|
61
|
+
next()
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
fastify.post('/hello', (req, reply) => {
|
|
66
|
+
return { hello: 'world' }
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
70
|
+
t.error(err)
|
|
71
|
+
sget({
|
|
72
|
+
method: 'post',
|
|
73
|
+
url: address + '/hello',
|
|
74
|
+
form: { input: 'test' },
|
|
75
|
+
timeout: 100
|
|
76
|
+
}, (err, res, data) => {
|
|
77
|
+
t.error(err)
|
|
78
|
+
t.equal(res.statusCode, 200)
|
|
79
|
+
t.same(JSON.parse(data), { hello: 'world' })
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('POST request with form body and body-parser hangs up', t => {
|
|
85
|
+
t.plan(3)
|
|
86
|
+
const fastify = Fastify()
|
|
87
|
+
const express = Express()
|
|
88
|
+
t.teardown(fastify.close)
|
|
89
|
+
|
|
90
|
+
fastify.register(fastifyFormBody)
|
|
91
|
+
fastify.register(expressPlugin)
|
|
92
|
+
.after(() => {
|
|
93
|
+
express.use(bodyParser.urlencoded({ extended: false }))
|
|
94
|
+
fastify.use(express)
|
|
95
|
+
fastify.use((req, res, next) => {
|
|
96
|
+
// body-parser result
|
|
97
|
+
t.same(req.body, { input: 'test' })
|
|
98
|
+
next()
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
fastify.post('/hello', (req, reply) => {
|
|
103
|
+
return { hello: 'world' }
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
107
|
+
t.error(err)
|
|
108
|
+
sget({
|
|
109
|
+
method: 'post',
|
|
110
|
+
url: address + '/hello',
|
|
111
|
+
form: { input: 'test' },
|
|
112
|
+
timeout: 100
|
|
113
|
+
}, (err, res, data) => {
|
|
114
|
+
t.equal(err.message, 'Request timed out')
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
test('POST request with form body and body-parser hangs up, compatibility case', t => {
|
|
120
|
+
t.plan(5)
|
|
121
|
+
const fastify = Fastify()
|
|
122
|
+
const express = Express()
|
|
123
|
+
t.teardown(fastify.close)
|
|
124
|
+
|
|
125
|
+
fastify.register(fastifyFormBody)
|
|
126
|
+
fastify.register(expressPlugin, { expressHook: 'preHandler' })
|
|
127
|
+
.after(() => {
|
|
128
|
+
fastify.use(express)
|
|
129
|
+
fastify.use((req, res, next) => {
|
|
130
|
+
// fastify-formbody with backward compatibility result
|
|
131
|
+
t.same(req.body, { input: 'test' })
|
|
132
|
+
next()
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
fastify.post('/hello', (req, reply) => {
|
|
137
|
+
return { hello: 'world' }
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
141
|
+
t.error(err)
|
|
142
|
+
sget({
|
|
143
|
+
method: 'post',
|
|
144
|
+
url: address + '/hello',
|
|
145
|
+
form: { input: 'test' },
|
|
146
|
+
timeout: 100
|
|
147
|
+
}, (err, res, data) => {
|
|
148
|
+
t.error(err)
|
|
149
|
+
t.equal(res.statusCode, 200)
|
|
150
|
+
t.same(JSON.parse(data), { hello: 'world' })
|
|
151
|
+
})
|
|
152
|
+
})
|
|
153
|
+
})
|
package/test/hooks.test.js
CHANGED
|
@@ -18,8 +18,8 @@ test('onSend hook should receive valid request and reply objects if middleware f
|
|
|
18
18
|
fastify.decorateReply('testDecorator', 'testDecoratorVal')
|
|
19
19
|
|
|
20
20
|
fastify.addHook('onSend', function (request, reply, payload, next) {
|
|
21
|
-
t.
|
|
22
|
-
t.
|
|
21
|
+
t.equal(request.testDecorator, 'testDecoratorVal')
|
|
22
|
+
t.equal(reply.testDecorator, 'testDecoratorVal')
|
|
23
23
|
next()
|
|
24
24
|
})
|
|
25
25
|
|
|
@@ -32,6 +32,6 @@ test('onSend hook should receive valid request and reply objects if middleware f
|
|
|
32
32
|
url: '/'
|
|
33
33
|
}, (err, res) => {
|
|
34
34
|
t.error(err)
|
|
35
|
-
t.
|
|
35
|
+
t.equal(res.statusCode, 500)
|
|
36
36
|
})
|
|
37
37
|
})
|
package/test/middleware.test.js
CHANGED
|
@@ -30,19 +30,19 @@ test('use a middleware', t => {
|
|
|
30
30
|
reply.send({ hello: 'world' })
|
|
31
31
|
})
|
|
32
32
|
|
|
33
|
-
instance.listen(0, err => {
|
|
33
|
+
instance.listen({ port: 0 }, err => {
|
|
34
34
|
t.error(err)
|
|
35
35
|
|
|
36
|
-
t.
|
|
36
|
+
t.teardown(instance.server.close.bind(instance.server))
|
|
37
37
|
|
|
38
38
|
sget({
|
|
39
39
|
method: 'GET',
|
|
40
40
|
url: 'http://localhost:' + instance.server.address().port
|
|
41
41
|
}, (err, response, body) => {
|
|
42
42
|
t.error(err)
|
|
43
|
-
t.
|
|
44
|
-
t.
|
|
45
|
-
t.
|
|
43
|
+
t.equal(response.statusCode, 200)
|
|
44
|
+
t.equal(response.headers['content-length'], '' + body.length)
|
|
45
|
+
t.same(JSON.parse(body), { hello: 'world' })
|
|
46
46
|
})
|
|
47
47
|
})
|
|
48
48
|
})
|
|
@@ -60,10 +60,10 @@ test('use cors', t => {
|
|
|
60
60
|
reply.send({ hello: 'world' })
|
|
61
61
|
})
|
|
62
62
|
|
|
63
|
-
instance.listen(0, err => {
|
|
63
|
+
instance.listen({ port: 0 }, err => {
|
|
64
64
|
t.error(err)
|
|
65
65
|
|
|
66
|
-
t.
|
|
66
|
+
t.teardown(instance.server.close.bind(instance.server))
|
|
67
67
|
|
|
68
68
|
sget({
|
|
69
69
|
method: 'GET',
|
|
@@ -88,10 +88,10 @@ test('use helmet', t => {
|
|
|
88
88
|
reply.send({ hello: 'world' })
|
|
89
89
|
})
|
|
90
90
|
|
|
91
|
-
instance.listen(0, err => {
|
|
91
|
+
instance.listen({ port: 0 }, err => {
|
|
92
92
|
t.error(err)
|
|
93
93
|
|
|
94
|
-
t.
|
|
94
|
+
t.teardown(instance.server.close.bind(instance.server))
|
|
95
95
|
|
|
96
96
|
sget({
|
|
97
97
|
method: 'GET',
|
|
@@ -117,10 +117,10 @@ test('use helmet and cors', t => {
|
|
|
117
117
|
reply.send({ hello: 'world' })
|
|
118
118
|
})
|
|
119
119
|
|
|
120
|
-
instance.listen(0, err => {
|
|
120
|
+
instance.listen({ port: 0 }, err => {
|
|
121
121
|
t.error(err)
|
|
122
122
|
|
|
123
|
-
t.
|
|
123
|
+
t.teardown(instance.server.close.bind(instance.server))
|
|
124
124
|
|
|
125
125
|
sget({
|
|
126
126
|
method: 'GET',
|
|
@@ -176,9 +176,9 @@ test('middlewares with prefix', t => {
|
|
|
176
176
|
instance.get('/prefix/', handler)
|
|
177
177
|
instance.get('/prefix/inner', handler)
|
|
178
178
|
|
|
179
|
-
instance.listen(0, err => {
|
|
179
|
+
instance.listen({ port: 0 }, err => {
|
|
180
180
|
t.error(err)
|
|
181
|
-
t.
|
|
181
|
+
t.teardown(instance.server.close.bind(instance.server))
|
|
182
182
|
|
|
183
183
|
t.test('/', t => {
|
|
184
184
|
t.plan(2)
|
|
@@ -188,7 +188,7 @@ test('middlewares with prefix', t => {
|
|
|
188
188
|
json: true
|
|
189
189
|
}, (err, response, body) => {
|
|
190
190
|
t.error(err)
|
|
191
|
-
t.
|
|
191
|
+
t.same(body, {
|
|
192
192
|
global: true,
|
|
193
193
|
global2: true,
|
|
194
194
|
root: true
|
|
@@ -204,7 +204,7 @@ test('middlewares with prefix', t => {
|
|
|
204
204
|
json: true
|
|
205
205
|
}, (err, response, body) => {
|
|
206
206
|
t.error(err)
|
|
207
|
-
t.
|
|
207
|
+
t.same(body, {
|
|
208
208
|
prefixed: true,
|
|
209
209
|
global: true,
|
|
210
210
|
global2: true,
|
|
@@ -222,7 +222,7 @@ test('middlewares with prefix', t => {
|
|
|
222
222
|
json: true
|
|
223
223
|
}, (err, response, body) => {
|
|
224
224
|
t.error(err)
|
|
225
|
-
t.
|
|
225
|
+
t.same(body, {
|
|
226
226
|
prefixed: true,
|
|
227
227
|
slashed: true,
|
|
228
228
|
global: true,
|
|
@@ -240,7 +240,7 @@ test('middlewares with prefix', t => {
|
|
|
240
240
|
json: true
|
|
241
241
|
}, (err, response, body) => {
|
|
242
242
|
t.error(err)
|
|
243
|
-
t.
|
|
243
|
+
t.same(body, {
|
|
244
244
|
prefixed: true,
|
|
245
245
|
slashed: true,
|
|
246
246
|
global: true,
|
|
@@ -290,15 +290,15 @@ test('res.end should block middleware execution', t => {
|
|
|
290
290
|
t.fail('we should no be here')
|
|
291
291
|
})
|
|
292
292
|
|
|
293
|
-
instance.listen(0, (err, address) => {
|
|
293
|
+
instance.listen({ port: 0 }, (err, address) => {
|
|
294
294
|
t.error(err)
|
|
295
295
|
sget({
|
|
296
296
|
method: 'GET',
|
|
297
297
|
url: address
|
|
298
298
|
}, (err, res, data) => {
|
|
299
299
|
t.error(err)
|
|
300
|
-
t.
|
|
301
|
-
t.
|
|
300
|
+
t.equal(res.statusCode, 200)
|
|
301
|
+
t.equal(data.toString(), 'hello')
|
|
302
302
|
})
|
|
303
303
|
})
|
|
304
304
|
})
|
|
@@ -331,15 +331,15 @@ test('Use a middleware inside a plugin after an encapsulated plugin', t => {
|
|
|
331
331
|
next()
|
|
332
332
|
}))
|
|
333
333
|
|
|
334
|
-
f.listen(0, (err, address) => {
|
|
334
|
+
f.listen({ port: 0 }, (err, address) => {
|
|
335
335
|
t.error(err)
|
|
336
336
|
sget({
|
|
337
337
|
method: 'GET',
|
|
338
338
|
url: address
|
|
339
339
|
}, (err, res, data) => {
|
|
340
340
|
t.error(err)
|
|
341
|
-
t.
|
|
342
|
-
t.
|
|
341
|
+
t.equal(res.statusCode, 200)
|
|
342
|
+
t.same(JSON.parse(data), { hello: 'world' })
|
|
343
343
|
})
|
|
344
344
|
})
|
|
345
345
|
})
|
|
@@ -352,14 +352,14 @@ test('middlewares should run in the order in which they are defined', t => {
|
|
|
352
352
|
|
|
353
353
|
f.register(fp(function (instance, opts, next) {
|
|
354
354
|
instance.use(function (req, res, next) {
|
|
355
|
-
t.
|
|
355
|
+
t.equal(req.previous, undefined)
|
|
356
356
|
req.previous = 1
|
|
357
357
|
next()
|
|
358
358
|
})
|
|
359
359
|
|
|
360
360
|
instance.register(fp(function (i, opts, next) {
|
|
361
361
|
i.use(function (req, res, next) {
|
|
362
|
-
t.
|
|
362
|
+
t.equal(req.previous, 2)
|
|
363
363
|
req.previous = 3
|
|
364
364
|
next()
|
|
365
365
|
})
|
|
@@ -367,7 +367,7 @@ test('middlewares should run in the order in which they are defined', t => {
|
|
|
367
367
|
}))
|
|
368
368
|
|
|
369
369
|
instance.use(function (req, res, next) {
|
|
370
|
-
t.
|
|
370
|
+
t.equal(req.previous, 1)
|
|
371
371
|
req.previous = 2
|
|
372
372
|
next()
|
|
373
373
|
})
|
|
@@ -377,19 +377,19 @@ test('middlewares should run in the order in which they are defined', t => {
|
|
|
377
377
|
|
|
378
378
|
f.register(function (instance, opts, next) {
|
|
379
379
|
instance.use(function (req, res, next) {
|
|
380
|
-
t.
|
|
380
|
+
t.equal(req.previous, 3)
|
|
381
381
|
req.previous = 4
|
|
382
382
|
next()
|
|
383
383
|
})
|
|
384
384
|
|
|
385
385
|
instance.get('/', function (request, reply) {
|
|
386
|
-
t.
|
|
386
|
+
t.equal(request.raw.previous, 5)
|
|
387
387
|
reply.send({ hello: 'world' })
|
|
388
388
|
})
|
|
389
389
|
|
|
390
390
|
instance.register(fp(function (i, opts, next) {
|
|
391
391
|
i.use(function (req, res, next) {
|
|
392
|
-
t.
|
|
392
|
+
t.equal(req.previous, 4)
|
|
393
393
|
req.previous = 5
|
|
394
394
|
next()
|
|
395
395
|
})
|
|
@@ -399,15 +399,15 @@ test('middlewares should run in the order in which they are defined', t => {
|
|
|
399
399
|
next()
|
|
400
400
|
})
|
|
401
401
|
|
|
402
|
-
f.listen(0, (err, address) => {
|
|
402
|
+
f.listen({ port: 0 }, (err, address) => {
|
|
403
403
|
t.error(err)
|
|
404
404
|
sget({
|
|
405
405
|
method: 'GET',
|
|
406
406
|
url: address
|
|
407
407
|
}, (err, res, data) => {
|
|
408
408
|
t.error(err)
|
|
409
|
-
t.
|
|
410
|
-
t.
|
|
409
|
+
t.equal(res.statusCode, 200)
|
|
410
|
+
t.same(JSON.parse(data), { hello: 'world' })
|
|
411
411
|
})
|
|
412
412
|
})
|
|
413
413
|
})
|