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