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 (base::StartsWith(request
.relative_url
, kServerRedirectUrl
,
34 base::CompareCase::SENSITIVE
)) {
35 // Extract the target URL and redirect there.
36 size_t query_string_pos
= request
.relative_url
.find('?');
37 std::string redirect_target
=
38 request
.relative_url
.substr(query_string_pos
+ 1);
40 scoped_ptr
<net::test_server::BasicHttpResponse
> http_response(
41 new net::test_server::BasicHttpResponse
);
42 http_response
->set_code(net::HTTP_MOVED_PERMANENTLY
);
43 http_response
->AddCustomHeader("Location", redirect_target
);
44 return http_response
.Pass();
46 scoped_ptr
<net::test_server::BasicHttpResponse
> http_response(
47 new net::test_server::BasicHttpResponse
);
48 http_response
->set_code(net::HTTP_OK
);
49 http_response
->set_content("Success");
50 return http_response
.Pass();
54 class TestDispatcherHostDelegate
: public ChromeResourceDispatcherHostDelegate
{
56 TestDispatcherHostDelegate() {}
57 ~TestDispatcherHostDelegate() override
{}
59 void RequestBeginning(
60 net::URLRequest
* request
,
61 content::ResourceContext
* resource_context
,
62 content::AppCacheService
* appcache_service
,
63 ResourceType resource_type
,
64 ScopedVector
<content::ResourceThrottle
>* throttles
) override
{
65 ChromeResourceDispatcherHostDelegate::RequestBeginning(
71 request_headers_
.MergeFrom(request
->extra_request_headers());
74 void OnRequestRedirected(const GURL
& redirect_url
,
75 net::URLRequest
* request
,
76 content::ResourceContext
* resource_context
,
77 content::ResourceResponse
* response
) override
{
78 ChromeResourceDispatcherHostDelegate::OnRequestRedirected(
83 request_headers_
.MergeFrom(request
->extra_request_headers());
86 net::HttpRequestHeaders request_headers_
;
89 DISALLOW_COPY_AND_ASSIGN(TestDispatcherHostDelegate
);
94 class ChromeResourceDispatcherHostDelegateBrowserTest
:
95 public InProcessBrowserTest
{
97 ChromeResourceDispatcherHostDelegateBrowserTest() {}
99 void SetUpOnMainThread() override
{
100 InProcessBrowserTest::SetUpOnMainThread();
101 // Hook navigations with our delegate.
102 dispatcher_host_delegate_
.reset(new TestDispatcherHostDelegate
);
103 content::ResourceDispatcherHost::Get()->SetDelegate(
104 dispatcher_host_delegate_
.get());
106 embedded_test_server()->RegisterRequestHandler(
107 base::Bind(&HandleTestRequest
));
108 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
109 // Tell chrome that this is our DM server.
110 dm_url_
= embedded_test_server()->GetURL("/DeviceManagement");
112 // At this point, the Profile is already initialized and it's too
113 // late to set the DMServer URL via command line flags, so directly
114 // inject it to the PolicyHeaderIOHelper.
115 policy::PolicyHeaderService
* policy_header_service
=
116 policy::PolicyHeaderServiceFactory::GetForBrowserContext(
117 browser()->profile());
118 std::vector
<policy::PolicyHeaderIOHelper
*> helpers
=
119 policy_header_service
->GetHelpersForTest();
120 for (std::vector
<policy::PolicyHeaderIOHelper
*>::const_iterator it
=
122 it
!= helpers
.end(); ++it
) {
123 (*it
)->SetServerURLForTest(dm_url_
.spec());
124 (*it
)->UpdateHeader(kTestPolicyHeader
);
128 void TearDownOnMainThread() override
{
129 content::ResourceDispatcherHost::Get()->SetDelegate(NULL
);
130 dispatcher_host_delegate_
.reset();
134 // The fake URL for DMServer we are using.
136 scoped_ptr
<TestDispatcherHostDelegate
> dispatcher_host_delegate_
;
139 DISALLOW_COPY_AND_ASSIGN(ChromeResourceDispatcherHostDelegateBrowserTest
);
143 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest
,
145 // When fetching non-DMServer URLs, we should not add a policy header to the
147 DCHECK(!embedded_test_server()->base_url().spec().empty());
148 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->base_url());
149 ASSERT_FALSE(dispatcher_host_delegate_
->request_headers_
.HasHeader(
150 policy::kChromePolicyHeader
));
153 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest
,
155 // When fetching a DMServer URL, we should add a policy header to the
157 ui_test_utils::NavigateToURL(browser(), dm_url_
);
159 ASSERT_TRUE(dispatcher_host_delegate_
->request_headers_
.GetHeader(
160 policy::kChromePolicyHeader
, &value
));
161 ASSERT_EQ(kTestPolicyHeader
, value
);
164 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest
,
165 PolicyHeaderForRedirect
) {
167 // Build up a URL that results in a redirect to the DMServer URL to make
168 // sure the policy header is still added.
169 std::string redirect_url
;
170 redirect_url
+= kServerRedirectUrl
;
172 redirect_url
+= dm_url_
.spec();
173 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL(
176 ASSERT_TRUE(dispatcher_host_delegate_
->request_headers_
.GetHeader(
177 policy::kChromePolicyHeader
, &value
));
178 ASSERT_EQ(kTestPolicyHeader
, value
);