Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

/* 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/. */
"use strict";
var data = [
{
// singleword.
wrong: "http://example/",
},
{
// upgrade protocol.
noAlternateURI: true,
},
{
// no difference.
noProtocolFixup: true,
noAlternateURI: true,
},
{
// don't add prefix when there's more than one dot.
noAlternateURI: true,
},
{
// http -> https.
noAlternateURI: true,
},
{
// domain.com -> https://www.domain.com.
},
{
// example/example... -> https://www.example.com/example/
},
{
// example/example/s#q -> www.example.com/example/s#q.
},
{
},
{
wrong: "http://モジラ",
},
{
wrong: "http://xn--yck6dwa",
},
{
noProtocolFixup: true,
},
{
wrong: "https://モジラ",
noProtocolFixup: true,
},
{
noProtocolFixup: true,
},
{
// Host is https -> fixup typos of protocol
noAlternateURI: true,
},
{
// Host is http -> fixup typos of protocol
noAlternateURI: true,
},
{
// Host is localhost -> fixup typos of protocol
noAlternateURI: true,
},
{
// view-source is not http/https -> error
reject: true,
comment: "Scheme should be either http or https",
},
{
// file is not http/https -> error
reject: true,
comment: "Scheme should be either http or https",
},
{
// Protocol is missing -> error
wrong: "example.com",
reject: true,
comment: "Scheme should be either http or https",
},
{
// Empty input -> error
wrong: "",
reject: true,
comment: "Should pass a non-null uri",
},
];
add_task(async function setup() {
Services.prefs.setStringPref("browser.fixup.alternate.prefix", "www.");
Services.prefs.setStringPref("browser.fixup.alternate.suffix", ".com");
Services.prefs.setStringPref("browser.fixup.alternate.protocol", "https");
registerCleanupFunction(function () {
Services.prefs.clearUserPref("browser.fixup.alternate.prefix");
Services.prefs.clearUserPref("browser.fixup.alternate.suffix");
Services.prefs.clearUserPref("browser.fixup.alternate.protocol");
});
});
add_task(function test_default_https_pref() {
for (let item of data) {
if (item.reject) {
Assert.throws(
() => Services.uriFixup.forceHttpFixup(item.wrong),
/NS_ERROR_FAILURE/,
item.comment
);
} else {
let { fixupChangedProtocol, fixupCreatedAlternateURI, fixedURI } =
Services.uriFixup.forceHttpFixup(item.wrong);
Assert.equal(fixedURI.spec, item.fixed, "Specs should be the same");
Assert.equal(
fixupChangedProtocol,
!item.noProtocolFixup,
`fixupChangedProtocol should be ${!item.noAlternateURI}`
);
Assert.equal(
fixupCreatedAlternateURI,
!item.noAlternateURI,
`fixupCreatedAlternateURI should be ${!item.limitedFixup}`
);
}
}
});