From 9a309bdbe7e2552c5bffbea253d58c39d6e6c3e3 Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Thu, 6 Aug 2020 04:13:37 +0200 Subject: [PATCH] Fixed deprecationWarning on https options (#1391) Co-authored-by: Aleksey Timchenko --- source/core/index.ts | 27 +++++++++++++++++++++++++++ test/https.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/source/core/index.ts b/source/core/index.ts index b51a6ae52..21f579bcc 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -1543,6 +1543,33 @@ export default class Request extends Duplex implements RequestEvents { options.timeout = timeout; options.agent = agent; + // HTTPS options restore + if (options.https) { + if ('rejectUnauthorized' in options.https) { + delete requestOptions.rejectUnauthorized; + } + + if (options.https.checkServerIdentity) { + delete requestOptions.checkServerIdentity; + } + + if (options.https.certificateAuthority) { + delete requestOptions.ca; + } + + if (options.https.certificate) { + delete requestOptions.cert; + } + + if (options.https.key) { + delete requestOptions.key; + } + + if (options.https.passphrase) { + delete requestOptions.passphrase; + } + } + if (isClientRequest(requestOrResponse)) { this._onRequest(requestOrResponse); diff --git a/test/https.ts b/test/https.ts index 9ead3ff92..d5a55866d 100644 --- a/test/https.ts +++ b/test/https.ts @@ -31,6 +31,48 @@ test('https request with ca', withServer, async (t, server, got) => { t.is(body, 'ok'); }); +test('https request with ca and afterResponse hook', withServer, async (t, server, got) => { + server.get('/', (_request, response) => { + response.end('ok'); + }); + + const warningListener = (warning: any) => { + if ( + warning.name === 'DeprecationWarning' && + warning.message === 'Got: "options.ca" was never documented, please use ' + + '"options.https.certificateAuthority"' + ) { + process.off('warning', warningListener); + t.fail('unexpected deprecation warning'); + } + }; + + process.once('warning', warningListener); + + let shouldRetry = true; + const {body} = await got.secure({ + https: { + certificateAuthority: server.caCert + }, + headers: {host: 'example.com'}, + hooks: { + afterResponse: [ + (response, retry) => { + if (shouldRetry) { + shouldRetry = false; + + return retry({}); + } + + return response; + } + ] + } + }); + + t.is(body, 'ok'); +}); + test('https request with `checkServerIdentity` OK', withServer, async (t, server, got) => { server.get('/', (_request, response) => { response.end('ok');