Skip to content

Commit

Permalink
Fix url not being reused on retry in rare case (#1487)
Browse files Browse the repository at this point in the history
  • Loading branch information
qlba authored Feb 26, 2021
1 parent 1120370 commit 462bc63
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,10 @@ export default class Request extends Duplex implements RequestEvents<Request> {
delete options.port;
}

if ('protocol' in options) {
delete options.protocol;
}

// Make it possible to change `options.prefixUrl`
let {prefixUrl} = options;
Object.defineProperty(options, 'prefixUrl', {
Expand Down
33 changes: 33 additions & 0 deletions test/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,38 @@ test('afterResponse allows to retry', withServer, async (t, server, got) => {
t.is(statusCode, 200);
});

test('afterResponse allows to retry without losing the port', withServer, async (t, server) => {
server.get('/', (request, response) => {
if (request.headers.token !== 'unicorn') {
response.statusCode = 401;
}

response.end();
});

const {statusCode} = await got({
protocol: 'http:',
hostname: server.hostname,
port: server.port,
hooks: {
afterResponse: [
(response, retryWithMergedOptions) => {
if (response.statusCode === 401) {
return retryWithMergedOptions({
headers: {
token: 'unicorn'
}
});
}

return response;
}
]
}
});
t.is(statusCode, 200);
});

test('cancelling the request after retrying in a afterResponse hook', withServer, async (t, server, got) => {
let requests = 0;
server.get('/', (_request, response) => {
Expand Down Expand Up @@ -946,6 +978,7 @@ test('async afterResponse allows to retry with allowGetBody and json payload', w
retry: 0,
throwHttpErrors: false
});

t.is(statusCode, 200);
});

Expand Down

0 comments on commit 462bc63

Please sign in to comment.