node/test/parallel/test-whatwg-url-properties.js
ExE Boss 0dfc0f5702 url: fix definitions of URL/SearchParams methods and accessors
This fixes getter and setter names for the WHATWG URL classes, and fixes
a few other inconsistencies with browsers implementations.

Co-authored-by: Gerhard Stöbich <deb2001-github@yahoo.de>

PR-URL: https://github.com/nodejs/node/pull/36799
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
2021-02-13 17:59:21 +01:00

101 lines
2.5 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const { URL, URLSearchParams } = require('url');
[
{ name: 'toString' },
{ name: 'toJSON' },
{ name: Symbol.for('nodejs.util.inspect.custom') },
].forEach(({ name }) => {
testMethod(URL.prototype, name);
});
[
{ name: 'href' },
{ name: 'protocol' },
{ name: 'username' },
{ name: 'password' },
{ name: 'host' },
{ name: 'hostname' },
{ name: 'port' },
{ name: 'pathname' },
{ name: 'search' },
{ name: 'hash' },
{ name: 'origin', readonly: true },
{ name: 'searchParams', readonly: true },
].forEach(({ name, readonly = false }) => {
testAccessor(URL.prototype, name, readonly);
});
[
{ name: 'append' },
{ name: 'delete' },
{ name: 'get' },
{ name: 'getAll' },
{ name: 'has' },
{ name: 'set' },
{ name: 'sort' },
{ name: 'entries' },
{ name: 'forEach' },
{ name: 'keys' },
{ name: 'values' },
{ name: 'toString' },
{ name: Symbol.iterator, methodName: 'entries' },
{ name: Symbol.for('nodejs.util.inspect.custom') },
].forEach(({ name, methodName }) => {
testMethod(URLSearchParams.prototype, name, methodName);
});
function stringifyName(name) {
if (typeof name === 'symbol') {
const { description } = name;
if (description === undefined) {
return '';
}
return `[${description}]`;
}
return name;
}
function testMethod(target, name, methodName = stringifyName(name)) {
const desc = Object.getOwnPropertyDescriptor(target, name);
assert.notStrictEqual(desc, undefined);
assert.strictEqual(desc.enumerable, typeof name === 'string');
const { value } = desc;
assert.strictEqual(typeof value, 'function');
assert.strictEqual(value.name, methodName);
assert.strictEqual(
Object.prototype.hasOwnProperty.call(value, 'prototype'),
false,
);
}
function testAccessor(target, name, readonly = false) {
const desc = Object.getOwnPropertyDescriptor(target, name);
assert.notStrictEqual(desc, undefined);
assert.strictEqual(desc.enumerable, typeof name === 'string');
const methodName = stringifyName(name);
const { get, set } = desc;
assert.strictEqual(typeof get, 'function');
assert.strictEqual(get.name, `get ${methodName}`);
assert.strictEqual(
Object.prototype.hasOwnProperty.call(get, 'prototype'),
false,
);
if (readonly) {
assert.strictEqual(set, undefined);
} else {
assert.strictEqual(typeof set, 'function');
assert.strictEqual(set.name, `set ${methodName}`);
assert.strictEqual(
Object.prototype.hasOwnProperty.call(set, 'prototype'),
false,
);
}
}