1 // Copyright 2014 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 "content/public/browser/resource_request_details.h"
6 #include "content/public/test/browser_test_utils.h"
7 #include "content/public/test/content_browser_test.h"
8 #include "content/public/test/content_browser_test_utils.h"
9 #include "content/shell/browser/shell.h"
10 #include "net/dns/mock_host_resolver.h"
11 #include "net/test/embedded_test_server/embedded_test_server.h"
15 class NavigationObserver
: public WebContentsObserver
{
17 explicit NavigationObserver(WebContents
* web_contents
)
18 : WebContentsObserver(web_contents
) {}
19 ~NavigationObserver() override
{}
21 void DidCommitProvisionalLoadForFrame(
22 RenderFrameHost
* render_frame_host
,
24 ui::PageTransition transition_type
) override
{
25 navigation_url_
= url
;
28 void DidGetRedirectForResourceRequest(
29 RenderFrameHost
* render_frame_host
,
30 const ResourceRedirectDetails
& details
) override
{
31 redirect_url_
= details
.new_url
;
34 const GURL
& navigation_url() const {
35 return navigation_url_
;
38 const GURL
& redirect_url() const {
46 DISALLOW_COPY_AND_ASSIGN(NavigationObserver
);
49 class CrossSiteRedirectorBrowserTest
: public ContentBrowserTest
{
51 CrossSiteRedirectorBrowserTest() {}
54 IN_PROC_BROWSER_TEST_F(CrossSiteRedirectorBrowserTest
,
55 VerifyCrossSiteRedirectURL
) {
56 // Map all hosts to localhost and setup the EmbeddedTestServer for redirects.
57 host_resolver()->AddRule("*", "127.0.0.1");
58 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
59 SetupCrossSiteRedirector(embedded_test_server());
61 // Navigate to http://localhost:<port>/cross-site/foo.com/title2.html and
62 // ensure that the redirector forwards the navigation to
63 // http://foo.com:<port>/title2.html
64 NavigationObserver
observer(shell()->web_contents());
67 embedded_test_server()->GetURL("/cross-site/foo.com/title2.html"));
69 // The expectation is that the cross-site redirector will take the
70 // hostname supplied in the URL and rewrite the URL. Build the
71 // expected URL to ensure navigation was properly redirected.
72 GURL::Replacements replace_host
;
73 GURL
expected_url(embedded_test_server()->GetURL("/title2.html"));
74 replace_host
.SetHostStr("foo.com");
75 expected_url
= expected_url
.ReplaceComponents(replace_host
);
77 EXPECT_EQ(expected_url
, observer
.navigation_url());
78 EXPECT_EQ(observer
.redirect_url(), observer
.navigation_url());
81 } // namespace content