| Index: net/url_request/url_request_throttler_unittest.cc
|
| diff --git a/net/url_request/url_request_throttler_unittest.cc b/net/url_request/url_request_throttler_unittest.cc
|
| index 0d018cd78b8e030a1406ce81ebc75b72abb2865c..9a6079d1a122d89a95622f6d552812a9907f4fff 100644
|
| --- a/net/url_request/url_request_throttler_unittest.cc
|
| +++ b/net/url_request/url_request_throttler_unittest.cc
|
| @@ -13,6 +13,7 @@
|
| #include "net/base/load_flags.h"
|
| #include "net/base/test_completion_callback.h"
|
| #include "net/url_request/url_request_context.h"
|
| +#include "net/url_request/url_request_test_util.h"
|
| #include "net/url_request/url_request_throttler_header_interface.h"
|
| #include "net/url_request/url_request_throttler_test_support.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| @@ -166,6 +167,9 @@ struct GurlAndString {
|
|
|
| class URLRequestThrottlerEntryTest : public testing::Test {
|
| protected:
|
| + URLRequestThrottlerEntryTest() : request_(GURL(), NULL) {
|
| + }
|
| +
|
| virtual void SetUp();
|
|
|
| // After calling this function, histogram snapshots in |samples_| contain
|
| @@ -178,6 +182,8 @@ class URLRequestThrottlerEntryTest : public testing::Test {
|
|
|
| std::map<std::string, Histogram::SampleSet> original_samples_;
|
| std::map<std::string, Histogram::SampleSet> samples_;
|
| +
|
| + TestURLRequest request_;
|
| };
|
|
|
| // List of all histograms we care about in these unit tests.
|
| @@ -189,6 +195,8 @@ const char* kHistogramNames[] = {
|
| };
|
|
|
| void URLRequestThrottlerEntryTest::SetUp() {
|
| + request_.set_load_flags(0);
|
| +
|
| now_ = TimeTicks::Now();
|
| entry_ = new MockURLRequestThrottlerEntry(&manager_);
|
| entry_->ResetToBlank(now_);
|
| @@ -234,10 +242,11 @@ std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) {
|
| TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) {
|
| entry_->set_exponential_backoff_release_time(
|
| entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1));
|
| - EXPECT_TRUE(entry_->ShouldRejectRequest(0));
|
| + EXPECT_TRUE(entry_->ShouldRejectRequest(request_));
|
|
|
| // Also end-to-end test the load flags exceptions.
|
| - EXPECT_FALSE(entry_->ShouldRejectRequest(LOAD_MAYBE_USER_GESTURE));
|
| + request_.set_load_flags(LOAD_MAYBE_USER_GESTURE);
|
| + EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
|
|
|
| CalculateHistogramDeltas();
|
| ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(0));
|
| @@ -246,10 +255,10 @@ TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) {
|
|
|
| TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) {
|
| entry_->set_exponential_backoff_release_time(entry_->fake_time_now_);
|
| - EXPECT_FALSE(entry_->ShouldRejectRequest(0));
|
| + EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
|
| entry_->set_exponential_backoff_release_time(
|
| entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1));
|
| - EXPECT_FALSE(entry_->ShouldRejectRequest(0));
|
| + EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
|
|
|
| CalculateHistogramDeltas();
|
| ASSERT_EQ(2, samples_["Throttling.RequestThrottled"].counts(0));
|
| @@ -278,10 +287,6 @@ TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) {
|
| EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_)
|
| << "This scenario should add delay";
|
| entry_->UpdateWithResponse("", &success_response);
|
| -
|
| - CalculateHistogramDeltas();
|
| - ASSERT_EQ(1, samples_["Throttling.FailureCountAtSuccess"].counts(1));
|
| - ASSERT_EQ(1, samples_["Throttling.PerceivedDowntime"].TotalCount());
|
| }
|
|
|
| TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) {
|
| @@ -371,7 +376,20 @@ TEST_F(URLRequestThrottlerEntryTest, ExplicitUserRequest) {
|
| ~LOAD_MAYBE_USER_GESTURE));
|
| }
|
|
|
| -TEST(URLRequestThrottlerManager, IsUrlStandardised) {
|
| +class URLRequestThrottlerManagerTest : public testing::Test {
|
| + protected:
|
| + URLRequestThrottlerManagerTest()
|
| + : request_(GURL(), NULL) {
|
| + }
|
| +
|
| + virtual void SetUp() {
|
| + request_.set_load_flags(0);
|
| + }
|
| +
|
| + TestURLRequest request_;
|
| +};
|
| +
|
| +TEST_F(URLRequestThrottlerManagerTest, IsUrlStandardised) {
|
| MockURLRequestThrottlerManager manager;
|
| GurlAndString test_values[] = {
|
| GurlAndString(GURL("http://www.example.com"),
|
| @@ -406,7 +424,7 @@ TEST(URLRequestThrottlerManager, IsUrlStandardised) {
|
| }
|
| }
|
|
|
| -TEST(URLRequestThrottlerManager, AreEntriesBeingCollected) {
|
| +TEST_F(URLRequestThrottlerManagerTest, AreEntriesBeingCollected) {
|
| MockURLRequestThrottlerManager manager;
|
|
|
| manager.CreateEntry(true); // true = Entry is outdated.
|
| @@ -423,7 +441,7 @@ TEST(URLRequestThrottlerManager, AreEntriesBeingCollected) {
|
| EXPECT_EQ(3, manager.GetNumberOfEntries());
|
| }
|
|
|
| -TEST(URLRequestThrottlerManager, IsHostBeingRegistered) {
|
| +TEST_F(URLRequestThrottlerManagerTest, IsHostBeingRegistered) {
|
| MockURLRequestThrottlerManager manager;
|
|
|
| manager.RegisterRequestUrl(GURL("http://www.example.com/"));
|
| @@ -437,14 +455,15 @@ TEST(URLRequestThrottlerManager, IsHostBeingRegistered) {
|
|
|
| void ExpectEntryAllowsAllOnErrorIfOptedOut(
|
| net::URLRequestThrottlerEntryInterface* entry,
|
| - bool opted_out) {
|
| - EXPECT_FALSE(entry->ShouldRejectRequest(0));
|
| + bool opted_out,
|
| + const URLRequest& request) {
|
| + EXPECT_FALSE(entry->ShouldRejectRequest(request));
|
| MockURLRequestThrottlerHeaderAdapter failure_adapter(503);
|
| for (int i = 0; i < 10; ++i) {
|
| // Host doesn't really matter in this scenario so we skip it.
|
| entry->UpdateWithResponse("", &failure_adapter);
|
| }
|
| - EXPECT_NE(opted_out, entry->ShouldRejectRequest(0));
|
| + EXPECT_NE(opted_out, entry->ShouldRejectRequest(request));
|
|
|
| if (opted_out) {
|
| // We're not mocking out GetTimeNow() in this scenario
|
| @@ -460,7 +479,7 @@ void ExpectEntryAllowsAllOnErrorIfOptedOut(
|
| }
|
| }
|
|
|
| -TEST(URLRequestThrottlerManager, OptOutHeader) {
|
| +TEST_F(URLRequestThrottlerManagerTest, OptOutHeader) {
|
| MockURLRequestThrottlerManager manager;
|
| scoped_refptr<net::URLRequestThrottlerEntryInterface> entry =
|
| manager.RegisterRequestUrl(GURL("http://www.google.com/yodude"));
|
| @@ -473,28 +492,28 @@ TEST(URLRequestThrottlerManager, OptOutHeader) {
|
| entry->UpdateWithResponse("www.google.com", &response_adapter);
|
|
|
| // Ensure that the same entry on error always allows everything.
|
| - ExpectEntryAllowsAllOnErrorIfOptedOut(entry, true);
|
| + ExpectEntryAllowsAllOnErrorIfOptedOut(entry, true, request_);
|
|
|
| // Ensure that a freshly created entry (for a different URL on an
|
| // already opted-out host) also gets "always allow" behavior.
|
| scoped_refptr<net::URLRequestThrottlerEntryInterface> other_entry =
|
| manager.RegisterRequestUrl(GURL("http://www.google.com/bingobob"));
|
| - ExpectEntryAllowsAllOnErrorIfOptedOut(other_entry, true);
|
| + ExpectEntryAllowsAllOnErrorIfOptedOut(other_entry, true, request_);
|
|
|
| // Fake a response with the opt-out header incorrectly specified.
|
| scoped_refptr<net::URLRequestThrottlerEntryInterface> no_opt_out_entry =
|
| manager.RegisterRequestUrl(GURL("http://www.nike.com/justdoit"));
|
| MockURLRequestThrottlerHeaderAdapter wrong_adapter("", "yesplease", 200);
|
| no_opt_out_entry->UpdateWithResponse("www.nike.com", &wrong_adapter);
|
| - ExpectEntryAllowsAllOnErrorIfOptedOut(no_opt_out_entry, false);
|
| + ExpectEntryAllowsAllOnErrorIfOptedOut(no_opt_out_entry, false, request_);
|
|
|
| // A localhost entry should always be opted out.
|
| scoped_refptr<net::URLRequestThrottlerEntryInterface> localhost_entry =
|
| manager.RegisterRequestUrl(GURL("http://localhost/hello"));
|
| - ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry, true);
|
| + ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry, true, request_);
|
| }
|
|
|
| -TEST(URLRequestThrottlerManager, ClearOnNetworkChange) {
|
| +TEST_F(URLRequestThrottlerManagerTest, ClearOnNetworkChange) {
|
| for (int i = 0; i < 3; ++i) {
|
| MockURLRequestThrottlerManager manager;
|
| scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_before =
|
| @@ -504,7 +523,7 @@ TEST(URLRequestThrottlerManager, ClearOnNetworkChange) {
|
| // Host doesn't really matter in this scenario so we skip it.
|
| entry_before->UpdateWithResponse("", &failure_adapter);
|
| }
|
| - EXPECT_TRUE(entry_before->ShouldRejectRequest(0));
|
| + EXPECT_TRUE(entry_before->ShouldRejectRequest(request_));
|
|
|
| switch (i) {
|
| case 0:
|
| @@ -524,7 +543,7 @@ TEST(URLRequestThrottlerManager, ClearOnNetworkChange) {
|
|
|
| scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after =
|
| manager.RegisterRequestUrl(GURL("http://www.example.com/"));
|
| - EXPECT_FALSE(entry_after->ShouldRejectRequest(0));
|
| + EXPECT_FALSE(entry_after->ShouldRejectRequest(request_));
|
| }
|
| }
|
|
|
|
|