Skip to content

Commit

Permalink
Merge pull request #46 from recurly/hostname
Browse files Browse the repository at this point in the history
Adds hostname configuration pass-through
  • Loading branch information
chrissrogers committed Sep 26, 2023
1 parent 7e577df commit dde3687
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/recurly.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
4 changes: 4 additions & 0 deletions lib/recurly/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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'));
});
});
});
});
});
Expand Down

0 comments on commit dde3687

Please sign in to comment.