summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/test_parseSubmissionURL.js
blob: d6e21fc61b01f962ac1cb63a5982220b5256bd72 (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
/* Any copyright is dedicated to the Public Domain.
 *    http://creativecommons.org/publicdomain/zero/1.0/ */

/*
 * Tests getAlternateDomains API.
 */

"use strict";

function run_test() {
  removeMetadata();
  updateAppInfo();
  useHttpServer();

  run_next_test();
}

add_task(function* test_parseSubmissionURL() {
  // Hide the default engines to prevent them from being used in the search.
  for (let engine of Services.search.getEngines()) {
    Services.search.removeEngine(engine);
  }

  let [engine1, engine2, engine3, engine4] = yield addTestEngines([
    { name: "Test search engine", xmlFileName: "engine.xml" },
    { name: "Test search engine (fr)", xmlFileName: "engine-fr.xml" },
    { name: "bacon_addParam", details: ["", "bacon_addParam", "Search Bacon",
                                        "GET", "http://www.bacon.test/find"] },
    { name: "idn_addParam", details: ["", "idn_addParam", "Search IDN",
                                        "GET", "http://www.xn--bcher-kva.ch/search"] },
    // The following engines cannot identify the search parameter.
    { name: "A second test engine", xmlFileName: "engine2.xml" },
    { name: "bacon", details: ["", "bacon", "Search Bacon", "GET",
                               "http://www.bacon.moz/search?q={searchTerms}"] },
  ]);

  engine3.addParam("q", "{searchTerms}", null);
  engine4.addParam("q", "{searchTerms}", null);

  // Test the first engine, whose URLs use UTF-8 encoding.
  let url = "http://www.google.com/search?foo=bar&q=caff%C3%A8";
  let result = Services.search.parseSubmissionURL(url);
  do_check_eq(result.engine, engine1);
  do_check_eq(result.terms, "caff\u00E8");
  do_check_true(url.slice(result.termsOffset).startsWith("caff%C3%A8"));
  do_check_eq(result.termsLength, "caff%C3%A8".length);

  // The second engine uses a locale-specific domain that is an alternate domain
  // of the first one, but the second engine should get priority when matching.
  // The URL used with this engine uses ISO-8859-1 encoding instead.
  url = "http://www.google.fr/search?q=caff%E8";
  result = Services.search.parseSubmissionURL(url);
  do_check_eq(result.engine, engine2);
  do_check_eq(result.terms, "caff\u00E8");
  do_check_true(url.slice(result.termsOffset).startsWith("caff%E8"));
  do_check_eq(result.termsLength, "caff%E8".length);

  // Test a domain that is an alternate domain of those defined.  In this case,
  // the first matching engine from the ordered list should be returned.
  url = "http://www.google.co.uk/search?q=caff%C3%A8";
  result = Services.search.parseSubmissionURL(url);
  do_check_eq(result.engine, engine1);
  do_check_eq(result.terms, "caff\u00E8");
  do_check_true(url.slice(result.termsOffset).startsWith("caff%C3%A8"));
  do_check_eq(result.termsLength, "caff%C3%A8".length);

  // We support parsing URLs from a dynamically added engine.  Those engines use
  // windows-1252 encoding by default.
  url = "http://www.bacon.test/find?q=caff%E8";
  result = Services.search.parseSubmissionURL(url);
  do_check_eq(result.engine, engine3);
  do_check_eq(result.terms, "caff\u00E8");
  do_check_true(url.slice(result.termsOffset).startsWith("caff%E8"));
  do_check_eq(result.termsLength, "caff%E8".length);

  // Test URLs with unescaped unicode characters.
  url = "http://www.google.com/search?q=foo+b\u00E4r";
  result = Services.search.parseSubmissionURL(url);
  do_check_eq(result.engine, engine1);
  do_check_eq(result.terms, "foo b\u00E4r");
  do_check_true(url.slice(result.termsOffset).startsWith("foo+b\u00E4r"));
  do_check_eq(result.termsLength, "foo+b\u00E4r".length);

  // Test search engines with unescaped IDNs.
  url = "http://www.b\u00FCcher.ch/search?q=foo+bar";
  result = Services.search.parseSubmissionURL(url);
  do_check_eq(result.engine, engine4);
  do_check_eq(result.terms, "foo bar");
  do_check_true(url.slice(result.termsOffset).startsWith("foo+bar"));
  do_check_eq(result.termsLength, "foo+bar".length);

  // Test search engines with escaped IDNs.
  url = "http://www.xn--bcher-kva.ch/search?q=foo+bar";
  result = Services.search.parseSubmissionURL(url);
  do_check_eq(result.engine, engine4);
  do_check_eq(result.terms, "foo bar");
  do_check_true(url.slice(result.termsOffset).startsWith("foo+bar"));
  do_check_eq(result.termsLength, "foo+bar".length);

  // Parsing of parameters from an engine template URL is not supported.
  do_check_eq(Services.search.parseSubmissionURL(
                              "http://www.bacon.moz/search?q=").engine, null);
  do_check_eq(Services.search.parseSubmissionURL(
                              "https://duckduckgo.com?q=test").engine, null);
  do_check_eq(Services.search.parseSubmissionURL(
                              "https://duckduckgo.com/?q=test").engine, null);

  // HTTP and HTTPS schemes are interchangeable.
  url = "https://www.google.com/search?q=caff%C3%A8";
  result = Services.search.parseSubmissionURL(url);
  do_check_eq(result.engine, engine1);
  do_check_eq(result.terms, "caff\u00E8");
  do_check_true(url.slice(result.termsOffset).startsWith("caff%C3%A8"));

  // Decoding search terms with multiple spaces should work.
  result = Services.search.parseSubmissionURL(
             "http://www.google.com/search?q=+with++spaces+");
  do_check_eq(result.engine, engine1);
  do_check_eq(result.terms, " with  spaces ");

  // An empty query parameter should work the same.
  url = "http://www.google.com/search?q=";
  result = Services.search.parseSubmissionURL(url);
  do_check_eq(result.engine, engine1);
  do_check_eq(result.terms, "");
  do_check_eq(result.termsOffset, url.length);

  // There should be no match when the path is different.
  result = Services.search.parseSubmissionURL(
             "http://www.google.com/search/?q=test");
  do_check_eq(result.engine, null);
  do_check_eq(result.terms, "");
  do_check_eq(result.termsOffset, -1);

  // There should be no match when the argument is different.
  result = Services.search.parseSubmissionURL(
             "http://www.google.com/search?q2=test");
  do_check_eq(result.engine, null);
  do_check_eq(result.terms, "");
  do_check_eq(result.termsOffset, -1);

  // There should be no match for URIs that are not HTTP or HTTPS.
  result = Services.search.parseSubmissionURL(
             "file://localhost/search?q=test");
  do_check_eq(result.engine, null);
  do_check_eq(result.terms, "");
  do_check_eq(result.termsOffset, -1);
});