@fastify/express 4.0.1 → 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.
@@ -1,24 +1,23 @@
1
1
  'use strict'
2
2
 
3
- const { test } = require('tap')
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(5)
9
+ test('Register express application', async t => {
10
+ t.plan(3)
12
11
  const fastify = Fastify()
13
12
  const express = Express()
14
- t.teardown(fastify.close)
13
+ t.after(() => fastify.close())
15
14
 
16
- express.use(function (req, res, next) {
15
+ express.use(function (_req, res, next) {
17
16
  res.setHeader('x-custom', true)
18
17
  next()
19
18
  })
20
19
 
21
- express.get('/hello', (req, res) => {
20
+ express.get('/hello', (_req, res) => {
22
21
  res.status(201)
23
22
  res.json({ hello: 'world' })
24
23
  })
@@ -26,38 +25,33 @@ test('Register express application', t => {
26
25
  fastify.register(expressPlugin)
27
26
  .after(() => { fastify.use(express) })
28
27
 
29
- fastify.listen({ port: 0 }, (err, address) => {
30
- t.error(err)
31
- sget({
32
- method: 'GET',
33
- url: address + '/hello'
34
- }, (err, res, data) => {
35
- t.error(err)
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(9)
37
+ test('Register express application that uses Router', async t => {
38
+ t.plan(6)
45
39
  const fastify = Fastify()
46
- t.teardown(fastify.close)
40
+ t.after(() => fastify.close())
47
41
 
48
42
  const router = Express.Router()
49
43
 
50
- router.use(function (req, res, next) {
44
+ router.use(function (_req, res, next) {
51
45
  res.setHeader('x-custom', true)
52
46
  next()
53
47
  })
54
48
 
55
- router.get('/hello', (req, res) => {
49
+ router.get('/hello', (_req, res) => {
56
50
  res.status(201)
57
51
  res.json({ hello: 'world' })
58
52
  })
59
53
 
60
- router.get('/foo', (req, res) => {
54
+ router.get('/foo', (_req, res) => {
61
55
  res.status(400)
62
56
  res.json({ foo: 'bar' })
63
57
  })
@@ -65,37 +59,29 @@ 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 }, (err, address) => {
69
- t.error(err)
70
- sget({
71
- method: 'GET',
72
- url: address + '/hello'
73
- }, (err, res, data) => {
74
- t.error(err)
75
- t.equal(res.statusCode, 201)
76
- t.match(res.headers, { 'x-custom': 'true' })
77
- t.same(JSON.parse(data), { hello: 'world' })
78
- })
79
- sget({
80
- method: 'GET',
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(3)
77
+ test('Should remove x-powered-by header', async t => {
78
+ t.plan(1)
93
79
  const fastify = Fastify()
94
- t.teardown(fastify.close)
80
+ t.after(() => fastify.close())
95
81
 
96
82
  const router = Express.Router()
97
83
 
98
- router.get('/', (req, res) => {
84
+ router.get('/', (_req, res) => {
99
85
  res.status(201)
100
86
  res.json({ hello: 'world' })
101
87
  })
@@ -104,26 +90,20 @@ 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 }, (err, address) => {
108
- t.error(err)
109
- sget({
110
- method: 'GET',
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(3)
99
+ test('Should expose the express app on the fastify instance', async t => {
100
+ t.plan(1)
121
101
  const fastify = Fastify()
122
- t.teardown(fastify.close)
102
+ t.after(() => fastify.close())
123
103
 
124
104
  const router = Express.Router()
125
105
 
126
- router.get('/', (req, res) => {
106
+ router.get('/', (_req, res) => {
127
107
  res.status(201)
128
108
  res.json({ hello: 'world' })
129
109
  })
@@ -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 }, (err, address) => {
136
- t.error(err)
137
- sget({
138
- method: 'GET',
139
- url: address
140
- }, (err, res) => {
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(3)
121
+ test('Should flush headers if express handles request', async t => {
122
+ t.plan(1)
149
123
  const fastify = Fastify()
150
- t.teardown(fastify.close)
124
+ t.after(() => fastify.close())
151
125
 
152
126
  fastify.addHook('onRequest', (_, reply, done) => {
153
127
  reply.header('foo', 'bar')
@@ -157,7 +131,7 @@ test('Should flush headers if express handles request', t => {
157
131
 
158
132
  const router = Express.Router()
159
133
 
160
- router.get('/', (req, res) => {
134
+ router.get('/', (_req, res) => {
161
135
  res.status(201)
162
136
  res.json({ hello: 'world' })
163
137
  })
@@ -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 }, (err, address) => {
170
- t.error(err)
171
- sget({
172
- method: 'GET',
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
  })