fastify 5.2.0 → 5.2.1
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.
- package/README.md +42 -43
- package/build/build-validation.js +1 -1
- package/docs/Reference/Errors.md +2 -0
- package/docs/Reference/Request.md +9 -10
- package/docs/Reference/Server.md +1 -1
- package/fastify.js +1 -1
- package/lib/errors.js +4 -0
- package/lib/reply.js +4 -0
- package/lib/request.js +13 -9
- package/package.json +19 -8
- package/test/custom-parser.2.test.js +19 -20
- package/test/custom-parser.3.test.js +56 -45
- package/test/delete.test.js +79 -67
- package/test/internals/errors.test.js +19 -7
- package/test/nullable-validation.test.js +30 -27
- package/test/web-api.test.js +44 -0
- package/.vscode/settings.json +0 -22
- package/test/test-reporter.mjs +0 -68
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test = t.test
|
|
3
|
+
const { test } = require('node:test')
|
|
5
4
|
const sget = require('simple-get').concat
|
|
6
|
-
const Fastify = require('
|
|
5
|
+
const Fastify = require('..')
|
|
7
6
|
const jsonParser = require('fast-json-body')
|
|
8
7
|
const { getServerUrl } = require('./helper')
|
|
9
8
|
|
|
10
9
|
process.removeAllListeners('warning')
|
|
11
10
|
|
|
12
|
-
test('should be able to use default parser for extra content type', t => {
|
|
11
|
+
test('should be able to use default parser for extra content type', (t, done) => {
|
|
13
12
|
t.plan(4)
|
|
14
13
|
const fastify = Fastify()
|
|
15
|
-
t.
|
|
14
|
+
t.after(() => fastify.close())
|
|
16
15
|
|
|
17
16
|
fastify.post('/', (request, reply) => {
|
|
18
17
|
reply.send(request.body)
|
|
@@ -21,7 +20,7 @@ test('should be able to use default parser for extra content type', t => {
|
|
|
21
20
|
fastify.addContentTypeParser('text/json', { parseAs: 'string' }, fastify.getDefaultJsonParser('ignore', 'ignore'))
|
|
22
21
|
|
|
23
22
|
fastify.listen({ port: 0 }, err => {
|
|
24
|
-
t.
|
|
23
|
+
t.assert.ifError(err)
|
|
25
24
|
|
|
26
25
|
sget({
|
|
27
26
|
method: 'POST',
|
|
@@ -31,18 +30,17 @@ test('should be able to use default parser for extra content type', t => {
|
|
|
31
30
|
'Content-Type': 'text/json'
|
|
32
31
|
}
|
|
33
32
|
}, (err, response, body) => {
|
|
34
|
-
t.
|
|
35
|
-
t.
|
|
36
|
-
t.
|
|
37
|
-
|
|
33
|
+
t.assert.ifError(err)
|
|
34
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
35
|
+
t.assert.deepStrictEqual(JSON.parse(body.toString()), { hello: 'world' })
|
|
36
|
+
done()
|
|
38
37
|
})
|
|
39
38
|
})
|
|
40
39
|
})
|
|
41
40
|
|
|
42
|
-
test('contentTypeParser should add a custom parser with RegExp value', t => {
|
|
43
|
-
t.plan(3)
|
|
44
|
-
|
|
41
|
+
test('contentTypeParser should add a custom parser with RegExp value', async (t) => {
|
|
45
42
|
const fastify = Fastify()
|
|
43
|
+
t.after(() => fastify.close())
|
|
46
44
|
|
|
47
45
|
fastify.post('/', (req, reply) => {
|
|
48
46
|
reply.send(req.body)
|
|
@@ -58,13 +56,12 @@ test('contentTypeParser should add a custom parser with RegExp value', t => {
|
|
|
58
56
|
})
|
|
59
57
|
})
|
|
60
58
|
|
|
61
|
-
fastify.listen({ port: 0 }, err => {
|
|
62
|
-
t.
|
|
63
|
-
|
|
64
|
-
t.teardown(() => fastify.close())
|
|
59
|
+
fastify.listen({ port: 0 }, async err => {
|
|
60
|
+
t.assert.ifError(err)
|
|
65
61
|
|
|
66
|
-
t.test('in POST', t => {
|
|
62
|
+
await t.test('in POST', (t, done) => {
|
|
67
63
|
t.plan(3)
|
|
64
|
+
t.after(() => fastify.close())
|
|
68
65
|
|
|
69
66
|
sget({
|
|
70
67
|
method: 'POST',
|
|
@@ -74,14 +71,16 @@ test('contentTypeParser should add a custom parser with RegExp value', t => {
|
|
|
74
71
|
'Content-Type': 'application/vnd.test+json'
|
|
75
72
|
}
|
|
76
73
|
}, (err, response, body) => {
|
|
77
|
-
t.
|
|
78
|
-
t.
|
|
79
|
-
t.
|
|
74
|
+
t.assert.ifError(err)
|
|
75
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
76
|
+
t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
77
|
+
done()
|
|
80
78
|
})
|
|
81
79
|
})
|
|
82
80
|
|
|
83
|
-
t.test('in OPTIONS', t => {
|
|
81
|
+
await t.test('in OPTIONS', (t, done) => {
|
|
84
82
|
t.plan(3)
|
|
83
|
+
t.after(() => fastify.close())
|
|
85
84
|
|
|
86
85
|
sget({
|
|
87
86
|
method: 'OPTIONS',
|
|
@@ -91,9 +90,10 @@ test('contentTypeParser should add a custom parser with RegExp value', t => {
|
|
|
91
90
|
'Content-Type': 'weird/content-type+json'
|
|
92
91
|
}
|
|
93
92
|
}, (err, response, body) => {
|
|
94
|
-
t.
|
|
95
|
-
t.
|
|
96
|
-
t.
|
|
93
|
+
t.assert.ifError(err)
|
|
94
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
95
|
+
t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
96
|
+
done()
|
|
97
97
|
})
|
|
98
98
|
})
|
|
99
99
|
})
|
|
@@ -102,7 +102,7 @@ test('contentTypeParser should add a custom parser with RegExp value', t => {
|
|
|
102
102
|
test('contentTypeParser should add multiple custom parsers with RegExp values', async t => {
|
|
103
103
|
t.plan(6)
|
|
104
104
|
const fastify = Fastify()
|
|
105
|
-
t.
|
|
105
|
+
t.after(() => fastify.close())
|
|
106
106
|
|
|
107
107
|
fastify.post('/', (req, reply) => {
|
|
108
108
|
reply.send(req.body)
|
|
@@ -137,8 +137,8 @@ test('contentTypeParser should add multiple custom parsers with RegExp values',
|
|
|
137
137
|
'Content-Type': 'application/vnd.hello+json'
|
|
138
138
|
}
|
|
139
139
|
})
|
|
140
|
-
t.
|
|
141
|
-
t.
|
|
140
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
141
|
+
t.assert.deepStrictEqual(response.payload.toString(), '{"hello":"world"}')
|
|
142
142
|
}
|
|
143
143
|
|
|
144
144
|
{
|
|
@@ -150,8 +150,8 @@ test('contentTypeParser should add multiple custom parsers with RegExp values',
|
|
|
150
150
|
'Content-Type': 'application/test+xml'
|
|
151
151
|
}
|
|
152
152
|
})
|
|
153
|
-
t.
|
|
154
|
-
t.
|
|
153
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
154
|
+
t.assert.deepStrictEqual(response.payload.toString(), 'xml')
|
|
155
155
|
}
|
|
156
156
|
|
|
157
157
|
await fastify.inject({
|
|
@@ -162,17 +162,17 @@ test('contentTypeParser should add multiple custom parsers with RegExp values',
|
|
|
162
162
|
'Content-Type': 'application/+myExtension'
|
|
163
163
|
}
|
|
164
164
|
}).then((response) => {
|
|
165
|
-
t.
|
|
166
|
-
t.
|
|
165
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
166
|
+
t.assert.deepStrictEqual(response.payload.toString(), 'abcdefgmyExtension')
|
|
167
167
|
}).catch((err) => {
|
|
168
|
-
t.
|
|
168
|
+
t.assert.ifError(err)
|
|
169
169
|
})
|
|
170
170
|
})
|
|
171
171
|
|
|
172
|
-
test('catch all content type parser should not interfere with content type parser', t => {
|
|
172
|
+
test('catch all content type parser should not interfere with content type parser', (t, done) => {
|
|
173
173
|
t.plan(10)
|
|
174
174
|
const fastify = Fastify()
|
|
175
|
-
t.
|
|
175
|
+
t.after(() => fastify.close())
|
|
176
176
|
|
|
177
177
|
fastify.post('/', (req, reply) => {
|
|
178
178
|
reply.send(req.body)
|
|
@@ -201,7 +201,15 @@ test('catch all content type parser should not interfere with content type parse
|
|
|
201
201
|
})
|
|
202
202
|
|
|
203
203
|
fastify.listen({ port: 0 }, err => {
|
|
204
|
-
t.
|
|
204
|
+
t.assert.ifError(err)
|
|
205
|
+
|
|
206
|
+
let pending = 3
|
|
207
|
+
|
|
208
|
+
function completed () {
|
|
209
|
+
if (--pending === 0) {
|
|
210
|
+
done()
|
|
211
|
+
}
|
|
212
|
+
}
|
|
205
213
|
|
|
206
214
|
sget({
|
|
207
215
|
method: 'POST',
|
|
@@ -211,9 +219,10 @@ test('catch all content type parser should not interfere with content type parse
|
|
|
211
219
|
'Content-Type': 'application/json'
|
|
212
220
|
}
|
|
213
221
|
}, (err, response, body) => {
|
|
214
|
-
t.
|
|
215
|
-
t.
|
|
216
|
-
t.
|
|
222
|
+
t.assert.ifError(err)
|
|
223
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
224
|
+
t.assert.deepStrictEqual(body.toString(), JSON.stringify({ myKey: 'myValue' }))
|
|
225
|
+
completed()
|
|
217
226
|
})
|
|
218
227
|
|
|
219
228
|
sget({
|
|
@@ -224,9 +233,10 @@ test('catch all content type parser should not interfere with content type parse
|
|
|
224
233
|
'Content-Type': 'very-weird-content-type'
|
|
225
234
|
}
|
|
226
235
|
}, (err, response, body) => {
|
|
227
|
-
t.
|
|
228
|
-
t.
|
|
229
|
-
t.
|
|
236
|
+
t.assert.ifError(err)
|
|
237
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
238
|
+
t.assert.deepStrictEqual(body.toString(), 'body')
|
|
239
|
+
completed()
|
|
230
240
|
})
|
|
231
241
|
|
|
232
242
|
sget({
|
|
@@ -237,9 +247,10 @@ test('catch all content type parser should not interfere with content type parse
|
|
|
237
247
|
'Content-Type': 'text/html'
|
|
238
248
|
}
|
|
239
249
|
}, (err, response, body) => {
|
|
240
|
-
t.
|
|
241
|
-
t.
|
|
242
|
-
t.
|
|
250
|
+
t.assert.ifError(err)
|
|
251
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
252
|
+
t.assert.deepStrictEqual(body.toString(), 'my texthtml')
|
|
253
|
+
completed()
|
|
243
254
|
})
|
|
244
255
|
})
|
|
245
256
|
})
|
package/test/delete.test.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test =
|
|
3
|
+
const assert = require('node:assert')
|
|
4
|
+
const { test } = require('node:test')
|
|
5
5
|
const sget = require('simple-get').concat
|
|
6
6
|
const fastify = require('..')()
|
|
7
7
|
|
|
@@ -85,15 +85,17 @@ const bodySchema = {
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
test('shorthand - delete', t => {
|
|
88
|
+
test('shorthand - delete', (t, done) => {
|
|
89
89
|
t.plan(1)
|
|
90
90
|
try {
|
|
91
91
|
fastify.delete('/', schema, function (req, reply) {
|
|
92
92
|
reply.code(200).send({ hello: 'world' })
|
|
93
93
|
})
|
|
94
|
-
t.
|
|
94
|
+
t.assert.ok(true)
|
|
95
95
|
} catch (e) {
|
|
96
|
-
t.fail()
|
|
96
|
+
t.assert.fail()
|
|
97
|
+
} finally {
|
|
98
|
+
done()
|
|
97
99
|
}
|
|
98
100
|
})
|
|
99
101
|
|
|
@@ -103,9 +105,9 @@ test('shorthand - delete params', t => {
|
|
|
103
105
|
fastify.delete('/params/:foo/:test', paramsSchema, function (req, reply) {
|
|
104
106
|
reply.code(200).send(req.params)
|
|
105
107
|
})
|
|
106
|
-
t.
|
|
108
|
+
t.assert.ok(true)
|
|
107
109
|
} catch (e) {
|
|
108
|
-
t.fail()
|
|
110
|
+
t.assert.fail()
|
|
109
111
|
}
|
|
110
112
|
})
|
|
111
113
|
|
|
@@ -115,9 +117,9 @@ test('shorthand - delete, querystring schema', t => {
|
|
|
115
117
|
fastify.delete('/query', querySchema, function (req, reply) {
|
|
116
118
|
reply.send(req.query)
|
|
117
119
|
})
|
|
118
|
-
t.
|
|
120
|
+
t.assert.ok(true)
|
|
119
121
|
} catch (e) {
|
|
120
|
-
t.fail()
|
|
122
|
+
t.assert.fail()
|
|
121
123
|
}
|
|
122
124
|
})
|
|
123
125
|
|
|
@@ -127,9 +129,9 @@ test('shorthand - get, headers schema', t => {
|
|
|
127
129
|
fastify.delete('/headers', headersSchema, function (req, reply) {
|
|
128
130
|
reply.code(200).send(req.headers)
|
|
129
131
|
})
|
|
130
|
-
t.
|
|
132
|
+
t.assert.ok(true)
|
|
131
133
|
} catch (e) {
|
|
132
|
-
t.fail()
|
|
134
|
+
t.assert.fail()
|
|
133
135
|
}
|
|
134
136
|
})
|
|
135
137
|
|
|
@@ -139,9 +141,9 @@ test('missing schema - delete', t => {
|
|
|
139
141
|
fastify.delete('/missing', function (req, reply) {
|
|
140
142
|
reply.code(200).send({ hello: 'world' })
|
|
141
143
|
})
|
|
142
|
-
t.
|
|
144
|
+
t.assert.ok(true)
|
|
143
145
|
} catch (e) {
|
|
144
|
-
t.fail()
|
|
146
|
+
t.assert.fail()
|
|
145
147
|
}
|
|
146
148
|
})
|
|
147
149
|
|
|
@@ -151,60 +153,63 @@ test('body - delete', t => {
|
|
|
151
153
|
fastify.delete('/body', bodySchema, function (req, reply) {
|
|
152
154
|
reply.send(req.body)
|
|
153
155
|
})
|
|
154
|
-
t.
|
|
156
|
+
t.assert.ok(true)
|
|
155
157
|
} catch (e) {
|
|
156
|
-
t.fail()
|
|
158
|
+
t.assert.fail()
|
|
157
159
|
}
|
|
158
160
|
})
|
|
159
161
|
|
|
160
162
|
fastify.listen({ port: 0 }, err => {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
+
assert.ifError(err)
|
|
164
|
+
test.after(() => { fastify.close() })
|
|
163
165
|
|
|
164
|
-
test('shorthand - request delete', t => {
|
|
166
|
+
test('shorthand - request delete', (t, done) => {
|
|
165
167
|
t.plan(4)
|
|
166
168
|
sget({
|
|
167
169
|
method: 'DELETE',
|
|
168
170
|
url: 'http://localhost:' + fastify.server.address().port
|
|
169
171
|
}, (err, response, body) => {
|
|
170
|
-
t.
|
|
171
|
-
t.
|
|
172
|
-
t.
|
|
173
|
-
t.
|
|
172
|
+
t.assert.ifError(err)
|
|
173
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
174
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
175
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
176
|
+
done()
|
|
174
177
|
})
|
|
175
178
|
})
|
|
176
179
|
|
|
177
|
-
test('shorthand - request delete params schema', t => {
|
|
180
|
+
test('shorthand - request delete params schema', (t, done) => {
|
|
178
181
|
t.plan(4)
|
|
179
182
|
sget({
|
|
180
183
|
method: 'DELETE',
|
|
181
184
|
url: 'http://localhost:' + fastify.server.address().port + '/params/world/123'
|
|
182
185
|
}, (err, response, body) => {
|
|
183
|
-
t.
|
|
184
|
-
t.
|
|
185
|
-
t.
|
|
186
|
-
t.
|
|
186
|
+
t.assert.ifError(err)
|
|
187
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
188
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
189
|
+
t.assert.deepStrictEqual(JSON.parse(body), { foo: 'world', test: 123 })
|
|
190
|
+
done()
|
|
187
191
|
})
|
|
188
192
|
})
|
|
189
193
|
|
|
190
|
-
test('shorthand - request delete params schema error', t => {
|
|
194
|
+
test('shorthand - request delete params schema error', (t, done) => {
|
|
191
195
|
t.plan(3)
|
|
192
196
|
sget({
|
|
193
197
|
method: 'DELETE',
|
|
194
198
|
url: 'http://localhost:' + fastify.server.address().port + '/params/world/string'
|
|
195
199
|
}, (err, response, body) => {
|
|
196
|
-
t.
|
|
197
|
-
t.
|
|
198
|
-
t.
|
|
200
|
+
t.assert.ifError(err)
|
|
201
|
+
t.assert.strictEqual(response.statusCode, 400)
|
|
202
|
+
t.assert.deepStrictEqual(JSON.parse(body), {
|
|
199
203
|
error: 'Bad Request',
|
|
200
204
|
code: 'FST_ERR_VALIDATION',
|
|
201
205
|
message: 'params/test must be integer',
|
|
202
206
|
statusCode: 400
|
|
203
207
|
})
|
|
208
|
+
done()
|
|
204
209
|
})
|
|
205
210
|
})
|
|
206
211
|
|
|
207
|
-
test('shorthand - request delete headers schema', t => {
|
|
212
|
+
test('shorthand - request delete headers schema', (t, done) => {
|
|
208
213
|
t.plan(4)
|
|
209
214
|
sget({
|
|
210
215
|
method: 'DELETE',
|
|
@@ -213,14 +218,15 @@ fastify.listen({ port: 0 }, err => {
|
|
|
213
218
|
},
|
|
214
219
|
url: 'http://localhost:' + fastify.server.address().port + '/headers'
|
|
215
220
|
}, (err, response, body) => {
|
|
216
|
-
t.
|
|
217
|
-
t.
|
|
218
|
-
t.
|
|
219
|
-
t.
|
|
221
|
+
t.assert.ifError(err)
|
|
222
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
223
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
224
|
+
t.assert.strictEqual(JSON.parse(body)['x-test'], 1)
|
|
225
|
+
done()
|
|
220
226
|
})
|
|
221
227
|
})
|
|
222
228
|
|
|
223
|
-
test('shorthand - request delete headers schema error', t => {
|
|
229
|
+
test('shorthand - request delete headers schema error', (t, done) => {
|
|
224
230
|
t.plan(3)
|
|
225
231
|
sget({
|
|
226
232
|
method: 'DELETE',
|
|
@@ -229,61 +235,65 @@ fastify.listen({ port: 0 }, err => {
|
|
|
229
235
|
},
|
|
230
236
|
url: 'http://localhost:' + fastify.server.address().port + '/headers'
|
|
231
237
|
}, (err, response, body) => {
|
|
232
|
-
t.
|
|
233
|
-
t.
|
|
234
|
-
t.
|
|
238
|
+
t.assert.ifError(err)
|
|
239
|
+
t.assert.strictEqual(response.statusCode, 400)
|
|
240
|
+
t.assert.deepStrictEqual(JSON.parse(body), {
|
|
235
241
|
error: 'Bad Request',
|
|
236
242
|
code: 'FST_ERR_VALIDATION',
|
|
237
243
|
message: 'headers/x-test must be number',
|
|
238
244
|
statusCode: 400
|
|
239
245
|
})
|
|
246
|
+
done()
|
|
240
247
|
})
|
|
241
248
|
})
|
|
242
249
|
|
|
243
|
-
test('shorthand - request delete querystring schema', t => {
|
|
250
|
+
test('shorthand - request delete querystring schema', (t, done) => {
|
|
244
251
|
t.plan(4)
|
|
245
252
|
sget({
|
|
246
253
|
method: 'DELETE',
|
|
247
254
|
url: 'http://localhost:' + fastify.server.address().port + '/query?hello=123'
|
|
248
255
|
}, (err, response, body) => {
|
|
249
|
-
t.
|
|
250
|
-
t.
|
|
251
|
-
t.
|
|
252
|
-
t.
|
|
256
|
+
t.assert.ifError(err)
|
|
257
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
258
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
259
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 123 })
|
|
260
|
+
done()
|
|
253
261
|
})
|
|
254
262
|
})
|
|
255
263
|
|
|
256
|
-
test('shorthand - request delete querystring schema error', t => {
|
|
264
|
+
test('shorthand - request delete querystring schema error', (t, done) => {
|
|
257
265
|
t.plan(3)
|
|
258
266
|
sget({
|
|
259
267
|
method: 'DELETE',
|
|
260
268
|
url: 'http://localhost:' + fastify.server.address().port + '/query?hello=world'
|
|
261
269
|
}, (err, response, body) => {
|
|
262
|
-
t.
|
|
263
|
-
t.
|
|
264
|
-
t.
|
|
270
|
+
t.assert.ifError(err)
|
|
271
|
+
t.assert.strictEqual(response.statusCode, 400)
|
|
272
|
+
t.assert.deepStrictEqual(JSON.parse(body), {
|
|
265
273
|
error: 'Bad Request',
|
|
266
274
|
code: 'FST_ERR_VALIDATION',
|
|
267
275
|
message: 'querystring/hello must be integer',
|
|
268
276
|
statusCode: 400
|
|
269
277
|
})
|
|
278
|
+
done()
|
|
270
279
|
})
|
|
271
280
|
})
|
|
272
281
|
|
|
273
|
-
test('shorthand - request delete missing schema', t => {
|
|
282
|
+
test('shorthand - request delete missing schema', (t, done) => {
|
|
274
283
|
t.plan(4)
|
|
275
284
|
sget({
|
|
276
285
|
method: 'DELETE',
|
|
277
286
|
url: 'http://localhost:' + fastify.server.address().port + '/missing'
|
|
278
287
|
}, (err, response, body) => {
|
|
279
|
-
t.
|
|
280
|
-
t.
|
|
281
|
-
t.
|
|
282
|
-
t.
|
|
288
|
+
t.assert.ifError(err)
|
|
289
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
290
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
291
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
292
|
+
done()
|
|
283
293
|
})
|
|
284
294
|
})
|
|
285
295
|
|
|
286
|
-
test('shorthand - delete with body', t => {
|
|
296
|
+
test('shorthand - delete with body', (t, done) => {
|
|
287
297
|
t.plan(3)
|
|
288
298
|
sget({
|
|
289
299
|
method: 'DELETE',
|
|
@@ -293,18 +303,19 @@ fastify.listen({ port: 0 }, err => {
|
|
|
293
303
|
},
|
|
294
304
|
json: true
|
|
295
305
|
}, (err, response, body) => {
|
|
296
|
-
t.
|
|
297
|
-
t.
|
|
298
|
-
t.
|
|
306
|
+
t.assert.ifError(err)
|
|
307
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
308
|
+
t.assert.deepStrictEqual(body, { hello: 'world' })
|
|
309
|
+
done()
|
|
299
310
|
})
|
|
300
311
|
})
|
|
301
312
|
})
|
|
302
313
|
|
|
303
|
-
test('shorthand - delete with application/json Content-Type header and null body', t => {
|
|
314
|
+
test('shorthand - delete with application/json Content-Type header and null body', (t, done) => {
|
|
304
315
|
t.plan(4)
|
|
305
316
|
const fastify = require('..')()
|
|
306
317
|
fastify.delete('/', {}, (req, reply) => {
|
|
307
|
-
t.
|
|
318
|
+
t.assert.strictEqual(req.body, null)
|
|
308
319
|
reply.send(req.body)
|
|
309
320
|
})
|
|
310
321
|
fastify.inject({
|
|
@@ -313,9 +324,10 @@ test('shorthand - delete with application/json Content-Type header and null body
|
|
|
313
324
|
headers: { 'Content-Type': 'application/json' },
|
|
314
325
|
body: 'null'
|
|
315
326
|
}, (err, response) => {
|
|
316
|
-
t.
|
|
317
|
-
t.
|
|
318
|
-
t.
|
|
327
|
+
t.assert.ifError(err)
|
|
328
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
329
|
+
t.assert.strictEqual(response.payload.toString(), 'null')
|
|
330
|
+
done()
|
|
319
331
|
})
|
|
320
332
|
})
|
|
321
333
|
|
|
@@ -325,7 +337,7 @@ test('shorthand - delete with application/json Content-Type header and without b
|
|
|
325
337
|
t.plan(4)
|
|
326
338
|
const fastify = require('..')()
|
|
327
339
|
fastify.delete('/', {}, (req, reply) => {
|
|
328
|
-
t.
|
|
340
|
+
t.assert.strictEqual(req.body, undefined)
|
|
329
341
|
reply.send(req.body)
|
|
330
342
|
})
|
|
331
343
|
fastify.inject({
|
|
@@ -334,8 +346,8 @@ test('shorthand - delete with application/json Content-Type header and without b
|
|
|
334
346
|
headers: { 'Content-Type': 'application/json' },
|
|
335
347
|
body: null
|
|
336
348
|
}, (err, response) => {
|
|
337
|
-
t.
|
|
338
|
-
t.
|
|
339
|
-
t.
|
|
349
|
+
t.assert.ifError(err)
|
|
350
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
351
|
+
t.assert.strictEqual(response.payload.toString(), '')
|
|
340
352
|
})
|
|
341
353
|
})
|
|
@@ -5,7 +5,9 @@ const errors = require('../../lib/errors')
|
|
|
5
5
|
const { readFileSync } = require('node:fs')
|
|
6
6
|
const { resolve } = require('node:path')
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
const expectedErrors = 84
|
|
9
|
+
|
|
10
|
+
test(`should expose ${expectedErrors} errors`, t => {
|
|
9
11
|
t.plan(1)
|
|
10
12
|
const exportedKeys = Object.keys(errors)
|
|
11
13
|
let counter = 0
|
|
@@ -14,11 +16,11 @@ test('should expose 83 errors', t => {
|
|
|
14
16
|
counter++
|
|
15
17
|
}
|
|
16
18
|
}
|
|
17
|
-
t.assert.strictEqual(counter,
|
|
19
|
+
t.assert.strictEqual(counter, expectedErrors)
|
|
18
20
|
})
|
|
19
21
|
|
|
20
22
|
test('ensure name and codes of Errors are identical', t => {
|
|
21
|
-
t.plan(
|
|
23
|
+
t.plan(expectedErrors)
|
|
22
24
|
const exportedKeys = Object.keys(errors)
|
|
23
25
|
for (const key of exportedKeys) {
|
|
24
26
|
if (errors[key].name === 'FastifyError') {
|
|
@@ -377,6 +379,16 @@ test('FST_ERR_REP_RESPONSE_BODY_CONSUMED', t => {
|
|
|
377
379
|
t.assert.ok(error instanceof Error)
|
|
378
380
|
})
|
|
379
381
|
|
|
382
|
+
test('FST_ERR_REP_READABLE_STREAM_LOCKED', t => {
|
|
383
|
+
t.plan(5)
|
|
384
|
+
const error = new errors.FST_ERR_REP_READABLE_STREAM_LOCKED()
|
|
385
|
+
t.assert.strictEqual(error.name, 'FastifyError')
|
|
386
|
+
t.assert.strictEqual(error.code, 'FST_ERR_REP_READABLE_STREAM_LOCKED')
|
|
387
|
+
t.assert.strictEqual(error.message, 'ReadableStream was locked. You should call releaseLock() method on reader before sending.')
|
|
388
|
+
t.assert.strictEqual(error.statusCode, 500)
|
|
389
|
+
t.assert.ok(error instanceof Error)
|
|
390
|
+
})
|
|
391
|
+
|
|
380
392
|
test('FST_ERR_REP_ALREADY_SENT', t => {
|
|
381
393
|
t.plan(5)
|
|
382
394
|
const error = new errors.FST_ERR_REP_ALREADY_SENT('/hello', 'GET')
|
|
@@ -868,7 +880,7 @@ test('FST_ERR_ERROR_HANDLER_NOT_FN', t => {
|
|
|
868
880
|
})
|
|
869
881
|
|
|
870
882
|
test('Ensure that all errors are in Errors.md TOC', t => {
|
|
871
|
-
t.plan(
|
|
883
|
+
t.plan(expectedErrors)
|
|
872
884
|
const errorsMd = readFileSync(resolve(__dirname, '../../docs/Reference/Errors.md'), 'utf8')
|
|
873
885
|
|
|
874
886
|
const exportedKeys = Object.keys(errors)
|
|
@@ -880,7 +892,7 @@ test('Ensure that all errors are in Errors.md TOC', t => {
|
|
|
880
892
|
})
|
|
881
893
|
|
|
882
894
|
test('Ensure that non-existing errors are not in Errors.md TOC', t => {
|
|
883
|
-
t.plan(
|
|
895
|
+
t.plan(expectedErrors)
|
|
884
896
|
const errorsMd = readFileSync(resolve(__dirname, '../../docs/Reference/Errors.md'), 'utf8')
|
|
885
897
|
|
|
886
898
|
const matchRE = / {4}- \[([A-Z0-9_]+)\]\(#[a-z0-9_]+\)/g
|
|
@@ -893,7 +905,7 @@ test('Ensure that non-existing errors are not in Errors.md TOC', t => {
|
|
|
893
905
|
})
|
|
894
906
|
|
|
895
907
|
test('Ensure that all errors are in Errors.md documented', t => {
|
|
896
|
-
t.plan(
|
|
908
|
+
t.plan(expectedErrors)
|
|
897
909
|
const errorsMd = readFileSync(resolve(__dirname, '../../docs/Reference/Errors.md'), 'utf8')
|
|
898
910
|
|
|
899
911
|
const exportedKeys = Object.keys(errors)
|
|
@@ -905,7 +917,7 @@ test('Ensure that all errors are in Errors.md documented', t => {
|
|
|
905
917
|
})
|
|
906
918
|
|
|
907
919
|
test('Ensure that non-existing errors are not in Errors.md documented', t => {
|
|
908
|
-
t.plan(
|
|
920
|
+
t.plan(expectedErrors)
|
|
909
921
|
const errorsMd = readFileSync(resolve(__dirname, '../../docs/Reference/Errors.md'), 'utf8')
|
|
910
922
|
|
|
911
923
|
const matchRE = /<a id="[0-9a-zA-Z_]+">([0-9a-zA-Z_]+)<\/a>/g
|