add header x-namespace-id (#140)
* add header x-namespace-id
Closes #139
* add tests for x-namespace-id header injection
diff --git a/lib/client.js b/lib/client.js
index 180a5b9..488e363 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -136,6 +136,10 @@
// caller asked for no user agent?
parms.headers['User-Agent'] = undefined
}
+ if (typeof this.options.namespace === 'string') {
+ // identify namespace targeting a public/shared entity
+ parms.headers['x-namespace-id'] = this.options.namespace
+ }
return parms
})
diff --git a/test/unit/client.test.js b/test/unit/client.test.js
index 6d8fad3..c79cfb5 100644
--- a/test/unit/client.test.js
+++ b/test/unit/client.test.js
@@ -205,3 +205,30 @@
const client = new Client({api_key: true, api: true})
t.throws(() => client.handleErrors({message: 'error message'}), /error message/)
})
+
+test('should contain x-namespace-id header when namespace in contructor options', async t => {
+ const authHandler = {
+ getAuthHeader: () => {
+ return Promise.resolve('Bearer access_token')
+ }
+ }
+ const client = new Client({apihost: 'my_host', namespace: 'ns', auth_handler: authHandler})
+ const METHOD = 'POST'
+ const PATH = '/publicnamespace/path/to/resource'
+ let params = await client.params(METHOD, PATH, {})
+ t.is(client.options.namespace, 'ns')
+ t.is(params.headers['x-namespace-id'], client.options.namespace)
+})
+
+test('should not contain x-namespace-id header when namespace is not in contructor options', async t => {
+ const authHandler = {
+ getAuthHeader: () => {
+ return Promise.resolve('Bearer access_token')
+ }
+ }
+ const client = new Client({apihost: 'my_host', auth_handler: authHandler})
+ const METHOD = 'POST'
+ const PATH = '/publicnamespace/path/to/resource'
+ let params = await client.params(METHOD, PATH, {})
+ t.is(params.headers['x-namespace-id'], undefined)
+})