File: url_utils_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (161 lines) | stat: -rw-r--r-- 6,419 bytes parent folder | download | duplicates (5)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/dom_distiller/core/url_utils.h"

#include "components/dom_distiller/core/url_constants.h"
#include "net/base/url_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/url_util.h"

namespace dom_distiller {

namespace url_utils {

TEST(DomDistillerUrlUtilsTest, TestPathUtil) {
  const std::string single_key = "mypath?foo=bar";
  EXPECT_EQ("bar", GetValueForKeyInUrlPathQuery(single_key, "foo"));

  const std::string two_keys = "mypath?key1=foo&key2=bar";
  EXPECT_EQ("foo", GetValueForKeyInUrlPathQuery(two_keys, "key1"));
  EXPECT_EQ("bar", GetValueForKeyInUrlPathQuery(two_keys, "key2"));

  // First occurrence wins.
  const std::string multiple_same_key = "mypath?key=foo&key=bar";
  EXPECT_EQ("foo", GetValueForKeyInUrlPathQuery(multiple_same_key, "key"));
}

TEST(DomDistillerUrlUtilsTest, TestGetValueForKeyInUrlPathQuery) {
  // Tests an invalid url.
  const std::string invalid_url = "http://%40[::1]/";
  EXPECT_EQ("", GetValueForKeyInUrlPathQuery(invalid_url, "key"));

  // Test a valid URL with the key we are searching for.
  const std::string valid_url_with_key = "http://www.google.com?key=foo";
  EXPECT_EQ("foo", GetValueForKeyInUrlPathQuery(valid_url_with_key, "key"));

  // Test a valid URL without the key we are searching for.
  const std::string valid_url_no_key = "http://www.google.com";
  EXPECT_EQ("", GetValueForKeyInUrlPathQuery(valid_url_no_key, "key"));

  // Test a valid URL with 2 values of the key we are searching for.
  const std::string valid_url_two_keys =
      "http://www.google.com?key=foo&key=bar";
  EXPECT_EQ("foo", GetValueForKeyInUrlPathQuery(valid_url_two_keys, "key"));
}

void AssertEqualExceptHost(const GURL& a, const GURL& b) {
  GURL::Replacements no_host;
  no_host.ClearHost();
  EXPECT_EQ(a.ReplaceComponents(no_host), b.ReplaceComponents(no_host));
}

TEST(DomDistillerUrlUtilsTest, TestGetDistillerViewUrlFromUrl) {
  AssertEqualExceptHost(
      GURL("chrome-distiller://any/"
           "?title=cats&time=123&url=http%3A%2F%2Fexample.com%2Fpath%3Fq%3Dabc%"
           "26p%3D1%"
           "23anchor"),
      GetDistillerViewUrlFromUrl(
          kDomDistillerScheme, GURL("http://example.com/path?q=abc&p=1#anchor"),
          "cats", 123));
}

TEST(DomDistillerUrlUtilsTest, TestGetPageTitleFromDistillerUrl) {
  // Ensure that odd characters make it through.
  std::string title = "An Interesting Article: Cats >= Dogs!";
  GURL distilled = GetDistillerViewUrlFromUrl(
      kDomDistillerScheme, GURL("http://example.com/path?q=abc&p=1#anchor"),
      title);
  EXPECT_EQ(title, GetTitleFromDistillerUrl(distilled));

  // Let's try some Unicode outside of BMP.
  title = "Γάτα قط ねこ חתול ␡";
  distilled = GetDistillerViewUrlFromUrl(
      kDomDistillerScheme, GURL("http://example.com/article.html"), title);
  EXPECT_EQ(title, GetTitleFromDistillerUrl(distilled));
}

std::string GetOriginalUrlFromDistillerUrl(const std::string& url) {
  return GetOriginalUrlFromDistillerUrl(GURL(url)).spec();
}

TEST(DomDistillerUrlUtilsTest, TestGetOriginalUrlFromDistillerUrl) {
  EXPECT_EQ(
      "http://example.com/path?q=abc&p=1#anchor",
      GetOriginalUrlFromDistillerUrl(
          "chrome-distiller://"
          "any_"
          "d091ebf8f841eae9ca23822c3d0f369c16d3748478d0b74111be176eb96722e5/"
          "?time=123&url=http%3A%2F%2Fexample.com%2Fpath%3Fq%3Dabc%26p%3D1%"
          "23anchor"));
}

std::string ThroughDistiller(const std::string& url) {
  return GetOriginalUrlFromDistillerUrl(
             GetDistillerViewUrlFromUrl(kDomDistillerScheme, GURL(url), "title",
                                        123))
      .spec();
}

TEST(DomDistillerUrlUtilsTest, TestDistillerEndToEnd) {
  // Tests a normal url.
  const std::string url = "http://example.com/";
  EXPECT_EQ(url, ThroughDistiller(url));
  EXPECT_EQ(url, GetOriginalUrlFromDistillerUrl(url));

  // Tests a url with arguments and anchor points.
  const std::string url_arguments =
      "https://example.com/?key=value&key=value2&key2=value3#here";
  EXPECT_EQ(url_arguments, ThroughDistiller(url_arguments));
  EXPECT_EQ(url_arguments, GetOriginalUrlFromDistillerUrl(url_arguments));

  // Tests a url with file:// scheme.
  const std::string url_file = "file:///home/userid/path/index.html";
  EXPECT_EQ("", ThroughDistiller(url_file));
  EXPECT_EQ(url_file, GetOriginalUrlFromDistillerUrl(url_file));

  // Tests a nested url.
  const std::string nested_url =
      GetDistillerViewUrlFromUrl(kDomDistillerScheme, GURL(url), "title")
          .spec();
  EXPECT_EQ("", ThroughDistiller(nested_url));
  EXPECT_EQ(url, GetOriginalUrlFromDistillerUrl(nested_url));
}

TEST(DomDistillerUrlUtilsTest, TestRejectInvalidURLs) {
  const std::string url = "http://example.com/";
  const std::string url2 = "http://example.org/";
  const GURL view_url =
      GetDistillerViewUrlFromUrl(kDomDistillerScheme, GURL(url), "title", 123);
  GURL bad_view_url =
      net::AppendOrReplaceQueryParameter(view_url, kUrlKey, url2);
  EXPECT_EQ(GURL(), GetOriginalUrlFromDistillerUrl(bad_view_url));
}

TEST(DomDistillerUrlUtilsTest, TestRejectInvalidDistilledURLs) {
  EXPECT_FALSE(IsDistilledPage(GURL("chrome-distiller://any")));
  EXPECT_FALSE(IsDistilledPage(GURL("chrome-distiller://any/invalid")));
  EXPECT_FALSE(
      IsDistilledPage(GURL("chrome-distiller://any/?time=123&url=abc")));

  EXPECT_FALSE(IsDistilledPage(GetDistillerViewUrlFromUrl(
      "not-distiller", GURL("http://example.com/"), "title")));
  EXPECT_FALSE(IsDistilledPage(GetDistillerViewUrlFromUrl(
      kDomDistillerScheme, GURL("not-http://example.com/"), "title")));

  EXPECT_TRUE(IsDistilledPage(GetDistillerViewUrlFromUrl(
      kDomDistillerScheme, GURL("http://example.com/"), "title")));
  EXPECT_TRUE(IsDistilledPage(GetDistillerViewUrlFromUrl(
      kDomDistillerScheme, GURL("http://www.example.com/page.html"), "title")));
  EXPECT_TRUE(IsDistilledPage(GetDistillerViewUrlFromUrl(
      kDomDistillerScheme,
      GURL("http://www.example.com/page.html?cats=1&dogs=2"), "title")));
  EXPECT_TRUE(IsDistilledPage(GetDistillerViewUrlFromUrl(
      kDomDistillerScheme, GURL("https://example.com/?params=any"), "title")));
}
}  // namespace url_utils

}  // namespace dom_distiller