@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,90 +1,80 @@
1
1
  'use strict'
2
2
 
3
- const { test } = require('tap')
3
+ const { test } = require('node:test')
4
4
  const Fastify = require('fastify')
5
- const sget = require('simple-get').concat
6
5
 
7
6
  const expressPlugin = require('../index')
8
7
 
9
- test('Should enhance the Node.js core request/response objects', t => {
10
- t.plan(10)
8
+ test('Should enhance the Node.js core request/response objects', async t => {
9
+ t.plan(8)
11
10
  const fastify = Fastify()
12
- t.teardown(fastify.close)
11
+ t.after(() => fastify.close())
13
12
 
14
13
  fastify.register(expressPlugin)
15
14
 
16
15
  fastify.get('/', async (req, reply) => {
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
- t.ok(req.raw.log)
24
- t.ok(reply.raw.log)
16
+ t.assert.deepStrictEqual(req.raw.originalUrl, req.raw.url)
17
+ t.assert.deepStrictEqual(req.raw.id, req.id)
18
+ t.assert.deepStrictEqual(req.raw.hostname, req.hostname)
19
+ t.assert.deepStrictEqual(req.raw.protocol, req.protocol)
20
+ t.assert.deepStrictEqual(req.raw.ip, req.ip)
21
+ t.assert.deepStrictEqual(req.raw.ips, req.ips)
22
+ t.assert.ok(req.raw.log)
23
+ t.assert.ok(reply.raw.log)
25
24
  return { hello: 'world' }
26
25
  })
27
26
 
28
- fastify.listen({ port: 0 }, (err, address) => {
29
- t.error(err)
30
- sget({
31
- method: 'GET',
32
- url: address
33
- }, (err, res, data) => {
34
- t.error(err)
35
- })
36
- })
27
+ const address = await fastify.listen({ port: 0 })
28
+
29
+ await fetch(address)
37
30
  })
38
31
 
39
- test('trust proxy protocol', (t) => {
40
- t.plan(5)
32
+ test('trust proxy protocol', async (t) => {
33
+ t.plan(3)
41
34
  const fastify = Fastify({ trustProxy: true })
42
35
 
43
- t.teardown(fastify.close.bind(fastify))
36
+ t.after(() => fastify.close())
44
37
 
45
38
  fastify.register(expressPlugin).after(() => {
46
39
  fastify.use('/', function (req, res) {
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')
40
+ t.assert.deepStrictEqual(req.ip, '1.1.1.1', 'gets ip from x-forwarded-for')
41
+ t.assert.deepStrictEqual(req.hostname, 'example.com', 'gets hostname from x-forwarded-host')
42
+ t.assert.deepStrictEqual(req.protocol, 'lorem', 'gets protocol from x-forwarded-proto')
50
43
 
51
44
  res.sendStatus(200)
52
45
  })
53
46
  })
54
47
 
55
- fastify.listen({ port: 0 }, (err, address) => {
56
- t.error(err)
57
- sget({
58
- method: 'GET',
59
- headers: {
60
- 'X-Forwarded-For': '1.1.1.1',
61
- 'X-Forwarded-Host': 'example.com',
62
- 'X-Forwarded-Proto': 'lorem'
63
- },
64
- url: address
65
- }, (err, res, data) => {
66
- t.error(err)
67
- })
48
+ const address = await fastify.listen({ port: 0 })
49
+
50
+ await fetch(address, {
51
+ headers: {
52
+ 'X-Forwarded-For': '1.1.1.1',
53
+ 'X-Forwarded-Host': 'example.com',
54
+ 'X-Forwarded-Proto': 'lorem'
55
+ }
68
56
  })
69
57
  })
70
58
 
71
- test('passing createProxyHandler sets up a Proxy with Express req', t => {
72
- t.plan(8)
59
+ test('passing createProxyHandler sets up a Proxy with Express req', async t => {
60
+ t.plan(6)
73
61
  const testString = 'test proxy'
74
62
 
75
63
  const fastify = Fastify()
64
+ t.after(() => fastify.close())
65
+
76
66
  fastify.register(expressPlugin, {
77
67
  createProxyHandler: () => ({
78
68
  set (target, prop, value) {
79
69
  if (prop === 'customField') {
80
- t.equal(value, testString)
70
+ t.assert.deepStrictEqual(value, testString)
81
71
  }
82
72
 
83
73
  return Reflect.set(target, prop, value)
84
74
  },
85
75
  get (target, prop) {
86
76
  if (prop === 'customField') {
87
- t.pass('get customField called')
77
+ t.assert.ok('get customField called')
88
78
  }
89
79
 
90
80
  return target[prop]
@@ -92,39 +82,33 @@ test('passing createProxyHandler sets up a Proxy with Express req', t => {
92
82
  })
93
83
  })
94
84
  .after(() => {
95
- fastify.use(function (req, res, next) {
85
+ fastify.use(function (req, _res, next) {
96
86
  req.customField = testString
97
- t.equal(req.customField, testString)
87
+ t.assert.deepStrictEqual(req.customField, testString)
98
88
  next()
99
89
  })
100
90
  })
101
91
 
102
- fastify.get('/', function (request, reply) {
92
+ fastify.get('/', function (_request, reply) {
103
93
  reply.send({ hello: 'world' })
104
94
  })
105
95
 
106
- fastify.listen({ port: 0 }, err => {
107
- t.error(err)
96
+ const address = await fastify.listen({ port: 0 })
108
97
 
109
- t.teardown(fastify.server.close.bind(fastify.server))
98
+ const response = await fetch(address)
110
99
 
111
- sget({
112
- method: 'GET',
113
- url: 'http://localhost:' + fastify.server.address().port
114
- }, (err, response, body) => {
115
- t.error(err)
116
- t.equal(response.statusCode, 200)
117
- t.equal(response.headers['content-length'], '' + body.length)
118
- t.same(JSON.parse(body), { hello: 'world' })
119
- })
120
- })
100
+ const responseText = await response.text()
101
+ t.assert.deepStrictEqual(response.status, 200)
102
+ t.assert.deepStrictEqual(response.headers.get('content-length'), '' + responseText.length)
103
+ t.assert.deepStrictEqual(JSON.parse(responseText), { hello: 'world' })
121
104
  })
122
105
 
123
- test('createProxyHandler has access to Fastify request object', t => {
124
- t.plan(12)
106
+ test('createProxyHandler has access to Fastify request object', async t => {
107
+ t.plan(10)
125
108
  const startTestString = 'original'
126
109
 
127
110
  const fastify = Fastify()
111
+ t.after(() => fastify.close())
128
112
  fastify.decorateRequest('getAndSetFastify', startTestString)
129
113
  fastify.decorateRequest('getOnlyFastify', startTestString)
130
114
 
@@ -132,7 +116,7 @@ test('createProxyHandler has access to Fastify request object', t => {
132
116
  createProxyHandler: fastifyReq => ({
133
117
  set (target, prop, value) {
134
118
  if (prop === 'getAndSetFastify') {
135
- t.pass('set to Fastify called')
119
+ t.assert.ok('set to Fastify called')
136
120
  return Reflect.set(fastifyReq, prop, value)
137
121
  } else if (prop === 'getOnlyFastify') {
138
122
  return true
@@ -143,7 +127,7 @@ test('createProxyHandler has access to Fastify request object', t => {
143
127
  get (target, prop) {
144
128
  if (prop === 'getAndSetFastify' || prop === 'getOnlyFastify') {
145
129
  // Return something from Fastify req
146
- t.pass('get from Fastify called')
130
+ t.assert.ok('get from Fastify called')
147
131
  return fastifyReq[prop]
148
132
  }
149
133
 
@@ -152,9 +136,9 @@ test('createProxyHandler has access to Fastify request object', t => {
152
136
  })
153
137
  })
154
138
  .after(() => {
155
- fastify.use(function (req, res, next) {
156
- t.equal(req.getAndSetFastify, startTestString)
157
- t.equal(req.getOnlyFastify, startTestString)
139
+ fastify.use(function (req, _res, next) {
140
+ t.assert.deepStrictEqual(req.getAndSetFastify, startTestString)
141
+ t.assert.deepStrictEqual(req.getOnlyFastify, startTestString)
158
142
  req.getAndSetFastify = 'updated'
159
143
  req.getOnlyFastify = 'updated'
160
144
  next()
@@ -163,25 +147,18 @@ test('createProxyHandler has access to Fastify request object', t => {
163
147
 
164
148
  fastify.get('/', function (request, reply) {
165
149
  // getOnlyFastify should change and getOnlyFastify should not
166
- t.equal(request.getAndSetFastify, 'updated')
167
- t.equal(request.getOnlyFastify, startTestString)
150
+ t.assert.deepStrictEqual(request.getAndSetFastify, 'updated')
151
+ t.assert.deepStrictEqual(request.getOnlyFastify, startTestString)
168
152
 
169
153
  reply.send({ hello: 'world' })
170
154
  })
171
155
 
172
- fastify.listen({ port: 0 }, err => {
173
- t.error(err)
156
+ const address = await fastify.listen({ port: 0 })
174
157
 
175
- t.teardown(fastify.server.close.bind(fastify.server))
158
+ const response = await fetch(address)
176
159
 
177
- sget({
178
- method: 'GET',
179
- url: 'http://localhost:' + fastify.server.address().port
180
- }, (err, response, body) => {
181
- t.error(err)
182
- t.equal(response.statusCode, 200)
183
- t.equal(response.headers['content-length'], '' + body.length)
184
- t.same(JSON.parse(body), { hello: 'world' })
185
- })
186
- })
160
+ const responseText = await response.text()
161
+ t.assert.deepStrictEqual(response.status, 200)
162
+ t.assert.deepStrictEqual(response.headers.get('content-length'), '' + responseText.length)
163
+ t.assert.deepStrictEqual(JSON.parse(responseText), { hello: 'world' })
187
164
  })
@@ -1,153 +1,139 @@
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 fastifyFormBody = require('@fastify/formbody')
6
6
  const Express = require('express')
7
7
  const bodyParser = require('body-parser')
8
- const sget = require('simple-get').concat
9
8
 
10
9
  const expressPlugin = require('../index')
11
10
 
12
- test('POST request without form body works', t => {
13
- t.plan(5)
11
+ test('POST request without form body works', async t => {
12
+ t.plan(3)
14
13
  const fastify = Fastify()
15
14
  const express = Express()
16
- t.teardown(fastify.close)
15
+ t.after(() => fastify.close())
17
16
 
18
17
  fastify.register(fastifyFormBody)
19
18
  fastify.register(expressPlugin)
20
19
  .after(() => {
21
20
  express.use(bodyParser.urlencoded({ extended: false }))
22
21
  fastify.use(express)
23
- fastify.use((req, res, next) => {
22
+ fastify.use((req, _res, next) => {
24
23
  // body-parser default value
25
- t.same(req.body, {})
24
+ t.assert.deepStrictEqual(req.body, {})
26
25
  next()
27
26
  })
28
27
  })
29
28
 
30
- fastify.post('/hello', (req, reply) => {
29
+ fastify.post('/hello', () => {
31
30
  return { hello: 'world' }
32
31
  })
33
32
 
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
- })
33
+ const address = await fastify.listen({ port: 0 })
34
+
35
+ const result = await fetch(address + '/hello', {
36
+ method: 'post',
37
+ signal: AbortSignal.timeout(100)
45
38
  })
39
+
40
+ t.assert.deepStrictEqual(result.status, 200)
41
+ t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
46
42
  })
47
43
 
48
- test('POST request with form body and without body-parser works', t => {
49
- t.plan(5)
44
+ test('POST request with form body and without body-parser works', async t => {
45
+ t.plan(3)
50
46
  const fastify = Fastify()
51
47
  const express = Express()
52
- t.teardown(fastify.close)
48
+ t.after(() => fastify.close())
53
49
 
54
50
  fastify.register(fastifyFormBody)
55
51
  fastify.register(expressPlugin)
56
52
  .after(() => {
57
53
  fastify.use(express)
58
- fastify.use((req, res, next) => {
54
+ fastify.use((req, _res, next) => {
59
55
  // req.body default value
60
- t.equal(req.body, undefined)
56
+ t.assert.deepStrictEqual(req.body, undefined)
61
57
  next()
62
58
  })
63
59
  })
64
60
 
65
- fastify.post('/hello', (req, reply) => {
61
+ fastify.post('/hello', () => {
66
62
  return { hello: 'world' }
67
63
  })
68
64
 
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
- })
65
+ const address = await fastify.listen({ port: 0 })
66
+
67
+ const result = await fetch(address + '/hello', {
68
+ method: 'post',
69
+ body: new URLSearchParams({ input: 'test' }),
70
+ signal: AbortSignal.timeout(100)
81
71
  })
72
+
73
+ t.assert.deepStrictEqual(result.status, 200)
74
+ t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
82
75
  })
83
76
 
84
- test('POST request with form body and body-parser hangs up', t => {
85
- t.plan(3)
77
+ test('POST request with form body and body-parser hangs up', async t => {
78
+ t.plan(2)
86
79
  const fastify = Fastify()
87
80
  const express = Express()
88
- t.teardown(fastify.close)
81
+ t.after(() => fastify.close())
89
82
 
90
83
  fastify.register(fastifyFormBody)
91
84
  fastify.register(expressPlugin)
92
85
  .after(() => {
93
86
  express.use(bodyParser.urlencoded({ extended: false }))
94
87
  fastify.use(express)
95
- fastify.use((req, res, next) => {
88
+ fastify.use((req, _res, next) => {
96
89
  // body-parser result
97
- t.same(req.body, { input: 'test' })
90
+ t.assert.deepStrictEqual(req.body, { input: 'test' })
98
91
  next()
99
92
  })
100
93
  })
101
94
 
102
- fastify.post('/hello', (req, reply) => {
95
+ fastify.post('/hello', () => {
103
96
  return { hello: 'world' }
104
97
  })
105
98
 
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
- })
99
+ const address = await fastify.listen({ port: 0 })
100
+
101
+ await t.assert.rejects(() => fetch(address + '/hello', {
102
+ method: 'post',
103
+ body: new URLSearchParams({ input: 'test' }),
104
+ signal: AbortSignal.timeout(5)
105
+ }), 'Request timed out')
117
106
  })
118
107
 
119
- test('POST request with form body and body-parser hangs up, compatibility case', t => {
120
- t.plan(5)
108
+ test('POST request with form body and body-parser hangs up, compatibility case', async t => {
109
+ t.plan(3)
121
110
  const fastify = Fastify()
122
111
  const express = Express()
123
- t.teardown(fastify.close)
112
+ t.after(() => fastify.close())
124
113
 
125
114
  fastify.register(fastifyFormBody)
126
115
  fastify.register(expressPlugin, { expressHook: 'preHandler' })
127
116
  .after(() => {
128
117
  fastify.use(express)
129
- fastify.use((req, res, next) => {
118
+ fastify.use((req, _res, next) => {
130
119
  // fastify-formbody with backward compatibility result
131
- t.same(req.body, { input: 'test' })
120
+ t.assert.deepStrictEqual(req.body.input, 'test')
132
121
  next()
133
122
  })
134
123
  })
135
124
 
136
- fastify.post('/hello', (req, reply) => {
125
+ fastify.post('/hello', () => {
137
126
  return { hello: 'world' }
138
127
  })
139
128
 
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
- })
129
+ const address = await fastify.listen({ port: 0 })
130
+
131
+ const result = await fetch(address + '/hello', {
132
+ method: 'post',
133
+ body: new URLSearchParams({ input: 'test' }),
134
+ signal: AbortSignal.timeout(100)
152
135
  })
136
+
137
+ t.assert.deepStrictEqual(result.status, 200)
138
+ t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
153
139
  })
@@ -1,16 +1,16 @@
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
6
  const expressPlugin = require('../index')
7
7
 
8
- test('onSend hook should receive valid request and reply objects if middleware fails', t => {
9
- t.plan(4)
8
+ test('onSend hook should receive valid request and reply objects if middleware fails', async t => {
9
+ t.plan(3)
10
10
  const fastify = Fastify()
11
11
  fastify.register(expressPlugin)
12
12
  .after(() => {
13
- fastify.use(function (req, res, next) {
13
+ fastify.use(function (_req, _res, next) {
14
14
  next(new Error('middlware failed'))
15
15
  })
16
16
  })
@@ -18,37 +18,36 @@ test('onSend hook should receive valid request and reply objects if middleware f
18
18
  fastify.decorateRequest('testDecorator', 'testDecoratorVal')
19
19
  fastify.decorateReply('testDecorator', 'testDecoratorVal')
20
20
 
21
- fastify.addHook('onSend', function (request, reply, payload, next) {
22
- t.equal(request.testDecorator, 'testDecoratorVal')
23
- t.equal(reply.testDecorator, 'testDecoratorVal')
21
+ fastify.addHook('onSend', function (request, reply, _payload, next) {
22
+ t.assert.deepStrictEqual(request.testDecorator, 'testDecoratorVal')
23
+ t.assert.deepStrictEqual(reply.testDecorator, 'testDecoratorVal')
24
24
  next()
25
25
  })
26
26
 
27
- fastify.get('/', (req, reply) => {
27
+ fastify.get('/', (_req, reply) => {
28
28
  reply.send('hello')
29
29
  })
30
30
 
31
- fastify.inject({
31
+ const result = await fastify.inject({
32
32
  method: 'GET',
33
33
  url: '/'
34
- }, (err, res) => {
35
- t.error(err)
36
- t.equal(res.statusCode, 500)
37
34
  })
35
+
36
+ t.assert.deepStrictEqual(result.statusCode, 500)
38
37
  })
39
38
 
40
- test('request.url is not mutated between onRequest and onResponse', t => {
41
- t.plan(4)
39
+ test('request.url is not mutated between onRequest and onResponse', async t => {
40
+ t.plan(3)
42
41
  const fastify = Fastify()
43
42
  const targetUrl = '/hubba/bubba'
44
43
 
45
44
  fastify.addHook('onRequest', (request, _, next) => {
46
- t.equal(request.url, targetUrl)
45
+ t.assert.deepStrictEqual(request.url, targetUrl)
47
46
  next()
48
47
  })
49
48
 
50
49
  fastify.addHook('onResponse', (request, _, next) => {
51
- t.equal(request.url, targetUrl)
50
+ t.assert.deepStrictEqual(request.url, targetUrl)
52
51
  next()
53
52
  })
54
53
 
@@ -56,17 +55,16 @@ test('request.url is not mutated between onRequest and onResponse', t => {
56
55
  const mainRouter = express.Router()
57
56
  const innerRouter = express.Router()
58
57
  mainRouter.use('/hubba', innerRouter)
59
- innerRouter.get('/bubba', (req, res) => {
58
+ innerRouter.get('/bubba', (_req, res) => {
60
59
  res.sendStatus(200)
61
60
  })
62
61
  fastify.use(mainRouter)
63
62
  })
64
63
 
65
- fastify.inject({
64
+ const result = await fastify.inject({
66
65
  method: 'GET',
67
66
  url: targetUrl
68
- }, (err, res) => {
69
- t.error(err)
70
- t.equal(res.statusCode, 200)
71
67
  })
68
+
69
+ t.assert.deepStrictEqual(result.statusCode, 200)
72
70
  })