1 // Copyright (c) 2010 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.
5 #include "net/tools/dump_cache/url_utilities.h"
9 #include "base/string_util.h"
10 #include "base/stringprintf.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 TEST(UrlUtilitiesTest
, GetUrlHost
) {
16 EXPECT_EQ("www.foo.com",
17 UrlUtilities::GetUrlHost("http://www.foo.com"));
18 EXPECT_EQ("www.foo.com",
19 UrlUtilities::GetUrlHost("http://www.foo.com:80"));
20 EXPECT_EQ("www.foo.com",
21 UrlUtilities::GetUrlHost("http://www.foo.com:80/"));
22 EXPECT_EQ("www.foo.com",
23 UrlUtilities::GetUrlHost("http://www.foo.com/news"));
24 EXPECT_EQ("www.foo.com",
25 UrlUtilities::GetUrlHost("www.foo.com:80/news?q=hello"));
26 EXPECT_EQ("www.foo.com",
27 UrlUtilities::GetUrlHost("www.foo.com/news?q=a:b"));
28 EXPECT_EQ("www.foo.com",
29 UrlUtilities::GetUrlHost("www.foo.com:80"));
32 TEST(UrlUtilitiesTest
, GetUrlHostPath
) {
33 EXPECT_EQ("www.foo.com",
34 UrlUtilities::GetUrlHostPath("http://www.foo.com"));
35 EXPECT_EQ("www.foo.com:80",
36 UrlUtilities::GetUrlHostPath("http://www.foo.com:80"));
37 EXPECT_EQ("www.foo.com:80/",
38 UrlUtilities::GetUrlHostPath("http://www.foo.com:80/"));
39 EXPECT_EQ("www.foo.com/news",
40 UrlUtilities::GetUrlHostPath("http://www.foo.com/news"));
41 EXPECT_EQ("www.foo.com:80/news?q=hello",
42 UrlUtilities::GetUrlHostPath("www.foo.com:80/news?q=hello"));
43 EXPECT_EQ("www.foo.com/news?q=a:b",
44 UrlUtilities::GetUrlHostPath("www.foo.com/news?q=a:b"));
45 EXPECT_EQ("www.foo.com:80",
46 UrlUtilities::GetUrlHostPath("www.foo.com:80"));
49 TEST(UrlUtilitiesTest
, GetUrlPath
) {
51 UrlUtilities::GetUrlPath("http://www.foo.com"));
53 UrlUtilities::GetUrlPath("http://www.foo.com:80"));
55 UrlUtilities::GetUrlPath("http://www.foo.com:80/"));
57 UrlUtilities::GetUrlPath("http://www.foo.com/news"));
58 EXPECT_EQ("/news?q=hello",
59 UrlUtilities::GetUrlPath("www.foo.com:80/news?q=hello"));
60 EXPECT_EQ("/news?q=a:b",
61 UrlUtilities::GetUrlPath("www.foo.com/news?q=a:b"));
63 UrlUtilities::GetUrlPath("www.foo.com:80"));
66 TEST(UrlUtilitiesTest
, Unescape
) {
67 // Basic examples are left alone.
68 EXPECT_EQ("http://www.foo.com",
69 UrlUtilities::Unescape("http://www.foo.com"));
70 EXPECT_EQ("www.foo.com:80/news?q=hello",
71 UrlUtilities::Unescape("www.foo.com:80/news?q=hello"));
73 // All chars can be unescaped.
74 EXPECT_EQ("~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/",
75 UrlUtilities::Unescape("%7E%60%21%40%23%24%25%5E%26%2A%28%29%5F%2D"
76 "%2B%3D%7B%5B%7D%5D%7C%5C%3A%3B%22%27%3C%2C"
78 for (int c
= 0; c
< 256; ++c
) {
79 std::string
unescaped_char(1, implicit_cast
<unsigned char>(c
));
80 std::string escaped_char
= base::StringPrintf("%%%02X", c
);
81 EXPECT_EQ(unescaped_char
, UrlUtilities::Unescape(escaped_char
))
82 << "escaped_char = " << escaped_char
;
83 escaped_char
= base::StringPrintf("%%%02x", c
);
84 EXPECT_EQ(unescaped_char
, UrlUtilities::Unescape(escaped_char
))
85 << "escaped_char = " << escaped_char
;
88 // All non-% chars are left alone.
89 EXPECT_EQ("~`!@#$^&*()_-+={[}]|\\:;\"'<,>.?/",
90 UrlUtilities::Unescape("~`!@#$^&*()_-+={[}]|\\:;\"'<,>.?/"));
91 for (int c
= 0; c
< 256; ++c
) {
93 std::string
just_char(1, implicit_cast
<unsigned char>(c
));
94 EXPECT_EQ(just_char
, UrlUtilities::Unescape(just_char
));
98 // Some examples to unescape.
99 EXPECT_EQ("Hello, world!", UrlUtilities::Unescape("Hello%2C world%21"));
101 // Not actually escapes.
102 EXPECT_EQ("%", UrlUtilities::Unescape("%"));
103 EXPECT_EQ("%www", UrlUtilities::Unescape("%www"));
104 EXPECT_EQ("%foo", UrlUtilities::Unescape("%foo"));
105 EXPECT_EQ("%1", UrlUtilities::Unescape("%1"));
106 EXPECT_EQ("%1x", UrlUtilities::Unescape("%1x"));
107 EXPECT_EQ("%%", UrlUtilities::Unescape("%%"));
108 // Escapes following non-escapes.
109 EXPECT_EQ("%!", UrlUtilities::Unescape("%%21"));
110 EXPECT_EQ("%2!", UrlUtilities::Unescape("%2%21"));