Files
node/test/parallel/test-urlpattern.js
Khaidi Chu 89b32cf16d url: add Symbol.toStringTag to URLPattern
Define Symbol.toStringTag on URLPattern.prototype so its class string
matches the Web IDL interface name.

Add coverage for the property descriptor and
Object.prototype.toString().

Fixes: https://github.com/nodejs/node/issues/65924
Signed-off-by: XadillaX <i@2333.moe>
PR-URL: https://github.com/nodejs/node/pull/65925
Reviewed-By: Xuguang Mei <meixuguang@gmail.com>
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
Reviewed-By: Aviv Keller <me@aviv.sh>
Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
2026-09-12 08:28:59 +00:00

107 lines
2.2 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const { URLPattern } = require('url');
{
assert.deepStrictEqual(
Object.getOwnPropertyDescriptor(URLPattern.prototype, Symbol.toStringTag),
{
configurable: true,
enumerable: false,
value: 'URLPattern',
writable: false,
});
assert.strictEqual(
Object.prototype.toString.call(new URLPattern()),
'[object URLPattern]');
}
// Verify that if an error is thrown while accessing any of the
// init options, the error is appropriately propagated.
assert.throws(() => {
new URLPattern({
get protocol() {
throw new Error('boom');
}
});
}, {
message: 'boom',
});
// Verify that if an error is thrown while accessing the ignoreCase
// option, the error is appropriately propagated.
assert.throws(() => {
new URLPattern({}, { get ignoreCase() {
throw new Error('boom');
} });
}, {
message: 'boom'
});
{
const result = new URLPattern({ pathname: '/:value' })
.exec('https://example.com/test');
assert.deepStrictEqual(Object.keys(result), [
'hash',
'hostname',
'inputs',
'password',
'pathname',
'port',
'protocol',
'search',
'username',
]);
assert.deepStrictEqual(Object.keys(result.pathname), [
'groups',
'input',
]);
assert.strictEqual(result.hostname.input, 'example.com');
assert.strictEqual(result.pathname.input, '/test');
assert.strictEqual(result.pathname.groups.value, 'test');
}
{
const accessed = [];
const expected = [
'baseURL',
'hash',
'hostname',
'password',
'pathname',
'port',
'protocol',
'search',
'username',
];
const init = new Proxy({}, {
get(target, name, receiver) {
accessed.push(name);
return Reflect.get(target, name, receiver);
},
});
new URLPattern(init);
assert.deepStrictEqual(accessed, expected);
}
{
const input = new URLPattern({ pathname: '/x' })
.exec({
protocol: 'https',
pathname: '/x',
username: undefined,
}).inputs[0];
assert.deepStrictEqual(Object.keys(input), ['pathname', 'protocol']);
assert.strictEqual('username' in input, false);
assert.deepStrictEqual({ ...input }, {
pathname: '/x',
protocol: 'https',
});
}