OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/loader/async_revalidation_manager.h" |
| 6 |
| 7 #include <queue> |
| 8 #include <set> |
| 9 #include <utility> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/memory/shared_memory_handle.h" |
| 15 #include "base/pickle.h" |
| 16 #include "base/run_loop.h" |
| 17 #include "content/browser/child_process_security_policy_impl.h" |
| 18 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 19 #include "content/browser/loader/resource_message_filter.h" |
| 20 #include "content/common/child_process_host_impl.h" |
| 21 #include "content/common/resource_messages.h" |
| 22 #include "content/public/browser/resource_context.h" |
| 23 #include "content/public/common/appcache_info.h" |
| 24 #include "content/public/common/process_type.h" |
| 25 #include "content/public/common/resource_type.h" |
| 26 #include "content/public/test/test_browser_context.h" |
| 27 #include "content/public/test/test_browser_thread_bundle.h" |
| 28 #include "ipc/ipc_param_traits.h" |
| 29 #include "net/base/load_flags.h" |
| 30 #include "net/base/network_delegate.h" |
| 31 #include "net/http/http_util.h" |
| 32 #include "net/url_request/url_request.h" |
| 33 #include "net/url_request/url_request_job.h" |
| 34 #include "net/url_request/url_request_job_factory.h" |
| 35 #include "net/url_request/url_request_test_job.h" |
| 36 #include "net/url_request/url_request_test_util.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" |
| 38 #include "ui/base/page_transition_types.h" |
| 39 #include "url/gurl.h" |
| 40 |
| 41 namespace content { |
| 42 |
| 43 namespace { |
| 44 |
| 45 // This class is a variation on URLRequestTestJob that |
| 46 // returns ERR_IO_PENDING before every read, not just the first one. |
| 47 class URLRequestTestDelayedCompletionJob : public net::URLRequestTestJob { |
| 48 public: |
| 49 URLRequestTestDelayedCompletionJob(net::URLRequest* request, |
| 50 net::NetworkDelegate* network_delegate, |
| 51 const std::string& response_headers, |
| 52 const std::string& response_data) |
| 53 : net::URLRequestTestJob(request, |
| 54 network_delegate, |
| 55 response_headers, |
| 56 response_data, |
| 57 false) {} |
| 58 |
| 59 private: |
| 60 ~URLRequestTestDelayedCompletionJob() override {} |
| 61 |
| 62 bool NextReadAsync() override { return true; } |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(URLRequestTestDelayedCompletionJob); |
| 65 }; |
| 66 |
| 67 class TestURLRequestJobFactory : public net::URLRequestJobFactory { |
| 68 public: |
| 69 TestURLRequestJobFactory() = default; |
| 70 |
| 71 void HandleScheme(const std::string& scheme) { |
| 72 supported_schemes_.insert(scheme); |
| 73 } |
| 74 |
| 75 // Sets the contents of the response. |headers| should have "\n" as line |
| 76 // breaks and end in "\n\n". |
| 77 void SetResponse(const std::string& headers, const std::string& data) { |
| 78 response_headers_ = headers; |
| 79 response_data_ = data; |
| 80 } |
| 81 |
| 82 using URLRequestJobCreateCallback = |
| 83 base::Callback<net::URLRequestJob*(net::URLRequest*, |
| 84 net::NetworkDelegate*)>; |
| 85 |
| 86 void SetCustomURLRequestJobCreateCallback( |
| 87 const URLRequestJobCreateCallback& callback) { |
| 88 custom_url_request_job_create_callback_ = callback; |
| 89 } |
| 90 |
| 91 net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
| 92 const std::string& scheme, |
| 93 net::URLRequest* request, |
| 94 net::NetworkDelegate* network_delegate) const override { |
| 95 if (!custom_url_request_job_create_callback_.is_null()) { |
| 96 return custom_url_request_job_create_callback_.Run(request, |
| 97 network_delegate); |
| 98 } |
| 99 |
| 100 return new URLRequestTestDelayedCompletionJob( |
| 101 request, network_delegate, response_headers_, response_data_); |
| 102 } |
| 103 |
| 104 net::URLRequestJob* MaybeInterceptRedirect( |
| 105 net::URLRequest* request, |
| 106 net::NetworkDelegate* network_delegate, |
| 107 const GURL& location) const override { |
| 108 return nullptr; |
| 109 } |
| 110 |
| 111 net::URLRequestJob* MaybeInterceptResponse( |
| 112 net::URLRequest* request, |
| 113 net::NetworkDelegate* network_delegate) const override { |
| 114 return nullptr; |
| 115 } |
| 116 |
| 117 bool IsHandledProtocol(const std::string& scheme) const override { |
| 118 return supported_schemes_.count(scheme) > 0; |
| 119 } |
| 120 |
| 121 bool IsHandledURL(const GURL& url) const override { |
| 122 return supported_schemes_.count(url.scheme()) > 0; |
| 123 } |
| 124 |
| 125 bool IsSafeRedirectTarget(const GURL& location) const override { |
| 126 return false; |
| 127 } |
| 128 |
| 129 private: |
| 130 std::string response_headers_; |
| 131 std::string response_data_; |
| 132 std::set<std::string> supported_schemes_; |
| 133 URLRequestJobCreateCallback custom_url_request_job_create_callback_; |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(TestURLRequestJobFactory); |
| 136 }; |
| 137 |
| 138 // On Windows, ResourceMsg_SetDataBuffer supplies a HANDLE which is not |
| 139 // automatically released. |
| 140 // |
| 141 // See ResourceDispatcher::ReleaseResourcesInDataMessage. |
| 142 // |
| 143 // TODO(ricea): Maybe share this implementation with |
| 144 // resource_dispatcher_host_unittest.cc |
| 145 void ReleaseHandlesInMessage(const IPC::Message& message) { |
| 146 if (message.type() == ResourceMsg_SetDataBuffer::ID) { |
| 147 base::PickleIterator iter(message); |
| 148 int request_id; |
| 149 CHECK(iter.ReadInt(&request_id)); |
| 150 base::SharedMemoryHandle shm_handle; |
| 151 if (IPC::ParamTraits<base::SharedMemoryHandle>::Read(&message, &iter, |
| 152 &shm_handle)) { |
| 153 if (base::SharedMemory::IsHandleValid(shm_handle)) |
| 154 base::SharedMemory::CloseHandle(shm_handle); |
| 155 } |
| 156 } |
| 157 } |
| 158 |
| 159 // This filter just deletes any messages that are sent through it. |
| 160 class BlackholeFilter : public ResourceMessageFilter { |
| 161 public: |
| 162 explicit BlackholeFilter(ResourceContext* resource_context) |
| 163 : ResourceMessageFilter( |
| 164 ChildProcessHostImpl::GenerateChildProcessUniqueId(), |
| 165 PROCESS_TYPE_RENDERER, |
| 166 nullptr, |
| 167 nullptr, |
| 168 nullptr, |
| 169 nullptr, |
| 170 nullptr, |
| 171 base::Bind(&BlackholeFilter::GetContexts, base::Unretained(this))), |
| 172 resource_context_(resource_context) { |
| 173 ChildProcessSecurityPolicyImpl::GetInstance()->Add(child_id()); |
| 174 } |
| 175 |
| 176 bool Send(IPC::Message* msg) override { |
| 177 ReleaseHandlesInMessage(*msg); |
| 178 delete msg; |
| 179 return true; |
| 180 } |
| 181 |
| 182 private: |
| 183 ~BlackholeFilter() override { |
| 184 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(child_id()); |
| 185 } |
| 186 |
| 187 void GetContexts(const ResourceHostMsg_Request& request, |
| 188 ResourceContext** resource_context, |
| 189 net::URLRequestContext** request_context) { |
| 190 *resource_context = resource_context_; |
| 191 *request_context = resource_context_->GetRequestContext(); |
| 192 } |
| 193 |
| 194 ResourceContext* resource_context_; |
| 195 |
| 196 DISALLOW_COPY_AND_ASSIGN(BlackholeFilter); |
| 197 }; |
| 198 |
| 199 ResourceHostMsg_Request CreateResourceRequest(const char* method, |
| 200 ResourceType type, |
| 201 const GURL& url) { |
| 202 ResourceHostMsg_Request request; |
| 203 request.method = std::string(method); |
| 204 request.url = url; |
| 205 request.first_party_for_cookies = url; // bypass third-party cookie blocking |
| 206 request.referrer_policy = blink::WebReferrerPolicyDefault; |
| 207 request.load_flags = 0; |
| 208 request.origin_pid = 0; |
| 209 request.resource_type = type; |
| 210 request.request_context = 0; |
| 211 request.appcache_host_id = kAppCacheNoHostId; |
| 212 request.download_to_file = false; |
| 213 request.should_reset_appcache = false; |
| 214 request.is_main_frame = true; |
| 215 request.parent_is_main_frame = false; |
| 216 request.parent_render_frame_id = -1; |
| 217 request.transition_type = ui::PAGE_TRANSITION_LINK; |
| 218 request.allow_download = true; |
| 219 return request; |
| 220 } |
| 221 |
| 222 } // namespace |
| 223 |
| 224 class AsyncRevalidationManagerTest : public ::testing::Test { |
| 225 protected: |
| 226 AsyncRevalidationManagerTest( |
| 227 scoped_ptr<net::TestNetworkDelegate> network_delegate) |
| 228 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 229 network_delegate_(std::move(network_delegate)) { |
| 230 browser_context_.reset(new TestBrowserContext()); |
| 231 BrowserContext::EnsureResourceContextInitialized(browser_context_.get()); |
| 232 base::RunLoop().RunUntilIdle(); |
| 233 ResourceContext* resource_context = browser_context_->GetResourceContext(); |
| 234 filter_ = new BlackholeFilter(resource_context); |
| 235 net::URLRequestContext* request_context = |
| 236 resource_context->GetRequestContext(); |
| 237 job_factory_.reset(new TestURLRequestJobFactory); |
| 238 request_context->set_job_factory(job_factory_.get()); |
| 239 request_context->set_network_delegate(network_delegate_.get()); |
| 240 host_.EnableStaleWhileRevalidateForTesting(); |
| 241 } |
| 242 |
| 243 AsyncRevalidationManagerTest() |
| 244 : AsyncRevalidationManagerTest( |
| 245 make_scoped_ptr(new net::TestNetworkDelegate)) {} |
| 246 |
| 247 void TearDown() override { |
| 248 host_.CancelRequestsForProcess(filter_->child_id()); |
| 249 host_.Shutdown(); |
| 250 host_.CancelRequestsForContext(browser_context_->GetResourceContext()); |
| 251 browser_context_.reset(); |
| 252 base::RunLoop().RunUntilIdle(); |
| 253 } |
| 254 |
| 255 void HandleScheme(const std::string& scheme) { |
| 256 job_factory_->HandleScheme(scheme); |
| 257 EnsureSchemeIsAllowed(scheme); |
| 258 } |
| 259 |
| 260 void SetResponse(const std::string& headers, const std::string& data) { |
| 261 job_factory_->SetResponse(headers, data); |
| 262 } |
| 263 |
| 264 void SetCustomURLRequestJobCreateCallback( |
| 265 const TestURLRequestJobFactory::URLRequestJobCreateCallback& callback) { |
| 266 job_factory_->SetCustomURLRequestJobCreateCallback(callback); |
| 267 } |
| 268 |
| 269 // Creates a request using the current test object as the filter and |
| 270 // SubResource as the resource type. |
| 271 void MakeTestRequest(int render_view_id, int request_id, const GURL& url) { |
| 272 ResourceHostMsg_Request request = |
| 273 CreateResourceRequest("GET", RESOURCE_TYPE_SUB_RESOURCE, url); |
| 274 ResourceHostMsg_RequestResource msg(render_view_id, request_id, request); |
| 275 host_.OnMessageReceived(msg, filter_.get()); |
| 276 base::RunLoop().RunUntilIdle(); |
| 277 } |
| 278 |
| 279 void EnsureSchemeIsAllowed(const std::string& scheme) { |
| 280 ChildProcessSecurityPolicyImpl* policy = |
| 281 ChildProcessSecurityPolicyImpl::GetInstance(); |
| 282 if (!policy->IsWebSafeScheme(scheme)) |
| 283 policy->RegisterWebSafeScheme(scheme); |
| 284 } |
| 285 |
| 286 net::TestNetworkDelegate* network_delegate() { |
| 287 return network_delegate_.get(); |
| 288 } |
| 289 |
| 290 content::TestBrowserThreadBundle thread_bundle_; |
| 291 scoped_ptr<TestBrowserContext> browser_context_; |
| 292 scoped_ptr<TestURLRequestJobFactory> job_factory_; |
| 293 scoped_refptr<BlackholeFilter> filter_; |
| 294 scoped_ptr<net::TestNetworkDelegate> network_delegate_; |
| 295 ResourceDispatcherHostImpl host_; |
| 296 }; |
| 297 |
| 298 TEST_F(AsyncRevalidationManagerTest, SupportsAsyncRevalidation) { |
| 299 // Scheme has to be HTTP or HTTPS to support async revalidation. |
| 300 HandleScheme("http"); |
| 301 SetResponse(net::URLRequestTestJob::test_headers(), "delay complete"); |
| 302 MakeTestRequest(0, 1, GURL("http://example.com/baz")); |
| 303 |
| 304 net::URLRequest* url_request( |
| 305 host_.GetURLRequest(GlobalRequestID(filter_->child_id(), 1))); |
| 306 ASSERT_TRUE(url_request); |
| 307 |
| 308 EXPECT_TRUE(url_request->load_flags() & net::LOAD_SUPPORT_ASYNC_REVALIDATION); |
| 309 } |
| 310 |
| 311 TEST_F(AsyncRevalidationManagerTest, AsyncRevalidationNotSupportedForPOST) { |
| 312 // Scheme has to be HTTP or HTTPS to support async revalidation. |
| 313 HandleScheme("http"); |
| 314 SetResponse(net::URLRequestTestJob::test_headers(), "delay complete"); |
| 315 // Create POST request. |
| 316 ResourceHostMsg_Request request = CreateResourceRequest( |
| 317 "POST", RESOURCE_TYPE_SUB_RESOURCE, GURL("http://example.com/baz.php")); |
| 318 ResourceHostMsg_RequestResource msg(0, 1, request); |
| 319 host_.OnMessageReceived(msg, filter_.get()); |
| 320 base::RunLoop().RunUntilIdle(); |
| 321 |
| 322 net::URLRequest* url_request( |
| 323 host_.GetURLRequest(GlobalRequestID(filter_->child_id(), 1))); |
| 324 ASSERT_TRUE(url_request); |
| 325 |
| 326 EXPECT_FALSE(url_request->load_flags() & |
| 327 net::LOAD_SUPPORT_ASYNC_REVALIDATION); |
| 328 } |
| 329 |
| 330 TEST_F(AsyncRevalidationManagerTest, |
| 331 AsyncRevalidationNotSupportedAfterRedirect) { |
| 332 static const char kRedirectHeaders[] = |
| 333 "HTTP/1.1 302 MOVED\n" |
| 334 "Location: http://example.com/var\n" |
| 335 "\n"; |
| 336 // Scheme has to be HTTP or HTTPS to support async revalidation. |
| 337 HandleScheme("http"); |
| 338 SetResponse(kRedirectHeaders, ""); |
| 339 |
| 340 MakeTestRequest(0, 1, GURL("http://example.com/baz")); |
| 341 |
| 342 net::URLRequest* url_request( |
| 343 host_.GetURLRequest(GlobalRequestID(filter_->child_id(), 1))); |
| 344 ASSERT_TRUE(url_request); |
| 345 |
| 346 EXPECT_FALSE(url_request->load_flags() & |
| 347 net::LOAD_SUPPORT_ASYNC_REVALIDATION); |
| 348 } |
| 349 |
| 350 // A URLRequestJob implementation which sets the |async_revalidation_required| |
| 351 // flag on the HttpResponseInfo object to true if the request has the |
| 352 // LOAD_SUPPORT_ASYNC_REVALIDATION flag. |
| 353 class AsyncRevalidationRequiredURLRequestTestJob |
| 354 : public net::URLRequestTestJob { |
| 355 public: |
| 356 // The Create() method is useful for wrapping the construction of the object |
| 357 // in a Callback. |
| 358 static net::URLRequestJob* Create(net::URLRequest* request, |
| 359 net::NetworkDelegate* network_delegate) { |
| 360 return new AsyncRevalidationRequiredURLRequestTestJob(request, |
| 361 network_delegate); |
| 362 } |
| 363 |
| 364 void GetResponseInfo(net::HttpResponseInfo* info) override { |
| 365 URLRequestTestJob::GetResponseInfo(info); |
| 366 if (request()->load_flags() & net::LOAD_SUPPORT_ASYNC_REVALIDATION) |
| 367 info->async_revalidation_required = true; |
| 368 } |
| 369 |
| 370 private: |
| 371 AsyncRevalidationRequiredURLRequestTestJob( |
| 372 net::URLRequest* request, |
| 373 net::NetworkDelegate* network_delegate) |
| 374 : URLRequestTestJob(request, |
| 375 network_delegate, |
| 376 net::URLRequestTestJob::test_headers(), |
| 377 std::string(), |
| 378 false) {} |
| 379 |
| 380 ~AsyncRevalidationRequiredURLRequestTestJob() override {} |
| 381 |
| 382 DISALLOW_COPY_AND_ASSIGN(AsyncRevalidationRequiredURLRequestTestJob); |
| 383 }; |
| 384 |
| 385 // A URLRequestJob implementation which serves a redirect and sets the |
| 386 // |async_revalidation_required| flag on the HttpResponseInfo object to true if |
| 387 // the request has the LOAD_SUPPORT_ASYNC_REVALIDATION flag. |
| 388 class RedirectAndRevalidateURLRequestTestJob : public net::URLRequestTestJob { |
| 389 public: |
| 390 // This Create() method returns a redirecting job if the URL contains the |
| 391 // string "redirect", otherwise a AsyncRevalidationRequiredURLRequestTestJob. |
| 392 static net::URLRequestJob* Create(net::URLRequest* request, |
| 393 net::NetworkDelegate* network_delegate) { |
| 394 if (request->url().spec().find("redirect") != std::string::npos) { |
| 395 return new RedirectAndRevalidateURLRequestTestJob(request, |
| 396 network_delegate); |
| 397 } |
| 398 return AsyncRevalidationRequiredURLRequestTestJob::Create(request, |
| 399 network_delegate); |
| 400 } |
| 401 |
| 402 void GetResponseInfo(net::HttpResponseInfo* info) override { |
| 403 URLRequestTestJob::GetResponseInfo(info); |
| 404 if (request()->load_flags() & net::LOAD_SUPPORT_ASYNC_REVALIDATION) |
| 405 info->async_revalidation_required = true; |
| 406 } |
| 407 |
| 408 private: |
| 409 RedirectAndRevalidateURLRequestTestJob(net::URLRequest* request, |
| 410 net::NetworkDelegate* network_delegate) |
| 411 : URLRequestTestJob(request, |
| 412 network_delegate, |
| 413 std::string(CreateRedirectHeaders()), |
| 414 std::string(), |
| 415 false) {} |
| 416 |
| 417 ~RedirectAndRevalidateURLRequestTestJob() override {} |
| 418 |
| 419 static std::string CreateRedirectHeaders() { |
| 420 static const char kRedirectHeaders[] = |
| 421 "HTTP/1.1 302 MOVED\n" |
| 422 "Location: http://example.com/var\n" |
| 423 "\n"; |
| 424 return std::string(kRedirectHeaders, arraysize(kRedirectHeaders)); |
| 425 } |
| 426 |
| 427 DISALLOW_COPY_AND_ASSIGN(RedirectAndRevalidateURLRequestTestJob); |
| 428 }; |
| 429 |
| 430 // A NetworkDelegate that records the URLRequests as they are created. |
| 431 class URLRequestRecordingNetworkDelegate : public net::TestNetworkDelegate { |
| 432 public: |
| 433 URLRequestRecordingNetworkDelegate() : requests_() {} |
| 434 |
| 435 net::URLRequest* NextRequest() { |
| 436 if (requests_.empty()) |
| 437 return nullptr; |
| 438 net::URLRequest* request = requests_.front(); |
| 439 requests_.pop(); |
| 440 return request; |
| 441 } |
| 442 |
| 443 bool IsEmpty() { return requests_.empty(); } |
| 444 |
| 445 int OnBeforeURLRequest(net::URLRequest* request, |
| 446 const net::CompletionCallback& callback, |
| 447 GURL* new_url) override { |
| 448 requests_.push(request); |
| 449 return TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 450 } |
| 451 |
| 452 private: |
| 453 std::queue<net::URLRequest*> requests_; |
| 454 |
| 455 DISALLOW_COPY_AND_ASSIGN(URLRequestRecordingNetworkDelegate); |
| 456 }; |
| 457 |
| 458 class AsyncRevalidationManagerRecordingTest |
| 459 : public AsyncRevalidationManagerTest { |
| 460 public: |
| 461 AsyncRevalidationManagerRecordingTest() |
| 462 : AsyncRevalidationManagerTest( |
| 463 make_scoped_ptr(new URLRequestRecordingNetworkDelegate)) { |
| 464 // Scheme has to be HTTP or HTTPS to support async revalidation. |
| 465 HandleScheme("http"); |
| 466 // Use the AsyncRevalidationRequiredURLRequestTestJob. |
| 467 SetCustomURLRequestJobCreateCallback( |
| 468 base::Bind(&AsyncRevalidationRequiredURLRequestTestJob::Create)); |
| 469 } |
| 470 |
| 471 URLRequestRecordingNetworkDelegate* recording_network_delegate() { |
| 472 return static_cast<URLRequestRecordingNetworkDelegate*>(network_delegate()); |
| 473 } |
| 474 |
| 475 net::URLRequest* NextRequest() { |
| 476 return recording_network_delegate()->NextRequest(); |
| 477 } |
| 478 |
| 479 bool IsEmpty() { return recording_network_delegate()->IsEmpty(); } |
| 480 }; |
| 481 |
| 482 // Verify that an async revalidation is actually created when needed. |
| 483 TEST_F(AsyncRevalidationManagerRecordingTest, Issued) { |
| 484 // Create the original request. |
| 485 MakeTestRequest(0, 1, GURL("http://example.com/baz")); |
| 486 |
| 487 net::URLRequest* initial_request = NextRequest(); |
| 488 ASSERT_TRUE(initial_request); |
| 489 EXPECT_TRUE(initial_request->load_flags() & |
| 490 net::LOAD_SUPPORT_ASYNC_REVALIDATION); |
| 491 |
| 492 net::URLRequest* async_request = NextRequest(); |
| 493 ASSERT_TRUE(async_request); |
| 494 } |
| 495 |
| 496 // Verify the the URL of the async revalidation matches the original request. |
| 497 TEST_F(AsyncRevalidationManagerRecordingTest, URLMatches) { |
| 498 // Create the original request. |
| 499 MakeTestRequest(0, 1, GURL("http://example.com/special-baz")); |
| 500 |
| 501 // Discard the original request. |
| 502 NextRequest(); |
| 503 |
| 504 net::URLRequest* async_request = NextRequest(); |
| 505 ASSERT_TRUE(async_request); |
| 506 EXPECT_EQ(GURL("http://example.com/special-baz"), async_request->url()); |
| 507 } |
| 508 |
| 509 TEST_F(AsyncRevalidationManagerRecordingTest, |
| 510 AsyncRevalidationsDoNotSupportAsyncRevalidation) { |
| 511 // Create the original request. |
| 512 MakeTestRequest(0, 1, GURL("http://example.com/baz")); |
| 513 |
| 514 // Discard the original request. |
| 515 NextRequest(); |
| 516 |
| 517 // Get the async revalidation request. |
| 518 net::URLRequest* async_request = NextRequest(); |
| 519 ASSERT_TRUE(async_request); |
| 520 EXPECT_FALSE(async_request->load_flags() & |
| 521 net::LOAD_SUPPORT_ASYNC_REVALIDATION); |
| 522 } |
| 523 |
| 524 TEST_F(AsyncRevalidationManagerRecordingTest, AsyncRevalidationsNotDuplicated) { |
| 525 // Create the original request. |
| 526 MakeTestRequest(0, 1, GURL("http://example.com/baz")); |
| 527 |
| 528 // Discard the original request. |
| 529 NextRequest(); |
| 530 |
| 531 // Get the async revalidation request. |
| 532 net::URLRequest* async_request = NextRequest(); |
| 533 EXPECT_TRUE(async_request); |
| 534 |
| 535 // Start a second request to the same URL. |
| 536 MakeTestRequest(0, 2, GURL("http://example.com/baz")); |
| 537 |
| 538 // Discard the second request. |
| 539 NextRequest(); |
| 540 |
| 541 // There should not be another async revalidation request. |
| 542 EXPECT_TRUE(IsEmpty()); |
| 543 } |
| 544 |
| 545 // Async revalidation to different URLs should not be treated as duplicates. |
| 546 TEST_F(AsyncRevalidationManagerRecordingTest, |
| 547 AsyncRevalidationsToSeparateURLsAreSeparate) { |
| 548 // Create two requests to two URLs. |
| 549 MakeTestRequest(0, 1, GURL("http://example.com/baz")); |
| 550 MakeTestRequest(0, 2, GURL("http://example.com/far")); |
| 551 |
| 552 net::URLRequest* initial_request = NextRequest(); |
| 553 ASSERT_TRUE(initial_request); |
| 554 net::URLRequest* initial_async_revalidation = NextRequest(); |
| 555 ASSERT_TRUE(initial_async_revalidation); |
| 556 net::URLRequest* second_request = NextRequest(); |
| 557 ASSERT_TRUE(second_request); |
| 558 net::URLRequest* second_async_revalidation = NextRequest(); |
| 559 ASSERT_TRUE(second_async_revalidation); |
| 560 |
| 561 EXPECT_EQ("http://example.com/baz", initial_request->url().spec()); |
| 562 EXPECT_EQ("http://example.com/baz", initial_async_revalidation->url().spec()); |
| 563 EXPECT_EQ("http://example.com/far", second_request->url().spec()); |
| 564 EXPECT_EQ("http://example.com/far", second_async_revalidation->url().spec()); |
| 565 } |
| 566 |
| 567 // A stale-while-revalidate applicable redirect response should not result in an |
| 568 // async revalidation. |
| 569 TEST_F(AsyncRevalidationManagerRecordingTest, InitialRedirectLegRevalidated) { |
| 570 // Use the appropriate URLRequestJob for the test. |
| 571 SetCustomURLRequestJobCreateCallback( |
| 572 base::Bind(&RedirectAndRevalidateURLRequestTestJob::Create)); |
| 573 MakeTestRequest(0, 1, GURL("http://example.com/redirect")); |
| 574 |
| 575 net::URLRequest* initial_request = NextRequest(); |
| 576 EXPECT_TRUE(initial_request); |
| 577 |
| 578 // There should be an async revalidation request. |
| 579 ASSERT_TRUE(NextRequest()); |
| 580 } |
| 581 |
| 582 // Nothing after the first redirect leg has stale-while-revalidate applied. |
| 583 // TODO(ricea): s-w-r should work with redirects. Change this test when it does. |
| 584 TEST_F(AsyncRevalidationManagerRecordingTest, NoSWRAfterFirstRedirectLeg) { |
| 585 SetCustomURLRequestJobCreateCallback( |
| 586 base::Bind(&RedirectAndRevalidateURLRequestTestJob::Create)); |
| 587 MakeTestRequest(0, 1, GURL("http://example.com/redirect")); |
| 588 |
| 589 net::URLRequest* initial_request = NextRequest(); |
| 590 EXPECT_TRUE(initial_request); |
| 591 |
| 592 EXPECT_FALSE(initial_request->load_flags() & |
| 593 net::LOAD_SUPPORT_ASYNC_REVALIDATION); |
| 594 |
| 595 // An async revalidation happens for the redirect. |
| 596 EXPECT_TRUE(NextRequest()); |
| 597 |
| 598 // But no others. |
| 599 EXPECT_TRUE(IsEmpty()); |
| 600 } |
| 601 |
| 602 } // namespace content |
OLD | NEW |