Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure document names with _local in them get properly url encoded #342

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/nano.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ module.exports = exports = function dbScope (cfg) {
if (opts.path) {
req.uri += '/' + opts.path
} else if (opts.doc) {
if (!/^_design|_local/.test(opts.doc)) {
if (!/^(_design|_local)/.test(opts.doc)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test for a slash immediately following _design or _local as well?

Suggested change
if (!/^(_design|_local)/.test(opts.doc)) {
if (!/^(_design|_local)\//.test(opts.doc)) {

// https://wiki.apache.org/couchdb/HTTP_Document_API#Naming.2FAddressing
req.uri += '/' + encodeURIComponent(opts.doc)
} else {
Expand Down
17 changes: 17 additions & 0 deletions test/document.insert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ test('should be able to insert document with known id - PUT /db/id - db.insert',
expect(scope.isDone()).toBe(true)
})

test('should be able to insert document with known id with _local - PUT /db/id - db.insert', async () => {
// mocks
const id = 'myid/_local'
const doc = { a: 1, b: 2 }
const response = { ok: true, id, rev: '1-123' }

const scope = nock(COUCH_URL)
.put(`/db/${encodeURIComponent(id)}`, doc)
.reply(200, response)

// test PUT /db
const db = nano.db.use('db')
const p = await db.insert(doc, id)
expect(p).toStrictEqual(response)
expect(scope.isDone()).toBe(true)
})

test('should be able to insert document with id in object - POST /db - db.insert', async () => {
// mocks
const doc = { _id: 'myid', a: 1, b: 2 }
Expand Down