fastify 5.3.3 → 5.4.0

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.
Files changed (70) hide show
  1. package/README.md +2 -0
  2. package/build/build-validation.js +2 -1
  3. package/docs/Guides/Delay-Accepting-Requests.md +3 -3
  4. package/docs/Guides/Ecosystem.md +9 -5
  5. package/docs/Reference/ContentTypeParser.md +1 -1
  6. package/docs/Reference/Errors.md +2 -2
  7. package/docs/Reference/Hooks.md +14 -14
  8. package/docs/Reference/Logging.md +3 -3
  9. package/docs/Reference/Middleware.md +1 -1
  10. package/docs/Reference/Reply.md +8 -8
  11. package/docs/Reference/Request.md +1 -1
  12. package/docs/Reference/Routes.md +3 -3
  13. package/docs/Reference/Server.md +35 -21
  14. package/docs/Reference/Validation-and-Serialization.md +1 -1
  15. package/fastify.d.ts +2 -1
  16. package/fastify.js +14 -2
  17. package/lib/configValidator.js +1 -1
  18. package/lib/errors.js +6 -0
  19. package/lib/pluginOverride.js +3 -1
  20. package/lib/reply.js +7 -11
  21. package/lib/request.js +3 -10
  22. package/lib/symbols.js +1 -0
  23. package/lib/warnings.js +8 -0
  24. package/package.json +8 -4
  25. package/test/404s.test.js +226 -325
  26. package/test/allow-unsafe-regex.test.js +19 -48
  27. package/test/als.test.js +28 -40
  28. package/test/async-await.test.js +11 -2
  29. package/test/body-limit.test.js +41 -65
  30. package/test/build-certificate.js +1 -1
  31. package/test/custom-parser-async.test.js +17 -22
  32. package/test/decorator-namespace.test._js_ +3 -4
  33. package/test/diagnostics-channel/async-delay-request.test.js +7 -16
  34. package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
  35. package/test/helper.js +1 -1
  36. package/test/hooks-async.test.js +248 -218
  37. package/test/hooks.test.js +910 -769
  38. package/test/http-methods/lock.test.js +31 -31
  39. package/test/http-methods/mkcol.test.js +5 -9
  40. package/test/http-methods/proppatch.test.js +23 -29
  41. package/test/http-methods/report.test.js +44 -69
  42. package/test/http-methods/search.test.js +67 -82
  43. package/test/http2/closing.test.js +38 -20
  44. package/test/http2/secure-with-fallback.test.js +28 -27
  45. package/test/https/https.test.js +56 -53
  46. package/test/internals/errors.test.js +1 -1
  47. package/test/internals/handle-request.test.js +49 -66
  48. package/test/issue-4959.test.js +12 -3
  49. package/test/listen.4.test.js +31 -43
  50. package/test/nullable-validation.test.js +33 -46
  51. package/test/output-validation.test.js +24 -26
  52. package/test/plugin.2.test.js +104 -86
  53. package/test/plugin.3.test.js +56 -35
  54. package/test/plugin.4.test.js +124 -119
  55. package/test/proto-poisoning.test.js +78 -97
  56. package/test/request-error.test.js +0 -46
  57. package/test/route-hooks.test.js +112 -92
  58. package/test/route-prefix.test.js +194 -133
  59. package/test/schema-serialization.test.js +177 -154
  60. package/test/schema-special-usage.test.js +165 -132
  61. package/test/schema-validation.test.js +242 -205
  62. package/test/set-error-handler.test.js +58 -1
  63. package/test/skip-reply-send.test.js +64 -69
  64. package/test/trust-proxy.test.js +32 -58
  65. package/test/types/fastify.test-d.ts +3 -0
  66. package/test/types/request.test-d.ts +1 -0
  67. package/test/url-rewriting.test.js +45 -62
  68. package/types/request.d.ts +1 -0
  69. package/.taprc +0 -7
  70. package/.vscode/settings.json +0 -22
@@ -1,7 +1,6 @@
1
1
  'use strict'
2
2
 
3
3
  const { test } = require('node:test')
