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

<!DOCTYPE HTML>
<link rel="help" href="http://url.spec.whatwg.org/#dom-url-username">
<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.username, '');

    url = new URL('http://[email protected]/');
    assert_equals(url.username, 'user');

    url.username = 'changed';
    assert_equals(url.toString(), 'http://[email protected]/');

    url.username = '';
    assert_equals(url.toString(), 'http://www.domain.com/');
}, 'username property');

test(function() {
    var url = new URL('about:test');
    assert_equals(url.username, '');

    url.username = 'user';
    assert_equals(url.username, '');
    assert_equals(url.toString(), 'about:test');
}, 'username property on non relative URL');

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

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

    url.username = 'changed';
    assert_equals(url.username, 'changed');
    assert_equals(url.href, 'http://[email protected]/');
}, 'username property invalid URL');

</script>