Skip to content

Commit ec3d57b

Browse files
wachuneirodneyrehm
authored andcommitted
fix(core): properly parse query property (#367)
closes #366
1 parent fde82ec commit ec3d57b

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/URI.js

+4
Original file line numberDiff line numberDiff line change
@@ -1241,10 +1241,14 @@
12411241
} else if (_URI || _object) {
12421242
var src = _URI ? href._parts : href;
12431243
for (key in src) {
1244+
if (key === 'query') { continue; }
12441245
if (hasOwn.call(this._parts, key)) {
12451246
this._parts[key] = src[key];
12461247
}
12471248
}
1249+
if (src.query) {
1250+
this.query(src.query, false);
1251+
}
12481252
} else {
12491253
throw new TypeError('invalid input');
12501254
}

test/test.js

+43
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,49 @@
3434
ok(u instanceof URI, 'instanceof URI');
3535
ok(u._parts.hostname !== undefined, 'host undefined');
3636
});
37+
38+
test('new URI(object)', function() {
39+
var u = new URI({
40+
protocol: 'http',
41+
hostname: 'example.org',
42+
query: {
43+
foo: 'bar',
44+
bar: 'foo',
45+
},
46+
});
47+
ok(u instanceof URI, 'instanceof URI');
48+
ok(typeof u.query() === 'string', 'query is string');
49+
equal(u.query(), 'foo=bar&bar=foo', 'query has right value');
50+
equal(u.search(), '?foo=bar&bar=foo', 'search has right value');
51+
deepEqual(u.query(true), { foo: 'bar', bar: 'foo' }, 'query(true) value');
52+
deepEqual(u.search(true), { foo: 'bar', bar: 'foo' }, 'search(true) value');
53+
});
54+
test('new URI(object)', function() {
55+
var u = new URI({
56+
protocol: 'http',
57+
hostname: 'example.org',
58+
query: 'foo=bar&bar=foo',
59+
});
60+
ok(u instanceof URI, 'instanceof URI');
61+
ok(typeof u.query() === 'string', 'query is string');
62+
equal(u.query(), 'foo=bar&bar=foo', 'query has right value');
63+
equal(u.search(), '?foo=bar&bar=foo', 'search has right value');
64+
deepEqual(u.query(true), { foo: 'bar', bar: 'foo' }, 'query(true) value');
65+
deepEqual(u.search(true), { foo: 'bar', bar: 'foo' }, 'search(true) value');
66+
});
67+
test('new URI(object)', function() {
68+
var u = new URI({
69+
protocol: 'http',
70+
hostname: 'example.org',
71+
query: '?foo=bar&bar=foo',
72+
});
73+
ok(u instanceof URI, 'instanceof URI');
74+
ok(typeof u.query() === 'string', 'query is string');
75+
equal(u.query(), 'foo=bar&bar=foo', 'query has right value');
76+
equal(u.search(), '?foo=bar&bar=foo', 'search has right value');
77+
deepEqual(u.query(true), { foo: 'bar', bar: 'foo' }, 'query(true) value');
78+
deepEqual(u.search(true), { foo: 'bar', bar: 'foo' }, 'search(true) value');
79+
});
3780
test('new URI(Location)', function () {
3881
var u = new URI(location);
3982
equal(u.href(), String(location.href), 'location object');

0 commit comments

Comments
 (0)