Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "content/browser/loader/throttling_resource_handler.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/memory/scoped_vector.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "content/browser/loader/mock_resource_loader.h" | |
| 14 #include "content/browser/loader/resource_request_info_impl.h" | |
| 15 #include "content/browser/loader/test_resource_handler.h" | |
| 16 #include "content/public/browser/resource_throttle.h" | |
| 17 #include "content/public/common/resource_response.h" | |
| 18 #include "net/base/request_priority.h" | |
| 19 #include "net/url_request/redirect_info.h" | |
| 20 #include "net/url_request/url_request.h" | |
| 21 #include "net/url_request/url_request_test_util.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 #include "url/gurl.h" | |
| 24 | |
| 25 namespace content { | |
| 26 namespace { | |
| 27 | |
| 28 const char kInitialUrl[] = "http://initial/"; | |
| 29 const char kRedirectUrl[] = "http://redirect/"; | |
| 30 | |
| 31 class TestResourceThrottle : public ResourceThrottle { | |
| 32 public: | |
| 33 explicit TestResourceThrottle(TestResourceThrottle* previous_throttle) { | |
| 34 if (previous_throttle) { | |
| 35 DCHECK(!previous_throttle->next_throttle_); | |
| 36 previous_throttle_ = previous_throttle; | |
| 37 previous_throttle_->next_throttle_ = this; | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 ~TestResourceThrottle() override {} | |
| 42 | |
| 43 // Sets the throttle after this one, to enable checks that they're called in | |
| 44 // the expected order. | |
| 45 void SetNextThrottle(TestResourceThrottle* throttle) { | |
| 46 DCHECK(!next_throttle_); | |
| 47 DCHECK(!throttle->previous_throttle_); | |
| 48 | |
| 49 next_throttle_ = throttle; | |
| 50 throttle->previous_throttle_ = this; | |
| 51 } | |
| 52 | |
| 53 // ResourceThrottle implemenation: | |
| 54 | |
| 55 void WillStartRequest(bool* defer) override { | |
| 56 EXPECT_EQ(0, will_start_request_called_); | |
| 57 EXPECT_EQ(0, will_redirect_request_called_); | |
| 58 EXPECT_EQ(0, will_process_response_called_); | |
| 59 | |
| 60 if (previous_throttle_) { | |
| 61 EXPECT_EQ(1, previous_throttle_->will_start_request_called_); | |
| 62 EXPECT_EQ(0, previous_throttle_->will_redirect_request_called_); | |
| 63 EXPECT_EQ(0, previous_throttle_->will_process_response_called_); | |
| 64 } | |
| 65 | |
| 66 if (next_throttle_) { | |
| 67 EXPECT_EQ(0, next_throttle_->will_start_request_called_); | |
| 68 EXPECT_EQ(0, next_throttle_->will_redirect_request_called_); | |
| 69 EXPECT_EQ(0, next_throttle_->will_process_response_called_); | |
| 70 } | |
| 71 | |
| 72 ++will_start_request_called_; | |
| 73 *defer = defer_on_will_start_request_; | |
| 74 if (cancel_on_will_start_request_) | |
| 75 CancelWithError(net::ERR_UNEXPECTED); | |
| 76 } | |
| 77 | |
| 78 void WillRedirectRequest(const net::RedirectInfo& redirect_info, | |
| 79 bool* defer) override { | |
| 80 EXPECT_EQ(GURL(kRedirectUrl), redirect_info.new_url); | |
| 81 | |
| 82 EXPECT_EQ(1, will_start_request_called_); | |
| 83 // None of these tests use multiple redirects. | |
| 84 EXPECT_EQ(0, will_redirect_request_called_); | |
| 85 EXPECT_EQ(0, will_process_response_called_); | |
| 86 | |
| 87 if (previous_throttle_) { | |
| 88 EXPECT_EQ(1, previous_throttle_->will_start_request_called_); | |
| 89 EXPECT_EQ(1, previous_throttle_->will_redirect_request_called_); | |
| 90 EXPECT_EQ(0, previous_throttle_->will_process_response_called_); | |
| 91 } | |
| 92 | |
| 93 if (next_throttle_) { | |
| 94 EXPECT_EQ(1, next_throttle_->will_start_request_called_); | |
| 95 EXPECT_EQ(0, next_throttle_->will_redirect_request_called_); | |
| 96 EXPECT_EQ(0, next_throttle_->will_process_response_called_); | |
| 97 } | |
| 98 | |
| 99 ++will_redirect_request_called_; | |
| 100 *defer = defer_on_will_redirect_request_; | |
| 101 if (cancel_on_will_redirect_request_) | |
| 102 CancelWithError(net::ERR_UNEXPECTED); | |
| 103 } | |
| 104 | |
| 105 void WillProcessResponse(bool* defer) override { | |
| 106 EXPECT_EQ(0, will_process_response_called_); | |
| 107 | |
| 108 if (previous_throttle_) | |
| 109 EXPECT_EQ(1, previous_throttle_->will_process_response_called_); | |
| 110 | |
| 111 if (next_throttle_) | |
| 112 EXPECT_EQ(0, next_throttle_->will_process_response_called_); | |
| 113 | |
| 114 ++will_process_response_called_; | |
| 115 *defer = defer_on_will_process_response_; | |
| 116 if (cancel_on_will_process_response_) | |
| 117 CancelWithError(net::ERR_UNEXPECTED); | |
| 118 } | |
| 119 | |
| 120 const char* GetNameForLogging() const override { return "Hank"; } | |
| 121 | |
| 122 int will_start_request_called() const { return will_start_request_called_; } | |
| 123 int will_redirect_request_called() const { | |
| 124 return will_redirect_request_called_; | |
| 125 } | |
| 126 int will_process_response_called() const { | |
| 127 return will_process_response_called_; | |
| 128 } | |
| 129 | |
| 130 void set_defer_on_will_start_request(bool defer_on_will_start_request) { | |
| 131 defer_on_will_start_request_ = defer_on_will_start_request; | |
| 132 } | |
| 133 void set_defer_on_will_redirect_request(bool defer_on_will_redirect_request) { | |
| 134 defer_on_will_redirect_request_ = defer_on_will_redirect_request; | |
| 135 } | |
| 136 void set_defer_on_will_process_response(bool defer_on_will_process_response) { | |
| 137 defer_on_will_process_response_ = defer_on_will_process_response; | |
| 138 } | |
| 139 | |
| 140 void set_cancel_on_will_start_request(bool cancel_on_will_start_request) { | |
| 141 cancel_on_will_start_request_ = cancel_on_will_start_request; | |
| 142 } | |
| 143 void set_cancel_on_will_redirect_request( | |
| 144 bool cancel_on_will_redirect_request) { | |
| 145 cancel_on_will_redirect_request_ = cancel_on_will_redirect_request; | |
| 146 } | |
| 147 void set_cancel_on_will_process_response( | |
| 148 bool cancel_on_will_process_response) { | |
| 149 cancel_on_will_process_response_ = cancel_on_will_process_response; | |
| 150 } | |
| 151 | |
| 152 using ResourceThrottle::Resume; | |
| 153 using ResourceThrottle::CancelWithError; | |
| 154 | |
| 155 private: | |
| 156 int will_start_request_called_ = 0; | |
| 157 int will_redirect_request_called_ = 0; | |
| 158 int will_process_response_called_ = 0; | |
| 159 | |
| 160 bool defer_on_will_start_request_ = false; | |
| 161 bool defer_on_will_redirect_request_ = false; | |
| 162 bool defer_on_will_process_response_ = false; | |
| 163 | |
| 164 bool cancel_on_will_start_request_ = false; | |
| 165 bool cancel_on_will_redirect_request_ = false; | |
| 166 bool cancel_on_will_process_response_ = false; | |
| 167 | |
| 168 TestResourceThrottle* previous_throttle_ = nullptr; | |
| 169 TestResourceThrottle* next_throttle_ = nullptr; | |
| 170 | |
| 171 DISALLOW_COPY_AND_ASSIGN(TestResourceThrottle); | |
| 172 }; | |
| 173 | |
| 174 class ThrottlingResourceHandlerTest : public testing::Test { | |
| 175 public: | |
| 176 ThrottlingResourceHandlerTest() | |
| 177 : never_started_url_request_(request_context_.CreateRequest( | |
| 178 GURL(kInitialUrl), | |
| 179 net::DEFAULT_PRIORITY, | |
| 180 &never_started_url_request_delegate_)), | |
| 181 throttle1_(new TestResourceThrottle(nullptr)), | |
| 182 throttle2_(new TestResourceThrottle(throttle1_)), | |
| 183 test_handler_(new TestResourceHandler()) { | |
| 184 ScopedVector<ResourceThrottle> throttles; | |
| 185 throttles.push_back(base::WrapUnique(throttle1_)); | |
| 186 throttles.push_back(base::WrapUnique(throttle2_)); | |
| 187 throttling_handler_.reset(new ThrottlingResourceHandler( | |
| 188 base::WrapUnique(test_handler_), never_started_url_request_.get(), | |
| 189 std::move(throttles))); | |
| 190 mock_loader_.reset(new MockResourceLoader(throttling_handler_.get())); | |
| 191 | |
| 192 // Basic initial state sanity checks. | |
| 193 EXPECT_EQ(0, test_handler_->on_will_start_called()); | |
| 194 EXPECT_EQ(0, throttle1_->will_start_request_called()); | |
| 195 EXPECT_EQ(0, throttle2_->will_start_request_called()); | |
| 196 } | |
| 197 | |
| 198 // Finish the request with a 0-byte read and success. All of this is just | |
| 199 // passed through to the TestResourceHandler, so is uninterested for these | |
|
Randy Smith (Not in Mondays)
2016/12/20 22:16:41
nit: "is uninterested" ?
mmenke
2017/01/04 22:02:29
Done.
| |
| 200 // tests. | |
| 201 void FinishRequestSuccessfully() { | |
| 202 EXPECT_EQ(0, test_handler_->on_will_read_called()); | |
| 203 | |
| 204 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead(1)); | |
| 205 EXPECT_EQ(1, test_handler_->on_will_read_called()); | |
| 206 EXPECT_EQ(0, test_handler_->on_read_completed_called()); | |
| 207 | |
| 208 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 209 mock_loader_->OnReadCompleted(0)); | |
| 210 EXPECT_EQ(1, test_handler_->on_read_completed_called()); | |
| 211 EXPECT_EQ(0, test_handler_->on_response_completed_called()); | |
| 212 | |
| 213 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 214 mock_loader_->OnResponseCompleted( | |
| 215 net::URLRequestStatus::FromError(net::OK))); | |
| 216 EXPECT_EQ(net::OK, mock_loader_->error_code()); | |
| 217 EXPECT_EQ(1, test_handler_->on_read_completed_called()); | |
| 218 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 219 } | |
| 220 | |
| 221 protected: | |
| 222 // Needs to be first, so it's destroyed last. | |
| 223 base::MessageLoopForIO message_loop_; | |
| 224 | |
| 225 // Machinery to construct a URLRequest that's just used as an argument to | |
| 226 // methods that expect one, and is never actually started. | |
| 227 net::TestURLRequestContext request_context_; | |
| 228 net::TestDelegate never_started_url_request_delegate_; | |
| 229 std::unique_ptr<net::URLRequest> never_started_url_request_; | |
| 230 | |
| 231 // Owned by test_handler_; | |
| 232 TestResourceThrottle* throttle1_; | |
| 233 TestResourceThrottle* throttle2_; | |
| 234 | |
| 235 // Owned by |throttling_handler_|. | |
| 236 TestResourceHandler* test_handler_; | |
| 237 std::unique_ptr<ThrottlingResourceHandler> throttling_handler_; | |
| 238 std::unique_ptr<MockResourceLoader> mock_loader_; | |
| 239 | |
| 240 private: | |
| 241 DISALLOW_COPY_AND_ASSIGN(ThrottlingResourceHandlerTest); | |
| 242 }; | |
| 243 | |
| 244 TEST_F(ThrottlingResourceHandlerTest, Sync) { | |
| 245 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 246 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 247 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 248 EXPECT_EQ(0, throttle1_->will_redirect_request_called()); | |
| 249 | |
| 250 EXPECT_EQ(1, throttle2_->will_start_request_called()); | |
| 251 | |
| 252 EXPECT_EQ(1, test_handler_->on_will_start_called()); | |
| 253 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 254 | |
| 255 net::RedirectInfo redirect_info; | |
| 256 redirect_info.status_code = 301; | |
| 257 redirect_info.new_url = GURL(kRedirectUrl); | |
| 258 ASSERT_EQ( | |
| 259 MockResourceLoader::Status::IDLE, | |
| 260 mock_loader_->OnRequestRedirected(redirect_info, new ResourceResponse())); | |
| 261 EXPECT_EQ(1, throttle1_->will_redirect_request_called()); | |
| 262 EXPECT_EQ(0, throttle1_->will_process_response_called()); | |
| 263 | |
| 264 EXPECT_EQ(1, throttle2_->will_redirect_request_called()); | |
| 265 | |
| 266 EXPECT_EQ(1, test_handler_->on_request_redirected_called()); | |
| 267 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 268 | |
| 269 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 270 mock_loader_->OnResponseStarted(new ResourceResponse())); | |
| 271 EXPECT_EQ(1, throttle1_->will_process_response_called()); | |
| 272 EXPECT_EQ(1, throttle2_->will_process_response_called()); | |
| 273 | |
| 274 EXPECT_EQ(1, test_handler_->on_request_redirected_called()); | |
| 275 EXPECT_EQ(1, test_handler_->on_response_started_called()); | |
| 276 EXPECT_EQ(0, test_handler_->on_read_completed_called()); | |
| 277 | |
| 278 FinishRequestSuccessfully(); | |
| 279 } | |
| 280 | |
| 281 TEST_F(ThrottlingResourceHandlerTest, Async) { | |
| 282 throttle1_->set_defer_on_will_start_request(true); | |
| 283 throttle1_->set_defer_on_will_redirect_request(true); | |
| 284 throttle1_->set_defer_on_will_process_response(true); | |
| 285 | |
| 286 throttle2_->set_defer_on_will_start_request(true); | |
| 287 throttle2_->set_defer_on_will_redirect_request(true); | |
| 288 throttle2_->set_defer_on_will_process_response(true); | |
| 289 | |
| 290 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, | |
| 291 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 292 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 293 EXPECT_EQ(0, throttle2_->will_start_request_called()); | |
| 294 | |
| 295 throttle1_->Resume(); | |
| 296 EXPECT_EQ(1, throttle2_->will_start_request_called()); | |
| 297 EXPECT_EQ(0, test_handler_->on_will_start_called()); | |
| 298 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, | |
| 299 mock_loader_->status()); | |
| 300 | |
| 301 throttle2_->Resume(); | |
| 302 EXPECT_EQ(1, test_handler_->on_will_start_called()); | |
| 303 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->status()); | |
| 304 | |
| 305 EXPECT_EQ(0, throttle1_->will_redirect_request_called()); | |
| 306 net::RedirectInfo redirect_info; | |
| 307 redirect_info.status_code = 301; | |
| 308 redirect_info.new_url = GURL(kRedirectUrl); | |
| 309 ASSERT_EQ( | |
| 310 MockResourceLoader::Status::CALLBACK_PENDING, | |
| 311 mock_loader_->OnRequestRedirected(redirect_info, new ResourceResponse())); | |
| 312 EXPECT_EQ(1, throttle1_->will_redirect_request_called()); | |
| 313 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 314 | |
| 315 throttle1_->Resume(); | |
| 316 EXPECT_EQ(1, throttle2_->will_redirect_request_called()); | |
| 317 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 318 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, | |
| 319 mock_loader_->status()); | |
| 320 | |
| 321 throttle2_->Resume(); | |
| 322 EXPECT_EQ(1, test_handler_->on_request_redirected_called()); | |
| 323 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->status()); | |
| 324 | |
| 325 EXPECT_EQ(0, throttle1_->will_process_response_called()); | |
| 326 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, | |
| 327 mock_loader_->OnResponseStarted(new ResourceResponse())); | |
| 328 EXPECT_EQ(1, throttle1_->will_process_response_called()); | |
| 329 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 330 | |
| 331 throttle1_->Resume(); | |
| 332 EXPECT_EQ(1, throttle2_->will_process_response_called()); | |
| 333 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 334 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, | |
| 335 mock_loader_->status()); | |
| 336 | |
| 337 throttle2_->Resume(); | |
| 338 EXPECT_EQ(1, test_handler_->on_request_redirected_called()); | |
| 339 EXPECT_EQ(1, test_handler_->on_response_started_called()); | |
| 340 EXPECT_EQ(0, test_handler_->on_read_completed_called()); | |
| 341 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->status()); | |
| 342 | |
| 343 FinishRequestSuccessfully(); | |
| 344 } | |
| 345 | |
| 346 // For a given method (WillStartRequest, WillRedirectRequest, | |
| 347 // WillProcessResponse), each of the two throttles can cancel asynchronously or | |
| 348 // synchronously, and the second throttle can cancel synchronously or | |
| 349 // asynchronously after the first throttle completes synchronously or | |
| 350 // asynchronously, for a total of 6 combinations. However: | |
|
Randy Smith (Not in Mondays)
2016/12/20 22:16:42
This comment is true if all that is being tested i
mmenke
2017/01/04 22:02:29
Done.
| |
| 351 // 1) Whenever the second throttle cancels asynchronously, it doesn't matter if | |
| 352 // the first one completed synchronously or asynchronously, the state when it | |
| 353 // cancels is the same. | |
| 354 // 2) The second cancelling asynchronously is much like the first one | |
| 355 // cancelling asynchronously, so isn't worth testing individually. | |
| 356 // 3) Similarly, the second cancelling synchronously after the first one | |
| 357 // completes synchronously doesn't really add anything to the first cancelling | |
| 358 // synchronously. | |
| 359 // So that leaves 3 interesting test cases for each of the three points | |
| 360 // throttles can cancel. | |
| 361 | |
| 362 TEST_F(ThrottlingResourceHandlerTest, FirstThrottleSyncCancelOnWillStart) { | |
| 363 throttle1_->set_cancel_on_will_start_request(true); | |
| 364 | |
| 365 ASSERT_EQ(MockResourceLoader::Status::CANCELED, | |
| 366 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 367 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 368 EXPECT_EQ(0, throttle2_->will_start_request_called()); | |
| 369 EXPECT_EQ(0, test_handler_->on_will_start_called()); | |
| 370 | |
| 371 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
|
Randy Smith (Not in Mondays)
2016/12/20 22:16:41
Worth a comment that this the the "URLRequest" res
mmenke
2017/01/04 22:02:29
Done.
| |
| 372 mock_loader_->OnResponseCompleted( | |
| 373 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 374 | |
| 375 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 376 EXPECT_EQ(0, throttle1_->will_redirect_request_called()); | |
| 377 EXPECT_EQ(0, throttle1_->will_process_response_called()); | |
| 378 | |
| 379 EXPECT_EQ(0, throttle2_->will_start_request_called()); | |
| 380 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 381 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 382 | |
| 383 EXPECT_EQ(0, test_handler_->on_will_start_called()); | |
| 384 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 385 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 386 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
|
Randy Smith (Not in Mondays)
2016/12/20 22:16:42
I wonder if it's worthwhile putting in a helper ro
mmenke
2017/01/04 22:02:29
TestHandler already does that, but I'm not sure th
Randy Smith (Not in Mondays)
2017/01/11 19:57:15
Acknowledged.
| |
| 387 } | |
| 388 | |
| 389 TEST_F(ThrottlingResourceHandlerTest, FirstThrottleAsyncCancelOnWillStart) { | |
| 390 throttle1_->set_defer_on_will_start_request(true); | |
| 391 | |
| 392 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, | |
| 393 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 394 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 395 EXPECT_EQ(0, throttle2_->will_start_request_called()); | |
| 396 EXPECT_EQ(0, test_handler_->on_will_start_called()); | |
| 397 | |
| 398 throttle1_->CancelWithError(net::ERR_UNEXPECTED); | |
| 399 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 400 EXPECT_EQ(0, throttle2_->will_start_request_called()); | |
| 401 EXPECT_EQ(0, test_handler_->on_will_start_called()); | |
| 402 ASSERT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); | |
| 403 | |
| 404 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 405 mock_loader_->OnResponseCompleted( | |
| 406 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 407 | |
| 408 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 409 EXPECT_EQ(0, throttle1_->will_redirect_request_called()); | |
| 410 EXPECT_EQ(0, throttle1_->will_process_response_called()); | |
| 411 | |
| 412 EXPECT_EQ(0, throttle2_->will_start_request_called()); | |
| 413 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 414 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 415 | |
| 416 EXPECT_EQ(0, test_handler_->on_will_start_called()); | |
| 417 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 418 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 419 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 420 } | |
| 421 | |
| 422 // The first throttle also defers and then resumes the request. | |
|
Randy Smith (Not in Mondays)
2016/12/20 22:16:41
I presume this is because that's a "more complex"
mmenke
2017/01/04 22:02:29
It's so that it's a sync cancel with ThrottlingRes
Randy Smith (Not in Mondays)
2017/01/11 19:57:15
After several flip-flops, I agree with you--the di
| |
| 423 TEST_F(ThrottlingResourceHandlerTest, SecondThrottleSyncCancelOnWillStart) { | |
| 424 throttle1_->set_defer_on_will_start_request(true); | |
| 425 throttle2_->set_cancel_on_will_start_request(true); | |
| 426 | |
| 427 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, | |
| 428 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 429 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 430 EXPECT_EQ(0, throttle2_->will_start_request_called()); | |
| 431 EXPECT_EQ(0, test_handler_->on_will_start_called()); | |
| 432 | |
| 433 throttle1_->Resume(); | |
| 434 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 435 EXPECT_EQ(1, throttle2_->will_start_request_called()); | |
| 436 EXPECT_EQ(0, test_handler_->on_will_start_called()); | |
| 437 ASSERT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); | |
| 438 | |
| 439 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 440 mock_loader_->OnResponseCompleted( | |
| 441 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 442 | |
| 443 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 444 EXPECT_EQ(0, throttle1_->will_redirect_request_called()); | |
| 445 EXPECT_EQ(0, throttle1_->will_process_response_called()); | |
| 446 | |
| 447 EXPECT_EQ(1, throttle2_->will_start_request_called()); | |
| 448 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 449 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 450 | |
| 451 EXPECT_EQ(0, test_handler_->on_will_start_called()); | |
| 452 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 453 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 454 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 455 } | |
| 456 | |
| 457 TEST_F(ThrottlingResourceHandlerTest, | |
| 458 FirstThrottleSyncCancelOnRequestRedirected) { | |
| 459 throttle1_->set_cancel_on_will_redirect_request(true); | |
| 460 | |
| 461 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 462 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 463 | |
| 464 net::RedirectInfo redirect_info; | |
| 465 redirect_info.status_code = 301; | |
| 466 redirect_info.new_url = GURL(kRedirectUrl); | |
| 467 ASSERT_EQ( | |
| 468 MockResourceLoader::Status::CANCELED, | |
| 469 mock_loader_->OnRequestRedirected(redirect_info, new ResourceResponse())); | |
| 470 EXPECT_EQ(1, throttle1_->will_redirect_request_called()); | |
| 471 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 472 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 473 | |
| 474 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 475 mock_loader_->OnResponseCompleted( | |
| 476 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 477 | |
| 478 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 479 EXPECT_EQ(1, throttle1_->will_redirect_request_called()); | |
| 480 EXPECT_EQ(0, throttle1_->will_process_response_called()); | |
| 481 | |
| 482 EXPECT_EQ(1, throttle2_->will_start_request_called()); | |
| 483 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 484 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 485 | |
| 486 EXPECT_EQ(1, test_handler_->on_will_start_called()); | |
| 487 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 488 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 489 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 490 } | |
| 491 | |
| 492 TEST_F(ThrottlingResourceHandlerTest, | |
| 493 FirstThrottleAsyncCancelOnRequestRedirected) { | |
| 494 throttle1_->set_defer_on_will_redirect_request(true); | |
| 495 | |
| 496 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 497 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 498 | |
| 499 net::RedirectInfo redirect_info; | |
| 500 redirect_info.status_code = 301; | |
| 501 redirect_info.new_url = GURL(kRedirectUrl); | |
| 502 ASSERT_EQ( | |
| 503 MockResourceLoader::Status::CALLBACK_PENDING, | |
| 504 mock_loader_->OnRequestRedirected(redirect_info, new ResourceResponse())); | |
| 505 | |
| 506 throttle1_->CancelWithError(net::ERR_UNEXPECTED); | |
| 507 EXPECT_EQ(1, throttle1_->will_redirect_request_called()); | |
| 508 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 509 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 510 ASSERT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); | |
| 511 | |
| 512 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 513 mock_loader_->OnResponseCompleted( | |
| 514 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 515 | |
| 516 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 517 EXPECT_EQ(1, throttle1_->will_redirect_request_called()); | |
| 518 EXPECT_EQ(0, throttle1_->will_process_response_called()); | |
| 519 | |
| 520 EXPECT_EQ(1, throttle2_->will_start_request_called()); | |
| 521 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 522 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 523 | |
| 524 EXPECT_EQ(1, test_handler_->on_will_start_called()); | |
| 525 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 526 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 527 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 528 } | |
| 529 | |
| 530 // The first throttle also defers and then resumes the request. | |
| 531 TEST_F(ThrottlingResourceHandlerTest, | |
| 532 SecondThrottleSyncCancelOnRequestRedirected) { | |
| 533 throttle1_->set_defer_on_will_redirect_request(true); | |
| 534 throttle2_->set_cancel_on_will_redirect_request(true); | |
| 535 | |
| 536 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 537 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 538 | |
| 539 net::RedirectInfo redirect_info; | |
| 540 redirect_info.status_code = 301; | |
| 541 redirect_info.new_url = GURL(kRedirectUrl); | |
| 542 ASSERT_EQ( | |
| 543 MockResourceLoader::Status::CALLBACK_PENDING, | |
| 544 mock_loader_->OnRequestRedirected(redirect_info, new ResourceResponse())); | |
| 545 | |
| 546 throttle1_->Resume(); | |
| 547 EXPECT_EQ(1, throttle1_->will_redirect_request_called()); | |
| 548 EXPECT_EQ(1, throttle2_->will_redirect_request_called()); | |
| 549 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 550 ASSERT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); | |
| 551 | |
| 552 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 553 mock_loader_->OnResponseCompleted( | |
| 554 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 555 | |
| 556 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 557 EXPECT_EQ(1, throttle1_->will_redirect_request_called()); | |
| 558 EXPECT_EQ(0, throttle1_->will_process_response_called()); | |
| 559 | |
| 560 EXPECT_EQ(1, throttle2_->will_start_request_called()); | |
| 561 EXPECT_EQ(1, throttle2_->will_redirect_request_called()); | |
| 562 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 563 | |
| 564 EXPECT_EQ(1, test_handler_->on_will_start_called()); | |
| 565 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 566 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 567 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 568 } | |
| 569 | |
| 570 TEST_F(ThrottlingResourceHandlerTest, | |
| 571 FirstThrottleSyncCancelOnWillProcessResponse) { | |
| 572 throttle1_->set_cancel_on_will_process_response(true); | |
| 573 | |
| 574 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 575 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 576 | |
| 577 ASSERT_EQ(MockResourceLoader::Status::CANCELED, | |
| 578 mock_loader_->OnResponseStarted(new ResourceResponse())); | |
| 579 EXPECT_EQ(1, throttle1_->will_process_response_called()); | |
| 580 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 581 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 582 | |
| 583 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 584 mock_loader_->OnResponseCompleted( | |
| 585 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 586 | |
| 587 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 588 EXPECT_EQ(0, throttle1_->will_redirect_request_called()); | |
| 589 EXPECT_EQ(1, throttle1_->will_process_response_called()); | |
| 590 | |
| 591 EXPECT_EQ(1, throttle2_->will_start_request_called()); | |
| 592 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 593 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 594 | |
| 595 EXPECT_EQ(1, test_handler_->on_will_start_called()); | |
| 596 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 597 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 598 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 599 } | |
| 600 | |
| 601 TEST_F(ThrottlingResourceHandlerTest, | |
| 602 FirstThrottleAsyncCancelOnWillProcessResponse) { | |
| 603 throttle1_->set_defer_on_will_process_response(true); | |
| 604 | |
| 605 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 606 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 607 | |
| 608 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, | |
| 609 mock_loader_->OnResponseStarted(new ResourceResponse())); | |
| 610 EXPECT_EQ(1, throttle1_->will_process_response_called()); | |
| 611 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 612 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 613 | |
| 614 throttle1_->CancelWithError(net::ERR_UNEXPECTED); | |
| 615 EXPECT_EQ(1, throttle1_->will_process_response_called()); | |
| 616 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 617 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 618 ASSERT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); | |
| 619 | |
| 620 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 621 mock_loader_->OnResponseCompleted( | |
| 622 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 623 | |
| 624 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 625 EXPECT_EQ(0, throttle1_->will_redirect_request_called()); | |
| 626 EXPECT_EQ(1, throttle1_->will_process_response_called()); | |
| 627 | |
| 628 EXPECT_EQ(1, throttle2_->will_start_request_called()); | |
| 629 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 630 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 631 | |
| 632 EXPECT_EQ(1, test_handler_->on_will_start_called()); | |
| 633 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 634 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 635 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 636 } | |
| 637 | |
| 638 // The first throttle also defers and then resumes the request. | |
| 639 TEST_F(ThrottlingResourceHandlerTest, | |
| 640 SecondThrottleSyncCancelOnWillProcessResponse) { | |
| 641 throttle1_->set_defer_on_will_process_response(true); | |
| 642 throttle2_->set_cancel_on_will_process_response(true); | |
| 643 | |
| 644 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 645 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 646 | |
| 647 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, | |
| 648 mock_loader_->OnResponseStarted(new ResourceResponse())); | |
| 649 EXPECT_EQ(1, throttle1_->will_process_response_called()); | |
| 650 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 651 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 652 | |
| 653 throttle1_->Resume(); | |
| 654 EXPECT_EQ(1, throttle1_->will_process_response_called()); | |
| 655 EXPECT_EQ(1, throttle2_->will_process_response_called()); | |
| 656 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 657 ASSERT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); | |
| 658 | |
| 659 ASSERT_EQ(MockResourceLoader::Status::IDLE, | |
| 660 mock_loader_->OnResponseCompleted( | |
| 661 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 662 | |
| 663 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 664 EXPECT_EQ(0, throttle1_->will_redirect_request_called()); | |
| 665 EXPECT_EQ(1, throttle1_->will_process_response_called()); | |
| 666 | |
| 667 EXPECT_EQ(1, throttle2_->will_start_request_called()); | |
| 668 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 669 EXPECT_EQ(1, throttle2_->will_process_response_called()); | |
| 670 | |
| 671 EXPECT_EQ(1, test_handler_->on_will_start_called()); | |
| 672 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 673 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 674 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 675 } | |
| 676 | |
| 677 TEST_F(ThrottlingResourceHandlerTest, OutOfBandCancelBeforeWillStart) { | |
|
Randy Smith (Not in Mondays)
2016/12/20 22:16:41
As noted elsewhere, I'd like to understand the OOB
Randy Smith (Not in Mondays)
2017/01/11 19:57:15
As noted elsewhere, accepted for this CL.
| |
| 678 throttle1_->CancelWithError(net::ERR_UNEXPECTED); | |
| 679 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); | |
| 680 EXPECT_EQ(net::ERR_UNEXPECTED, mock_loader_->error_code()); | |
| 681 | |
| 682 EXPECT_EQ(MockResourceLoader::Status::IDLE, | |
| 683 mock_loader_->OnResponseCompleted( | |
| 684 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 685 | |
| 686 EXPECT_EQ(0, throttle1_->will_start_request_called()); | |
| 687 EXPECT_EQ(0, throttle1_->will_redirect_request_called()); | |
| 688 EXPECT_EQ(0, throttle1_->will_process_response_called()); | |
| 689 | |
| 690 EXPECT_EQ(0, throttle2_->will_start_request_called()); | |
| 691 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 692 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 693 | |
| 694 EXPECT_EQ(0, test_handler_->on_will_start_called()); | |
| 695 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 696 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 697 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 698 } | |
| 699 | |
| 700 TEST_F(ThrottlingResourceHandlerTest, OutOfBandCancelAfterWillStart) { | |
| 701 EXPECT_EQ(MockResourceLoader::Status::IDLE, | |
| 702 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 703 | |
| 704 throttle1_->CancelWithError(net::ERR_UNEXPECTED); | |
| 705 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); | |
| 706 EXPECT_EQ(net::ERR_UNEXPECTED, mock_loader_->error_code()); | |
| 707 | |
| 708 EXPECT_EQ(MockResourceLoader::Status::IDLE, | |
| 709 mock_loader_->OnResponseCompleted( | |
| 710 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 711 | |
| 712 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 713 EXPECT_EQ(0, throttle1_->will_redirect_request_called()); | |
| 714 EXPECT_EQ(0, throttle1_->will_process_response_called()); | |
| 715 | |
| 716 EXPECT_EQ(1, throttle2_->will_start_request_called()); | |
| 717 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 718 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 719 | |
| 720 EXPECT_EQ(1, test_handler_->on_will_start_called()); | |
| 721 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 722 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 723 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 724 } | |
| 725 | |
| 726 TEST_F(ThrottlingResourceHandlerTest, OutOfBandCancelAfterRequestRedirected) { | |
| 727 EXPECT_EQ(MockResourceLoader::Status::IDLE, | |
| 728 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 729 net::RedirectInfo redirect_info; | |
| 730 redirect_info.status_code = 301; | |
| 731 redirect_info.new_url = GURL(kRedirectUrl); | |
| 732 EXPECT_EQ( | |
| 733 MockResourceLoader::Status::IDLE, | |
| 734 mock_loader_->OnRequestRedirected(redirect_info, new ResourceResponse())); | |
| 735 | |
| 736 throttle1_->CancelWithError(net::ERR_UNEXPECTED); | |
| 737 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); | |
| 738 EXPECT_EQ(net::ERR_UNEXPECTED, mock_loader_->error_code()); | |
| 739 | |
| 740 EXPECT_EQ(MockResourceLoader::Status::IDLE, | |
| 741 mock_loader_->OnResponseCompleted( | |
| 742 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 743 | |
| 744 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 745 EXPECT_EQ(1, throttle1_->will_redirect_request_called()); | |
| 746 EXPECT_EQ(0, throttle1_->will_process_response_called()); | |
| 747 | |
| 748 EXPECT_EQ(1, throttle2_->will_start_request_called()); | |
| 749 EXPECT_EQ(1, throttle2_->will_redirect_request_called()); | |
| 750 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 751 | |
| 752 EXPECT_EQ(1, test_handler_->on_will_start_called()); | |
| 753 EXPECT_EQ(1, test_handler_->on_request_redirected_called()); | |
| 754 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 755 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 756 } | |
| 757 | |
| 758 TEST_F(ThrottlingResourceHandlerTest, OutOfBandCancelAfterResponseStarted) { | |
| 759 EXPECT_EQ(MockResourceLoader::Status::IDLE, | |
| 760 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 761 net::RedirectInfo redirect_info; | |
| 762 redirect_info.status_code = 301; | |
| 763 redirect_info.new_url = GURL(kRedirectUrl); | |
| 764 EXPECT_EQ( | |
| 765 MockResourceLoader::Status::IDLE, | |
| 766 mock_loader_->OnRequestRedirected(redirect_info, new ResourceResponse())); | |
| 767 EXPECT_EQ(MockResourceLoader::Status::IDLE, | |
| 768 mock_loader_->OnResponseStarted(new ResourceResponse())); | |
| 769 | |
| 770 throttle1_->CancelWithError(net::ERR_UNEXPECTED); | |
| 771 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); | |
| 772 EXPECT_EQ(net::ERR_UNEXPECTED, mock_loader_->error_code()); | |
| 773 | |
| 774 EXPECT_EQ(MockResourceLoader::Status::IDLE, | |
| 775 mock_loader_->OnResponseCompleted( | |
| 776 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 777 | |
| 778 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 779 EXPECT_EQ(1, throttle1_->will_redirect_request_called()); | |
| 780 EXPECT_EQ(1, throttle1_->will_process_response_called()); | |
| 781 | |
| 782 EXPECT_EQ(1, throttle2_->will_start_request_called()); | |
| 783 EXPECT_EQ(1, throttle2_->will_redirect_request_called()); | |
| 784 EXPECT_EQ(1, throttle2_->will_process_response_called()); | |
| 785 | |
| 786 EXPECT_EQ(1, test_handler_->on_will_start_called()); | |
| 787 EXPECT_EQ(1, test_handler_->on_request_redirected_called()); | |
| 788 EXPECT_EQ(1, test_handler_->on_response_started_called()); | |
| 789 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 790 } | |
| 791 | |
| 792 TEST_F(ThrottlingResourceHandlerTest, OutOfBandCancelAndResumeDuringWillStart) { | |
| 793 throttle1_->set_defer_on_will_start_request(1); | |
| 794 EXPECT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, | |
| 795 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 796 | |
| 797 // |throttle2_| cancels. | |
| 798 throttle2_->CancelWithError(net::ERR_UNEXPECTED); | |
| 799 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); | |
| 800 EXPECT_EQ(net::ERR_UNEXPECTED, mock_loader_->error_code()); | |
| 801 | |
| 802 // |throttle1_|, blissfully unaware of cancellation, resumes the request. | |
| 803 throttle1_->Resume(); | |
| 804 | |
| 805 EXPECT_EQ(MockResourceLoader::Status::IDLE, | |
| 806 mock_loader_->OnResponseCompleted( | |
| 807 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 808 | |
| 809 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 810 EXPECT_EQ(0, throttle1_->will_redirect_request_called()); | |
| 811 EXPECT_EQ(0, throttle1_->will_process_response_called()); | |
| 812 | |
| 813 EXPECT_EQ(0, throttle2_->will_start_request_called()); | |
| 814 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 815 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 816 | |
| 817 EXPECT_EQ(0, test_handler_->on_will_start_called()); | |
| 818 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 819 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 820 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 821 } | |
| 822 | |
| 823 TEST_F(ThrottlingResourceHandlerTest, DoubleCancelDuringWillStart) { | |
| 824 throttle1_->set_defer_on_will_start_request(1); | |
| 825 EXPECT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, | |
| 826 mock_loader_->OnWillStart(GURL(kInitialUrl))); | |
| 827 | |
| 828 // |throttle2_| cancels. | |
| 829 throttle2_->CancelWithError(net::ERR_UNEXPECTED); | |
| 830 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); | |
| 831 EXPECT_EQ(net::ERR_UNEXPECTED, mock_loader_->error_code()); | |
| 832 | |
| 833 // |throttle1_|, unaware of the cancellation, also cancels. | |
| 834 throttle1_->CancelWithError(net::ERR_FAILED); | |
| 835 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); | |
| 836 EXPECT_EQ(net::ERR_UNEXPECTED, mock_loader_->error_code()); | |
| 837 | |
| 838 EXPECT_EQ(MockResourceLoader::Status::IDLE, | |
| 839 mock_loader_->OnResponseCompleted( | |
| 840 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED))); | |
| 841 | |
| 842 EXPECT_EQ(1, throttle1_->will_start_request_called()); | |
| 843 EXPECT_EQ(0, throttle1_->will_redirect_request_called()); | |
| 844 EXPECT_EQ(0, throttle1_->will_process_response_called()); | |
| 845 | |
| 846 EXPECT_EQ(0, throttle2_->will_start_request_called()); | |
| 847 EXPECT_EQ(0, throttle2_->will_redirect_request_called()); | |
| 848 EXPECT_EQ(0, throttle2_->will_process_response_called()); | |
| 849 | |
| 850 EXPECT_EQ(0, test_handler_->on_will_start_called()); | |
| 851 EXPECT_EQ(0, test_handler_->on_request_redirected_called()); | |
| 852 EXPECT_EQ(0, test_handler_->on_response_started_called()); | |
| 853 EXPECT_EQ(1, test_handler_->on_response_completed_called()); | |
| 854 } | |
| 855 | |
| 856 } // namespace | |
| 857 } // namespace content | |
| OLD | NEW |