test_URIFixup_forced.js (4395B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 var data = [ 8 { 9 // singleword. 10 wrong: "http://example/", 11 fixed: "https://www.example.com/", 12 }, 13 { 14 // upgrade protocol. 15 wrong: "http://www.example.com/", 16 fixed: "https://www.example.com/", 17 noAlternateURI: true, 18 }, 19 { 20 // no difference. 21 wrong: "https://www.example.com/", 22 fixed: "https://www.example.com/", 23 noProtocolFixup: true, 24 noAlternateURI: true, 25 }, 26 { 27 // don't add prefix when there's more than one dot. 28 wrong: "http://www.example.abc.def/", 29 fixed: "https://www.example.abc.def/", 30 noAlternateURI: true, 31 }, 32 { 33 // http -> https. 34 wrong: "http://www.example/", 35 fixed: "https://www.example/", 36 noAlternateURI: true, 37 }, 38 { 39 // domain.com -> https://www.domain.com. 40 wrong: "http://example.com/", 41 fixed: "https://www.example.com/", 42 }, 43 { 44 // example/example... -> https://www.example.com/example/ 45 wrong: "http://example/example/", 46 fixed: "https://www.example.com/example/", 47 }, 48 { 49 // example/example/s#q -> www.example.com/example/s#q. 50 wrong: "http://example/example/s#q", 51 fixed: "https://www.example.com/example/s#q", 52 }, 53 { 54 wrong: "http://モジラ.org", 55 fixed: "https://www.xn--yck6dwa.org/", 56 }, 57 { 58 wrong: "http://モジラ", 59 fixed: "https://www.xn--yck6dwa.com/", 60 }, 61 { 62 wrong: "http://xn--yck6dwa", 63 fixed: "https://www.xn--yck6dwa.com/", 64 }, 65 { 66 wrong: "https://モジラ.org", 67 fixed: "https://www.xn--yck6dwa.org/", 68 noProtocolFixup: true, 69 }, 70 { 71 wrong: "https://モジラ", 72 fixed: "https://www.xn--yck6dwa.com/", 73 noProtocolFixup: true, 74 }, 75 { 76 wrong: "https://xn--yck6dwa", 77 fixed: "https://www.xn--yck6dwa.com/", 78 noProtocolFixup: true, 79 }, 80 { 81 // Host is https -> fixup typos of protocol 82 wrong: "htp://https://mozilla.org", 83 fixed: "http://https//mozilla.org", 84 noAlternateURI: true, 85 }, 86 { 87 // Host is http -> fixup typos of protocol 88 wrong: "ttp://http://mozilla.org", 89 fixed: "http://http//mozilla.org", 90 noAlternateURI: true, 91 }, 92 { 93 // Host is localhost -> fixup typos of protocol 94 wrong: "htps://localhost://mozilla.org", 95 fixed: "https://localhost//mozilla.org", 96 noAlternateURI: true, 97 }, 98 { 99 // view-source is not http/https -> error 100 wrong: "view-source:http://example/example/example/example", 101 reject: true, 102 comment: "Scheme should be either http or https", 103 }, 104 { 105 // file is not http/https -> error 106 wrong: "file://http://example/example/example/example", 107 reject: true, 108 comment: "Scheme should be either http or https", 109 }, 110 { 111 // Protocol is missing -> error 112 wrong: "example.com", 113 reject: true, 114 comment: "Scheme should be either http or https", 115 }, 116 { 117 // Empty input -> error 118 wrong: "", 119 reject: true, 120 comment: "Should pass a non-null uri", 121 }, 122 ]; 123 124 add_task(async function setup() { 125 Services.prefs.setStringPref("browser.fixup.alternate.prefix", "www."); 126 Services.prefs.setStringPref("browser.fixup.alternate.suffix", ".com"); 127 Services.prefs.setStringPref("browser.fixup.alternate.protocol", "https"); 128 registerCleanupFunction(function () { 129 Services.prefs.clearUserPref("browser.fixup.alternate.prefix"); 130 Services.prefs.clearUserPref("browser.fixup.alternate.suffix"); 131 Services.prefs.clearUserPref("browser.fixup.alternate.protocol"); 132 }); 133 }); 134 135 add_task(function test_default_https_pref() { 136 for (let item of data) { 137 if (item.reject) { 138 Assert.throws( 139 () => Services.uriFixup.forceHttpFixup(item.wrong), 140 /NS_ERROR_FAILURE/, 141 item.comment 142 ); 143 } else { 144 let { fixupChangedProtocol, fixupCreatedAlternateURI, fixedURI } = 145 Services.uriFixup.forceHttpFixup(item.wrong); 146 Assert.equal(fixedURI.spec, item.fixed, "Specs should be the same"); 147 Assert.equal( 148 fixupChangedProtocol, 149 !item.noProtocolFixup, 150 `fixupChangedProtocol should be ${!item.noAlternateURI}` 151 ); 152 Assert.equal( 153 fixupCreatedAlternateURI, 154 !item.noAlternateURI, 155 `fixupCreatedAlternateURI should be ${!item.limitedFixup}` 156 ); 157 } 158 } 159 });