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

Unified Diff: components/variations/service/variations_service_unittest.cc

Issue 1404583004: Support gzip-compressed seed data from the variations server. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move IM header logic into a separate function; check empty seed data Created 5 years, 2 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/variations/service/variations_service_unittest.cc
diff --git a/components/variations/service/variations_service_unittest.cc b/components/variations/service/variations_service_unittest.cc
index 1b85ce73ce36f12135c0592163f6bde3641b3876..3b5a1e5fedc5be9c3090d3c5d34217462beb857c 100644
--- a/components/variations/service/variations_service_unittest.cc
+++ b/components/variations/service/variations_service_unittest.cc
@@ -83,7 +83,9 @@ class TestVariationsService : public VariationsService {
UIStringOverrider()),
intercepts_fetch_(true),
fetch_attempted_(false),
- seed_stored_(false) {
+ seed_stored_(false),
+ delta_compressed_seed_(false),
+ gzip_compressed_seed_(false) {
// Set this so StartRepeatedVariationsSeedFetch can be called in tests.
SetCreateTrialsFromSeedCalledForTesting(true);
}
@@ -97,6 +99,8 @@ class TestVariationsService : public VariationsService {
bool fetch_attempted() const { return fetch_attempted_; }
bool seed_stored() const { return seed_stored_; }
const std::string& stored_country() const { return stored_country_; }
+ bool delta_compressed_seed() const { return delta_compressed_seed_; }
+ bool gzip_compressed_seed() const { return gzip_compressed_seed_; }
void DoActualFetch() override {
if (intercepts_fetch_) {
@@ -111,10 +115,13 @@ class TestVariationsService : public VariationsService {
const std::string& seed_signature,
const std::string& country_code,
const base::Time& date_fetched,
- bool is_delta_compressed) override {
+ bool is_delta_compressed,
+ bool is_gzip_compressed) override {
seed_stored_ = true;
stored_seed_data_ = seed_data;
stored_country_ = country_code;
+ delta_compressed_seed_ = is_delta_compressed;
+ gzip_compressed_seed_ = is_gzip_compressed;
return true;
}
@@ -130,6 +137,8 @@ class TestVariationsService : public VariationsService {
bool seed_stored_;
std::string stored_seed_data_;
std::string stored_country_;
+ bool delta_compressed_seed_;
+ bool gzip_compressed_seed_;
DISALLOW_COPY_AND_ASSIGN(TestVariationsService);
};
@@ -201,13 +210,17 @@ std::string SerializeSeed(const variations::VariationsSeed& seed) {
}
// Simulates a variations service response by setting a date header and the
-// specified HTTP |response_code| on |fetcher|.
+// specified HTTP |response_code| on |fetcher|. Sets additional header |header|
+// if it is not NULL.
Alexei Svitkine (slow) 2015/10/15 20:30:26 Nit: For new code, in code use "nullptr", and in c
veranika 2015/10/15 22:10:38 Done.
scoped_refptr<net::HttpResponseHeaders> SimulateServerResponse(
int response_code,
- net::TestURLFetcher* fetcher) {
+ net::TestURLFetcher* fetcher,
+ const std::string* header = NULL) {
EXPECT_TRUE(fetcher);
scoped_refptr<net::HttpResponseHeaders> headers(
new net::HttpResponseHeaders("date:Wed, 13 Feb 2013 00:25:24 GMT\0\0"));
+ if (header)
+ headers->AddHeader(*header);
fetcher->set_response_headers(headers);
fetcher->set_response_code(response_code);
return headers;
@@ -253,7 +266,7 @@ TEST_F(VariationsServiceTest, CreateTrialsFromSeed) {
// Store a seed.
service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(),
- std::string(), base::Time::Now(), false);
+ std::string(), base::Time::Now(), false, false);
prefs.SetInt64(prefs::kVariationsLastFetchTime,
base::Time::Now().ToInternalValue());
@@ -284,7 +297,7 @@ TEST_F(VariationsServiceTest, CreateTrialsFromSeedNoLastFetchTime) {
// Store a seed. To simulate a first run, |prefs::kVariationsLastFetchTime|
// is left empty.
service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(),
- std::string(), base::Time::Now(), false);
+ std::string(), base::Time::Now(), false, false);
EXPECT_EQ(0, prefs.GetInt64(prefs::kVariationsLastFetchTime));
// Check that field trials are created from the seed. Since the test study has
@@ -315,7 +328,7 @@ TEST_F(VariationsServiceTest, CreateTrialsFromOutdatedSeed) {
const base::Time seed_date =
base::Time::Now() - base::TimeDelta::FromDays(31);
service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(),
- std::string(), seed_date, false);
+ std::string(), seed_date, false, false);
prefs.SetInt64(prefs::kVariationsLastFetchTime, seed_date.ToInternalValue());
// Check that field trials are not created from the seed.
@@ -469,6 +482,71 @@ TEST_F(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) {
}
}
+TEST_F(VariationsServiceTest, RequestGzipCompressedSeed) {
+ TestingPrefServiceSimple prefs;
+ VariationsService::RegisterPrefs(prefs.registry());
+ net::TestURLFetcherFactory factory;
+
+ TestVariationsService service(
+ make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)),
+ &prefs);
+ service.variations_server_url_ =
+ service.GetVariationsServerURL(&prefs, std::string());
+ service.set_intercepts_fetch(false);
+ service.DoActualFetch();
+
+ net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
+ net::HttpRequestHeaders headers;
+ fetcher->GetExtraRequestHeaders(&headers);
+ std::string field;
+ ASSERT_TRUE(headers.GetHeader("A-IM", &field));
+ EXPECT_EQ("gzip", field);
+}
+
+TEST_F(VariationsServiceTest, InstanceManipulations) {
+ struct {
+ std::string im;
+ bool delta_compressed;
+ bool gzip_compressed;
+ bool seed_stored;
+ } cases[] = {
+ {"", false, false, true},
+ {"IM:gzip", false, true, true},
+ {"IM:x-bm", true, false, true},
+ {"IM:x-bm,gzip", true, true, true},
+ {"IM: x-bm, gzip", true, true, true},
+ {"IM:gzip,x-bm", false, false, false},
+ {"IM:deflate,x-bm,gzip", false, false, false},
+ };
+
+ TestingPrefServiceSimple prefs;
+ VariationsService::RegisterPrefs(prefs.registry());
+ std::string serialized_seed = SerializeSeed(CreateTestSeed());
+ net::TestURLFetcherFactory factory;
+
+ for (size_t i = 0; i < arraysize(cases); ++i) {
+ TestVariationsService service(
+ make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)),
+ &prefs);
+ service.variations_server_url_ =
+ service.GetVariationsServerURL(&prefs, std::string());
+ service.set_intercepts_fetch(false);
+ service.DoActualFetch();
+ net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
+
+ if (cases[i].im.empty())
+ SimulateServerResponse(net::HTTP_OK, fetcher);
+ else
+ SimulateServerResponse(net::HTTP_OK, fetcher, &cases[i].im);
+ fetcher->SetResponseString(serialized_seed);
+ service.OnURLFetchComplete(fetcher);
+
+ EXPECT_EQ(cases[i].seed_stored, service.seed_stored());
+ EXPECT_EQ(cases[i].delta_compressed, service.delta_compressed_seed());
+ EXPECT_EQ(cases[i].gzip_compressed, service.gzip_compressed_seed());
+ }
+}
+
TEST_F(VariationsServiceTest, CountryHeader) {
TestingPrefServiceSimple prefs;
VariationsService::RegisterPrefs(prefs.registry());

Powered by Google App Engine
This is Rietveld 408576698