(fixes #236) - url reformating

This solves a bug when initializing nano with a url with databasename, which
also occurs somewhere else in the url, eg. in the username.
Also makes it possible to use nano with couches which are exposed from a
subdirectory, eg the server lives at http://example.com/couchdb.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-nano/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-nano/commit/76004b23
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-nano/tree/76004b23
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-nano/diff/76004b23

Branch: refs/heads/master
Commit: 76004b230bfeff5862bb491f6ea23399ed1fce90
Parents: 6bb82d9
Author: Johannes J. Schmidt <schm...@netzmerk.com>
Authored: Tue Nov 25 22:44:19 2014 +0100
Committer: Johannes J. Schmidt <schm...@netzmerk.com>
Committed: Tue Nov 25 22:44:19 2014 +0100

----------------------------------------------------------------------
 lib/nano.js                        |  5 +++--
 tests/integration/shared/config.js | 15 +++++++++++++++
 2 files changed, 18 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-nano/blob/76004b23/lib/nano.js
----------------------------------------------------------------------
diff --git a/lib/nano.js b/lib/nano.js
index 277c1d9..f780a5e 100644
--- a/lib/nano.js
+++ b/lib/nano.js
@@ -718,10 +718,11 @@ module.exports = exports = nano = function dbScope(cfg) {
 
   var path = u.parse(cfg.url);
   var pathArray = path.pathname.split('/').filter(function(e) { return e; });
-  var db = path.pathname && pathArray[0];
+  var db = pathArray.pop();
+  var rootPath = path.pathname.replace(/\/?$/, '/..');
 
   if (db) {
-    cfg.url = cfg.url.replace('/' + db, '');
+    cfg.url = u.resolve(cfg.url, rootPath).replace(/\/?$/, '');
     return docModule(db);
   }
 

http://git-wip-us.apache.org/repos/asf/couchdb-nano/blob/76004b23/tests/integration/shared/config.js
----------------------------------------------------------------------
diff --git a/tests/integration/shared/config.js 
b/tests/integration/shared/config.js
index 23089b6..43c1cff 100644
--- a/tests/integration/shared/config.js
+++ b/tests/integration/shared/config.js
@@ -50,6 +50,21 @@ it('should be able to parse urls', function(assert) {
     'with escaped auth');
 
   assert.equal(
+    Nano('http://a:b%20c...@someurl.com:5984/my%2Fdb').config.url,
+    'http://a:b%20c...@someurl.com:5984',
+    'with dbname containing encoded slash');
+
+  assert.equal(
+    Nano('http://mydb-a:b%20c...@someurl.com:5984/mydb').config.url,
+    'http://mydb-a:b%20c...@someurl.com:5984',
+    'with repeating dbname');
+
+  assert.equal(
+    Nano('http://a:b%20c...@someurl.com:5984/prefix/mydb').config.url,
+    'http://a:b%20c...@someurl.com:5984/prefix',
+    'with subdir');
+
+  assert.equal(
     Nano(baseUrl + ':5984/a').config.url,
     baseUrl + ':5984',
     'with port');

Reply via email to