1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef URL_GURL_ABSTRACT_TESTS_H_
6 #define URL_GURL_ABSTRACT_TESTS_H_
8 // Test suite for tests that cover both url::Url and blink::SecurityUrl.
10 // AbstractUrlTest below abstracts away differences between GURL and blink::KURL
11 // by parametrizing the tests with a class that has to expose the following
13 // using UrlType = ...;
14 // static UrlType CreateUrlFromString(std::string_view s);
15 // static bool IsAboutBlank(const UrlType& url);
16 // static bool IsAboutSrcdoc(const UrlType& url);
17 template <typename TUrlTraits>
18 class AbstractUrlTest : public testing::Test {
20 // Wrappers that help ellide away TUrlTraits.
22 // Note that calling the wrappers needs to be prefixed with `this->...` to
23 // avoid hitting: explicit qualification required to use member 'IsAboutBlank'
24 // from dependent base class.
25 using UrlType = typename TUrlTraits::UrlType;
26 UrlType CreateUrlFromString(std::string_view s) {
27 return TUrlTraits::CreateUrlFromString(s);
29 bool IsAboutBlank(const UrlType& url) {
30 return TUrlTraits::IsAboutBlank(url);
32 bool IsAboutSrcdoc(const UrlType& url) {
33 return TUrlTraits::IsAboutSrcdoc(url);
37 TYPED_TEST_SUITE_P(AbstractUrlTest);
39 TYPED_TEST_P(AbstractUrlTest, IsAboutBlankTest) {
40 // See https://tools.ietf.org/html/rfc6694 which explicitly allows
41 // `about-query` and `about-fragment` parts in about: URLs.
42 const std::string kAboutBlankUrls[] = {"about:blank", "about:blank?foo",
44 "about:blank?foo#foo"};
45 for (const auto& input : kAboutBlankUrls) {
46 SCOPED_TRACE(testing::Message() << "Test input: " << input);
47 auto url = this->CreateUrlFromString(input);
48 EXPECT_TRUE(this->IsAboutBlank(url));
51 const std::string kNotAboutBlankUrls[] = {"",
61 "about://:8000/blank",
62 "about://foo:foo@/blank",
64 "foo:bar@about:blank",
67 for (const auto& input : kNotAboutBlankUrls) {
68 SCOPED_TRACE(testing::Message() << "Test input: " << input);
69 auto url = this->CreateUrlFromString(input);
70 EXPECT_FALSE(this->IsAboutBlank(url));
74 TYPED_TEST_P(AbstractUrlTest, IsAboutSrcdocTest) {
75 // See https://tools.ietf.org/html/rfc6694 which explicitly allows
76 // `about-query` and `about-fragment` parts in about: URLs.
78 // `about:srcdoc` is defined in
79 // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#about:srcdoc
80 // which refers to rfc6694 for details.
81 const std::string kAboutSrcdocUrls[] = {
82 "about:srcdoc", "about:srcdoc/", "about:srcdoc?foo", "about:srcdoc/#foo",
83 "about:srcdoc?foo#foo"};
84 for (const auto& input : kAboutSrcdocUrls) {
85 SCOPED_TRACE(testing::Message() << "Test input: " << input);
86 auto url = this->CreateUrlFromString(input);
87 EXPECT_TRUE(this->IsAboutSrcdoc(url));
90 const std::string kNotAboutSrcdocUrls[] = {"",
95 "about:about:srcdoc:",
102 "about://:8000/srcdoc",
103 "about://foo:foo@/srcdoc",
105 "foo:bar@about:srcdoc",
108 for (const auto& input : kNotAboutSrcdocUrls) {
109 SCOPED_TRACE(testing::Message() << "Test input: " << input);
110 auto url = this->CreateUrlFromString(input);
111 EXPECT_FALSE(this->IsAboutSrcdoc(url));
115 REGISTER_TYPED_TEST_SUITE_P(AbstractUrlTest,
119 #endif // URL_GURL_ABSTRACT_TESTS_H_