Skip to content

Commit 462bc63

Browse files
author
qlba
authoredFeb 26, 2021
Fix url not being reused on retry in rare case (#1487)
1 parent 1120370 commit 462bc63

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
 

‎source/core/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,10 @@ export default class Request extends Duplex implements RequestEvents<Request> {
16481648
delete options.port;
16491649
}
16501650

1651+
if ('protocol' in options) {
1652+
delete options.protocol;
1653+
}
1654+
16511655
// Make it possible to change `options.prefixUrl`
16521656
let {prefixUrl} = options;
16531657
Object.defineProperty(options, 'prefixUrl', {

‎test/hooks.ts

+33
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,38 @@ test('afterResponse allows to retry', withServer, async (t, server, got) => {
552552
t.is(statusCode, 200);
553553
});
554554

555+
test('afterResponse allows to retry without losing the port', withServer, async (t, server) => {
556+
server.get('/', (request, response) => {
557+
if (request.headers.token !== 'unicorn') {
558+
response.statusCode = 401;
559+
}
560+
561+
response.end();
562+
});
563+
564+
const {statusCode} = await got({
565+
protocol: 'http:',
566+
hostname: server.hostname,
567+
port: server.port,
568+
hooks: {
569+
afterResponse: [
570+
(response, retryWithMergedOptions) => {
571+
if (response.statusCode === 401) {
572+
return retryWithMergedOptions({
573+
headers: {
574+
token: 'unicorn'
575+
}
576+
});
577+
}
578+
579+
return response;
580+
}
581+
]
582+
}
583+
});
584+
t.is(statusCode, 200);
585+
});
586+
555587
test('cancelling the request after retrying in a afterResponse hook', withServer, async (t, server, got) => {
556588
let requests = 0;
557589
server.get('/', (_request, response) => {
@@ -946,6 +978,7 @@ test('async afterResponse allows to retry with allowGetBody and json payload', w
946978
retry: 0,
947979
throwHttpErrors: false
948980
});
981+
949982
t.is(statusCode, 200);
950983
});
951984

0 commit comments

Comments
 (0)
Please sign in to comment.