OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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. | |
4 | |
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" | |
24 | |
25 namespace { | |
26 static const char kTestPolicyHeader[] = "test_header"; | |
27 static const char kServerRedirectUrl[] = "/server-redirect"; | |
28 | |
29 scoped_ptr<net::test_server::HttpResponse> HandleTestRequest( | |
30 const net::test_server::HttpRequest& request) { | |
31 if (StartsWithASCII(request.relative_url, kServerRedirectUrl, true)) { | |
32 // Extract the target URL and redirect there. | |
33 size_t query_string_pos = request.relative_url.find('?'); | |
34 std::string redirect_target = | |
35 request.relative_url.substr(query_string_pos + 1); | |
36 | |
37 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | |
38 new net::test_server::BasicHttpResponse); | |
39 http_response->set_code(net::HTTP_MOVED_PERMANENTLY); | |
40 http_response->AddCustomHeader("Location", redirect_target); | |
41 return http_response.PassAs<net::test_server::HttpResponse>(); | |
42 } else { | |
43 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | |
44 new net::test_server::BasicHttpResponse); | |
45 http_response->set_code(net::HTTP_OK); | |
46 http_response->set_content("Success"); | |
47 return http_response.PassAs<net::test_server::HttpResponse>(); | |
48 } | |
49 } | |
50 | |
51 class TestDispatcherHostDelegate : public ChromeResourceDispatcherHostDelegate { | |
52 public: | |
53 TestDispatcherHostDelegate( | |
jochen (gone - plz use gerrit)
2014/06/06 07:18:39
explicit
Andrew T Wilson (Slow)
2014/06/06 08:42:19
Done.
| |
54 prerender::PrerenderTracker* prerender_tracker) | |
55 : ChromeResourceDispatcherHostDelegate(prerender_tracker) { | |
56 } | |
57 | |
58 virtual ~TestDispatcherHostDelegate() {} | |
59 | |
60 virtual void RequestBeginning( | |
61 net::URLRequest* request, | |
62 content::ResourceContext* resource_context, | |
63 appcache::AppCacheService* appcache_service, | |
64 ResourceType::Type resource_type, | |
65 int child_id, | |
66 int route_id, | |
67 ScopedVector<content::ResourceThrottle>* throttles) OVERRIDE { | |
68 ChromeResourceDispatcherHostDelegate::RequestBeginning( | |
69 request, | |
70 resource_context, | |
71 appcache_service, | |
72 resource_type, | |
73 child_id, | |
74 route_id, | |
75 throttles); | |
76 request_headers_.MergeFrom(request->extra_request_headers()); | |
77 } | |
78 | |
79 virtual void OnRequestRedirected( | |
80 const GURL& redirect_url, | |
81 net::URLRequest* request, | |
82 content::ResourceContext* resource_context, | |
83 content::ResourceResponse* response) OVERRIDE { | |
84 ChromeResourceDispatcherHostDelegate::OnRequestRedirected( | |
85 redirect_url, | |
86 request, | |
87 resource_context, | |
88 response); | |
89 request_headers_.MergeFrom(request->extra_request_headers()); | |
90 } | |
91 | |
92 net::HttpRequestHeaders request_headers_; | |
jochen (gone - plz use gerrit)
2014/06/06 07:18:39
should be private
Andrew T Wilson (Slow)
2014/06/06 08:42:19
Done.
| |
93 | |
94 DISALLOW_COPY_AND_ASSIGN(TestDispatcherHostDelegate); | |
95 }; | |
96 | |
97 } // namespace | |
98 | |
99 class ChromeResourceDispatcherHostDelegateBrowserTest : | |
100 public InProcessBrowserTest { | |
101 public: | |
102 | |
103 virtual void SetUpOnMainThread() OVERRIDE { | |
104 InProcessBrowserTest::SetUpOnMainThread(); | |
105 // Hook navigations with our delegate. | |
106 dispatcher_host_delegate_.reset(new TestDispatcherHostDelegate( | |
107 g_browser_process->prerender_tracker())); | |
108 content::ResourceDispatcherHost::Get()->SetDelegate( | |
109 dispatcher_host_delegate_.get()); | |
110 | |
111 embedded_test_server()->RegisterRequestHandler( | |
112 base::Bind(&HandleTestRequest)); | |
113 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
114 // Tell chrome that this is our DM server. | |
115 dm_url_ = embedded_test_server()->GetURL("/DeviceManagement"); | |
116 | |
117 // At this point, the Profile is already initialized and it's too | |
118 // late to set the DMServer URL via command line flags, so directly | |
119 // inject it to the PolicyHeaderIOHelper. | |
120 policy::PolicyHeaderService* policy_header_service = | |
121 policy::PolicyHeaderServiceFactory::GetForBrowserContext( | |
122 browser()->profile()); | |
123 std::vector<policy::PolicyHeaderIOHelper*> helpers = | |
124 policy_header_service->GetHelpersForTest(); | |
125 for (std::vector<policy::PolicyHeaderIOHelper*>::const_iterator it = | |
126 helpers.begin(); | |
127 it != helpers.end(); ++it) { | |
128 (*it)->SetServerURLForTest(dm_url_.spec()); | |
129 (*it)->UpdateHeader(kTestPolicyHeader); | |
130 } | |
131 } | |
132 | |
133 virtual void CleanUpOnMainThread() OVERRIDE { | |
134 dispatcher_host_delegate_.reset(); | |
135 InProcessBrowserTest::CleanUpOnMainThread(); | |
136 } | |
137 | |
138 // The fake URL for DMServer we are using. | |
139 GURL dm_url_; | |
jochen (gone - plz use gerrit)
2014/06/06 07:18:39
private
Andrew T Wilson (Slow)
2014/06/06 08:42:19
I use these in the tests below, so they must be pu
jochen (gone - plz use gerrit)
2014/06/06 13:43:50
tests inherit from their fixture, so protected sho
| |
140 scoped_ptr<TestDispatcherHostDelegate> dispatcher_host_delegate_; | |
141 }; | |
jochen (gone - plz use gerrit)
2014/06/06 07:18:39
disallow copy & assign
Andrew T Wilson (Slow)
2014/06/06 08:42:19
Done.
| |
142 | |
143 | |
144 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, | |
145 NoPolicyHeader) { | |
146 // When fetching non-DMServer URLs, we should not add a policy header to the | |
147 // request. | |
148 DCHECK(!embedded_test_server()->base_url().spec().empty()); | |
149 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->base_url()); | |
150 ASSERT_FALSE(dispatcher_host_delegate_->request_headers_.HasHeader( | |
151 policy::kChromePolicyHeader)); | |
152 } | |
153 | |
154 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, | |
155 PolicyHeader) { | |
156 // When fetching a DMServer URL, we should add a policy header to the | |
157 // request. | |
158 ui_test_utils::NavigateToURL(browser(), dm_url_); | |
159 std::string value; | |
160 ASSERT_TRUE(dispatcher_host_delegate_->request_headers_.GetHeader( | |
161 policy::kChromePolicyHeader, &value)); | |
162 ASSERT_EQ(kTestPolicyHeader, value); | |
163 } | |
164 | |
165 IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, | |
166 PolicyHeaderForRedirect) { | |
167 | |
168 // Build up a URL that results in a redirect to the DMServer URL to make | |
169 // sure the policy header is still added. | |
170 std::string redirect_url; | |
171 redirect_url += kServerRedirectUrl; | |
172 redirect_url += "?"; | |
173 redirect_url += dm_url_.spec(); | |
174 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL( | |
175 redirect_url)); | |
176 std::string value; | |
177 ASSERT_TRUE(dispatcher_host_delegate_->request_headers_.GetHeader( | |
178 policy::kChromePolicyHeader, &value)); | |
179 ASSERT_EQ(kTestPolicyHeader, value); | |
180 } | |
OLD | NEW |