From 462bc630015064fa4ad4358cf28d24f95e1c958b Mon Sep 17 00:00:00 2001 From: qlba Date: Fri, 26 Feb 2021 21:37:47 +0300 Subject: [PATCH] Fix `url` not being reused on retry in rare case (#1487) --- source/core/index.ts | 4 ++++ test/hooks.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/source/core/index.ts b/source/core/index.ts index 987770e6c..5dae15bb9 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -1648,6 +1648,10 @@ export default class Request extends Duplex implements RequestEvents { delete options.port; } + if ('protocol' in options) { + delete options.protocol; + } + // Make it possible to change `options.prefixUrl` let {prefixUrl} = options; Object.defineProperty(options, 'prefixUrl', { diff --git a/test/hooks.ts b/test/hooks.ts index 6a53eb02d..f7775405c 100644 --- a/test/hooks.ts +++ b/test/hooks.ts @@ -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) => { @@ -946,6 +978,7 @@ test('async afterResponse allows to retry with allowGetBody and json payload', w retry: 0, throwHttpErrors: false }); + t.is(statusCode, 200); });