@fastify/express 4.0.2 → 4.0.4

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,100 @@
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) => {
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) => {
66
- t.error(err)
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
+ }
56
+ })
57
+ })
58
+
59
+ test('query params still in in the request url after decodeURI', async (t) => {
60
+ t.plan(2)
61
+ const fastify = Fastify()
62
+
63
+ t.after(() => fastify.close())
64
+
65
+ fastify.register(expressPlugin).after(() => {
66
+ fastify.use('/admin', function (req, res) {
67
+ t.assert.deepStrictEqual(req.originalUrl, '/%61dmin?test=%76alue&test2=%76alue', 'originalUrl is not decoded')
68
+ t.assert.deepStrictEqual(req.url, '/admin?test=value&test2=value', 'url is decoded')
69
+
70
+ res.sendStatus(200)
67
71
  })
68
72
  })
73
+
74
+ const address = await fastify.listen({ port: 0 })
75
+
76
+ await fetch(`${address}/%61dmin?test=%76alue&test2=%76alue`)
69
77
  })
70
78
 
71
- test('passing createProxyHandler sets up a Proxy with Express req', t => {
72
- t.plan(8)
79
+ test('passing createProxyHandler sets up a Proxy with Express req', async t => {
80
+ t.plan(6)
73
81
  const testString = 'test proxy'
74
82
 
75
83
  const fastify = Fastify()
84
+ t.after(() => fastify.close())
85
+
76
86
  fastify.register(expressPlugin, {
77
87
  createProxyHandler: () => ({
78
88
  set (target, prop, value) {
79
89
  if (prop === 'customField') {
80
- t.equal(value, testString)
90
+ t.assert.deepStrictEqual(value, testString)
81
91
  }
82
92
 
83
93
  return Reflect.set(target, prop, value)
84
94
  },
85
95
  get (target, prop) {
86
96
  if (prop === 'customField') {
87
- t.pass('get customField called')
97
+ t.assert.ok('get customField called')
88
98
  }
89
99
 
90
100
  return target[prop]
@@ -94,7 +104,7 @@ test('passing createProxyHandler sets up a Proxy with Express req', t => {
94
104
  .after(() => {
95
105
  fastify.use(function (req, _res, next) {
96
106
  req.customField = testString
97
- t.equal(req.customField, testString)
107
+ t.assert.deepStrictEqual(req.customField, testString)
98
108
  next()
99
109
  })
100
110
  })
@@ -103,28 +113,22 @@ test('passing createProxyHandler sets up a Proxy with Express req', t => {
103
113
  reply.send({ hello: 'world' })
104
114
  })
105
115
 
106
- fastify.listen({ port: 0 }, err => {
107
- t.error(err)
116
+ const address = await fastify.listen({ port: 0 })
108
117
 
109
- t.teardown(fastify.server.close.bind(fastify.server))
118
+ const response = await fetch(address)
110
119
 
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
- })
120
+ const responseText = await response.text()
121
+ t.assert.deepStrictEqual(response.status, 200)
122
+ t.assert.deepStrictEqual(response.headers.get('content-length'), '' + responseText.length)
123
+ t.assert.deepStrictEqual(JSON.parse(responseText), { hello: 'world' })
121
124
  })
122
125
 
123
- test('createProxyHandler has access to Fastify request object', t => {
124
- t.plan(12)
126
+ test('createProxyHandler has access to Fastify request object', async t => {
127
+ t.plan(10)
125
128
  const startTestString = 'original'
126
129
 
127
130
  const fastify = Fastify()
131
+ t.after(() => fastify.close())
128
132
  fastify.decorateRequest('getAndSetFastify', startTestString)
129
133
  fastify.decorateRequest('getOnlyFastify', startTestString)
130
134
 
@@ -132,7 +136,7 @@ test('createProxyHandler has access to Fastify request object', t => {
132
136
  createProxyHandler: fastifyReq => ({
133
137
  set (target, prop, value) {
134
138
  if (prop === 'getAndSetFastify') {
135
- t.pass('set to Fastify called')
139
+ t.assert.ok('set to Fastify called')
136
140
  return Reflect.set(fastifyReq, prop, value)
137
141
  } else if (prop === 'getOnlyFastify') {
138
142
  return true
@@ -143,7 +147,7 @@ test('createProxyHandler has access to Fastify request object', t => {
143
147
  get (target, prop) {
144
148
  if (prop === 'getAndSetFastify' || prop === 'getOnlyFastify') {
145
149
  // Return something from Fastify req
146
- t.pass('get from Fastify called')
150
+ t.assert.ok('get from Fastify called')
147
151
  return fastifyReq[prop]
148
152
  }
149
153
 
@@ -153,8 +157,8 @@ test('createProxyHandler has access to Fastify request object', t => {
153
157
  })
154
158
  .after(() => {
155
159
  fastify.use(function (req, _res, next) {
156
- t.equal(req.getAndSetFastify, startTestString)
157
- t.equal(req.getOnlyFastify, startTestString)
160
+ t.assert.deepStrictEqual(req.getAndSetFastify, startTestString)
161
+ t.assert.deepStrictEqual(req.getOnlyFastify, startTestString)
158
162
  req.getAndSetFastify = 'updated'
159
163
  req.getOnlyFastify = 'updated'
160
164
  next()
@@ -163,25 +167,18 @@ test('createProxyHandler has access to Fastify request object', t => {
163
167
 
164
168
  fastify.get('/', function (request, reply) {
165
169
  // getOnlyFastify should change and getOnlyFastify should not
166
- t.equal(request.getAndSetFastify, 'updated')
167
- t.equal(request.getOnlyFastify, startTestString)
170
+ t.assert.deepStrictEqual(request.getAndSetFastify, 'updated')
171
+ t.assert.deepStrictEqual(request.getOnlyFastify, startTestString)
168
172
 
169
173
  reply.send({ hello: 'world' })
170
174
  })
171
175
 
172
- fastify.listen({ port: 0 }, err => {
173
- t.error(err)
176
+ const address = await fastify.listen({ port: 0 })
174
177
 
175
- t.teardown(fastify.server.close.bind(fastify.server))
178
+ const response = await fetch(address)
176
179
 
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
- })
180
+ const responseText = await response.text()
181
+ t.assert.deepStrictEqual(response.status, 200)
182
+ t.assert.deepStrictEqual(response.headers.get('content-length'), '' + responseText.length)
183
+ t.assert.deepStrictEqual(JSON.parse(responseText), { hello: 'world' })
187
184
  })
@@ -1,19 +1,18 @@
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)
@@ -22,7 +21,7 @@ test('POST request without form body works', t => {
22
21
  fastify.use(express)
23
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
  })
@@ -31,25 +30,22 @@ test('POST request without form body works', t => {
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)
@@ -57,7 +53,7 @@ test('POST request with form body and without body-parser works', t => {
57
53
  fastify.use(express)
58
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
  })
@@ -66,61 +62,56 @@ test('POST request with form body and without body-parser works', t => {
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 => {
77
+ test('POST request with form body and body-parser hangs up', async t => {
85
78
  t.plan(3)
79
+
86
80
  const fastify = Fastify()
87
81
  const express = Express()
88
- t.teardown(fastify.close)
82
+ t.after(() => fastify.close())
89
83
 
90
84
  fastify.register(fastifyFormBody)
91
85
  fastify.register(expressPlugin)
92
86
  .after(() => {
93
87
  express.use(bodyParser.urlencoded({ extended: false }))
94
88
  fastify.use(express)
95
- fastify.use((req, _res, next) => {
96
- // body-parser result
97
- t.same(req.body, { input: 'test' })
98
- next()
99
- })
89
+
90
+ fastify.use((_req, _res, next) => next())
100
91
  })
101
92
 
102
- fastify.post('/hello', () => {
103
- return { hello: 'world' }
104
- })
93
+ fastify.post('/hello', () => ({ hello: 'world' }))
94
+ const address = await fastify.listen({ port: 0 })
105
95
 
106
- fastify.listen({ port: 0 }, (err, address) => {
107
- t.error(err)
108
- sget({
96
+ await t.assert.rejects(
97
+ () => fetch(address + '/hello', {
109
98
  method: 'post',
110
- url: address + '/hello',
111
- form: { input: 'test' },
112
- timeout: 100
113
- }, (err) => {
114
- t.equal(err.message, 'Request timed out')
115
- })
116
- })
99
+ body: new URLSearchParams({ input: 'test' }),
100
+ signal: AbortSignal.timeout(200)
101
+ }),
102
+ (err) => {
103
+ t.assert.equal(err?.name, 'TimeoutError')
104
+ t.assert.ok(err)
105
+ return true
106
+ }
107
+ )
117
108
  })
118
109
 
119
- test('POST request with form body and body-parser hangs up, compatibility case', t => {
120
- t.plan(5)
110
+ test('POST request with form body and body-parser hangs up, compatibility case', async t => {
111
+ t.plan(3)
121
112
  const fastify = Fastify()
122
113
  const express = Express()
123
- t.teardown(fastify.close)
114
+ t.after(() => fastify.close())
124
115
 
125
116
  fastify.register(fastifyFormBody)
126
117
  fastify.register(expressPlugin, { expressHook: 'preHandler' })
@@ -128,7 +119,7 @@ test('POST request with form body and body-parser hangs up, compatibility case',
128
119
  fastify.use(express)
129
120
  fastify.use((req, _res, next) => {
130
121
  // fastify-formbody with backward compatibility result
131
- t.same(req.body, { input: 'test' })
122
+ t.assert.deepStrictEqual(req.body.input, 'test')
132
123
  next()
133
124
  })
134
125
  })
@@ -137,17 +128,14 @@ test('POST request with form body and body-parser hangs up, compatibility case',
137
128
  return { hello: 'world' }
138
129
  })
139
130
 
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
- })
131
+ const address = await fastify.listen({ port: 0 })
132
+
133
+ const result = await fetch(address + '/hello', {
134
+ method: 'post',
135
+ body: new URLSearchParams({ input: 'test' }),
136
+ signal: AbortSignal.timeout(100)
152
137
  })
138
+
139
+ t.assert.deepStrictEqual(result.status, 200)
140
+ t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
153
141
  })
@@ -1,12 +1,12 @@
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(() => {
@@ -19,8 +19,8 @@ test('onSend hook should receive valid request and reply objects if middleware f
19
19
  fastify.decorateReply('testDecorator', 'testDecoratorVal')
20
20
 
21
21
  fastify.addHook('onSend', function (request, reply, _payload, next) {
22
- t.equal(request.testDecorator, 'testDecoratorVal')
23
- t.equal(reply.testDecorator, 'testDecoratorVal')
22
+ t.assert.deepStrictEqual(request.testDecorator, 'testDecoratorVal')
23
+ t.assert.deepStrictEqual(reply.testDecorator, 'testDecoratorVal')
24
24
  next()
25
25
  })
26
26
 
@@ -28,27 +28,26 @@ test('onSend hook should receive valid request and reply objects if middleware f
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
 
@@ -62,11 +61,10 @@ test('request.url is not mutated between onRequest and onResponse', t => {
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
  })