Skip to content

Commit

Permalink
Prevent calling the callback more than once (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
snocorp committed Dec 9, 2022
1 parent 7c3e31a commit aa8d938
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Auth0RestClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ class Auth0RestClient {
this.restClient[method](...args, (err, data, headers) => {
const payload = { data, headers };
if (err) {
(callback && callback(err)) || reject(err);
if (callback) callback(err);
else reject(err);
return;
}
(callback && callback(null, payload)) || resolve(payload);
if (callback) callback(null, payload);
else resolve(payload);
});
});
}
Expand Down
23 changes: 23 additions & 0 deletions test/auth0-rest-client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,29 @@ describe('Auth0RestClient', () => {
});
});

it('should accept a callback and handle errors with response headers', (done) => {
const providerMock = {
async getAccessToken() {
return 'fake-token';
},
};

const errorBody = { error: 'message' };
nock(API_URL).get('/some-resource').reply(500, errorBody);

const options = {
headers: {},
includeResponseHeaders: true,
};
const client = new Auth0RestClient(`${API_URL}/some-resource`, options, providerMock);
client.getAll((err) => {
expect(err).to.not.null;
expect(err.message).to.be.equal(JSON.stringify(errorBody));
done();
nock.cleanAll();
});
});

it('should set access token as Authorization header in options object', async function () {
nock(API_URL).get('/some-resource').reply(200);

Expand Down

0 comments on commit aa8d938

Please sign in to comment.