add User-Agent to request header (#118)
Fixes #117
diff --git a/README.md b/README.md
index cdaa050..74df04e 100644
--- a/README.md
+++ b/README.md
@@ -90,7 +90,20 @@
- *__OW_APIGW_TOKEN*
- *__OW_APIGW_SPACE_SUID*
+### User-Agent
+A User-Agent header may be specified to be passed along with all calls
+to OpenWhisk. This can be helpful, if you wish to discriminate client
+traffic to your OpenWhisk backend. By default, the header will have
+the value `openwhisk-client-js`. You may override this by passing
+along a `'User-Agent'` field in the options structure of any API
+calls; note that this is *not* a constructor argument, but rather an
+option to the API calls themselves. For example, one might specify a
+`myClient` user agent to an action invocation as follows:
+
+```javascript
+ow.actions.invoke({ 'User-Agent': 'myClient', name, params })
+```
## Examples
diff --git a/lib/client.js b/lib/client.js
index 900173c..48723e3 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -124,6 +124,7 @@
url: this.pathUrl(path),
rejectUnauthorized: !this.options.ignoreCerts,
headers: {
+ 'User-Agent': (options && options['User-Agent']) || 'openwhisk-client-js',
Authorization: this.authHeader()
}
}, options)
diff --git a/test/unit/actions.test.js b/test/unit/actions.test.js
index 8d3140f..c3a6455 100644
--- a/test/unit/actions.test.js
+++ b/test/unit/actions.test.js
@@ -429,3 +429,18 @@
return actions.create({name: '12345', action, version})
})
+
+test('should pass through requested User-Agent header', t => {
+ t.plan(1)
+ 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)
+ }
+
+ return actions.create({name: '12345', action, version, 'User-Agent': userAgent})
+})