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 "base/command_line.h"
6 #include "base/strings/string_util.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/policy/cloud/policy_header_service_factory.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
15 #include "components/policy/core/common/cloud/policy_header_io_helper.h"
16 #include "components/policy/core/common/cloud/policy_header_service.h"
17 #include "components/policy/core/common/policy_switches.h"
18 #include "content/public/browser/resource_dispatcher_host.h"
19 #include "net/http/http_request_headers.h"
20 #include "net/test/embedded_test_server/embedded_test_server.h"
21 #include "net/test/embedded_test_server/http_request.h"
22 #include "net/test/embedded_test_server/http_response.h"
23 #include "net/url_request/url_request.h"
25 using content::ResourceType
;
28 static const char kTestPolicyHeader
[] = "test_header";
29 static const char kServerRedirectUrl
[] = "/server-redirect";
31 scoped_ptr
<net::test_server::HttpResponse
> HandleTestRequest(
32 const net::test_server::HttpRequest
& request
) {
33 if (StartsWithASCII(request
.relative_url
, kServerRedirectUrl
, true)) {
34 // Extract the target URL and redirect there.
35 size_t query_string_pos
= request
.relative_url
.find('?');
36 std::string redirect_target
=
37 request
.relative_url
.substr(query_string_pos
+ 1);
39 scoped_ptr
<net::test_server::BasicHttpResponse
> http_response(
40 new net::test_server::BasicHttpResponse
);
41 http_response
->set_code(net::HTTP_MOVED_PERMANENTLY
);
42 http_response
->AddCustomHeader("Location", redirect_target
);
43 return http_response
.Pass();
45 scoped_ptr
<net::test_server::BasicHttpResponse
> http_response(
46 new net::test_server::BasicHttpResponse
);
47 http_response
->set_code(net::HTTP_OK
);
48 http_response
->set_content("Success");
49 return http_response
.Pass();
53 class TestDispatcherHostDelegate
: public ChromeResourceDispatcherHostDelegate
{
55 TestDispatcherHostDelegate() {}
56 ~TestDispatcherHostDelegate() override
{}
58 void RequestBeginning(
59 net::URLRequest
* request
,
60 content::ResourceContext
* resource_context
,
61 content::AppCacheService
* appcache_service
,
62 ResourceType resource_type
,
63 ScopedVector
<content::ResourceThrottle
>* throttles
) override
{
64 ChromeResourceDispatcherHostDelegate::RequestBeginning(
70 request_headers_
.MergeFrom(request
->extra_request_headers());
73 void OnRequestRedirected(const GURL
& redirect_url
,
74 net::URLRequest
* request
,
75 content::ResourceContext
* resource_context
,
76 content::ResourceResponse
* response
) override
{
77 ChromeResourceDispatcherHostDelegate::OnRequestRedirected(
82 request_headers_
.MergeFrom(request
->extra_request_headers());
85 net::HttpRequestHeaders request_headers_
;
88 DISALLOW_COPY_AND_ASSIGN(TestDispatcherHostDelegate
);
93 class ChromeResourceDispatcherHostDelegateBrowserTest
:
94 public InProcessBrowserTest
{
96 ChromeResourceDispatcherHostDelegateBrowserTest() {}
98 void SetUpOnMainThread() override
{
99 InProcessBrowserTest::SetUpOnMainThread();
100 // Hook navigations with our delegate.
101 dispatcher_host_delegate_
.reset(new TestDispatcherHostDelegate
);
102 content::ResourceDispatcherHost::Get()->SetDelegate(
103 dispatcher_host_delegate_
.get());
105 embedded_test_server()->RegisterRequestHandler(
106 base::Bind(&HandleTestRequest
));
107 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
108 // Tell chrome that this is our DM server.
109 dm_url_
= embedded_test_server()->GetURL("/DeviceManagement");
111 // At this point, the Profile is already initialized and it's too
112 // late to set the DMServer URL via command line flags, so directly
113 // inject it to the PolicyHeaderIOHelper.
114 policy::PolicyHeaderService
* policy_header_service
=
115 policy::PolicyHeaderServiceFactory::GetForBrowserContext(
116 browser()->profile());
117 std::vector
<policy::PolicyHeaderIOHelper
*> helpers
=
118 policy_header_service
->GetHelpersForTest();
119 for (std::vector
<policy::PolicyHeaderIOHelper
*>::const_iterator it
=
121 it
!= helpers
.end(); ++it
) {
122 (*it
)->SetServerURLForTest(dm_url_
.spec());
123 (*it
)->UpdateHeader(kTestPolicyHeader
);
127 void TearDownOnMainThread() override
{
128 content::ResourceDispatcherHost::Get()->SetDelegate(NULL
);
129 dispatcher_host_delegate_
.reset();
133 // The fake URL for DMServer we are using.
135 scoped_ptr
<TestDispatcherHostDelegate
> dispatcher_host_delegate_
;
138 DISALLOW_COPY_AND_ASSIGN(ChromeResourceDispatcherHostDelegateBrowserTest
);
142 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest
,
144 // When fetching non-DMServer URLs, we should not add a policy header to the
146 DCHECK(!embedded_test_server()->base_url().spec().empty());
147 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->base_url());
148 ASSERT_FALSE(dispatcher_host_delegate_
->request_headers_
.HasHeader(
149 policy::kChromePolicyHeader
));
152 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest
,
154 // When fetching a DMServer URL, we should add a policy header to the
156 ui_test_utils::NavigateToURL(browser(), dm_url_
);
158 ASSERT_TRUE(dispatcher_host_delegate_
->request_headers_
.GetHeader(
159 policy::kChromePolicyHeader
, &value
));
160 ASSERT_EQ(kTestPolicyHeader
, value
);
163 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest
,
164 PolicyHeaderForRedirect
) {
166 // Build up a URL that results in a redirect to the DMServer URL to make
167 // sure the policy header is still added.
168 std::string redirect_url
;
169 redirect_url
+= kServerRedirectUrl
;
171 redirect_url
+= dm_url_
.spec();
172 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL(
175 ASSERT_TRUE(dispatcher_host_delegate_
->request_headers_
.GetHeader(
176 policy::kChromePolicyHeader
, &value
));
177 ASSERT_EQ(kTestPolicyHeader
, value
);