summaryrefslogtreecommitdiffstats
path: root/net/base/net_util_unittest.cc
diff options
context:
space:
mode:
Diffstat (limited to 'net/base/net_util_unittest.cc')
-rw-r--r--net/base/net_util_unittest.cc189
1 files changed, 189 insertions, 0 deletions
diff --git a/net/base/net_util_unittest.cc b/net/base/net_util_unittest.cc
index 67cc03e..a3b3afb 100644
--- a/net/base/net_util_unittest.cc
+++ b/net/base/net_util_unittest.cc
@@ -388,6 +388,16 @@ struct SuggestedFilenameCase {
struct UrlTestData {
const char* description;
const char* input;
+ const char* languages;
+ net::FormatUrlTypes format_types;
+ UnescapeRule::Type escape_rules;
+ const wchar_t* output; // Use |wchar_t| to handle Unicode constants easily.
+ size_t prefix_len;
+};
+
+struct UrlTestDataDeprecated {
+ const char* description;
+ const char* input;
const std::wstring languages;
net::FormatUrlTypes format_types;
UnescapeRule::Type escape_rules;
@@ -1341,6 +1351,185 @@ TEST(NetUtilTest, GetHostName) {
TEST(NetUtilTest, FormatUrl) {
net::FormatUrlTypes default_format_type = net::kFormatUrlOmitUsernamePassword;
const UrlTestData tests[] = {
+ {"Empty URL", "", "", default_format_type, UnescapeRule::NORMAL, L"", 0},
+
+ {"Simple URL",
+ "http://www.google.com/", "", default_format_type, UnescapeRule::NORMAL,
+ L"http://www.google.com/", 7},
+
+ {"With a port number and a reference",
+ "http://www.google.com:8080/#\xE3\x82\xB0", "", default_format_type,
+ UnescapeRule::NORMAL,
+ L"http://www.google.com:8080/#\x30B0", 7},
+
+ // -------- IDN tests --------
+ {"Japanese IDN with ja",
+ "http://xn--l8jvb1ey91xtjb.jp", "ja", default_format_type,
+ UnescapeRule::NORMAL, L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7},
+
+ {"Japanese IDN with en",
+ "http://xn--l8jvb1ey91xtjb.jp", "en", default_format_type,
+ UnescapeRule::NORMAL, L"http://xn--l8jvb1ey91xtjb.jp/", 7},
+
+ {"Japanese IDN without any languages",
+ "http://xn--l8jvb1ey91xtjb.jp", "", default_format_type,
+ UnescapeRule::NORMAL,
+ // Single script is safe for empty languages.
+ L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7},
+
+ {"mailto: with Japanese IDN",
+ "mailto:foo@xn--l8jvb1ey91xtjb.jp", "ja", default_format_type,
+ UnescapeRule::NORMAL,
+ // GURL doesn't assume an email address's domain part as a host name.
+ L"mailto:foo@xn--l8jvb1ey91xtjb.jp", 7},
+
+ {"file: with Japanese IDN",
+ "file://xn--l8jvb1ey91xtjb.jp/config.sys", "ja", default_format_type,
+ UnescapeRule::NORMAL,
+ L"file://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 7},
+
+ {"ftp: with Japanese IDN",
+ "ftp://xn--l8jvb1ey91xtjb.jp/config.sys", "ja", default_format_type,
+ UnescapeRule::NORMAL,
+ L"ftp://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 6},
+
+ // -------- omit_username_password flag tests --------
+ {"With username and password, omit_username_password=false",
+ "http://user:passwd@example.com/foo", "",
+ net::kFormatUrlOmitNothing, UnescapeRule::NORMAL,
+ L"http://user:passwd@example.com/foo", 19},
+
+ {"With username and password, omit_username_password=true",
+ "http://user:passwd@example.com/foo", "", default_format_type,
+ UnescapeRule::NORMAL, L"http://example.com/foo", 7},
+
+ {"With username and no password",
+ "http://user@example.com/foo", "", default_format_type,
+ UnescapeRule::NORMAL, L"http://example.com/foo", 7},
+
+ {"Just '@' without username and password",
+ "http://@example.com/foo", "", default_format_type, UnescapeRule::NORMAL,
+ L"http://example.com/foo", 7},
+
+ // GURL doesn't think local-part of an email address is username for URL.
+ {"mailto:, omit_username_password=true",
+ "mailto:foo@example.com", "", default_format_type, UnescapeRule::NORMAL,
+ L"mailto:foo@example.com", 7},
+
+ // -------- unescape flag tests --------
+ {"Do not unescape",
+ "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/"
+ "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"
+ "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", "en", default_format_type,
+ UnescapeRule::NONE,
+ // GURL parses %-encoded hostnames into Punycode.
+ L"http://xn--qcka1pmc.jp/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"
+ L"?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", 7},
+
+ {"Unescape normally",
+ "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/"
+ "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"
+ "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", "en", default_format_type,
+ UnescapeRule::NORMAL,
+ L"http://xn--qcka1pmc.jp/\x30B0\x30FC\x30B0\x30EB"
+ L"?q=\x30B0\x30FC\x30B0\x30EB", 7},
+
+ {"Unescape normally including unescape spaces",
+ "http://www.google.com/search?q=Hello%20World", "en", default_format_type,
+ UnescapeRule::SPACES, L"http://www.google.com/search?q=Hello World", 7},
+
+ /*
+ {"unescape=true with some special characters",
+ "http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", "",
+ net::kFormatUrlOmitNothing, UnescapeRule::NORMAL,
+ L"http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", 25},
+ */
+ // Disabled: the resultant URL becomes "...user%253A:%2540passwd...".
+
+ // -------- omit http: --------
+ {"omit http with user name",
+ "http://user@example.com/foo", "", net::kFormatUrlOmitAll,
+ UnescapeRule::NORMAL, L"example.com/foo", 0},
+
+ {"omit http",
+ "http://www.google.com/", "en", net::kFormatUrlOmitHTTP,
+ UnescapeRule::NORMAL, L"www.google.com/",
+ 0},
+
+ {"omit http with https",
+ "https://www.google.com/", "en", net::kFormatUrlOmitHTTP,
+ UnescapeRule::NORMAL, L"https://www.google.com/",
+ 8},
+
+ {"omit http starts with ftp.",
+ "http://ftp.google.com/", "en", net::kFormatUrlOmitHTTP,
+ UnescapeRule::NORMAL, L"http://ftp.google.com/",
+ 7},
+
+ // -------- omit trailing slash on bare hostname --------
+ {"omit slash when it's the entire path",
+ "http://www.google.com/", "en",
+ net::kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL,
+ L"http://www.google.com", 7},
+ {"omit slash when there's a ref",
+ "http://www.google.com/#ref", "en",
+ net::kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL,
+ L"http://www.google.com/#ref", 7},
+ {"omit slash when there's a query",
+ "http://www.google.com/?", "en",
+ net::kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL,
+ L"http://www.google.com/?", 7},
+ {"omit slash when it's not the entire path",
+ "http://www.google.com/foo", "en",
+ net::kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL,
+ L"http://www.google.com/foo", 7},
+ {"omit slash for nonstandard URLs",
+ "data:/", "en", net::kFormatUrlOmitTrailingSlashOnBareHostname,
+ UnescapeRule::NORMAL, L"data:/", 5},
+ {"omit slash for file URLs",
+ "file:///", "en", net::kFormatUrlOmitTrailingSlashOnBareHostname,
+ UnescapeRule::NORMAL, L"file:///", 7},
+
+ // -------- view-source: --------
+ {"view-source",
+ "view-source:http://xn--qcka1pmc.jp/", "ja", default_format_type,
+ UnescapeRule::NORMAL, L"view-source:http://\x30B0\x30FC\x30B0\x30EB.jp/",
+ 19},
+
+ {"view-source of view-source",
+ "view-source:view-source:http://xn--qcka1pmc.jp/", "ja",
+ default_format_type, UnescapeRule::NORMAL,
+ L"view-source:view-source:http://xn--qcka1pmc.jp/", 12},
+
+ // view-source should omit http and trailing slash where non-view-source
+ // would.
+ {"view-source omit http",
+ "view-source:http://a.b/c", "en", net::kFormatUrlOmitAll,
+ UnescapeRule::NORMAL, L"view-source:a.b/c",
+ 12},
+ {"view-source omit http starts with ftp.",
+ "view-source:http://ftp.b/c", "en", net::kFormatUrlOmitAll,
+ UnescapeRule::NORMAL, L"view-source:http://ftp.b/c",
+ 19},
+ {"view-source omit slash when it's the entire path",
+ "view-source:http://a.b/", "en", net::kFormatUrlOmitAll,
+ UnescapeRule::NORMAL, L"view-source:a.b",
+ 12},
+ };
+
+ for (size_t i = 0; i < arraysize(tests); ++i) {
+ size_t prefix_len;
+ string16 formatted = net::FormatUrl(
+ GURL(tests[i].input), tests[i].languages, tests[i].format_types,
+ tests[i].escape_rules, NULL, &prefix_len, NULL);
+ EXPECT_EQ(WideToUTF16(tests[i].output), formatted) << tests[i].description;
+ EXPECT_EQ(tests[i].prefix_len, prefix_len) << tests[i].description;
+ }
+}
+
+TEST(NetUtilTest, FormatUrlDeprecated) {
+ net::FormatUrlTypes default_format_type = net::kFormatUrlOmitUsernamePassword;
+ const UrlTestDataDeprecated tests[] = {
{"Empty URL", "", L"", default_format_type, UnescapeRule::NORMAL, L"", 0},
{"Simple URL",