Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / net / url_request / url_request_backoff_manager_unittest.cc
blob21a892e9d344db8559d6250f4b3645429c37ea04
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.
5 #include "net/url_request/url_request_backoff_manager.h"
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"
14 namespace net {
16 namespace {
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);
33 scoped_ptr<URLRequestBackoffManager> manager_;
35 } // namespace
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)));
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());
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)));
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());
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());
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());
84 // Should clear all previous outdated entries.
85 RegisterURL(GURL("http://example.com"), 10, base::Time::Now());
86 ASSERT_EQ(1, manager_->GetNumberOfEntriesForTests());
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();
111 EXPECT_FALSE(manager_->ShouldRejectRequest(GURL("http://www.example.com/"),
112 base::Time::Now()));
113 ASSERT_EQ(0, manager_->GetNumberOfEntriesForTests());
117 } // namespace net