summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/unit/test_UrlbarUtils_getShortcutOrURIAndPostData.js
blob: 034005b0faed9c1eec1b6b141752f9315bfedd7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* Any copyright is dedicated to the Public Domain.
   https://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * These tests unit test the functionality of UrlbarController by stubbing out the
 * model and providing stubs to be called.
 */

"use strict";

function getPostDataString(aIS) {
  if (!aIS) {
    return null;
  }

  let sis = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
    Ci.nsIScriptableInputStream
  );
  sis.init(aIS);
  let dataLines = sis.read(aIS.available()).split("\n");

  // only want the last line
  return dataLines[dataLines.length - 1];
}

function keywordResult(aURL, aPostData, aIsUnsafe) {
  this.url = aURL;
  this.postData = aPostData;
  this.isUnsafe = aIsUnsafe;
}

function keyWordData() {}
keyWordData.prototype = {
  init(aKeyWord, aURL, aPostData, aSearchWord) {
    this.keyword = aKeyWord;
    this.uri = Services.io.newURI(aURL);
    this.postData = aPostData;
    this.searchWord = aSearchWord;

    this.method = this.postData ? "POST" : "GET";
  },
};

function bmKeywordData(aKeyWord, aURL, aPostData, aSearchWord) {
  this.init(aKeyWord, aURL, aPostData, aSearchWord);
}
bmKeywordData.prototype = new keyWordData();

function searchKeywordData(aKeyWord, aURL, aPostData, aSearchWord) {
  this.init(aKeyWord, aURL, aPostData, aSearchWord);
}
searchKeywordData.prototype = new keyWordData();

var testData = [
  [
    new bmKeywordData("bmget", "https://bmget/search=%s", null, "foo"),
    new keywordResult("https://bmget/search=foo", null),
  ],

  [
    new bmKeywordData("bmpost", "https://bmpost/", "search=%s", "foo2"),
    new keywordResult("https://bmpost/", "search=foo2"),
  ],

  [
    new bmKeywordData(
      "bmpostget",
      "https://bmpostget/search1=%s",
      "search2=%s",
      "foo3"
    ),
    new keywordResult("https://bmpostget/search1=foo3", "search2=foo3"),
  ],

  [
    new bmKeywordData("bmget-nosearch", "https://bmget-nosearch/", null, ""),
    new keywordResult("https://bmget-nosearch/", null),
  ],

  [
    new searchKeywordData(
      "searchget",
      "https://searchget/?search={searchTerms}",
      null,
      "foo4"
    ),
    new keywordResult("https://searchget/?search=foo4", null, true),
  ],

  [
    new searchKeywordData(
      "searchpost",
      "https://searchpost/",
      "search={searchTerms}",
      "foo5"
    ),
    new keywordResult("https://searchpost/", "search=foo5", true),
  ],

  [
    new searchKeywordData(
      "searchpostget",
      "https://searchpostget/?search1={searchTerms}",
      "search2={searchTerms}",
      "foo6"
    ),
    new keywordResult(
      "https://searchpostget/?search1=foo6",
      "search2=foo6",
      true
    ),
  ],

  // Bookmark keywords that don't take parameters should not be activated if a
  // parameter is passed (bug 420328).
  [
    new bmKeywordData("bmget-noparam", "https://bmget-noparam/", null, "foo7"),
    new keywordResult(null, null, true),
  ],
  [
    new bmKeywordData(
      "bmpost-noparam",
      "https://bmpost-noparam/",
      "not_a=param",
      "foo8"
    ),
    new keywordResult(null, null, true),
  ],

  // Test escaping (%s = escaped, %S = raw)
  // UTF-8 default
  [
    new bmKeywordData(
      "bmget-escaping",
      "https://bmget/?esc=%s&raw=%S",
      null,
      "fo\xE9"
    ),
    new keywordResult("https://bmget/?esc=fo%C3%A9&raw=fo\xE9", null),
  ],
  // Explicitly-defined ISO-8859-1
  [
    new bmKeywordData(
      "bmget-escaping2",
      "https://bmget/?esc=%s&raw=%S&mozcharset=ISO-8859-1",
      null,
      "fo\xE9"
    ),
    new keywordResult("https://bmget/?esc=fo%E9&raw=fo\xE9", null),
  ],

  // Bug 359809: Test escaping +, /, and @
  // UTF-8 default
  [
    new bmKeywordData(
      "bmget-escaping",
      "https://bmget/?esc=%s&raw=%S",
      null,
      "+/@"
    ),
    new keywordResult("https://bmget/?esc=%2B%2F%40&raw=+/@", null),
  ],
  // Explicitly-defined ISO-8859-1
  [
    new bmKeywordData(
      "bmget-escaping2",
      "https://bmget/?esc=%s&raw=%S&mozcharset=ISO-8859-1",
      null,
      "+/@"
    ),
    new keywordResult("https://bmget/?esc=%2B%2F%40&raw=+/@", null),
  ],

  // Test using a non-bmKeywordData object, to test the behavior of
  // getShortcutOrURIAndPostData for non-keywords (setupKeywords only adds keywords for
  // bmKeywordData objects)
  [{ keyword: "https://gavinsharp.com" }, new keywordResult(null, null, true)],
];

add_task(async function test_getshortcutoruri() {
  await setupKeywords();

  for (let item of testData) {
    let [data, result] = item;

    let query = data.keyword;
    if (data.searchWord) {
      query += " " + data.searchWord;
    }
    let returnedData = await UrlbarUtils.getShortcutOrURIAndPostData(query);
    // null result.url means we should expect the same query we sent in
    let expected = result.url || query;
    Assert.equal(
      returnedData.url,
      expected,
      "got correct URL for " + data.keyword
    );
    Assert.equal(
      getPostDataString(returnedData.postData),
      result.postData,
      "got correct postData for " + data.keyword
    );
    Assert.equal(
      returnedData.mayInheritPrincipal,
      !result.isUnsafe,
      "got correct mayInheritPrincipal for " + data.keyword
    );
  }

  await cleanupKeywords();
});

var folder = null;

async function setupKeywords() {
  folder = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
    title: "keyword-test",
  });
  for (let item of testData) {
    let data = item[0];
    if (data instanceof bmKeywordData) {
      await PlacesUtils.bookmarks.insert({
        url: data.uri,
        parentGuid: folder.guid,
      });
      await PlacesUtils.keywords.insert({
        keyword: data.keyword,
        url: data.uri.spec,
        postData: data.postData,
      });
    }

    if (data instanceof searchKeywordData) {
      await SearchTestUtils.installSearchExtension({
        name: data.keyword,
        keyword: data.keyword,
        search_url: data.uri.spec,
        search_url_get_params: "",
        search_url_post_params: data.postData,
      });
    }
  }
}

async function cleanupKeywords() {
  await PlacesUtils.bookmarks.remove(folder);
}