fastify 5.3.2 → 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 (103) 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 +16 -7
  5. package/docs/Guides/Serverless.md +28 -69
  6. package/docs/Reference/ContentTypeParser.md +1 -1
  7. package/docs/Reference/Errors.md +2 -4
  8. package/docs/Reference/Hooks.md +14 -14
  9. package/docs/Reference/Logging.md +3 -3
  10. package/docs/Reference/Middleware.md +1 -1
  11. package/docs/Reference/Reply.md +8 -8
  12. package/docs/Reference/Request.md +1 -1
  13. package/docs/Reference/Routes.md +3 -3
  14. package/docs/Reference/Server.md +40 -10
  15. package/docs/Reference/Validation-and-Serialization.md +1 -1
  16. package/eslint.config.js +17 -9
  17. package/fastify.d.ts +2 -1
  18. package/fastify.js +20 -4
  19. package/lib/configValidator.js +1 -1
  20. package/lib/decorate.js +2 -2
  21. package/lib/errors.js +6 -8
  22. package/lib/logger-factory.js +1 -1
  23. package/lib/logger-pino.js +2 -2
  24. package/lib/pluginOverride.js +3 -1
  25. package/lib/reply.js +9 -13
  26. package/lib/request.js +4 -11
  27. package/lib/server.js +30 -51
  28. package/lib/symbols.js +1 -0
  29. package/lib/warnings.js +8 -0
  30. package/package.json +11 -7
  31. package/test/404s.test.js +226 -325
  32. package/test/allow-unsafe-regex.test.js +19 -48
  33. package/test/als.test.js +28 -40
  34. package/test/async-await.test.js +11 -2
  35. package/test/body-limit.test.js +41 -65
  36. package/test/build-certificate.js +1 -1
  37. package/test/close-pipelining.test.js +5 -4
  38. package/test/custom-parser-async.test.js +17 -22
  39. package/test/decorator-namespace.test._js_ +3 -4
  40. package/test/decorator.test.js +422 -341
  41. package/test/diagnostics-channel/async-delay-request.test.js +7 -16
  42. package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
  43. package/test/helper.js +108 -70
  44. package/test/hooks-async.test.js +248 -218
  45. package/test/hooks.on-listen.test.js +255 -239
  46. package/test/hooks.on-ready.test.js +110 -92
  47. package/test/hooks.test.js +910 -769
  48. package/test/http-methods/lock.test.js +31 -31
  49. package/test/http-methods/mkcol.test.js +5 -9
  50. package/test/http-methods/proppatch.test.js +23 -29
  51. package/test/http-methods/report.test.js +44 -69
  52. package/test/http-methods/search.test.js +67 -82
  53. package/test/http2/closing.test.js +38 -20
  54. package/test/http2/secure-with-fallback.test.js +28 -27
  55. package/test/https/https.test.js +56 -53
  56. package/test/inject.test.js +114 -97
  57. package/test/input-validation.js +63 -53
  58. package/test/internals/errors.test.js +0 -10
  59. package/test/internals/handle-request.test.js +49 -66
  60. package/test/internals/hooks.test.js +17 -0
  61. package/test/issue-4959.test.js +14 -5
  62. package/test/listen.4.test.js +31 -43
  63. package/test/logger/response.test.js +19 -20
  64. package/test/nullable-validation.test.js +33 -46
  65. package/test/options.error-handler.test.js +1 -1
  66. package/test/options.test.js +1 -1
  67. package/test/output-validation.test.js +49 -72
  68. package/test/patch.error-handler.test.js +1 -1
  69. package/test/patch.test.js +1 -1
  70. package/test/plugin.1.test.js +71 -60
  71. package/test/plugin.2.test.js +104 -86
  72. package/test/plugin.3.test.js +56 -35
  73. package/test/plugin.4.test.js +124 -119
  74. package/test/promises.test.js +36 -30
  75. package/test/proto-poisoning.test.js +78 -97
  76. package/test/put.error-handler.test.js +1 -1
  77. package/test/put.test.js +1 -1
  78. package/test/reply-error.test.js +169 -148
  79. package/test/reply-trailers.test.js +119 -108
  80. package/test/request-error.test.js +0 -46
  81. package/test/route-hooks.test.js +112 -92
  82. package/test/route-prefix.test.js +194 -133
  83. package/test/schema-feature.test.js +309 -238
  84. package/test/schema-serialization.test.js +177 -154
  85. package/test/schema-special-usage.test.js +165 -132
  86. package/test/schema-validation.test.js +278 -199
  87. package/test/set-error-handler.test.js +58 -1
  88. package/test/skip-reply-send.test.js +64 -69
  89. package/test/stream.1.test.js +30 -27
  90. package/test/stream.2.test.js +20 -10
  91. package/test/stream.3.test.js +37 -31
  92. package/test/trust-proxy.test.js +32 -58
  93. package/test/types/errors.test-d.ts +0 -1
  94. package/test/types/fastify.test-d.ts +3 -0
  95. package/test/types/plugin.test-d.ts +1 -1
  96. package/test/types/register.test-d.ts +1 -1
  97. package/test/types/request.test-d.ts +1 -0
  98. package/test/url-rewriting.test.js +45 -62
  99. package/test/use-semicolon-delimiter.test.js +1 -1
  100. package/types/errors.d.ts +0 -1
  101. package/types/request.d.ts +1 -0
  102. package/.taprc +0 -7
  103. package/test/http2/missing-http2-module.test.js +0 -17
