GURL: Add GetWithoutFilename() and replace uses of Resolve(".") with it
Code that interacts with manifests often needs to get an instance of a
URL without the filename. This includes code that checks if a URL is in
scope and code that uses another url for the scope. There is no
such helper function in base which has led to re-implementing the
function in many places. Furthermore, the result of calling 'url.Resolve(".")'
is not that clear, making it really easy to miss the fact that it could
be used to get a url without the filename.
To avoid this we introduce GetWithoutFilename(). We also add tests to make
the result of calling GetWithoutFilename() clear for its users.
Change-Id: Ibb0b2a9918a20333072c407a6a25e75ea5891faf
Reviewed-on: https://chromium-review.googlesource.com/582034
Commit-Queue: Giovanni Ortuño Urquidi <[email protected]>
Reviewed-by: Matt Giuca <[email protected]>
Reviewed-by: Kinuko Yasuda <[email protected]>
Reviewed-by: Michael Nordman <[email protected]>
Reviewed-by: Mike West <[email protected]>
Cr-Commit-Position: refs/heads/master@{#492900}
diff --git a/url/gurl_unittest.cc b/url/gurl_unittest.cc
index 30db59eb..464d08a 100644
--- a/url/gurl_unittest.cc
+++ b/url/gurl_unittest.cc
@@ -387,6 +387,52 @@
}
}
+TEST(GURLTest, GetWithoutFilename) {
+ struct TestCase {
+ const char* input;
+ const char* expected;
+ } cases[] = {
+ // Common Standard URLs.
+ {"https://www.google.com", "https://www.google.com/"},
+ {"https://www.google.com/", "https://www.google.com/"},
+ {"https://www.google.com/maps.htm", "https://www.google.com/"},
+ {"https://www.google.com/maps/", "https://www.google.com/maps/"},
+ {"https://www.google.com/index.html", "https://www.google.com/"},
+ {"https://www.google.com/index.html?q=maps", "https://www.google.com/"},
+ {"https://www.google.com/index.html#maps/", "https://www.google.com/"},
+ {"https://foo:[email protected]/maps.htm", "https://foo:[email protected]/"},
+ {"https://www.google.com/maps/au/index.html", "https://www.google.com/maps/au/"},
+ {"https://www.google.com/maps/au/north", "https://www.google.com/maps/au/"},
+ {"https://www.google.com/maps/au/north/", "https://www.google.com/maps/au/north/"},
+ {"https://www.google.com/maps/au/index.html?q=maps#fragment/", "https://www.google.com/maps/au/"},
+ {"http://www.google.com:8000/maps/au/index.html?q=maps#fragment/", "http://www.google.com:8000/maps/au/"},
+ {"https://www.google.com/maps/au/north/?q=maps#fragment", "https://www.google.com/maps/au/north/"},
+ {"https://www.google.com/maps/au/north?q=maps#fragment", "https://www.google.com/maps/au/"},
+ // Less common standard URLs.
+ {"filesystem:http://www.google.com/temporary/bar.html?baz=22", "filesystem:http://www.google.com/temporary/"},
+ {"file:///temporary/bar.html?baz=22","file:///temporary/"},
+ {"ftp://foo/test/index.html", "ftp://foo/test/"},
+ {"gopher://foo/test/index.html", "gopher://foo/test/"},
+ {"ws://foo/test/index.html", "ws://foo/test/"},
+ // Non-standard, hierarchical URLs.
+ {"chrome://foo/bar.html", "chrome://foo/"},
+ {"httpa://foo/test/index.html", "httpa://foo/test/"},
+ // Non-standard, non-hierarchical URLs.
+ {"blob:https://foo.bar/test/index.html", ""},
+ {"about:blank", ""},
+ {"data:foobar", ""},
+ {"scheme:opaque_data", ""},
+ // Invalid URLs.
+ {"foobar", ""},
+ };
+
+ for (size_t i = 0; i < arraysize(cases); i++) {
+ GURL url(cases[i].input);
+ GURL without_filename = url.GetWithoutFilename();
+ EXPECT_EQ(cases[i].expected, without_filename.spec()) << i;
+ }
+}
+
TEST(GURLTest, Replacements) {
// The URL canonicalizer replacement test will handle most of these case.
// The most important thing to do here is to check that the proper