Skip to content

Commit 26303fa

Browse files
glynnbirdGlynn Bird
and
Glynn Bird
authored
ensure requestDefaults.headers is used correctly. Fixes issue #273 (#277)
Co-authored-by: Glynn Bird <glynnbird@apache.org>
1 parent 93a9eff commit 26303fa

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,15 @@ You can also pass options to the require to specify further configuration option
213213
// nano parses the URL and knows this is a database
214214
const opts = {
215215
url: 'http://localhost:5984/foo',
216-
requestDefaults: { proxy: { 'protocol': 'http', 'host': 'myproxy.net' } }
216+
requestDefaults: {
217+
proxy: {
218+
protocol: 'http',
219+
host: 'myproxy.net'
220+
},
221+
headers: {
222+
customheader: 'MyCustomHeader'
223+
}
224+
}
217225
};
218226
const db = require('nano')(opts);
219227
```

lib/nano.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,10 @@ module.exports = exports = function dbScope (cfg) {
263263
method: (opts.method || 'GET'),
264264
headers: headers,
265265
uri: cfg.url
266-
}, cfg.requestDefaults)
266+
}, {
267+
...cfg.requestDefaults,
268+
headers: Object.assign(headers, cfg.requestDefaults && cfg.requestDefaults.headers ? cfg.requestDefaults.headers : {})
269+
})
267270

268271
// https://github.com/mikeal/request#requestjar
269272
const isJar = opts.jar || cfg.jar || (cfg.requestDefaults && cfg.requestDefaults.jar)

test/nano.customheaders.test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
2+
// use this file except in compliance with the License. You may obtain a copy of
3+
// the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
9+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
// License for the specific language governing permissions and limitations under
11+
// the License.
12+
13+
const Nano = require('..')
14+
const COUCH_URL = 'http://localhost:5984'
15+
const CUSTOM_HEADER = 'thequickbrownfox'
16+
const nano = Nano({
17+
url: COUCH_URL,
18+
requestDefaults: {
19+
headers: {
20+
customheader: CUSTOM_HEADER
21+
}
22+
}
23+
})
24+
const nock = require('nock')
25+
const response = {
26+
db_name: 'db',
27+
purge_seq: '0-8KhNZEiqhyjKAgBm5Rxs',
28+
update_seq: '23523-gUFPHo-6PQIAJ_EdrA',
29+
sizes: {
30+
file: 18215344,
31+
external: 5099714,
32+
active: 6727596
33+
},
34+
other: {
35+
data_size: 5099714
36+
},
37+
doc_del_count: 23000,
38+
doc_count: 0,
39+
disk_size: 18215344,
40+
disk_format_version: 7,
41+
data_size: 6727596,
42+
compact_running: false,
43+
cluster: {
44+
q: 2,
45+
n: 1,
46+
w: 1,
47+
r: 1
48+
},
49+
instance_start_time: '0'
50+
}
51+
52+
afterEach(() => {
53+
nock.cleanAll()
54+
})
55+
56+
test('should be able to fetch the database info - GET /db - nano.db.get', async () => {
57+
// mocks
58+
const scope = nock(COUCH_URL)
59+
.matchHeader('customheader', CUSTOM_HEADER)
60+
.get('/db')
61+
.reply(200, response)
62+
63+
// test GET /db
64+
const p = await nano.db.get('db')
65+
expect(typeof p).toBe('object')
66+
expect(p.doc_count).toBe(0)
67+
expect(p.db_name).toBe('db')
68+
expect(scope.isDone()).toBe(true)
69+
})

0 commit comments

Comments
 (0)