You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by gl...@apache.org on 2021/09/01 10:14:10 UTC
[couchdb-nano] 01/01: ensure requestDefaults.headers is used
correctly. Fixes issue #273
This is an automated email from the ASF dual-hosted git repository.
glynnbird pushed a commit to branch issue#273
in repository https://gitbox.apache.org/repos/asf/couchdb-nano.git
commit a5e55cdf4f508c42f1a06711c865cf222e73faf4
Author: Glynn Bird <gl...@apache.org>
AuthorDate: Wed Sep 1 11:13:59 2021 +0100
ensure requestDefaults.headers is used correctly. Fixes issue #273
---
README.md | 10 +++++-
lib/nano.js | 5 ++-
test/nano.customheaders.test.js | 69 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 82 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index 31e3fd5..60f74ac 100644
--- a/README.md
+++ b/README.md
@@ -213,7 +213,15 @@ You can also pass options to the require to specify further configuration option
// nano parses the URL and knows this is a database
const opts = {
url: 'http://localhost:5984/foo',
- requestDefaults: { proxy: { 'protocol': 'http', 'host': 'myproxy.net' } }
+ requestDefaults: {
+ proxy: {
+ protocol: 'http',
+ host: 'myproxy.net'
+ },
+ headers: {
+ customheader: 'MyCustomHeader'
+ }
+ }
};
const db = require('nano')(opts);
```
diff --git a/lib/nano.js b/lib/nano.js
index a9a81c4..1f58559 100644
--- a/lib/nano.js
+++ b/lib/nano.js
@@ -263,7 +263,10 @@ module.exports = exports = function dbScope (cfg) {
method: (opts.method || 'GET'),
headers: headers,
uri: cfg.url
- }, cfg.requestDefaults)
+ }, {
+ ...cfg.requestDefaults,
+ headers: Object.assign(headers, cfg.requestDefaults && cfg.requestDefaults.headers ? cfg.requestDefaults.headers : {})
+ })
// https://github.com/mikeal/request#requestjar
const isJar = opts.jar || cfg.jar || (cfg.requestDefaults && cfg.requestDefaults.jar)
diff --git a/test/nano.customheaders.test.js b/test/nano.customheaders.test.js
new file mode 100644
index 0000000..d6f0c72
--- /dev/null
+++ b/test/nano.customheaders.test.js
@@ -0,0 +1,69 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+const Nano = require('..')
+const COUCH_URL = 'http://localhost:5984'
+const CUSTOM_HEADER = 'thequickbrownfox'
+const nano = Nano({
+ url: COUCH_URL,
+ requestDefaults: {
+ headers: {
+ customheader: CUSTOM_HEADER
+ }
+ }
+})
+const nock = require('nock')
+const response = {
+ db_name: 'db',
+ purge_seq: '0-8KhNZEiqhyjKAgBm5Rxs',
+ update_seq: '23523-gUFPHo-6PQIAJ_EdrA',
+ sizes: {
+ file: 18215344,
+ external: 5099714,
+ active: 6727596
+ },
+ other: {
+ data_size: 5099714
+ },
+ doc_del_count: 23000,
+ doc_count: 0,
+ disk_size: 18215344,
+ disk_format_version: 7,
+ data_size: 6727596,
+ compact_running: false,
+ cluster: {
+ q: 2,
+ n: 1,
+ w: 1,
+ r: 1
+ },
+ instance_start_time: '0'
+}
+
+afterEach(() => {
+ nock.cleanAll()
+})
+
+test('should be able to fetch the database info - GET /db - nano.db.get', async () => {
+ // mocks
+ const scope = nock(COUCH_URL)
+ .matchHeader('customheader', CUSTOM_HEADER)
+ .get('/db')
+ .reply(200, response)
+
+ // test GET /db
+ const p = await nano.db.get('db')
+ expect(typeof p).toBe('object')
+ expect(p.doc_count).toBe(0)
+ expect(p.db_name).toBe('db')
+ expect(scope.isDone()).toBe(true)
+})