fastify 5.4.0 → 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.
- package/.vscode/settings.json +22 -0
- package/LICENSE +1 -1
- package/SECURITY.md +158 -2
- package/build/build-validation.js +19 -1
- package/docs/Guides/Delay-Accepting-Requests.md +8 -5
- package/docs/Guides/Ecosystem.md +11 -0
- package/docs/Guides/Migration-Guide-V5.md +6 -10
- package/docs/Guides/Recommendations.md +1 -1
- package/docs/Reference/Errors.md +3 -1
- package/docs/Reference/Hooks.md +2 -6
- package/docs/Reference/Lifecycle.md +2 -2
- package/docs/Reference/Request.md +1 -1
- package/docs/Reference/Routes.md +4 -3
- package/docs/Reference/Server.md +306 -179
- package/docs/Reference/TypeScript.md +1 -3
- package/docs/Reference/Validation-and-Serialization.md +55 -3
- package/docs/Reference/Warnings.md +2 -1
- package/fastify.d.ts +2 -2
- package/fastify.js +34 -33
- package/lib/configValidator.js +196 -28
- package/lib/contentTypeParser.js +41 -48
- package/lib/error-handler.js +3 -3
- package/lib/errors.js +5 -0
- package/lib/handleRequest.js +13 -17
- package/lib/promise.js +23 -0
- package/lib/reply.js +17 -19
- package/lib/route.js +37 -3
- package/lib/server.js +36 -35
- package/lib/warnings.js +11 -1
- package/package.json +7 -7
- package/test/async-await.test.js +81 -134
- package/test/async_hooks.test.js +18 -37
- package/test/body-limit.test.js +51 -0
- package/test/buffer.test.js +22 -0
- package/test/case-insensitive.test.js +44 -65
- package/test/check.test.js +17 -21
- package/test/close-pipelining.test.js +24 -15
- package/test/constrained-routes.test.js +231 -0
- package/test/custom-http-server.test.js +7 -15
- package/test/custom-parser.0.test.js +267 -348
- package/test/custom-parser.1.test.js +141 -191
- package/test/custom-parser.2.test.js +34 -44
- package/test/custom-parser.3.test.js +56 -104
- package/test/custom-parser.4.test.js +106 -144
- package/test/custom-parser.5.test.js +56 -75
- package/test/custom-querystring-parser.test.js +51 -77
- package/test/decorator.test.js +76 -259
- package/test/delete.test.js +101 -110
- package/test/diagnostics-channel/404.test.js +7 -15
- package/test/diagnostics-channel/async-request.test.js +8 -16
- package/test/diagnostics-channel/error-request.test.js +7 -15
- package/test/diagnostics-channel/sync-request-reply.test.js +9 -16
- package/test/diagnostics-channel/sync-request.test.js +9 -16
- package/test/fastify-instance.test.js +1 -1
- package/test/header-overflow.test.js +18 -29
- package/test/helper.js +138 -134
- package/test/hooks-async.test.js +26 -32
- package/test/hooks.test.js +261 -447
- package/test/http-methods/copy.test.js +14 -19
- package/test/http-methods/get.test.js +131 -143
- package/test/http-methods/head.test.js +53 -84
- package/test/http-methods/mkcalendar.test.js +45 -72
- package/test/http-methods/move.test.js +6 -10
- package/test/http-methods/propfind.test.js +34 -44
- package/test/http-methods/unlock.test.js +5 -9
- package/test/http2/secure-with-fallback.test.js +3 -1
- package/test/https/custom-https-server.test.js +9 -13
- package/test/input-validation.js +139 -150
- package/test/internals/errors.test.js +50 -1
- package/test/internals/handle-request.test.js +29 -5
- package/test/internals/promise.test.js +63 -0
- package/test/internals/reply.test.js +277 -496
- package/test/plugin.1.test.js +40 -68
- package/test/plugin.2.test.js +40 -70
- package/test/plugin.3.test.js +25 -68
- package/test/promises.test.js +42 -63
- package/test/register.test.js +8 -18
- package/test/request-error.test.js +57 -100
- package/test/request-id.test.js +30 -49
- package/test/route-hooks.test.js +12 -16
- package/test/route-shorthand.test.js +9 -27
- package/test/route.1.test.js +74 -131
- package/test/route.8.test.js +9 -17
- package/test/router-options.test.js +450 -0
- package/test/schema-validation.test.js +30 -31
- package/test/server.test.js +143 -5
- package/test/stream.1.test.js +33 -50
- package/test/stream.4.test.js +18 -28
- package/test/stream.5.test.js +11 -19
- package/test/types/errors.test-d.ts +13 -1
- package/test/types/type-provider.test-d.ts +55 -0
- package/test/use-semicolon-delimiter.test.js +117 -59
- package/test/versioned-routes.test.js +39 -56
- package/types/errors.d.ts +11 -1
- package/types/hooks.d.ts +1 -1
- package/types/instance.d.ts +1 -1
- package/types/reply.d.ts +2 -2
|
@@ -1,29 +1,13 @@
|
|
|
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('COPY')
|
|
7
6
|
|
|
8
|
-
test('can be created - copy',
|
|
9
|
-
t.plan(
|
|
7
|
+
test('can be created - copy', async t => {
|
|
8
|
+
t.plan(3)
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
t.assert.ifError(err)
|
|
13
|
-
t.after(() => { fastify.close() })
|
|
14
|
-
|
|
15
|
-
sget({
|
|
16
|
-
url: `http://localhost:${fastify.server.address().port}/test.txt`,
|
|
17
|
-
method: 'COPY',
|
|
18
|
-
headers: {
|
|
19
|
-
Destination: '/test2.txt'
|
|
20
|
-
}
|
|
21
|
-
}, (err, response, body) => {
|
|
22
|
-
t.assert.ifError(err)
|
|
23
|
-
t.assert.strictEqual(response.statusCode, 204)
|
|
24
|
-
done()
|
|
25
|
-
})
|
|
26
|
-
})
|
|
10
|
+
t.after(() => fastify.close())
|
|
27
11
|
|
|
28
12
|
try {
|
|
29
13
|
fastify.route({
|
|
@@ -37,4 +21,15 @@ test('can be created - copy', (t, done) => {
|
|
|
37
21
|
} catch (e) {
|
|
38
22
|
t.assert.fail()
|
|
39
23
|
}
|
|
24
|
+
|
|
25
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
26
|
+
|
|
27
|
+
const result = await fetch(`${fastifyServer}/test.txt`, {
|
|
28
|
+
method: 'COPY',
|
|
29
|
+
headers: {
|
|
30
|
+
Destination: '/test2.txt'
|
|
31
|
+
}
|
|
32
|
+
})
|
|
33
|
+
t.assert.ok(result.ok)
|
|
34
|
+
t.assert.strictEqual(result.status, 204)
|
|
40
35
|
})
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { test } = require('node:test')
|
|
4
|
-
const
|
|
4
|
+
const { Client } = require('undici')
|
|
5
5
|
const fastify = require('../../fastify')()
|
|
6
6
|
|
|
7
7
|
const schema = {
|
|
@@ -219,206 +219,194 @@ test('get test', async t => {
|
|
|
219
219
|
|
|
220
220
|
await fastify.listen({ port: 0 })
|
|
221
221
|
|
|
222
|
-
await t.test('shorthand - request get',
|
|
222
|
+
await t.test('shorthand - request get', async t => {
|
|
223
223
|
t.plan(4)
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}, (err, response, body) => {
|
|
228
|
-
t.assert.ifError(err)
|
|
229
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
230
|
-
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
231
|
-
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
232
|
-
done()
|
|
224
|
+
|
|
225
|
+
const response = await fetch('http://localhost:' + fastify.server.address().port, {
|
|
226
|
+
method: 'GET'
|
|
233
227
|
})
|
|
228
|
+
t.assert.ok(response.ok)
|
|
229
|
+
t.assert.strictEqual(response.status, 200)
|
|
230
|
+
const body = await response.text()
|
|
231
|
+
t.assert.strictEqual(response.headers.get('content-length'), '' + body.length)
|
|
232
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
234
233
|
})
|
|
235
234
|
|
|
236
|
-
await t.test('shorthand - request get params schema',
|
|
235
|
+
await t.test('shorthand - request get params schema', async t => {
|
|
237
236
|
t.plan(4)
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
}, (err, response, body) => {
|
|
242
|
-
t.assert.ifError(err)
|
|
243
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
244
|
-
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
245
|
-
t.assert.deepStrictEqual(JSON.parse(body), { foo: 'world', test: 123 })
|
|
246
|
-
done()
|
|
237
|
+
|
|
238
|
+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/params/world/123', {
|
|
239
|
+
method: 'GET'
|
|
247
240
|
})
|
|
241
|
+
t.assert.ok(response.ok)
|
|
242
|
+
t.assert.strictEqual(response.status, 200)
|
|
243
|
+
const body = await response.text()
|
|
244
|
+
t.assert.strictEqual(response.headers.get('content-length'), '' + body.length)
|
|
245
|
+
t.assert.deepStrictEqual(JSON.parse(body), { foo: 'world', test: 123 })
|
|
248
246
|
})
|
|
249
247
|
|
|
250
|
-
await t.test('shorthand - request get params schema error',
|
|
248
|
+
await t.test('shorthand - request get params schema error', async t => {
|
|
251
249
|
t.plan(3)
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
done()
|
|
250
|
+
|
|
251
|
+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/params/world/string', {
|
|
252
|
+
method: 'GET'
|
|
253
|
+
})
|
|
254
|
+
t.assert.ok(!response.ok)
|
|
255
|
+
t.assert.strictEqual(response.status, 400)
|
|
256
|
+
const body = await response.text()
|
|
257
|
+
t.assert.deepStrictEqual(JSON.parse(body), {
|
|
258
|
+
error: 'Bad Request',
|
|
259
|
+
code: 'FST_ERR_VALIDATION',
|
|
260
|
+
message: 'params/test must be integer',
|
|
261
|
+
statusCode: 400
|
|
265
262
|
})
|
|
266
263
|
})
|
|
267
264
|
|
|
268
|
-
await t.test('shorthand - request get headers schema',
|
|
265
|
+
await t.test('shorthand - request get headers schema', async t => {
|
|
269
266
|
t.plan(4)
|
|
270
|
-
|
|
267
|
+
|
|
268
|
+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/headers', {
|
|
271
269
|
method: 'GET',
|
|
272
270
|
headers: {
|
|
273
271
|
'x-test': '1',
|
|
274
272
|
'Y-Test': '3'
|
|
275
|
-
}
|
|
276
|
-
json: true,
|
|
277
|
-
url: 'http://localhost:' + fastify.server.address().port + '/headers'
|
|
278
|
-
}, (err, response, body) => {
|
|
279
|
-
t.assert.ifError(err)
|
|
280
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
281
|
-
t.assert.strictEqual(body['x-test'], 1)
|
|
282
|
-
t.assert.strictEqual(body['y-test'], 3)
|
|
283
|
-
done()
|
|
273
|
+
}
|
|
284
274
|
})
|
|
275
|
+
t.assert.ok(response.ok)
|
|
276
|
+
t.assert.strictEqual(response.status, 200)
|
|
277
|
+
const body = await response.json()
|
|
278
|
+
t.assert.strictEqual(body['x-test'], 1)
|
|
279
|
+
t.assert.strictEqual(body['y-test'], 3)
|
|
285
280
|
})
|
|
286
281
|
|
|
287
|
-
await t.test('shorthand - request get headers schema error',
|
|
282
|
+
await t.test('shorthand - request get headers schema error', async t => {
|
|
288
283
|
t.plan(3)
|
|
289
|
-
|
|
284
|
+
|
|
285
|
+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/headers', {
|
|
290
286
|
method: 'GET',
|
|
291
287
|
headers: {
|
|
292
288
|
'x-test': 'abc'
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
})
|
|
304
|
-
done()
|
|
289
|
+
}
|
|
290
|
+
})
|
|
291
|
+
t.assert.ok(!response.ok)
|
|
292
|
+
t.assert.strictEqual(response.status, 400)
|
|
293
|
+
const body = await response.text()
|
|
294
|
+
t.assert.deepStrictEqual(JSON.parse(body), {
|
|
295
|
+
error: 'Bad Request',
|
|
296
|
+
code: 'FST_ERR_VALIDATION',
|
|
297
|
+
message: 'headers/x-test must be number',
|
|
298
|
+
statusCode: 400
|
|
305
299
|
})
|
|
306
300
|
})
|
|
307
301
|
|
|
308
|
-
await t.test('shorthand - request get querystring schema',
|
|
302
|
+
await t.test('shorthand - request get querystring schema', async t => {
|
|
309
303
|
t.plan(4)
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
}, (err, response, body) => {
|
|
314
|
-
t.assert.ifError(err)
|
|
315
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
316
|
-
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
317
|
-
t.assert.deepStrictEqual(JSON.parse(body), { hello: 123 })
|
|
318
|
-
done()
|
|
304
|
+
|
|
305
|
+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/query?hello=123', {
|
|
306
|
+
method: 'GET'
|
|
319
307
|
})
|
|
308
|
+
t.assert.ok(response.ok)
|
|
309
|
+
t.assert.strictEqual(response.status, 200)
|
|
310
|
+
const body = await response.text()
|
|
311
|
+
t.assert.strictEqual(response.headers.get('content-length'), '' + body.length)
|
|
312
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 123 })
|
|
320
313
|
})
|
|
321
314
|
|
|
322
|
-
await t.test('shorthand - request get querystring schema error',
|
|
315
|
+
await t.test('shorthand - request get querystring schema error', async t => {
|
|
323
316
|
t.plan(3)
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
done()
|
|
317
|
+
|
|
318
|
+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/query?hello=world', {
|
|
319
|
+
method: 'GET'
|
|
320
|
+
})
|
|
321
|
+
t.assert.ok(!response.ok)
|
|
322
|
+
t.assert.strictEqual(response.status, 400)
|
|
323
|
+
const body = await response.text()
|
|
324
|
+
t.assert.deepStrictEqual(JSON.parse(body), {
|
|
325
|
+
error: 'Bad Request',
|
|
326
|
+
code: 'FST_ERR_VALIDATION',
|
|
327
|
+
message: 'querystring/hello must be integer',
|
|
328
|
+
statusCode: 400
|
|
337
329
|
})
|
|
338
330
|
})
|
|
339
331
|
|
|
340
|
-
await t.test('shorthand - request get missing schema',
|
|
332
|
+
await t.test('shorthand - request get missing schema', async t => {
|
|
341
333
|
t.plan(4)
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
}, (err, response, body) => {
|
|
346
|
-
t.assert.ifError(err)
|
|
347
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
348
|
-
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
349
|
-
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
350
|
-
done()
|
|
334
|
+
|
|
335
|
+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/missing', {
|
|
336
|
+
method: 'GET'
|
|
351
337
|
})
|
|
338
|
+
t.assert.ok(response.ok)
|
|
339
|
+
t.assert.strictEqual(response.status, 200)
|
|
340
|
+
const body = await response.text()
|
|
341
|
+
t.assert.strictEqual(response.headers.get('content-length'), '' + body.length)
|
|
342
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
352
343
|
})
|
|
353
344
|
|
|
354
|
-
await t.test('shorthand - custom serializer',
|
|
345
|
+
await t.test('shorthand - custom serializer', async t => {
|
|
355
346
|
t.plan(4)
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
}, (err, response, body) => {
|
|
360
|
-
t.assert.ifError(err)
|
|
361
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
362
|
-
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
363
|
-
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
364
|
-
done()
|
|
347
|
+
|
|
348
|
+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/custom-serializer', {
|
|
349
|
+
method: 'GET'
|
|
365
350
|
})
|
|
351
|
+
t.assert.ok(response.ok)
|
|
352
|
+
t.assert.strictEqual(response.status, 200)
|
|
353
|
+
const body = await response.text()
|
|
354
|
+
t.assert.strictEqual(response.headers.get('content-length'), '' + body.length)
|
|
355
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
366
356
|
})
|
|
367
357
|
|
|
368
|
-
await t.test('shorthand - empty response',
|
|
358
|
+
await t.test('shorthand - empty response', async t => {
|
|
369
359
|
t.plan(4)
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
}, (err, response, body) => {
|
|
374
|
-
t.assert.ifError(err)
|
|
375
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
376
|
-
t.assert.strictEqual(response.headers['content-length'], '0')
|
|
377
|
-
t.assert.deepStrictEqual(body.toString(), '')
|
|
378
|
-
done()
|
|
360
|
+
|
|
361
|
+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/empty', {
|
|
362
|
+
method: 'GET'
|
|
379
363
|
})
|
|
364
|
+
t.assert.ok(response.ok)
|
|
365
|
+
t.assert.strictEqual(response.status, 200)
|
|
366
|
+
const body = await response.text()
|
|
367
|
+
t.assert.strictEqual(response.headers.get('content-length'), '0')
|
|
368
|
+
t.assert.deepStrictEqual(body.toString(), '')
|
|
380
369
|
})
|
|
381
370
|
|
|
382
|
-
await t.test('shorthand - send a falsy boolean',
|
|
371
|
+
await t.test('shorthand - send a falsy boolean', async t => {
|
|
383
372
|
t.plan(3)
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
}, (err, response, body) => {
|
|
388
|
-
t.assert.ifError(err)
|
|
389
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
390
|
-
t.assert.deepStrictEqual(body.toString(), 'false')
|
|
391
|
-
done()
|
|
373
|
+
|
|
374
|
+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/boolean', {
|
|
375
|
+
method: 'GET'
|
|
392
376
|
})
|
|
377
|
+
t.assert.ok(response.ok)
|
|
378
|
+
t.assert.strictEqual(response.status, 200)
|
|
379
|
+
const body = await response.text()
|
|
380
|
+
t.assert.deepStrictEqual(body.toString(), 'false')
|
|
393
381
|
})
|
|
394
382
|
|
|
395
|
-
await t.test('shorthand - send null value',
|
|
383
|
+
await t.test('shorthand - send null value', async t => {
|
|
396
384
|
t.plan(3)
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
}, (err, response, body) => {
|
|
401
|
-
t.assert.ifError(err)
|
|
402
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
403
|
-
t.assert.deepStrictEqual(body.toString(), 'null')
|
|
404
|
-
done()
|
|
385
|
+
|
|
386
|
+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/null', {
|
|
387
|
+
method: 'GET'
|
|
405
388
|
})
|
|
389
|
+
t.assert.ok(response.ok)
|
|
390
|
+
t.assert.strictEqual(response.status, 200)
|
|
391
|
+
const body = await response.text()
|
|
392
|
+
t.assert.deepStrictEqual(body.toString(), 'null')
|
|
406
393
|
})
|
|
407
394
|
|
|
408
|
-
await t.test('shorthand - request get headers - test fall back port',
|
|
409
|
-
t.plan(
|
|
410
|
-
|
|
395
|
+
await t.test('shorthand - request get headers - test fall back port', async t => {
|
|
396
|
+
t.plan(2)
|
|
397
|
+
|
|
398
|
+
const instance = new Client('http://localhost:' + fastify.server.address().port)
|
|
399
|
+
|
|
400
|
+
const response = await instance.request({
|
|
401
|
+
path: '/port',
|
|
411
402
|
method: 'GET',
|
|
412
403
|
headers: {
|
|
413
404
|
host: 'example.com'
|
|
414
|
-
}
|
|
415
|
-
json: true,
|
|
416
|
-
url: 'http://localhost:' + fastify.server.address().port + '/port'
|
|
417
|
-
}, (err, response, body) => {
|
|
418
|
-
t.assert.ifError(err)
|
|
419
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
420
|
-
t.assert.strictEqual(body.port, null)
|
|
421
|
-
done()
|
|
405
|
+
}
|
|
422
406
|
})
|
|
407
|
+
|
|
408
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
409
|
+
const body = JSON.parse(await response.body.text())
|
|
410
|
+
t.assert.strictEqual(body.port, null)
|
|
423
411
|
})
|
|
424
412
|
})
|
|
@@ -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
|
|
|
7
6
|
const schema = {
|
|
@@ -164,131 +163,101 @@ test('missing schema - head', t => {
|
|
|
164
163
|
|
|
165
164
|
test('head test', async t => {
|
|
166
165
|
t.after(() => { fastify.close() })
|
|
167
|
-
await fastify.listen({ port: 0 })
|
|
166
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
168
167
|
|
|
169
|
-
await t.test('shorthand - request head',
|
|
168
|
+
await t.test('shorthand - request head', async t => {
|
|
170
169
|
t.plan(2)
|
|
171
|
-
|
|
172
|
-
method: 'HEAD'
|
|
173
|
-
url: 'http://localhost:' + fastify.server.address().port
|
|
174
|
-
}, (err, response) => {
|
|
175
|
-
t.assert.ifError(err)
|
|
176
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
177
|
-
done()
|
|
170
|
+
const result = await fetch(fastifyServer, {
|
|
171
|
+
method: 'HEAD'
|
|
178
172
|
})
|
|
173
|
+
t.assert.ok(result.ok)
|
|
174
|
+
t.assert.strictEqual(result.status, 200)
|
|
179
175
|
})
|
|
180
176
|
|
|
181
|
-
await t.test('shorthand - request head params schema',
|
|
177
|
+
await t.test('shorthand - request head params schema', async t => {
|
|
182
178
|
t.plan(2)
|
|
183
|
-
|
|
184
|
-
method: 'HEAD'
|
|
185
|
-
url: 'http://localhost:' + fastify.server.address().port + '/params/world/123'
|
|
186
|
-
}, (err, response) => {
|
|
187
|
-
t.assert.ifError(err)
|
|
188
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
189
|
-
done()
|
|
179
|
+
const result = await fetch(`${fastifyServer}/params/world/123`, {
|
|
180
|
+
method: 'HEAD'
|
|
190
181
|
})
|
|
182
|
+
t.assert.ok(result.ok)
|
|
183
|
+
t.assert.strictEqual(result.status, 200)
|
|
191
184
|
})
|
|
192
185
|
|
|
193
|
-
await t.test('shorthand - request head params schema error',
|
|
186
|
+
await t.test('shorthand - request head params schema error', async t => {
|
|
194
187
|
t.plan(2)
|
|
195
|
-
|
|
196
|
-
method: 'HEAD'
|
|
197
|
-
url: 'http://localhost:' + fastify.server.address().port + '/params/world/string'
|
|
198
|
-
}, (err, response) => {
|
|
199
|
-
t.assert.ifError(err)
|
|
200
|
-
t.assert.strictEqual(response.statusCode, 400)
|
|
201
|
-
done()
|
|
188
|
+
const result = await fetch(`${fastifyServer}/params/world/string`, {
|
|
189
|
+
method: 'HEAD'
|
|
202
190
|
})
|
|
191
|
+
t.assert.ok(!result.ok)
|
|
192
|
+
t.assert.strictEqual(result.status, 400)
|
|
203
193
|
})
|
|
204
194
|
|
|
205
|
-
await t.test('shorthand - request head querystring schema',
|
|
195
|
+
await t.test('shorthand - request head querystring schema', async t => {
|
|
206
196
|
t.plan(2)
|
|
207
|
-
|
|
208
|
-
method: 'HEAD'
|
|
209
|
-
url: 'http://localhost:' + fastify.server.address().port + '/query?hello=123'
|
|
210
|
-
}, (err, response) => {
|
|
211
|
-
t.assert.ifError(err)
|
|
212
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
213
|
-
done()
|
|
197
|
+
const result = await fetch(`${fastifyServer}/query?hello=123`, {
|
|
198
|
+
method: 'HEAD'
|
|
214
199
|
})
|
|
200
|
+
t.assert.ok(result.ok)
|
|
201
|
+
t.assert.strictEqual(result.status, 200)
|
|
215
202
|
})
|
|
216
203
|
|
|
217
|
-
await t.test('shorthand - request head querystring schema error',
|
|
204
|
+
await t.test('shorthand - request head querystring schema error', async t => {
|
|
218
205
|
t.plan(2)
|
|
219
|
-
|
|
220
|
-
method: 'HEAD'
|
|
221
|
-
url: 'http://localhost:' + fastify.server.address().port + '/query?hello=world'
|
|
222
|
-
}, (err, response) => {
|
|
223
|
-
t.assert.ifError(err)
|
|
224
|
-
t.assert.strictEqual(response.statusCode, 400)
|
|
225
|
-
done()
|
|
206
|
+
const result = await fetch(`${fastifyServer}/query?hello=world`, {
|
|
207
|
+
method: 'HEAD'
|
|
226
208
|
})
|
|
209
|
+
t.assert.ok(!result.ok)
|
|
210
|
+
t.assert.strictEqual(result.status, 400)
|
|
227
211
|
})
|
|
228
212
|
|
|
229
|
-
await t.test('shorthand - request head missing schema',
|
|
213
|
+
await t.test('shorthand - request head missing schema', async t => {
|
|
230
214
|
t.plan(2)
|
|
231
|
-
|
|
232
|
-
method: 'HEAD'
|
|
233
|
-
url: 'http://localhost:' + fastify.server.address().port + '/missing'
|
|
234
|
-
}, (err, response) => {
|
|
235
|
-
t.assert.ifError(err)
|
|
236
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
237
|
-
done()
|
|
215
|
+
const result = await fetch(`${fastifyServer}/missing`, {
|
|
216
|
+
method: 'HEAD'
|
|
238
217
|
})
|
|
218
|
+
t.assert.ok(result.ok)
|
|
219
|
+
t.assert.strictEqual(result.status, 200)
|
|
239
220
|
})
|
|
240
221
|
|
|
241
|
-
await t.test('shorthand - request head custom head',
|
|
222
|
+
await t.test('shorthand - request head custom head', async t => {
|
|
242
223
|
t.plan(3)
|
|
243
|
-
|
|
244
|
-
method: 'HEAD'
|
|
245
|
-
url: 'http://localhost:' + fastify.server.address().port + '/proxy/test'
|
|
246
|
-
}, (err, response) => {
|
|
247
|
-
t.assert.ifError(err)
|
|
248
|
-
t.assert.strictEqual(response.headers['x-foo'], 'bar')
|
|
249
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
250
|
-
done()
|
|
224
|
+
const result = await fetch(`${fastifyServer}/proxy/test`, {
|
|
225
|
+
method: 'HEAD'
|
|
251
226
|
})
|
|
227
|
+
t.assert.ok(result.ok)
|
|
228
|
+
t.assert.strictEqual(result.headers.get('x-foo'), 'bar')
|
|
229
|
+
t.assert.strictEqual(result.status, 200)
|
|
252
230
|
})
|
|
253
231
|
|
|
254
|
-
await t.test('shorthand - request head custom head with constraints',
|
|
232
|
+
await t.test('shorthand - request head custom head with constraints', async t => {
|
|
255
233
|
t.plan(3)
|
|
256
|
-
|
|
234
|
+
const result = await fetch(`${fastifyServer}/proxy/test`, {
|
|
257
235
|
method: 'HEAD',
|
|
258
|
-
url: 'http://localhost:' + fastify.server.address().port + '/proxy/test',
|
|
259
236
|
headers: {
|
|
260
237
|
version: '1.0.0'
|
|
261
238
|
}
|
|
262
|
-
}, (err, response) => {
|
|
263
|
-
t.assert.ifError(err)
|
|
264
|
-
t.assert.strictEqual(response.headers['x-foo'], 'bar')
|
|
265
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
266
|
-
done()
|
|
267
239
|
})
|
|
240
|
+
t.assert.ok(result.ok)
|
|
241
|
+
t.assert.strictEqual(result.headers.get('x-foo'), 'bar')
|
|
242
|
+
t.assert.strictEqual(result.status, 200)
|
|
268
243
|
})
|
|
269
244
|
|
|
270
|
-
await t.test('shorthand - should not reset a head route',
|
|
245
|
+
await t.test('shorthand - should not reset a head route', async t => {
|
|
271
246
|
t.plan(2)
|
|
272
|
-
|
|
273
|
-
method: 'HEAD'
|
|
274
|
-
url: 'http://localhost:' + fastify.server.address().port + '/query1'
|
|
275
|
-
}, (err, response) => {
|
|
276
|
-
t.assert.ifError(err)
|
|
277
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
278
|
-
done()
|
|
247
|
+
const result = await fetch(`${fastifyServer}/query1`, {
|
|
248
|
+
method: 'HEAD'
|
|
279
249
|
})
|
|
250
|
+
t.assert.ok(result.ok)
|
|
251
|
+
t.assert.strictEqual(result.status, 200)
|
|
280
252
|
})
|
|
281
253
|
|
|
282
|
-
await t.test('shorthand - should set get and head route in the same api call',
|
|
254
|
+
await t.test('shorthand - should set get and head route in the same api call', async t => {
|
|
283
255
|
t.plan(3)
|
|
284
|
-
|
|
285
|
-
method: 'HEAD'
|
|
286
|
-
url: 'http://localhost:' + fastify.server.address().port + '/query4'
|
|
287
|
-
}, (err, response) => {
|
|
288
|
-
t.assert.ifError(err)
|
|
289
|
-
t.assert.strictEqual(response.headers['x-foo'], 'bar')
|
|
290
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
291
|
-
done()
|
|
256
|
+
const result = await fetch(`${fastifyServer}/query4`, {
|
|
257
|
+
method: 'HEAD'
|
|
292
258
|
})
|
|
259
|
+
t.assert.ok(result.ok)
|
|
260
|
+
t.assert.strictEqual(result.headers.get('x-foo'), 'bar')
|
|
261
|
+
t.assert.strictEqual(result.status, 200)
|
|
293
262
|
})
|
|
294
263
|
})
|