chrome_resource_dispatcher_host_delegate_browsertest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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. 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 25using content::ResourceType; 26 27namespace { 28static const char kTestPolicyHeader[] = "test_header"; 29static const char kServerRedirectUrl[] = "/server-redirect"; 30 31scoped_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); 38 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.PassAs<net::test_server::HttpResponse>(); 44 } else { 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.PassAs<net::test_server::HttpResponse>(); 50 } 51} 52 53class TestDispatcherHostDelegate : public ChromeResourceDispatcherHostDelegate { 54 public: 55 explicit TestDispatcherHostDelegate( 56 prerender::PrerenderTracker* prerender_tracker) 57 : ChromeResourceDispatcherHostDelegate(prerender_tracker) { 58 } 59 60 virtual ~TestDispatcherHostDelegate() {} 61 62 virtual void RequestBeginning( 63 net::URLRequest* request, 64 content::ResourceContext* resource_context, 65 content::AppCacheService* appcache_service, 66 ResourceType resource_type, 67 int child_id, 68 int route_id, 69 ScopedVector<content::ResourceThrottle>* throttles) OVERRIDE { 70 ChromeResourceDispatcherHostDelegate::RequestBeginning( 71 request, 72 resource_context, 73 appcache_service, 74 resource_type, 75 child_id, 76 route_id, 77 throttles); 78 request_headers_.MergeFrom(request->extra_request_headers()); 79 } 80 81 virtual void OnRequestRedirected( 82 const GURL& redirect_url, 83 net::URLRequest* request, 84 content::ResourceContext* resource_context, 85 content::ResourceResponse* response) OVERRIDE { 86 ChromeResourceDispatcherHostDelegate::OnRequestRedirected( 87 redirect_url, 88 request, 89 resource_context, 90 response); 91 request_headers_.MergeFrom(request->extra_request_headers()); 92 } 93 94 net::HttpRequestHeaders request_headers_; 95 96 private: 97 DISALLOW_COPY_AND_ASSIGN(TestDispatcherHostDelegate); 98}; 99 100} // namespace 101 102class ChromeResourceDispatcherHostDelegateBrowserTest : 103 public InProcessBrowserTest { 104 public: 105 ChromeResourceDispatcherHostDelegateBrowserTest() {} 106 107 virtual void SetUpOnMainThread() OVERRIDE { 108 InProcessBrowserTest::SetUpOnMainThread(); 109 // Hook navigations with our delegate. 110 dispatcher_host_delegate_.reset(new TestDispatcherHostDelegate( 111 g_browser_process->prerender_tracker())); 112 content::ResourceDispatcherHost::Get()->SetDelegate( 113 dispatcher_host_delegate_.get()); 114 115 embedded_test_server()->RegisterRequestHandler( 116 base::Bind(&HandleTestRequest)); 117 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 118 // Tell chrome that this is our DM server. 119 dm_url_ = embedded_test_server()->GetURL("/DeviceManagement"); 120 121 // At this point, the Profile is already initialized and it's too 122 // late to set the DMServer URL via command line flags, so directly 123 // inject it to the PolicyHeaderIOHelper. 124 policy::PolicyHeaderService* policy_header_service = 125 policy::PolicyHeaderServiceFactory::GetForBrowserContext( 126 browser()->profile()); 127 std::vector<policy::PolicyHeaderIOHelper*> helpers = 128 policy_header_service->GetHelpersForTest(); 129 for (std::vector<policy::PolicyHeaderIOHelper*>::const_iterator it = 130 helpers.begin(); 131 it != helpers.end(); ++it) { 132 (*it)->SetServerURLForTest(dm_url_.spec()); 133 (*it)->UpdateHeader(kTestPolicyHeader); 134 } 135 } 136 137 virtual void TearDownOnMainThread() OVERRIDE { 138 content::ResourceDispatcherHost::Get()->SetDelegate(NULL); 139 dispatcher_host_delegate_.reset(); 140 } 141 142 protected: 143 // The fake URL for DMServer we are using. 144 GURL dm_url_; 145 scoped_ptr<TestDispatcherHostDelegate> dispatcher_host_delegate_; 146 147 private: 148 DISALLOW_COPY_AND_ASSIGN(ChromeResourceDispatcherHostDelegateBrowserTest); 149}; 150 151 152IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, 153 NoPolicyHeader) { 154 // When fetching non-DMServer URLs, we should not add a policy header to the 155 // request. 156 DCHECK(!embedded_test_server()->base_url().spec().empty()); 157 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->base_url()); 158 ASSERT_FALSE(dispatcher_host_delegate_->request_headers_.HasHeader( 159 policy::kChromePolicyHeader)); 160} 161 162IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, 163 PolicyHeader) { 164 // When fetching a DMServer URL, we should add a policy header to the 165 // request. 166 ui_test_utils::NavigateToURL(browser(), dm_url_); 167 std::string value; 168 ASSERT_TRUE(dispatcher_host_delegate_->request_headers_.GetHeader( 169 policy::kChromePolicyHeader, &value)); 170 ASSERT_EQ(kTestPolicyHeader, value); 171} 172 173IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest, 174 PolicyHeaderForRedirect) { 175 176 // Build up a URL that results in a redirect to the DMServer URL to make 177 // sure the policy header is still added. 178 std::string redirect_url; 179 redirect_url += kServerRedirectUrl; 180 redirect_url += "?"; 181 redirect_url += dm_url_.spec(); 182 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL( 183 redirect_url)); 184 std::string value; 185 ASSERT_TRUE(dispatcher_host_delegate_->request_headers_.GetHeader( 186 policy::kChromePolicyHeader, &value)); 187 ASSERT_EQ(kTestPolicyHeader, value); 188} 189