PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

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