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