|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const t = require('tap') |
| 3 | +const { test } = require('tap') |
4 | 4 | const Fastify = require('fastify') |
5 | 5 | const From = require('..') |
6 | 6 | const got = require('got') |
7 | 7 | const FakeTimers = require('@sinonjs/fake-timers') |
8 | 8 |
|
9 | | -const clock = FakeTimers.createClock() |
10 | | - |
11 | | -t.test('undici request timeout', async (t) => { |
| 9 | +test('undici request timeout', async (t) => { |
| 10 | + const clock = FakeTimers.createClock() |
12 | 11 | const target = Fastify() |
13 | 12 | t.teardown(target.close.bind(target)) |
14 | 13 |
|
15 | 14 | target.get('/', (_request, reply) => { |
16 | 15 | t.pass('request arrives') |
17 | 16 |
|
18 | | - clock.setTimeout(() => { |
| 17 | + setTimeout(() => { |
19 | 18 | reply.status(200).send('hello world') |
20 | | - t.end() |
21 | 19 | }, 1000) |
| 20 | + |
| 21 | + clock.tick(1000) |
22 | 22 | }) |
23 | 23 |
|
24 | 24 | await target.listen({ port: 0 }) |
@@ -50,7 +50,66 @@ t.test('undici request timeout', async (t) => { |
50 | 50 | error: 'Gateway Timeout', |
51 | 51 | message: 'Gateway Timeout' |
52 | 52 | }) |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + t.fail() |
| 57 | +}) |
| 58 | + |
| 59 | +test('undici request with specific timeout', async (t) => { |
| 60 | + const clock = FakeTimers.createClock() |
| 61 | + const target = Fastify() |
| 62 | + t.teardown(target.close.bind(target)) |
| 63 | + |
| 64 | + target.get('/', (_request, reply) => { |
| 65 | + t.pass('request arrives') |
| 66 | + |
| 67 | + setTimeout(() => { |
| 68 | + reply.status(200).send('hello world') |
| 69 | + }, 1000) |
| 70 | + |
53 | 71 | clock.tick(1000) |
| 72 | + }) |
| 73 | + |
| 74 | + await target.listen({ port: 0 }) |
| 75 | + |
| 76 | + const instance = Fastify() |
| 77 | + t.teardown(instance.close.bind(instance)) |
| 78 | + |
| 79 | + instance.register(From, { |
| 80 | + base: `http://localhost:${target.server.address().port}`, |
| 81 | + undici: { |
| 82 | + headersTimeout: 100, |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + instance.get('/success', (_request, reply) => { |
| 87 | + reply.from('/', { |
| 88 | + timeout: 1000 |
| 89 | + }) |
| 90 | + }) |
| 91 | + instance.get('/fail', (_request, reply) => { |
| 92 | + reply.from('/', { |
| 93 | + timeout: 50 |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + await instance.listen({ port: 0 }) |
| 98 | + |
| 99 | + const { statusCode } = await got.get(`http://localhost:${instance.server.address().port}/success`, { retry: 0 }) |
| 100 | + t.equal(statusCode, 200) |
| 101 | + |
| 102 | + try { |
| 103 | + await got.get(`http://localhost:${instance.server.address().port}/fail`, { retry: 0 }) |
| 104 | + } catch (err) { |
| 105 | + t.equal(err.response.statusCode, 504) |
| 106 | + t.match(err.response.headers['content-type'], /application\/json/) |
| 107 | + t.same(JSON.parse(err.response.body), { |
| 108 | + statusCode: 504, |
| 109 | + code: 'FST_REPLY_FROM_GATEWAY_TIMEOUT', |
| 110 | + error: 'Gateway Timeout', |
| 111 | + message: 'Gateway Timeout' |
| 112 | + }) |
54 | 113 | return |
55 | 114 | } |
56 | 115 |
|
|
0 commit comments