Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rejectUnauthorized as an extended RequestInit option #1749

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions @types/index.d.ts
Expand Up @@ -109,6 +109,7 @@ export interface RequestInit {
size?: number;
highWaterMark?: number;
insecureHTTPParser?: boolean;
rejectUnauthorized?: boolean;
}

export interface ResponseInit {
Expand Down
2 changes: 2 additions & 0 deletions src/request.js
Expand Up @@ -137,6 +137,7 @@ export default class Request extends Body {
this.agent = init.agent || input.agent;
this.highWaterMark = init.highWaterMark || input.highWaterMark || 16384;
this.insecureHTTPParser = init.insecureHTTPParser || input.insecureHTTPParser || false;
this.rejectUnauthorized = init.rejectUnauthorized === undefined ? (input.rejectUnauthorized === undefined ? true : input.rejectUnauthorized) : init.rejectUnauthorized;

// §5.4, Request constructor steps, step 16.
// Default is empty string per https://fetch.spec.whatwg.org/#concept-request-referrer-policy
Expand Down Expand Up @@ -306,6 +307,7 @@ export const getNodeRequestOptions = request => {
method: request.method,
headers: headers[Symbol.for('nodejs.util.inspect.custom')](),
insecureHTTPParser: request.insecureHTTPParser,
rejectUnauthorized: request.rejectUnauthorized,
agent
};

Expand Down
15 changes: 14 additions & 1 deletion test/request.js
Expand Up @@ -7,6 +7,7 @@ import FormData from 'form-data';
import Blob from 'fetch-blob';

import {Request} from '../src/index.js';
import {getNodeRequestOptions} from '../src/request.js';
import TestServer from './utils/server.js';

const {expect} = chai;
Expand Down Expand Up @@ -85,6 +86,16 @@ describe('Request', () => {
expect(r2.counter).to.equal(0);
});

it('should pass through rejectUnauthorized from init', () => {
const request = new Request(`${base}hello`, {
rejectUnauthorized: false
});
expect(request.rejectUnauthorized).to.equal(false);

const requestOpts = getNodeRequestOptions(request);
expect(requestOpts.options.rejectUnauthorized).to.equal(false);
});

it('should override signal on derived Request instances', () => {
const parentAbortController = new AbortController();
const derivedAbortController = new AbortController();
Expand Down Expand Up @@ -247,7 +258,8 @@ describe('Request', () => {
follow: 3,
compress: false,
agent,
signal
signal,
rejectUnauthorized: false
});
const cl = request.clone();
expect(cl.url).to.equal(url);
Expand All @@ -260,6 +272,7 @@ describe('Request', () => {
expect(cl.counter).to.equal(0);
expect(cl.agent).to.equal(agent);
expect(cl.signal).to.equal(signal);
expect(cl.rejectUnauthorized).to.equal(false);
// Clone body shouldn't be the same body
expect(cl.body).to.not.equal(body);
return Promise.all([cl.text(), request.text()]).then(results => {
Expand Down