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

fix: restore proxy config backwards compatibility with 0.x #5097

Merged
merged 3 commits into from Oct 13, 2022
Merged
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 README.md
Expand Up @@ -482,6 +482,7 @@ These are the available config options for making requests. Only the `url` is re
proxy: {
protocol: 'https',
host: '127.0.0.1',
// hostname: '127.0.0.1' // Takes precedence over 'host' if both are defined
port: 9000,
auth: {
username: 'mikeymike',
Expand Down
11 changes: 7 additions & 4 deletions lib/adapters/http.js
Expand Up @@ -51,7 +51,7 @@ function dispatchBeforeRedirect(options) {
* If the proxy or config afterRedirects functions are defined, call them with the options
*
* @param {http.ClientRequestArgs} options
* @param {AxiosProxyConfig} configProxy
* @param {AxiosProxyConfig} configProxy configuration from Axios options object
* @param {string} location
*
* @returns {http.ClientRequestArgs}
Expand Down Expand Up @@ -82,13 +82,14 @@ function setProxy(options, configProxy, location) {
}

options.headers.host = options.hostname + (options.port ? ':' + options.port : '');
options.hostname = proxy.hostname;
const proxyHost = proxy.hostname || proxy.host;
options.hostname = proxyHost;
// Replace 'host' since options is not a URL object
options.host = proxy.hostname;
options.host = proxyHost;
options.port = proxy.port;
options.path = location;
if (proxy.protocol) {
options.protocol = proxy.protocol;
options.protocol = proxy.protocol.includes(':') ? proxy.protocol : `${proxy.protocol}:`;
}
}

Expand Down Expand Up @@ -586,3 +587,5 @@ export default function httpAdapter(config) {
}
});
}

export const __setProxy = setProxy;
33 changes: 33 additions & 0 deletions test/unit/adapters/http.js
Expand Up @@ -20,6 +20,7 @@ const isBlobSupported = typeof Blob !== 'undefined';
import {Throttle} from 'stream-throttle';
import devNull from 'dev-null';
import {AbortController} from 'abortcontroller-polyfill/dist/cjs-ponyfill.js';
import {__setProxy} from "../../../lib/adapters/http.js";

const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand Down Expand Up @@ -1236,6 +1237,38 @@ describe('supports http with nodejs', function () {
});
});

context('different options for direct proxy configuration (without env variables)', () => {
const destination = 'www.example.com';

const testCases = [{
description: 'hostname and trailing colon in protocol',
proxyConfig: { hostname: '127.0.0.1', protocol: 'http:', port: 80 },
expectedOptions: { host: '127.0.0.1', protocol: 'http:', port: 80, path: destination }
}, {
description: 'hostname and no trailing colon in protocol',
proxyConfig: { hostname: '127.0.0.1', protocol: 'http', port: 80 },
expectedOptions: { host: '127.0.0.1', protocol: 'http:', port: 80, path: destination }
}, {
description: 'both hostname and host -> hostname takes precedence',
proxyConfig: { hostname: '127.0.0.1', host: '0.0.0.0', protocol: 'http', port: 80 },
expectedOptions: { host: '127.0.0.1', protocol: 'http:', port: 80, path: destination }
}, {
description: 'only host and https protocol',
proxyConfig: { host: '0.0.0.0', protocol: 'https', port: 80 },
expectedOptions: { host: '0.0.0.0', protocol: 'https:', port: 80, path: destination }
}];

for (const test of testCases) {
it(test.description, () => {
const options = { headers: {}, beforeRedirects: {} };
__setProxy(options, test.proxyConfig, destination);
for (const [key, expected] of Object.entries(test.expectedOptions)) {
assert.equal(options[key], expected);
}
});
}
});

it('should support cancel', function (done) {
var source = axios.CancelToken.source();
server = http.createServer(function (req, res) {
Expand Down