Skip to content

Commit

Permalink
Adds ability to specifiy a custom user agent to be appended to user a…
Browse files Browse the repository at this point in the history
…gent headers.
  • Loading branch information
chrisradek committed Dec 2, 2015
1 parent 93147f2 commit 19a0e75
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ require('./credentials/credential_provider_chain');
* requests that fail because of an skewed client clock. Defaults to
* `false`.
*
* @!attribute customUserAgent
* @return [String] a custom identifier to append to user agent headers.
*
* @!attribute sslEnabled
* @return [Boolean] whether SSL is enabled for requests
*
Expand Down Expand Up @@ -150,6 +153,8 @@ AWS.Config = AWS.util.inherit({
* @option options correctClockSkew [Boolean] whether to apply a clock skew
* correction and retry requests that fail because of an skewed client
* clock. Defaults to `false`.
* @option options customUserAgent [String] A custom identifier to append to
* user agent headers.
* @option options s3ForcePathStyle [Boolean] whether to force path
* style URLs for S3 objects.
* @option options s3BucketEndpoint [Boolean] whether the provided endpoint
Expand Down Expand Up @@ -405,6 +410,7 @@ AWS.Config = AWS.util.inherit({
computeChecksums: true,
convertResponseTypes: true,
correctClockSkew: false,
customUserAgent: null,
dynamoDbCrc32: true,
systemClockOffset: 0,
signatureVersion: null,
Expand Down
9 changes: 5 additions & 4 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,24 @@ AWS.HttpRequest = inherit({
/**
* @api private
*/
constructor: function HttpRequest(endpoint, region) {
constructor: function HttpRequest(endpoint, region, customUserAgent) {
endpoint = new AWS.Endpoint(endpoint);
this.method = 'POST';
this.path = endpoint.path || '/';
this.headers = {};
this.body = '';
this.endpoint = endpoint;
this.region = region;
this.setUserAgent();
this.setUserAgent(customUserAgent);
},

/**
* @api private
*/
setUserAgent: function setUserAgent() {
setUserAgent: function setUserAgent(customUserAgent) {
var prefix = AWS.util.isBrowser() ? 'X-Amz-' : '';
this.headers[prefix + 'User-Agent'] = AWS.util.userAgent();
var customSuffix = (typeof customUserAgent === 'string' && customUserAgent) ? '/' + customUserAgent : '';
this.headers[prefix + 'User-Agent'] = AWS.util.userAgent() + customSuffix;
},

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ AWS.Request = inherit({
constructor: function Request(service, operation, params) {
var endpoint = service.endpoint;
var region = service.config.region;
var customUserAgent = service.config.customUserAgent;

// global endpoints sign as us-east-1
if (service.isGlobalEndpoint) region = 'us-east-1';
Expand All @@ -312,7 +313,7 @@ AWS.Request = inherit({
this.service = service;
this.operation = operation;
this.params = params || {};
this.httpRequest = new AWS.HttpRequest(endpoint, region);
this.httpRequest = new AWS.HttpRequest(endpoint, region, customUserAgent);
this.startTime = AWS.util.date.getDate();

this.response = new AWS.Response(this);
Expand Down
4 changes: 4 additions & 0 deletions test/config.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ describe 'AWS.Config', ->
it 'defaults to false', ->
expect(configure().correctClockSkew).to.equal(false)

describe 'customUserAgent', ->
it 'defaults to null', ->
expect(configure().customUserAgent).to.equal(null)

describe 'set', ->
it 'should set a default value for a key', ->
config = new AWS.Config()
Expand Down
6 changes: 6 additions & 0 deletions test/http_request.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ describe 'AWS.HttpRequest', ->
headers[agentHeader] = AWS.util.userAgent()
expect(request.headers).to.eql(headers)

it 'adds the customUserAgent to the user agent header if provided', ->
headers = {}
headers[agentHeader] = AWS.util.userAgent() + '/custom'
request = new AWS.HttpRequest('http://domain.com', '', 'custom')
expect(request.headers).to.eql(headers)

it 'defaults body to empty string', ->
expect(request.body).to.equal('')

Expand Down

0 comments on commit 19a0e75

Please sign in to comment.