Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!DOCTYPE HTML>
<html>
<head>
<title>Test for simple WebExtension</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script type="text/javascript" src="head.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="text/javascript">
"use strict";
add_task(async function test_webRequest_upgrade() {
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: [
"webRequest",
"webRequestBlocking",
"*://mochi.test/tests/*",
],
},
background() {
browser.webRequest.onSendHeaders.addListener((details) => {
// At this point, the request should have been upgraded.
browser.test.assertTrue(details.url.startsWith("https:"), "request is upgraded");
browser.test.assertTrue(details.url.includes("file_sample"), "redirect after upgrade worked");
// Note: although not significant for the test assertions, note that
// the requested file won't load - https://mochi.test:8888/ does not
// resolve to anything on the test server.
browser.test.sendMessage("finished");
}, {urls: ["*://mochi.test/tests/*"]});
browser.webRequest.onBeforeRequest.addListener((details) => {
browser.test.log(`onBeforeRequest ${details.requestId} ${details.url}`);
let url = new URL(details.url);
if (url.protocol == "http:") {
return {upgradeToSecure: true};
}
// After the channel is initially upgraded, we get another onBeforeRequest
// call. Here we can redirect again to a new url.
if (details.url.includes("file_mixed.html")) {
let redirectUrl = new URL("file_sample.html", details.url).href;
return {redirectUrl};
}
}, {urls: ["*://mochi.test/tests/*"]}, ["blocking"]);
},
});
await extension.startup();
await extension.awaitMessage("finished");
win.close();
await extension.unload();
});
add_task(async function test_webRequest_redirect_wins() {
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: [
"webRequest",
"webRequestBlocking",
"*://mochi.test/tests/*",
],
},
background() {
browser.webRequest.onSendHeaders.addListener((details) => {
// At this point, the request should have been redirected instead of upgraded.
browser.test.assertTrue(details.url.includes("file_sample"), "request was redirected");
browser.test.sendMessage("finished");
}, {urls: ["*://mochi.test/tests/*"]});
browser.webRequest.onBeforeRequest.addListener((details) => {
if (details.url.includes("file_mixed.html")) {
let redirectUrl = new URL("file_sample.html", details.url).href;
return {upgradeToSecure: true, redirectUrl};
}
}, {urls: ["*://mochi.test/tests/*"]}, ["blocking"]);
},
});
await extension.startup();
await extension.awaitMessage("finished");
win.close();
await extension.unload();
});
// Test that there is no infinite redirect loop when upgradeToSecure is used on
// https. This test checks that the redirect chain is: http -> https -> done.
add_task(async function upgradeToSecure_for_https_is_noop() {
await SpecialPowers.pushPrefEnv({
set: [["dom.security.https_first", false]],
});
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: [
"webRequest",
"webRequestBlocking",
"*://example.com/tests/*",
],
},
background() {
let count = 0;
browser.webRequest.onBeforeRequest.addListener(
details => {
browser.test.log(`onBeforeRequest ${details.requestId} ${details.url}`);
++count;
if (details.url.startsWith("http:")) {
browser.test.assertEq(1, count, "Initial request is http:");
} else {
browser.test.assertEq(2, count, "Second request is https:");
}
return {upgradeToSecure: true};
},
{ urls: ["*://example.com/tests/*file_sample.html"] },
["blocking"]
);
browser.webRequest.onCompleted.addListener(
details => {
browser.test.log(`onCompleted ${details.requestId} ${details.url}`);
browser.test.assertTrue(details.url.startsWith("https"), "is https");
browser.test.assertEq(2, count, "Seen two requests (http + https)");
browser.test.sendMessage("finished");
},
{ urls: ["*://example.com/tests/*file_sample.html"] },
);
},
});
await extension.startup();
// eslint-disable-next-line @microsoft/sdl/no-insecure-url
await extension.awaitMessage("finished");
win.close();
await extension.unload();
});
</script>
</body>
</html>