fastify 5.3.3 → 5.5.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 (137) hide show
  1. package/.vscode/settings.json +15 -15
  2. package/LICENSE +1 -1
  3. package/README.md +2 -0
  4. package/SECURITY.md +158 -2
  5. package/build/build-validation.js +20 -1
  6. package/docs/Guides/Delay-Accepting-Requests.md +8 -5
  7. package/docs/Guides/Ecosystem.md +20 -5
  8. package/docs/Guides/Migration-Guide-V5.md +6 -10
  9. package/docs/Guides/Recommendations.md +1 -1
  10. package/docs/Reference/ContentTypeParser.md +1 -1
  11. package/docs/Reference/Errors.md +5 -3
  12. package/docs/Reference/Hooks.md +16 -20
  13. package/docs/Reference/Lifecycle.md +2 -2
  14. package/docs/Reference/Logging.md +3 -3
  15. package/docs/Reference/Middleware.md +1 -1
  16. package/docs/Reference/Reply.md +8 -8
  17. package/docs/Reference/Request.md +2 -2
  18. package/docs/Reference/Routes.md +7 -6
  19. package/docs/Reference/Server.md +341 -200
  20. package/docs/Reference/TypeScript.md +1 -3
  21. package/docs/Reference/Validation-and-Serialization.md +56 -4
  22. package/docs/Reference/Warnings.md +2 -1
  23. package/fastify.d.ts +4 -3
  24. package/fastify.js +47 -34
  25. package/lib/configValidator.js +196 -28
  26. package/lib/contentTypeParser.js +41 -48
  27. package/lib/error-handler.js +3 -3
  28. package/lib/errors.js +11 -0
  29. package/lib/handleRequest.js +13 -17
  30. package/lib/pluginOverride.js +3 -1
  31. package/lib/promise.js +23 -0
  32. package/lib/reply.js +24 -30
  33. package/lib/request.js +3 -10
  34. package/lib/route.js +37 -3
  35. package/lib/server.js +36 -35
  36. package/lib/symbols.js +1 -0
  37. package/lib/warnings.js +19 -1
  38. package/package.json +14 -10
  39. package/test/404s.test.js +226 -325
  40. package/test/allow-unsafe-regex.test.js +19 -48
  41. package/test/als.test.js +28 -40
  42. package/test/async-await.test.js +84 -128
  43. package/test/async_hooks.test.js +18 -37
  44. package/test/body-limit.test.js +90 -63
  45. package/test/buffer.test.js +22 -0
  46. package/test/build-certificate.js +1 -1
  47. package/test/case-insensitive.test.js +44 -65
  48. package/test/check.test.js +17 -21
  49. package/test/close-pipelining.test.js +24 -15
  50. package/test/constrained-routes.test.js +231 -0
  51. package/test/custom-http-server.test.js +7 -15
  52. package/test/custom-parser-async.test.js +17 -22
  53. package/test/custom-parser.0.test.js +267 -348
  54. package/test/custom-parser.1.test.js +141 -191
  55. package/test/custom-parser.2.test.js +34 -44
  56. package/test/custom-parser.3.test.js +56 -104
  57. package/test/custom-parser.4.test.js +106 -144
  58. package/test/custom-parser.5.test.js +56 -75
  59. package/test/custom-querystring-parser.test.js +51 -77
  60. package/test/decorator-namespace.test._js_ +3 -4
  61. package/test/decorator.test.js +76 -259
  62. package/test/delete.test.js +101 -110
  63. package/test/diagnostics-channel/404.test.js +7 -15
  64. package/test/diagnostics-channel/async-delay-request.test.js +7 -16
  65. package/test/diagnostics-channel/async-request.test.js +8 -16
  66. package/test/diagnostics-channel/error-request.test.js +7 -15
  67. package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
  68. package/test/diagnostics-channel/sync-request-reply.test.js +9 -16
  69. package/test/diagnostics-channel/sync-request.test.js +9 -16
  70. package/test/fastify-instance.test.js +1 -1
  71. package/test/header-overflow.test.js +18 -29
  72. package/test/helper.js +139 -135
  73. package/test/hooks-async.test.js +259 -235
  74. package/test/hooks.test.js +951 -996
  75. package/test/http-methods/copy.test.js +14 -19
  76. package/test/http-methods/get.test.js +131 -143
  77. package/test/http-methods/head.test.js +53 -84
  78. package/test/http-methods/lock.test.js +31 -31
  79. package/test/http-methods/mkcalendar.test.js +45 -72
  80. package/test/http-methods/mkcol.test.js +5 -9
  81. package/test/http-methods/move.test.js +6 -10
  82. package/test/http-methods/propfind.test.js +34 -44
  83. package/test/http-methods/proppatch.test.js +23 -29
  84. package/test/http-methods/report.test.js +44 -69
  85. package/test/http-methods/search.test.js +67 -82
  86. package/test/http-methods/unlock.test.js +5 -9
  87. package/test/http2/closing.test.js +38 -20
  88. package/test/http2/secure-with-fallback.test.js +31 -28
  89. package/test/https/custom-https-server.test.js +9 -13
  90. package/test/https/https.test.js +56 -53
  91. package/test/input-validation.js +139 -150
  92. package/test/internals/errors.test.js +50 -1
  93. package/test/internals/handle-request.test.js +72 -65
  94. package/test/internals/promise.test.js +63 -0
  95. package/test/internals/reply.test.js +277 -496
  96. package/test/issue-4959.test.js +12 -3
  97. package/test/listen.4.test.js +31 -43
  98. package/test/nullable-validation.test.js +33 -46
  99. package/test/output-validation.test.js +24 -26
  100. package/test/plugin.1.test.js +40 -68
  101. package/test/plugin.2.test.js +108 -120
  102. package/test/plugin.3.test.js +50 -72
  103. package/test/plugin.4.test.js +124 -119
  104. package/test/promises.test.js +42 -63
  105. package/test/proto-poisoning.test.js +78 -97
  106. package/test/register.test.js +8 -18
  107. package/test/request-error.test.js +57 -146
  108. package/test/request-id.test.js +30 -49
  109. package/test/route-hooks.test.js +117 -101
  110. package/test/route-prefix.test.js +194 -133
  111. package/test/route-shorthand.test.js +9 -27
  112. package/test/route.1.test.js +74 -131
  113. package/test/route.8.test.js +9 -17
  114. package/test/router-options.test.js +450 -0
  115. package/test/schema-serialization.test.js +177 -154
  116. package/test/schema-special-usage.test.js +165 -132
  117. package/test/schema-validation.test.js +254 -218
  118. package/test/server.test.js +143 -5
  119. package/test/set-error-handler.test.js +58 -1
  120. package/test/skip-reply-send.test.js +64 -69
  121. package/test/stream.1.test.js +33 -50
  122. package/test/stream.4.test.js +18 -28
  123. package/test/stream.5.test.js +11 -19
  124. package/test/trust-proxy.test.js +32 -58
  125. package/test/types/errors.test-d.ts +13 -1
  126. package/test/types/fastify.test-d.ts +3 -0
  127. package/test/types/request.test-d.ts +1 -0
  128. package/test/types/type-provider.test-d.ts +55 -0
  129. package/test/url-rewriting.test.js +45 -62
  130. package/test/use-semicolon-delimiter.test.js +117 -59
  131. package/test/versioned-routes.test.js +39 -56
  132. package/types/errors.d.ts +11 -1
  133. package/types/hooks.d.ts +1 -1
  134. package/types/instance.d.ts +1 -1
  135. package/types/reply.d.ts +2 -2
  136. package/types/request.d.ts +1 -0
  137. package/.taprc +0 -7
