-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathhttp-retry.test.js
102 lines (76 loc) · 2.66 KB
/
http-retry.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
'use strict'
const Fastify = require('fastify')
const { request, Agent } = require('undici')
const From = require('..')
const { test } = require('tap')
let retryNum = 1
const target = require('node:http').createServer(function (req, res) {
if (retryNum % 2 !== 0) {
req.socket.destroy()
} else {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('hello world')
}
retryNum += 1
})
test('Will retry', async function (t) {
t.teardown(() => { retryNum = 1 })
await target.listen({ port: 0 })
t.teardown(target.close.bind(target))
const instance = Fastify()
instance.register(From, { http: true })
instance.get('/', (_request, reply) => {
reply.from(`http://localhost:${target.address().port}/`, {
retriesCount: 1,
onError: (reply, { error }) => {
t.equal(error.code, 'ECONNRESET')
reply.send(error)
}
})
})
await instance.listen({ port: 0 })
t.teardown(instance.close.bind(instance))
const { statusCode } = await request(`http://localhost:${instance.server.address().port}/`, { dispatcher: new Agent({ pipelining: 0 }) })
t.equal(statusCode, 200)
})
test('will not retry', async function (t) {
t.teardown(() => { retryNum = 1 })
await target.listen({ port: 0 })
t.teardown(target.close.bind(target))
const instance = Fastify()
instance.register(From, { http: true })
instance.get('/', (_request, reply) => {
reply.from(`http://localhost:${target.address().port}/`, {
retriesCount: 0,
onError: (reply, { error }) => {
t.equal(error.code, 'ECONNRESET')
reply.send(error)
}
})
})
await instance.listen({ port: 0 })
t.teardown(instance.close.bind(instance))
const result = await request(`http://localhost:${instance.server.address().port}/`, { dispatcher: new Agent({ pipelining: 0 }) })
t.equal(result.statusCode, 500)
})
test('will not retry unsupported method', async function (t) {
t.teardown(() => { retryNum = 1 })
await new Promise(resolve => target.listen({ port: 0 }, resolve))
t.teardown(target.close.bind(target))
const instance = Fastify()
instance.register(From, { http: true, retryMethods: ['DELETE'] })
instance.get('/', (_request, reply) => {
reply.from(`http://localhost:${target.address().port}/`, {
retriesCount: 1,
onError: (reply, { error }) => {
t.equal(error.code, 'ECONNRESET')
reply.send(error)
}
})
})
await instance.listen({ port: 0 })
t.teardown(instance.close.bind(instance))
const result = await request(`http://localhost:${instance.server.address().port}/`, { dispatcher: new Agent({ pipelining: 0 }) })
t.equal(result.statusCode, 500)
})