Skip to content

Commit

Permalink
[SDK-3653] Add proxy support to Management Client (#747)
Browse files Browse the repository at this point in the history
* Add proxy support to Management Client

* fix yarn

* yarn again

* Update src/management/index.js

Co-authored-by: Ewan Harris <ewanharris93@gmail.com>

Co-authored-by: Ewan Harris <ewanharris93@gmail.com>
  • Loading branch information
adamjmcgrath and ewanharris committed Oct 11, 2022
1 parent f8a1783 commit 209cc74
Show file tree
Hide file tree
Showing 5 changed files with 5,252 additions and 5,183 deletions.
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',
});
});
});

0 comments on commit 209cc74

Please sign in to comment.