Chromium Code Reviews| Index: content/browser/loader/resource_dispatcher_host_browsertest.cc |
| diff --git a/content/browser/loader/resource_dispatcher_host_browsertest.cc b/content/browser/loader/resource_dispatcher_host_browsertest.cc |
| index b612d302df5e85ae96088109474302b5df7708d8..37a12c12d88241ebc154f027e8effe6744454840 100644 |
| --- a/content/browser/loader/resource_dispatcher_host_browsertest.cc |
| +++ b/content/browser/loader/resource_dispatcher_host_browsertest.cc |
| @@ -2,7 +2,9 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include "base/command_line.h" |
| #include "base/memory/ref_counted.h" |
| +#include "base/run_loop.h" |
| #include "base/strings/string_util.h" |
| #include "base/strings/stringprintf.h" |
| #include "base/strings/utf_string_conversions.h" |
| @@ -264,16 +266,18 @@ IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, |
| namespace { |
| +using net::test_server::HttpResponse; |
| +using net::test_server::HttpRequest; |
| +using net::test_server::BasicHttpResponse; |
| + |
| // Handles |request| by serving a redirect response. |
| -scoped_ptr<net::test_server::HttpResponse> NoContentResponseHandler( |
| - const std::string& path, |
| - const net::test_server::HttpRequest& request) { |
| +scoped_ptr<HttpResponse> NoContentResponseHandler(const std::string& path, |
| + const HttpRequest& request) { |
| if (!base::StartsWith(path, request.relative_url, |
| base::CompareCase::SENSITIVE)) |
| - return scoped_ptr<net::test_server::HttpResponse>(); |
| + return scoped_ptr<HttpResponse>(); |
| - scoped_ptr<net::test_server::BasicHttpResponse> http_response( |
| - new net::test_server::BasicHttpResponse); |
| + scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| http_response->set_code(net::HTTP_NO_CONTENT); |
| return http_response.Pass(); |
| } |
| @@ -447,15 +451,13 @@ IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, |
| namespace { |
| -scoped_ptr<net::test_server::HttpResponse> HandleRedirectRequest( |
| - const std::string& request_path, |
| - const net::test_server::HttpRequest& request) { |
| +scoped_ptr<HttpResponse> HandleRedirectRequest(const std::string& request_path, |
| + const HttpRequest& request) { |
| if (!base::StartsWith(request.relative_url, request_path, |
| base::CompareCase::SENSITIVE)) |
| - return scoped_ptr<net::test_server::HttpResponse>(); |
| + return scoped_ptr<HttpResponse>(); |
| - scoped_ptr<net::test_server::BasicHttpResponse> http_response( |
| - new net::test_server::BasicHttpResponse); |
| + scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| http_response->set_code(net::HTTP_FOUND); |
| http_response->AddCustomHeader( |
| "Location", request.relative_url.substr(request_path.length())); |
| @@ -526,4 +528,175 @@ IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, |
| delegate.page_transition() & ui::PAGE_TRANSITION_CLIENT_REDIRECT); |
| } |
| +class ResourceDispatcherHostBrowserStaleWhileRevalidateTest |
| + : public ResourceDispatcherHostBrowserTest { |
| + protected: |
| + ResourceDispatcherHostBrowserStaleWhileRevalidateTest() {} |
| + ~ResourceDispatcherHostBrowserStaleWhileRevalidateTest() override {} |
| + |
| + base::RunLoop* run_loop() { return &run_loop_; } |
| + int requests_counted() { return requests_counted_; } |
| + |
| + void RegisterCountingRequestHandler() { |
| + embedded_test_server()->RegisterRequestHandler( |
| + base::Bind(&ResourceDispatcherHostBrowserStaleWhileRevalidateTest:: |
| + CountingRequestHandler, |
| + this)); |
| + } |
| + |
| + void RegisterCookieRequestHandler() { |
| + embedded_test_server()->RegisterRequestHandler( |
| + base::Bind(&ResourceDispatcherHostBrowserStaleWhileRevalidateTest:: |
| + CookieRequestHandler, |
| + this)); |
| + } |
| + |
| + void SetUpCommandLine(base::CommandLine* command_line) override { |
| + // TODO(ricea): Remove this once it becomes the default. |
| + command_line->AppendSwitch("enable-stale-while-revalidate"); |
| + } |
| + |
| + private: |
| + // A request handler which increases the number in the title tag on every |
| + // request. |
| + scoped_ptr<HttpResponse> CountingRequestHandler(const HttpRequest& request) { |
|
tyoshino (SeeGerritForStatus)
2015/08/31 09:37:46
this is special to StaleWhileRevalidateIsApplied f
Adam Rice
2015/09/01 00:38:09
I have just documented it in comments for now. If
|
| + if (request.relative_url != "/counted.html") |
| + return nullptr; |
| + |
| + int version = ++requests_counted_; |
| + |
| + scoped_ptr<BasicHttpResponse> http_response(StaleWhileRevalidateHeaders()); |
| + http_response->set_content( |
| + base::StringPrintf("<title>Version %d</title>", version)); |
| + |
| + if (version == 2) |
| + run_loop_.Quit(); |
| + return http_response.Pass(); |
| + } |
| + |
| + // A request handler which increases a cookie value on every request. |
| + scoped_ptr<HttpResponse> CookieRequestHandler(const HttpRequest& request) { |
| + static const char kHtml[] = |
| + "<script>\n" |
| + "var intervalId;\n" |
| + "function checkCookie() {\n" |
| + " if (document.cookie.search(/version=2/) != -1) {\n" |
| + " clearInterval(intervalId);\n" |
| + " document.title = \"PASS\";\n" |
| + " }\n" |
| + "}\n" |
| + "intervalId = setInterval(checkCookie, 10);\n" |
| + "</script>\n" |
| + "<title>Loaded</title>\n"; |
| + |
| + if (request.relative_url != "/cookie.html") |
| + return nullptr; |
| + |
| + int version = ++requests_counted_; |
| + |
| + scoped_ptr<BasicHttpResponse> http_response(StaleWhileRevalidateHeaders()); |
| + http_response->AddCustomHeader("Set-Cookie", |
| + base::StringPrintf("version=%d", version)); |
| + http_response->set_content(kHtml); |
| + return http_response.Pass(); |
| + } |
| + |
| + // Generate the standard response headers common to all request handlers. |
| + scoped_ptr<BasicHttpResponse> StaleWhileRevalidateHeaders() { |
| + scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| + http_response->set_code(net::HTTP_OK); |
| + http_response->set_content_type("text/html; charset=utf-8"); |
| + http_response->AddCustomHeader("Cache-Control", |
| + "max-age=0, stale-while-revalidate=86400"); |
| + // A validator is needed for revalidations, and hence |
| + // stale-while-revalidate, to work. |
| + std::string etag = base::StringPrintf( |
| + "\"ResourceDispatcherHostBrowserStaleWhileRevalidateTest%d\"", |
| + requests_counted_); |
| + http_response->AddCustomHeader("ETag", etag); |
| + return http_response.Pass(); |
| + } |
| + |
| + base::RunLoop run_loop_; |
| + int requests_counted_ = 0; |
| + |
| + DISALLOW_COPY_AND_ASSIGN( |
| + ResourceDispatcherHostBrowserStaleWhileRevalidateTest); |
| +}; |
| + |
| +// Verify that the "Cache-Control: stale-while-revalidate" directive correctly |
| +// triggers an async revalidation. |
| +IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserStaleWhileRevalidateTest, |
| + StaleWhileRevalidateIsApplied) { |
| + ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| + RegisterCountingRequestHandler(); |
| + GURL url(embedded_test_server()->GetURL("/counted.html")); |
| + |
| + CheckTitleTest(url, "Version 1"); |
| + |
| + // Force the renderer to be destroyed so that the Blink cache doesn't |
| + // interfere with the result. |
| + NavigateToURL(shell(), GURL("about:blank")); |
| + |
| + // Load the page again. We should get the stale version from the cache. |
| + CheckTitleTest(url, "Version 1"); |
| + |
| + // Wait for the async revalidation to complete. |
| + run_loop()->Run(); |
| + EXPECT_EQ(2, requests_counted()); |
| +} |
| + |
| +// The fresh cache entry must become visible eventually. |
| +IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserStaleWhileRevalidateTest, |
| + CacheIsUpdated) { |
| + ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| + RegisterCountingRequestHandler(); |
| + GURL url(embedded_test_server()->GetURL("/counted.html")); |
| + |
| + CheckTitleTest(url, "Version 1"); |
| + |
| + // Reset the renderer cache. |
| + NavigateToURL(shell(), GURL("about:blank")); |
| + |
| + // Load the page again. We should get the stale version from the cache. |
| + CheckTitleTest(url, "Version 1"); |
| + |
| + // Load the page repeatedly until we see the new version. |
| + const int kMaxLoads = 100; |
| + int actual_loads = 0; |
| + bool matched = false; |
| + |
| + for (; actual_loads < kMaxLoads && !matched; ++actual_loads) { |
| + TitleWatcher title_watcher(shell()->web_contents(), |
| + ASCIIToUTF16("Version 2")); |
| + title_watcher.AlsoWaitForTitle(ASCIIToUTF16("Version 1")); |
| + NavigateToURL(shell(), GURL("about:blank")); |
| + NavigateToURL(shell(), url); |
| + matched = (title_watcher.WaitAndGetTitle() == ASCIIToUTF16("Version 2")); |
| + } |
| + |
| + EXPECT_TRUE(matched); |
| + // actual_loads is usually 1 but larger numbers are not an error. |
| + DVLOG(1) << "Number of loads: " << actual_loads; |
| +} |
| + |
| +// When the asynchronous revalidation arrives, any cookies it contains must be |
| +// applied immediately. |
| +IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserStaleWhileRevalidateTest, |
| + CookieSetAsynchronously) { |
| + ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| + RegisterCookieRequestHandler(); |
| + GURL url(embedded_test_server()->GetURL("/cookie.html")); |
| + |
| + // Set cookie to version=1 |
| + NavigateToURL(shell(), url); |
| + |
| + // Reset render cache. |
| + NavigateToURL(shell(), GURL("about:blank")); |
| + |
| + // The page will load from the cache, then when the async revalidation |
| + // completes the cookie will update. |
| + CheckTitleTest(url, "PASS"); |
| +} |
| + |
| } // namespace content |