Index: content/browser/loader/async_revalidation_manager_browsertest.cc |
diff --git a/content/browser/loader/async_revalidation_manager_browsertest.cc b/content/browser/loader/async_revalidation_manager_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..964ce4b9c3af191373c82b21b079c14e270b6a2c |
--- /dev/null |
+++ b/content/browser/loader/async_revalidation_manager_browsertest.cc |
@@ -0,0 +1,220 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <string> |
+#include <utility> |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/command_line.h" |
+#include "base/macros.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/run_loop.h" |
+#include "base/strings/string16.h" |
+#include "base/strings/string_piece.h" |
+#include "base/strings/stringprintf.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "content/public/test/browser_test_utils.h" |
+#include "content/public/test/content_browser_test.h" |
+#include "content/public/test/content_browser_test_utils.h" |
+#include "content/shell/browser/shell.h" |
+#include "net/test/embedded_test_server/embedded_test_server.h" |
+#include "net/test/embedded_test_server/http_request.h" |
+#include "net/test/embedded_test_server/http_response.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "url/gurl.h" |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+using net::test_server::HttpResponse; |
+using net::test_server::HttpRequest; |
+using net::test_server::BasicHttpResponse; |
+ |
+const char kCountedHtmlPath[] = "/counted.html"; |
+const char kCookieHtmlPath[] = "/cookie.html"; |
+ |
+class AsyncRevalidationManagerBrowserTest : public ContentBrowserTest { |
+ protected: |
+ AsyncRevalidationManagerBrowserTest() {} |
+ ~AsyncRevalidationManagerBrowserTest() override {} |
+ |
+ void SetUp() override { |
+ ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); |
+ ContentBrowserTest::SetUp(); |
+ } |
+ |
+ void SetUpOnMainThread() override { |
+ embedded_test_server()->StartAcceptingConnections(); |
+ } |
+ |
+ void SetUpCommandLine(base::CommandLine* command_line) override { |
+ command_line->AppendSwitch("enable-stale-while-revalidate"); |
+ } |
+ |
+ base::RunLoop* run_loop() { return &run_loop_; } |
+ int requests_counted() const { return requests_counted_; } |
+ |
+ void CheckTitleTest(const GURL& url, const std::string& expected_title) { |
+ base::string16 expected_title16(base::ASCIIToUTF16(expected_title)); |
+ TitleWatcher title_watcher(shell()->web_contents(), expected_title16); |
+ NavigateToURL(shell(), url); |
+ EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle()); |
+ } |
+ |
+ void RegisterCountingRequestHandler() { |
+ embedded_test_server()->RegisterRequestHandler(base::Bind( |
+ &AsyncRevalidationManagerBrowserTest::CountingRequestHandler, this)); |
+ } |
+ |
+ void RegisterCookieRequestHandler() { |
+ embedded_test_server()->RegisterRequestHandler(base::Bind( |
+ &AsyncRevalidationManagerBrowserTest::CookieRequestHandler, this)); |
+ } |
+ |
+ private: |
+ // A request handler which increases the number in the title tag on every |
+ // request. |
+ scoped_ptr<HttpResponse> CountingRequestHandler(const HttpRequest& request) { |
+ if (request.relative_url != kCountedHtmlPath) |
+ return nullptr; |
+ |
+ int version = ++requests_counted_; |
+ |
+ scoped_ptr<BasicHttpResponse> http_response(StaleWhileRevalidateHeaders()); |
+ http_response->set_content( |
+ base::StringPrintf("<title>Version %d</title>", version)); |
+ |
+ // The StaleWhileRevalidateIsApplied test should exit once the resource has |
+ // been fetched from the origin server twice. |
+ if (version == 2) |
+ run_loop_.Quit(); |
+ return std::move(http_response); |
+ } |
+ |
+ // 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 != kCookieHtmlPath) |
+ 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 std::move(http_response); |
+ } |
+ |
+ // 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( |
+ "\"AsyncRevalidationManagerBrowserTest%d\"", requests_counted_); |
+ http_response->AddCustomHeader("ETag", etag); |
+ return http_response; |
+ } |
+ |
+ // This is only used by the StaleWhileRevalidateIsApplied test. |
+ base::RunLoop run_loop_; |
+ int requests_counted_ = 0; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(AsyncRevalidationManagerBrowserTest); |
+}; |
+ |
+// Verify that the "Cache-Control: stale-while-revalidate" directive correctly |
+// triggers an async revalidation. |
+IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest, |
+ StaleWhileRevalidateIsApplied) { |
+ RegisterCountingRequestHandler(); |
+ GURL url(embedded_test_server()->GetURL(kCountedHtmlPath)); |
+ |
+ CheckTitleTest(url, "Version 1"); |
+ |
+ // The first request happens synchronously. |
+ EXPECT_EQ(1, requests_counted()); |
+ |
+ // 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(AsyncRevalidationManagerBrowserTest, CacheIsUpdated) { |
+ using base::ASCIIToUTF16; |
+ RegisterCountingRequestHandler(); |
+ GURL url(embedded_test_server()->GetURL(kCountedHtmlPath)); |
+ |
+ 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; |
+ bool matched = false; |
+ |
+ for (int loads = 0; loads < kMaxLoads && !matched; ++loads) { |
kinuko
2016/01/05 13:43:53
Is the reload number (100) chosen arbitrary? It f
Adam Rice
2016/01/05 14:38:46
Yes. I've only tested on my workstation, but I've
|
+ 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); |
+} |
+ |
+// When the asynchronous revalidation arrives, any cookies it contains must be |
+// applied immediately. |
+IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest, |
+ CookieSetAsynchronously) { |
+ RegisterCookieRequestHandler(); |
+ GURL url(embedded_test_server()->GetURL(kCookieHtmlPath)); |
+ |
+ // 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 |
+ |
+} // namespace content |