diff --git a/packages/client/src/classes/client.js b/packages/client/src/classes/client.js index c91768d4d..1497ae1d8 100644 --- a/packages/client/src/classes/client.js +++ b/packages/client/src/classes/client.js @@ -38,7 +38,6 @@ class Client { setApiKey(apiKey) { this.auth = 'Bearer ' + apiKey; - this.setDefaultRequest('baseUrl', SENDGRID_BASE_URL); if (!this.isValidApiKey(apiKey)) { console.warn(`API key does not start with "${API_KEY_PREFIX}".`); @@ -48,7 +47,9 @@ class Client { setTwilioEmailAuth(username, password) { const b64Auth = Buffer.from(username + ':' + password).toString('base64'); this.auth = 'Basic ' + b64Auth; - this.setDefaultRequest('baseUrl', TWILIO_BASE_URL); + if (this.defaultRequest.baseUrl === SENDGRID_BASE_URL) { + this.setDefaultRequest('baseUrl', TWILIO_BASE_URL); + } if (!this.isValidTwilioAuth(username, password)) { console.warn('Twilio Email credentials must be non-empty strings.'); diff --git a/packages/client/src/client.spec.js b/packages/client/src/client.spec.js index 65e0e5997..9a526f173 100644 --- a/packages/client/src/client.spec.js +++ b/packages/client/src/client.spec.js @@ -106,6 +106,23 @@ describe('setImpersonateSubuser', () => { }); }); +describe('setDefaultRequest', () => { + const customBaseUrl = 'localhost:3030'; + const sgClient = require('./client'); + + it('should set the custom base URL without being overwritten by setApiKey', () => { + sgClient.setDefaultRequest('baseUrl', customBaseUrl); + sgClient.setApiKey('SG\.1234567890'); + expect(sgClient.defaultRequest.baseUrl).to.equal(customBaseUrl); + }); + + it('should set the custom base URL without being overwritten by setTwilioEmailAuth', () => { + sgClient.setDefaultRequest('baseUrl', customBaseUrl); + sgClient.setTwilioEmailAuth('username', 'password'); + expect(sgClient.defaultRequest.baseUrl).to.equal(customBaseUrl); + }); +}); + describe('test_access_settings_activity_get', () => { const request = {}; request.qs = {