Bug 1436377 - Policy engine - Implement URLorEmpty parameter type. r=Mossop

MozReview-Commit-ID: 96GqykNb3Zr
This commit is contained in:
Felipe Gomes 2018-02-07 20:37:55 -02:00
Родитель 5260eb4e2d
Коммит 5be5d04274
2 изменённых файлов: 35 добавлений и 0 удалений

Просмотреть файл

@ -43,6 +43,7 @@ function validateAndParseParamRecursive(param, properties) {
case "integer":
case "string":
case "URL":
case "URLorEmpty":
case "origin":
return validateAndParseSimpleParam(param, properties.type);
@ -143,10 +144,16 @@ function validateAndParseSimpleParam(param, type) {
break;
case "URL":
case "URLorEmpty":
if (typeof(param) != "string") {
break;
}
if (type == "URLorEmpty" && param === "") {
valid = true;
break;
}
try {
parsedParam = Services.io.newURI(param);
valid = true;

Просмотреть файл

@ -99,11 +99,39 @@ add_task(async function test_URL_values() {
is(parsed.pathQueryRef, "/foo#bar", "pathQueryRef is correct");
// Invalid values:
ok(!PoliciesValidator.validateAndParseParameters("", schema)[0], "Empty string is not accepted for URL");
ok(!PoliciesValidator.validateAndParseParameters("www.example.com", schema)[0], "Scheme is required for URL");
ok(!PoliciesValidator.validateAndParseParameters("https://:!$%", schema)[0], "Invalid URL");
ok(!PoliciesValidator.validateAndParseParameters({}, schema)[0], "Invalid value");
});
add_task(async function test_URLorEmpty_values() {
let schema = {
type: "URLorEmpty"
};
let valid, parsed;
[valid, parsed] = PoliciesValidator.validateAndParseParameters("https://www.example.com/foo#bar", schema);
ok(valid, "URL is valid");
ok(parsed instanceof Ci.nsIURI, "parsed is a nsIURI");
is(parsed.prePath, "https://www.example.com", "prePath is correct");
is(parsed.pathQueryRef, "/foo#bar", "pathQueryRef is correct");
// Test that this type also accept empty strings
[valid, parsed] = PoliciesValidator.validateAndParseParameters("", schema);
ok(valid, "URLorEmpty is valid");
ok(!parsed, "parsed value is falsy");
is(typeof(parsed), "string", "parsed is a string");
is(parsed, "", "parsed is an empty string");
// Invalid values:
ok(!PoliciesValidator.validateAndParseParameters(" ", schema)[0], "Non-empty string is not accepted");
ok(!PoliciesValidator.validateAndParseParameters("www.example.com", schema)[0], "Scheme is required for URL");
ok(!PoliciesValidator.validateAndParseParameters("https://:!$%", schema)[0], "Invalid URL");
ok(!PoliciesValidator.validateAndParseParameters({}, schema)[0], "Invalid value");
});
add_task(async function test_origin_values() {
// Origin is a URL that doesn't contain a path/query string (i.e., it's only scheme + host + port)
let schema = {