forked from vercel/fetch-retry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
200 lines (180 loc) · 4.61 KB
/
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* globals test, afterEach, jest, expect */
const { createServer } = require('http')
const setup = require('./index')
const { ResponseError } = setup
const retryFetch = setup(require('node-fetch'))
let server
afterEach(() => {
if (server) {
server.close()
}
server = null
})
test('retries upon 500', async () => {
let i = 0
server = createServer((req, res) => {
if (i++ < 2) {
res.writeHead(500)
res.end()
} else {
res.end('ha')
}
})
return new Promise((resolve, reject) => {
server.listen(async () => {
try {
const { port } = server.address()
const res = await retryFetch(`http://127.0.0.1:${port}`)
expect(await res.text()).toBe('ha')
resolve()
} catch (err) {
reject(err)
}
})
server.on('error', reject)
})
})
test('resolves on >MAX_RETRIES', async () => {
server = createServer((req, res) => {
res.writeHead(500)
res.end()
})
return new Promise((resolve, reject) => {
server.listen(async () => {
try {
const { port } = server.address()
const res = await retryFetch(`http://127.0.0.1:${port}`, {
retry: {
retries: 3
}
})
expect(res.status).toBe(500)
return resolve()
} catch (err) {
reject(err)
}
})
server.on('error', reject)
})
})
test('accepts a custom onRetry option', async () => {
server = createServer((req, res) => {
res.writeHead(500)
res.end()
})
return new Promise((resolve, reject) => {
const opts = {
onRetry: jest.fn(),
retry: {
retries: 3
}
}
server.listen(async () => {
const { port } = server.address()
const res = await retryFetch(`http://127.0.0.1:${port}`, opts)
expect(opts.onRetry).toHaveBeenCalledTimes(3)
expect(opts.onRetry.mock.calls[0][0]).toBeInstanceOf(ResponseError)
expect(opts.onRetry.mock.calls[0][1]).toEqual(opts)
expect(await res.status).toBe(500)
resolve()
})
server.on('error', reject)
})
})
test('accepts a custom retry.onRetry option', () => {
server = createServer((req, res) => {
res.writeHead(500)
res.end()
})
return new Promise((resolve, reject) => {
const opts = {
onRetry: jest.fn(),
retry: {
retries: 2,
onRetry: jest.fn()
}
}
server.listen(async () => {
try {
const { port } = server.address()
const res = await retryFetch(`http://127.0.0.1:${port}`, opts)
expect(opts.onRetry).toHaveBeenCalledTimes(2)
expect(opts.onRetry.mock.calls[0][0]).toBeInstanceOf(ResponseError)
expect(opts.onRetry.mock.calls[0][1]).toEqual(opts)
expect(opts.retry.onRetry).toHaveBeenCalledTimes(2)
expect(opts.retry.onRetry.mock.calls[0][0]).toBeInstanceOf(ResponseError)
expect(await res.status).toBe(500)
resolve()
} catch (e) {
reject(e)
}
})
server.on('error', reject)
})
})
test('handles the Retry-After header', async () => {
server = createServer((req, res) => {
res.writeHead(429, { 'Retry-After': 1 })
res.end()
})
return new Promise((resolve, reject) => {
server.listen(async () => {
const { port } = server.address()
try {
const startedAt = Date.now()
const res = await retryFetch(`http://127.0.0.1:${port}`, {
retry: {
minTimeout: 10,
retries: 1
}
})
expect(res.status).toBe(429)
expect(Date.now() - startedAt).toBeGreaterThanOrEqual(1010)
resolve()
} catch (err) {
reject(err)
}
})
server.on('error', reject)
})
})
test('stops retrying when the Retry-After header exceeds the maxRetryAfter option', async () => {
server = createServer((req, res) => {
res.writeHead(429, { 'Retry-After': 21 })
res.end()
})
return new Promise((resolve, reject) => {
const opts = {
onRetry: jest.fn()
}
server.listen(async () => {
const { port } = server.address()
try {
const res = await retryFetch(`http://127.0.0.1:${port}`, opts)
expect(opts.onRetry.mock.calls.length).toBe(0)
expect(res.status).toBe(429)
resolve()
} catch (err) {
reject(err)
}
})
server.on('error', reject)
})
})
test('throws non ResponseError', async () => {
let err
const port = 84934
const opts = {
retry: {
retries: 1,
factor: 1,
maxTimeout: 20
}
}
try {
await retryFetch(`http://127.0.0.1:${port}`, opts)
} catch (e) {
err = e
}
expect(err.code).toBe('ERR_SOCKET_BAD_PORT')
})