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

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

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

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

test(function() {
    var url = new URL('https://www.mydomain.com:8080/path/');
    url.host = 'www.otherdomain.com:80';
    assert_equals(url.href, 'https://www.otherdomain.com:80/path/');
}, 'Set default port for another protocol');

test(function() {
    var url = new URL('https://www.mydomain.com:8080/path/');
    url.host = 'www.otherdomain.com:443';
    assert_equals(url.href, 'https://www.otherdomain.com/path/');
}, 'Set default port');

test(function() {
    var url = new URL('https://www.mydomain.com:8080/path/');
    url.host = 'www.otherdomain.com:44a5';
    assert_equals(url.href, 'https://www.otherdomain.com:44/path/');
}, 'Set host with letters in port number');

test(function() {
    var url = new URL('https://www.mydomain.com:8080/path/');
    url.host = 'www.otherdomain.com: 443';
    assert_equals(url.href, 'https://www.otherdomain.com:8080/path/');
}, 'Leading space in port number');

test(function() {
    var url = new URL('https://www.mydomain.com:8080/path/');
    url.host = 'www.otherdomain.com:';
    assert_equals(url.href, 'https://www.otherdomain.com:8080/path/');
}, 'Colon without port number');

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

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

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

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

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

test(function() {
    var url = new URL('https://domain.com:8080/path/');
    url.host = ':www.otherdomain.com:15';
    assert_equals(url.href, 'https://domain.com:8080/path/');
}, 'Set host that starts with :');

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

test(function() {
    var url = new URL('tel:+1-816-555-1212');
    assert_equals(url.host, '');
    url.host = '+1-800-555-1212';
    assert_equals(url.href, 'tel:+1-816-555-1212');
}, 'Set host 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.host, expected);

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

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

test(function() {
    var url = new URL('http://www.domain.com/');
    assert_equals(url.host, 'www.domain.com');

    url.host = 'www.bo\udc01\ud802gus.org';
    assert_equals(url.host, 'www.domain.com');
}, 'hostname with unmatched surrogates');

</script>