Skip to content

Commit

Permalink
Merge cf6323f into f75fc77
Browse files Browse the repository at this point in the history
  • Loading branch information
ewandennis committed Nov 9, 2016
2 parents f75fc77 + cf6323f commit dafe3f3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ npm install sparkpost
* Required: no
* Type: `String`
* Default: `v1`
* `options.stackIdentity`
* Required: no
* Type: `String`
* An optional identifier to include in the User-Agent header. e.g. `product/1.0.0`
* `options.headers`
* Required: no
* Type: `Object`
Expand Down
12 changes: 10 additions & 2 deletions lib/sparkpost.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var version = require('../package.json').version
, Promise = require('./Promise')
, request = require('request')
, _ = require('lodash')
, defaults, resolveUri, handleOptions, createSparkPostError, SparkPost;
, defaults, resolveUri, handleOptions, createSparkPostError, createVersionStr, SparkPost;

//REST API Config Defaults
defaults = {
Expand Down Expand Up @@ -45,6 +45,14 @@ createSparkPostError = function(res, body) {
return err;
};

createVersionStr = function(version, options) {
let versionStr = 'node-sparkpost/' + version + ' node.js/' + process.version;
if (options.stackIdentity) {
versionStr += options.stackIdentity + ' ' + versionStr;
}
return versionStr;
};

SparkPost = function(apiKey, options) {

options = handleOptions(apiKey, options);
Expand All @@ -60,7 +68,7 @@ SparkPost = function(apiKey, options) {

// setting up default headers
this.defaultHeaders = _.merge({
'User-Agent': 'node-sparkpost/' + this.version
'User-Agent': createVersionStr(version, options)
, 'Content-Type': 'application/json'
}, options.headers);

Expand Down
46 changes: 45 additions & 1 deletion test/spec/sparkpost.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ var chai = require('chai')
, sinon = require('sinon')
, zlib = require('zlib')
, nock = require('nock')
, SparkPost = require('../../lib/sparkpost');
, SparkPost = require('../../lib/sparkpost')
, libVersion = require('../../package.json').version;

chai.use(require('sinon-chai'));


describe('SparkPost Library', function() {

it('should be a constructor', function() {
Expand Down Expand Up @@ -81,6 +83,48 @@ describe('SparkPost Library', function() {
expect(client.debug).to.equal(true);
});

function checkUserAgent(clientOptions, checkFn, done) {
let req = {
method: 'GET'
, uri: 'get/test'
, json: true
, debug: true
}
, client;

nock('https://api.sparkpost.com')
.get('/api/v1/get/test')
.reply(200, function() {
expect(this.req.headers).to.have.property('user-agent');
checkFn(this.req.headers['user-agent']);
return { ok: true };
});

client = new SparkPost('123456789', clientOptions);
client.request(req, done);
}

it('should allow users to self identify in user-agent', function(done) {
let options = {
stackIdentity: 'phantasmatron/1.1.3.8'
}
checkUserAgent(options, function(userAgent) {
expect(userAgent).to.include(options.stackIdentity);
}, done);
});

it('should include lib version in user-agent', function(done) {
checkUserAgent({}, function(userAgent) {
expect(userAgent).to.include('node-sparkpost/' + libVersion);
}, done);
});

it('should include Node.JS version in user-agent', function(done) {
checkUserAgent({}, function(userAgent) {
expect(userAgent).to.include('node.js/' + process.version);
}, done);
});

describe('request method', function() {
var client;

Expand Down

0 comments on commit dafe3f3

Please sign in to comment.