Skip to content

Commit

Permalink
Added unpack Kopps config
Browse files Browse the repository at this point in the history
  • Loading branch information
jhsware committed Jan 30, 2017
1 parent 6c58910 commit 9c67bbe
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
generateConfig: require('./generateConfig'),
decodeUri: require('./decodeUri'),
unpackKOPPSConfig: require('./unpackKOPPSConfig'),
unpackLDAPConfig: require('./unpackLDAPConfig'),
unpackMongodbConfig: require('./unpackMongodbConfig'),
unpackNodeApiConfig: require('./unpackNodeApiConfig'),
Expand Down
31 changes: 31 additions & 0 deletions lib/unpackKOPPSConfig.js
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
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kth-node-configuration",
"version": "1.3.1",
"version": "1.3.2",
"description": "Configuration module for Node.js projects",
"main": "lib/index.js",
"repository": {
Expand Down
57 changes: 57 additions & 0 deletions test/test-unpackKOPPSConfig.js
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)
})
})

0 comments on commit 9c67bbe

Please sign in to comment.