From commits-return-7781-archive-asf-public=cust-asf.ponee.io@openwhisk.apache.org Mon Jul 15 08:34:15 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 0B9A1180595 for ; Mon, 15 Jul 2019 10:34:14 +0200 (CEST) Received: (qmail 9739 invoked by uid 500); 15 Jul 2019 08:34:14 -0000 Mailing-List: contact commits-help@openwhisk.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@openwhisk.apache.org Delivered-To: mailing list commits@openwhisk.apache.org Received: (qmail 9730 invoked by uid 99); 15 Jul 2019 08:34:14 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Jul 2019 08:34:14 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 4880E85E18; Mon, 15 Jul 2019 08:34:14 +0000 (UTC) Date: Mon, 15 Jul 2019 08:34:14 +0000 To: "commits@openwhisk.apache.org" Subject: [incubator-openwhisk-client-js] branch master updated: Allow environment level set of default user-agent (#183) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Message-ID: <156317965423.20585.233646683661670323@gitbox.apache.org> From: jamesthomas@apache.org X-Git-Host: gitbox.apache.org X-Git-Repo: incubator-openwhisk-client-js X-Git-Refname: refs/heads/master X-Git-Reftype: branch X-Git-Oldrev: feb6a887e8c613106713fbf414228d139fc14b20 X-Git-Newrev: aa79d757d108f80372d061d549d8c9d365cb49b5 X-Git-Rev: aa79d757d108f80372d061d549d8c9d365cb49b5 X-Git-NotificationType: ref_changed_plus_diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated 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 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 = {}