browser_httpsfirst_console_logging.js (2246B)
1 // Bug 1658924 - HTTPS-First Mode - Tests for console logging 2 // https://bugzilla.mozilla.org/show_bug.cgi?id=1658924 3 // This test makes sure that the various console messages from the HTTPS-First 4 // mode get logged to the console. 5 "use strict"; 6 7 // Test Cases 8 // description: Description of what the subtests expects. 9 // expectLogLevel: Expected log-level of a message. 10 // expectIncludes: Expected substrings the message should contain. 11 let tests = [ 12 { 13 description: "Top-Level upgrade should get logged", 14 expectLogLevel: Ci.nsIConsoleMessage.warn, 15 expectIncludes: ["Upgrading insecure request", "to use", "httpsfirst.com"], 16 }, 17 { 18 description: "Top-Level upgrade failure should get logged", 19 expectLogLevel: Ci.nsIConsoleMessage.warn, 20 expectIncludes: [ 21 "Upgrading insecure request", 22 "failed", 23 "httpsfirst.com", 24 "Downgrading to", 25 ], 26 }, 27 ]; 28 29 add_task(async function () { 30 // A longer timeout is necessary for this test than the plain mochitests 31 // due to opening a new tab with the web console. 32 requestLongerTimeout(4); 33 34 // Enable HTTPS-First Mode and register console-listener 35 await SpecialPowers.pushPrefEnv({ 36 set: [["dom.security.https_first", true]], 37 }); 38 Services.console.registerListener(on_new_message); 39 // 1. Upgrade page to https:// 40 let promiseLoaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser); 41 BrowserTestUtils.startLoadingURIString( 42 gBrowser.selectedBrowser, 43 "http://httpsfirst.com" 44 ); 45 await promiseLoaded; 46 await BrowserTestUtils.waitForCondition(() => tests.length === 0); 47 48 // Clean up 49 Services.console.unregisterListener(on_new_message); 50 }); 51 52 function on_new_message(msgObj) { 53 const message = msgObj.message; 54 const logLevel = msgObj.logLevel; 55 56 if (message.includes("HTTPS-First Mode:")) { 57 for (let i = 0; i < tests.length; i++) { 58 const testCase = tests[i]; 59 // Check if log-level matches 60 if (logLevel !== testCase.expectLogLevel) { 61 continue; 62 } 63 // Check if all substrings are included 64 if (testCase.expectIncludes.some(str => !message.includes(str))) { 65 continue; 66 } 67 ok(true, testCase.description); 68 tests.splice(i, 1); 69 break; 70 } 71 } 72 }