Skip to content

Commit

Permalink
feat: Allow user agent to be modified (#98)
Browse files Browse the repository at this point in the history
This allows other libraries to prefix the user agent, rather than overwriting it entirely with passed options
  • Loading branch information
robertjd authored and robertdamphousse-okta committed Mar 16, 2018
1 parent 68b6403 commit 77c3839
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"max-depth": [2, 3],
"max-len": [2, 120],
"max-params": [2, 5],
"max-statements": [2, 20],
"max-statements": [2, 25],
"quotes": [2, "single"],
"semi": 2,
"strict": 0,
Expand Down
4 changes: 3 additions & 1 deletion lib/clientBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ function OktaAuthBuilder(args) {
headers: args.headers
};

this.userAgent = 'okta-auth-js-' + config.SDK_VERSION;

// Digital clocks will drift over time, so the server
// can misalign with the time reported by the browser.
// The maxClockSkew allows relaxing the time-based
Expand Down Expand Up @@ -271,7 +273,7 @@ module.exports = function(ajaxRequest) {
if (!(this instanceof OktaAuth)) {
return new OktaAuth(args);
}

if (args && !args.ajaxRequest) {
args.ajaxRequest = ajaxRequest;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function httpRequest(sdk, options) {
var headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Okta-User-Agent-Extended': 'okta-auth-js-' + config.SDK_VERSION
'X-Okta-User-Agent-Extended': sdk.userAgent
};
util.extend(headers, sdk.options.headers, options.headers);

Expand Down Expand Up @@ -66,7 +66,7 @@ function httpRequest(sdk, options) {

return res;
})
.fail(function(resp) {
.fail(function(resp) {
var serverErr = resp.responseText || {};
if (util.isString(serverErr)) {
try {
Expand Down
24 changes: 24 additions & 0 deletions test/spec/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,30 @@ define(function(require) {
});
});

describe('modified user agent', function () {
util.itMakesCorrectRequestResponse({
title: 'should be added to requests headers',
setup: {
request: {
uri: '/api/v1/sessions/me',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Okta-User-Agent-Extended': 'custom okta-auth-js-' + packageJson.version
}
},
response: 'session'
},
execute: function (test) {
test.oa.userAgent = 'custom ' + test.oa.userAgent;
return test.oa.session.get();
},
expectations: function (test, res) {
// We validate the headers for each request in our ajaxMock
}
});
});

describe('custom headers', function () {
util.itMakesCorrectRequestResponse({
title: 'adds custom headers',
Expand Down

0 comments on commit 77c3839

Please sign in to comment.