Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
const { matchURLPattern, parseURLPattern } = ChromeUtils.importESModule(
);
// Test several variations which should match a string based http://example.com
// pattern.
add_task(async function test_matchURLPattern_url_variations() {
const pattern = parseURLPattern({
type: "string",
pattern: "http://example.com",
});
const urls = [
"http:example.com",
"http:/example.com",
];
for (const url of urls) {
ok(
matchURLPattern(pattern, url),
`url "${url}" should match pattern "http://example.com"`
);
}
// Test URLs close to http://example.com but which should not match.
const failingUrls = [
];
for (const url of failingUrls) {
ok(
!matchURLPattern(pattern, url),
`url "${url}" should not match pattern "http://example.com"`
);
}
});
add_task(async function test_matchURLPattern_stringPatterns() {
const tests = [
{
pattern: "http://example.com",
match: true,
},
{
pattern: "HTTP://example.com:80",
match: true,
},
{
pattern: "http://example.com:80",
match: true,
},
{
match: true,
},
{
match: false,
},
{
match: false,
},
{
match: false,
},
{
match: true,
},
{
match: false,
},
{
match: true,
},
{
match: true,
},
{
match: false,
},
{
match: false,
},
{
match: true,
},
{
match: false,
},
{
match: true,
},
{
match: true,
},
{
match: false,
},
{
match: true,
},
{
match: true,
},
{
match: true,
},
{
match: false,
},
{
match: false,
},
{
match: false,
},
{
match: true,
},
{
pattern: "ws://example.com",
match: true,
},
];
runMatchPatternTests(tests, "string");
});
add_task(async function test_patternPatterns_no_property() {
const tests = [
// Test protocol
{
pattern: {},
match: true,
},
{
pattern: {},
match: true,
},
{
pattern: {},
match: true,
},
{
pattern: {},
match: true,
},
{
pattern: {},
match: true,
},
];
runMatchPatternTests(tests, "pattern");
});
add_task(async function test_patternPatterns_protocol() {
const tests = [
// Test protocol
{
pattern: {
protocol: "http",
},
match: true,
},
{
pattern: {
protocol: "HTTP",
},
match: true,
},
{
pattern: {
protocol: "http",
},
match: true,
},
{
pattern: {
protocol: "http",
},
match: true,
},
{
pattern: {
protocol: "http",
port: "80",
},
match: true,
},
{
pattern: {
protocol: "http",
port: "1234",
},
match: true,
},
{
pattern: {
protocol: "http",
port: "1234",
},
match: false,
},
{
pattern: {
protocol: "http",
},
match: false,
},
{
pattern: {
protocol: "http",
},
match: true,
},
{
pattern: {
protocol: "http",
},
match: true,
},
{
pattern: {
protocol: "http",
},
match: true,
},
];
runMatchPatternTests(tests, "pattern");
});
add_task(async function test_patternPatterns_port() {
const tests = [
{
pattern: {
protocol: "http",
port: "80",
},
url: "http://abc.com/",
match: true,
},
{
pattern: {
port: "1234",
},
match: true,
},
{
pattern: {
port: "1234",
},
match: true,
},
];
runMatchPatternTests(tests, "pattern");
});
add_task(async function test_patternPatterns_hostname() {
const tests = [
{
pattern: {
hostname: "example.com",
},
match: true,
},
{
pattern: {
hostname: "example.com",
},
match: true,
},
{
pattern: {
hostname: "example.com",
},
match: true,
},
{
pattern: {
hostname: "example.com",
},
match: true,
},
{
pattern: {
hostname: "example.com",
},
match: true,
},
{
pattern: {
hostname: "example.com",
},
match: true,
},
{
pattern: {
hostname: "example.com",
},
match: true,
},
{
pattern: {
hostname: "example.com",
},
match: true,
},
{
pattern: {
hostname: "example\\{.com",
},
match: true,
},
{
pattern: {
hostname: "example\\{.com",
},
match: false,
},
{
pattern: {
hostname: "127.0.0.1",
},
match: true,
},
{
pattern: {
hostname: "127.0.0.1",
},
match: false,
},
{
pattern: {
hostname: "[2001:db8::1]",
},
match: true,
},
{
pattern: {
hostname: "[::AB:1]",
},
match: true,
},
];
runMatchPatternTests(tests, "pattern");
});
add_task(async function test_patternPatterns_pathname() {
const tests = [
{
pattern: {
pathname: "/",
},
match: true,
},
{
pattern: {
pathname: "/",
},
match: true,
},
{
pattern: {
pathname: "/",
},
match: true,
},
{
pattern: {
pathname: "path",
},
match: true,
},
{
pattern: {
pathname: "/path",
},
match: true,
},
{
pattern: {
pathname: "path",
},
match: false,
},
{
pattern: {
pathname: "path",
},
match: false,
},
{
pattern: {
pathname: "/",
},
match: false,
},
];
runMatchPatternTests(tests, "pattern");
});
add_task(async function test_patternPatterns_search() {
const tests = [
{
pattern: {
search: "",
},
match: true,
},
{
pattern: {
search: "",
},
match: true,
},
{
pattern: {
search: "",
},
match: true,
},
{
pattern: {
search: "?",
},
match: true,
},
{
pattern: {
search: "?a",
},
match: true,
},
{
pattern: {
search: "?",
},
match: false,
},
{
pattern: {
search: "query",
},
match: true,
},
{
pattern: {
search: "?query",
},
match: true,
},
{
pattern: {
search: "query=value",
},
match: true,
},
{
pattern: {
search: "query",
},
match: false,
},
{
pattern: {
search: "query",
},
match: true,
},
];
runMatchPatternTests(tests, "pattern");
});
function runMatchPatternTests(tests, type) {
for (const test of tests) {
let pattern;
if (type == "pattern") {
pattern = parseURLPattern({ type: "pattern", ...test.pattern });
} else {
pattern = parseURLPattern({ type: "string", pattern: test.pattern });
}
equal(
matchURLPattern(pattern, test.url),
test.match,
`url "${test.url}" ${
test.match ? "should" : "should not"
} match pattern ${JSON.stringify(test.pattern)}`
);
}
}