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

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

Issue 791493015: Adding q=low to the Chrome-Proxy request header (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 11 months 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_request_options_unittest.cc
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options_unittest.cc
similarity index 53%
rename from components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler_unittest.cc
rename to components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options_unittest.cc
index 68eedfe6ec85027148e757d0a8feff1ed9f05c99..54081446b32abad1d22fce4c873e82bf5e7e976c 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler_unittest.cc
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options_unittest.cc
@@ -3,8 +3,9 @@
// found in the LICENSE file.
-#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.h"
+#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.h"
+#include "base/command_line.h"
#include "base/md5.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
@@ -13,6 +14,7 @@
#include "base/time/time.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_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_switches.h"
#include "net/base/auth.h"
#include "net/base/host_port_pair.h"
#include "testing/gmock/include/gmock/gmock.h"
@@ -76,28 +78,15 @@ const Client kClient = Client::UNKNOWN;
const char kClientStr[] = "";
#endif
-const std::string kExpectedHeader2 =
- std::string("ps=0-1633771873-1633771873-1633771873, ") +
- std::string("sid=c911fdb402f578787562cf7f00eda972, b=2, p=3, c=") +
- kClientStr;
-const std::string kExpectedHeader3 =
- std::string("ps=86401-1633771873-1633771873-1633771873, ") +
- std::string("sid=d7c1c34ef6b90303b01c48a6c1db6419, b=2, p=3, c=") +
- kClientStr;
-const std::string kExpectedHeader4 =
- std::string("ps=0-1633771873-1633771873-1633771873, ") +
- std::string("sid=c911fdb402f578787562cf7f00eda972, c=") +
- kClientStr;
-
-class TestDataReductionProxyAuthRequestHandler
- : public DataReductionProxyAuthRequestHandler {
+class TestDataReductionProxyRequestOptions
+ : public DataReductionProxyRequestOptions {
public:
- TestDataReductionProxyAuthRequestHandler(
+ TestDataReductionProxyRequestOptions(
Client client,
const std::string& version,
DataReductionProxyParams* params,
base::MessageLoopProxy* loop_proxy)
- : DataReductionProxyAuthRequestHandler(
+ : DataReductionProxyRequestOptions(
client, version, params, loop_proxy) {}
std::string GetDefaultKey() const override { return kTestKey; }
@@ -124,17 +113,56 @@ class TestDataReductionProxyAuthRequestHandler
} // namespace
-class DataReductionProxyAuthRequestHandlerTest : public testing::Test {
+class DataReductionProxyRequestOptionsTest : public testing::Test {
public:
- DataReductionProxyAuthRequestHandlerTest()
+ DataReductionProxyRequestOptionsTest()
: loop_proxy_(base::MessageLoopProxy::current().get()) {
}
+
+ std::map<std::string, std::string> ParseHeader(std::string header) {
+ std::map<std::string, std::string> header_options;
+ size_t pos = 0;
+ std::string name;
+ std::string value;
+ std::string equals_delimiter = "=";
+ std::string comma_delimiter = ", ";
+ while ((pos = header.find(equals_delimiter)) != std::string::npos) {
+ name = header.substr(0, pos);
+ header.erase(0, pos + equals_delimiter.length());
+ pos = header.find(comma_delimiter);
+ if (pos != std::string::npos) {
+ value = header.substr(0, pos);
+ header.erase(0, pos + comma_delimiter.length());
+ header_options[name] = value;
+ }
+ }
+
+ if (!header.empty())
+ header_options[name] = header;
+
+ return header_options;
+ }
+
// Required for MessageLoopProxy::current().
base::MessageLoopForUI loop_;
base::MessageLoopProxy* loop_proxy_;
};
-TEST_F(DataReductionProxyAuthRequestHandlerTest, AuthorizationOnIOThread) {
+TEST_F(DataReductionProxyRequestOptionsTest, AuthorizationOnIOThread) {
+ std::map<std::string, std::string> kExpectedHeader;
+ kExpectedHeader["ps"] = kExpectedSession2;
+ kExpectedHeader["sid"] = kExpectedCredentials2;
+ kExpectedHeader["b"] = "2";
+ kExpectedHeader["p"] = "3";
+ kExpectedHeader["c"] = kClientStr;
+
+ std::map<std::string, std::string> kExpectedHeader2;
+ kExpectedHeader2["ps"] = "86401-1633771873-1633771873-1633771873";
+ kExpectedHeader2["sid"] = "d7c1c34ef6b90303b01c48a6c1db6419";
+ kExpectedHeader2["b"] = "2";
+ kExpectedHeader2["p"] = "3";
+ kExpectedHeader2["c"] = kClientStr;
+
scoped_ptr<TestDataReductionProxyParams> params;
params.reset(
new TestDataReductionProxyParams(
@@ -145,41 +173,41 @@ TEST_F(DataReductionProxyAuthRequestHandlerTest, AuthorizationOnIOThread) {
~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.
- TestDataReductionProxyAuthRequestHandler auth_handler(kClient,
- kVersion,
- params.get(),
- loop_proxy_);
- auth_handler.Init();
+ // is the network thread used by DataReductionProxyRequestOptions.
+ TestDataReductionProxyRequestOptions request_options(kClient,
+ kVersion,
+ params.get(),
+ loop_proxy_);
+ request_options.Init();
base::RunLoop().RunUntilIdle();
- EXPECT_EQ(auth_handler.client_, kClientStr);
- EXPECT_EQ(kExpectedBuild, auth_handler.build_number_);
- EXPECT_EQ(kExpectedPatch, auth_handler.patch_number_);
- EXPECT_EQ(auth_handler.key_, kTestKey);
- EXPECT_EQ(kExpectedCredentials, auth_handler.credentials_);
- EXPECT_EQ(kExpectedSession, auth_handler.session_);
+ EXPECT_EQ(request_options.client_, kClientStr);
+ EXPECT_EQ(kExpectedBuild, request_options.build_number_);
+ EXPECT_EQ(kExpectedPatch, request_options.patch_number_);
+ EXPECT_EQ(request_options.key_, kTestKey);
+ EXPECT_EQ(kExpectedCredentials, request_options.credentials_);
+ EXPECT_EQ(kExpectedSession, request_options.session_);
// Now set a key.
- auth_handler.InitAuthentication(kTestKey2);
+ request_options.InitAuthentication(kTestKey2);
base::RunLoop().RunUntilIdle();
- EXPECT_EQ(kTestKey2, auth_handler.key_);
- EXPECT_EQ(kExpectedCredentials2, auth_handler.credentials_);
- EXPECT_EQ(kExpectedSession2, auth_handler.session_);
+ EXPECT_EQ(kTestKey2, request_options.key_);
+ EXPECT_EQ(kExpectedCredentials2, request_options.credentials_);
+ EXPECT_EQ(kExpectedSession2, request_options.session_);
// Don't write headers if the proxy is invalid.
net::HttpRequestHeaders headers;
- auth_handler.MaybeAddRequestHeader(NULL, net::ProxyServer(), &headers);
+ request_options.MaybeAddRequestHeader(NULL, net::ProxyServer(), &headers);
EXPECT_FALSE(headers.HasHeader(kChromeProxyHeader));
// Don't write headers with a valid proxy, that's not a data reduction proxy.
- auth_handler.MaybeAddRequestHeader(
+ request_options.MaybeAddRequestHeader(
NULL,
net::ProxyServer::FromURI(kOtherProxy, net::ProxyServer::SCHEME_HTTP),
&headers);
EXPECT_FALSE(headers.HasHeader(kChromeProxyHeader));
// Don't write headers with a valid data reduction ssl proxy.
- auth_handler.MaybeAddRequestHeader(
+ request_options.MaybeAddRequestHeader(
NULL,
net::ProxyServer::FromURI(
net::HostPortPair::FromURL(
@@ -189,7 +217,7 @@ TEST_F(DataReductionProxyAuthRequestHandlerTest, AuthorizationOnIOThread) {
EXPECT_FALSE(headers.HasHeader(kChromeProxyHeader));
// Write headers with a valid data reduction proxy.
- auth_handler.MaybeAddRequestHeader(
+ request_options.MaybeAddRequestHeader(
NULL,
net::ProxyServer::FromURI(
net::HostPortPair::FromURL(GURL(params->DefaultOrigin())).ToString(),
@@ -198,23 +226,23 @@ TEST_F(DataReductionProxyAuthRequestHandlerTest, AuthorizationOnIOThread) {
EXPECT_TRUE(headers.HasHeader(kChromeProxyHeader));
std::string header_value;
headers.GetHeader(kChromeProxyHeader, &header_value);
- EXPECT_EQ(kExpectedHeader2, header_value);
+ EXPECT_EQ(kExpectedHeader, ParseHeader(header_value));
// Write headers with a valid data reduction ssl proxy when one is expected.
net::HttpRequestHeaders ssl_headers;
- auth_handler.MaybeAddProxyTunnelRequestHandler(
+ request_options.MaybeAddProxyTunnelRequestHandler(
net::HostPortPair::FromURL(GURL(params->DefaultSSLOrigin())),
&ssl_headers);
EXPECT_TRUE(ssl_headers.HasHeader(kChromeProxyHeader));
std::string ssl_header_value;
ssl_headers.GetHeader(kChromeProxyHeader, &ssl_header_value);
- EXPECT_EQ(kExpectedHeader2, ssl_header_value);
+ EXPECT_EQ(kExpectedHeader, ParseHeader(ssl_header_value));
// Fast forward 24 hours. The header should be the same.
- auth_handler.set_offset(base::TimeDelta::FromSeconds(24 * 60 * 60));
+ request_options.set_offset(base::TimeDelta::FromSeconds(24 * 60 * 60));
net::HttpRequestHeaders headers2;
// Write headers with a valid data reduction proxy.
- auth_handler.MaybeAddRequestHeader(
+ request_options.MaybeAddRequestHeader(
NULL,
net::ProxyServer::FromURI(
net::HostPortPair::FromURL(GURL(params->DefaultOrigin())).ToString(),
@@ -223,13 +251,13 @@ TEST_F(DataReductionProxyAuthRequestHandlerTest, AuthorizationOnIOThread) {
EXPECT_TRUE(headers2.HasHeader(kChromeProxyHeader));
std::string header_value2;
headers2.GetHeader(kChromeProxyHeader, &header_value2);
- EXPECT_EQ(kExpectedHeader2, header_value2);
+ EXPECT_EQ(kExpectedHeader, ParseHeader(header_value2));
// Fast forward one more second. The header should be new.
- auth_handler.set_offset(base::TimeDelta::FromSeconds(24 * 60 * 60 + 1));
+ request_options.set_offset(base::TimeDelta::FromSeconds(24 * 60 * 60 + 1));
net::HttpRequestHeaders headers3;
// Write headers with a valid data reduction proxy.
- auth_handler.MaybeAddRequestHeader(
+ request_options.MaybeAddRequestHeader(
NULL,
net::ProxyServer::FromURI(
net::HostPortPair::FromURL(GURL(params->DefaultOrigin())).ToString(),
@@ -238,10 +266,10 @@ TEST_F(DataReductionProxyAuthRequestHandlerTest, AuthorizationOnIOThread) {
EXPECT_TRUE(headers3.HasHeader(kChromeProxyHeader));
std::string header_value3;
headers3.GetHeader(kChromeProxyHeader, &header_value3);
- EXPECT_EQ(kExpectedHeader3, header_value3);
+ EXPECT_EQ(kExpectedHeader2, ParseHeader(header_value3));
}
-TEST_F(DataReductionProxyAuthRequestHandlerTest, AuthorizationIgnoresEmptyKey) {
+TEST_F(DataReductionProxyRequestOptionsTest, AuthorizationIgnoresEmptyKey) {
scoped_ptr<TestDataReductionProxyParams> params;
params.reset(
new TestDataReductionProxyParams(
@@ -252,30 +280,35 @@ scoped_ptr<TestDataReductionProxyParams> params;
~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.
- TestDataReductionProxyAuthRequestHandler auth_handler(kClient,
- kVersion,
- params.get(),
- loop_proxy_);
- auth_handler.Init();
+ // is the network thread used by DataReductionProxyRequestOptions.
+ TestDataReductionProxyRequestOptions request_options(kClient,
+ kVersion,
+ params.get(),
+ loop_proxy_);
+ request_options.Init();
base::RunLoop().RunUntilIdle();
- EXPECT_EQ(auth_handler.client_, kClientStr);
- EXPECT_EQ(kExpectedBuild, auth_handler.build_number_);
- EXPECT_EQ(kExpectedPatch, auth_handler.patch_number_);
- EXPECT_EQ(auth_handler.key_, kTestKey);
- EXPECT_EQ(kExpectedCredentials, auth_handler.credentials_);
- EXPECT_EQ(kExpectedSession, auth_handler.session_);
+ EXPECT_EQ(request_options.client_, kClientStr);
+ EXPECT_EQ(kExpectedBuild, request_options.build_number_);
+ EXPECT_EQ(kExpectedPatch, request_options.patch_number_);
+ EXPECT_EQ(request_options.key_, kTestKey);
+ EXPECT_EQ(kExpectedCredentials, request_options.credentials_);
+ EXPECT_EQ(kExpectedSession, request_options.session_);
// Now set an empty key. The auth handler should ignore that, and the key
// remains |kTestKey|.
- auth_handler.InitAuthentication("");
+ request_options.InitAuthentication("");
base::RunLoop().RunUntilIdle();
- EXPECT_EQ(auth_handler.key_, kTestKey);
- EXPECT_EQ(kExpectedCredentials, auth_handler.credentials_);
- EXPECT_EQ(kExpectedSession, auth_handler.session_);
+ EXPECT_EQ(request_options.key_, kTestKey);
+ EXPECT_EQ(kExpectedCredentials, request_options.credentials_);
+ EXPECT_EQ(kExpectedSession, request_options.session_);
}
-TEST_F(DataReductionProxyAuthRequestHandlerTest, AuthorizationBogusVersion) {
+TEST_F(DataReductionProxyRequestOptionsTest, AuthorizationBogusVersion) {
+ std::map<std::string, std::string> kExpectedHeader;
+ kExpectedHeader["ps"] = kExpectedSession2;
+ kExpectedHeader["sid"] = kExpectedCredentials2;
+ kExpectedHeader["c"] = kClientStr;
+
scoped_ptr<TestDataReductionProxyParams> params;
params.reset(
new TestDataReductionProxyParams(
@@ -285,23 +318,23 @@ TEST_F(DataReductionProxyAuthRequestHandlerTest, AuthorizationBogusVersion) {
TestDataReductionProxyParams::HAS_EVERYTHING &
~TestDataReductionProxyParams::HAS_DEV_ORIGIN &
~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN));
- TestDataReductionProxyAuthRequestHandler auth_handler(kClient,
- kBogusVersion,
- params.get(),
- loop_proxy_);
- EXPECT_TRUE(auth_handler.build_number_.empty());
- EXPECT_TRUE(auth_handler.patch_number_.empty());
+ TestDataReductionProxyRequestOptions request_options(kClient,
+ kBogusVersion,
+ params.get(),
+ loop_proxy_);
+ EXPECT_TRUE(request_options.build_number_.empty());
+ EXPECT_TRUE(request_options.patch_number_.empty());
// Now set a key.
- auth_handler.InitAuthentication(kTestKey2);
+ request_options.InitAuthentication(kTestKey2);
base::RunLoop().RunUntilIdle();
- EXPECT_EQ(kTestKey2, auth_handler.key_);
- EXPECT_EQ(kExpectedCredentials2, auth_handler.credentials_);
- EXPECT_EQ(kExpectedSession2, auth_handler.session_);
+ EXPECT_EQ(kTestKey2, request_options.key_);
+ EXPECT_EQ(kExpectedCredentials2, request_options.credentials_);
+ EXPECT_EQ(kExpectedSession2, request_options.session_);
net::HttpRequestHeaders headers;
// Write headers with a valid data reduction proxy;
- auth_handler.MaybeAddRequestHeader(
+ request_options.MaybeAddRequestHeader(
NULL,
net::ProxyServer::FromURI(
net::HostPortPair::FromURL(GURL(params->DefaultOrigin())).ToString(),
@@ -310,16 +343,62 @@ TEST_F(DataReductionProxyAuthRequestHandlerTest, AuthorizationBogusVersion) {
EXPECT_TRUE(headers.HasHeader(kChromeProxyHeader));
std::string header_value;
headers.GetHeader(kChromeProxyHeader, &header_value);
- EXPECT_EQ(kExpectedHeader4, header_value);
+ EXPECT_EQ(kExpectedHeader, ParseHeader(header_value));
}
-TEST_F(DataReductionProxyAuthRequestHandlerTest, AuthHashForSalt) {
+TEST_F(DataReductionProxyRequestOptionsTest, AuthHashForSalt) {
std::string salt = "8675309"; // Jenny's number to test the hash generator.
std::string salted_key = salt + kDataReductionProxyKey + salt;
base::string16 expected_hash = base::UTF8ToUTF16(base::MD5String(salted_key));
EXPECT_EQ(expected_hash,
- DataReductionProxyAuthRequestHandler::AuthHashForSalt(
+ DataReductionProxyRequestOptions::AuthHashForSalt(
8675309, kDataReductionProxyKey));
}
+TEST_F(DataReductionProxyRequestOptionsTest, AuthorizationLoFi) {
+ std::map<std::string, std::string> kExpectedHeader;
+ kExpectedHeader["ps"] = kExpectedSession2;
+ kExpectedHeader["sid"] = kExpectedCredentials2;
+ kExpectedHeader["c"] = kClientStr;
+ kExpectedHeader["q"] = "low";
+
+ 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));
+
+ base::CommandLine::ForCurrentProcess()->AppendSwitch(
+ data_reduction_proxy::switches::kEnableDataReductionProxyLoFi);
+
+ TestDataReductionProxyRequestOptions request_options(kClient,
+ kBogusVersion,
+ params.get(),
+ loop_proxy_);
+
+ // Now set a key.
+ request_options.InitAuthentication(kTestKey2);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(kTestKey2, request_options.key_);
+ EXPECT_EQ(kExpectedCredentials2, request_options.credentials_);
+ EXPECT_EQ(kExpectedSession2, request_options.session_);
+
+ net::HttpRequestHeaders headers;
+ // Write headers with a valid data reduction proxy;
+ request_options.MaybeAddRequestHeader(
+ NULL,
+ net::ProxyServer::FromURI(
+ net::HostPortPair::FromURL(GURL(params->DefaultOrigin())).ToString(),
+ net::ProxyServer::SCHEME_HTTP),
+ &headers);
+ EXPECT_TRUE(headers.HasHeader(kChromeProxyHeader));
+ std::string header_value;
+ headers.GetHeader(kChromeProxyHeader, &header_value);
+ EXPECT_EQ(kExpectedHeader, ParseHeader(header_value));
+}
+
} // namespace data_reduction_proxy

Powered by Google App Engine
This is Rietveld 408576698