Skip to content

Commit

Permalink
Merge a17cdfe into 3e1c6ec
Browse files Browse the repository at this point in the history
  • Loading branch information
kenperkins committed Feb 11, 2015
2 parents 3e1c6ec + a17cdfe commit 9fbbf78
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
3 changes: 1 addition & 2 deletions lib/pkgcloud/amazon/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

var util = require('util'),
AWS = require('aws-sdk'),
pkgcloud = require('../../../../pkgcloud'),
base = require('../core/base');

var userAgent = AWS.util.userAgent();
Expand Down Expand Up @@ -46,7 +45,7 @@ var Client = exports.Client = function (options) {
});
}

this.userAgent = util.format('nodejs-pkgcloud/%s %s', pkgcloud.version, userAgent);
this.userAgent = util.format('%s %s', self.getUserAgent(), userAgent);

// Setup a custom user agent for pkgcloud
AWS.util.userAgent = function () {
Expand Down
26 changes: 25 additions & 1 deletion lib/pkgcloud/core/base/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ var Client = exports.Client = function (options) {

util.inherits(Client, events.EventEmitter2);

/**
* Client.setCustomUserAgent
*
* @description allows the caller to specify a custom prefix for the HTTP UserAgent
* for all queries generated during the lifetime of the client.
*
* @param {String} userAgent the new userAgent to be prefixed
*/
Client.prototype.setCustomUserAgent = function (userAgent) {
this._customUserAgent = userAgent;
};

/**
* Client.getUserAgent
*
* @description gets the full UserAgent for the current client
*
* @returns {string}
*/
Client.prototype.getUserAgent = function() {
return util.format('%snodejs-pkgcloud/%s', this._customUserAgent ?
this._customUserAgent + ' ' : '', pkgcloud.version);
};

/**
* Client._request
*
Expand Down Expand Up @@ -103,7 +127,7 @@ Client.prototype._request = function (options, callback) {
delete opts.signingUrl;

// Set our User Agent
opts.headers['User-Agent'] = util.format('nodejs-pkgcloud/%s', pkgcloud.version);
opts.headers['User-Agent'] = self.getUserAgent();

// If we are missing callback
if (!callback) {
Expand Down
7 changes: 7 additions & 0 deletions test/common/base/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

var should = require('should'),
pkgcloud = require('../../../lib/pkgcloud'),
Client = new require('../../../lib/pkgcloud/core/base/client').Client;

describe('pkgcloud/core/base/client', function () {
Expand Down Expand Up @@ -59,6 +60,12 @@ describe('pkgcloud/core/base/client', function () {
});
});

it('custom user agents should work', function () {
var cli = new Client();
cli.setCustomUserAgent('my-app/1.2.3');
cli.getUserAgent().should.equal('my-app/1.2.3 nodejs-pkgcloud/' + pkgcloud.version);
});

it('the before filters throwing an error without a callback should return the error on the EE', function(done) {
var cli = new Client();

Expand Down

0 comments on commit 9fbbf78

Please sign in to comment.