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

[SDK-3653] Add proxy support to Management Client #747

Merged
merged 4 commits into from Oct 11, 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 package.json
Expand Up @@ -65,6 +65,7 @@
"proxyquire": "^2.1.3",
"sinon": "^14.0.0",
"string-replace-webpack-plugin": "0.1.3",
"superagent-proxy": "^3.0.0",
"webpack": "^4.36.1"
}
}
1 change: 1 addition & 0 deletions src/management/BaseManager.js
Expand Up @@ -24,6 +24,7 @@ class BaseManager {
const clientOptions = {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
proxy: options.proxy,
query: { repeatParams: false },
includeResponseHeaders: options.includeResponseHeaders,
};
Expand Down
4 changes: 3 additions & 1 deletion src/management/index.js
Expand Up @@ -90,7 +90,8 @@ class ManagementClient {
* @param {boolean} [options.retry.enabled=true] Enabled or Disable Retry Policy functionality.
* @param {number} [options.retry.maxRetries=10] Retry failed requests X times.
* @param {object} [options.headers] Additional headers that will be added to the outgoing requests.
* @param {object} [options.includeResponseHeaders] Include the response headers in the payload in the format `{ data, headers }`.
* @param {string} [options.proxy] Add the `superagent-proxy` dependency and specify a proxy url eg 'https://myproxy.com:1234'
* @param {boolean} [options.includeResponseHeaders] Include the response headers in the payload in the format `{ data, headers }`.
*/
constructor(options) {
if (!options || typeof options !== 'object') {
Expand Down Expand Up @@ -149,6 +150,7 @@ class ManagementClient {

managerOptions.retry = options.retry;
managerOptions.includeResponseHeaders = options.includeResponseHeaders;
managerOptions.proxy = options.proxy;

/**
* Simple abstraction for performing CRUD operations on the
Expand Down
32 changes: 31 additions & 1 deletion test/auth0-rest-client.tests.js
@@ -1,8 +1,10 @@
const { expect } = require('chai');
const nock = require('nock');
const sinon = require('sinon');

const { ArgumentError } = require('rest-facade');
const { ArgumentError, Client } = require('rest-facade');
const Auth0RestClient = require('../src/Auth0RestClient');
const proxyquire = require('proxyquire');

const API_URL = 'https://tenant.auth0.com';

Expand Down Expand Up @@ -260,4 +262,32 @@ describe('Auth0RestClient', () => {
done();
});
});

it('should make request with proxy', async function () {
const spy = sinon.spy();
class MockClient extends Client {
constructor(...args) {
spy(...args);
super(...args);
}
}
const RestClient = proxyquire('../src/Auth0RestClient', {
'rest-facade': {
Client: MockClient,
},
});
nock(API_URL).get('/some-resource').reply(200, { data: 'value' });

const options = {
headers: {},
proxy: 'http://proxy',
};

const client = new RestClient(`${API_URL}/some-resource`, options, this.providerMock);
const data = await client.getAll();
expect(data).to.deep.equal({ data: 'value' });
sinon.assert.calledWithMatch(spy, 'https://tenant.auth0.com/some-resource', {
proxy: 'http://proxy',
});
});
});