From ac4e3e8ec94a5e11384642b23d0ecea3d2edef3a Mon Sep 17 00:00:00 2001 From: Christopher Rogers Date: Sat, 9 Sep 2023 00:50:53 -0700 Subject: [PATCH] Adds hostname configuration pass-through --- lib/recurly.js | 2 ++ lib/recurly/request.js | 4 ++++ test/unit/request.test.js | 10 ++++++++++ 3 files changed, 16 insertions(+) diff --git a/lib/recurly.js b/lib/recurly.js index 114ab927d..01e017e82 100644 --- a/lib/recurly.js +++ b/lib/recurly.js @@ -234,6 +234,8 @@ export class Recurly extends Emitter { if (options.publicKey) { this.config.publicKey = options.publicKey; + } else if (options.hostname) { + this.config.hostname = options.hostname; } else if (!this.config.publicKey) { throw errors('config-missing-public-key'); } diff --git a/lib/recurly/request.js b/lib/recurly/request.js index 1985c0b83..2730e5a07 100644 --- a/lib/recurly/request.js +++ b/lib/recurly/request.js @@ -297,6 +297,10 @@ export class Request { } }; + if (this.recurly.config.hostname && req.setRequestHeader) { + req.setRequestHeader('Recurly-Credential-Checkout-Hostname', this.recurly.config.hostname); + } + // XDR requests will abort if too many are sent simultaneously setTimeout(() => { if (method === 'post') { diff --git a/test/unit/request.test.js b/test/unit/request.test.js index 1cb4dd922..56aef92fa 100644 --- a/test/unit/request.test.js +++ b/test/unit/request.test.js @@ -240,11 +240,13 @@ describe('Request', () => { beforeEach(function () { sinon.spy(this.XHR.prototype, 'open'); sinon.spy(this.XHR.prototype, 'send'); + sinon.spy(this.XHR.prototype, 'setRequestHeader'); }); afterEach(function () { this.XHR.prototype.open.restore(); this.XHR.prototype.send.restore(); + this.XHR.prototype.setRequestHeader.restore(); }); describe('when performing a POST request', () => { @@ -271,6 +273,14 @@ describe('Request', () => { assert(this.XHR.prototype.send.calledWith()); }); }); + + describe('when configured with a hostname', () => { + it('Applies the hostname as a header', function () { + this.recurly.configure({ hostname: 'test-hostname.recurly.com' }); + this.request.request({ method: 'get', route: 'test' }); + assert(this.XHR.prototype.setRequestHeader.calledWithMatch('Recurly-Credential-Checkout-Hostname', 'test-hostname.recurly.com')); + }); + }); }); }); });