chromium/third_party/blink/web_tests/fast/domurl/url-hostname.html

<!DOCTYPE HTML>
<link rel="help" href="http://url.spec.whatwg.org/#dom-url-hostname">
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>

test(function() {
    var url = new URL('https://www.mydomain.com/path/');
    assert_equals(url.hostname, 'www.mydomain.com');
    url.hostname = 'www.otherdomain.com';
    assert_equals(url.href, 'https://www.otherdomain.com/path/');
}, 'Basic test');

test(function() {
    var url = new URL('https://www.mydomain.com/path/?key=value');
    url.hostname = 'www.other?domain.com';
    assert_equals(url.href, 'https://www.other/path/?key=value');
}, 'Set hostname with ? in it');

test(function() {
    var url = new URL('https://www.mydomain.com/path/');
    url.hostname = null;
    assert_equals(url.href, 'https://null/path/');
}, 'Set hostname to null');

test(function() {
    var url = new URL('https://www.mydomain.com/path/');
    url.hostname = '';
    assert_equals(url.href, 'https://www.mydomain.com/path/');
}, 'Set hostname to empty string');

test(function() {
    var url = new URL('file:///path/');
    assert_equals(url.hostname, '');
    url.hostname = 'mydomain.com';
    assert_equals(url.href, 'file://mydomain.com/path/');
}, 'Set hostname to URL with file: protocol');

test(function() {
    var url = new URL('https://www.mydomain.com/path/');
    url.hostname = 'www.other\\dom/ain.com';
    assert_equals(url.href, 'https://www.other/path/');
}, 'Set hostname containing slashes in it');

test(function() {
    var url = new URL('https:/\[email protected]/path/');
    assert_equals(url.hostname, 'domain.com');
    url.hostname = 'www.other^domain.com';
    assert_equals(url.href, 'https://[email protected]/path/');
}, 'Set hostname to a malformed URL');

test(function() {
    var url = new URL('https://[email protected]/purl..th/');
    url.hostname = 'www.other^domain.com';
    assert_equals(url.href, 'https://[email protected]/purl..th/');
}, 'Set hostname to URL containing username and ..');

test(function() {
    var url = new URL('tel:+1-816-555-1212');
    assert_equals(url.hostname, '');
    url.hostname = '+1-800-555-1212';
    assert_equals(url.href, 'tel:+1-816-555-1212');
}, 'Set hostname to a URL with tel: protocol');

test(function() {
    var url = new URL('http://abc.de/path/file?query#fragment');
    var expected = 'abc.de';
    assert_equals(url.hostname, expected);

    assert_throws_js(TypeError, () => { url.href = 'invalid'; });
    assert_equals(url.hostname, expected);

    url.hostname = 'changed';
    assert_equals(url.hostname, 'changed');
    assert_equals(url.href, 'http://changed/path/file?query#fragment');
}, 'hostname property invalid URL');

</script>