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

https://github.com/dot-browser/desktop · JavaScript · 173 lines · 129 code · 19 blank · 25 comment · 1 complexity · 6185328de83f450401d10138689d5784 MD5 · raw file

  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. alias: "bacon_addParam",
  23. description: "Search Bacon",
  24. method: "GET",
  25. template: "http://www.bacon.test/find",
  26. searchGetParams: "q={searchTerms}",
  27. },
  28. },
  29. {
  30. name: "idn_addParam",
  31. details: {
  32. alias: "idn_addParam",
  33. description: "Search IDN",
  34. method: "GET",
  35. template: "http://www.xn--bcher-kva.ch/search",
  36. searchGetParams: "q={searchTerms}",
  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. alias: "bacon",
  45. description: "Search Bacon",
  46. method: "GET",
  47. template: "http://www.bacon.moz/search?q={searchTerms}",
  48. },
  49. },
  50. ]);
  51. // Test the first engine, whose URLs use UTF-8 encoding.
  52. let url = "http://www.google.com/search?foo=bar&q=caff%C3%A8";
  53. let result = Services.search.parseSubmissionURL(url);
  54. Assert.equal(result.engine, engine1);
  55. Assert.equal(result.terms, "caff\u00E8");
  56. Assert.ok(url.slice(result.termsOffset).startsWith("caff%C3%A8"));
  57. Assert.equal(result.termsLength, "caff%C3%A8".length);
  58. // The second engine uses a locale-specific domain that is an alternate domain
  59. // of the first one, but the second engine should get priority when matching.
  60. // The URL used with this engine uses ISO-8859-1 encoding instead.
  61. url = "http://www.google.fr/search?q=caff%E8";
  62. result = Services.search.parseSubmissionURL(url);
  63. Assert.equal(result.engine, engine2);
  64. Assert.equal(result.terms, "caff\u00E8");
  65. Assert.ok(url.slice(result.termsOffset).startsWith("caff%E8"));
  66. Assert.equal(result.termsLength, "caff%E8".length);
  67. // Test a domain that is an alternate domain of those defined. In this case,
  68. // the first matching engine from the ordered list should be returned.
  69. url = "http://www.google.co.uk/search?q=caff%C3%A8";
  70. result = Services.search.parseSubmissionURL(url);
  71. Assert.equal(result.engine, engine1);
  72. Assert.equal(result.terms, "caff\u00E8");
  73. Assert.ok(url.slice(result.termsOffset).startsWith("caff%C3%A8"));
  74. Assert.equal(result.termsLength, "caff%C3%A8".length);
  75. // We support parsing URLs from a dynamically added engine. Those engines use
  76. // windows-1252 encoding by default.
  77. url = "http://www.bacon.test/find?q=caff%E8";
  78. result = Services.search.parseSubmissionURL(url);
  79. Assert.equal(result.engine, engine3);
  80. Assert.equal(result.terms, "caff\u00E8");
  81. Assert.ok(url.slice(result.termsOffset).startsWith("caff%E8"));
  82. Assert.equal(result.termsLength, "caff%E8".length);
  83. // Test URLs with unescaped unicode characters.
  84. url = "http://www.google.com/search?q=foo+b\u00E4r";
  85. result = Services.search.parseSubmissionURL(url);
  86. Assert.equal(result.engine, engine1);
  87. Assert.equal(result.terms, "foo b\u00E4r");
  88. Assert.ok(url.slice(result.termsOffset).startsWith("foo+b\u00E4r"));
  89. Assert.equal(result.termsLength, "foo+b\u00E4r".length);
  90. // Test search engines with unescaped IDNs.
  91. url = "http://www.b\u00FCcher.ch/search?q=foo+bar";
  92. result = Services.search.parseSubmissionURL(url);
  93. Assert.equal(result.engine, engine4);
  94. Assert.equal(result.terms, "foo bar");
  95. Assert.ok(url.slice(result.termsOffset).startsWith("foo+bar"));
  96. Assert.equal(result.termsLength, "foo+bar".length);
  97. // Test search engines with escaped IDNs.
  98. url = "http://www.xn--bcher-kva.ch/search?q=foo+bar";
  99. result = Services.search.parseSubmissionURL(url);
  100. Assert.equal(result.engine, engine4);
  101. Assert.equal(result.terms, "foo bar");
  102. Assert.ok(url.slice(result.termsOffset).startsWith("foo+bar"));
  103. Assert.equal(result.termsLength, "foo+bar".length);
  104. // Parsing of parameters from an engine template URL is not supported.
  105. Assert.equal(
  106. Services.search.parseSubmissionURL("http://www.bacon.moz/search?q=").engine,
  107. null
  108. );
  109. Assert.equal(
  110. Services.search.parseSubmissionURL("https://duckduckgo.com?q=test").engine,
  111. null
  112. );
  113. Assert.equal(
  114. Services.search.parseSubmissionURL("https://duckduckgo.com/?q=test").engine,
  115. null
  116. );
  117. // HTTP and HTTPS schemes are interchangeable.
  118. url = "https://www.google.com/search?q=caff%C3%A8";
  119. result = Services.search.parseSubmissionURL(url);
  120. Assert.equal(result.engine, engine1);
  121. Assert.equal(result.terms, "caff\u00E8");
  122. Assert.ok(url.slice(result.termsOffset).startsWith("caff%C3%A8"));
  123. // Decoding search terms with multiple spaces should work.
  124. result = Services.search.parseSubmissionURL(
  125. "http://www.google.com/search?q=+with++spaces+"
  126. );
  127. Assert.equal(result.engine, engine1);
  128. Assert.equal(result.terms, " with spaces ");
  129. // An empty query parameter should work the same.
  130. url = "http://www.google.com/search?q=";
  131. result = Services.search.parseSubmissionURL(url);
  132. Assert.equal(result.engine, engine1);
  133. Assert.equal(result.terms, "");
  134. Assert.equal(result.termsOffset, url.length);
  135. // There should be no match when the path is different.
  136. result = Services.search.parseSubmissionURL(
  137. "http://www.google.com/search/?q=test"
  138. );
  139. Assert.equal(result.engine, null);
  140. Assert.equal(result.terms, "");
  141. Assert.equal(result.termsOffset, -1);
  142. // There should be no match when the argument is different.
  143. result = Services.search.parseSubmissionURL(
  144. "http://www.google.com/search?q2=test"
  145. );
  146. Assert.equal(result.engine, null);
  147. Assert.equal(result.terms, "");
  148. Assert.equal(result.termsOffset, -1);
  149. // There should be no match for URIs that are not HTTP or HTTPS.
  150. result = Services.search.parseSubmissionURL("file://localhost/search?q=test");
  151. Assert.equal(result.engine, null);
  152. Assert.equal(result.terms, "");
  153. Assert.equal(result.termsOffset, -1);
  154. });