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