@fastify/express 4.0.2 → 4.0.3
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/dependabot.yml +1 -1
- package/.github/workflows/ci.yml +12 -2
- package/LICENSE +4 -1
- package/README.md +3 -3
- package/index.js +8 -1
- package/package.json +7 -10
- package/test/404s.test.js +83 -114
- package/test/application.test.js +48 -80
- package/test/basic.test.js +157 -284
- package/test/enhance-request.test.js +57 -80
- package/test/form-data.test.js +50 -64
- package/test/hooks.test.js +15 -17
- package/test/middleware.test.js +228 -183
package/test/application.test.js
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test } = require('
|
|
3
|
+
const { test } = require('node:test')
|
|
4
4
|
const Fastify = require('fastify')
|
|
5
5
|
const Express = require('express')
|
|
6
|
-
const sget = require('simple-get').concat
|
|
7
6
|
|
|
8
7
|
const expressPlugin = require('../index')
|
|
9
8
|
|
|
10
|
-
test('Register express application', t => {
|
|
11
|
-
t.plan(
|
|
9
|
+
test('Register express application', async t => {
|
|
10
|
+
t.plan(3)
|
|
12
11
|
const fastify = Fastify()
|
|
13
12
|
const express = Express()
|
|
14
|
-
t.
|
|
13
|
+
t.after(() => fastify.close())
|
|
15
14
|
|
|
16
15
|
express.use(function (_req, res, next) {
|
|
17
16
|
res.setHeader('x-custom', true)
|
|
@@ -26,24 +25,19 @@ test('Register express application', t => {
|
|
|
26
25
|
fastify.register(expressPlugin)
|
|
27
26
|
.after(() => { fastify.use(express) })
|
|
28
27
|
|
|
29
|
-
fastify.listen({ port: 0 }
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
t.equal(res.statusCode, 201)
|
|
37
|
-
t.match(res.headers, { 'x-custom': 'true' })
|
|
38
|
-
t.same(JSON.parse(data), { hello: 'world' })
|
|
39
|
-
})
|
|
40
|
-
})
|
|
28
|
+
const address = await fastify.listen({ port: 0 })
|
|
29
|
+
|
|
30
|
+
const result = await fetch(address + '/hello')
|
|
31
|
+
|
|
32
|
+
t.assert.deepStrictEqual(result.status, 201)
|
|
33
|
+
t.assert.deepStrictEqual(result.headers.get('x-custom'), 'true')
|
|
34
|
+
t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
|
|
41
35
|
})
|
|
42
36
|
|
|
43
|
-
test('Register express application that uses Router', t => {
|
|
44
|
-
t.plan(
|
|
37
|
+
test('Register express application that uses Router', async t => {
|
|
38
|
+
t.plan(6)
|
|
45
39
|
const fastify = Fastify()
|
|
46
|
-
t.
|
|
40
|
+
t.after(() => fastify.close())
|
|
47
41
|
|
|
48
42
|
const router = Express.Router()
|
|
49
43
|
|
|
@@ -65,33 +59,25 @@ test('Register express application that uses Router', t => {
|
|
|
65
59
|
fastify.register(expressPlugin)
|
|
66
60
|
.after(() => { fastify.use(router) })
|
|
67
61
|
|
|
68
|
-
fastify.listen({ port: 0 }
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
url: address + '/foo'
|
|
82
|
-
}, (err, res, data) => {
|
|
83
|
-
t.error(err)
|
|
84
|
-
t.equal(res.statusCode, 400)
|
|
85
|
-
t.match(res.headers, { 'x-custom': 'true' })
|
|
86
|
-
t.same(JSON.parse(data), { foo: 'bar' })
|
|
87
|
-
})
|
|
88
|
-
})
|
|
62
|
+
const address = await fastify.listen({ port: 0 })
|
|
63
|
+
|
|
64
|
+
const result = await fetch(address + '/hello')
|
|
65
|
+
|
|
66
|
+
t.assert.deepStrictEqual(result.status, 201)
|
|
67
|
+
t.assert.deepStrictEqual(result.headers.get('x-custom'), 'true')
|
|
68
|
+
t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
|
|
69
|
+
|
|
70
|
+
const result2 = await fetch(address + '/foo')
|
|
71
|
+
|
|
72
|
+
t.assert.deepStrictEqual(result2.status, 400)
|
|
73
|
+
t.assert.deepStrictEqual(result2.headers.get('x-custom'), 'true')
|
|
74
|
+
t.assert.deepStrictEqual(await result2.json(), { foo: 'bar' })
|
|
89
75
|
})
|
|
90
76
|
|
|
91
|
-
test('Should remove x-powered-by header', t => {
|
|
92
|
-
t.plan(
|
|
77
|
+
test('Should remove x-powered-by header', async t => {
|
|
78
|
+
t.plan(1)
|
|
93
79
|
const fastify = Fastify()
|
|
94
|
-
t.
|
|
80
|
+
t.after(() => fastify.close())
|
|
95
81
|
|
|
96
82
|
const router = Express.Router()
|
|
97
83
|
|
|
@@ -104,22 +90,16 @@ test('Should remove x-powered-by header', t => {
|
|
|
104
90
|
.register(expressPlugin)
|
|
105
91
|
.after(() => { fastify.use(router) })
|
|
106
92
|
|
|
107
|
-
fastify.listen({ port: 0 }
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
url: address
|
|
112
|
-
}, (err, res) => {
|
|
113
|
-
t.error(err)
|
|
114
|
-
t.equal(res.headers['x-powered-by'], undefined)
|
|
115
|
-
})
|
|
116
|
-
})
|
|
93
|
+
const address = await fastify.listen({ port: 0 })
|
|
94
|
+
|
|
95
|
+
const result = await fetch(address)
|
|
96
|
+
t.assert.deepStrictEqual(result.headers.get('x-powered-by'), null)
|
|
117
97
|
})
|
|
118
98
|
|
|
119
|
-
test('Should expose the express app on the fastify instance', t => {
|
|
120
|
-
t.plan(
|
|
99
|
+
test('Should expose the express app on the fastify instance', async t => {
|
|
100
|
+
t.plan(1)
|
|
121
101
|
const fastify = Fastify()
|
|
122
|
-
t.
|
|
102
|
+
t.after(() => fastify.close())
|
|
123
103
|
|
|
124
104
|
const router = Express.Router()
|
|
125
105
|
|
|
@@ -132,22 +112,16 @@ test('Should expose the express app on the fastify instance', t => {
|
|
|
132
112
|
.register(expressPlugin)
|
|
133
113
|
.after(() => { fastify.use(router) })
|
|
134
114
|
|
|
135
|
-
fastify.listen({ port: 0 }
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
url: address
|
|
140
|
-
}, (err) => {
|
|
141
|
-
t.error(err)
|
|
142
|
-
t.equal(fastify.express.disabled('x-powered-by'), true)
|
|
143
|
-
})
|
|
144
|
-
})
|
|
115
|
+
const address = await fastify.listen({ port: 0 })
|
|
116
|
+
|
|
117
|
+
await fetch(address)
|
|
118
|
+
t.assert.deepStrictEqual(fastify.express.disabled('x-powered-by'), true)
|
|
145
119
|
})
|
|
146
120
|
|
|
147
|
-
test('Should flush headers if express handles request', t => {
|
|
148
|
-
t.plan(
|
|
121
|
+
test('Should flush headers if express handles request', async t => {
|
|
122
|
+
t.plan(1)
|
|
149
123
|
const fastify = Fastify()
|
|
150
|
-
t.
|
|
124
|
+
t.after(() => fastify.close())
|
|
151
125
|
|
|
152
126
|
fastify.addHook('onRequest', (_, reply, done) => {
|
|
153
127
|
reply.header('foo', 'bar')
|
|
@@ -166,14 +140,8 @@ test('Should flush headers if express handles request', t => {
|
|
|
166
140
|
.register(expressPlugin)
|
|
167
141
|
.after(() => { fastify.use(router) })
|
|
168
142
|
|
|
169
|
-
fastify.listen({ port: 0 }
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
url: address
|
|
174
|
-
}, (err, res) => {
|
|
175
|
-
t.error(err)
|
|
176
|
-
t.equal(res.headers.foo, 'bar')
|
|
177
|
-
})
|
|
178
|
-
})
|
|
143
|
+
const address = await fastify.listen({ port: 0 })
|
|
144
|
+
|
|
145
|
+
const result = await fetch(address)
|
|
146
|
+
t.assert.deepStrictEqual(result.headers.get('foo'), 'bar')
|
|
179
147
|
})
|