Index: components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc |
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc |
index d84886a59173a7d3343357781354c71d3b372f99..80f26a9dbe3fd906957f8fcd539251abbb92a3f3 100644 |
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc |
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc |
@@ -13,6 +13,7 @@ |
#include "base/run_loop.h" |
#include "base/stl_util.h" |
#include "base/strings/stringprintf.h" |
+#include "base/test/histogram_tester.h" |
#include "base/test/mock_entropy_provider.h" |
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.h" |
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.h" |
@@ -84,16 +85,7 @@ class DataReductionProxyProtocolTest : public testing::Test { |
} |
void SetUp() override { |
- test_context_ = |
- DataReductionProxyTestContext::Builder() |
- .WithParamsFlags(DataReductionProxyParams::kAllowed | |
- DataReductionProxyParams::kFallbackAllowed | |
- DataReductionProxyParams::kPromoAllowed) |
- .WithParamsDefinitions( |
- TestDataReductionProxyParams::HAS_EVERYTHING & |
- ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & |
- ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN) |
- .Build(); |
+ test_context_ = DataReductionProxyTestContext::Builder().Build(); |
network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); |
net::NetworkChangeNotifier::SetTestNotificationsOnly(true); |
test_context_->RunUntilIdle(); |
@@ -774,10 +766,48 @@ TEST_F(DataReductionProxyProtocolTest, BypassLogic) { |
} |
} |
-TEST(DataReductionProxyProtocolStandaloneTest, |
- BypassLogicAlwaysAppliesWhenViaHeaderPresent) { |
- base::MessageLoopForIO message_loop; |
+class DataReductionProxyBypassProtocolEndToEndTest : public testing::Test { |
+ public: |
+ DataReductionProxyBypassProtocolEndToEndTest() {} |
+ |
+ void ResetDependencies() { |
+ context_.reset(new net::TestURLRequestContext(true)); |
+ storage_.reset(new net::URLRequestContextStorage(context_.get())); |
+ mock_socket_factory_.reset(new net::MockClientSocketFactory()); |
+ context_->set_client_socket_factory(mock_socket_factory_.get()); |
+ drp_test_context_ = |
+ DataReductionProxyTestContext::Builder() |
+ .WithMockClientSocketFactory(mock_socket_factory_.get()) |
+ .WithURLRequestContext(context_.get()) |
+ .Build(); |
+ } |
+ |
+ void AttachToContextAndInit() { |
+ drp_test_context_->AttachToURLRequestContext(storage_.get()); |
+ context_->Init(); |
+ } |
+ |
+ net::TestURLRequestContext* context() { return context_.get(); } |
+ net::URLRequestContextStorage* storage() { return storage_.get(); } |
+ net::MockClientSocketFactory* mock_socket_factory() { |
+ return mock_socket_factory_.get(); |
+ } |
+ DataReductionProxyTestContext* drp_test_context() { |
+ return drp_test_context_.get(); |
+ } |
+ |
+ private: |
+ base::MessageLoopForIO loop_; |
+ scoped_ptr<net::TestURLRequestContext> context_; |
+ scoped_ptr<net::URLRequestContextStorage> storage_; |
+ scoped_ptr<net::MockClientSocketFactory> mock_socket_factory_; |
+ scoped_ptr<DataReductionProxyTestContext> drp_test_context_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(DataReductionProxyBypassProtocolEndToEndTest); |
+}; |
+TEST_F(DataReductionProxyBypassProtocolEndToEndTest, |
+ BypassLogicAlwaysAppliesWhenViaHeaderPresent) { |
const struct { |
const char* first_response; |
bool expected_retry; |
@@ -820,41 +850,21 @@ TEST(DataReductionProxyProtocolStandaloneTest, |
for (const auto& test : test_cases) { |
const std::string kPrimary = "https://unrecognized-drp.net:443"; |
- net::TestURLRequestContext context(true); |
- net::URLRequestContextStorage storage(&context); |
- |
- net::MockClientSocketFactory mock_socket_factory; |
- context.set_client_socket_factory(&mock_socket_factory); |
- |
- storage.set_proxy_service( |
+ ResetDependencies(); |
+ storage()->set_proxy_service( |
ProxyService::CreateFixed(kPrimary + ",direct://")); |
- |
- scoped_ptr<DataReductionProxyTestContext> drp_test_context( |
- DataReductionProxyTestContext::Builder() |
- .WithParamsFlags(DataReductionProxyParams::kAllowed | |
- DataReductionProxyParams::kFallbackAllowed) |
- .WithParamsDefinitions( |
- TestDataReductionProxyParams::HAS_EVERYTHING & |
- ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & |
- ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN) |
- .WithMockClientSocketFactory(&mock_socket_factory) |
- .WithURLRequestContext(&context) |
- .Build()); |
- drp_test_context->AttachToURLRequestContext(&storage); |
- context.Init(); |
- |
- drp_test_context->EnableDataReductionProxyWithSecureProxyCheckSuccess(); |
+ AttachToContextAndInit(); |
// The proxy is an HTTPS proxy, so set up the fake SSL socket data. |
net::SSLSocketDataProvider ssl_socket(net::ASYNC, net::OK); |
- mock_socket_factory.AddSSLSocketDataProvider(&ssl_socket); |
+ mock_socket_factory()->AddSSLSocketDataProvider(&ssl_socket); |
MockRead first_reads[] = {MockRead(test.first_response), |
MockRead(""), |
MockRead(net::SYNCHRONOUS, net::OK)}; |
net::StaticSocketDataProvider first_socket( |
first_reads, arraysize(first_reads), nullptr, 0); |
- mock_socket_factory.AddSocketDataProvider(&first_socket); |
+ mock_socket_factory()->AddSocketDataProvider(&first_socket); |
MockRead retry_reads[] = {MockRead("HTTP/1.1 200 OK\n\r\n\r"), |
MockRead(""), |
@@ -862,20 +872,76 @@ TEST(DataReductionProxyProtocolStandaloneTest, |
net::StaticSocketDataProvider retry_socket( |
retry_reads, arraysize(retry_reads), nullptr, 0); |
if (test.expected_retry) |
- mock_socket_factory.AddSocketDataProvider(&retry_socket); |
+ mock_socket_factory()->AddSocketDataProvider(&retry_socket); |
net::TestDelegate delegate; |
- scoped_ptr<net::URLRequest> url_request(context.CreateRequest( |
+ scoped_ptr<net::URLRequest> url_request(context()->CreateRequest( |
GURL("http://www.google.com"), net::IDLE, &delegate)); |
url_request->Start(); |
- drp_test_context->RunUntilIdle(); |
+ drp_test_context()->RunUntilIdle(); |
EXPECT_EQ(test.expected_bypass_type, |
- drp_test_context->io_data()->bypass_stats()->GetBypassType()); |
+ drp_test_context()->io_data()->bypass_stats()->GetBypassType()); |
// Check the bad proxy list. |
EXPECT_EQ( |
test.expected_bad_proxy, |
- ContainsKey(context.proxy_service()->proxy_retry_info(), kPrimary)); |
+ ContainsKey(context()->proxy_service()->proxy_retry_info(), kPrimary)); |
+ } |
+} |
+ |
+TEST_F(DataReductionProxyBypassProtocolEndToEndTest, |
+ ResponseProxyServerStateHistogram) { |
+ const struct { |
+ const char* proxy_rules; |
+ bool enable_data_reduction_proxy; |
+ const char* response_headers; |
+ // |RESPONSE_PROXY_SERVER_STATUS_MAX| indicates no expected value. |
+ DataReductionProxyBypassProtocol::ResponseProxyServerStatus expected_status; |
+ } test_cases[] = { |
+ {"direct://", |
+ false, |
+ "HTTP/1.1 200 OK\r\n\r\n", |
+ DataReductionProxyBypassProtocol::RESPONSE_PROXY_SERVER_STATUS_EMPTY}, |
+ {"direct://", |
+ true, |
+ "HTTP/1.1 200 OK\r\nVia: 1.1 Chrome-Compression-Proxy\r\n\r\n", |
+ DataReductionProxyBypassProtocol::RESPONSE_PROXY_SERVER_STATUS_DRP}, |
+ {"unrecognized-drp.net", |
+ false, |
+ "HTTP/1.1 200 OK\r\n\r\n", |
+ DataReductionProxyBypassProtocol:: |
+ RESPONSE_PROXY_SERVER_STATUS_NON_DRP_NO_VIA}, |
+ {"unrecognized-drp.net", |
+ false, |
+ "HTTP/1.1 200 OK\r\nVia: 1.1 Chrome-Compression-Proxy\r\n\r\n", |
+ DataReductionProxyBypassProtocol:: |
+ RESPONSE_PROXY_SERVER_STATUS_NON_DRP_WITH_VIA}, |
+ }; |
+ |
+ for (const auto& test : test_cases) { |
+ ResetDependencies(); |
+ storage()->set_proxy_service( |
+ net::ProxyService::CreateFixed(test.proxy_rules)); |
+ AttachToContextAndInit(); |
+ if (test.enable_data_reduction_proxy) |
+ drp_test_context()->EnableDataReductionProxyWithSecureProxyCheckSuccess(); |
+ |
+ MockRead reads[] = {MockRead(test.response_headers), |
+ MockRead(""), |
+ MockRead(net::SYNCHRONOUS, net::OK)}; |
+ net::StaticSocketDataProvider socket(reads, arraysize(reads), nullptr, 0); |
+ mock_socket_factory()->AddSocketDataProvider(&socket); |
+ |
+ base::HistogramTester histogram_tester; |
+ net::TestDelegate delegate; |
+ scoped_ptr<net::URLRequest> request(context()->CreateRequest( |
+ GURL("http://google.com"), net::IDLE, &delegate)); |
+ request->Start(); |
+ drp_test_context()->RunUntilIdle(); |
+ |
+ histogram_tester.ExpectUniqueSample( |
+ "DataReductionProxy.ResponseProxyServerStatus", test.expected_status, |
+ 1); |
} |
} |