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