diff --git a/src/URI.js b/src/URI.js index 1d7957a0..d2cf26d6 100644 --- a/src/URI.js +++ b/src/URI.js @@ -1241,10 +1241,14 @@ } else if (_URI || _object) { var src = _URI ? href._parts : href; for (key in src) { + if (key === 'query') { continue; } if (hasOwn.call(this._parts, key)) { this._parts[key] = src[key]; } } + if (src.query) { + this.query(src.query, false); + } } else { throw new TypeError('invalid input'); } diff --git a/test/test.js b/test/test.js index fe62c01c..c29c2ad1 100644 --- a/test/test.js +++ b/test/test.js @@ -34,6 +34,49 @@ ok(u instanceof URI, 'instanceof URI'); ok(u._parts.hostname !== undefined, 'host undefined'); }); + + test('new URI(object)', function() { + var u = new URI({ + protocol: 'http', + hostname: 'example.org', + query: { + foo: 'bar', + bar: 'foo', + }, + }); + ok(u instanceof URI, 'instanceof URI'); + ok(typeof u.query() === 'string', 'query is string'); + equal(u.query(), 'foo=bar&bar=foo', 'query has right value'); + equal(u.search(), '?foo=bar&bar=foo', 'search has right value'); + deepEqual(u.query(true), { foo: 'bar', bar: 'foo' }, 'query(true) value'); + deepEqual(u.search(true), { foo: 'bar', bar: 'foo' }, 'search(true) value'); + }); + test('new URI(object)', function() { + var u = new URI({ + protocol: 'http', + hostname: 'example.org', + query: 'foo=bar&bar=foo', + }); + ok(u instanceof URI, 'instanceof URI'); + ok(typeof u.query() === 'string', 'query is string'); + equal(u.query(), 'foo=bar&bar=foo', 'query has right value'); + equal(u.search(), '?foo=bar&bar=foo', 'search has right value'); + deepEqual(u.query(true), { foo: 'bar', bar: 'foo' }, 'query(true) value'); + deepEqual(u.search(true), { foo: 'bar', bar: 'foo' }, 'search(true) value'); + }); + test('new URI(object)', function() { + var u = new URI({ + protocol: 'http', + hostname: 'example.org', + query: '?foo=bar&bar=foo', + }); + ok(u instanceof URI, 'instanceof URI'); + ok(typeof u.query() === 'string', 'query is string'); + equal(u.query(), 'foo=bar&bar=foo', 'query has right value'); + equal(u.search(), '?foo=bar&bar=foo', 'search has right value'); + deepEqual(u.query(true), { foo: 'bar', bar: 'foo' }, 'query(true) value'); + deepEqual(u.search(true), { foo: 'bar', bar: 'foo' }, 'search(true) value'); + }); test('new URI(Location)', function () { var u = new URI(location); equal(u.href(), String(location.href), 'location object');