From c98f0d7c39ebe71bd0b4875e03c1a7a7b5207347 Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Fri, 5 Jun 2020 17:51:42 +0200 Subject: [PATCH] Deep merge of `https` options (#1304) --- source/core/index.ts | 4 ++++ test/merge-instances.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/source/core/index.ts b/source/core/index.ts index 974af6210..996ac9557 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -888,6 +888,10 @@ export default class Request extends Duplex implements RequestEvents { } // HTTPS options + if (defaults?.https) { + options.https = {...defaults.https, ...options.https}; + } + if ('rejectUnauthorized' in options) { deprecationWarning('"options.rejectUnauthorized" is now deprecated, please use "options.https.rejectUnauthorized"'); } diff --git a/test/merge-instances.ts b/test/merge-instances.ts index 31f133119..0e9596739 100644 --- a/test/merge-instances.ts +++ b/test/merge-instances.ts @@ -116,3 +116,33 @@ test('URL is not polluted', withServer, async (t, server, got) => { t.is(normalizedOptions.username, ''); }); + +test('merging instances with HTTPS options', t => { + const instanceA = got.extend({https: { + rejectUnauthorized: true, + certificate: 'FIRST' + }}); + const instanceB = got.extend({https: { + certificate: 'SECOND' + }}); + + const merged = instanceA.extend(instanceB); + + t.true(merged.defaults.options.https?.rejectUnauthorized); + t.is(merged.defaults.options.https?.certificate, 'SECOND'); +}); + +test('merging instances with HTTPS options undefined', t => { + const instanceA = got.extend({https: { + rejectUnauthorized: true, + certificate: 'FIRST' + }}); + const instanceB = got.extend({https: { + certificate: undefined + }}); + + const merged = instanceA.extend(instanceB); + + t.true(merged.defaults.options.https?.rejectUnauthorized); + t.is(merged.defaults.options.https?.certificate, undefined); +});