-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict' | ||
|
||
const { getEnv, typeConversion } = require('./utils') | ||
const urlgrey = require('urlgrey') | ||
|
||
module.exports = function (envVarName, defaultUri, options) { | ||
const envObj = urlgrey(getEnv(envVarName, defaultUri)) | ||
|
||
if (!/^http[s]*/.test(envObj.protocol())) { | ||
throw new Error('Node API URI protocol must be http or https, got: ' + envObj.protocol()) | ||
} | ||
|
||
const outp = { | ||
https: envObj.protocol() === 'https', | ||
// Netscaler doesn't answer https calls properly if we pass the port to kth-node-api-call | ||
host: envObj.hostname(), | ||
basePath: envObj.path() | ||
} | ||
|
||
if (typeof options === 'object') { | ||
Object.assign(outp, options) | ||
} | ||
|
||
if (envObj.queryString) { | ||
var tmpQuery = envObj.query() | ||
Object.keys(tmpQuery).forEach((key) => { | ||
outp[key] = typeConversion(tmpQuery[key]) | ||
}) | ||
} | ||
return outp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
const expect = require('chai').expect | ||
const unpackKOPPSConfig = require('../lib/unpackKOPPSConfig') | ||
|
||
const testURI = 'http://kopps-r.referens.sys.kth.se/api/kopps/v2/' | ||
const testURIWithSSL = 'https://kopps-r.referens.sys.kth.se/api/kopps/v2/' | ||
const failProtocol = 'mailto://' | ||
|
||
describe('unpackKOPPSConfig', function () { | ||
it('can decode a KOPPS URI from fallback URI', function () { | ||
const obj = unpackKOPPSConfig('no-env-exists', testURI) | ||
expect(obj.https).to.equal(false) | ||
expect(obj.host).to.equal('kopps-r.referens.sys.kth.se') | ||
expect(obj.port).to.equal(undefined) | ||
expect(obj.basePath).to.equal('/api/kopps/v2/') | ||
}) | ||
|
||
it('can decode a KOPPS URI from env var', function () { | ||
process.env['TEST_ENV_NOW_HERE'] = testURI | ||
const obj = unpackKOPPSConfig('TEST_ENV_NOW_HERE') | ||
expect(obj.https).to.equal(false) | ||
expect(obj.host).to.equal('kopps-r.referens.sys.kth.se') | ||
expect(obj.port).to.equal(undefined) | ||
expect(obj.basePath).to.equal('/api/kopps/v2/') | ||
}) | ||
|
||
it('can decode a KOPPS URI from fallback URI and merge with options', function () { | ||
const obj = unpackKOPPSConfig('no-env-exists', testURI, { extraOption: true }) | ||
expect(obj.https).to.equal(false) | ||
expect(obj.host).to.equal('kopps-r.referens.sys.kth.se') | ||
expect(obj.port).to.equal(undefined) | ||
expect(obj.basePath).to.equal('/api/kopps/v2/') | ||
expect(obj.extraOption).to.equal(true) | ||
}) | ||
|
||
it('should not expose protocol property', function () { | ||
const obj = unpackKOPPSConfig('no-env-exists', testURI) | ||
expect(obj.protocol).to.equal(undefined) | ||
}) | ||
|
||
it('should not expose port if https', function () { | ||
const obj = unpackKOPPSConfig('no-env-exists', testURIWithSSL) | ||
expect(obj.port).to.equal(undefined) | ||
expect(obj.https).to.equal(true) | ||
}) | ||
|
||
it('should not accept wrong protocol', function () { | ||
var theErr | ||
try { | ||
unpackKOPPSConfig('no-env-exists', failProtocol) | ||
} catch (err) { | ||
theErr = err | ||
} | ||
expect(theErr).not.to.equal(undefined) | ||
}) | ||
}) |