diff --git a/client/src/sanitiser.js b/client/src/sanitiser.js index 27963012d..3f088a826 100644 --- a/client/src/sanitiser.js +++ b/client/src/sanitiser.js @@ -1,5 +1,8 @@ 'use strict'; +var format = require('stringformat'); + +var packageInfo = require('../package'); var _ = require('./utils/helpers'); var lowerHeaderKeys = function(headers){ @@ -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; diff --git a/test/unit/client-sanitiser.js b/test/unit/client-sanitiser.js new file mode 100644 index 000000000..a651d30cb --- /dev/null +++ b/test/unit/client-sanitiser.js @@ -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'); + }); + }); + }); +}); \ No newline at end of file