4
- const sget = require('simple-get').concat
5
4
  const Fastify = require('..')
6
5
 
7
6
  test('nullable string', (t, done) => {
@@ -52,8 +51,8 @@ test('nullable string', (t, done) => {
52
51
  })
53
52
  })
54
53
 
55
- test('object or null body', (t, done) => {
56
- t.plan(5)
54
+ test('object or null body', async (t) => {
55
+ t.plan(4)
57
56
 
58
57
  const fastify = Fastify()
59
58
 
@@ -88,24 +87,20 @@ test('object or null body', (t, done) => {
88
87
  }
89
88
  })
90
89
 
91
- fastify.listen({ port: 0 }, (err) => {
92
- t.assert.ifError(err)
93
- t.after(() => { fastify.close() })
94
-
95
- sget({
96
- method: 'POST',
97
- url: 'http://localhost:' + fastify.server.address().port
98
- }, (err, response, body) => {
99
- t.assert.ifError(err)
100
- t.assert.strictEqual(response.statusCode, 200)
101
- t.assert.deepStrictEqual(JSON.parse(body), { isUndefinedBody: true })
102
- done()
103
- })
90
+ const fastifyServer = await fastify.listen({ port: 0 })
91
+ t.after(() => { fastify.close() })
92
+
93
+ const result = await fetch(fastifyServer, {
94
+ method: 'POST'
104
95
  })
96
+
97
+ t.assert.ok(result.ok)
98
+ t.assert.strictEqual(result.status, 200)
99
+ t.assert.deepStrictEqual(await result.json(), { isUndefinedBody: true })
105
100
  })
106
101
 
107
- test('nullable body', (t, done) => {
108
- t.plan(5)
102
+ test('nullable body', async (t) => {
103
+ t.plan(4)
109
104
 
110
105
  const fastify = Fastify()
111
106
 
@@ -141,24 +136,20 @@ test('nullable body', (t, done) => {
141
136
  }
142
137
  })
143
138
 
144
- fastify.listen({ port: 0 }, (err) => {
145
- t.assert.ifError(err)
146
- t.after(() => fastify.close())
147
-
148
- sget({
149
- method: 'POST',
150
- url: 'http://localhost:' + fastify.server.address().port
151
- }, (err, response, body) => {
152
- t.assert.ifError(err)
153
- t.assert.strictEqual(response.statusCode, 200)
154
- t.assert.deepStrictEqual(JSON.parse(body), { isUndefinedBody: true })
155
- done()
156
- })
139
+ const fastifyServer = await fastify.listen({ port: 0 })
140
+ t.after(() => fastify.close())
141
+
142
+ const result = await fetch(fastifyServer, {
143
+ method: 'POST'
157
144
  })
145
+
146
+ t.assert.ok(result.ok)
147
+ t.assert.strictEqual(result.status, 200)
148
+ t.assert.deepStrictEqual(await result.json(), { isUndefinedBody: true })
158
149
  })
159
150
 
160
- test('Nullable body with 204', (t, done) => {
161
- t.plan(5)
151
+ test('Nullable body with 204', async (t) => {
152
+ t.plan(4)
162
153
 
163
154
  const fastify = Fastify()
164
155
 
@@ -183,18 +174,14 @@ test('Nullable body with 204', (t, done) => {
183
174
  }
184
175
  })
185
176
 
186
- fastify.listen({ port: 0 }, (err) => {
187
- t.assert.ifError(err)
188
- t.after(() => fastify.close())
189
-
190
- sget({
191
- method: 'POST',
192
- url: 'http://localhost:' + fastify.server.address().port
193
- }, (err, response, body) => {
194
- t.assert.ifError(err)
195
- t.assert.strictEqual(response.statusCode, 204)
196
- t.assert.strictEqual(body.length, 0)
197
- done()
198
- })
177
+ const fastifyServer = await fastify.listen({ port: 0 })
178
+ t.after(() => fastify.close())
179
+
180
+ const result = await fetch(fastifyServer, {
181
+ method: 'POST'
199
182
  })
183
+
184
+ t.assert.ok(result.ok)
185
+ t.assert.strictEqual(result.status, 204)
186
+ t.assert.strictEqual((await result.text()).length, 0)
200
187
  })
@@ -1,7 +1,6 @@
1
1
  'use strict'
2
2
 
3
3
  const { test } = require('node:test')
4
- const sget = require('simple-get').concat
5
4
  const fastify = require('..')()
6
5
 
7
6
  const opts = {
@@ -90,37 +89,33 @@ test('unlisted response code', t => {
90
89
  })
91
90
 
92
91
  test('start server and run tests', async (t) => {
93
- await fastify.listen({ port: 0 })
94
- const baseUrl = 'http://localhost:' + fastify.server.address().port
92
+ const fastifyServer = await fastify.listen({ port: 0 })
95
93
  t.after(() => fastify.close())
96
94
 
97
- function sgetAsync (opts) {
98
- return new Promise((resolve, reject) => {
99
- sget(opts, (err, res, body) => {
100
- if (err) return reject(err)
101
- resolve({ res, body })
102
- })
103
- })
104
- }
105
-
106
95
  await test('shorthand - string get ok', async (t) => {
107
- const { res, body } = await sgetAsync({ method: 'GET', url: `${baseUrl}/string` })
108
- t.assert.strictEqual(res.statusCode, 200)
109
- t.assert.strictEqual(res.headers['content-length'], '' + body.length)
96
+ const result = await fetch(fastifyServer + '/string')
97
+ t.assert.ok(result.ok)
98
+ t.assert.strictEqual(result.status, 200)
99
+ const body = await result.text()
100
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
110
101
  t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
111
102
  })
112
103
 
113
104
  await test('shorthand - number get ok', async (t) => {
114
- const { res, body } = await sgetAsync({ method: 'GET', url: `${baseUrl}/number` })
115
- t.assert.strictEqual(res.statusCode, 201)
116
- t.assert.strictEqual(res.headers['content-length'], '' + body.length)
105
+ const result = await fetch(fastifyServer + '/number')
106
+ t.assert.ok(result.ok)
107
+ t.assert.strictEqual(result.status, 201)
108
+ const body = await result.text()
109
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
117
110
  t.assert.deepStrictEqual(JSON.parse(body), { hello: 55 })
118
111
  })
119
112
 
120
113
  await test('shorthand - wrong-object-for-schema', async (t) => {
121
- const { res, body } = await sgetAsync({ method: 'GET', url: `${baseUrl}/wrong-object-for-schema` })
122
- t.assert.strictEqual(res.statusCode, 500)
123
- t.assert.strictEqual(res.headers['content-length'], '' + body.length)
114
+ const result = await fetch(fastifyServer + '/wrong-object-for-schema')
115
+ t.assert.ok(!result.ok)
116
+ t.assert.strictEqual(result.status, 500)
117
+ const body = await result.text()
118
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
124
119
  t.assert.deepStrictEqual(JSON.parse(body), {
125
120
  statusCode: 500,
126
121
  error: 'Internal Server Error',
@@ -129,14 +124,17 @@ test('start server and run tests', async (t) => {
129
124
  })
130
125
 
131
126
  await test('shorthand - empty', async (t) => {
132
- const { res } = await sgetAsync({ method: 'GET', url: `${baseUrl}/empty` })
133
- t.assert.strictEqual(res.statusCode, 204)
127
+ const result = await fetch(fastifyServer + '/empty')
128
+ t.assert.ok(result.ok)
129
+ t.assert.strictEqual(result.status, 204)
134
130
  })
135
131
 
136
132
  await test('shorthand - 400', async (t) => {
137
- const { res, body } = await sgetAsync({ method: 'GET', url: `${baseUrl}/400` })
138
- t.assert.strictEqual(res.statusCode, 400)
139
- t.assert.strictEqual(res.headers['content-length'], '' + body.length)
133
+ const result = await fetch(fastifyServer + '/400')
134
+ t.assert.ok(!result.ok)
135
+ t.assert.strictEqual(result.status, 400)
136
+ const body = await result.text()
137
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
140
138
  t.assert.deepStrictEqual(JSON.parse(body), { hello: 'DOOM' })
141
139
  })
142
140
  })
@@ -1,36 +1,36 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const test = t.test
3
+ const { test } = require('node:test')
5
4
  const Fastify = require('../fastify')
6
5
  const sget = require('simple-get').concat
7
6
  const fp = require('fastify-plugin')
7
+ const { waitForCb } = require('./toolkit')
8
8
 
9
- test('check dependencies - should not throw', t => {
9
+ test('check dependencies - should not throw', (t, testDone) => {
10
10
  t.plan(12)
11
11
  const fastify = Fastify()
12
12
 
13
13
  fastify.register((instance, opts, done) => {
14
14
  instance.register(fp((i, o, n) => {
15
15
  i.decorate('test', () => {})
16
- t.ok(i.test)
16
+ t.assert.ok(i.test)
17
17
  n()
18
18
  }))
19
19
 
20
20
  instance.register(fp((i, o, n) => {
21
21
  try {
22
22
  i.decorate('otherTest', () => {}, ['test'])
23
- t.ok(i.test)
24
- t.ok(i.otherTest)
23
+ t.assert.ok(i.test)
24
+ t.assert.ok(i.otherTest)
25
25
  n()
26
26
  } catch (e) {
27
- t.fail()
27
+ t.assert.fail()
28
28
  }
29
29
  }))
30
30
 
31
31
  instance.get('/', (req, reply) => {
32
- t.ok(instance.test)
33
- t.ok(instance.otherTest)
32
+ t.assert.ok(instance.test)
33
+ t.assert.ok(instance.otherTest)
34
34
  reply.send({ hello: 'world' })
35
35
  })
36
36
 
@@ -38,27 +38,28 @@ test('check dependencies - should not throw', t => {
38
38
  })
39
39
 
40
40
  fastify.ready(() => {
41
- t.notOk(fastify.test)
42
- t.notOk(fastify.otherTest)
41
+ t.assert.ok(!fastify.test)
42
+ t.assert.ok(!fastify.otherTest)
43
43
  })
44
44
 
45
45
  fastify.listen({ port: 0 }, err => {
46
- t.error(err)
47
- t.teardown(() => { fastify.close() })
46
+ t.assert.ifError(err)
47
+ t.after(() => { fastify.close() })
48
48
 
49
49
  sget({
50
50
  method: 'GET',
51
51
  url: 'http://localhost:' + fastify.server.address().port
52
52
  }, (err, response, body) => {
53
- t.error(err)
54
- t.equal(response.statusCode, 200)
55
- t.equal(response.headers['content-length'], '' + body.length)
56
- t.same(JSON.parse(body), { hello: 'world' })
53
+ t.assert.ifError(err)
54
+ t.assert.strictEqual(response.statusCode, 200)
55
+ t.assert.strictEqual(response.headers['content-length'], '' + body.length)
56
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
57
+ testDone()
57
58
  })
58
59
  })
59
60
  })
60
61
 
61
- test('check dependencies - should throw', t => {
62
+ test('check dependencies - should throw', (t, testDone) => {
62
63
  t.plan(12)
63
64
  const fastify = Fastify()
64
65
 
@@ -66,24 +67,24 @@ test('check dependencies - should throw', t => {
66
67
  instance.register(fp((i, o, n) => {
67
68
  try {
68
69
  i.decorate('otherTest', () => {}, ['test'])
69
- t.fail()
70
+ t.assert.fail()
70
71
  } catch (e) {
71
- t.equal(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')
72
- t.equal(e.message, 'The decorator is missing dependency \'test\'.')
72
+ t.assert.strictEqual(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')
73
+ t.assert.strictEqual(e.message, 'The decorator is missing dependency \'test\'.')
73
74
  }
74
75
  n()
75
76
  }))
76
77
 
77
78
  instance.register(fp((i, o, n) => {
78
79
  i.decorate('test', () => {})
79
- t.ok(i.test)
80
- t.notOk(i.otherTest)
80
+ t.assert.ok(i.test)
81
+ t.assert.ok(!i.otherTest)
81
82
  n()
82
83
  }))
83
84
 
84
85
  instance.get('/', (req, reply) => {
85
- t.ok(instance.test)
86
- t.notOk(instance.otherTest)
86
+ t.assert.ok(instance.test)
87
+ t.assert.ok(!instance.otherTest)
87
88
  reply.send({ hello: 'world' })
88
89
  })
89
90
 
@@ -91,183 +92,192 @@ test('check dependencies - should throw', t => {
91
92
  })
92
93
 
93
94
  fastify.ready(() => {
94
- t.notOk(fastify.test)
95
+ t.assert.ok(!fastify.test)
95
96
  })
96
97
 
97
98
  fastify.listen({ port: 0 }, err => {
98
- t.error(err)
99
- t.teardown(() => { fastify.close() })
99
+ t.assert.ifError(err)
100
+ t.after(() => { fastify.close() })
100
101
 
101
102
  sget({
102
103
  method: 'GET',
103
104
  url: 'http://localhost:' + fastify.server.address().port
104
105
  }, (err, response, body) => {
105
- t.error(err)
106
- t.equal(response.statusCode, 200)
107
- t.equal(response.headers['content-length'], '' + body.length)
108
- t.same(JSON.parse(body), { hello: 'world' })
106
+ t.assert.ifError(err)
107
+ t.assert.strictEqual(response.statusCode, 200)
108
+ t.assert.strictEqual(response.headers['content-length'], '' + body.length)
109
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
110
+ testDone()
109
111
  })
110
112
  })
111
113
  })
112
114
 
113
- test('set the plugin name based on the plugin displayName symbol', t => {
115
+ test('set the plugin name based on the plugin displayName symbol', (t, testDone) => {
114
116
  t.plan(6)
115
117
  const fastify = Fastify()
118
+ t.after(() => fastify.close())
116
119
 
117
120
  fastify.register(fp((fastify, opts, done) => {
118
- t.equal(fastify.pluginName, 'fastify -> plugin-A')
121
+ t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A')
119
122
  fastify.register(fp((fastify, opts, done) => {
120
- t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB')
123
+ t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB')
121
124
  done()
122
125
  }, { name: 'plugin-AB' }))
123
126
  fastify.register(fp((fastify, opts, done) => {
124
- t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
127
+ t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
125
128
  done()
126
129
  }, { name: 'plugin-AC' }))
127
130
  done()
128
131
  }, { name: 'plugin-A' }))
129
132
 
130
133
  fastify.register(fp((fastify, opts, done) => {
131
- t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC -> plugin-B')
134
+ t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC -> plugin-B')
132
135
  done()
133
136
  }, { name: 'plugin-B' }))
134
137
 
135
- t.equal(fastify.pluginName, 'fastify')
138
+ t.assert.strictEqual(fastify.pluginName, 'fastify')
136
139
 
137
140
  fastify.listen({ port: 0 }, err => {
138
- t.error(err)
139
- fastify.close()
141
+ t.assert.ifError(err)
142
+ testDone()
140
143
  })
141
144
  })
142
145
 
143
- test('plugin name will change when using no encapsulation', t => {
146
+ test('plugin name will change when using no encapsulation', (t, testDone) => {
144
147
  t.plan(6)
145
148
  const fastify = Fastify()
149
+ t.after(() => fastify.close())
146
150
 
147
151
  fastify.register(fp((fastify, opts, done) => {
148
152
  // store it in a different variable will hold the correct name
149
153
  const pluginName = fastify.pluginName
150
154
  fastify.register(fp((fastify, opts, done) => {
151
- t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB')
155
+ t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB')
152
156
  done()
153
157
  }, { name: 'plugin-AB' }))
154
158
  fastify.register(fp((fastify, opts, done) => {
155
- t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
159
+ t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
156
160
  done()
157
161
  }, { name: 'plugin-AC' }))
158
162
  setImmediate(() => {
159
163
  // normally we would expect the name plugin-A
160
164
  // but we operate on the same instance in each plugin
161
- t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
162
- t.equal(pluginName, 'fastify -> plugin-A')
165
+ t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
166
+ t.assert.strictEqual(pluginName, 'fastify -> plugin-A')
163
167
  })
164
168
  done()
165
169
  }, { name: 'plugin-A' }))
166
170
 
167
- t.equal(fastify.pluginName, 'fastify')
171
+ t.assert.strictEqual(fastify.pluginName, 'fastify')
168
172
 
169
173
  fastify.listen({ port: 0 }, err => {
170
- t.error(err)
171
- fastify.close()
174
+ t.assert.ifError(err)
175
+ testDone()
172
176
  })
173
177
  })
174
178
 
175
- test('plugin name is undefined when accessing in no plugin context', t => {
179
+ test('plugin name is undefined when accessing in no plugin context', (t, testDone) => {
176
180
  t.plan(2)
177
181
  const fastify = Fastify()
182
+ t.after(() => fastify.close())
178
183
 
179
- t.equal(fastify.pluginName, 'fastify')
184
+ t.assert.strictEqual(fastify.pluginName, 'fastify')
180
185
 
181
186
  fastify.listen({ port: 0 }, err => {
182
- t.error(err)
183
- fastify.close()
187
+ t.assert.ifError(err)
188
+ testDone()
184
189
  })
185
190
  })
186
191
 
187
- test('set the plugin name based on the plugin function name', t => {
192
+ test('set the plugin name based on the plugin function name', (t, testDone) => {
188
193
  t.plan(5)
189
194
  const fastify = Fastify()
195
+ t.after(() => fastify.close())
190
196
 
191
197
  fastify.register(function myPluginA (fastify, opts, done) {
192
- t.equal(fastify.pluginName, 'myPluginA')
198
+ t.assert.strictEqual(fastify.pluginName, 'myPluginA')
193
199
  fastify.register(function myPluginAB (fastify, opts, done) {
194
- t.equal(fastify.pluginName, 'myPluginAB')
200
+ t.assert.strictEqual(fastify.pluginName, 'myPluginAB')
195
201
  done()
196
202
  })
197
203
  setImmediate(() => {
198
204
  // exact name due to encapsulation
199
- t.equal(fastify.pluginName, 'myPluginA')
205
+ t.assert.strictEqual(fastify.pluginName, 'myPluginA')
200
206
  })
201
207
  done()
202
208
  })
203
209
 
204
210
  fastify.register(function myPluginB (fastify, opts, done) {
205
- t.equal(fastify.pluginName, 'myPluginB')
211
+ t.assert.strictEqual(fastify.pluginName, 'myPluginB')
206
212
  done()
207
213
  })
208
214
 
209
215
  fastify.listen({ port: 0 }, err => {
210
- t.error(err)
211
- fastify.close()
216
+ t.assert.ifError(err)
217
+ testDone()
212
218
  })
213
219
  })
214
220
 
215
- test('approximate a plugin name when no meta data is available', t => {
221
+ test('approximate a plugin name when no meta data is available', (t, testDone) => {
216
222
  t.plan(7)
217
223
  const fastify = Fastify()
224
+ t.after(() => fastify.close())
218
225
 
219
226
  fastify.register((fastify, opts, done) => {
220
227
  // A
221
- t.equal(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
222
- t.equal(fastify.pluginName.includes('// A'), true)
228
+ t.assert.strictEqual(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
229
+ t.assert.strictEqual(fastify.pluginName.includes('// A'), true)
223
230
  fastify.register((fastify, opts, done) => {
224
231
  // B
225
- t.equal(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
226
- t.equal(fastify.pluginName.includes('// B'), true)
232
+ t.assert.strictEqual(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
233
+ t.assert.strictEqual(fastify.pluginName.includes('// B'), true)
227
234
  done()
228
235
  })
229
236
  setImmediate(() => {
230
- t.equal(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
231
- t.equal(fastify.pluginName.includes('// A'), true)
237
+ t.assert.strictEqual(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
238
+ t.assert.strictEqual(fastify.pluginName.includes('// A'), true)
232
239
  })
233
240
  done()
234
241
  })
235
242
 
236
243
  fastify.listen({ port: 0 }, err => {
237
- t.error(err)
238
- fastify.close()
244
+ t.assert.ifError(err)
245
+ testDone()
239
246
  })
240
247
  })
241
248
 
242
- test('approximate a plugin name also when fastify-plugin has no meta data', t => {
249
+ test('approximate a plugin name also when fastify-plugin has no meta data', (t, testDone) => {
243
250
  t.plan(4)
244
251
  const fastify = Fastify()
252
+ t.after(() => fastify.close())
253
+
245
254
  // plugin name is got from current file name
246
255
  const pluginName = /plugin\.2\.test/
247
256
  const pluginNameWithFunction = /plugin\.2\.test-auto-\d+ -> B/
248
257
 
249
258
  fastify.register(fp((fastify, opts, done) => {
250
- t.match(fastify.pluginName, pluginName)
259
+ t.assert.match(fastify.pluginName, pluginName)
251
260
  fastify.register(fp(function B (fastify, opts, done) {
252
261
  // function has name
253
- t.match(fastify.pluginName, pluginNameWithFunction)
262
+ t.assert.match(fastify.pluginName, pluginNameWithFunction)
254
263
  done()
255
264
  }))
256
265
  setImmediate(() => {
257
- t.match(fastify.pluginName, pluginNameWithFunction)
266
+ t.assert.match(fastify.pluginName, pluginNameWithFunction)
258
267
  })
259
268
  done()
260
269
  }))
261
270
 
262
271
  fastify.listen({ port: 0 }, err => {
263
- t.error(err)
264
- fastify.close()
272
+ t.assert.ifError(err)
273
+ testDone()
265
274
  })
266
275
  })
267
276
 
268
- test('plugin encapsulation', t => {
277
+ test('plugin encapsulation', (t, testDone) => {
269
278
  t.plan(10)
270
279
  const fastify = Fastify()
280
+ t.after(() => fastify.close())
271
281
 
272
282
  fastify.register((instance, opts, done) => {
273
283
  instance.register(fp((i, o, n) => {
@@ -296,31 +306,39 @@ test('plugin encapsulation', t => {
296
306
  })
297
307
 
298
308
  fastify.ready(() => {
299
- t.notOk(fastify.test)
309
+ t.assert.ok(!fastify.test)
300
310
  })
301
311
 
302
312
  fastify.listen({ port: 0 }, err => {
303
- t.error(err)
304
- t.teardown(() => { fastify.close() })
313
+ t.assert.ifError(err)
314
+ t.after(() => { fastify.close() })
315
+
316
+ const completion = waitForCb({
317
+ steps: 2
318
+ })
305
319
 
306
320
  sget({
307
321
  method: 'GET',
308
322
  url: 'http://localhost:' + fastify.server.address().port + '/first'
309
323
  }, (err, response, body) => {
310
- t.error(err)
311
- t.equal(response.statusCode, 200)
312
- t.equal(response.headers['content-length'], '' + body.length)
313
- t.same(JSON.parse(body), { plugin: 'first' })
324
+ t.assert.ifError(err)
325
+ t.assert.strictEqual(response.statusCode, 200)
326
+ t.assert.strictEqual(response.headers['content-length'], '' + body.length)
327
+ t.assert.deepStrictEqual(JSON.parse(body), { plugin: 'first' })
328
+ completion.stepIn()
314
329
  })
315
330
 
316
331
  sget({
317
332
  method: 'GET',
318
333
  url: 'http://localhost:' + fastify.server.address().port + '/second'
319
334
  }, (err, response, body) => {
320
- t.error(err)
321
- t.equal(response.statusCode, 200)
322
- t.equal(response.headers['content-length'], '' + body.length)
323
- t.same(JSON.parse(body), { plugin: 'second' })
335
+ t.assert.ifError(err)
336
+ t.assert.strictEqual(response.statusCode, 200)
337
+ t.assert.strictEqual(response.headers['content-length'], '' + body.length)
338
+ t.assert.deepStrictEqual(JSON.parse(body), { plugin: 'second' })
339
+ completion.stepIn()
324
340
  })
341
+
342
+ completion.patience.then(testDone)
325
343
  })
326
344
  })