From a458ce2a4ebe2fad090c58231fe3bd9954c331bc Mon Sep 17 00:00:00 2001 From: Manuel Tanzi Date: Wed, 27 Mar 2019 09:10:23 -0700 Subject: [PATCH 1/7] chore: Use fetch as default for browsers instead of reqwest --- fetch/fetchRequest.js | 4 ++-- fetch/index.js | 17 ----------------- lib/browser/browserIndex.js | 6 ++++-- lib/server/serverIndex.js | 6 ++++-- 4 files changed, 10 insertions(+), 23 deletions(-) delete mode 100644 fetch/index.js diff --git a/fetch/fetchRequest.js b/fetch/fetchRequest.js index 78a5b0dcd..3f2d16369 100644 --- a/fetch/fetchRequest.js +++ b/fetch/fetchRequest.js @@ -33,8 +33,8 @@ function fetchRequest(method, url, args) { } return result; }; - if (response.headers.get('Accept') && - response.headers.get('Accept').toLowerCase().indexOf('application/json') >= 0) { + if (response.headers.get('Content-Type') && + response.headers.get('Content-Type').toLowerCase().indexOf('application/json') >= 0) { return response.json().then(respHandler); } else { return response.text().then(respHandler); diff --git a/fetch/index.js b/fetch/index.js deleted file mode 100644 index 7961689d2..000000000 --- a/fetch/index.js +++ /dev/null @@ -1,17 +0,0 @@ -/*! - * Copyright (c) 2018-present, Okta, Inc. and/or its affiliates. All rights reserved. - * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.") - * - * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and limitations under the License. - * - */ - -var fetchRequest = require('./fetchRequest'); -var storageUtil = require('../lib/server/serverStorage'); - -module.exports = require('../lib/server/server')(storageUtil, fetchRequest); diff --git a/lib/browser/browserIndex.js b/lib/browser/browserIndex.js index b100651c9..91822a4a8 100644 --- a/lib/browser/browserIndex.js +++ b/lib/browser/browserIndex.js @@ -11,5 +11,7 @@ * */ -// This exists to use reqwest for http requests by default when library used on browser -module.exports = require('../../reqwest'); +var fetchRequest = require('../../fetch/fetchRequest'); +var storageUtil = require('./browserStorage'); + +module.exports = require('./browser')(storageUtil, fetchRequest); diff --git a/lib/server/serverIndex.js b/lib/server/serverIndex.js index f2d24ccfa..e1019bec8 100644 --- a/lib/server/serverIndex.js +++ b/lib/server/serverIndex.js @@ -11,5 +11,7 @@ * */ -// This exists to use fetch for http requests by default when library used on server side -module.exports = require('../../fetch'); +var fetchRequest = require('../../fetch/fetchRequest'); +var storageUtil = require('./serverStorage'); + +module.exports = require('./server')(storageUtil, fetchRequest); From 47be450744286550420f2287c6d719fe80bd0df8 Mon Sep 17 00:00:00 2001 From: Manuel Tanzi Date: Wed, 27 Mar 2019 09:11:34 -0700 Subject: [PATCH 2/7] Convert tests to use fetch instead of jquery --- jest.browser.json | 2 +- jest.server.json | 9 +++++++-- test/spec/fingerprint.js | 2 ++ test/spec/oauthUtil.js | 2 ++ test/spec/token.js | 2 ++ test/util/util.js | 40 ++++++++++++++++++++++++---------------- 6 files changed, 38 insertions(+), 19 deletions(-) diff --git a/jest.browser.json b/jest.browser.json index af27b09df..0ac65e486 100644 --- a/jest.browser.json +++ b/jest.browser.json @@ -3,7 +3,7 @@ "coverageDirectory": "./build2/reports/coverage", "restoreMocks": true, "moduleNameMapper": { - "^OktaAuth(.*)$": "/jquery/$1" + "^OktaAuth$": "/lib/browser/browserIndex" }, "testMatch": [ "**/test/spec/*.js" diff --git a/jest.server.json b/jest.server.json index b677696cb..3decc1061 100644 --- a/jest.server.json +++ b/jest.server.json @@ -2,13 +2,18 @@ "coverageDirectory": "./build2/reports/coverage", "restoreMocks": true, "moduleNameMapper": { - "^OktaAuth(.*)$": "/jquery/$1" + "^OktaAuth$": "/lib/server/serverIndex" }, "testMatch": [ "**/test/spec/*.js" ], "testPathIgnorePatterns": [ - "./test/spec/oauthUtil.js" + "./test/spec/fingerprint.js", + "./test/spec/general.js", + "./test/spec/oauthUtil.js", + "./test/spec/token.js", + "./test/spec/tokenManager.js", + "./test/spec/webfinger.js" ], "reporters": [ "default", diff --git a/test/spec/fingerprint.js b/test/spec/fingerprint.js index c3ce542e7..0b61bf610 100644 --- a/test/spec/fingerprint.js +++ b/test/spec/fingerprint.js @@ -2,6 +2,8 @@ var OktaAuth = require('OktaAuth'); var util = require('../util/util'); var packageJson = require('../../package.json'); +jest.mock('cross-fetch'); + describe('fingerprint', function() { function setup(options) { options = options || {}; diff --git a/test/spec/oauthUtil.js b/test/spec/oauthUtil.js index 89e9ec8c4..4a317a099 100644 --- a/test/spec/oauthUtil.js +++ b/test/spec/oauthUtil.js @@ -7,6 +7,8 @@ var wellKnown = require('../xhr/well-known'); var keys = require('../xhr/keys'); var tokens = require('../util/tokens'); +jest.mock('cross-fetch'); + describe('getWellKnown', function() { util.itMakesCorrectRequestResponse({ title: 'caches response and uses cache on subsequent requests', diff --git a/test/spec/token.js b/test/spec/token.js index 4eb089a09..ec89221ca 100644 --- a/test/spec/token.js +++ b/test/spec/token.js @@ -6,6 +6,8 @@ var packageJson = require('../../package.json'); var _ = require('lodash'); var Q = require('q'); +jest.mock('cross-fetch'); + function setupSync() { return new OktaAuth({ issuer: 'http://example.okta.com' }); } diff --git a/test/util/util.js b/test/util/util.js index 0f02bb0dc..22558b5ba 100644 --- a/test/util/util.js +++ b/test/util/util.js @@ -1,10 +1,12 @@ /* globals expect, JSON */ /* eslint-disable max-statements, complexity */ var Q = require('q'), - $ = require('jquery'), _ = require('lodash'), OktaAuth = require('OktaAuth'), - cookies = require('../../lib/browser/browserStorage').storage; + cookies = require('../../lib/browser/browserStorage').storage, + fetch = require('cross-fetch'); + +jest.mock('cross-fetch'); var util = {}; @@ -80,44 +82,50 @@ function mockAjax(pairs) { setNextPair(pairs); } - jest.spyOn($, 'ajax').mockImplementation(function(args) { - + fetch.mockImplementation(function (url, args) { var pair = allPairs.shift(); if (!pair) { throw new Error('We are making a request that we have not anticipated.'); } // Make sure every request is attaching cookies - expect(args.xhrFields).toEqual({ - withCredentials: true - }); + expect(args.credentials).toEqual('include'); if (pair.request) { - expect(pair.request.uri).toEqual(args.url); - if (pair.request.data || args.data) { - expect(pair.request.data).toEqual(JSON.parse(args.data)); + expect(pair.request.uri).toEqual(url); + if (pair.request.data || args.body) { + expect(pair.request.data).toEqual(JSON.parse(args.body)); } if (pair.request.headers) { expect(pair.request.headers).toEqual(args.headers); } } - var deferred = $.Deferred(); + var deferred = Q.defer(); var xhr = pair.response; + xhr.headers = xhr.headers || {}; + xhr.headers['Content-Type'] = 'application/json'; + xhr.headers.get = function(attr) { + return xhr.headers[attr]; + } + xhr.ok = xhr.status >= 200 && xhr.status < 300; + xhr.json = function() { + return Q.Promise(function(resolve) { + resolve(xhr.responseText); + }); + } xhr.getResponseHeader = function(name) { return xhr.headers && xhr.headers[name]; }; if (xhr.status > 0 && xhr.status < 300) { - // $.ajax send (data, textStatus, jqXHR) on success - _.defer(function () { deferred.resolve(xhr.response, null, xhr); }); + _.defer(function () { deferred.resolve(xhr); }); } else { - // $.ajax send (jqXHR, textStatus, errorThrown) on failure xhr.responseJSON = xhr.response; - deferred.reject(xhr, null, xhr.response); + deferred.reject(xhr); } - return deferred; + return deferred.promise; }); return { From 42559605faf28b9d8b6b3aa4444d26bf62e0f7c1 Mon Sep 17 00:00:00 2001 From: Manuel Tanzi Date: Mon, 1 Apr 2019 10:53:22 -0700 Subject: [PATCH 3/7] Addressing comments --- jest.browser.json | 3 --- jest.server.json | 3 --- lib/token.js | 2 +- test/spec/errors.js | 2 +- test/spec/fingerprint.js | 2 +- test/spec/oauthUtil.js | 2 +- test/spec/token.js | 2 +- test/spec/tokenManager.js | 2 +- test/util/oauthUtil.js | 2 +- test/util/util.js | 6 +----- 10 files changed, 8 insertions(+), 18 deletions(-) diff --git a/jest.browser.json b/jest.browser.json index 0ac65e486..224002e7c 100644 --- a/jest.browser.json +++ b/jest.browser.json @@ -2,9 +2,6 @@ "browser": true, "coverageDirectory": "./build2/reports/coverage", "restoreMocks": true, - "moduleNameMapper": { - "^OktaAuth$": "/lib/browser/browserIndex" - }, "testMatch": [ "**/test/spec/*.js" ], diff --git a/jest.server.json b/jest.server.json index 3decc1061..0f7479703 100644 --- a/jest.server.json +++ b/jest.server.json @@ -1,9 +1,6 @@ { "coverageDirectory": "./build2/reports/coverage", "restoreMocks": true, - "moduleNameMapper": { - "^OktaAuth$": "/lib/server/serverIndex" - }, "testMatch": [ "**/test/spec/*.js" ], diff --git a/lib/token.js b/lib/token.js index 7b342e0bd..179f716b8 100644 --- a/lib/token.js +++ b/lib/token.js @@ -594,7 +594,7 @@ function getUserInfo(sdk, accessTokenObject) { }) .fail(function(err) { if (err.xhr && (err.xhr.status === 401 || err.xhr.status === 403)) { - var authenticateHeader = err.xhr.getResponseHeader('WWW-Authenticate'); + var authenticateHeader = err.xhr.headers.get('WWW-Authenticate'); if (authenticateHeader) { var errorMatches = authenticateHeader.match(/error="(.*?)"/) || []; var errorDescriptionMatches = authenticateHeader.match(/error_description="(.*?)"/) || []; diff --git a/test/spec/errors.js b/test/spec/errors.js index d6f902084..9046c4ec9 100644 --- a/test/spec/errors.js +++ b/test/spec/errors.js @@ -1,6 +1,6 @@ var _ = require('lodash'), util = require('../util/util'), - OktaAuth = require('OktaAuth'); + OktaAuth = require('../../lib/server/serverIndex'); describe('General Errors', function () { util.itErrorsCorrectly({ diff --git a/test/spec/fingerprint.js b/test/spec/fingerprint.js index 0b61bf610..15ae3eb03 100644 --- a/test/spec/fingerprint.js +++ b/test/spec/fingerprint.js @@ -1,4 +1,4 @@ -var OktaAuth = require('OktaAuth'); +var OktaAuth = require('../../lib/server/serverIndex'); var util = require('../util/util'); var packageJson = require('../../package.json'); diff --git a/test/spec/oauthUtil.js b/test/spec/oauthUtil.js index 4a317a099..8316a5e67 100644 --- a/test/spec/oauthUtil.js +++ b/test/spec/oauthUtil.js @@ -1,4 +1,4 @@ -var OktaAuth = require('OktaAuth'); +var OktaAuth = require('../../lib/server/serverIndex'); var oauthUtil = require('../../lib/oauthUtil'); var libUtil = require('../../lib/util'); var oauthUtilHelpers = require('../util/oauthUtil'); diff --git a/test/spec/token.js b/test/spec/token.js index ec89221ca..2c4b62017 100644 --- a/test/spec/token.js +++ b/test/spec/token.js @@ -1,4 +1,4 @@ -var OktaAuth = require('OktaAuth'); +var OktaAuth = require('../../lib/server/serverIndex'); var tokens = require('../util/tokens'); var util = require('../util/util'); var oauthUtil = require('../util/oauthUtil'); diff --git a/test/spec/tokenManager.js b/test/spec/tokenManager.js index e9213a538..a6caad8c5 100644 --- a/test/spec/tokenManager.js +++ b/test/spec/tokenManager.js @@ -1,4 +1,4 @@ -var OktaAuth = require('OktaAuth'); +var OktaAuth = require('../../lib/server/serverIndex'); var tokens = require('../util/tokens'); var util = require('../util/util'); var oauthUtil = require('../util/oauthUtil'); diff --git a/test/util/oauthUtil.js b/test/util/oauthUtil.js index 97df8bc7a..dc5f89869 100644 --- a/test/util/oauthUtil.js +++ b/test/util/oauthUtil.js @@ -1,6 +1,6 @@ /* eslint-disable complexity, max-statements */ var util = require('../util/util'); -var OktaAuth = require('OktaAuth'); +var OktaAuth = require('../../lib/server/serverIndex'); var tokens = require('./tokens'); var Q = require('q'); var EventEmitter = require('tiny-emitter'); diff --git a/test/util/util.js b/test/util/util.js index 22558b5ba..d7c23c702 100644 --- a/test/util/util.js +++ b/test/util/util.js @@ -2,7 +2,7 @@ /* eslint-disable max-statements, complexity */ var Q = require('q'), _ = require('lodash'), - OktaAuth = require('OktaAuth'), + OktaAuth = require('../../lib/server/serverIndex'), cookies = require('../../lib/browser/browserStorage').storage, fetch = require('cross-fetch'); @@ -115,10 +115,6 @@ function mockAjax(pairs) { }); } - xhr.getResponseHeader = function(name) { - return xhr.headers && xhr.headers[name]; - }; - if (xhr.status > 0 && xhr.status < 300) { _.defer(function () { deferred.resolve(xhr); }); } else { From b5e3dc011de92ecf7b8021ef9d6b5cb119425e6c Mon Sep 17 00:00:00 2001 From: Manuel Tanzi Date: Mon, 1 Apr 2019 13:40:06 -0700 Subject: [PATCH 4/7] Addressing comments --- jest.browser.json => jest.browser.js | 11 ++++++++--- jest.server.json => jest.server.js | 10 ++++++++-- package.json | 4 ++-- test/spec/errors.js | 2 +- test/spec/fingerprint.js | 2 +- test/spec/oauthUtil.js | 2 +- test/spec/token.js | 2 +- test/spec/tokenManager.js | 2 +- test/util/oauthUtil.js | 2 +- test/util/util.js | 2 +- 10 files changed, 25 insertions(+), 14 deletions(-) rename jest.browser.json => jest.browser.js (57%) rename jest.server.json => jest.server.js (69%) diff --git a/jest.browser.json b/jest.browser.js similarity index 57% rename from jest.browser.json rename to jest.browser.js index 224002e7c..b43a185d7 100644 --- a/jest.browser.json +++ b/jest.browser.js @@ -1,7 +1,12 @@ -{ - "browser": true, +var packageJson = require('./package.json'); +var OktaAuth = '/' + packageJson.browser; + +module.exports = { "coverageDirectory": "./build2/reports/coverage", "restoreMocks": true, + "moduleNameMapper": { + "^OktaAuth(.*)$": OktaAuth + }, "testMatch": [ "**/test/spec/*.js" ], @@ -12,4 +17,4 @@ "default", "jest-junit" ] -} +}; diff --git a/jest.server.json b/jest.server.js similarity index 69% rename from jest.server.json rename to jest.server.js index 0f7479703..69280b794 100644 --- a/jest.server.json +++ b/jest.server.js @@ -1,6 +1,12 @@ -{ +var packageJson = require('./package.json'); +var OktaAuth = '/' + packageJson.main; + +module.exports = { "coverageDirectory": "./build2/reports/coverage", "restoreMocks": true, + "moduleNameMapper": { + "^OktaAuth(.*)$": OktaAuth + }, "testMatch": [ "**/test/spec/*.js" ], @@ -16,4 +22,4 @@ "default", "jest-junit" ] -} +}; diff --git a/package.json b/package.json index e3e3799a5..86cbceceb 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,8 @@ "lint": "eslint .", "lint:report": "eslint -f checkstyle -o build2/reports/lint/eslint-checkstyle-result.xml .", "test": "yarn test:browser && yarn test:server", - "test:browser": "jest --config ./jest.browser.json", - "test:server": "jest --config ./jest.server.json", + "test:browser": "jest --config ./jest.browser.js", + "test:server": "jest --config ./jest.server.js", "test:report": "yarn test --ci --silent || true", "build": "node ./writeConfig.js && webpack --config webpack.config.js", "prepublish": "yarn build" diff --git a/test/spec/errors.js b/test/spec/errors.js index 9046c4ec9..d6f902084 100644 --- a/test/spec/errors.js +++ b/test/spec/errors.js @@ -1,6 +1,6 @@ var _ = require('lodash'), util = require('../util/util'), - OktaAuth = require('../../lib/server/serverIndex'); + OktaAuth = require('OktaAuth'); describe('General Errors', function () { util.itErrorsCorrectly({ diff --git a/test/spec/fingerprint.js b/test/spec/fingerprint.js index 15ae3eb03..0b61bf610 100644 --- a/test/spec/fingerprint.js +++ b/test/spec/fingerprint.js @@ -1,4 +1,4 @@ -var OktaAuth = require('../../lib/server/serverIndex'); +var OktaAuth = require('OktaAuth'); var util = require('../util/util'); var packageJson = require('../../package.json'); diff --git a/test/spec/oauthUtil.js b/test/spec/oauthUtil.js index 8316a5e67..4a317a099 100644 --- a/test/spec/oauthUtil.js +++ b/test/spec/oauthUtil.js @@ -1,4 +1,4 @@ -var OktaAuth = require('../../lib/server/serverIndex'); +var OktaAuth = require('OktaAuth'); var oauthUtil = require('../../lib/oauthUtil'); var libUtil = require('../../lib/util'); var oauthUtilHelpers = require('../util/oauthUtil'); diff --git a/test/spec/token.js b/test/spec/token.js index 2c4b62017..ec89221ca 100644 --- a/test/spec/token.js +++ b/test/spec/token.js @@ -1,4 +1,4 @@ -var OktaAuth = require('../../lib/server/serverIndex'); +var OktaAuth = require('OktaAuth'); var tokens = require('../util/tokens'); var util = require('../util/util'); var oauthUtil = require('../util/oauthUtil'); diff --git a/test/spec/tokenManager.js b/test/spec/tokenManager.js index a6caad8c5..e9213a538 100644 --- a/test/spec/tokenManager.js +++ b/test/spec/tokenManager.js @@ -1,4 +1,4 @@ -var OktaAuth = require('../../lib/server/serverIndex'); +var OktaAuth = require('OktaAuth'); var tokens = require('../util/tokens'); var util = require('../util/util'); var oauthUtil = require('../util/oauthUtil'); diff --git a/test/util/oauthUtil.js b/test/util/oauthUtil.js index dc5f89869..97df8bc7a 100644 --- a/test/util/oauthUtil.js +++ b/test/util/oauthUtil.js @@ -1,6 +1,6 @@ /* eslint-disable complexity, max-statements */ var util = require('../util/util'); -var OktaAuth = require('../../lib/server/serverIndex'); +var OktaAuth = require('OktaAuth'); var tokens = require('./tokens'); var Q = require('q'); var EventEmitter = require('tiny-emitter'); diff --git a/test/util/util.js b/test/util/util.js index d7c23c702..b7b860e4f 100644 --- a/test/util/util.js +++ b/test/util/util.js @@ -2,7 +2,7 @@ /* eslint-disable max-statements, complexity */ var Q = require('q'), _ = require('lodash'), - OktaAuth = require('../../lib/server/serverIndex'), + OktaAuth = require('OktaAuth'), cookies = require('../../lib/browser/browserStorage').storage, fetch = require('cross-fetch'); From 2b4335dadbf2fe7ad484266623b7dcb5e5a9ee9e Mon Sep 17 00:00:00 2001 From: Manuel Tanzi Date: Mon, 1 Apr 2019 14:04:02 -0700 Subject: [PATCH 5/7] Fixing lint errors --- jest.browser.js | 22 +++++++++++----------- jest.server.js | 32 ++++++++++++++++---------------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/jest.browser.js b/jest.browser.js index b43a185d7..25c768466 100644 --- a/jest.browser.js +++ b/jest.browser.js @@ -2,19 +2,19 @@ var packageJson = require('./package.json'); var OktaAuth = '/' + packageJson.browser; module.exports = { - "coverageDirectory": "./build2/reports/coverage", - "restoreMocks": true, - "moduleNameMapper": { - "^OktaAuth(.*)$": OktaAuth + 'coverageDirectory': './build2/reports/coverage', + 'restoreMocks': true, + 'moduleNameMapper': { + '^OktaAuth(.*)$': OktaAuth }, - "testMatch": [ - "**/test/spec/*.js" + 'testMatch': [ + '**/test/spec/*.js' ], - "testPathIgnorePatterns": [ - "./test/spec/serverStorage.js" + 'testPathIgnorePatterns': [ + './test/spec/serverStorage.js' ], - "reporters": [ - "default", - "jest-junit" + 'reporters': [ + 'default', + 'jest-junit' ] }; diff --git a/jest.server.js b/jest.server.js index 69280b794..28a378e81 100644 --- a/jest.server.js +++ b/jest.server.js @@ -2,24 +2,24 @@ var packageJson = require('./package.json'); var OktaAuth = '/' + packageJson.main; module.exports = { - "coverageDirectory": "./build2/reports/coverage", - "restoreMocks": true, - "moduleNameMapper": { - "^OktaAuth(.*)$": OktaAuth + 'coverageDirectory': './build2/reports/coverage', + 'restoreMocks': true, + 'moduleNameMapper': { + '^OktaAuth(.*)$': OktaAuth }, - "testMatch": [ - "**/test/spec/*.js" + 'testMatch': [ + '**/test/spec/*.js' ], - "testPathIgnorePatterns": [ - "./test/spec/fingerprint.js", - "./test/spec/general.js", - "./test/spec/oauthUtil.js", - "./test/spec/token.js", - "./test/spec/tokenManager.js", - "./test/spec/webfinger.js" + 'testPathIgnorePatterns': [ + './test/spec/fingerprint.js', + './test/spec/general.js', + './test/spec/oauthUtil.js', + './test/spec/token.js', + './test/spec/tokenManager.js', + './test/spec/webfinger.js' ], - "reporters": [ - "default", - "jest-junit" + 'reporters': [ + 'default', + 'jest-junit' ] }; From 0a305170e13c093547865e5b4b6e877a2f1f78ba Mon Sep 17 00:00:00 2001 From: Manuel Tanzi Date: Thu, 4 Apr 2019 11:55:20 -0700 Subject: [PATCH 6/7] Addressing comments --- lib/token.js | 7 ++++++- lib/util.js | 4 ++++ test/spec/util.js | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/token.js b/lib/token.js index 179f716b8..a722e4c0e 100644 --- a/lib/token.js +++ b/lib/token.js @@ -594,7 +594,12 @@ function getUserInfo(sdk, accessTokenObject) { }) .fail(function(err) { if (err.xhr && (err.xhr.status === 401 || err.xhr.status === 403)) { - var authenticateHeader = err.xhr.headers.get('WWW-Authenticate'); + var authenticateHeader; + if (err.xhr.headers && util.isFunction(err.xhr.headers.get) && err.xhr.headers.get('WWW-Authenticate')) { + authenticateHeader = err.xhr.headers.get('WWW-Authenticate'); + } else if (util.isFunction(err.xhr.getResponseHeader)) { + authenticateHeader = err.xhr.getResponseHeader('WWW-Authenticate'); + } if (authenticateHeader) { var errorMatches = authenticateHeader.match(/error="(.*?)"/) || []; var errorDescriptionMatches = authenticateHeader.match(/error_description="(.*?)"/) || []; diff --git a/lib/util.js b/lib/util.js index d720525ff..2029bac8a 100644 --- a/lib/util.js +++ b/lib/util.js @@ -252,3 +252,7 @@ util.removeTrailingSlash = function(path) { util.isIE11OrLess = function() { return !!document.documentMode && document.documentMode <= 11; }; + +util.isFunction = function(fn) { + return !!fn && {}.toString.call(fn) === '[object Function]'; +}; diff --git a/test/spec/util.js b/test/spec/util.js index f0a7c1e98..b567e944b 100644 --- a/test/spec/util.js +++ b/test/spec/util.js @@ -122,4 +122,25 @@ describe('util', function() { }); }); + describe('isFunction', function() { + it('returns false if argument is undefined', function() { + var fn; + expect(util.isFunction(fn)).toBe(false); + }); + + it('returns false if argument is null', function() { + var fn = null; + expect(util.isFunction(fn)).toBe(false); + }); + + it('returns false if argument is not a function', function() { + var fn = "I am not a function!"; + expect(util.isFunction(fn)).toBe(false); + }); + + it('returns true if argument is a function', function() { + var fn = function() { return "I am a function!"; }; + expect(util.isFunction(fn)).toBe(true); + }); + }); }); From 798a526218ebe5f6486c57540ae90fb113117015 Mon Sep 17 00:00:00 2001 From: Manuel Tanzi Date: Fri, 5 Apr 2019 14:13:55 -0700 Subject: [PATCH 7/7] Addressing comments --- test/spec/util.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/test/spec/util.js b/test/spec/util.js index b567e944b..9a4600e42 100644 --- a/test/spec/util.js +++ b/test/spec/util.js @@ -133,13 +133,38 @@ describe('util', function() { expect(util.isFunction(fn)).toBe(false); }); - it('returns false if argument is not a function', function() { - var fn = "I am not a function!"; + it('returns false if argument is a boolean', function() { + var fn = true; + expect(util.isFunction(fn)).toBe(false); + }); + + it('returns false if argument is a number', function() { + var fn = 3; + expect(util.isFunction(fn)).toBe(false); + }); + + it('returns false if argument a string', function() { + var fn = 'I am not a function!'; + expect(util.isFunction(fn)).toBe(false); + }); + + it('returns false if argument is an Object', function() { + var fn = { name: 'Not a function!' }; + expect(util.isFunction(fn)).toBe(false); + }); + + it('returns false if argument is a Date', function() { + var fn = new Date('December 17, 1995 03:24:00'); + expect(util.isFunction(fn)).toBe(false); + }); + + it('returns false if argument is a RegExp', function() { + var fn = new RegExp('\\w+'); expect(util.isFunction(fn)).toBe(false); }); it('returns true if argument is a function', function() { - var fn = function() { return "I am a function!"; }; + var fn = function() { return 'I am a function!'; }; expect(util.isFunction(fn)).toBe(true); }); });