OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/url_request/url_request_throttler_manager.h" | 5 #include "net/url_request/url_request_throttler_manager.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/time.h" | 12 #include "base/time.h" |
13 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
14 #include "net/base/test_completion_callback.h" | 14 #include "net/base/test_completion_callback.h" |
15 #include "net/url_request/url_request_context.h" | 15 #include "net/url_request/url_request_context.h" |
| 16 #include "net/url_request/url_request_test_util.h" |
16 #include "net/url_request/url_request_throttler_header_interface.h" | 17 #include "net/url_request/url_request_throttler_header_interface.h" |
17 #include "net/url_request/url_request_throttler_test_support.h" | 18 #include "net/url_request/url_request_throttler_test_support.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 using base::TimeDelta; | 21 using base::TimeDelta; |
21 using base::TimeTicks; | 22 using base::TimeTicks; |
22 | 23 |
23 namespace net { | 24 namespace net { |
24 | 25 |
25 namespace { | 26 namespace { |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 } | 160 } |
160 GURL url; | 161 GURL url; |
161 std::string result; | 162 std::string result; |
162 int line; | 163 int line; |
163 }; | 164 }; |
164 | 165 |
165 } // namespace | 166 } // namespace |
166 | 167 |
167 class URLRequestThrottlerEntryTest : public testing::Test { | 168 class URLRequestThrottlerEntryTest : public testing::Test { |
168 protected: | 169 protected: |
| 170 URLRequestThrottlerEntryTest() : request_(GURL(), NULL) { |
| 171 } |
| 172 |
169 virtual void SetUp(); | 173 virtual void SetUp(); |
170 | 174 |
171 // After calling this function, histogram snapshots in |samples_| contain | 175 // After calling this function, histogram snapshots in |samples_| contain |
172 // only the delta caused by the test case currently running. | 176 // only the delta caused by the test case currently running. |
173 void CalculateHistogramDeltas(); | 177 void CalculateHistogramDeltas(); |
174 | 178 |
175 TimeTicks now_; | 179 TimeTicks now_; |
176 MockURLRequestThrottlerManager manager_; // Dummy object, not used. | 180 MockURLRequestThrottlerManager manager_; // Dummy object, not used. |
177 scoped_refptr<MockURLRequestThrottlerEntry> entry_; | 181 scoped_refptr<MockURLRequestThrottlerEntry> entry_; |
178 | 182 |
179 std::map<std::string, Histogram::SampleSet> original_samples_; | 183 std::map<std::string, Histogram::SampleSet> original_samples_; |
180 std::map<std::string, Histogram::SampleSet> samples_; | 184 std::map<std::string, Histogram::SampleSet> samples_; |
| 185 |
| 186 TestURLRequest request_; |
181 }; | 187 }; |
182 | 188 |
183 // List of all histograms we care about in these unit tests. | 189 // List of all histograms we care about in these unit tests. |
184 const char* kHistogramNames[] = { | 190 const char* kHistogramNames[] = { |
185 "Throttling.FailureCountAtSuccess", | 191 "Throttling.FailureCountAtSuccess", |
186 "Throttling.PerceivedDowntime", | 192 "Throttling.PerceivedDowntime", |
187 "Throttling.RequestThrottled", | 193 "Throttling.RequestThrottled", |
188 "Throttling.SiteOptedOut", | 194 "Throttling.SiteOptedOut", |
189 }; | 195 }; |
190 | 196 |
191 void URLRequestThrottlerEntryTest::SetUp() { | 197 void URLRequestThrottlerEntryTest::SetUp() { |
| 198 request_.set_load_flags(0); |
| 199 |
192 now_ = TimeTicks::Now(); | 200 now_ = TimeTicks::Now(); |
193 entry_ = new MockURLRequestThrottlerEntry(&manager_); | 201 entry_ = new MockURLRequestThrottlerEntry(&manager_); |
194 entry_->ResetToBlank(now_); | 202 entry_->ResetToBlank(now_); |
195 | 203 |
196 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { | 204 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { |
197 // Must retrieve original samples for each histogram for comparison | 205 // Must retrieve original samples for each histogram for comparison |
198 // as other tests may affect them. | 206 // as other tests may affect them. |
199 const char* name = kHistogramNames[i]; | 207 const char* name = kHistogramNames[i]; |
200 Histogram::SampleSet& original = original_samples_[name]; | 208 Histogram::SampleSet& original = original_samples_[name]; |
201 Histogram* histogram; | 209 Histogram* histogram; |
(...skipping 25 matching lines...) Expand all Loading... |
227 original_samples_.clear(); | 235 original_samples_.clear(); |
228 } | 236 } |
229 | 237 |
230 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { | 238 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { |
231 return out << time.ToInternalValue(); | 239 return out << time.ToInternalValue(); |
232 } | 240 } |
233 | 241 |
234 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { | 242 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
235 entry_->set_exponential_backoff_release_time( | 243 entry_->set_exponential_backoff_release_time( |
236 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 244 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
237 EXPECT_TRUE(entry_->ShouldRejectRequest(0)); | 245 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); |
238 | 246 |
239 // Also end-to-end test the load flags exceptions. | 247 // Also end-to-end test the load flags exceptions. |
240 EXPECT_FALSE(entry_->ShouldRejectRequest(LOAD_MAYBE_USER_GESTURE)); | 248 request_.set_load_flags(LOAD_MAYBE_USER_GESTURE); |
| 249 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
241 | 250 |
242 CalculateHistogramDeltas(); | 251 CalculateHistogramDeltas(); |
243 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(0)); | 252 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(0)); |
244 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(1)); | 253 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(1)); |
245 } | 254 } |
246 | 255 |
247 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { | 256 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
248 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); | 257 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); |
249 EXPECT_FALSE(entry_->ShouldRejectRequest(0)); | 258 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
250 entry_->set_exponential_backoff_release_time( | 259 entry_->set_exponential_backoff_release_time( |
251 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 260 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
252 EXPECT_FALSE(entry_->ShouldRejectRequest(0)); | 261 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
253 | 262 |
254 CalculateHistogramDeltas(); | 263 CalculateHistogramDeltas(); |
255 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"].counts(0)); | 264 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"].counts(0)); |
256 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"].counts(1)); | 265 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"].counts(1)); |
257 } | 266 } |
258 | 267 |
259 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 268 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
260 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 269 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
261 entry_->UpdateWithResponse("", &failure_response); | 270 entry_->UpdateWithResponse("", &failure_response); |
262 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 271 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
263 << "A failure should increase the release_time"; | 272 << "A failure should increase the release_time"; |
264 } | 273 } |
265 | 274 |
266 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { | 275 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { |
267 MockURLRequestThrottlerHeaderAdapter success_response(200); | 276 MockURLRequestThrottlerHeaderAdapter success_response(200); |
268 entry_->UpdateWithResponse("", &success_response); | 277 entry_->UpdateWithResponse("", &success_response); |
269 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 278 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
270 << "A success should not add any delay"; | 279 << "A success should not add any delay"; |
271 } | 280 } |
272 | 281 |
273 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) { | 282 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) { |
274 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 283 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
275 MockURLRequestThrottlerHeaderAdapter success_response(200); | 284 MockURLRequestThrottlerHeaderAdapter success_response(200); |
276 entry_->UpdateWithResponse("", &success_response); | 285 entry_->UpdateWithResponse("", &success_response); |
277 entry_->UpdateWithResponse("", &failure_response); | 286 entry_->UpdateWithResponse("", &failure_response); |
278 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 287 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
279 << "This scenario should add delay"; | 288 << "This scenario should add delay"; |
280 entry_->UpdateWithResponse("", &success_response); | 289 entry_->UpdateWithResponse("", &success_response); |
281 | |
282 CalculateHistogramDeltas(); | |
283 ASSERT_EQ(1, samples_["Throttling.FailureCountAtSuccess"].counts(1)); | |
284 ASSERT_EQ(1, samples_["Throttling.PerceivedDowntime"].TotalCount()); | |
285 } | 290 } |
286 | 291 |
287 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { | 292 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { |
288 TimeDelta lifetime = TimeDelta::FromMilliseconds( | 293 TimeDelta lifetime = TimeDelta::FromMilliseconds( |
289 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); | 294 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); |
290 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); | 295 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); |
291 | 296 |
292 TimeAndBool test_values[] = { | 297 TimeAndBool test_values[] = { |
293 TimeAndBool(now_, false, __LINE__), | 298 TimeAndBool(now_, false, __LINE__), |
294 TimeAndBool(now_ - kFiveMs, false, __LINE__), | 299 TimeAndBool(now_ - kFiveMs, false, __LINE__), |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 } | 369 } |
365 | 370 |
366 TEST_F(URLRequestThrottlerEntryTest, ExplicitUserRequest) { | 371 TEST_F(URLRequestThrottlerEntryTest, ExplicitUserRequest) { |
367 ASSERT_FALSE(MockURLRequestThrottlerEntry::ExplicitUserRequest(0)); | 372 ASSERT_FALSE(MockURLRequestThrottlerEntry::ExplicitUserRequest(0)); |
368 ASSERT_TRUE(MockURLRequestThrottlerEntry::ExplicitUserRequest( | 373 ASSERT_TRUE(MockURLRequestThrottlerEntry::ExplicitUserRequest( |
369 LOAD_MAYBE_USER_GESTURE)); | 374 LOAD_MAYBE_USER_GESTURE)); |
370 ASSERT_FALSE(MockURLRequestThrottlerEntry::ExplicitUserRequest( | 375 ASSERT_FALSE(MockURLRequestThrottlerEntry::ExplicitUserRequest( |
371 ~LOAD_MAYBE_USER_GESTURE)); | 376 ~LOAD_MAYBE_USER_GESTURE)); |
372 } | 377 } |
373 | 378 |
374 TEST(URLRequestThrottlerManager, IsUrlStandardised) { | 379 class URLRequestThrottlerManagerTest : public testing::Test { |
| 380 protected: |
| 381 URLRequestThrottlerManagerTest() |
| 382 : request_(GURL(), NULL) { |
| 383 } |
| 384 |
| 385 virtual void SetUp() { |
| 386 request_.set_load_flags(0); |
| 387 } |
| 388 |
| 389 TestURLRequest request_; |
| 390 }; |
| 391 |
| 392 TEST_F(URLRequestThrottlerManagerTest, IsUrlStandardised) { |
375 MockURLRequestThrottlerManager manager; | 393 MockURLRequestThrottlerManager manager; |
376 GurlAndString test_values[] = { | 394 GurlAndString test_values[] = { |
377 GurlAndString(GURL("http://www.example.com"), | 395 GurlAndString(GURL("http://www.example.com"), |
378 std::string("http://www.example.com/"), | 396 std::string("http://www.example.com/"), |
379 __LINE__), | 397 __LINE__), |
380 GurlAndString(GURL("http://www.Example.com"), | 398 GurlAndString(GURL("http://www.Example.com"), |
381 std::string("http://www.example.com/"), | 399 std::string("http://www.example.com/"), |
382 __LINE__), | 400 __LINE__), |
383 GurlAndString(GURL("http://www.ex4mple.com/Pr4c71c41"), | 401 GurlAndString(GURL("http://www.ex4mple.com/Pr4c71c41"), |
384 std::string("http://www.ex4mple.com/pr4c71c41"), | 402 std::string("http://www.ex4mple.com/pr4c71c41"), |
(...skipping 14 matching lines...) Expand all Loading... |
399 std::string("http://www.example.com:1234/"), | 417 std::string("http://www.example.com:1234/"), |
400 __LINE__)}; | 418 __LINE__)}; |
401 | 419 |
402 for (unsigned int i = 0; i < arraysize(test_values); ++i) { | 420 for (unsigned int i = 0; i < arraysize(test_values); ++i) { |
403 std::string temp = manager.DoGetUrlIdFromUrl(test_values[i].url); | 421 std::string temp = manager.DoGetUrlIdFromUrl(test_values[i].url); |
404 EXPECT_EQ(temp, test_values[i].result) << | 422 EXPECT_EQ(temp, test_values[i].result) << |
405 "Test case #" << i << " line " << test_values[i].line << " failed"; | 423 "Test case #" << i << " line " << test_values[i].line << " failed"; |
406 } | 424 } |
407 } | 425 } |
408 | 426 |
409 TEST(URLRequestThrottlerManager, AreEntriesBeingCollected) { | 427 TEST_F(URLRequestThrottlerManagerTest, AreEntriesBeingCollected) { |
410 MockURLRequestThrottlerManager manager; | 428 MockURLRequestThrottlerManager manager; |
411 | 429 |
412 manager.CreateEntry(true); // true = Entry is outdated. | 430 manager.CreateEntry(true); // true = Entry is outdated. |
413 manager.CreateEntry(true); | 431 manager.CreateEntry(true); |
414 manager.CreateEntry(true); | 432 manager.CreateEntry(true); |
415 manager.DoGarbageCollectEntries(); | 433 manager.DoGarbageCollectEntries(); |
416 EXPECT_EQ(0, manager.GetNumberOfEntries()); | 434 EXPECT_EQ(0, manager.GetNumberOfEntries()); |
417 | 435 |
418 manager.CreateEntry(false); | 436 manager.CreateEntry(false); |
419 manager.CreateEntry(false); | 437 manager.CreateEntry(false); |
420 manager.CreateEntry(false); | 438 manager.CreateEntry(false); |
421 manager.CreateEntry(true); | 439 manager.CreateEntry(true); |
422 manager.DoGarbageCollectEntries(); | 440 manager.DoGarbageCollectEntries(); |
423 EXPECT_EQ(3, manager.GetNumberOfEntries()); | 441 EXPECT_EQ(3, manager.GetNumberOfEntries()); |
424 } | 442 } |
425 | 443 |
426 TEST(URLRequestThrottlerManager, IsHostBeingRegistered) { | 444 TEST_F(URLRequestThrottlerManagerTest, IsHostBeingRegistered) { |
427 MockURLRequestThrottlerManager manager; | 445 MockURLRequestThrottlerManager manager; |
428 | 446 |
429 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 447 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
430 manager.RegisterRequestUrl(GURL("http://www.google.com/")); | 448 manager.RegisterRequestUrl(GURL("http://www.google.com/")); |
431 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); | 449 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); |
432 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); | 450 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); |
433 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); | 451 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); |
434 | 452 |
435 EXPECT_EQ(3, manager.GetNumberOfEntries()); | 453 EXPECT_EQ(3, manager.GetNumberOfEntries()); |
436 } | 454 } |
437 | 455 |
438 void ExpectEntryAllowsAllOnErrorIfOptedOut( | 456 void ExpectEntryAllowsAllOnErrorIfOptedOut( |
439 net::URLRequestThrottlerEntryInterface* entry, | 457 net::URLRequestThrottlerEntryInterface* entry, |
440 bool opted_out) { | 458 bool opted_out, |
441 EXPECT_FALSE(entry->ShouldRejectRequest(0)); | 459 const URLRequest& request) { |
| 460 EXPECT_FALSE(entry->ShouldRejectRequest(request)); |
442 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); | 461 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); |
443 for (int i = 0; i < 10; ++i) { | 462 for (int i = 0; i < 10; ++i) { |
444 // Host doesn't really matter in this scenario so we skip it. | 463 // Host doesn't really matter in this scenario so we skip it. |
445 entry->UpdateWithResponse("", &failure_adapter); | 464 entry->UpdateWithResponse("", &failure_adapter); |
446 } | 465 } |
447 EXPECT_NE(opted_out, entry->ShouldRejectRequest(0)); | 466 EXPECT_NE(opted_out, entry->ShouldRejectRequest(request)); |
448 | 467 |
449 if (opted_out) { | 468 if (opted_out) { |
450 // We're not mocking out GetTimeNow() in this scenario | 469 // We're not mocking out GetTimeNow() in this scenario |
451 // so add a 100 ms buffer to avoid flakiness (that should always | 470 // so add a 100 ms buffer to avoid flakiness (that should always |
452 // give enough time to get from the TimeTicks::Now() call here | 471 // give enough time to get from the TimeTicks::Now() call here |
453 // to the TimeTicks::Now() call in the entry class). | 472 // to the TimeTicks::Now() call in the entry class). |
454 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), | 473 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), |
455 entry->GetExponentialBackoffReleaseTime()); | 474 entry->GetExponentialBackoffReleaseTime()); |
456 } else { | 475 } else { |
457 // As above, add 100 ms. | 476 // As above, add 100 ms. |
458 EXPECT_LT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), | 477 EXPECT_LT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), |
459 entry->GetExponentialBackoffReleaseTime()); | 478 entry->GetExponentialBackoffReleaseTime()); |
460 } | 479 } |
461 } | 480 } |
462 | 481 |
463 TEST(URLRequestThrottlerManager, OptOutHeader) { | 482 TEST_F(URLRequestThrottlerManagerTest, OptOutHeader) { |
464 MockURLRequestThrottlerManager manager; | 483 MockURLRequestThrottlerManager manager; |
465 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry = | 484 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry = |
466 manager.RegisterRequestUrl(GURL("http://www.google.com/yodude")); | 485 manager.RegisterRequestUrl(GURL("http://www.google.com/yodude")); |
467 | 486 |
468 // Fake a response with the opt-out header. | 487 // Fake a response with the opt-out header. |
469 MockURLRequestThrottlerHeaderAdapter response_adapter( | 488 MockURLRequestThrottlerHeaderAdapter response_adapter( |
470 "", | 489 "", |
471 MockURLRequestThrottlerEntry::kExponentialThrottlingDisableValue, | 490 MockURLRequestThrottlerEntry::kExponentialThrottlingDisableValue, |
472 200); | 491 200); |
473 entry->UpdateWithResponse("www.google.com", &response_adapter); | 492 entry->UpdateWithResponse("www.google.com", &response_adapter); |
474 | 493 |
475 // Ensure that the same entry on error always allows everything. | 494 // Ensure that the same entry on error always allows everything. |
476 ExpectEntryAllowsAllOnErrorIfOptedOut(entry, true); | 495 ExpectEntryAllowsAllOnErrorIfOptedOut(entry, true, request_); |
477 | 496 |
478 // Ensure that a freshly created entry (for a different URL on an | 497 // Ensure that a freshly created entry (for a different URL on an |
479 // already opted-out host) also gets "always allow" behavior. | 498 // already opted-out host) also gets "always allow" behavior. |
480 scoped_refptr<net::URLRequestThrottlerEntryInterface> other_entry = | 499 scoped_refptr<net::URLRequestThrottlerEntryInterface> other_entry = |
481 manager.RegisterRequestUrl(GURL("http://www.google.com/bingobob")); | 500 manager.RegisterRequestUrl(GURL("http://www.google.com/bingobob")); |
482 ExpectEntryAllowsAllOnErrorIfOptedOut(other_entry, true); | 501 ExpectEntryAllowsAllOnErrorIfOptedOut(other_entry, true, request_); |
483 | 502 |
484 // Fake a response with the opt-out header incorrectly specified. | 503 // Fake a response with the opt-out header incorrectly specified. |
485 scoped_refptr<net::URLRequestThrottlerEntryInterface> no_opt_out_entry = | 504 scoped_refptr<net::URLRequestThrottlerEntryInterface> no_opt_out_entry = |
486 manager.RegisterRequestUrl(GURL("http://www.nike.com/justdoit")); | 505 manager.RegisterRequestUrl(GURL("http://www.nike.com/justdoit")); |
487 MockURLRequestThrottlerHeaderAdapter wrong_adapter("", "yesplease", 200); | 506 MockURLRequestThrottlerHeaderAdapter wrong_adapter("", "yesplease", 200); |
488 no_opt_out_entry->UpdateWithResponse("www.nike.com", &wrong_adapter); | 507 no_opt_out_entry->UpdateWithResponse("www.nike.com", &wrong_adapter); |
489 ExpectEntryAllowsAllOnErrorIfOptedOut(no_opt_out_entry, false); | 508 ExpectEntryAllowsAllOnErrorIfOptedOut(no_opt_out_entry, false, request_); |
490 | 509 |
491 // A localhost entry should always be opted out. | 510 // A localhost entry should always be opted out. |
492 scoped_refptr<net::URLRequestThrottlerEntryInterface> localhost_entry = | 511 scoped_refptr<net::URLRequestThrottlerEntryInterface> localhost_entry = |
493 manager.RegisterRequestUrl(GURL("http://localhost/hello")); | 512 manager.RegisterRequestUrl(GURL("http://localhost/hello")); |
494 ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry, true); | 513 ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry, true, request_); |
495 } | 514 } |
496 | 515 |
497 TEST(URLRequestThrottlerManager, ClearOnNetworkChange) { | 516 TEST_F(URLRequestThrottlerManagerTest, ClearOnNetworkChange) { |
498 for (int i = 0; i < 3; ++i) { | 517 for (int i = 0; i < 3; ++i) { |
499 MockURLRequestThrottlerManager manager; | 518 MockURLRequestThrottlerManager manager; |
500 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_before = | 519 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_before = |
501 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 520 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
502 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); | 521 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); |
503 for (int j = 0; j < 10; ++j) { | 522 for (int j = 0; j < 10; ++j) { |
504 // Host doesn't really matter in this scenario so we skip it. | 523 // Host doesn't really matter in this scenario so we skip it. |
505 entry_before->UpdateWithResponse("", &failure_adapter); | 524 entry_before->UpdateWithResponse("", &failure_adapter); |
506 } | 525 } |
507 EXPECT_TRUE(entry_before->ShouldRejectRequest(0)); | 526 EXPECT_TRUE(entry_before->ShouldRejectRequest(request_)); |
508 | 527 |
509 switch (i) { | 528 switch (i) { |
510 case 0: | 529 case 0: |
511 manager.OnIPAddressChanged(); | 530 manager.OnIPAddressChanged(); |
512 break; | 531 break; |
513 case 1: | 532 case 1: |
514 manager.OnConnectionTypeChanged( | 533 manager.OnConnectionTypeChanged( |
515 net::NetworkChangeNotifier::CONNECTION_UNKNOWN); | 534 net::NetworkChangeNotifier::CONNECTION_UNKNOWN); |
516 break; | 535 break; |
517 case 2: | 536 case 2: |
518 manager.OnConnectionTypeChanged( | 537 manager.OnConnectionTypeChanged( |
519 net::NetworkChangeNotifier::CONNECTION_NONE); | 538 net::NetworkChangeNotifier::CONNECTION_NONE); |
520 break; | 539 break; |
521 default: | 540 default: |
522 FAIL(); | 541 FAIL(); |
523 } | 542 } |
524 | 543 |
525 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | 544 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = |
526 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 545 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
527 EXPECT_FALSE(entry_after->ShouldRejectRequest(0)); | 546 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); |
528 } | 547 } |
529 } | 548 } |
530 | 549 |
531 } // namespace net | 550 } // namespace net |
OLD | NEW |