Skip to content

Commit

Permalink
fix(request): include node version in user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
gurpreetatwal committed Jul 11, 2018
1 parent 9a9398b commit c0205f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
11 changes: 10 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const _ = require('lodash');
const request = require('request-promise');
const {format} = require('util');
const {StatusCodeError} = require('request-promise/errors');

const config = require('./config');
Expand All @@ -10,6 +11,14 @@ const {version} = require('../package.json');

const util = {};

util.USER_AGENT = format(
'Smartcar/%s (%s; %s) Node.js %s',
version,
process.platform,
process.arch,
process.version
);

/**
* Format Access object and set expiration properties.
*
Expand Down Expand Up @@ -53,7 +62,7 @@ util.getUrl = function(id, endpoint) {
util.request = request.defaults({
json: true,
headers: {
'User-Agent': `smartcar-node-sdk:${version}`,
'User-Agent': util.USER_AGENT,
},
});

Expand Down
9 changes: 5 additions & 4 deletions test/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const _ = require('lodash');
const test = require('ava');
const nock = require('nock');
const Promise = require('bluebird');
const StatusCodeError = require('request-promise/errors').StatusCodeError;
const {StatusCodeError} = require('request-promise/errors');

const util = require('../../lib/util');
const config = require('../../lib/config');
Expand Down Expand Up @@ -63,13 +63,14 @@ test('getUrl - id & endpoint', function(t) {
t.is(url, API_URL + '/vehicles/VID/odometer');
});


test('request - default opts', async function(t) {

const n = nock('https://mock.com')
.get('/test')
.matchHeader('accept', 'application/json')
.matchHeader('user-agent', /smartcar-node-sdk:.*/)
.matchHeader(
'user-agent',
/^Smartcar\/(\d+\.\d+\.\d+-[\w-]*) \((\w+); (\w+)\) Node.js v(\d+\.\d+\.\d+)$/
)
.reply(200, {test: 'data'});

const response = await util.request('https://mock.com/test');
Expand Down
3 changes: 1 addition & 2 deletions test/lib/vehicle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ const nock = require('nock');
const test = require('ava');

const Vehicle = require('../../lib/vehicle');
const {version} = require('../../package.json');
const {USER_AGENT} = require('../../lib/util');

const VID = 'ada7207c-3c0a-4027-a47f-6215ce6f7b93';
const TOKEN = '9ad942c6-32b8-4af2-ada6-5e8ecdbad9c2';
const USER_AGENT = `smartcar-node-sdk:${version}`;

const vehicle = new Vehicle(VID, TOKEN);

Expand Down

0 comments on commit c0205f0

Please sign in to comment.