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 <string> |
| 6 #include <utility> |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" |
| 10 #include "base/command_line.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "base/strings/string16.h" |
| 15 #include "base/strings/string_piece.h" |
| 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "content/public/test/browser_test_utils.h" |
| 19 #include "content/public/test/content_browser_test.h" |
| 20 #include "content/public/test/content_browser_test_utils.h" |
| 21 #include "content/shell/browser/shell.h" |
| 22 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 23 #include "net/test/embedded_test_server/http_request.h" |
| 24 #include "net/test/embedded_test_server/http_response.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "url/gurl.h" |
| 27 |
| 28 namespace content { |
| 29 |
| 30 namespace { |
| 31 |
| 32 using net::test_server::HttpResponse; |
| 33 using net::test_server::HttpRequest; |
| 34 using net::test_server::BasicHttpResponse; |
| 35 |
| 36 const char kCountedHtmlPath[] = "/counted.html"; |
| 37 const char kCookieHtmlPath[] = "/cookie.html"; |
| 38 |
| 39 class AsyncRevalidationManagerBrowserTest : public ContentBrowserTest { |
| 40 protected: |
| 41 AsyncRevalidationManagerBrowserTest() {} |
| 42 ~AsyncRevalidationManagerBrowserTest() override {} |
| 43 |
| 44 void SetUp() override { |
| 45 ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); |
| 46 ContentBrowserTest::SetUp(); |
| 47 } |
| 48 |
| 49 void SetUpOnMainThread() override { |
| 50 embedded_test_server()->StartAcceptingConnections(); |
| 51 } |
| 52 |
| 53 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 54 command_line->AppendSwitch("enable-stale-while-revalidate"); |
| 55 } |
| 56 |
| 57 base::RunLoop* run_loop() { return &run_loop_; } |
| 58 int requests_counted() const { return requests_counted_; } |
| 59 |
| 60 // This method lacks diagnostics for the failure case because TitleWatcher |
| 61 // will just wait until the test times out if |expected_title| does not |
| 62 // appear. |
| 63 bool TitleBecomes(const GURL& url, const std::string& expected_title) { |
| 64 base::string16 expected_title16(base::ASCIIToUTF16(expected_title)); |
| 65 TitleWatcher title_watcher(shell()->web_contents(), expected_title16); |
| 66 NavigateToURL(shell(), url); |
| 67 return title_watcher.WaitAndGetTitle() == expected_title16; |
| 68 } |
| 69 |
| 70 void RegisterCountingRequestHandler() { |
| 71 embedded_test_server()->RegisterRequestHandler(base::Bind( |
| 72 &AsyncRevalidationManagerBrowserTest::CountingRequestHandler, this)); |
| 73 } |
| 74 |
| 75 void RegisterCookieRequestHandler() { |
| 76 embedded_test_server()->RegisterRequestHandler(base::Bind( |
| 77 &AsyncRevalidationManagerBrowserTest::CookieRequestHandler, this)); |
| 78 } |
| 79 |
| 80 private: |
| 81 // A request handler which increases the number in the title tag on every |
| 82 // request. |
| 83 scoped_ptr<HttpResponse> CountingRequestHandler(const HttpRequest& request) { |
| 84 if (request.relative_url != kCountedHtmlPath) |
| 85 return nullptr; |
| 86 |
| 87 int version = ++requests_counted_; |
| 88 |
| 89 scoped_ptr<BasicHttpResponse> http_response(StaleWhileRevalidateHeaders()); |
| 90 http_response->set_content( |
| 91 base::StringPrintf("<title>Version %d</title>", version)); |
| 92 |
| 93 // The second time this handler is run is the async revalidation. Tests can |
| 94 // use this for synchronisation. |
| 95 if (version == 2) |
| 96 run_loop_.Quit(); |
| 97 return std::move(http_response); |
| 98 } |
| 99 |
| 100 // A request handler which increases a cookie value on every request. |
| 101 scoped_ptr<HttpResponse> CookieRequestHandler(const HttpRequest& request) { |
| 102 static const char kHtml[] = |
| 103 "<script>\n" |
| 104 "var intervalId;\n" |
| 105 "function checkCookie() {\n" |
| 106 " if (document.cookie.search(/version=2/) != -1) {\n" |
| 107 " clearInterval(intervalId);\n" |
| 108 " document.title = \"PASS\";\n" |
| 109 " }\n" |
| 110 "}\n" |
| 111 "intervalId = setInterval(checkCookie, 10);\n" |
| 112 "</script>\n" |
| 113 "<title>Loaded</title>\n"; |
| 114 |
| 115 if (request.relative_url != kCookieHtmlPath) |
| 116 return nullptr; |
| 117 |
| 118 int version = ++requests_counted_; |
| 119 |
| 120 scoped_ptr<BasicHttpResponse> http_response(StaleWhileRevalidateHeaders()); |
| 121 http_response->AddCustomHeader("Set-Cookie", |
| 122 base::StringPrintf("version=%d", version)); |
| 123 http_response->set_content(kHtml); |
| 124 |
| 125 return std::move(http_response); |
| 126 } |
| 127 |
| 128 // Generate the standard response headers common to all request handlers. |
| 129 scoped_ptr<BasicHttpResponse> StaleWhileRevalidateHeaders() { |
| 130 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 131 http_response->set_code(net::HTTP_OK); |
| 132 http_response->set_content_type("text/html; charset=utf-8"); |
| 133 http_response->AddCustomHeader("Cache-Control", |
| 134 "max-age=0, stale-while-revalidate=86400"); |
| 135 // A validator is needed for revalidations, and hence |
| 136 // stale-while-revalidate, to work. |
| 137 std::string etag = base::StringPrintf( |
| 138 "\"AsyncRevalidationManagerBrowserTest%d\"", requests_counted_); |
| 139 http_response->AddCustomHeader("ETag", etag); |
| 140 return http_response; |
| 141 } |
| 142 |
| 143 base::RunLoop run_loop_; |
| 144 int requests_counted_ = 0; |
| 145 |
| 146 DISALLOW_COPY_AND_ASSIGN(AsyncRevalidationManagerBrowserTest); |
| 147 }; |
| 148 |
| 149 // Verify that the "Cache-Control: stale-while-revalidate" directive correctly |
| 150 // triggers an async revalidation. |
| 151 IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest, |
| 152 StaleWhileRevalidateIsApplied) { |
| 153 RegisterCountingRequestHandler(); |
| 154 GURL url(embedded_test_server()->GetURL(kCountedHtmlPath)); |
| 155 |
| 156 EXPECT_TRUE(TitleBecomes(url, "Version 1")); |
| 157 |
| 158 // The first request happens synchronously. |
| 159 EXPECT_EQ(1, requests_counted()); |
| 160 |
| 161 // Force the renderer to be destroyed so that the Blink cache doesn't |
| 162 // interfere with the result. |
| 163 NavigateToURL(shell(), GURL("about:blank")); |
| 164 |
| 165 // Load the page again. We should get the stale version from the cache. |
| 166 EXPECT_TRUE(TitleBecomes(url, "Version 1")); |
| 167 |
| 168 // Wait for the async revalidation to complete. |
| 169 run_loop()->Run(); |
| 170 EXPECT_EQ(2, requests_counted()); |
| 171 } |
| 172 |
| 173 // The fresh cache entry must become visible once the async revalidation request |
| 174 // has been sent. |
| 175 IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest, CacheIsUpdated) { |
| 176 using base::ASCIIToUTF16; |
| 177 RegisterCountingRequestHandler(); |
| 178 GURL url(embedded_test_server()->GetURL(kCountedHtmlPath)); |
| 179 |
| 180 EXPECT_TRUE(TitleBecomes(url, "Version 1")); |
| 181 |
| 182 // Reset the renderer cache. |
| 183 NavigateToURL(shell(), GURL("about:blank")); |
| 184 |
| 185 // Load the page again. We should get the stale version from the cache. |
| 186 EXPECT_TRUE(TitleBecomes(url, "Version 1")); |
| 187 |
| 188 // Wait for the async revalidation request to be processed by the |
| 189 // EmbeddedTestServer. |
| 190 run_loop()->Run(); |
| 191 |
| 192 // Reset the renderer cache. |
| 193 NavigateToURL(shell(), GURL("about:blank")); |
| 194 |
| 195 // Since the async revalidation request has been sent, the cache can no |
| 196 // longer return the stale contents. |
| 197 EXPECT_TRUE(TitleBecomes(url, "Version 2")); |
| 198 } |
| 199 |
| 200 // When the asynchronous revalidation arrives, any cookies it contains must be |
| 201 // applied immediately. |
| 202 IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest, |
| 203 CookieSetAsynchronously) { |
| 204 RegisterCookieRequestHandler(); |
| 205 GURL url(embedded_test_server()->GetURL(kCookieHtmlPath)); |
| 206 |
| 207 // Set cookie to version=1 |
| 208 NavigateToURL(shell(), url); |
| 209 |
| 210 // Reset render cache. |
| 211 NavigateToURL(shell(), GURL("about:blank")); |
| 212 |
| 213 // The page will load from the cache, then when the async revalidation |
| 214 // completes the cookie will update. |
| 215 EXPECT_TRUE(TitleBecomes(url, "PASS")); |
| 216 } |
| 217 |
| 218 } // namespace |
| 219 |
| 220 } // namespace content |
OLD | NEW |