-
-
Notifications
You must be signed in to change notification settings - Fork 757
Expand file tree
/
Copy pathretry-body-factory.js
More file actions
148 lines (123 loc) · 3.76 KB
/
retry-body-factory.js
File metadata and controls
148 lines (123 loc) · 3.76 KB
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
'use strict'
const { createServer } = require('node:http')
const { test, after } = require('node:test')
const { tspl } = require('@matteo.collina/tspl')
const { Client, interceptors } = require('..')
const { Readable } = require('node:stream')
const { once } = require('node:events')
test('retry with body factory function - stream', async t => {
t = tspl(t, { plan: 2 })
let requestCount = 0
const server = createServer((req, res) => {
requestCount++
req.on('data', () => {})
req.on('end', () => {
if (requestCount < 2) {
res.writeHead(500, { 'content-type': 'application/json' })
res.end('{"message": "failed"}')
} else {
res.writeHead(200, { 'content-type': 'application/json' })
res.end('{"message": "success"}')
}
})
})
server.listen(0)
await once(server, 'listening')
const client = new Client(
`http://localhost:${server.address().port}`
).compose(interceptors.retry({
minTimeout: 100,
maxTimeout: 100,
methods: ['POST']
}))
after(async () => {
await client.close()
server.close()
await once(server, 'close')
})
const response = await client.request({
method: 'POST',
path: '/',
headers: { 'content-type': 'application/json' },
// Body factory function - creates a new stream for each retry
body: () => Readable.from(Buffer.from(JSON.stringify({ hello: 'world' })))
})
t.equal(response.statusCode, 200)
t.equal(requestCount, 2, 'server received 2 requests')
})
test('retry with body factory function - async generator', async t => {
t = tspl(t, { plan: 2 })
let requestCount = 0
const server = createServer((req, res) => {
requestCount++
req.on('data', () => {})
req.on('end', () => {
if (requestCount < 2) {
res.writeHead(500, { 'content-type': 'application/json' })
res.end('{"message": "failed"}')
} else {
res.writeHead(200, { 'content-type': 'application/json' })
res.end('{"message": "success"}')
}
})
})
server.listen(0)
await once(server, 'listening')
const client = new Client(
`http://localhost:${server.address().port}`
).compose(interceptors.retry({
minTimeout: 100,
maxTimeout: 100,
methods: ['POST']
}))
after(async () => {
await client.close()
server.close()
await once(server, 'close')
})
const response = await client.request({
method: 'POST',
path: '/',
headers: { 'content-type': 'application/json' },
// Body factory function returning async generator
body: () => (async function * () {
yield '{"hello": "world"}'
})()
})
t.equal(response.statusCode, 200)
t.equal(requestCount, 2, 'server received 2 requests')
})
test('non-retryable body (regular stream) fails on retry', async t => {
t = tspl(t, { plan: 2 })
let requestCount = 0
const server = createServer((req, res) => {
requestCount++
res.writeHead(500, { 'content-type': 'application/json' })
res.end('{"message": "failed"}')
})
server.listen(0)
await once(server, 'listening')
const client = new Client(
`http://localhost:${server.address().port}`
).compose(interceptors.retry({
minTimeout: 100,
maxTimeout: 100,
methods: ['POST'],
throwOnError: false
}))
after(async () => {
await client.close()
server.close()
await once(server, 'close')
})
const response = await client.request({
method: 'POST',
path: '/',
headers: { 'content-type': 'application/json' },
body: Readable.from(Buffer.from(JSON.stringify({ hello: 'world' })))
})
// The retry should not happen because the stream was consumed,
// so we should get the 500 response directly
t.equal(response.statusCode, 500)
t.equal(requestCount, 1, 'only 1 request sent (stream consumed)')
})