1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/general/browser_getshortcutoruri.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,147 @@ 1.4 +function getPostDataString(aIS) { 1.5 + if (!aIS) 1.6 + return null; 1.7 + 1.8 + var sis = Cc["@mozilla.org/scriptableinputstream;1"]. 1.9 + createInstance(Ci.nsIScriptableInputStream); 1.10 + sis.init(aIS); 1.11 + var dataLines = sis.read(aIS.available()).split("\n"); 1.12 + 1.13 + // only want the last line 1.14 + return dataLines[dataLines.length-1]; 1.15 +} 1.16 + 1.17 +function keywordResult(aURL, aPostData, aIsUnsafe) { 1.18 + this.url = aURL; 1.19 + this.postData = aPostData; 1.20 + this.isUnsafe = aIsUnsafe; 1.21 +} 1.22 + 1.23 +function keyWordData() {} 1.24 +keyWordData.prototype = { 1.25 + init: function(aKeyWord, aURL, aPostData, aSearchWord) { 1.26 + this.keyword = aKeyWord; 1.27 + this.uri = makeURI(aURL); 1.28 + this.postData = aPostData; 1.29 + this.searchWord = aSearchWord; 1.30 + 1.31 + this.method = (this.postData ? "POST" : "GET"); 1.32 + } 1.33 +} 1.34 + 1.35 +function bmKeywordData(aKeyWord, aURL, aPostData, aSearchWord) { 1.36 + this.init(aKeyWord, aURL, aPostData, aSearchWord); 1.37 +} 1.38 +bmKeywordData.prototype = new keyWordData(); 1.39 + 1.40 +function searchKeywordData(aKeyWord, aURL, aPostData, aSearchWord) { 1.41 + this.init(aKeyWord, aURL, aPostData, aSearchWord); 1.42 +} 1.43 +searchKeywordData.prototype = new keyWordData(); 1.44 + 1.45 +var testData = [ 1.46 + [new bmKeywordData("bmget", "http://bmget/search=%s", null, "foo"), 1.47 + new keywordResult("http://bmget/search=foo", null)], 1.48 + 1.49 + [new bmKeywordData("bmpost", "http://bmpost/", "search=%s", "foo2"), 1.50 + new keywordResult("http://bmpost/", "search=foo2")], 1.51 + 1.52 + [new bmKeywordData("bmpostget", "http://bmpostget/search1=%s", "search2=%s", "foo3"), 1.53 + new keywordResult("http://bmpostget/search1=foo3", "search2=foo3")], 1.54 + 1.55 + [new bmKeywordData("bmget-nosearch", "http://bmget-nosearch/", null, ""), 1.56 + new keywordResult("http://bmget-nosearch/", null)], 1.57 + 1.58 + [new searchKeywordData("searchget", "http://searchget/?search={searchTerms}", null, "foo4"), 1.59 + new keywordResult("http://searchget/?search=foo4", null, true)], 1.60 + 1.61 + [new searchKeywordData("searchpost", "http://searchpost/", "search={searchTerms}", "foo5"), 1.62 + new keywordResult("http://searchpost/", "search=foo5", true)], 1.63 + 1.64 + [new searchKeywordData("searchpostget", "http://searchpostget/?search1={searchTerms}", "search2={searchTerms}", "foo6"), 1.65 + new keywordResult("http://searchpostget/?search1=foo6", "search2=foo6", true)], 1.66 + 1.67 + // Bookmark keywords that don't take parameters should not be activated if a 1.68 + // parameter is passed (bug 420328). 1.69 + [new bmKeywordData("bmget-noparam", "http://bmget-noparam/", null, "foo7"), 1.70 + new keywordResult(null, null, true)], 1.71 + [new bmKeywordData("bmpost-noparam", "http://bmpost-noparam/", "not_a=param", "foo8"), 1.72 + new keywordResult(null, null, true)], 1.73 + 1.74 + // Test escaping (%s = escaped, %S = raw) 1.75 + // UTF-8 default 1.76 + [new bmKeywordData("bmget-escaping", "http://bmget/?esc=%s&raw=%S", null, "foé"), 1.77 + new keywordResult("http://bmget/?esc=fo%C3%A9&raw=foé", null)], 1.78 + // Explicitly-defined ISO-8859-1 1.79 + [new bmKeywordData("bmget-escaping2", "http://bmget/?esc=%s&raw=%S&mozcharset=ISO-8859-1", null, "foé"), 1.80 + new keywordResult("http://bmget/?esc=fo%E9&raw=foé", null)], 1.81 + 1.82 + // Bug 359809: Test escaping +, /, and @ 1.83 + // UTF-8 default 1.84 + [new bmKeywordData("bmget-escaping", "http://bmget/?esc=%s&raw=%S", null, "+/@"), 1.85 + new keywordResult("http://bmget/?esc=%2B%2F%40&raw=+/@", null)], 1.86 + // Explicitly-defined ISO-8859-1 1.87 + [new bmKeywordData("bmget-escaping2", "http://bmget/?esc=%s&raw=%S&mozcharset=ISO-8859-1", null, "+/@"), 1.88 + new keywordResult("http://bmget/?esc=%2B%2F%40&raw=+/@", null)], 1.89 + 1.90 + // Test using a non-bmKeywordData object, to test the behavior of 1.91 + // getShortcutOrURIAndPostData for non-keywords (setupKeywords only adds keywords for 1.92 + // bmKeywordData objects) 1.93 + [{keyword: "http://gavinsharp.com"}, 1.94 + new keywordResult(null, null, true)] 1.95 +]; 1.96 + 1.97 +function test() { 1.98 + waitForExplicitFinish(); 1.99 + 1.100 + setupKeywords(); 1.101 + 1.102 + Task.spawn(function() { 1.103 + for each (var item in testData) { 1.104 + let [data, result] = item; 1.105 + 1.106 + let query = data.keyword; 1.107 + if (data.searchWord) 1.108 + query += " " + data.searchWord; 1.109 + let returnedData = yield new Promise( 1.110 + resolve => getShortcutOrURIAndPostData(query, resolve)); 1.111 + // null result.url means we should expect the same query we sent in 1.112 + let expected = result.url || query; 1.113 + is(returnedData.url, expected, "got correct URL for " + data.keyword); 1.114 + is(getPostDataString(returnedData.postData), result.postData, "got correct postData for " + data.keyword); 1.115 + is(returnedData.mayInheritPrincipal, !result.isUnsafe, "got correct mayInheritPrincipal for " + data.keyword); 1.116 + } 1.117 + cleanupKeywords(); 1.118 + }).then(finish); 1.119 +} 1.120 + 1.121 +var gBMFolder = null; 1.122 +var gAddedEngines = []; 1.123 +function setupKeywords() { 1.124 + gBMFolder = Application.bookmarks.menu.addFolder("keyword-test"); 1.125 + for each (var item in testData) { 1.126 + var data = item[0]; 1.127 + if (data instanceof bmKeywordData) { 1.128 + var bm = gBMFolder.addBookmark(data.keyword, data.uri); 1.129 + bm.keyword = data.keyword; 1.130 + if (data.postData) 1.131 + bm.annotations.set("bookmarkProperties/POSTData", data.postData, Ci.nsIAnnotationService.EXPIRE_SESSION); 1.132 + } 1.133 + 1.134 + if (data instanceof searchKeywordData) { 1.135 + Services.search.addEngineWithDetails(data.keyword, "", data.keyword, "", data.method, data.uri.spec); 1.136 + var addedEngine = Services.search.getEngineByName(data.keyword); 1.137 + if (data.postData) { 1.138 + var [paramName, paramValue] = data.postData.split("="); 1.139 + addedEngine.addParam(paramName, paramValue, null); 1.140 + } 1.141 + 1.142 + gAddedEngines.push(addedEngine); 1.143 + } 1.144 + } 1.145 +} 1.146 + 1.147 +function cleanupKeywords() { 1.148 + gBMFolder.remove(); 1.149 + gAddedEngines.map(Services.search.removeEngine); 1.150 +}