This is an automated email from the ASF dual-hosted git repository. jamesthomas pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-client-js.git
The following commit(s) were added to refs/heads/master by this push: new aa79d75 Allow environment level set of default user-agent (#183) aa79d75 is described below commit aa79d757d108f80372d061d549d8c9d365cb49b5 Author: Jesse MacFadyen <purplecabb...@gmail.com> AuthorDate: Mon Jul 15 01:34:09 2019 -0700 Allow environment level set of default user-agent (#183) * Allow environment level set of default user-agent Currently it is very difficult to set the user agent globally. We have a library which is using this library, and changing the user-agent to something we can track requires modifying the options to every client call. * rename __OW_USER_AGENT to follow existing configuration pattern, and include in docs * added tests --- README.md | 1 + lib/client.js | 2 +- test/unit/actions.test.js | 18 ++++++++++++ test/unit/namespaces.test.js | 70 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 88ba2b2..010e509 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ Client constructor will read values for the `apihost`, `namespace`, `api_key`, ` - *__OW_IGNORE_CERTS* - *__OW_APIGW_TOKEN* - *__OW_APIGW_SPACE_SUID* +- *__OW_USER_AGENT* ### User-Agent diff --git a/lib/client.js b/lib/client.js index 94ab447..debde18 100644 --- a/lib/client.js +++ b/lib/client.js @@ -141,7 +141,7 @@ class Client { url: this.pathUrl(path), rejectUnauthorized: !this.options.ignoreCerts, headers: { - 'User-Agent': (options && options['User-Agent']) || 'openwhisk-client-js', + 'User-Agent': (options && options['User-Agent']) || process.env['__OW_USER_AGENT'] || 'openwhisk-client-js', Authorization: header } }, options) diff --git a/test/unit/actions.test.js b/test/unit/actions.test.js index 63c7f72..8dbed3f 100644 --- a/test/unit/actions.test.js +++ b/test/unit/actions.test.js @@ -521,6 +521,24 @@ test('should pass through requested User-Agent header', t => { return actions.create({name: '12345', action, version, 'User-Agent': userAgent}) }) +test('should pass through requested User-Agent header even when __OW_USER_AGENT is set', t => { + t.plan(1) + process.env['__OW_USER_AGENT'] = 'my-useragent' + + const userAgent = 'userAgentShouldPassThroughPlease' + const client = {} + const actions = new Actions(client) + const action = 'function main() { // main function body};' + const version = '1.0.0' + + client.request = (method, path, options) => { + t.is(options['User-Agent'], userAgent) + delete process.env['__OW_USER_AGENT'] + } + + return actions.create({name: '12345', action, version, 'User-Agent': userAgent}) +}) + test('should pass through exec.image parameter', t => { t.plan(1) const image = 'openwhisk/action-nodejs-v8:latest' diff --git a/test/unit/namespaces.test.js b/test/unit/namespaces.test.js index fab8a6a..4e5f2ad 100644 --- a/test/unit/namespaces.test.js +++ b/test/unit/namespaces.test.js @@ -93,6 +93,76 @@ test('should list all namespaces, NOT passing through user-agent header (variant return namespaces.list({noUserAgent: true}) }) +test('should list all namespaces, using __OW_USER_AGENT', t => { + t.plan(3) + const client = {} + process.env['__OW_USER_AGENT'] = 'my-useragent' + client.request = async (method, path, options) => { + t.is(method, 'GET') + t.is(path, `namespaces`) + + const parms = await new Client({api: 'aaa', api_key: 'aaa'}).params(method, path, options) + t.is(parms.headers['User-Agent'], 'my-useragent') + delete process.env['__OW_USER_AGENT'] + } + + const namespaces = new Namespaces(client) + return namespaces.list({}) +}) + +test('should list all namespaces, NOT using __OW_USER_AGENT when noUserAgent true', t => { + t.plan(3) + const client = {} + process.env['__OW_USER_AGENT'] = 'my-useragent' + client.request = async (method, path, options) => { + t.is(method, 'GET') + t.is(path, `namespaces`) + + const parms = await new Client({api: 'aaa', api_key: 'aaa', noUserAgent: true}).params(method, path, options) + t.is(parms.headers['User-Agent'], undefined) + delete process.env['__OW_USER_AGENT'] + } + + const namespaces = new Namespaces(client) + return namespaces.list({}) +}) + +test('should list all namespaces, NOT using __OW_USER_AGENT when user-agent is passed through', t => { + t.plan(3) + const client = {} + const userAgent = 'userAgentShouldPassThroughPlease' + process.env['__OW_USER_AGENT'] = 'my-useragent' + client.request = async (method, path, options) => { + t.is(method, 'GET') + t.is(path, `namespaces`) + + const parms = await new Client({api: 'aaa', api_key: 'aaa'}).params(method, path, options) + t.is(parms.headers['User-Agent'], userAgent) + delete process.env['__OW_USER_AGENT'] + } + + const namespaces = new Namespaces(client) + return namespaces.list({'User-Agent': userAgent}) +}) + +test('should list all namespaces, NOT using __OW_USER_AGENT or user-agent when noUserAgent is true', t => { + t.plan(3) + const client = {} + const userAgent = 'userAgentShouldPassThroughPlease' + process.env['__OW_USER_AGENT'] = 'my-useragent' + client.request = async (method, path, options) => { + t.is(method, 'GET') + t.is(path, `namespaces`) + + const parms = await new Client({api: 'aaa', api_key: 'aaa', noUserAgent: true}).params(method, path, options) + t.is(parms.headers['User-Agent'], undefined) + delete process.env['__OW_USER_AGENT'] + } + + const namespaces = new Namespaces(client) + return namespaces.list({'User-Agent': userAgent}) +}) + test('should retrieve namespace entities', t => { t.plan(16) const client = {}