| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/url_request/url_request_backoff_manager.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/time/time.h" |
| 10 #include "net/http/http_response_headers.h" |
| 11 #include "net/http/http_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace net { |
| 15 |
| 16 namespace { |
| 17 |
| 18 class URLRequestBackoffManagerTest : public testing::Test { |
| 19 protected: |
| 20 URLRequestBackoffManagerTest() : manager_(new URLRequestBackoffManager) {} |
| 21 void RegisterURL(const GURL& url, |
| 22 int backoff_in_sec, |
| 23 const base::Time& request_time) { |
| 24 std::string raw_headers = base::StringPrintf( |
| 25 "HTTP/1.0 200 OK\n" |
| 26 "backoff: %d\n\n", |
| 27 backoff_in_sec); |
| 28 scoped_refptr<HttpResponseHeaders> headers(new HttpResponseHeaders( |
| 29 HttpUtil::AssembleRawHeaders(raw_headers.c_str(), raw_headers.size()))); |
| 30 manager_->UpdateWithResponse(url, headers.get(), request_time); |
| 31 } |
| 32 |
| 33 scoped_ptr<URLRequestBackoffManager> manager_; |
| 34 }; |
| 35 } // namespace |
| 36 |
| 37 TEST_F(URLRequestBackoffManagerTest, ShouldRejectRequest) { |
| 38 base::Time request_time = base::Time::Now(); |
| 39 RegisterURL(GURL("https://example.com"), 3600, request_time); |
| 40 ASSERT_EQ(1, manager_->GetNumberOfEntriesForTests()); |
| 41 ASSERT_TRUE(manager_->ShouldRejectRequest( |
| 42 GURL("https://example.com?q=v"), |
| 43 request_time + base::TimeDelta::FromSeconds(3500))); |
| 44 |
| 45 // Only can try once in the interval of |
| 46 // [|request_time| + 3600, |request_time| + 3600 * 1.1). |
| 47 ASSERT_FALSE(manager_->ShouldRejectRequest( |
| 48 GURL("https://example.com?q=v"), |
| 49 request_time + base::TimeDelta::FromSeconds(3700))); |
| 50 ASSERT_TRUE(manager_->ShouldRejectRequest( |
| 51 GURL("https://example.com?q=v"), |
| 52 request_time + base::TimeDelta::FromSeconds(3700))); |
| 53 ASSERT_EQ(1, manager_->GetNumberOfEntriesForTests()); |
| 54 |
| 55 // After release time, throttling should not be applied. |
| 56 ASSERT_FALSE(manager_->ShouldRejectRequest( |
| 57 GURL("https://example.com?q=v"), |
| 58 request_time + base::TimeDelta::FromSeconds(3960))); |
| 59 } |
| 60 |
| 61 TEST_F(URLRequestBackoffManagerTest, MisconfiguredHeaders) { |
| 62 // Backoff time is smaller than the minimum allowed. |
| 63 RegisterURL(GURL("https://example.com"), |
| 64 URLRequestBackoffManager::kMinimumBackoffInSeconds - 1, |
| 65 base::Time::Now()); |
| 66 ASSERT_EQ(0, manager_->GetNumberOfEntriesForTests()); |
| 67 |
| 68 // Backoff time is bigger than the maximum allowed. |
| 69 RegisterURL(GURL("https://example.com"), |
| 70 URLRequestBackoffManager::kMaximumBackoffInSeconds + 1, |
| 71 base::Time::Now()); |
| 72 ASSERT_EQ(0, manager_->GetNumberOfEntriesForTests()); |
| 73 } |
| 74 |
| 75 TEST_F(URLRequestBackoffManagerTest, ShouldGarbageCollect) { |
| 76 base::Time request_time = |
| 77 base::Time::Now() - base::TimeDelta::FromSeconds(60); |
| 78 for (int i = 0; |
| 79 i < URLRequestBackoffManager::kNewEntriesBetweenCollecting - 1; i++) { |
| 80 RegisterURL(GURL(base::StringPrintf("http://example%d.com", i)), 10, |
| 81 request_time); |
| 82 ASSERT_EQ(i + 1, manager_->GetNumberOfEntriesForTests()); |
| 83 } |
| 84 // Should clear all previous outdated entries. |
| 85 RegisterURL(GURL("http://example.com"), 10, base::Time::Now()); |
| 86 ASSERT_EQ(1, manager_->GetNumberOfEntriesForTests()); |
| 87 } |
| 88 |
| 89 TEST_F(URLRequestBackoffManagerTest, ClearOnNetworkChange) { |
| 90 for (int i = 0; i < 3; ++i) { |
| 91 RegisterURL(GURL("http://www.example.com/"), 60, base::Time::Now()); |
| 92 ASSERT_EQ(1, manager_->GetNumberOfEntriesForTests()); |
| 93 EXPECT_TRUE(manager_->ShouldRejectRequest(GURL("http://www.example.com/"), |
| 94 base::Time::Now())); |
| 95 switch (i) { |
| 96 case 0: |
| 97 manager_->OnIPAddressChanged(); |
| 98 break; |
| 99 case 1: |
| 100 manager_->OnConnectionTypeChanged( |
| 101 NetworkChangeNotifier::CONNECTION_UNKNOWN); |
| 102 break; |
| 103 case 2: |
| 104 manager_->OnConnectionTypeChanged( |
| 105 NetworkChangeNotifier::CONNECTION_NONE); |
| 106 break; |
| 107 default: |
| 108 FAIL(); |
| 109 } |
| 110 |
| 111 EXPECT_FALSE(manager_->ShouldRejectRequest(GURL("http://www.example.com/"), |
| 112 base::Time::Now())); |
| 113 ASSERT_EQ(0, manager_->GetNumberOfEntriesForTests()); |
| 114 } |
| 115 } |
| 116 |
| 117 } // namespace net |
| OLD | NEW |