PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/toolkit/components/search/tests/xpcshell/test_parseSubmissionURL.js

https://bitbucket.org/vionika/spin.android
JavaScript | 177 lines | 132 code | 20 blank | 25 comment | 1 complexity | 81e8795a5cd361281756096733aee5e2 MD5 | raw file
Possible License(s): JSON, 0BSD, AGPL-1.0, BSD-2-Clause, GPL-3.0, LGPL-2.1, LGPL-3.0, CC0-1.0, AGPL-3.0, MPL-2.0, Apache-2.0, MIT, BSD-3-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, Unlicense
  1. /* Any copyright is dedicated to the Public Domain.
  2. * http://creativecommons.org/publicdomain/zero/1.0/ */
  3. /*
  4. * Tests getAlternateDomains API.
  5. */
  6. "use strict";
  7. add_task(async function setup() {
  8. useHttpServer();
  9. await AddonTestUtils.promiseStartupManager();
  10. });
  11. add_task(async function test_parseSubmissionURL() {
  12. // Hide the default engines to prevent them from being used in the search.
  13. for (let engine of await Services.search.getEngines()) {
  14. await Services.search.removeEngine(engine);
  15. }
  16. let [engine1, engine2, engine3, engine4] = await addTestEngines([
  17. { name: "Test search engine", xmlFileName: "engine.xml" },
  18. { name: "Test search engine (fr)", xmlFileName: "engine-fr.xml" },
  19. {
  20. name: "bacon_addParam",
  21. details: [
  22. "",
  23. "bacon_addParam",
  24. "Search Bacon",
  25. "GET",
  26. "http://www.bacon.test/find",
  27. ],
  28. },
  29. {
  30. name: "idn_addParam",
  31. details: [
  32. "",
  33. "idn_addParam",
  34. "Search IDN",
  35. "GET",
  36. "http://www.xn--bcher-kva.ch/search",
  37. ],
  38. },
  39. // The following engines cannot identify the search parameter.
  40. { name: "A second test engine", xmlFileName: "engine2.xml" },
  41. {
  42. name: "bacon",
  43. details: [
  44. "",
  45. "bacon",
  46. "Search Bacon",
  47. "GET",
  48. "http://www.bacon.moz/search?q={searchTerms}",
  49. ],
  50. },
  51. ]);
  52. engine3.addParam("q", "{searchTerms}", null);
  53. engine4.addParam("q", "{searchTerms}", null);
  54. // Test the first engine, whose URLs use UTF-8 encoding.
  55. let url = "http://www.google.com/search?foo=bar&q=caff%C3%A8";
  56. let result = Services.search.parseSubmissionURL(url);
  57. Assert.equal(result.engine, engine1);
  58. Assert.equal(result.terms, "caff\u00E8");
  59. Assert.ok(url.slice(result.termsOffset).startsWith("caff%C3%A8"));
  60. Assert.equal(result.termsLength, "caff%C3%A8".length);
  61. // The second engine uses a locale-specific domain that is an alternate domain
  62. // of the first one, but the second engine should get priority when matching.
  63. // The URL used with this engine uses ISO-8859-1 encoding instead.
  64. url = "http://www.google.fr/search?q=caff%E8";
  65. result = Services.search.parseSubmissionURL(url);
  66. Assert.equal(result.engine, engine2);
  67. Assert.equal(result.terms, "caff\u00E8");
  68. Assert.ok(url.slice(result.termsOffset).startsWith("caff%E8"));
  69. Assert.equal(result.termsLength, "caff%E8".length);
  70. // Test a domain that is an alternate domain of those defined. In this case,
  71. // the first matching engine from the ordered list should be returned.
  72. url = "http://www.google.co.uk/search?q=caff%C3%A8";
  73. result = Services.search.parseSubmissionURL(url);
  74. Assert.equal(result.engine, engine1);
  75. Assert.equal(result.terms, "caff\u00E8");
  76. Assert.ok(url.slice(result.termsOffset).startsWith("caff%C3%A8"));
  77. Assert.equal(result.termsLength, "caff%C3%A8".length);
  78. // We support parsing URLs from a dynamically added engine. Those engines use
  79. // windows-1252 encoding by default.
  80. url = "http://www.bacon.test/find?q=caff%E8";
  81. result = Services.search.parseSubmissionURL(url);
  82. Assert.equal(result.engine, engine3);
  83. Assert.equal(result.terms, "caff\u00E8");
  84. Assert.ok(url.slice(result.termsOffset).startsWith("caff%E8"));
  85. Assert.equal(result.termsLength, "caff%E8".length);
  86. // Test URLs with unescaped unicode characters.
  87. url = "http://www.google.com/search?q=foo+b\u00E4r";
  88. result = Services.search.parseSubmissionURL(url);
  89. Assert.equal(result.engine, engine1);
  90. Assert.equal(result.terms, "foo b\u00E4r");
  91. Assert.ok(url.slice(result.termsOffset).startsWith("foo+b\u00E4r"));
  92. Assert.equal(result.termsLength, "foo+b\u00E4r".length);
  93. // Test search engines with unescaped IDNs.
  94. url = "http://www.b\u00FCcher.ch/search?q=foo+bar";
  95. result = Services.search.parseSubmissionURL(url);
  96. Assert.equal(result.engine, engine4);
  97. Assert.equal(result.terms, "foo bar");
  98. Assert.ok(url.slice(result.termsOffset).startsWith("foo+bar"));
  99. Assert.equal(result.termsLength, "foo+bar".length);
  100. // Test search engines with escaped IDNs.
  101. url = "http://www.xn--bcher-kva.ch/search?q=foo+bar";
  102. result = Services.search.parseSubmissionURL(url);
  103. Assert.equal(result.engine, engine4);
  104. Assert.equal(result.terms, "foo bar");
  105. Assert.ok(url.slice(result.termsOffset).startsWith("foo+bar"));
  106. Assert.equal(result.termsLength, "foo+bar".length);
  107. // Parsing of parameters from an engine template URL is not supported.
  108. Assert.equal(
  109. Services.search.parseSubmissionURL("http://www.bacon.moz/search?q=").engine,
  110. null
  111. );
  112. Assert.equal(
  113. Services.search.parseSubmissionURL("https://duckduckgo.com?q=test").engine,
  114. null
  115. );
  116. Assert.equal(
  117. Services.search.parseSubmissionURL("https://duckduckgo.com/?q=test").engine,
  118. null
  119. );
  120. // HTTP and HTTPS schemes are interchangeable.
  121. url = "https://www.google.com/search?q=caff%C3%A8";
  122. result = Services.search.parseSubmissionURL(url);
  123. Assert.equal(result.engine, engine1);
  124. Assert.equal(result.terms, "caff\u00E8");
  125. Assert.ok(url.slice(result.termsOffset).startsWith("caff%C3%A8"));
  126. // Decoding search terms with multiple spaces should work.
  127. result = Services.search.parseSubmissionURL(
  128. "http://www.google.com/search?q=+with++spaces+"
  129. );
  130. Assert.equal(result.engine, engine1);
  131. Assert.equal(result.terms, " with spaces ");
  132. // An empty query parameter should work the same.
  133. url = "http://www.google.com/search?q=";
  134. result = Services.search.parseSubmissionURL(url);
  135. Assert.equal(result.engine, engine1);
  136. Assert.equal(result.terms, "");
  137. Assert.equal(result.termsOffset, url.length);
  138. // There should be no match when the path is different.
  139. result = Services.search.parseSubmissionURL(
  140. "http://www.google.com/search/?q=test"
  141. );
  142. Assert.equal(result.engine, null);
  143. Assert.equal(result.terms, "");
  144. Assert.equal(result.termsOffset, -1);
  145. // There should be no match when the argument is different.
  146. result = Services.search.parseSubmissionURL(
  147. "http://www.google.com/search?q2=test"
  148. );
  149. Assert.equal(result.engine, null);
  150. Assert.equal(result.terms, "");
  151. Assert.equal(result.termsOffset, -1);
  152. // There should be no match for URIs that are not HTTP or HTTPS.
  153. result = Services.search.parseSubmissionURL("file://localhost/search?q=test");
  154. Assert.equal(result.engine, null);
  155. Assert.equal(result.terms, "");
  156. Assert.equal(result.termsOffset, -1);
  157. });