Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

/* Any copyright is dedicated to the Public Domain.
const { UrlTokenizer } = ChromeUtils.importESModule(
"moz-src:///browser/components/aiwindow/ui/modules/UrlTokenizer.sys.mjs"
);
add_task(async function test_UrlTokenizer_encodeToken() {
const cases = [
{
message: "Works for a URL with a path.",
expected: "GITHUB_COM_FOO_BAR_BAZ_1",
},
{
message:
"Returns a new number for a URL that is different but creates the same token.",
expected: "GITHUB_COM_FOO_BAR_BAZ_2",
},
{
message: "Returns the exact same token given another URL",
expected: "GITHUB_COM_FOO_BAR_BAZ_1",
},
{
message:
"Returns a different token given the same URL with a different protocol",
expected: "GITHUB_COM_FOO_BAR_BAZ_3",
},
{
message: "Can handle about URLs.",
url: "about:config",
expected: "ABOUT_CONFIG_1",
},
{
message: "Uses non-http protocols",
expected: "FTP_GITHUB_COM_FOO_BAR_BAZ_1",
},
{
message: "Uses invalid protocols",
expected: "ASDF_GITHUB_COM_FOO_BAR_BAZ_1",
},
{
message: "Ignores the port.",
expected: "GITHUB_COM_IGNORE_PORT_1",
},
{
message: "Ignores the params.",
expected: "GITHUB_COM_IGNORE_PARAMS_1",
},
{
message: "Ignores the hash.",
expected: "GITHUB_COM_IGNORE_HASH_PART_1",
},
{
message: "Truncates text in the host from 110 to 100.",
url: `http://www.${"a".repeat(110)}.com/foo`,
expected: "A".repeat(100) + "_1",
},
{
message: "Skips text in the path that is too long",
url: `http://github.com/skip/long/path/` + "A".repeat(100),
expected: "GITHUB_COM_SKIP_LONG_PATH_1",
},
];
const urlTokenizer = new UrlTokenizer({});
for (const { message, url, expected } of cases) {
const token = urlTokenizer.encodeToken(url);
Assert.equal(token, expected, message);
const decodedUrl = urlTokenizer.tokenToUrl.get(token);
Assert.equal(
decodedUrl,
url,
`Expected the decoded token (${token}) to equal '${url}' but got: ${decodedUrl}`
);
}
});