Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Unified Diff: components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc

Issue 734263003: Move data reduction proxy logic out of chrome and android webview network delegate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing mmenke comments Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..69c354262505229e843347d9d7ab9ac2290f0e68
--- /dev/null
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc
@@ -0,0 +1,319 @@
+// 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 "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h"
+
+#include "base/bind.h"
+#include "base/message_loop/message_loop.h"
+#include "base/prefs/pref_registry_simple.h"
+#include "base/prefs/testing_pref_service.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/test/histogram_tester.h"
+#include "base/time/time.h"
+#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.h"
+#include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers_test_utils.h"
+#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params_test_utils.h"
+#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
+#include "net/http/http_response_headers.h"
+#include "net/url_request/url_request.h"
+#include "net/url_request/url_request_job_factory_impl.h"
+#include "net/url_request/url_request_test_job.h"
+#include "net/url_request/url_request_test_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace data_reduction_proxy {
+namespace {
+
+class TestNetworkDelegate : public net::NetworkDelegateImpl {
+ public:
+ TestNetworkDelegate() {
+ }
+
+ virtual ~TestNetworkDelegate() {
+ }
+};
+
+const char kChromeProxyHeader[] = "chrome-proxy";
+
+#if defined(OS_ANDROID)
bengr 2014/12/04 22:58:57 DataReductionProxyAuthRequestHandler should provid
megjablon 2014/12/05 22:24:20 You have to give the DataReductionProxyAuthRequest
bengr 2014/12/06 01:05:37 Why can't that live in the component?
+const Client kClient = Client::CHROME_ANDROID;
+#elif defined(OS_IOS)
+const Client kClient = Client::CHROME_IOS;
+#elif defined(OS_MACOSX)
+const Client kClient = Client::CHROME_MAC;
+#elif defined(OS_CHROMEOS)
+const Client kClient = Client::CHROME_CHROMEOS;
+#elif defined(OS_LINUX)
+const Client kClient = Client::CHROME_LINUX;
+#elif defined(OS_WIN)
+const Client kClient = Client::CHROME_WINDOWS;
+#elif defined(OS_FREEBSD)
+const Client kClient = Client::CHROME_FREEBSD;
+#elif defined(OS_OPENBSD)
+const Client kClient = Client::CHROME_OPENBSD;
+#elif defined(OS_SOLARIS)
+const Client kClient = Client::CHROME_SOLARIS;
+#elif defined(OS_QNX)
+const Client kClient = Client::CHROME_QNX;
+#else
+const Client kClient = Client::UNKNOWN;
+#endif
+
+} // namespace
+
+class DataReductionProxyNetworkDelegateTest : public testing::Test {
+ protected:
+ DataReductionProxyNetworkDelegateTest()
+ : loop_proxy_(base::MessageLoopProxy::current().get()),
bengr 2014/12/04 22:58:57 I think you should just be able to declare: base:
megjablon 2014/12/05 22:24:20 Done.
+ context_(true) {
+ context_.Init();
+
+ // The |test_job_factory_| takes ownership of the interceptor.
+ test_job_interceptor_ = new net::TestJobInterceptor();
+ EXPECT_TRUE(test_job_factory_.SetProtocolHandler(url::kHttpScheme,
+ test_job_interceptor_));
+
+ context_.set_job_factory(&test_job_factory_);
+ }
+
+ scoped_ptr<net::URLRequest> CreateURLRequest(
bengr 2014/12/04 22:58:58 Maybe rename as FetchURL
megjablon 2014/12/05 22:24:20 Done.
+ const GURL& url) {
bengr 2014/12/04 22:58:57 Move up a line.
megjablon 2014/12/05 22:24:20 Done.
+ scoped_ptr<net::URLRequest> fake_request = context_.CreateRequest(
+ url, net::IDLE, &delegate_, NULL);
+
+ // Create a test job that will fill in the given response headers for the
+ // |fake_request|.
+ scoped_refptr<net::URLRequestTestJob> test_job(
+ new net::URLRequestTestJob(fake_request.get(),
+ context_.network_delegate(),
+ std::string(), std::string(), true));
+
+ // Configure the interceptor to use the test job to handle the next request.
+ test_job_interceptor_->set_main_intercept_job(test_job.get());
+
+ fake_request->Start();
+ base::MessageLoop::current()->RunUntilIdle();
+
+ return fake_request.Pass();
+ }
+
+ scoped_ptr<net::URLRequest> CreateURLRequestWithResponseHeaders(
+ const GURL& url,
+ const std::string& raw_response_headers,
+ int64 response_content_length) {
+ scoped_ptr<net::URLRequest> fake_request = context_.CreateRequest(
+ url, net::IDLE, &delegate_, NULL);
+
+ // Create a test job that will fill in the given response headers for the
+ // |fake_request|.
+ scoped_refptr<net::URLRequestTestJob> test_job(
+ new net::URLRequestTestJob(fake_request.get(),
+ context_.network_delegate(),
+ raw_response_headers, std::string(), true));
+
+ // Configure the interceptor to use the test job to handle the next request.
+ test_job_interceptor_->set_main_intercept_job(test_job.get());
+
+ fake_request->set_received_response_content_length(response_content_length);
+
+ fake_request->Start();
+ base::MessageLoop::current()->RunUntilIdle();
bengr 2014/12/04 22:58:58 Factor out as much as you can from this function a
megjablon 2014/12/05 22:24:20 Done.
+
+ EXPECT_TRUE(fake_request->response_headers() != NULL);
+
+ return fake_request.Pass();
+ }
+
+ void SetDelegate(net::NetworkDelegate* delegate) {
bengr 2014/12/04 22:58:57 Rename: set_network_delegate()
megjablon 2014/12/05 22:24:20 Done.
+ network_delegate_ = delegate;
+ context_.set_network_delegate(network_delegate_);
+ }
+
+ // Required for base::MessageLoopProxy::current().
bengr 2014/12/04 22:58:57 Shouldn't these be private too?
megjablon 2014/12/05 22:24:20 I'm using loop_ to get the MessageLoopProxy in the
bengr 2014/12/06 01:05:37 Yes, please.
+ base::MessageLoopForUI loop_;
bengr 2014/12/04 22:58:57 Are you intending to run on UI or IO?
megjablon 2014/12/05 22:24:20 Done.
+ base::MessageLoopProxy* loop_proxy_;
+
+ private:
+ net::TestURLRequestContext context_;
+ net::TestDelegate delegate_;
+ // |test_job_interceptor_| is owned by |test_job_factory_|.
+ net::TestJobInterceptor* test_job_interceptor_;
+ net::URLRequestJobFactoryImpl test_job_factory_;
+
+ net::NetworkDelegate* network_delegate_;
+};
+
+TEST_F(DataReductionProxyNetworkDelegateTest, AuthenticationTest) {
+ scoped_ptr<TestDataReductionProxyParams> params;
+ params.reset(
+ new TestDataReductionProxyParams(
+ DataReductionProxyParams::kAllowed |
+ DataReductionProxyParams::kFallbackAllowed |
+ DataReductionProxyParams::kPromoAllowed,
+ TestDataReductionProxyParams::HAS_EVERYTHING &
+ ~TestDataReductionProxyParams::HAS_DEV_ORIGIN &
+ ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN));
+ // loop_proxy_ is just the current message loop. This means loop_proxy_
+ // is the network thread used by DataReductionProxyAuthRequestHandler.
+ DataReductionProxyAuthRequestHandler auth_handler(kClient,
+ params.get(),
+ loop_proxy_);
+
+ scoped_ptr<DataReductionProxyNetworkDelegate> network_delegate(
+ new DataReductionProxyNetworkDelegate(
+ scoped_ptr<net::NetworkDelegate>(new TestNetworkDelegate()),
+ params.get(), &auth_handler,
+ DataReductionProxyNetworkDelegate::ProxyConfigGetter()));
+
+ SetDelegate(network_delegate.get());
+ scoped_ptr<net::URLRequest> fake_request(
+ CreateURLRequest(GURL("http://www.google.com/")));
+
+ net::ProxyInfo data_reduction_proxy_info;
+ std::string data_reduction_proxy;
+ base::TrimString(params->DefaultOrigin(), "/", &data_reduction_proxy);
+ data_reduction_proxy_info.UseNamedProxy(data_reduction_proxy);
+
+ net::HttpRequestHeaders headers;
+ network_delegate->NotifyBeforeSendProxyHeaders(
+ fake_request.get(), data_reduction_proxy_info, &headers);
+
+ EXPECT_TRUE(headers.HasHeader(kChromeProxyHeader));
bengr 2014/12/04 22:58:57 You should probably also check that this header ha
megjablon 2014/12/05 22:24:20 Done.
+}
+
+TEST_F(DataReductionProxyNetworkDelegateTest, NetHistograms) {
+ const std::string kReceivedValidOCLHistogramName =
+ "Net.HttpContentLengthWithValidOCL";
+ const std::string kOriginalValidOCLHistogramName =
+ "Net.HttpOriginalContentLengthWithValidOCL";
+ const std::string kDifferenceValidOCLHistogramName =
+ "Net.HttpContentLengthDifferenceWithValidOCL";
+ const std::string kReceivedHistogramName = "Net.HttpContentLength";
+ const std::string kOriginalHistogramName = "Net.HttpOriginalContentLength";
+ const std::string kDifferenceHistogramName =
+ "Net.HttpContentLengthDifference";
+ const std::string kFreshnessLifetimeHistogramName =
+ "Net.HttpContentFreshnessLifetime";
+ const std::string kCacheableHistogramName = "Net.HttpContentLengthCacheable";
+ const std::string kCacheable4HoursHistogramName =
+ "Net.HttpContentLengthCacheable4Hours";
+ const std::string kCacheable24HoursHistogramName =
+ "Net.HttpContentLengthCacheable24Hours";
+ const int64 kResponseContentLength = 100;
+ const int64 kOriginalContentLength = 200;
+
+ base::HistogramTester histogram_tester;
+ scoped_ptr<DataReductionProxyNetworkDelegate> network_delegate(
+ new DataReductionProxyNetworkDelegate(
+ scoped_ptr<net::NetworkDelegate>(
+ new TestNetworkDelegate()), NULL, NULL,
+ DataReductionProxyNetworkDelegate::ProxyConfigGetter()));
+
+ SetDelegate(network_delegate.get());
+
+ std::string raw_headers =
+ "HTTP/1.1 200 OK\n"
+ "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
+ "Expires: Mon, 24 Nov 2014 12:45:26 GMT\n"
+ "Via: 1.1 Chrome-Compression-Proxy\n"
+ "x-original-content-length: " +
+ base::Int64ToString(kOriginalContentLength) + "\n";
+
+ HeadersToRaw(&raw_headers);
+
+ scoped_ptr<net::URLRequest> fake_request(
+ CreateURLRequestWithResponseHeaders(GURL("http://www.google.com/"),
+ raw_headers,
+ kResponseContentLength));
+
+ base::TimeDelta freshness_lifetime =
+ fake_request->response_info().headers->GetFreshnessLifetimes(
+ fake_request->response_info().response_time).freshness;
+
+ histogram_tester.ExpectUniqueSample(kReceivedValidOCLHistogramName,
+ kResponseContentLength, 1);
+ histogram_tester.ExpectUniqueSample(kOriginalValidOCLHistogramName,
+ kOriginalContentLength, 1);
+ histogram_tester.ExpectUniqueSample(
+ kDifferenceValidOCLHistogramName,
+ kOriginalContentLength - kResponseContentLength, 1);
+ histogram_tester.ExpectUniqueSample(kReceivedHistogramName,
+ kResponseContentLength, 1);
+ histogram_tester.ExpectUniqueSample(kOriginalHistogramName,
+ kOriginalContentLength, 1);
+ histogram_tester.ExpectUniqueSample(
+ kDifferenceHistogramName,
+ kOriginalContentLength - kResponseContentLength, 1);
+ histogram_tester.ExpectUniqueSample(kFreshnessLifetimeHistogramName,
+ freshness_lifetime.InSeconds(), 1);
+ histogram_tester.ExpectUniqueSample(kCacheableHistogramName,
+ kResponseContentLength, 1);
+ histogram_tester.ExpectUniqueSample(kCacheable4HoursHistogramName,
+ kResponseContentLength, 1);
+ histogram_tester.ExpectUniqueSample(kCacheable24HoursHistogramName,
+ kResponseContentLength, 1);
+}
+
+class DataReductionProxyHistoricNetworkStatsTest
+ : public testing::Test {
+ public:
+ DataReductionProxyHistoricNetworkStatsTest() {}
+
+ void SetUp() override {
+ simple_pref_service_.registry()->RegisterInt64Pref(
+ prefs::kHttpReceivedContentLength, 0);
+ simple_pref_service_.registry()->RegisterInt64Pref(
+ prefs::kHttpOriginalContentLength, 0);
+ }
+
+ // Verify the pref values in |dict| are equal to that in
+ // |simple_pref_service|.
+ void VerifyPrefs(base::DictionaryValue* dict) {
+ base::string16 dict_pref_string;
+ int64 dict_pref;
+ int64 service_pref;
+
+ dict->GetString("historic_original_content_length", &dict_pref_string);
+ base::StringToInt64(dict_pref_string, &dict_pref);
+ service_pref = simple_pref_service_.GetInt64(
+ prefs::kHttpOriginalContentLength);
+ EXPECT_EQ(service_pref, dict_pref);
+
+ dict->GetString("historic_received_content_length", &dict_pref_string);
+ base::StringToInt64(dict_pref_string, &dict_pref);
+ service_pref = simple_pref_service_.GetInt64(
+ prefs::kHttpReceivedContentLength);
+ EXPECT_EQ(service_pref, dict_pref);
+ }
+
+ TestingPrefServiceSimple simple_pref_service_;
+};
+
+TEST_F(DataReductionProxyHistoricNetworkStatsTest,
+ HistoricNetworkStatsInfoToValue) {
+ const int64 kOriginalLength = 150;
+ const int64 kReceivedLength = 100;
+
+ base::DictionaryValue* dict = NULL;
bengr 2014/12/04 22:58:57 use nullptr
megjablon 2014/12/05 22:24:20 Done.
+ base::Value* stats_value =
bengr 2014/12/04 22:58:58 Use a scoped_ptr here so you don't have to remembe
megjablon 2014/12/05 22:24:20 Done.
+ DataReductionProxyNetworkDelegate::HistoricNetworkStatsInfoToValue(
+ &simple_pref_service_);
+ EXPECT_TRUE(stats_value->GetAsDictionary(&dict));
+ VerifyPrefs(dict);
+
+ simple_pref_service_.SetInt64(prefs::kHttpOriginalContentLength,
+ kOriginalLength);
+ simple_pref_service_.SetInt64(prefs::kHttpReceivedContentLength,
+ kReceivedLength);
+
+ stats_value =
+ DataReductionProxyNetworkDelegate::HistoricNetworkStatsInfoToValue(
+ &simple_pref_service_);
+ EXPECT_TRUE(stats_value->GetAsDictionary(&dict));
+ VerifyPrefs(dict);
+
+ delete stats_value;
+}
+
+} // namespace data_reduction_proxy

Powered by Google App Engine
This is Rietveld 408576698