@@ -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('../../fastify')()
6
5
  fastify.addHttpMethod('MKCALENDAR', { hasBody: true })
7
6
 
@@ -78,93 +77,67 @@ test('can be created - mkcalendar', (t) => {
78
77
  })
79
78
 
80
79
  test('mkcalendar test', async t => {
81
- await fastify.listen({ port: 0 })
80
+ const fastifyServer = await fastify.listen({ port: 0 })
82
81
  t.after(() => {
83
82
  fastify.close()
84
83
  })
85
84
 
86
- await t.test('request - mkcalendar', (t, done) => {
87
- t.plan(3)
88
- sget(
89
- {
90
- url: `http://localhost:${fastify.server.address().port}/`,
91
- method: 'MKCALENDAR'
92
- },
93
- (err, response, body) => {
94
- t.assert.ifError(err)
95
- t.assert.strictEqual(response.statusCode, 207)
96
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
97
- done()
98
- }
99
- )
85
+ await t.test('request - mkcalendar', async t => {
86
+ t.plan(2)
87
+ const result = await fetch(`${fastifyServer}/`, {
88
+ method: 'MKCALENDAR'
89
+ })
90
+ t.assert.strictEqual(result.status, 207)
91
+ const body = await result.text()
92
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
100
93
  })
101
94
 
102
- await t.test('request with other path - mkcalendar', (t, done) => {
103
- t.plan(3)
104
- sget(
105
- {
106
- url: `http://localhost:${fastify.server.address().port}/test`,
107
- method: 'MKCALENDAR'
108
- },
109
- (err, response, body) => {
110
- t.assert.ifError(err)
111
- t.assert.strictEqual(response.statusCode, 207)
112
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
113
- done()
114
- }
115
- )
95
+ await t.test('request with other path - mkcalendar', async t => {
96
+ t.plan(2)
97
+ const result = await fetch(`${fastifyServer}/test`, {
98
+ method: 'MKCALENDAR'
99
+ })
100
+ t.assert.strictEqual(result.status, 207)
101
+ const body = await result.text()
102
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
116
103
  })
117
104
 
118
105
  // the body test uses a text/plain content type instead of application/xml because it requires
119
106
  // a specific content type parser
120
- await t.test('request with body - mkcalendar', (t, done) => {
107
+ await t.test('request with body - mkcalendar', async t => {
121
108
  t.plan(3)
122
- sget(
123
- {
124
- url: `http://localhost:${fastify.server.address().port}/test`,
125
- headers: { 'content-type': 'text/plain' },
126
- body: bodySample,
127
- method: 'MKCALENDAR'
128
- },
129
- (err, response, body) => {
130
- t.assert.ifError(err)
131
- t.assert.strictEqual(response.statusCode, 207)
132
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
133
- done()
134
- }
135
- )
109
+ const result = await fetch(`${fastifyServer}/test`, {
110
+ method: 'MKCALENDAR',
111
+ headers: { 'content-type': 'text/plain' },
112
+ body: bodySample
113
+ })
114
+ t.assert.ok(result.ok)
115
+ t.assert.strictEqual(result.status, 207)
116
+ const body = await result.text()
117
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
136
118
  })
137
119
 
138
- await t.test('request with body and no content type (415 error) - mkcalendar', (t, done) => {
120
+ await t.test('request with body and no content type (415 error) - mkcalendar', async t => {
139
121
  t.plan(3)
140
- sget(
141
- {
142
- url: `http://localhost:${fastify.server.address().port}/test`,
143
- body: bodySample,
144
- method: 'MKCALENDAR'
145
- },
146
- (err, response, body) => {
147
- t.assert.ifError(err)
148
- t.assert.strictEqual(response.statusCode, 415)
149
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
150
- done()
151
- }
152
- )
122
+ const result = await fetch(`${fastifyServer}/test`, {
123
+ method: 'MKCALENDAR',
124
+ body: bodySample,
125
+ headers: { 'content-type': undefined }
126
+ })
127
+ t.assert.ok(!result.ok)
128
+ t.assert.strictEqual(result.status, 415)
129
+ const body = await result.text()
130
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
153
131
  })
154
132
 
155
- await t.test('request without body - mkcalendar', (t, done) => {
133
+ await t.test('request without body - mkcalendar', async t => {
156
134
  t.plan(3)
157
- sget(
158
- {
159
- url: `http://localhost:${fastify.server.address().port}/test`,
160
- method: 'MKCALENDAR'
161
- },
162
- (err, response, body) => {
163
- t.assert.ifError(err)
164
- t.assert.strictEqual(response.statusCode, 207)
165
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
166
- done()
167
- }
168
- )
135
+ const result = await fetch(`${fastifyServer}/test`, {
136
+ method: 'MKCALENDAR'
137
+ })
138
+ t.assert.ok(result.ok)
139
+ t.assert.strictEqual(result.status, 207)
140
+ const body = await result.text()
141
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
169
142
  })
170
143
  })
@@ -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('MOVE')
7
6
 
@@ -24,23 +23,20 @@ test('shorthand - move', t => {
24
23
  }
25
24
  })
26
25
  test('move test', async t => {
27
- await fastify.listen({ port: 0 })
26
+ const fastifyServer = await fastify.listen({ port: 0 })
28
27
 
29
28
  t.after(() => { fastify.close() })
30
29
 
31
- await t.test('request - move', (t, done) => {
30
+ await t.test('request - move', async t => {
32
31
  t.plan(3)
33
- sget({
34
- url: `http://localhost:${fastify.server.address().port}/test.txt`,
32
+ const result = await fetch(`${fastifyServer}/test.txt`, {
35
33
  method: 'MOVE',
36
34
  headers: {
37
35
  Destination: '/test2.txt'
38
36
  }
39
- }, (err, response, body) => {
40
- t.assert.ifError(err)
41
- t.assert.strictEqual(response.statusCode, 201)
42
- t.assert.strictEqual(response.headers.location, '/test2.txt')
43
- done()
44
37
  })
38
+ t.assert.ok(result.ok)
39
+ t.assert.strictEqual(result.status, 201)
40
+ t.assert.strictEqual(result.headers.get('location'), '/test2.txt')
45
41
  })
46
42
  })
@@ -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('PROPFIND', { hasBody: true })
7
6
 
@@ -74,73 +73,64 @@ test('propfind test', async t => {
74
73
  fastify.close()
75
74
  })
76
75
 
77
- await t.test('request - propfind', (t, done) => {
76
+ await t.test('request - propfind', async t => {
78
77
  t.plan(3)
79
- sget({
80
- url: `http://localhost:${fastify.server.address().port}/`,
78
+ const result = await fetch(`http://localhost:${fastify.server.address().port}/`, {
81
79
  method: 'PROPFIND'
82
- }, (err, response, body) => {
83
- t.assert.ifError(err)
84
- t.assert.strictEqual(response.statusCode, 207)
85
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
86
- done()
87
80
  })
81
+ t.assert.ok(result.ok)
82
+ t.assert.strictEqual(result.status, 207)
83
+ const body = await result.text()
84
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
88
85
  })
89
86
 
90
- await t.test('request with other path - propfind', (t, done) => {
87
+ await t.test('request with other path - propfind', async t => {
91
88
  t.plan(3)
92
- sget({
93
- url: `http://localhost:${fastify.server.address().port}/test`,
89
+ const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
94
90
  method: 'PROPFIND'
95
- }, (err, response, body) => {
96
- t.assert.ifError(err)
97
- t.assert.strictEqual(response.statusCode, 207)
98
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
99
- done()
100
91
  })
92
+ t.assert.ok(result.ok)
93
+ t.assert.strictEqual(result.status, 207)
94
+ const body = await result.text()
95
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
101
96
  })
102
97
 
103
98
  // the body test uses a text/plain content type instead of application/xml because it requires
104
99
  // a specific content type parser
105
- await t.test('request with body - propfind', (t, done) => {
100
+ await t.test('request with body - propfind', async t => {
106
101
  t.plan(3)
107
- sget({
108
- url: `http://localhost:${fastify.server.address().port}/test`,
102
+ const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
103
+ method: 'PROPFIND',
109
104
  headers: { 'content-type': 'text/plain' },
110
- body: bodySample,
111
- method: 'PROPFIND'
112
- }, (err, response, body) => {
113
- t.assert.ifError(err)
114
- t.assert.strictEqual(response.statusCode, 207)
115
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
116
- done()
105
+ body: bodySample
117
106
  })
107
+ t.assert.ok(result.ok)
108
+ t.assert.strictEqual(result.status, 207)
109
+ const body = await result.text()
110
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
118
111
  })
119
112
 
120
- await t.test('request with body and no content type (415 error) - propfind', (t, done) => {
113
+ await t.test('request with body and no content type (415 error) - propfind', async t => {
121
114
  t.plan(3)
122
- sget({
123
- url: `http://localhost:${fastify.server.address().port}/test`,
115
+ const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
116
+ method: 'PROPFIND',
124
117
  body: bodySample,
125
- method: 'PROPFIND'
126
- }, (err, response, body) => {
127
- t.assert.ifError(err)
128
- t.assert.strictEqual(response.statusCode, 415)
129
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
130
- done()
118
+ headers: { 'content-type': '' }
131
119
  })
120
+ t.assert.ok(!result.ok)
121
+ t.assert.strictEqual(result.status, 415)
122
+ const body = await result.text()
123
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
132
124
  })
133
125
 
134
- await t.test('request without body - propfind', (t, done) => {
126
+ await t.test('request without body - propfind', async t => {
135
127
  t.plan(3)
136
- sget({
137
- url: `http://localhost:${fastify.server.address().port}/test`,
128
+ const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
138
129
  method: 'PROPFIND'
139
- }, (err, response, body) => {
140
- t.assert.ifError(err)
141
- t.assert.strictEqual(response.statusCode, 207)
142
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
143
- done()
144
130
  })
131
+ t.assert.ok(result.ok)
132
+ t.assert.strictEqual(result.status, 207)
133
+ const body = await result.text()
134
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
145
135
  })
146
136
  })
@@ -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
  })