Skip to content
This repository has been archived by the owner on Feb 23, 2019. It is now read-only.

Commit

Permalink
Make Agents include their usernames in the X-Thorn header (#144)
Browse files Browse the repository at this point in the history
This makes it easier to distinguish which is which when using verbose mode.
  • Loading branch information
rhoggSugarcrm committed Feb 25, 2017
1 parent 49ae696 commit d93f94f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ class UserAgent {
username: this.username,
password: this.password,
version: this.version,
xthorn: 'Agent',
xthorn: this._xthorn(),
}).then((response) => {
this._updateAuthState(response);
this._setState('sessionAttempt', 0);
Expand Down Expand Up @@ -672,7 +672,7 @@ class UserAgent {
return utils.wrapRequest(chakramMethod, args, {
refreshToken: this._getState('refreshToken'),
afterRefresh: _.bind(this._updateAuthState, this),
xthorn: 'Agent',
xthorn: this._xthorn(),
retryVersion: this.version,
});
});
Expand Down Expand Up @@ -704,7 +704,7 @@ class UserAgent {
_state: {
headers: {
'Content-Type': 'application/json',
'X-Thorn': 'Agent',
'X-Thorn': this._xthorn(),
},
},
};
Expand Down Expand Up @@ -737,6 +737,15 @@ class UserAgent {
cachedAgents[this.username]._state[key] = value;
};

/**
* Return the desired value for the X-Thorn header.
*
* @return {string} The value of the X-Thorn header.
*
* @private
*/
_xthorn = () => `Agent-${this.username}`;

/**
* Perform a GET request as this user.
*
Expand Down
8 changes: 4 additions & 4 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ describe('Thorn', () => {
let server = nock(process.env.THORN_SERVER_URL)
.get(isNotRealEndpoint)
.reply(200, function(uri, requestBody) {
expect(this.req.headers['x-thorn']).to.equal('Agent');
expect(this.req.headers['x-thorn']).to.equal('Agent-foo');
return [];
});
let getRequest = myAgent.get(endpoint);
Expand All @@ -897,7 +897,7 @@ describe('Thorn', () => {
let server = nock(process.env.THORN_SERVER_URL)
.post(isNotRealEndpoint)
.reply(200, function(uri, requestBody) {
expect(this.req.headers['x-thorn']).to.equal('Agent');
expect(this.req.headers['x-thorn']).to.equal('Agent-foo');
expect(requestBody).to.eql(data);

return [];
Expand All @@ -917,7 +917,7 @@ describe('Thorn', () => {
let server = nock(process.env.THORN_SERVER_URL)
.put(isNotRealEndpoint)
.reply(200, function(uri, requestBody) {
expect(this.req.headers['x-thorn']).to.equal('Agent');
expect(this.req.headers['x-thorn']).to.equal('Agent-foo');
expect(requestBody).to.eql(data);

return [];
Expand All @@ -937,7 +937,7 @@ describe('Thorn', () => {
let server = nock(process.env.THORN_SERVER_URL)
.delete(isNotRealEndpoint)
.reply(200, function(uri, requestBody) {
expect(this.req.headers['x-thorn']).to.equal('Agent');
expect(this.req.headers['x-thorn']).to.equal('Agent-foo');
expect(requestBody).to.eql(data);

return [];
Expand Down

0 comments on commit d93f94f

Please sign in to comment.