Skip to content

Commit

Permalink
fix(core): properly parse query property (#367)
Browse files Browse the repository at this point in the history
closes #366
  • Loading branch information
wachunei authored and rodneyrehm committed Feb 10, 2018
1 parent fde82ec commit ec3d57b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/URI.js
Expand Up @@ -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');
}
Expand Down
43 changes: 43 additions & 0 deletions test/test.js
Expand Up @@ -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');
Expand Down

0 comments on commit ec3d57b

Please sign in to comment.