Skip to content

Commit

Permalink
Update stats extractor to use user agent parser
Browse files Browse the repository at this point in the history
  • Loading branch information
pfleidi committed Jan 31, 2016
1 parent 0bad9ee commit 9377727
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
23 changes: 8 additions & 15 deletions lib/stats_extractor.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
'use strict';

var useragent = require('useragent');
const parseUserAgent = require('./user_agent_parser');

module.exports = function statsExtractor(req, res) {

function extractUserAgent() {
return req.headers['user-agent'] || 'unknown';
}

function parseUserAgent() {
let ua = extractUserAgent();
let parsedUa = useragent.parse(ua);

return parsedUa.family;
}

function exctractStatistics(transferState, duration, sentBytes) {
let referrer = req.headers.referrer || 'unknown';
let userAgent = req.headers['user-agent'] || 'unknown';
let parsedUserAgent = parseUserAgent(userAgent);

return {
transfer_state: transferState,
transferred_bytes: sentBytes,
transfer_time: duration,
response_code: res.statusCode,
ip_address: req.ip,
country: '?',
raw_user_agent: extractUserAgent(),
parsed_user_agent: parseUserAgent(),
raw_referrer: req.headers.referrer || 'unknown',
raw_user_agent: userAgent,
parsed_user_agent: parsedUserAgent,
raw_referrer: referrer,
parsed_referrer: '?'
};
}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
"gulp-exit": "0.0.2",
"gulp-istanbul": "^0.10.3",
"gulp-mocha": "^2.2.0",
"proxyquire": "^1.7.3",
"sinon": "^1.17.3",
"supertest": "^1.1.0"
}
}
27 changes: 20 additions & 7 deletions test/unit/stats_extractor_test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
'use strict';

const assert = require('assert');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const _ = require('lodash');

const statsExtractor = require('../../lib/stats_extractor');
const parseUserAgent = sinon.stub();

const statsExtractor = proxyquire('../../lib/stats_extractor', {
'./user_agent_parser': parseUserAgent
});

describe('statsExtractor', function () {
const transferState = 'canceled';
Expand All @@ -29,16 +35,18 @@ describe('statsExtractor', function () {
const exctractStatistics = statsExtractor(req, res);

describe('without a user agent or a referrer', function () {
beforeEach(function () {
parseUserAgent.withArgs('unknown').returns('Other');
});

it('returns a valid response object', function () {
let stats = exctractStatistics('canceled', duration, sentBytes)
console.dir(req);
console.dir(stats);
let stats = exctractStatistics('canceled', duration, sentBytes);

assert.deepEqual(stats, expectedStatsTemplate);
});
});

describe('with a known user agent', function () {
describe('with a user agent', function () {
let userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36';

before(function () {
Expand All @@ -49,8 +57,13 @@ describe('statsExtractor', function () {
req.headers['user-agent'] = null;
});

it('returns a valid response object', function () {
let stats = exctractStatistics('canceled', duration, sentBytes)
beforeEach(function () {
parseUserAgent.withArgs(userAgent).returns('Chrome');
});

it('returns a response with the correctly parsed user agent', function () {
let stats = exctractStatistics('canceled', duration, sentBytes);

let expectedStats = _.merge(expectedStatsTemplate, {
raw_user_agent: userAgent,
parsed_user_agent: 'Chrome'
Expand Down

0 comments on commit 9377727

Please sign in to comment.