| Index: chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate_browsertest.cc
|
| diff --git a/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate_browsertest.cc b/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate_browsertest.cc
|
| index 1026d5c4169b1513260c8943de3b81359d5debdb..57388aaed81d6c3ad32f7421455e84acb653a35b 100644
|
| --- a/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate_browsertest.cc
|
| +++ b/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate_browsertest.cc
|
| @@ -1,33 +1,42 @@
|
| // Copyright 2014 The Chromium Authors. All rights reserved.
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.h"
|
|
|
| #include <stddef.h>
|
| +
|
| +#include <memory>
|
| #include <utility>
|
|
|
| #include "base/command_line.h"
|
| #include "base/macros.h"
|
| #include "base/strings/string_util.h"
|
| #include "chrome/browser/browser_process.h"
|
| #include "chrome/browser/policy/cloud/policy_header_service_factory.h"
|
| #include "chrome/browser/profiles/profile.h"
|
| +#include "chrome/browser/renderer_host/chrome_navigation_data.h"
|
| #include "chrome/browser/ui/browser.h"
|
| +#include "chrome/browser/ui/tabs/tab_strip_model.h"
|
| #include "chrome/test/base/in_process_browser_test.h"
|
| #include "chrome/test/base/ui_test_utils.h"
|
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data.h"
|
| #include "components/policy/core/common/cloud/cloud_policy_constants.h"
|
| #include "components/policy/core/common/cloud/policy_header_io_helper.h"
|
| #include "components/policy/core/common/cloud/policy_header_service.h"
|
| #include "components/policy/core/common/policy_switches.h"
|
| +#include "content/public/browser/navigation_data.h"
|
| +#include "content/public/browser/navigation_handle.h"
|
| #include "content/public/browser/resource_dispatcher_host.h"
|
| +#include "content/public/browser/web_contents_observer.h"
|
| +#include "content/public/test/browser_test_utils.h"
|
| #include "net/http/http_request_headers.h"
|
| #include "net/test/embedded_test_server/embedded_test_server.h"
|
| #include "net/test/embedded_test_server/http_request.h"
|
| #include "net/test/embedded_test_server/http_response.h"
|
| #include "net/url_request/url_request.h"
|
|
|
| using content::ResourceType;
|
|
|
| namespace {
|
| static const char kTestPolicyHeader[] = "test_header";
|
| @@ -51,21 +60,21 @@ std::unique_ptr<net::test_server::HttpResponse> HandleTestRequest(
|
| std::unique_ptr<net::test_server::BasicHttpResponse> http_response(
|
| new net::test_server::BasicHttpResponse);
|
| http_response->set_code(net::HTTP_OK);
|
| http_response->set_content("Success");
|
| return std::move(http_response);
|
| }
|
| }
|
|
|
| class TestDispatcherHostDelegate : public ChromeResourceDispatcherHostDelegate {
|
| public:
|
| - TestDispatcherHostDelegate() {}
|
| + TestDispatcherHostDelegate() : should_add_data_reduction_proxy_data_(false) {}
|
| ~TestDispatcherHostDelegate() override {}
|
|
|
| void RequestBeginning(
|
| net::URLRequest* request,
|
| content::ResourceContext* resource_context,
|
| content::AppCacheService* appcache_service,
|
| ResourceType resource_type,
|
| ScopedVector<content::ResourceThrottle>* throttles) override {
|
| ChromeResourceDispatcherHostDelegate::RequestBeginning(
|
| request,
|
| @@ -81,26 +90,66 @@ class TestDispatcherHostDelegate : public ChromeResourceDispatcherHostDelegate {
|
| content::ResourceContext* resource_context,
|
| content::ResourceResponse* response) override {
|
| ChromeResourceDispatcherHostDelegate::OnRequestRedirected(
|
| redirect_url,
|
| request,
|
| resource_context,
|
| response);
|
| request_headers_.MergeFrom(request->extra_request_headers());
|
| }
|
|
|
| + content::NavigationData* GetNavigationData(
|
| + net::URLRequest* request) const override {
|
| + if (request && should_add_data_reduction_proxy_data_) {
|
| + data_reduction_proxy::DataReductionProxyData* data =
|
| + data_reduction_proxy::DataReductionProxyData::
|
| + GetDataAndCreateIfNecessary(request);
|
| + data->set_used_data_reduction_proxy(true);
|
| + }
|
| + return ChromeResourceDispatcherHostDelegate::GetNavigationData(request);
|
| + }
|
| +
|
| + bool should_add_data_reduction_proxy_data_;
|
| net::HttpRequestHeaders request_headers_;
|
|
|
| private:
|
| DISALLOW_COPY_AND_ASSIGN(TestDispatcherHostDelegate);
|
| };
|
|
|
| +// Helper class to track DidFinishNavigation and verify that NavigationData is
|
| +// added to NavigationHandle and pause/resume execution of the test.
|
| +class DidFinishNavigationObserver : public content::WebContentsObserver {
|
| + public:
|
| + DidFinishNavigationObserver(content::WebContents* web_contents,
|
| + bool add_data_reduction_proxy_data)
|
| + : content::WebContentsObserver(web_contents),
|
| + add_data_reduction_proxy_data_(add_data_reduction_proxy_data) {}
|
| + ~DidFinishNavigationObserver() override {}
|
| +
|
| + void DidFinishNavigation(
|
| + content::NavigationHandle* navigation_handle) override {
|
| + ChromeNavigationData* data = static_cast<ChromeNavigationData*>(
|
| + navigation_handle->GetNavigationData());
|
| + if (add_data_reduction_proxy_data_) {
|
| + EXPECT_TRUE(data->GetDataReductionProxyData());
|
| + EXPECT_TRUE(
|
| + data->GetDataReductionProxyData()->used_data_reduction_proxy());
|
| + } else {
|
| + EXPECT_FALSE(data->GetDataReductionProxyData());
|
| + }
|
| + }
|
| +
|
| + private:
|
| + bool add_data_reduction_proxy_data_;
|
| + DISALLOW_COPY_AND_ASSIGN(DidFinishNavigationObserver);
|
| +};
|
| +
|
| } // namespace
|
|
|
| class ChromeResourceDispatcherHostDelegateBrowserTest :
|
| public InProcessBrowserTest {
|
| public:
|
| ChromeResourceDispatcherHostDelegateBrowserTest() {}
|
|
|
| void SetUpOnMainThread() override {
|
| InProcessBrowserTest::SetUpOnMainThread();
|
| // Hook navigations with our delegate.
|
| @@ -128,25 +177,28 @@ class ChromeResourceDispatcherHostDelegateBrowserTest :
|
| (*it)->SetServerURLForTest(dm_url_.spec());
|
| (*it)->UpdateHeader(kTestPolicyHeader);
|
| }
|
| }
|
|
|
| void TearDownOnMainThread() override {
|
| content::ResourceDispatcherHost::Get()->SetDelegate(NULL);
|
| dispatcher_host_delegate_.reset();
|
| }
|
|
|
| + void SetShouldAddDataReductionProxyData(bool add_data) {
|
| + dispatcher_host_delegate_->should_add_data_reduction_proxy_data_ = add_data;
|
| + }
|
| +
|
| protected:
|
| // The fake URL for DMServer we are using.
|
| GURL dm_url_;
|
| std::unique_ptr<TestDispatcherHostDelegate> dispatcher_host_delegate_;
|
| -
|
| private:
|
| DISALLOW_COPY_AND_ASSIGN(ChromeResourceDispatcherHostDelegateBrowserTest);
|
| };
|
|
|
|
|
| IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest,
|
| NoPolicyHeader) {
|
| // When fetching non-DMServer URLs, we should not add a policy header to the
|
| // request.
|
| DCHECK(!embedded_test_server()->base_url().spec().empty());
|
| @@ -175,10 +227,27 @@ IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest,
|
| redirect_url += kServerRedirectUrl;
|
| redirect_url += "?";
|
| redirect_url += dm_url_.spec();
|
| ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL(
|
| redirect_url));
|
| std::string value;
|
| ASSERT_TRUE(dispatcher_host_delegate_->request_headers_.GetHeader(
|
| policy::kChromePolicyHeader, &value));
|
| ASSERT_EQ(kTestPolicyHeader, value);
|
| }
|
| +
|
| +IN_PROC_BROWSER_TEST_F(ChromeResourceDispatcherHostDelegateBrowserTest,
|
| + NavigationDataProcessed) {
|
| + ui_test_utils::NavigateToURL(browser(), embedded_test_server()->base_url());
|
| + {
|
| + DidFinishNavigationObserver nav_observer(
|
| + browser()->tab_strip_model()->GetActiveWebContents(), false);
|
| + ui_test_utils::NavigateToURL(
|
| + browser(), embedded_test_server()->GetURL("/google/google.html"));
|
| + }
|
| + SetShouldAddDataReductionProxyData(true);
|
| + {
|
| + DidFinishNavigationObserver nav_observer(
|
| + browser()->tab_strip_model()->GetActiveWebContents(), true);
|
| + ui_test_utils::NavigateToURL(browser(), embedded_test_server()->base_url());
|
| + }
|
| +}
|
|
|