@@ -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('../../fastify')()
6
5
  fastify.addHttpMethod('LOCK', { hasBody: true })
7
6
 
@@ -57,52 +56,53 @@ test('can be created - lock', t => {
57
56
  })
58
57
 
59
58
  test('lock test', async t => {
60
- await fastify.listen({ port: 0 })
59
+ const fastifyServer = await fastify.listen({ port: 0 })
61
60
  t.after(() => {
62
61
  fastify.close()
63
62
  })
64
63
  // the body test uses a text/plain content type instead of application/xml because it requires
65
64
  // a specific content type parser
66
- await t.test('request with body - lock', (t, done) => {
65
+ await t.test('request with body - lock', async (t) => {
67
66
  t.plan(3)
68
- sget({
69
- url: `http://localhost:${fastify.server.address().port}/test/a.txt`,
67
+
68
+ const result = await fetch(`${fastifyServer}/test/a.txt`, {
69
+ method: 'LOCK',
70
70
  headers: { 'content-type': 'text/plain' },
71
- body: bodySample,
72
- method: 'LOCK'
73
- }, (err, response, body) => {
74
- t.assert.ifError(err)
75
- t.assert.strictEqual(response.statusCode, 200)
76
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
77
- done()
71
+ body: bodySample
78
72
  })
73
+
74
+ t.assert.ok(result.ok)
75
+ t.assert.strictEqual(result.status, 200)
76
+ const body = await result.text()
77
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
79
78
  })
80
79
 
81
- await t.test('request with body and no content type (415 error) - lock', (t, done) => {
80
+ await t.test('request with body and no content type (415 error) - lock', async (t) => {
82
81
  t.plan(3)
83
- sget({
84
- url: `http://localhost:${fastify.server.address().port}/test/a.txt`,
82
+
83
+ const result = await fetch(`${fastifyServer}/test/a.txt`, {
84
+ method: 'LOCK',
85
85
  body: bodySample,
86
- method: 'LOCK'
87
- }, (err, response, body) => {
88
- t.assert.ifError(err)
89
- t.assert.strictEqual(response.statusCode, 415)
90
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
91
- done()
86
+ headers: { 'content-type': undefined }
92
87
  })
88
+
89
+ t.assert.ok(!result.ok)
90
+ t.assert.strictEqual(result.status, 415)
91
+ const body = await result.text()
92
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
93
93
  })
94
94
 
95
- await t.test('request without body - lock', (t, done) => {
95
+ await t.test('request without body - lock', async (t) => {
96
96
  t.plan(3)
97
- sget({
98
- url: `http://localhost:${fastify.server.address().port}/test/a.txt`,
99
- headers: { 'content-type': 'text/plain' },
100
- method: 'LOCK'
101
- }, (err, response, body) => {
102
- t.assert.ifError(err)
103
- t.assert.strictEqual(response.statusCode, 200)
104
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
105
- done()
97
+
98
+ const result = await fetch(`${fastifyServer}/test/a.txt`, {
99
+ method: 'LOCK',
100
+ headers: { 'content-type': 'text/plain' }
106
101
  })
102
+
103
+ t.assert.ok(result.ok)
104
+ t.assert.strictEqual(result.status, 200)
105
+ const body = await result.text()
106
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
107
107
  })
108
108
  })
@@ -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
  fastify.addHttpMethod('MKCOL')
7
6
 
@@ -22,18 +21,15 @@ test('can be created - mkcol', t => {
22
21
  })
23
22
 
24
23
  test('mkcol test', async t => {
25
- await fastify.listen({ port: 0 })
24
+ const fastifyServer = await fastify.listen({ port: 0 })
26
25
  t.after(() => { fastify.close() })
27
26
 
28
- await t.test('request - mkcol', (t, done) => {
27
+ await t.test('request - mkcol', async t => {
29
28
  t.plan(2)
30
- sget({
31
- url: `http://localhost:${fastify.server.address().port}/test/`,
29
+ const result = await fetch(`${fastifyServer}/test/`, {
32
30
  method: 'MKCOL'
33
- }, (err, response, body) => {
34
- t.assert.ifError(err)
35
- t.assert.strictEqual(response.statusCode, 201)
36
- done()
37
31
  })
32
+ t.assert.ok(result.ok)
33
+ t.assert.strictEqual(result.status, 201)
38
34
  })
39
35
  })
@@ -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
  fastify.addHttpMethod('PROPPATCH', { hasBody: true })
7
6
 
@@ -62,50 +61,45 @@ test('shorthand - proppatch', t => {
62
61
  })
63
62
 
64
63
  test('proppatch test', async t => {
65
- await fastify.listen({ port: 0 })
64
+ const fastifyServer = await fastify.listen({ port: 0 })
66
65
 
67
66
  t.after(() => { fastify.close() })
68
67
  // the body test uses a text/plain content type instead of application/xml because it requires
69
68
  // a specific content type parser
70
- await t.test('request with body - proppatch', (t, done) => {
69
+ await t.test('request with body - proppatch', async t => {
71
70
  t.plan(3)
72
- sget({
73
- url: `http://localhost:${fastify.server.address().port}/test/a.txt`,
71
+ const result = await fetch(`${fastifyServer}/test/a.txt`, {
72
+ method: 'PROPPATCH',
74
73
  headers: { 'content-type': 'text/plain' },
75
- body: bodySample,
76
- method: 'PROPPATCH'
77
- }, (err, response, body) => {
78
- t.assert.ifError(err)
79
- t.assert.strictEqual(response.statusCode, 207)
80
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
81
- done()
74
+ body: bodySample
82
75
  })
76
+ t.assert.ok(result.ok)
77
+ t.assert.strictEqual(result.status, 207)
78
+ const body = await result.text()
79
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
83
80
  })
84
81
 
85
- await t.test('request with body and no content type (415 error) - proppatch', (t, done) => {
82
+ await t.test('request with body and no content type (415 error) - proppatch', async t => {
86
83
  t.plan(3)
87
- sget({
88
- url: `http://localhost:${fastify.server.address().port}/test/a.txt`,
84
+ const result = await fetch(`${fastifyServer}/test/a.txt`, {
85
+ method: 'PROPPATCH',
89
86
  body: bodySample,
90
- method: 'PROPPATCH'
91
- }, (err, response, body) => {
92
- t.assert.ifError(err)
93
- t.assert.strictEqual(response.statusCode, 415)
94
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
95
- done()
87
+ headers: { 'content-type': undefined }
96
88
  })
89
+ t.assert.ok(!result.ok)
90
+ t.assert.strictEqual(result.status, 415)
91
+ const body = await result.text()
92
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
97
93
  })
98
94
 
99
- await t.test('request without body - proppatch', (t, done) => {
95
+ await t.test('request without body - proppatch', async t => {
100
96
  t.plan(3)
101
- sget({
102
- url: `http://localhost:${fastify.server.address().port}/test/a.txt`,
97
+ const result = await fetch(`${fastifyServer}/test/a.txt`, {
103
98
  method: 'PROPPATCH'
104
- }, (err, response, body) => {
105
- t.assert.ifError(err)
106
- t.assert.strictEqual(response.statusCode, 207)
107
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
108
- done()
109
99
  })
100
+ t.assert.ok(result.ok)
101
+ t.assert.strictEqual(result.status, 207)
102
+ const body = await result.text()
103
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
110
104
  })
111
105
  })
@@ -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('../../fastify')()
6
5
  fastify.addHttpMethod('REPORT', { hasBody: true })
7
6
 
@@ -80,88 +79,64 @@ test('report test', async t => {
80
79
  fastify.close()
81
80
  })
82
81
 
83
- await t.test('request - report', (t, done) => {
82
+ await t.test('request - report', async (t) => {
84
83
  t.plan(3)
85
- sget(
86
- {
87
- url: `http://localhost:${fastify.server.address().port}/`,
88
- method: 'REPORT'
89
- },
90
- (err, response, body) => {
91
- t.assert.ifError(err)
92
- t.assert.strictEqual(response.statusCode, 207)
93
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
94
- done()
95
- }
96
- )
84
+ const result = await fetch(`http://localhost:${fastify.server.address().port}/`, {
85
+ method: 'REPORT'
86
+ })
87
+
88
+ t.assert.ok(result.ok)
89
+ t.assert.strictEqual(result.status, 207)
90
+ t.assert.strictEqual(result.headers.get('content-length'), '' + (await result.text()).length)
97
91
  })
98
92
 
99
- await t.test('request with other path - report', (t, done) => {
93
+ await t.test('request with other path - report', async (t) => {
100
94
  t.plan(3)
101
- sget(
102
- {
103
- url: `http://localhost:${fastify.server.address().port}/test`,
104
- method: 'REPORT'
105
- },
106
- (err, response, body) => {
107
- t.assert.ifError(err)
108
- t.assert.strictEqual(response.statusCode, 207)
109
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
110
- done()
111
- }
112
- )
95
+ const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
96
+ method: 'REPORT'
97
+ })
98
+
99
+ t.assert.ok(result.ok)
100
+ t.assert.strictEqual(result.status, 207)
101
+ t.assert.strictEqual(result.headers.get('content-length'), '' + (await result.text()).length)
113
102
  })
114
103
 
115
104
  // the body test uses a text/plain content type instead of application/xml because it requires
116
105
  // a specific content type parser
117
- await t.test('request with body - report', (t, done) => {
106
+ await t.test('request with body - report', async (t) => {
118
107
  t.plan(3)
119
- sget(
120
- {
121
- url: `http://localhost:${fastify.server.address().port}/test`,
122
- headers: { 'content-type': 'text/plain' },
123
- body: bodySample,
124
- method: 'REPORT'
125
- },
126
- (err, response, body) => {
127
- t.assert.ifError(err)
128
- t.assert.strictEqual(response.statusCode, 207)
129
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
130
- done()
131
- }
132
- )
108
+ const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
109
+ method: 'REPORT',
110
+ headers: { 'content-type': 'text/plain' },
111
+ body: bodySample
112
+ })
113
+
114
+ t.assert.ok(result.ok)
115
+ t.assert.strictEqual(result.status, 207)
116
+ t.assert.strictEqual(result.headers.get('content-length'), '' + (await result.text()).length)
133
117
  })
134
118
 
135
- await t.test('request with body and no content type (415 error) - report', (t, done) => {
119
+ await t.test('request with body and no content type (415 error) - report', async (t) => {
136
120
  t.plan(3)
137
- sget(
138
- {
139
- url: `http://localhost:${fastify.server.address().port}/test`,
140
- body: bodySample,
141
- method: 'REPORT'
142
- },
143
- (err, response, body) => {
144
- t.assert.ifError(err)
145
- t.assert.strictEqual(response.statusCode, 415)
146
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
147
- done()
148
- }
149
- )
121
+ const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
122
+ method: 'REPORT',
123
+ body: bodySample,
124
+ headers: { 'content-type': '' }
125
+ })
126
+
127
+ t.assert.ok(!result.ok)
128
+ t.assert.strictEqual(result.status, 415)
129
+ t.assert.strictEqual(result.headers.get('content-length'), '' + (await result.text()).length)
150
130
  })
151
131
 
152
- await t.test('request without body - report', (t, done) => {
132
+ await t.test('request without body - report', async (t) => {
153
133
  t.plan(3)
154
- sget(
155
- {
156
- url: `http://localhost:${fastify.server.address().port}/test`,
157
- method: 'REPORT'
158
- },
159
- (err, response, body) => {
160
- t.assert.ifError(err)
161
- t.assert.strictEqual(response.statusCode, 207)
162
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
163
- done()
164
- }
165
- )
134
+ const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
135
+ method: 'REPORT'
136
+ })
137
+
138
+ t.assert.ok(result.ok)
139
+ t.assert.strictEqual(result.status, 207)
140
+ t.assert.strictEqual(result.headers.get('content-length'), '' + (await result.text()).length)
166
141
  })
167
142
  })
@@ -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('../../fastify')()
6
5
  fastify.addHttpMethod('SEARCH', { hasBody: true })
7
6
 
@@ -130,119 +129,105 @@ test('search test', async t => {
130
129
  t.after(() => { fastify.close() })
131
130
  const url = `http://localhost:${fastify.server.address().port}`
132
131
 
133
- await t.test('request - search', (t, done) => {
132
+ await t.test('request - search', async t => {
134
133
  t.plan(4)
135
- sget({
136
- method: 'SEARCH',
137
- url
138
- }, (err, response, body) => {
139
- t.assert.ifError(err)
140
- t.assert.strictEqual(response.statusCode, 200)
141
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
142
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
143
- done()
134
+ const result = await fetch(url, {
135
+ method: 'SEARCH'
144
136
  })
137
+ const body = await result.text()
138
+ t.assert.ok(result.ok)
139
+ t.assert.strictEqual(result.status, 200)
140
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
141
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
145
142
  })
146
143
 
147
- await t.test('request search params schema', (t, done) => {
144
+ await t.test('request search params schema', async t => {
148
145
  t.plan(4)
149
- sget({
150
- method: 'SEARCH',
151
- url: `${url}/params/world/123`
152
- }, (err, response, body) => {
153
- t.assert.ifError(err)
154
- t.assert.strictEqual(response.statusCode, 200)
155
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
156
- t.assert.deepStrictEqual(JSON.parse(body), { foo: 'world', test: 123 })
157
- done()
146
+ const result = await fetch(`${url}/params/world/123`, {
147
+ method: 'SEARCH'
158
148
  })
149
+ const body = await result.text()
150
+ t.assert.ok(result.ok)
151
+ t.assert.strictEqual(result.status, 200)
152
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
153
+ t.assert.deepStrictEqual(JSON.parse(body), { foo: 'world', test: 123 })
159
154
  })
160
155
 
161
- await t.test('request search params schema error', (t, done) => {
156
+ await t.test('request search params schema error', async t => {
162
157
  t.plan(3)
163
- sget({
164
- method: 'SEARCH',
165
- url: `${url}/params/world/string`
166
- }, (err, response, body) => {
167
- t.assert.ifError(err)
168
- t.assert.strictEqual(response.statusCode, 400)
169
- t.assert.deepStrictEqual(JSON.parse(body), {
170
- error: 'Bad Request',
171
- code: 'FST_ERR_VALIDATION',
172
- message: 'params/test must be integer',
173
- statusCode: 400
174
- })
175
- done()
158
+ const result = await fetch(`${url}/params/world/string`, {
159
+ method: 'SEARCH'
160
+ })
161
+ const body = await result.text()
162
+ t.assert.strictEqual(result.status, 400)
163
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
164
+ t.assert.deepStrictEqual(JSON.parse(body), {
165
+ error: 'Bad Request',
166
+ code: 'FST_ERR_VALIDATION',
167
+ message: 'params/test must be integer',
168
+ statusCode: 400
176
169
  })
177
170
  })
178
171
 
179
- await t.test('request search querystring schema', (t, done) => {
172
+ await t.test('request search querystring schema', async t => {
180
173
  t.plan(4)
181
- sget({
182
- method: 'SEARCH',
183
- url: `${url}/query?hello=123`
184
- }, (err, response, body) => {
185
- t.assert.ifError(err)
186
- t.assert.strictEqual(response.statusCode, 200)
187
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
188
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 123 })
189
- done()
174
+ const result = await fetch(`${url}/query?hello=123`, {
175
+ method: 'SEARCH'
190
176
  })
177
+ const body = await result.text()
178
+ t.assert.ok(result.ok)
179
+ t.assert.strictEqual(result.status, 200)
180
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
181
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 123 })
191
182
  })
192
183
 
193
- await t.test('request search querystring schema error', (t, done) => {
184
+ await t.test('request search querystring schema error', async t => {
194
185
  t.plan(3)
195
- sget({
196
- method: 'SEARCH',
197
- url: `${url}/query?hello=world`
198
- }, (err, response, body) => {
199
- t.assert.ifError(err)
200
- t.assert.strictEqual(response.statusCode, 400)
201
- t.assert.deepStrictEqual(JSON.parse(body), {
202
- error: 'Bad Request',
203
- code: 'FST_ERR_VALIDATION',
204
- message: 'querystring/hello must be integer',
205
- statusCode: 400
206
- })
207
- done()
186
+ const result = await fetch(`${url}/query?hello=world`, {
187
+ method: 'SEARCH'
188
+ })
189
+ const body = await result.text()
190
+ t.assert.strictEqual(result.status, 400)
191
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
192
+ t.assert.deepStrictEqual(JSON.parse(body), {
193
+ error: 'Bad Request',
194
+ code: 'FST_ERR_VALIDATION',
195
+ message: 'querystring/hello must be integer',
196
+ statusCode: 400
208
197
  })
209
198
  })
210
199
 
211
- await t.test('request search body schema', (t, done) => {
200
+ await t.test('request search body schema', async t => {
212
201
  t.plan(4)
213
202
  const replyBody = { foo: 'bar', test: 5 }
214
- sget({
203
+ const result = await fetch(`${url}/body`, {
215
204
  method: 'SEARCH',
216
- url: `${url}/body`,
217
205
  body: JSON.stringify(replyBody),
218
206
  headers: { 'content-type': 'application/json' }
219
- }, (err, response, body) => {
220
- t.assert.ifError(err)
221
- t.assert.strictEqual(response.statusCode, 200)
222
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
223
- t.assert.deepStrictEqual(JSON.parse(body), replyBody)
224
- done()
225
207
  })
208
+ const body = await result.text()
209
+ t.assert.ok(result.ok)
210
+ t.assert.strictEqual(result.status, 200)
211
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
212
+ t.assert.deepStrictEqual(JSON.parse(body), replyBody)
226
213
  })
227
214
 
228
- await t.test('request search body schema error', (t, done) => {
215
+ await t.test('request search body schema error', async t => {
229
216
  t.plan(4)
230
- sget({
217
+ const result = await fetch(`${url}/body`, {
231
218
  method: 'SEARCH',
232
- url: `${url}/body`,
233
219
  body: JSON.stringify({ foo: 'bar', test: 'test' }),
234
220
  headers: { 'content-type': 'application/json' }
235
- }, (err, response, body) => {
236
- t.assert.ifError(err)
237
- t.assert.strictEqual(response.statusCode, 400)
238
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
239
- t.assert.deepStrictEqual(JSON.parse(body), {
240
- error: 'Bad Request',
241
- code: 'FST_ERR_VALIDATION',
242
- message: 'body/test must be integer',
243
- statusCode: 400
244
- })
245
- done()
221
+ })
222
+ const body = await result.text()
223
+ t.assert.ok(!result.ok)
224
+ t.assert.strictEqual(result.status, 400)
225
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
226
+ t.assert.deepStrictEqual(JSON.parse(body), {
227
+ error: 'Bad Request',
228
+ code: 'FST_ERR_VALIDATION',
229
+ message: 'body/test must be integer',
230
+ statusCode: 400
246
231
  })
247
232
  })
248
233
  })
@@ -11,16 +11,13 @@ const { getServerUrl } = require('../helper')
11
11
 
12
12
  test.before(buildCertificate)
13
13
 
14
- test('http/2 request while fastify closing', (t, done) => {
15
- let fastify
16
- try {
17
- fastify = Fastify({
18
- http2: true
19
- })
20
- t.assert.ok('http2 successfully loaded')
21
- } catch (e) {
22
- t.assert.fail('http2 loading failed')
23
- }
14
+ const isNode24OrGreater = Number(process.versions.node.split('.')[0]) >= 24
15
+
16
+ test('http/2 request while fastify closing Node <24', { skip: isNode24OrGreater }, (t, done) => {
17
+ const fastify = Fastify({
18
+ http2: true
19
+ })
20
+ t.assert.ok('http2 successfully loaded')
24
21
 
25
22
  fastify.get('/', () => Promise.resolve({}))
26
23
 
@@ -53,17 +50,38 @@ test('http/2 request while fastify closing', (t, done) => {
53
50
  })
54
51
  })
55
52
 
56
- test('http/2 request while fastify closing - return503OnClosing: false', (t, done) => {
57
- let fastify
58
- try {
59
- fastify = Fastify({
60
- http2: true,
61
- return503OnClosing: false
53
+ test('http/2 request while fastify closing Node >=24', { skip: !isNode24OrGreater }, (t, done) => {
54
+ const fastify = Fastify({
55
+ http2: true
56
+ })
57
+ t.assert.ok('http2 successfully loaded')
58
+
59
+ fastify.get('/', () => Promise.resolve({}))
60
+
61
+ t.after(() => { fastify.close() })
62
+ fastify.listen({ port: 0 }, err => {
63
+ t.assert.ifError(err)
64
+
65
+ const url = getServerUrl(fastify)
66
+ const session = http2.connect(url, function () {
67
+ session.on('error', () => {
68
+ // Nothing to do here,
69
+ // we are not interested in this error that might
70
+ // happen or not
71
+ })
72
+ session.on('close', () => {
73
+ done()
74
+ })
75
+ fastify.close()
62
76
  })
63
- t.assert.ok('http2 successfully loaded')
64
- } catch (e) {
65
- t.assert.fail('http2 loading failed')
66
- }
77
+ })
78
+ })
79
+
80
+ test('http/2 request while fastify closing - return503OnClosing: false', { skip: isNode24OrGreater }, (t, done) => {
81
+ const fastify = Fastify({
82
+ http2: true,
83
+ return503OnClosing: false
84
+ })
67
85
 
68
86
  t.after(() => { fastify.close() })
69
87