url_utils_unittest.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/dom_distiller/core/url_utils.h"
  5. #include "components/dom_distiller/core/url_constants.h"
  6. #include "net/base/url_util.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. #include "url/gurl.h"
  9. #include "url/url_util.h"
  10. namespace dom_distiller {
  11. namespace url_utils {
  12. TEST(DomDistillerUrlUtilsTest, TestPathUtil) {
  13. const std::string single_key = "mypath?foo=bar";
  14. EXPECT_EQ("bar", GetValueForKeyInUrlPathQuery(single_key, "foo"));
  15. const std::string two_keys = "mypath?key1=foo&key2=bar";
  16. EXPECT_EQ("foo", GetValueForKeyInUrlPathQuery(two_keys, "key1"));
  17. EXPECT_EQ("bar", GetValueForKeyInUrlPathQuery(two_keys, "key2"));
  18. // First occurrence wins.
  19. const std::string multiple_same_key = "mypath?key=foo&key=bar";
  20. EXPECT_EQ("foo", GetValueForKeyInUrlPathQuery(multiple_same_key, "key"));
  21. }
  22. TEST(DomDistillerUrlUtilsTest, TestGetValueForKeyInUrlPathQuery) {
  23. // Tests an invalid url.
  24. const std::string invalid_url = "http://%40[::1]/";
  25. EXPECT_EQ("", GetValueForKeyInUrlPathQuery(invalid_url, "key"));
  26. // Test a valid URL with the key we are searching for.
  27. const std::string valid_url_with_key = "http://www.google.com?key=foo";
  28. EXPECT_EQ("foo", GetValueForKeyInUrlPathQuery(valid_url_with_key, "key"));
  29. // Test a valid URL without the key we are searching for.
  30. const std::string valid_url_no_key = "http://www.google.com";
  31. EXPECT_EQ("", GetValueForKeyInUrlPathQuery(valid_url_no_key, "key"));
  32. // Test a valid URL with 2 values of the key we are searching for.
  33. const std::string valid_url_two_keys =
  34. "http://www.google.com?key=foo&key=bar";
  35. EXPECT_EQ("foo", GetValueForKeyInUrlPathQuery(valid_url_two_keys, "key"));
  36. }
  37. void AssertEqualExceptHost(const GURL& a, const GURL& b) {
  38. GURL::Replacements no_host;
  39. no_host.ClearHost();
  40. EXPECT_EQ(a.ReplaceComponents(no_host), b.ReplaceComponents(no_host));
  41. }
  42. TEST(DomDistillerUrlUtilsTest, TestGetDistillerViewUrlFromUrl) {
  43. AssertEqualExceptHost(
  44. GURL("chrome-distiller://any/"
  45. "?title=cats&time=123&url=http%3A%2F%2Fexample.com%2Fpath%3Fq%3Dabc%"
  46. "26p%3D1%"
  47. "23anchor"),
  48. GetDistillerViewUrlFromUrl(
  49. kDomDistillerScheme, GURL("http://example.com/path?q=abc&p=1#anchor"),
  50. "cats", 123));
  51. }
  52. TEST(DomDistillerUrlUtilsTest, TestGetPageTitleFromDistillerUrl) {
  53. // Ensure that odd characters make it through.
  54. std::string title = "An Interesting Article: Cats >= Dogs!";
  55. GURL distilled = GetDistillerViewUrlFromUrl(
  56. kDomDistillerScheme, GURL("http://example.com/path?q=abc&p=1#anchor"),
  57. title);
  58. EXPECT_EQ(title, GetTitleFromDistillerUrl(distilled));
  59. // Let's try some Unicode outside of BMP.
  60. title = "Γάτα قط ねこ חתול ␡";
  61. distilled = GetDistillerViewUrlFromUrl(
  62. kDomDistillerScheme, GURL("http://example.com/article.html"), title);
  63. EXPECT_EQ(title, GetTitleFromDistillerUrl(distilled));
  64. }
  65. std::string GetOriginalUrlFromDistillerUrl(const std::string& url) {
  66. return GetOriginalUrlFromDistillerUrl(GURL(url)).spec();
  67. }
  68. TEST(DomDistillerUrlUtilsTest, TestGetOriginalUrlFromDistillerUrl) {
  69. EXPECT_EQ(
  70. "http://example.com/path?q=abc&p=1#anchor",
  71. GetOriginalUrlFromDistillerUrl(
  72. "chrome-distiller://"
  73. "any_"
  74. "d091ebf8f841eae9ca23822c3d0f369c16d3748478d0b74111be176eb96722e5/"
  75. "?time=123&url=http%3A%2F%2Fexample.com%2Fpath%3Fq%3Dabc%26p%3D1%"
  76. "23anchor"));
  77. }
  78. std::string ThroughDistiller(const std::string& url) {
  79. return GetOriginalUrlFromDistillerUrl(
  80. GetDistillerViewUrlFromUrl(kDomDistillerScheme, GURL(url), "title",
  81. 123))
  82. .spec();
  83. }
  84. TEST(DomDistillerUrlUtilsTest, TestDistillerEndToEnd) {
  85. // Tests a normal url.
  86. const std::string url = "http://example.com/";
  87. EXPECT_EQ(url, ThroughDistiller(url));
  88. EXPECT_EQ(url, GetOriginalUrlFromDistillerUrl(url));
  89. // Tests a url with arguments and anchor points.
  90. const std::string url_arguments =
  91. "https://example.com/?key=value&key=value2&key2=value3#here";
  92. EXPECT_EQ(url_arguments, ThroughDistiller(url_arguments));
  93. EXPECT_EQ(url_arguments, GetOriginalUrlFromDistillerUrl(url_arguments));
  94. // Tests a url with file:// scheme.
  95. const std::string url_file = "file:///home/userid/path/index.html";
  96. EXPECT_EQ("", ThroughDistiller(url_file));
  97. EXPECT_EQ(url_file, GetOriginalUrlFromDistillerUrl(url_file));
  98. // Tests a nested url.
  99. const std::string nested_url =
  100. GetDistillerViewUrlFromUrl(kDomDistillerScheme, GURL(url), "title")
  101. .spec();
  102. EXPECT_EQ("", ThroughDistiller(nested_url));
  103. EXPECT_EQ(url, GetOriginalUrlFromDistillerUrl(nested_url));
  104. }
  105. TEST(DomDistillerUrlUtilsTest, TestRejectInvalidURLs) {
  106. const std::string url = "http://example.com/";
  107. const std::string url2 = "http://example.org/";
  108. const GURL view_url =
  109. GetDistillerViewUrlFromUrl(kDomDistillerScheme, GURL(url), "title", 123);
  110. GURL bad_view_url =
  111. net::AppendOrReplaceQueryParameter(view_url, kUrlKey, url2);
  112. EXPECT_EQ(GURL(), GetOriginalUrlFromDistillerUrl(bad_view_url));
  113. }
  114. TEST(DomDistillerUrlUtilsTest, TestRejectInvalidDistilledURLs) {
  115. EXPECT_FALSE(IsDistilledPage(GURL("chrome-distiller://any")));
  116. EXPECT_FALSE(IsDistilledPage(GURL("chrome-distiller://any/invalid")));
  117. EXPECT_FALSE(
  118. IsDistilledPage(GURL("chrome-distiller://any/?time=123&url=abc")));
  119. EXPECT_FALSE(IsDistilledPage(GetDistillerViewUrlFromUrl(
  120. "not-distiller", GURL("http://example.com/"), "title")));
  121. EXPECT_FALSE(IsDistilledPage(GetDistillerViewUrlFromUrl(
  122. kDomDistillerScheme, GURL("not-http://example.com/"), "title")));
  123. EXPECT_TRUE(IsDistilledPage(GetDistillerViewUrlFromUrl(
  124. kDomDistillerScheme, GURL("http://example.com/"), "title")));
  125. EXPECT_TRUE(IsDistilledPage(GetDistillerViewUrlFromUrl(
  126. kDomDistillerScheme, GURL("http://www.example.com/page.html"), "title")));
  127. EXPECT_TRUE(IsDistilledPage(GetDistillerViewUrlFromUrl(
  128. kDomDistillerScheme,
  129. GURL("http://www.example.com/page.html?cats=1&dogs=2"), "title")));
  130. EXPECT_TRUE(IsDistilledPage(GetDistillerViewUrlFromUrl(
  131. kDomDistillerScheme, GURL("https://example.com/?params=any"), "title")));
  132. }
  133. } // namespace url_utils
  134. } // namespace dom_distiller