-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathhttp-timeout.test.js
140 lines (108 loc) · 3.52 KB
/
http-timeout.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict'
const { test } = require('tap')
const Fastify = require('fastify')
const { request, Agent } = require('undici')
const From = require('..')
const FakeTimers = require('@sinonjs/fake-timers')
test('http request timeout', async (t) => {
const clock = FakeTimers.createClock()
const target = Fastify()
t.teardown(target.close.bind(target))
target.get('/', (_request, reply) => {
t.pass('request arrives')
setTimeout(() => {
reply.status(200).send('hello world')
}, 200)
clock.tick(200)
})
await target.listen({ port: 0 })
const instance = Fastify()
t.teardown(instance.close.bind(instance))
instance.register(From, { http: { requestOptions: { timeout: 100 } } })
instance.get('/', (_request, reply) => {
reply.from(`http://localhost:${target.server.address().port}/`)
})
await instance.listen({ port: 0 })
const result = await request(`http://localhost:${instance.server.address().port}/`, {
dispatcher: new Agent({
pipelining: 0
})
})
t.equal(result.statusCode, 504)
t.match(result.headers['content-type'], /application\/json/)
t.same(await result.body.json(), {
statusCode: 504,
code: 'FST_REPLY_FROM_GATEWAY_TIMEOUT',
error: 'Gateway Timeout',
message: 'Gateway Timeout'
})
clock.tick(200)
})
test('http request with specific timeout', async (t) => {
const clock = FakeTimers.createClock()
const target = Fastify()
t.teardown(target.close.bind(target))
target.get('/', (_request, reply) => {
t.pass('request arrives')
setTimeout(() => {
reply.status(200).send('hello world')
}, 200)
clock.tick(200)
})
await target.listen({ port: 0 })
const instance = Fastify()
t.teardown(instance.close.bind(instance))
instance.register(From, { http: { requestOptions: { timeout: 100 } } })
instance.get('/success', (_request, reply) => {
reply.from(`http://localhost:${target.server.address().port}/`, {
timeout: 300
})
})
instance.get('/fail', (_request, reply) => {
reply.from(`http://localhost:${target.server.address().port}/`, {
timeout: 50
})
})
await instance.listen({ port: 0 })
const result = await request(`http://localhost:${instance.server.address().port}/success`, {
dispatcher: new Agent({
pipelining: 0
})
})
t.equal(result.statusCode, 200)
const result2 = await request(`http://localhost:${instance.server.address().port}/fail`, {
dispatcher: new Agent({
pipelining: 0
})
})
t.equal(result2.statusCode, 504)
t.match(result2.headers['content-type'], /application\/json/)
t.same(await result2.body.json(), {
statusCode: 504,
code: 'FST_REPLY_FROM_GATEWAY_TIMEOUT',
error: 'Gateway Timeout',
message: 'Gateway Timeout'
})
})
test('http sse removes timeout test', async (t) => {
const target = Fastify()
t.teardown(target.close.bind(target))
target.get('/', (_request, reply) => {
t.pass('request arrives')
reply.header('content-type', 'text/event-stream').status(200).send('hello world')
})
await target.listen({ port: 0 })
const instance = Fastify()
t.teardown(instance.close.bind(instance))
instance.register(From, { http: { requestOptions: { timeout: 100 } } })
instance.get('/', (_request, reply) => {
reply.from(`http://localhost:${target.server.address().port}/`)
})
await instance.listen({ port: 0 })
const { statusCode } = await request(`http://localhost:${instance.server.address().port}/`, {
dispatcher: new Agent({
pipelining: 0
})
})
t.equal(statusCode, 200)
})