Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / extensions / extension_request_limiting_throttle_browsertest.cc
blobcff93452afe7c8fd62cde2186f397208f315d1b6
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 "base/bind.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/extensions/extension_browsertest.h"
10 #include "chrome/browser/profiles/profile_io_data.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "chrome/test/base/ui_test_utils.h"
13 #include "extensions/browser/extension_throttle_manager.h"
14 #include "extensions/test/result_catcher.h"
15 #include "net/base/backoff_entry.h"
16 #include "net/base/url_util.h"
17 #include "net/dns/mock_host_resolver.h"
18 #include "net/test/embedded_test_server/embedded_test_server.h"
19 #include "net/test/embedded_test_server/http_request.h"
20 #include "net/test/embedded_test_server/http_response.h"
22 namespace extensions {
24 namespace {
26 scoped_ptr<net::test_server::HttpResponse> HandleRequest(
27 bool set_cache_header_redirect_page,
28 bool set_cache_header_test_throttle_page,
29 const net::test_server::HttpRequest& request) {
30 if (base::StartsWith(request.relative_url, "/redirect",
31 base::CompareCase::SENSITIVE)) {
32 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
33 new net::test_server::BasicHttpResponse());
34 http_response->set_code(net::HTTP_FOUND);
35 http_response->set_content("Redirecting...");
36 http_response->set_content_type("text/plain");
37 http_response->AddCustomHeader("Location", "/test_throttle");
38 if (set_cache_header_redirect_page)
39 http_response->AddCustomHeader("Cache-Control", "max-age=3600");
40 return http_response.Pass();
43 if (base::StartsWith(request.relative_url, "/test_throttle",
44 base::CompareCase::SENSITIVE)) {
45 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
46 new net::test_server::BasicHttpResponse());
47 http_response->set_code(net::HTTP_SERVICE_UNAVAILABLE);
48 http_response->set_content("The server is overloaded right now.");
49 http_response->set_content_type("text/plain");
50 if (set_cache_header_test_throttle_page)
51 http_response->AddCustomHeader("Cache-Control", "max-age=3600");
52 return http_response.Pass();
55 // Unhandled requests result in the Embedded test server sending a 404.
56 return scoped_ptr<net::test_server::BasicHttpResponse>();
59 } // namespace
61 class ExtensionRequestLimitingThrottleBrowserTest
62 : public ExtensionBrowserTest {
63 public:
64 void SetUpOnMainThread() override {
65 ExtensionBrowserTest::SetUpOnMainThread();
66 ProfileIOData* io_data =
67 ProfileIOData::FromResourceContext(profile()->GetResourceContext());
68 ExtensionThrottleManager* manager = io_data->GetExtensionThrottleManager();
69 if (manager) {
70 // Requests issued within within |kUserGestureWindowMs| of a user gesture
71 // are also considered as user gestures (see
72 // resource_dispatcher_host_impl.cc), so these tests need to bypass the
73 // checking of the net::LOAD_MAYBE_USER_GESTURE load flag in the manager
74 // in order to test the throttling logic.
75 manager->SetIgnoreUserGestureLoadFlagForTests(true);
76 scoped_ptr<
77 net::BackoffEntry::Policy> policy(new net::BackoffEntry::Policy{
78 // Number of initial errors (in sequence) to ignore before applying
79 // exponential back-off rules.
82 // Initial delay for exponential back-off in ms.
83 10 * 60 * 1000,
85 // Factor by which the waiting time will be multiplied.
86 10,
88 // Fuzzing percentage. ex: 10% will spread requests randomly
89 // between 90%-100% of the calculated time.
90 0.1,
92 // Maximum amount of time we are willing to delay our request in ms.
93 15 * 60 * 1000,
95 // Time to keep an entry from being discarded even when it
96 // has no significant state, -1 to never discard.
97 -1,
99 // Don't use initial delay unless the last request was an error.
100 false,
102 manager->SetBackoffPolicyForTests(policy.Pass());
104 // Requests to 127.0.0.1 bypass throttling, so set up a host resolver rule
105 // to use a fake domain.
106 host_resolver()->AddRule("www.example.com", "127.0.0.1");
107 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
108 extension_ =
109 LoadExtension(test_data_dir_.AppendASCII("extension_throttle"));
110 ASSERT_TRUE(extension_);
113 void RunTest(const std::string& file_path, const std::string& request_url) {
114 ResultCatcher catcher;
115 GURL test_url = net::AppendQueryParameter(
116 extension_->GetResourceURL(file_path), "url", request_url);
117 ui_test_utils::NavigateToURL(browser(), test_url);
118 ASSERT_TRUE(catcher.GetNextResult());
121 private:
122 const Extension* extension_;
125 class ExtensionRequestLimitingThrottleCommandLineBrowserTest
126 : public ExtensionRequestLimitingThrottleBrowserTest {
127 public:
128 void SetUpCommandLine(base::CommandLine* command_line) override {
129 ExtensionRequestLimitingThrottleBrowserTest::SetUpCommandLine(command_line);
130 command_line->AppendSwitch(switches::kDisableExtensionsHttpThrottling);
134 // Tests that if the same URL is requested repeatedly by an extension, it will
135 // eventually be throttled.
136 IN_PROC_BROWSER_TEST_F(ExtensionRequestLimitingThrottleBrowserTest,
137 ThrottleRequest) {
138 embedded_test_server()->RegisterRequestHandler(
139 base::Bind(&HandleRequest, false, false));
140 ASSERT_NO_FATAL_FAILURE(
141 RunTest("test_request_eventually_throttled.html",
142 base::StringPrintf("http://www.example.com:%d/test_throttle",
143 embedded_test_server()->port())));
146 // Tests that if the same URL is repeatedly requested by an extension, and the
147 // response is served from the cache, it will not be throttled.
148 IN_PROC_BROWSER_TEST_F(ExtensionRequestLimitingThrottleBrowserTest,
149 DoNotThrottleCachedResponse) {
150 embedded_test_server()->RegisterRequestHandler(
151 base::Bind(&HandleRequest, false, true));
152 ASSERT_NO_FATAL_FAILURE(
153 RunTest("test_request_not_throttled.html",
154 base::StringPrintf("http://www.example.com:%d/test_throttle",
155 embedded_test_server()->port())));
158 // Tests that the redirected request is also being throttled.
159 IN_PROC_BROWSER_TEST_F(ExtensionRequestLimitingThrottleBrowserTest,
160 ThrottleRequest_Redirect) {
161 embedded_test_server()->RegisterRequestHandler(
162 base::Bind(&HandleRequest, false, false));
163 // Issue a bunch of requests to a url which gets redirected to a new url that
164 // generates 503.
165 ASSERT_NO_FATAL_FAILURE(
166 RunTest("test_request_eventually_throttled.html",
167 base::StringPrintf("http://www.example.com:%d/redirect",
168 embedded_test_server()->port())));
170 // Now requests to both URLs should be throttled. Explicitly validate that the
171 // second URL is throttled.
172 ASSERT_NO_FATAL_FAILURE(
173 RunTest("test_request_throttled_on_first_try.html",
174 base::StringPrintf("http://www.example.com:%d/test_throttle",
175 embedded_test_server()->port())));
178 // Tests that if both redirect (302) and non-redirect (503) responses are
179 // served from cache, the extension throttle does not throttle the request.
180 IN_PROC_BROWSER_TEST_F(ExtensionRequestLimitingThrottleBrowserTest,
181 DoNotThrottleCachedResponse_Redirect) {
182 embedded_test_server()->RegisterRequestHandler(
183 base::Bind(&HandleRequest, true, true));
184 ASSERT_NO_FATAL_FAILURE(
185 RunTest("test_request_not_throttled.html",
186 base::StringPrintf("http://www.example.com:%d/redirect",
187 embedded_test_server()->port())));
190 // Tests that if the redirect (302) is served from cache, but the non-redirect
191 // (503) is not, the extension throttle throttles the requests for the second
192 // url.
193 IN_PROC_BROWSER_TEST_F(ExtensionRequestLimitingThrottleBrowserTest,
194 ThrottleRequest_RedirectCached) {
195 embedded_test_server()->RegisterRequestHandler(
196 base::Bind(&HandleRequest, true, false));
197 ASSERT_NO_FATAL_FAILURE(
198 RunTest("test_request_eventually_throttled.html",
199 base::StringPrintf("http://www.example.com:%d/redirect",
200 embedded_test_server()->port())));
202 // Explicitly validate that the second URL is throttled.
203 ASSERT_NO_FATAL_FAILURE(
204 RunTest("test_request_throttled_on_first_try.html",
205 base::StringPrintf("http://www.example.com:%d/test_throttle",
206 embedded_test_server()->port())));
209 // Tests that if the redirect (302) is not served from cache, but the
210 // non-redirect (503) is, the extension throttle only throttles requests to the
211 // redirect URL.
212 IN_PROC_BROWSER_TEST_F(ExtensionRequestLimitingThrottleBrowserTest,
213 DoNotThrottleCachedResponse_NonRedirectCached) {
214 embedded_test_server()->RegisterRequestHandler(
215 base::Bind(&HandleRequest, false, true));
216 ASSERT_NO_FATAL_FAILURE(
217 RunTest("test_request_not_throttled.html",
218 base::StringPrintf("http://www.example.com:%d/redirect",
219 embedded_test_server()->port())));
221 // Explicitly validate that the second URL is not throttled.
222 ASSERT_NO_FATAL_FAILURE(
223 RunTest("test_request_not_throttled.html",
224 base::StringPrintf("http://www.example.com:%d/test_throttle",
225 embedded_test_server()->port())));
228 // Tests that if switches::kDisableExtensionsHttpThrottling is set on the
229 // command line, throttling is disabled.
230 IN_PROC_BROWSER_TEST_F(ExtensionRequestLimitingThrottleCommandLineBrowserTest,
231 ThrottleRequestDisabled) {
232 embedded_test_server()->RegisterRequestHandler(
233 base::Bind(&HandleRequest, false, false));
234 ASSERT_NO_FATAL_FAILURE(
235 RunTest("test_request_not_throttled.html",
236 base::StringPrintf("http://www.example.com:%d/test_throttle",
237 embedded_test_server()->port())));
240 } // namespace extensions