test_UrlbarUtils_getShortcutOrURIAndPostData.js (6231B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /** 5 * These tests unit test the functionality of UrlbarController by stubbing out the 6 * model and providing stubs to be called. 7 */ 8 9 "use strict"; 10 11 function getPostDataString(aIS) { 12 if (!aIS) { 13 return null; 14 } 15 16 let sis = Cc["@mozilla.org/scriptableinputstream;1"].createInstance( 17 Ci.nsIScriptableInputStream 18 ); 19 sis.init(aIS); 20 let dataLines = sis.read(aIS.available()).split("\n"); 21 22 // only want the last line 23 return dataLines[dataLines.length - 1]; 24 } 25 26 function keywordResult(aURL, aPostData, aIsUnsafe) { 27 this.url = aURL; 28 this.postData = aPostData; 29 this.isUnsafe = aIsUnsafe; 30 } 31 32 function keyWordData() {} 33 keyWordData.prototype = { 34 init(aKeyWord, aURL, aPostData, aSearchWord) { 35 this.keyword = aKeyWord; 36 this.uri = Services.io.newURI(aURL); 37 this.postData = aPostData; 38 this.searchWord = aSearchWord; 39 40 this.method = this.postData ? "POST" : "GET"; 41 }, 42 }; 43 44 function bmKeywordData(aKeyWord, aURL, aPostData, aSearchWord) { 45 this.init(aKeyWord, aURL, aPostData, aSearchWord); 46 } 47 bmKeywordData.prototype = new keyWordData(); 48 49 function searchKeywordData(aKeyWord, aURL, aPostData, aSearchWord) { 50 this.init(aKeyWord, aURL, aPostData, aSearchWord); 51 } 52 searchKeywordData.prototype = new keyWordData(); 53 54 var testData = [ 55 [ 56 new bmKeywordData("bmget", "https://bmget/search=%s", null, "foo"), 57 new keywordResult("https://bmget/search=foo", null), 58 ], 59 60 [ 61 new bmKeywordData("bmpost", "https://bmpost/", "search=%s", "foo2"), 62 new keywordResult("https://bmpost/", "search=foo2"), 63 ], 64 65 [ 66 new bmKeywordData( 67 "bmpostget", 68 "https://bmpostget/search1=%s", 69 "search2=%s", 70 "foo3" 71 ), 72 new keywordResult("https://bmpostget/search1=foo3", "search2=foo3"), 73 ], 74 75 [ 76 new bmKeywordData("bmget-nosearch", "https://bmget-nosearch/", null, ""), 77 new keywordResult("https://bmget-nosearch/", null), 78 ], 79 80 [ 81 new searchKeywordData( 82 "searchget", 83 "https://searchget/?search={searchTerms}", 84 null, 85 "foo4" 86 ), 87 new keywordResult("https://searchget/?search=foo4", null, true), 88 ], 89 90 [ 91 new searchKeywordData( 92 "searchpost", 93 "https://searchpost/", 94 "search={searchTerms}", 95 "foo5" 96 ), 97 new keywordResult("https://searchpost/", "search=foo5", true), 98 ], 99 100 [ 101 new searchKeywordData( 102 "searchpostget", 103 "https://searchpostget/?search1={searchTerms}", 104 "search2={searchTerms}", 105 "foo6" 106 ), 107 new keywordResult( 108 "https://searchpostget/?search1=foo6", 109 "search2=foo6", 110 true 111 ), 112 ], 113 114 // Bookmark keywords that don't take parameters should not be activated if a 115 // parameter is passed (bug 420328). 116 [ 117 new bmKeywordData("bmget-noparam", "https://bmget-noparam/", null, "foo7"), 118 new keywordResult(null, null, true), 119 ], 120 [ 121 new bmKeywordData( 122 "bmpost-noparam", 123 "https://bmpost-noparam/", 124 "not_a=param", 125 "foo8" 126 ), 127 new keywordResult(null, null, true), 128 ], 129 130 // Test escaping (%s = escaped, %S = raw) 131 // UTF-8 default 132 [ 133 new bmKeywordData( 134 "bmget-escaping", 135 "https://bmget/?esc=%s&raw=%S", 136 null, 137 "fo\xE9" 138 ), 139 new keywordResult("https://bmget/?esc=fo%C3%A9&raw=fo\xE9", null), 140 ], 141 // Explicitly-defined ISO-8859-1 142 [ 143 new bmKeywordData( 144 "bmget-escaping2", 145 "https://bmget/?esc=%s&raw=%S&mozcharset=ISO-8859-1", 146 null, 147 "fo\xE9" 148 ), 149 new keywordResult("https://bmget/?esc=fo%E9&raw=fo\xE9", null), 150 ], 151 152 // Bug 359809: Test escaping +, /, and @ 153 // UTF-8 default 154 [ 155 new bmKeywordData( 156 "bmget-escaping", 157 "https://bmget/?esc=%s&raw=%S", 158 null, 159 "+/@" 160 ), 161 new keywordResult("https://bmget/?esc=%2B%2F%40&raw=+/@", null), 162 ], 163 // Explicitly-defined ISO-8859-1 164 [ 165 new bmKeywordData( 166 "bmget-escaping2", 167 "https://bmget/?esc=%s&raw=%S&mozcharset=ISO-8859-1", 168 null, 169 "+/@" 170 ), 171 new keywordResult("https://bmget/?esc=%2B%2F%40&raw=+/@", null), 172 ], 173 174 // Test using a non-bmKeywordData object, to test the behavior of 175 // getShortcutOrURIAndPostData for non-keywords (setupKeywords only adds keywords for 176 // bmKeywordData objects) 177 [{ keyword: "https://gavinsharp.com" }, new keywordResult(null, null, true)], 178 ]; 179 180 add_task(async function test_getshortcutoruri() { 181 await setupKeywords(); 182 183 for (let item of testData) { 184 let [data, result] = item; 185 186 let query = data.keyword; 187 if (data.searchWord) { 188 query += " " + data.searchWord; 189 } 190 let returnedData = await UrlbarUtils.getShortcutOrURIAndPostData(query); 191 // null result.url means we should expect the same query we sent in 192 let expected = result.url || query; 193 Assert.equal( 194 returnedData.url, 195 expected, 196 "got correct URL for " + data.keyword 197 ); 198 Assert.equal( 199 getPostDataString(returnedData.postData), 200 result.postData, 201 "got correct postData for " + data.keyword 202 ); 203 Assert.equal( 204 returnedData.mayInheritPrincipal, 205 !result.isUnsafe, 206 "got correct mayInheritPrincipal for " + data.keyword 207 ); 208 } 209 210 await cleanupKeywords(); 211 }); 212 213 var folder = null; 214 215 async function setupKeywords() { 216 folder = await PlacesUtils.bookmarks.insert({ 217 parentGuid: PlacesUtils.bookmarks.unfiledGuid, 218 type: PlacesUtils.bookmarks.TYPE_FOLDER, 219 title: "keyword-test", 220 }); 221 for (let item of testData) { 222 let data = item[0]; 223 if (data instanceof bmKeywordData) { 224 await PlacesUtils.bookmarks.insert({ 225 url: data.uri, 226 parentGuid: folder.guid, 227 }); 228 await PlacesUtils.keywords.insert({ 229 keyword: data.keyword, 230 url: data.uri.spec, 231 postData: data.postData, 232 }); 233 } 234 235 if (data instanceof searchKeywordData) { 236 await SearchTestUtils.installSearchExtension({ 237 name: data.keyword, 238 keyword: data.keyword, 239 search_url: data.uri.spec, 240 search_url_get_params: "", 241 search_url_post_params: data.postData, 242 }); 243 } 244 } 245 } 246 247 async function cleanupKeywords() { 248 await PlacesUtils.bookmarks.remove(folder); 249 }