Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
QUnit.test('URL constructor', assert => {
assert.isFunction(URL);
assert.arity(URL, 1);
assert.same(String(new URL('http://www.domain.com/a/b')), 'http://www.domain.com/a/b');
assert.same(String(new URL('/c/d', 'http://www.domain.com/a/b')), 'http://www.domain.com/c/d');
assert.same(String(new URL('b/c', 'http://www.domain.com/a/b')), 'http://www.domain.com/a/b/c');
assert.same(String(new URL('b/c', new URL('http://www.domain.com/a/b'))), 'http://www.domain.com/a/b/c');
assert.same(String(new URL({ toString: () => 'https://example.org/' })), 'https://example.org/');
assert.same(String(new URL('nonspecial://example.com/')), 'nonspecial://example.com/');
assert.same(String(new URL('https://測試')), 'https://xn--g6w251d/', 'unicode parsing');
assert.same(String(new URL('https://xxпривет.тест')), 'https://xn--xx-flcmn5bht.xn--e1aybc/', 'unicode parsing');
assert.same(String(new URL('https://xxПРИВЕТ.тест')), 'https://xn--xx-flcmn5bht.xn--e1aybc/', 'unicode parsing');
assert.same(String(new URL('http://Example.com/', 'https://example.org/')), 'http://example.com/');
assert.same(String(new URL('https://Example.com/', 'https://example.org/')), 'https://example.com/');
assert.same(String(new URL('nonspecial://Example.com/', 'https://example.org/')), 'nonspecial://Example.com/');
assert.same(String(new URL('http:Example.com/', 'https://example.org/')), 'http://example.com/');
assert.same(String(new URL('https:Example.com/', 'https://example.org/')), 'https://example.org/Example.com/');
assert.same(String(new URL('nonspecial:Example.com/', 'https://example.org/')), 'nonspecial:Example.com/');
assert.same(String(new URL('http://0300.168.0xF0')), 'http://192.168.0.240/');
assert.same(String(new URL('http://[20:0:0:1:0:0:0:ff]')), 'http://[20:0:0:1::ff]/');
// assert.same(String(new URL('http://257.168.0xF0')), 'http://257.168.0xf0/', 'incorrect IPv4 parsed as host'); // TypeError in Chrome and Safari
assert.same(String(new URL('http://0300.168.0xG0')), 'http://0300.168.0xg0/', 'incorrect IPv4 parsed as host');
QUnit.test('URL#password', assert => {
let url = new URL('http://zloirock.ru/');
if (DESCRIPTORS) {
assert.ok(!hasOwnProperty.call(url, 'password'));
const descriptor = Object.getOwnPropertyDescriptor(URL.prototype, 'password');
assert.same(descriptor.enumerable, true);
assert.same(descriptor.configurable, true);
assert.same(typeof descriptor.get, 'function');
assert.same(typeof descriptor.set, 'function');
}
assert.same(url.password, '');
url = new URL('http://username:password@zloirock.ru/');
assert.same(url.password, 'password');
// url = new URL('http://:password@zloirock.ru/'); // TypeError in FF
QUnit.test('URL constructor', assert => {
assert.isFunction(URL);
assert.arity(URL, 1);
assert.same(String(new URL('http://www.domain.com/a/b')), 'http://www.domain.com/a/b');
assert.same(String(new URL('/c/d', 'http://www.domain.com/a/b')), 'http://www.domain.com/c/d');
assert.same(String(new URL('b/c', 'http://www.domain.com/a/b')), 'http://www.domain.com/a/b/c');
assert.same(String(new URL('b/c', new URL('http://www.domain.com/a/b'))), 'http://www.domain.com/a/b/c');
assert.same(String(new URL({ toString: () => 'https://example.org/' })), 'https://example.org/');
assert.same(String(new URL('nonspecial://example.com/')), 'nonspecial://example.com/');
assert.same(String(new URL('https://測試')), 'https://xn--g6w251d/', 'unicode parsing');
assert.same(String(new URL('https://xxпривет.тест')), 'https://xn--xx-flcmn5bht.xn--e1aybc/', 'unicode parsing');
assert.same(String(new URL('https://xxПРИВЕТ.тест')), 'https://xn--xx-flcmn5bht.xn--e1aybc/', 'unicode parsing');
assert.same(String(new URL('http://Example.com/', 'https://example.org/')), 'http://example.com/');
assert.same(String(new URL('https://Example.com/', 'https://example.org/')), 'https://example.com/');
assert.same(String(new URL('nonspecial://Example.com/', 'https://example.org/')), 'nonspecial://Example.com/');
assert.same(String(new URL('http:Example.com/', 'https://example.org/')), 'http://example.com/');
assert.same(String(new URL('https:Example.com/', 'https://example.org/')), 'https://example.org/Example.com/');
assert.same(String(new URL('nonspecial:Example.com/', 'https://example.org/')), 'nonspecial:Example.com/');
assert.same(String(new URL('http://0300.168.0xF0')), 'http://192.168.0.240/');
assert.same(String(new URL('http://[20:0:0:1:0:0:0:ff]')), 'http://[20:0:0:1::ff]/');
// assert.same(String(new URL('http://257.168.0xF0')), 'http://257.168.0xf0/', 'incorrect IPv4 parsed as host'); // TypeError in Chrome and Safari
assert.same(entries, URLSearchParams.prototype.entries);
const expectedValues = { a: '1', b: '2', c: '3' };
let params = new URLSearchParams('a=1&b=2&c=3');
let iterator = params[Symbol.iterator]();
let result = '';
let entry;
while (!(entry = iterator.next()).done) {
const [key, value] = entry.value;
assert.same(params.get(key), expectedValues[key]);
assert.same(value, expectedValues[key]);
result += key;
}
assert.same(result, 'abc');
assert.ok(new URL('http://a.b/c').searchParams[Symbol.iterator]().next().done, 'should be finished');
// fails in Chrome 66-
if (DESCRIPTORS) {
const url = new URL('http://a.b/c?a=1&b=2&c=3&d=4');
iterator = url.searchParams[Symbol.iterator]();
result = '';
while (!(entry = iterator.next()).done) {
const [key, value] = entry.value;
url.search = 'x=1&y=2&z=3';
result += key + value;
}
assert.same(result, 'a1y2z3');
}
// fails in Chrome 66-
params = new URLSearchParams('a=1&b=2&c=3');
assert.same(descriptor.enumerable, true);
assert.same(descriptor.configurable, true);
assert.same(typeof descriptor.get, 'function');
assert.same(typeof descriptor.set, 'function');
}
assert.same(url.password, '');
url = new URL('http://username:password@zloirock.ru/');
assert.same(url.password, 'password');
// url = new URL('http://:password@zloirock.ru/'); // TypeError in FF
// assert.same(url.password, 'password');
if (DESCRIPTORS) {
url = new URL('http://zloirock.ru/');
url.username = 'username';
url.password = 'password';
assert.same(url.password, 'password');
assert.same(String(url), 'http://username:password@zloirock.ru/');
// url = new URL('http://zloirock.ru/');
// url.password = 'password';
// assert.same(url.password, 'password'); // '' in FF
// assert.same(String(url), 'http://:password@zloirock.ru/'); // 'http://zloirock.ru/' in FF
}
});
assert.same(params.get('first'), '1', 'search params object has name "first" with value "1"');
params.delete('first');
assert.same(params.has('first'), false, 'search params object has no "first" name');
params.append('first', 1);
params.append('first', 10);
params.delete('first');
assert.same(params.has('first'), false, 'search params object has no "first" name');
if (DESCRIPTORS) {
let url = new URL('http://example.com/?param1¶m2');
url.searchParams.delete('param1');
url.searchParams.delete('param2');
assert.same(String(url), 'http://example.com/', 'url.href does not have ?');
assert.same(url.search, '', 'url.search does not have ?');
url = new URL('http://example.com/?');
url.searchParams.delete('param1');
// assert.same(String(url), 'http://example.com/', 'url.href does not have ?'); // Safari bug
assert.same(url.search, '', 'url.search does not have ?');
}
assert.throws(() => {
return new URLSearchParams('').delete();
}, 'throws w/o arguments');
});
QUnit.test('URL constructor', assert => {
assert.isFunction(URL);
assert.arity(URL, 1);
assert.same(String(new URL('http://www.domain.com/a/b')), 'http://www.domain.com/a/b');
assert.same(String(new URL('/c/d', 'http://www.domain.com/a/b')), 'http://www.domain.com/c/d');
assert.same(String(new URL('b/c', 'http://www.domain.com/a/b')), 'http://www.domain.com/a/b/c');
assert.same(String(new URL('b/c', new URL('http://www.domain.com/a/b'))), 'http://www.domain.com/a/b/c');
assert.same(String(new URL({ toString: () => 'https://example.org/' })), 'https://example.org/');
assert.same(String(new URL('nonspecial://example.com/')), 'nonspecial://example.com/');
assert.same(String(new URL('https://測試')), 'https://xn--g6w251d/', 'unicode parsing');
assert.same(String(new URL('https://xxпривет.тест')), 'https://xn--xx-flcmn5bht.xn--e1aybc/', 'unicode parsing');
assert.same(String(new URL('https://xxПРИВЕТ.тест')), 'https://xn--xx-flcmn5bht.xn--e1aybc/', 'unicode parsing');
assert.same(String(new URL('http://Example.com/', 'https://example.org/')), 'http://example.com/');
assert.same(String(new URL('https://Example.com/', 'https://example.org/')), 'https://example.com/');
assert.same(String(new URL('nonspecial://Example.com/', 'https://example.org/')), 'nonspecial://Example.com/');
assert.same(String(new URL('http:Example.com/', 'https://example.org/')), 'http://example.com/');
assert.same(String(new URL('https:Example.com/', 'https://example.org/')), 'https://example.org/Example.com/');
assert.same(String(new URL('nonspecial:Example.com/', 'https://example.org/')), 'nonspecial:Example.com/');
QUnit.test('URL constructor', assert => {
assert.isFunction(URL);
assert.arity(URL, 1);
assert.same(String(new URL('http://www.domain.com/a/b')), 'http://www.domain.com/a/b');
assert.same(String(new URL('/c/d', 'http://www.domain.com/a/b')), 'http://www.domain.com/c/d');
assert.same(String(new URL('b/c', 'http://www.domain.com/a/b')), 'http://www.domain.com/a/b/c');
assert.same(String(new URL('b/c', new URL('http://www.domain.com/a/b'))), 'http://www.domain.com/a/b/c');
assert.same(String(new URL({ toString: () => 'https://example.org/' })), 'https://example.org/');
assert.same(String(new URL('nonspecial://example.com/')), 'nonspecial://example.com/');
assert.same(String(new URL('https://測試')), 'https://xn--g6w251d/', 'unicode parsing');
assert.same(String(new URL('https://xxпривет.тест')), 'https://xn--xx-flcmn5bht.xn--e1aybc/', 'unicode parsing');
assert.same(String(new URL('https://xxПРИВЕТ.тест')), 'https://xn--xx-flcmn5bht.xn--e1aybc/', 'unicode parsing');
assert.same(String(new URL('http://Example.com/', 'https://example.org/')), 'http://example.com/');
assert.same(String(new URL('https://Example.com/', 'https://example.org/')), 'https://example.com/');
assert.same(String(new URL('nonspecial://Example.com/', 'https://example.org/')), 'nonspecial://Example.com/');
assert.same(String(new URL('http:Example.com/', 'https://example.org/')), 'http://example.com/');
assert.same(String(new URL('https:Example.com/', 'https://example.org/')), 'https://example.org/Example.com/');
assert.same(String(new URL('nonspecial:Example.com/', 'https://example.org/')), 'nonspecial:Example.com/');
assert.same(String(new URL('http://0300.168.0xF0')), 'http://192.168.0.240/');
assert.same(String(new URL('http://[20:0:0:1:0:0:0:ff]')), 'http://[20:0:0:1::ff]/');
assert.same(value, reqValue);
});
i = 0;
const url = new URL(`?${ input }`, 'https://example/');
params = url.searchParams;
params.sort();
params.forEach((value, key) => {
const [reqKey, reqValue] = output[i++];
assert.same(key, reqKey);
assert.same(value, reqValue);
});
}
if (DESCRIPTORS) {
const url = new URL('http://example.com/?');
url.searchParams.sort();
assert.same(url.href, 'http://example.com/', 'Sorting non-existent params removes ? from URL');
assert.same(url.search, '', 'Sorting non-existent params removes ? from URL');
}
});
assert.throws(() => new URL('1http://zloirock.ru'), 'incorrect scheme');
});