You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by ra...@apache.org on 2020/05/02 19:51:46 UTC

[openwhisk-client-js] branch master updated: Allow custom headers (#209)

This is an automated email from the ASF dual-hosted git repository.

rabbah pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwhisk-client-js.git


The following commit(s) were added to refs/heads/master by this push:
     new 6953b25  Allow custom headers (#209)
6953b25 is described below

commit 6953b253a764dbf89a4462f5157928ebfe17841a
Author: Jesse MacFadyen <pu...@gmail.com>
AuthorDate: Sat May 2 12:51:39 2020 -0700

    Allow custom headers (#209)
    
    * headers were being overwritten, use Object.assign on the inner headers object
    * add test for OPTIONS.headers
---
 lib/client.js            | 11 ++++++-----
 test/unit/client.test.js | 16 ++++++++++++++++
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/lib/client.js b/lib/client.js
index f86c4c7..504c7d5 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -161,13 +161,14 @@ class Client {
         json: true,
         method: method,
         url: this.pathUrl(path),
-        rejectUnauthorized: !this.options.ignoreCerts,
-        headers: {
-          'User-Agent': (options && options['User-Agent']) || process.env['__OW_USER_AGENT'] || 'openwhisk-client-js',
-          Authorization: header
-        }
+        rejectUnauthorized: !this.options.ignoreCerts
       }, options)
 
+      parms.headers = Object.assign({
+        'User-Agent': (options && options['User-Agent']) || process.env['__OW_USER_AGENT'] || 'openwhisk-client-js',
+        Authorization: header
+      }, parms.headers)
+
       if (this.options.cert && this.options.key) {
         parms.cert = this.options.cert
         parms.key = this.options.key
diff --git a/test/unit/client.test.js b/test/unit/client.test.js
index 3e51eb3..99b5ca1 100644
--- a/test/unit/client.test.js
+++ b/test/unit/client.test.js
@@ -77,6 +77,22 @@ test('apihost and apiversion set', t => {
   t.is(client.options.apiVersion, 'v2')
 })
 
+test('option headers are respected', async t => {
+  const client = new Client({ api_key: 'username:password', apihost: 'blah' })
+  const METHOD = 'get'
+  const PATH = 'some/path/to/resource'
+  const OPTIONS = { headers: {
+    'x-custom-header': 'some-value',
+    'User-Agent': 'some-custom-useragent-string'
+  } }
+  const params = await client.params(METHOD, PATH, OPTIONS)
+  t.true(params.headers.hasOwnProperty('Authorization'))
+  t.true(params.headers.hasOwnProperty('x-custom-header'))
+  // options.headers overwrite defaults if they match
+  t.true(params.headers.hasOwnProperty('User-Agent'))
+  t.true(params.headers['User-Agent'] === OPTIONS.headers['User-Agent'])
+})
+
 test('should support deprecated explicit constructor options', t => {
   const client = new Client({
     namespace: 'ns',