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

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

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

    url = new URL('http://www.domain.com:8080/');
    assert_equals(url.port, '8080');

    url.port = 8081;
    assert_equals(url.port, '8081');

    url.port = '';
    assert_equals(url.port, '');

    url.port = 80;
    assert_equals(url.port, '');

    url.port = 0;
    assert_equals(url.port, '0');

    url.port = '8080junk';
    assert_equals(url.port, '8080');

    url.port = 0;

    url.port = 'junk8080';
    assert_equals(url.port, '0');
}, 'Basic port');

test(function() {
    var url = new URL('mailto:[email protected]');
    assert_equals(url.port, '');

    url = new URL('file:///home/abarth');
    assert_equals(url.port, '');
}, 'no port');

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

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

    url.port = '8081';
    assert_equals(url.port, '8081');
    assert_equals(url.href, 'http://abc.de:8081/path/file?query#fragment');
}, 'port property invalid URL');
</script>