1'use strict'; 2require('../common'); 3const assert = require('assert'); 4const { URL, URLSearchParams, format } = require('url'); 5 6[ 7 { name: 'toString' }, 8 { name: 'toJSON' }, 9 { name: Symbol.for('nodejs.util.inspect.custom') }, 10].forEach(({ name }) => { 11 testMethod(URL.prototype, name); 12}); 13 14[ 15 'http://www.google.com', 16 'https://www.domain.com:443', 17 'file:///Users/yagiz/Developer/node', 18].forEach((url) => { 19 const u = new URL(url); 20 assert.strictEqual(JSON.stringify(u), `"${u.href}"`); 21 assert.strictEqual(u.toString(), u.href); 22 assert.strictEqual(format(u), u.href); 23}); 24 25[ 26 { name: 'href' }, 27 { name: 'protocol' }, 28 { name: 'username' }, 29 { name: 'password' }, 30 { name: 'host' }, 31 { name: 'hostname' }, 32 { name: 'port' }, 33 { name: 'pathname' }, 34 { name: 'search' }, 35 { name: 'hash' }, 36 { name: 'origin', readonly: true }, 37 { name: 'searchParams', readonly: true }, 38].forEach(({ name, readonly = false }) => { 39 testAccessor(URL.prototype, name, readonly); 40}); 41 42[ 43 { name: 'createObjectURL' }, 44 { name: 'revokeObjectURL' }, 45].forEach(({ name }) => { 46 testStaticAccessor(URL, name); 47}); 48 49[ 50 { name: 'append' }, 51 { name: 'delete' }, 52 { name: 'get' }, 53 { name: 'getAll' }, 54 { name: 'has' }, 55 { name: 'set' }, 56 { name: 'sort' }, 57 { name: 'entries' }, 58 { name: 'forEach' }, 59 { name: 'keys' }, 60 { name: 'values' }, 61 { name: 'toString' }, 62 { name: Symbol.iterator, methodName: 'entries' }, 63 { name: Symbol.for('nodejs.util.inspect.custom') }, 64].forEach(({ name, methodName }) => { 65 testMethod(URLSearchParams.prototype, name, methodName); 66}); 67 68{ 69 const params = new URLSearchParams(); 70 params.append('a', 'b'); 71 params.append('a', 'c'); 72 params.append('b', 'c'); 73 assert.strictEqual(params.size, 3); 74} 75 76{ 77 const u = new URL('https://abc.com/?q=old'); 78 const s = u.searchParams; 79 u.href = 'http://abc.com/?q=new'; 80 assert.strictEqual(s.get('q'), 'new'); 81} 82 83function stringifyName(name) { 84 if (typeof name === 'symbol') { 85 const { description } = name; 86 if (description === undefined) { 87 return ''; 88 } 89 return `[${description}]`; 90 } 91 92 return name; 93} 94 95function testMethod(target, name, methodName = stringifyName(name)) { 96 const desc = Object.getOwnPropertyDescriptor(target, name); 97 assert.notStrictEqual(desc, undefined); 98 assert.strictEqual(desc.enumerable, typeof name === 'string'); 99 100 const { value } = desc; 101 assert.strictEqual(typeof value, 'function'); 102 assert.strictEqual(value.name, methodName); 103 assert.strictEqual( 104 Object.hasOwn(value, 'prototype'), 105 false, 106 ); 107} 108 109function testAccessor(target, name, readonly = false) { 110 const desc = Object.getOwnPropertyDescriptor(target, name); 111 assert.notStrictEqual(desc, undefined); 112 assert.strictEqual(desc.enumerable, typeof name === 'string'); 113 114 const methodName = stringifyName(name); 115 const { get, set } = desc; 116 assert.strictEqual(typeof get, 'function'); 117 assert.strictEqual(get.name, `get ${methodName}`); 118 assert.strictEqual( 119 Object.hasOwn(get, 'prototype'), 120 false, 121 ); 122 123 if (readonly) { 124 assert.strictEqual(set, undefined); 125 } else { 126 assert.strictEqual(typeof set, 'function'); 127 assert.strictEqual(set.name, `set ${methodName}`); 128 assert.strictEqual( 129 Object.hasOwn(set, 'prototype'), 130 false, 131 ); 132 } 133} 134 135function testStaticAccessor(target, name) { 136 const desc = Object.getOwnPropertyDescriptor(target, name); 137 assert.notStrictEqual(desc, undefined); 138 139 assert.strictEqual(desc.configurable, true); 140 assert.strictEqual(desc.enumerable, true); 141 assert.strictEqual(desc.writable, true); 142} 143