browser/base/content/test/general/browser_getshortcutoruri.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 function getPostDataString(aIS) {
     2   if (!aIS)
     3     return null;
     5   var sis = Cc["@mozilla.org/scriptableinputstream;1"].
     6             createInstance(Ci.nsIScriptableInputStream);
     7   sis.init(aIS);
     8   var dataLines = sis.read(aIS.available()).split("\n");
    10   // only want the last line
    11   return dataLines[dataLines.length-1];
    12 }
    14 function keywordResult(aURL, aPostData, aIsUnsafe) {
    15   this.url = aURL;
    16   this.postData = aPostData;
    17   this.isUnsafe = aIsUnsafe;
    18 }
    20 function keyWordData() {}
    21 keyWordData.prototype = {
    22   init: function(aKeyWord, aURL, aPostData, aSearchWord) {
    23     this.keyword = aKeyWord;
    24     this.uri = makeURI(aURL);
    25     this.postData = aPostData;
    26     this.searchWord = aSearchWord;
    28     this.method = (this.postData ? "POST" : "GET");
    29   }
    30 }
    32 function bmKeywordData(aKeyWord, aURL, aPostData, aSearchWord) {
    33   this.init(aKeyWord, aURL, aPostData, aSearchWord);
    34 }
    35 bmKeywordData.prototype = new keyWordData();
    37 function searchKeywordData(aKeyWord, aURL, aPostData, aSearchWord) {
    38   this.init(aKeyWord, aURL, aPostData, aSearchWord);
    39 }
    40 searchKeywordData.prototype = new keyWordData();
    42 var testData = [
    43   [new bmKeywordData("bmget", "http://bmget/search=%s", null, "foo"),
    44    new keywordResult("http://bmget/search=foo", null)],
    46   [new bmKeywordData("bmpost", "http://bmpost/", "search=%s", "foo2"),
    47    new keywordResult("http://bmpost/", "search=foo2")],
    49   [new bmKeywordData("bmpostget", "http://bmpostget/search1=%s", "search2=%s", "foo3"),
    50    new keywordResult("http://bmpostget/search1=foo3", "search2=foo3")],
    52   [new bmKeywordData("bmget-nosearch", "http://bmget-nosearch/", null, ""),
    53    new keywordResult("http://bmget-nosearch/", null)],
    55   [new searchKeywordData("searchget", "http://searchget/?search={searchTerms}", null, "foo4"),
    56    new keywordResult("http://searchget/?search=foo4", null, true)],
    58   [new searchKeywordData("searchpost", "http://searchpost/", "search={searchTerms}", "foo5"),
    59    new keywordResult("http://searchpost/", "search=foo5", true)],
    61   [new searchKeywordData("searchpostget", "http://searchpostget/?search1={searchTerms}", "search2={searchTerms}", "foo6"),
    62    new keywordResult("http://searchpostget/?search1=foo6", "search2=foo6", true)],
    64   // Bookmark keywords that don't take parameters should not be activated if a
    65   // parameter is passed (bug 420328).
    66   [new bmKeywordData("bmget-noparam", "http://bmget-noparam/", null, "foo7"),
    67    new keywordResult(null, null, true)],
    68   [new bmKeywordData("bmpost-noparam", "http://bmpost-noparam/", "not_a=param", "foo8"),
    69    new keywordResult(null, null, true)],
    71   // Test escaping (%s = escaped, %S = raw)
    72   // UTF-8 default
    73   [new bmKeywordData("bmget-escaping", "http://bmget/?esc=%s&raw=%S", null, "foé"),
    74    new keywordResult("http://bmget/?esc=fo%C3%A9&raw=foé", null)],
    75   // Explicitly-defined ISO-8859-1
    76   [new bmKeywordData("bmget-escaping2", "http://bmget/?esc=%s&raw=%S&mozcharset=ISO-8859-1", null, "foé"),
    77    new keywordResult("http://bmget/?esc=fo%E9&raw=foé", null)],
    79   // Bug 359809: Test escaping +, /, and @
    80   // UTF-8 default
    81   [new bmKeywordData("bmget-escaping", "http://bmget/?esc=%s&raw=%S", null, "+/@"),
    82    new keywordResult("http://bmget/?esc=%2B%2F%40&raw=+/@", null)],
    83   // Explicitly-defined ISO-8859-1
    84   [new bmKeywordData("bmget-escaping2", "http://bmget/?esc=%s&raw=%S&mozcharset=ISO-8859-1", null, "+/@"),
    85    new keywordResult("http://bmget/?esc=%2B%2F%40&raw=+/@", null)],
    87   // Test using a non-bmKeywordData object, to test the behavior of
    88   // getShortcutOrURIAndPostData for non-keywords (setupKeywords only adds keywords for
    89   // bmKeywordData objects)
    90   [{keyword: "http://gavinsharp.com"},
    91    new keywordResult(null, null, true)]
    92 ];
    94 function test() {
    95   waitForExplicitFinish();
    97   setupKeywords();
    99   Task.spawn(function() {
   100     for each (var item in testData) {
   101       let [data, result] = item;
   103       let query = data.keyword;
   104       if (data.searchWord)
   105         query += " " + data.searchWord;
   106       let returnedData = yield new Promise(
   107         resolve => getShortcutOrURIAndPostData(query, resolve));
   108       // null result.url means we should expect the same query we sent in
   109       let expected = result.url || query;
   110       is(returnedData.url, expected, "got correct URL for " + data.keyword);
   111       is(getPostDataString(returnedData.postData), result.postData, "got correct postData for " + data.keyword);
   112       is(returnedData.mayInheritPrincipal, !result.isUnsafe, "got correct mayInheritPrincipal for " + data.keyword);
   113     }
   114     cleanupKeywords();
   115   }).then(finish);
   116 }
   118 var gBMFolder = null;
   119 var gAddedEngines = [];
   120 function setupKeywords() {
   121   gBMFolder = Application.bookmarks.menu.addFolder("keyword-test");
   122   for each (var item in testData) {
   123     var data = item[0];
   124     if (data instanceof bmKeywordData) {
   125       var bm = gBMFolder.addBookmark(data.keyword, data.uri);
   126       bm.keyword = data.keyword;
   127       if (data.postData)
   128         bm.annotations.set("bookmarkProperties/POSTData", data.postData, Ci.nsIAnnotationService.EXPIRE_SESSION);
   129     }
   131     if (data instanceof searchKeywordData) {
   132       Services.search.addEngineWithDetails(data.keyword, "", data.keyword, "", data.method, data.uri.spec);
   133       var addedEngine = Services.search.getEngineByName(data.keyword);
   134       if (data.postData) {
   135         var [paramName, paramValue] = data.postData.split("=");
   136         addedEngine.addParam(paramName, paramValue, null);
   137       }
   139       gAddedEngines.push(addedEngine);
   140     }
   141   }
   142 }
   144 function cleanupKeywords() {
   145   gBMFolder.remove();
   146   gAddedEngines.map(Services.search.removeEngine);
   147 }

mercurial