Skip to content

Commit 9a646a3

Browse files
szmarczaksindresorhus
authored andcommitted
Don't continue the request after cancellation (#464)
1 parent 871c3bd commit 9a646a3

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,18 @@ function requestAsEventEmitter(opts) {
160160
});
161161

162162
cacheReq.once('request', req => {
163+
let aborted = false;
164+
req.once('abort', _ => {
165+
aborted = true;
166+
});
167+
163168
req.once('error', err => {
164169
clearInterval(progressInterval);
165170

171+
if (aborted) {
172+
return;
173+
}
174+
166175
const backoff = opts.retries(++retryCount, err);
167176

168177
if (backoff) {

test/cancel.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,43 @@ async function createAbortServer() {
2121
res.end();
2222
});
2323
});
24+
25+
s.on('/redirect', (req, res) => {
26+
res.writeHead(302, {
27+
location: `${s.url}/abort`
28+
});
29+
res.end();
30+
31+
ee.emit('sentRedirect');
32+
33+
setTimeout(resolve, 3000);
34+
});
2435
});
2536

2637
await s.listen(s.port);
2738
ee.url = `${s.url}/abort`;
39+
ee.redirectUrl = `${s.url}/redirect`;
2840

2941
return ee;
3042
}
3143

44+
test('cancel do not retry after cancelation', async t => {
45+
const helper = await createAbortServer();
46+
47+
const p = got(helper.redirectUrl, {
48+
retries: _ => {
49+
t.fail('Makes a new try after cancelation');
50+
}
51+
});
52+
53+
helper.on('sentRedirect', () => {
54+
p.cancel();
55+
});
56+
57+
await t.throws(p, PCancelable.CancelError);
58+
await t.notThrows(helper.aborted, 'Request finished instead of aborting.');
59+
});
60+
3261
test('cancel in-progress request', async t => {
3362
const helper = await createAbortServer();
3463
const body = new Readable({

0 commit comments

Comments
 (0)