From 8d0c31c0898bce79df43f24d6cd6c88f2d3cd053 Mon Sep 17 00:00:00 2001 From: Daniel Cassidy Date: Tue, 23 Apr 2013 15:51:07 +0100 Subject: [PATCH 1/2] Add tests demonstrating incorrect empty relative path serialization by uri.path() and uri.pathname(). Clearly the relative path from"/a/b/c/" to "/a/b/c/" is the empty relative path "", and not "/". Equivalently, URI("").path() should return the empty string "", but in fact returns "/". The empty relative path is serialized correctly by .toString(), but incorrectly by .path() and .pathname(). --- test/test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test.js b/test/test.js index 0c4a1f47..f3465861 100644 --- a/test/test.js +++ b/test/test.js @@ -155,6 +155,14 @@ test("path", function() { u.pathname('/~userhome/@mine;is %2F and/'); equal(u.pathname(), '/~userhome/@mine;is%20%2F%20and/', "path encoding"); equal(u.pathname(true), '/~userhome/@mine;is %2F and/', "path decoded"); + + u = new URI('/a/b/c/').relativeTo('/a/b/c/'); + equal(u.pathname(), '', "empty relative path"); + equal(u.toString(), '', "empty relative path to string"); + + u.pathname('/'); + equal(u.pathname(), '/', "empty absolute path"); + equal(u.toString(), '/', "empty absolute path to string"); }); test("query", function() { var u = new URI("http://example.org/foo.html"); From 8049654a319d66f534050de0966f31a6292801de Mon Sep 17 00:00:00 2001 From: Daniel Cassidy Date: Tue, 23 Apr 2013 16:08:26 +0100 Subject: [PATCH 2/2] Fix incorrect serialization of empty relative paths. If the URI includes an authority component, then the path is always absolute. Otherwise, the path may be relative and we should not coerce an empty relative path to the empty absolute path. We check for the presence of a hostname as a proxy for checking for an authority component, because in our implementation the presence of a hostname determines the presence of an authority component. There was previously a check to see if the URI was a URN, but that is now redundant since a URN cannot include an authority component. --- src/URI.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/URI.js b/src/URI.js index 65985000..b98b8c0e 100644 --- a/src/URI.js +++ b/src/URI.js @@ -800,7 +800,7 @@ for (_part in _parts) { p.pathname = function(v, build) { if (v === undefined || v === true) { - var res = this._parts.path || (this._parts.urn ? '' : '/'); + var res = this._parts.path || (this._parts.hostname ? '/' : ''); return v ? URI.decodePath(res) : res; } else { this._parts.path = v ? URI.recodePath(v) : "/";