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

feat : Added support for proxy definitions #2923

Open
wants to merge 5 commits into
base: develop
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
3 changes: 3 additions & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
date: 2022-01-10
chores:
- Updated dependencies
new features:
- >-
Added support for proxy definitions

5.3.0:
date: 2021-09-07
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ Newman can also be configured to work with proxy settings via the following envi

For more details on using these variables, [refer here](https://github.com/postmanlabs/postman-request/blob/master/README.md#controlling-proxy-behaviour-using-environment-variables).

#### Using Proxy configuration file

- `--proxy-config-list`<br/>
The path to the Proxy configuration file (JSON format). See [examples/proxy-config-list.json](https://github.com/postmanlabs/newman/blob/develop/examples/proxy-config-list.json).

This option takes precedence over `HTTP_PROXY` / `http_proxy`, `HTTPS_PROXY` / `https_proxy` and `NO_PROXY` / `no_proxy` environment variables. If there is no match for the URL in the list, these environment variables are used as fallback.

[back to top](#table-of-contents)

## API Reference
Expand Down Expand Up @@ -303,6 +310,7 @@ return of the `newman.run` function is a run instance, which emits run events th
| options.sslExtraCaCerts | The path to the file, that holds one or more trusted CA certificates in PEM format.<br /><br />_Optional_<br />Type: `string` |
| options.requestAgents | Specify the custom requesting agents to be used when performing HTTP and HTTPS requests respectively. Example: [Using Socks Proxy](#using-socks-proxy)<br /><br />_Optional_<br />Type: `object` |
| options.cookieJar | One can optionally pass a CookieJar file path as `string` to this property and that will be deserialized using [`tough-cookie`](https://github.com/salesforce/tough-cookie). This property also accepts a `tough-cookie` CookieJar instance.<br /><br />_Optional_<br />Type: `object\|string` |
| options.proxyConfigList | The path to the proxy configuration list file. This option takes precedence over `HTTP_PROXY` / `http_proxy`, `HTTPS_PROXY` / `https_proxy` and `NO_PROXY` / `no_proxy` environment variables. When there is no match in this configuration list, these environment variables are used as fallback.<br /><br />_Optional_<br />Type:`object\|string` |
| options.newmanVersion | The Newman version used for the collection run.<br /><br />_This will be set by Newman_ |
| callback | Upon completion of the run, this callback is executed with the `error`, `summary` argument.<br /><br />_Required_<br />Type: `function` |

Expand Down
1 change: 1 addition & 0 deletions bin/newman.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ program
.option('--cookie-jar <path>', 'Specify the path to a custom cookie jar (serialized tough-cookie JSON) ')
.option('--export-cookie-jar <path>', 'Exports the cookie jar to a file after completing the run')
.option('--verbose', 'Show detailed information of collection run and each request sent')
.option('--proxy-config-list <path>', 'Specify the path to a proxy configurations (JSON)')
.action((collection, command) => {
let options = util.commanderToObject(command),

Expand Down
26 changes: 26 additions & 0 deletions examples/proxy-config-list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"proxies": [

{
"match": "https://example.com/*",
"host": "proxy.com",
"port": "8080",
"tunnel": true
},
{
"match": "http+https://example2.com/*",
"host": "proxy2.com"
},
{
"match": "http+https://example3.com/*",
"host": "proxy3.com",
"port": "8080",
"tunnel": true,
"disabled": false,
"authenticate": true,
"username": "proxy_username",
"password": "proxy_password"
}

]
}
5 changes: 4 additions & 1 deletion lib/run/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ module.exports = function (options, callback) {
// different URLs
var sslClientCertList = options.sslClientCertList || [],
// allow providing custom cookieJar
cookieJar = options.cookieJar || request.jar();
cookieJar = options.cookieJar || request.jar(),
proxyConfigList = options.proxyConfigList || [];


// if sslClientCert option is set, put it at the end of the list to
// match all URLs that didn't match in the list
Expand Down Expand Up @@ -166,6 +168,7 @@ module.exports = function (options, callback) {
iterationCount: options.iterationCount,
environment: options.environment,
globals: options.globals,
proxies: proxyConfigList.length && new sdk.ProxyConfigList({}, proxyConfigList),
entrypoint: entrypoint,
data: options.iterationData,
delay: {
Expand Down
34 changes: 34 additions & 0 deletions lib/run/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,41 @@ var _ = require('lodash'),

return callback(null, value);
});
},

proxyConfigList: function (location, options, callback) {
if (_.isObject(location) && Array.isArray(location.proxies)) {
return callback(null, location.proxies);
}

if (typeof location !== 'string') {
return callback(new Error('path for proxy configuration list file must be a string'));
}

fs.readFile(location, function (err, value) {
if (err) {
return callback(new Error(`unable to read the proxy configuration file "${location}"`));
}

try {
value = liquidJSON.parse(value.toString(util.detectEncoding(value)).trim());
}
catch (e) {
return callback(new Error(`the file at ${location} does not contain valid JSON data.`));
}
// ensure that `proxyConfigList` is an object
if (!_.isObject(value)) {
return callback(new Error('expected proxy configuration list to be an object.'));
}
// ensure that `proxyConfigList.proxies` is an array
if (!Array.isArray(value.proxies)) {
return callback(new Error('expected proxies option to be an array.'));
}

return callback(null, value.proxies);
});
}

};

/**
Expand Down
32 changes: 32 additions & 0 deletions test/cli/proxy-config-list.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@


describe('Proxy Configuration', function () {
it('should work correctly with proxy configuration list', function (done) {
// eslint-disable-next-line max-len
exec('node ./bin/newman.js run test/fixtures/run/proxy-config-list.json --verbose --proxy-config-list ./test/fixtures/files/proxy-config.json ', function (code) {
expect(code, 'should have exit code of 0').to.equal(0);
done();
});
});

it('should bail out if proxy configuration list file does not exist', function (done) {
var cmd = 'node ./bin/newman.js run test/fixtures/run/proxy-config-list.json' +
' --proxy-config-list invalid-proxy-config-file.json'; // using an invalid proxy config list


exec(cmd, function (code) {
expect(code, 'should not have exit code 0').to.not.equal(0);
done();
});
});

it('should bail out if unable to parse proxy configuration list', function (done) {
var cmd = 'node ./bin/newman.js run test/fixtures/run/proxy-config-list.json' +
' --proxy-config-list test/cli/proxy-config-list-test.js'; // using an invalid proxy config list

exec(cmd, function (code) {
expect(code, 'should not have exit code 0').to.not.equal(0);
done();
});
});
});
15 changes: 15 additions & 0 deletions test/fixtures/files/proxy-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"proxies": [
{
"match": "https://postman-echo.com/get?source=newman-sample-github-collection",
"host": "proxy.com",
"tunnel": true
},
{
"match": "http://postman-echo.com/type/html?source=newman-sample-github-collection",
"host": "proxy2.com",
"port":"8080",
"tunnel": true
}
]
}
45 changes: 45 additions & 0 deletions test/fixtures/run/proxy-config-list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"info": {
"name": "Proxy Configuration Example",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},

"item": [{
"name": "A simple GET request",
"event": [{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('response code is 200', function () {",
" pm.response.to.be.ok;",
" pm.response.to.have.header('Content-Type', 'application/json; charset=utf-8');",
"});"
]
}
}],
"request": {
"url": "https://postman-echo.com/get?source=newman-sample-github-collection",
"method": "GET"
}
}, {
"name": "A simple GET request",
"event": [{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('response code is 200', function () {",
" pm.response.to.be.ok;",
" pm.response.to.have.header('Content-Type', 'application/html; charset=utf-8');",
"});"
]
}
}],
"request": {
"url": "http://postman-echo.com/type/html?source=newman-sample-github-collection",
"method": "GET"
}
}
]
}
46 changes: 46 additions & 0 deletions test/library/proxy-config-list.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
describe('Proxy Configuration', function () {
var proxyConfigListPath = 'test/fixtures/files/proxy-config.json',
collection = 'test/fixtures/run/proxy-config-list.json';

it('should work correctly with proxy configuration list', function (done) {
newman.run({
collection: collection,
proxyConfigList: proxyConfigListPath
}, done);
});

it('should bail out if proxy configuration list file does not exist', function (done) {
newman.run({
collection: collection,
proxyConfigList: 'invalid-proxy-config-file.json' // using an invalid proxy config list
}, function (err) {
expect(err).to.exist;
expect(err.message)
.to.equal('unable to read the proxy configuration file "invalid-proxy-config-file.json"');
done();
});
});

it('should bail out if unable to parse proxy configuration list', function (done) {
newman.run({
collection: collection,
proxyConfigList: './proxy-config-list-test.js' // using an invalid proxy config list
}, function (err) {
expect(err).to.exist;
expect(err.message)
.to.equal('unable to read the proxy configuration file "./proxy-config-list-test.js"');
done();
});
});

it('should bail if proxy configuration list file path is invalid', function (done) {
newman.run({
collection: collection,
proxyConfigList: {}
}, function (err) {
expect(err).to.exist;
expect(err.message).to.equal('path for proxy configuration list file must be a string');
done();
});
});
});