export const test1 = () => {
if (URL.canParse("cats", "http://www.example.com/dogs")) {
const url = new URL("cats", "http://www.example.com/dogs");
console.log(url.hostname); console.log(url.pathname);
return url.hostname === "www.example.com" && url.pathname === "/cats" && url.protocol === "http:";
} else {
console.log("Invalid URL");
return false;
}
};
export const test2 = () => {
if (URL.canParse("../cats", "http://www.example.com/dogs")) {
const url = new URL("../cats", "http://www.example.com/dogs");
console.log(url.hostname); console.log(url.pathname);
return url.hostname === "www.example.com" && url.pathname === "/cats";
} else {
console.log("Invalid URL");
return false;
}
};
export const test3 = () => {
const paramsString = "q=URLUtils.searchParams&topic=api";
const searchParams = new URLSearchParams(paramsString);
for (const p of searchParams) {
console.log(p);
}
console.log(searchParams.has("topic")); console.log(searchParams.has("topic", "fish")); console.log(searchParams.get("topic") === "api"); console.log(searchParams.getAll("topic")); console.log(searchParams.get("foo") === null); console.log(searchParams.append("topic", "webdev"));
console.log(searchParams.toString()); console.log(searchParams.set("topic", "More webdev"));
console.log(searchParams.toString()); console.log(searchParams.delete("topic"));
console.log(searchParams.toString()); }
export const test4 = () => {
const url = new URL("https://test:pass@example.com:1234/path?query=URLUtils.searchParams&topic=api#fragment");
console.log(url.protocol);
console.log(url.username);
console.log(url.password);
console.log(url.hostname);
console.log(url.port);
console.log(url.pathname);
console.log(url.hash);
console.log(url.searchParams.get("topic"));
console.log(url.searchParams.get("query"));
console.log(url.href);
}
export const test5 = () => {
const url = new URL("https://example.com/path?query=value#fragment", undefined);
console.log(url.protocol); console.log(url.hostname); console.log(url.pathname); console.log(url.search); console.log(url.hash);
return url.protocol === "https:" &&
url.hostname === "example.com" &&
url.pathname === "/path" &&
url.search === "?query=value" &&
url.hash === "#fragment";
}
export const test6 = () => {
const url = require('node:url');
const path1 = url.fileURLToPath('file:///foo/bar');
console.log('fileURLToPath:', path1);
if (path1 !== '/foo/bar') return false;
const path2 = url.fileURLToPath(new URL('file:///foo%20bar'));
console.log('fileURLToPath URL:', path2);
if (path2 !== '/foo bar') return false;
const fileUrl = url.pathToFileURL('/foo/bar');
console.log('pathToFileURL:', fileUrl.href);
if (fileUrl.href !== 'file:///foo/bar') return false;
const fileUrl2 = url.pathToFileURL('/foo bar');
console.log('pathToFileURL space:', fileUrl2.href);
if (fileUrl2.href !== 'file:///foo%20bar') return false;
const opts = url.urlToHttpOptions(new URL('http://user:pass@example.com:8080/path?q=1#hash'));
console.log('urlToHttpOptions:', JSON.stringify(opts));
if (opts.protocol !== 'http:') return false;
if (opts.hostname !== 'example.com') return false;
if (opts.port !== 8080) return false;
if (opts.auth !== 'user:pass') return false;
if (opts.pathname !== '/path') return false;
if (opts.path !== '/path?q=1') return false;
const formatted = url.format(new URL('http://user:pass@example.com/a?b=c#d'), { auth: false });
console.log('format no auth:', formatted);
if (formatted !== 'http://example.com/a?b=c#d') return false;
const parsed = url.parse('http://example.com/path?q=1#hash');
console.log('parse:', parsed.protocol, parsed.hostname, parsed.pathname);
if (parsed.protocol !== 'http:') return false;
if (parsed.hostname !== 'example.com') return false;
if (parsed.pathname !== '/path') return false;
return true;
};
export const test7 = () => {
const base = new URL("https://example.com/a/b/");
const url = new URL("x", base);
console.log('test7 href:', url.href);
return url.href === "https://example.com/a/b/x";
};
export const test8 = () => {
const base = new URL("https://example.com/a/b/");
const canParse = URL.canParse("x", base);
console.log('test8 canParse:', canParse);
if (!canParse) return false;
const parsed = URL.parse("x", base);
console.log('test8 parsed:', parsed ? parsed.href : null);
if (!parsed) return false;
if (parsed.href !== "https://example.com/a/b/x") return false;
if (URL.canParse("://invalid")) return false;
return true;
};
export const test9 = () => {
const url = new URL("https://example.com/old?old=1#oldhash");
url.pathname = { toString: () => '/v1' };
console.log('test9 pathname:', url.pathname);
if (url.pathname !== '/v1') return false;
url.hash = { toString: () => '#newhash' };
console.log('test9 hash:', url.hash);
if (url.hash !== '#newhash') return false;
url.search = { toString: () => '?key=value' };
console.log('test9 search:', url.search);
if (url.search !== '?key=value') return false;
url.protocol = { toString: () => 'http:' };
console.log('test9 protocol:', url.protocol);
if (url.protocol !== 'http:') return false;
url.username = { toString: () => 'user' };
console.log('test9 username:', url.username);
if (url.username !== 'user') return false;
url.password = { toString: () => 'pass' };
console.log('test9 password:', url.password);
if (url.password !== 'pass') return false;
url.hostname = { toString: () => 'other.com' };
console.log('test9 hostname:', url.hostname);
if (url.hostname !== 'other.com') return false;
url.port = { toString: () => '8080' };
console.log('test9 port:', url.port);
if (url.port !== '8080') return false;
url.href = { toString: () => 'https://final.example.com/' };
console.log('test9 href:', url.href);
if (url.href !== 'https://final.example.com/') return false;
url.host = { toString: () => 'host.example.com:9090' };
console.log('test9 host:', url.host);
if (url.host !== 'host.example.com:9090') return false;
return true;
};