Skip to content

Commit

Permalink
Merge pull request #196 from opentable/client-useragent
Browse files Browse the repository at this point in the history
Adding user-agent to node oc-client
  • Loading branch information
matteofigus committed Mar 2, 2016
2 parents 255552c + f56285f commit 90d7269
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
12 changes: 12 additions & 0 deletions client/src/sanitiser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

var format = require('stringformat');

var packageInfo = require('../package');
var _ = require('./utils/helpers');

var lowerHeaderKeys = function(headers){
Expand All @@ -26,9 +29,18 @@ module.exports = {
options = {};
}

var defaultUserAgent = format('oc-client-{0}/{1}-{2}-{3}',
packageInfo.version,
process.version,
process.platform,
process.arch);

options = options || {};

options.headers = lowerHeaderKeys(options.headers);
options.headers.accept = 'application/vnd.oc.unrendered+json';
options.headers['user-agent'] = options.headers['user-agent'] || defaultUserAgent;

options.timeout = options.timeout || 5;
options.container = (options.container === true) ? true : false;
options.renderInfo = (options.renderInfo === false) ? false : true;
Expand Down
31 changes: 31 additions & 0 deletions test/unit/client-sanitiser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

var expect = require('chai').expect;
var injectr = require('injectr');

describe('client : sanitiser', function(){

var sanitiser = injectr('../../client/src/sanitiser.js', {
'../package': {
version: '1.2.3'
}
}, { process:
{
version: 'v0.10.40',
platform: 'darwin',
arch: 'x64'
}
});

describe('when sanitising global rendering options', function(){

describe('when user-agent not already set', function(){

var result = sanitiser.sanitiseGlobalRenderOptions({}, {});

it('should set oc-client user-agent', function(){
expect(result.headers['user-agent']).to.equal('oc-client-1.2.3/v0.10.40-darwin-x64');
});
});
});
});

0 comments on commit 90d7269

Please sign in to comment.