1 // Copyright 2012 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 #include "chrome/browser/browser_about_handler.h"
13 #include "chrome/common/url_constants.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/browser/navigation_controller.h"
16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/common/referrer.h"
18 #include "content/public/test/browser_task_environment.h"
19 #include "services/network/public/cpp/shared_url_loader_factory.h"
20 #include "testing/gtest/include/gtest/gtest.h"
23 using content::BrowserThread;
24 using content::NavigationController;
25 using content::NavigationEntry;
26 using content::Referrer;
29 struct AboutURLTestCase {
35 class BrowserAboutHandlerTest : public testing::Test {
37 void TestHandleChromeAboutAndChromeSyncRewrite(
38 const std::vector<AboutURLTestCase>& test_cases) {
39 TestingProfile profile;
41 for (const auto& test_case : test_cases) {
42 GURL url(test_case.test_url);
43 HandleChromeAboutAndChromeSyncRewrite(&url, &profile);
44 EXPECT_EQ(test_case.expected_url, url);
49 content::BrowserTaskEnvironment task_environment_;
52 TEST_F(BrowserAboutHandlerTest, HandleChromeAboutAndChromeSyncRewrite) {
53 std::string chrome_prefix(content::kChromeUIScheme);
54 chrome_prefix.append(url::kStandardSchemeSeparator);
55 std::vector<AboutURLTestCase> test_cases(
56 {{GURL("http://google.com"), GURL("http://google.com")},
57 {GURL(url::kAboutBlankURL), GURL(url::kAboutBlankURL)},
58 {GURL(chrome_prefix + chrome::kChromeUIDefaultHost),
59 GURL(chrome_prefix + chrome::kChromeUIVersionHost)},
60 {GURL(chrome_prefix + chrome::kChromeUIAboutHost),
61 GURL(chrome_prefix + chrome::kChromeUIChromeURLsHost)},
62 {GURL(chrome_prefix + chrome::kChromeUISignInInternalsHost),
63 GURL(chrome_prefix + chrome::kChromeUISignInInternalsHost)},
64 {GURL(chrome_prefix + chrome::kChromeUISyncHost),
65 GURL(chrome_prefix + chrome::kChromeUISyncInternalsHost)},
67 GURL(chrome_prefix + "host/path?query#ref"),
68 GURL(chrome_prefix + "host/path?query#ref"),
70 TestHandleChromeAboutAndChromeSyncRewrite(test_cases);
73 TEST_F(BrowserAboutHandlerTest,
74 HandleChromeAboutAndChromeSyncRewriteForMDSettings) {
75 std::string chrome_prefix(content::kChromeUIScheme);
76 chrome_prefix.append(url::kStandardSchemeSeparator);
77 std::vector<AboutURLTestCase> test_cases(
78 {{GURL(chrome_prefix + chrome::kChromeUISettingsHost),
79 GURL(chrome_prefix + chrome::kChromeUISettingsHost)}});
80 TestHandleChromeAboutAndChromeSyncRewrite(test_cases);
83 TEST_F(BrowserAboutHandlerTest,
84 HandleChromeAboutAndChromeSyncRewriteForHistory) {
85 GURL::Replacements replace_foo_query;
86 replace_foo_query.SetQueryStr("foo");
88 GURL(chrome::kChromeUIHistoryURL).ReplaceComponents(replace_foo_query));
89 TestHandleChromeAboutAndChromeSyncRewrite(std::vector<AboutURLTestCase>({
90 {GURL("chrome:history"), GURL(chrome::kChromeUIHistoryURL)},
91 {GURL(chrome::kChromeUIHistoryURL), GURL(chrome::kChromeUIHistoryURL)},
92 {history_foo_url, history_foo_url},
96 // Ensure that minor BrowserAboutHandler fixup to a URL does not cause us to
97 // keep a separate virtual URL, which would not be updated on redirects.
98 // See https://crbug.com/449829.
99 TEST_F(BrowserAboutHandlerTest, NoVirtualURLForFixup) {
100 GURL url("view-source:http://.foo");
102 // No "fixing" of the URL is expected at the content::NavigationEntry layer.
103 // We should only "fix" strings from the user (e.g. URLs from the Omnibox).
105 // Rewriters will remove the view-source prefix and expect it to stay in the
107 GURL expected_virtual_url = url;
108 GURL expected_url("http://.foo/");
110 TestingProfile profile;
111 std::unique_ptr<NavigationEntry> entry(
112 NavigationController::CreateNavigationEntry(
113 url, Referrer(), /* initiator_origin= */ absl::nullopt,
114 /* initiator_base_url= */ absl::nullopt, ui::PAGE_TRANSITION_RELOAD,
115 false, std::string(), &profile,
116 nullptr /* blob_url_loader_factory */));
117 EXPECT_EQ(expected_virtual_url, entry->GetVirtualURL());
118 EXPECT_EQ(expected_url, entry->GetURL());
121 TEST_F(BrowserAboutHandlerTest, HandleNonNavigationAboutURL_Invalid) {
122 GURL invalid_url("https:");
123 ASSERT_FALSE(invalid_url.is_valid());
124 EXPECT_FALSE(HandleNonNavigationAboutURL(invalid_url));