11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// Tests below are not from WPT.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_cirequire('../common');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst url = new URL('http://user:pass@foo.bar.com:21/aaa/zzz?l=24#test');
101cb0ef41Sopenharmony_ciconst oldParams = url.searchParams;  // For test of [SameObject]
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// To retrieve enumerable but not necessarily own properties,
131cb0ef41Sopenharmony_ci// we need to use the for-in loop.
141cb0ef41Sopenharmony_ciconst props = [];
151cb0ef41Sopenharmony_cifor (const prop in url) {
161cb0ef41Sopenharmony_ci  props.push(prop);
171cb0ef41Sopenharmony_ci}
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci// See: https://url.spec.whatwg.org/#api
201cb0ef41Sopenharmony_ci// https://heycam.github.io/webidl/#es-attributes
211cb0ef41Sopenharmony_ci// https://heycam.github.io/webidl/#es-stringifier
221cb0ef41Sopenharmony_ciconst expected = ['toString',
231cb0ef41Sopenharmony_ci                  'href', 'origin', 'protocol',
241cb0ef41Sopenharmony_ci                  'username', 'password', 'host', 'hostname', 'port',
251cb0ef41Sopenharmony_ci                  'pathname', 'search', 'searchParams', 'hash', 'toJSON'];
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciassert.deepStrictEqual(props, expected);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci// `href` is writable (not readonly) and is stringifier
301cb0ef41Sopenharmony_ciassert.strictEqual(url.toString(), url.href);
311cb0ef41Sopenharmony_ciurl.href = 'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test';
321cb0ef41Sopenharmony_ciassert.strictEqual(url.href,
331cb0ef41Sopenharmony_ci                   'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test');
341cb0ef41Sopenharmony_ciassert.strictEqual(url.toString(), url.href);
351cb0ef41Sopenharmony_ci// Return true because it's configurable, but because the properties
361cb0ef41Sopenharmony_ci// are defined on the prototype per the spec, the deletion has no effect
371cb0ef41Sopenharmony_ciassert.strictEqual((delete url.href), true);
381cb0ef41Sopenharmony_ciassert.strictEqual(url.href,
391cb0ef41Sopenharmony_ci                   'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test');
401cb0ef41Sopenharmony_ciassert.strictEqual(url.searchParams, oldParams);  // [SameObject]
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci// searchParams is readonly. Under strict mode setting a
431cb0ef41Sopenharmony_ci// non-writable property should throw.
441cb0ef41Sopenharmony_ci// Note: this error message is subject to change in V8 updates
451cb0ef41Sopenharmony_ciassert.throws(
461cb0ef41Sopenharmony_ci  () => url.origin = 'http://foo.bar.com:22',
471cb0ef41Sopenharmony_ci  /^TypeError: Cannot set property origin of \[object URL\] which has only a getter$/
481cb0ef41Sopenharmony_ci);
491cb0ef41Sopenharmony_ciassert.strictEqual(url.origin, 'http://foo.bar.com:21');
501cb0ef41Sopenharmony_ciassert.strictEqual(url.toString(),
511cb0ef41Sopenharmony_ci                   'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test');
521cb0ef41Sopenharmony_ciassert.strictEqual((delete url.origin), true);
531cb0ef41Sopenharmony_ciassert.strictEqual(url.origin, 'http://foo.bar.com:21');
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci// The following properties should be writable (not readonly)
561cb0ef41Sopenharmony_ciurl.protocol = 'https:';
571cb0ef41Sopenharmony_ciassert.strictEqual(url.protocol, 'https:');
581cb0ef41Sopenharmony_ciassert.strictEqual(url.toString(),
591cb0ef41Sopenharmony_ci                   'https://user:pass@foo.bar.com:21/aaa/zzz?l=25#test');
601cb0ef41Sopenharmony_ciassert.strictEqual((delete url.protocol), true);
611cb0ef41Sopenharmony_ciassert.strictEqual(url.protocol, 'https:');
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ciurl.username = 'user2';
641cb0ef41Sopenharmony_ciassert.strictEqual(url.username, 'user2');
651cb0ef41Sopenharmony_ciassert.strictEqual(url.toString(),
661cb0ef41Sopenharmony_ci                   'https://user2:pass@foo.bar.com:21/aaa/zzz?l=25#test');
671cb0ef41Sopenharmony_ciassert.strictEqual((delete url.username), true);
681cb0ef41Sopenharmony_ciassert.strictEqual(url.username, 'user2');
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ciurl.password = 'pass2';
711cb0ef41Sopenharmony_ciassert.strictEqual(url.password, 'pass2');
721cb0ef41Sopenharmony_ciassert.strictEqual(url.toString(),
731cb0ef41Sopenharmony_ci                   'https://user2:pass2@foo.bar.com:21/aaa/zzz?l=25#test');
741cb0ef41Sopenharmony_ciassert.strictEqual((delete url.password), true);
751cb0ef41Sopenharmony_ciassert.strictEqual(url.password, 'pass2');
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ciurl.host = 'foo.bar.net:22';
781cb0ef41Sopenharmony_ciassert.strictEqual(url.host, 'foo.bar.net:22');
791cb0ef41Sopenharmony_ciassert.strictEqual(url.toString(),
801cb0ef41Sopenharmony_ci                   'https://user2:pass2@foo.bar.net:22/aaa/zzz?l=25#test');
811cb0ef41Sopenharmony_ciassert.strictEqual((delete url.host), true);
821cb0ef41Sopenharmony_ciassert.strictEqual(url.host, 'foo.bar.net:22');
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ciurl.hostname = 'foo.bar.org';
851cb0ef41Sopenharmony_ciassert.strictEqual(url.hostname, 'foo.bar.org');
861cb0ef41Sopenharmony_ciassert.strictEqual(url.toString(),
871cb0ef41Sopenharmony_ci                   'https://user2:pass2@foo.bar.org:22/aaa/zzz?l=25#test');
881cb0ef41Sopenharmony_ciassert.strictEqual((delete url.hostname), true);
891cb0ef41Sopenharmony_ciassert.strictEqual(url.hostname, 'foo.bar.org');
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ciurl.port = '23';
921cb0ef41Sopenharmony_ciassert.strictEqual(url.port, '23');
931cb0ef41Sopenharmony_ciassert.strictEqual(url.toString(),
941cb0ef41Sopenharmony_ci                   'https://user2:pass2@foo.bar.org:23/aaa/zzz?l=25#test');
951cb0ef41Sopenharmony_ciassert.strictEqual((delete url.port), true);
961cb0ef41Sopenharmony_ciassert.strictEqual(url.port, '23');
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ciurl.pathname = '/aaa/bbb';
991cb0ef41Sopenharmony_ciassert.strictEqual(url.pathname, '/aaa/bbb');
1001cb0ef41Sopenharmony_ciassert.strictEqual(url.toString(),
1011cb0ef41Sopenharmony_ci                   'https://user2:pass2@foo.bar.org:23/aaa/bbb?l=25#test');
1021cb0ef41Sopenharmony_ciassert.strictEqual((delete url.pathname), true);
1031cb0ef41Sopenharmony_ciassert.strictEqual(url.pathname, '/aaa/bbb');
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ciurl.search = '?k=99';
1061cb0ef41Sopenharmony_ciassert.strictEqual(url.search, '?k=99');
1071cb0ef41Sopenharmony_ciassert.strictEqual(url.toString(),
1081cb0ef41Sopenharmony_ci                   'https://user2:pass2@foo.bar.org:23/aaa/bbb?k=99#test');
1091cb0ef41Sopenharmony_ciassert.strictEqual((delete url.search), true);
1101cb0ef41Sopenharmony_ciassert.strictEqual(url.search, '?k=99');
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ciurl.hash = '#abcd';
1131cb0ef41Sopenharmony_ciassert.strictEqual(url.hash, '#abcd');
1141cb0ef41Sopenharmony_ciassert.strictEqual(url.toString(),
1151cb0ef41Sopenharmony_ci                   'https://user2:pass2@foo.bar.org:23/aaa/bbb?k=99#abcd');
1161cb0ef41Sopenharmony_ciassert.strictEqual((delete url.hash), true);
1171cb0ef41Sopenharmony_ciassert.strictEqual(url.hash, '#abcd');
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci// searchParams is readonly. Under strict mode setting a
1201cb0ef41Sopenharmony_ci// non-writable property should throw.
1211cb0ef41Sopenharmony_ci// Note: this error message is subject to change in V8 updates
1221cb0ef41Sopenharmony_ciassert.throws(
1231cb0ef41Sopenharmony_ci  () => url.searchParams = '?k=88',
1241cb0ef41Sopenharmony_ci  /^TypeError: Cannot set property searchParams of \[object URL\] which has only a getter$/
1251cb0ef41Sopenharmony_ci);
1261cb0ef41Sopenharmony_ciassert.strictEqual(url.searchParams, oldParams);
1271cb0ef41Sopenharmony_ciassert.strictEqual(url.toString(),
1281cb0ef41Sopenharmony_ci                   'https://user2:pass2@foo.bar.org:23/aaa/bbb?k=99#abcd');
1291cb0ef41Sopenharmony_ciassert.strictEqual((delete url.searchParams), true);
1301cb0ef41Sopenharmony_ciassert.strictEqual(url.searchParams, oldParams);
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci// Test special origins
1331cb0ef41Sopenharmony_ci[
1341cb0ef41Sopenharmony_ci  { expected: 'https://whatwg.org',
1351cb0ef41Sopenharmony_ci    url: 'blob:https://whatwg.org/d0360e2f-caee-469f-9a2f-87d5b0456f6f' },
1361cb0ef41Sopenharmony_ci  { expected: 'ftp://example.org', url: 'ftp://example.org/foo' },
1371cb0ef41Sopenharmony_ci  { expected: 'http://example.org', url: 'http://example.org/foo' },
1381cb0ef41Sopenharmony_ci  { expected: 'https://example.org', url: 'https://example.org/foo' },
1391cb0ef41Sopenharmony_ci  { expected: 'ws://example.org', url: 'ws://example.org/foo' },
1401cb0ef41Sopenharmony_ci  { expected: 'wss://example.org', url: 'wss://example.org/foo' },
1411cb0ef41Sopenharmony_ci  { expected: 'null', url: 'gopher://gopher.quux.org/1/' },
1421cb0ef41Sopenharmony_ci  { expected: 'null', url: 'file:///tmp/mock/path' },
1431cb0ef41Sopenharmony_ci  { expected: 'null', url: 'npm://nodejs/rules' },
1441cb0ef41Sopenharmony_ci].forEach((test) => {
1451cb0ef41Sopenharmony_ci  assert.strictEqual(new URL(test.url).origin, test.expected);
1461cb0ef41Sopenharmony_ci});
147