Chromium Code Reviews| 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 void CheckTitleTest(const GURL& url, const std::string& expected_title) { | |
| 61 base::string16 expected_title16(base::ASCIIToUTF16(expected_title)); | |
| 62 TitleWatcher title_watcher(shell()->web_contents(), expected_title16); | |
| 63 NavigateToURL(shell(), url); | |
| 64 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle()); | |
| 65 } | |
| 66 | |
| 67 void RegisterCountingRequestHandler() { | |
| 68 embedded_test_server()->RegisterRequestHandler(base::Bind( | |
| 69 &AsyncRevalidationManagerBrowserTest::CountingRequestHandler, this)); | |
| 70 } | |
| 71 | |
| 72 void RegisterCookieRequestHandler() { | |
| 73 embedded_test_server()->RegisterRequestHandler(base::Bind( | |
| 74 &AsyncRevalidationManagerBrowserTest::CookieRequestHandler, this)); | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 // A request handler which increases the number in the title tag on every | |
| 79 // request. | |
| 80 scoped_ptr<HttpResponse> CountingRequestHandler(const HttpRequest& request) { | |
| 81 if (request.relative_url != kCountedHtmlPath) | |
| 82 return nullptr; | |
| 83 | |
| 84 int version = ++requests_counted_; | |
| 85 | |
| 86 scoped_ptr<BasicHttpResponse> http_response(StaleWhileRevalidateHeaders()); | |
| 87 http_response->set_content( | |
| 88 base::StringPrintf("<title>Version %d</title>", version)); | |
| 89 | |
| 90 // The StaleWhileRevalidateIsApplied test should exit once the resource has | |
| 91 // been fetched from the origin server twice. | |
| 92 if (version == 2) | |
| 93 run_loop_.Quit(); | |
| 94 return std::move(http_response); | |
| 95 } | |
| 96 | |
| 97 // A request handler which increases a cookie value on every request. | |
| 98 scoped_ptr<HttpResponse> CookieRequestHandler(const HttpRequest& request) { | |
| 99 static const char kHtml[] = | |
| 100 "<script>\n" | |
| 101 "var intervalId;\n" | |
| 102 "function checkCookie() {\n" | |
| 103 " if (document.cookie.search(/version=2/) != -1) {\n" | |
| 104 " clearInterval(intervalId);\n" | |
| 105 " document.title = \"PASS\";\n" | |
| 106 " }\n" | |
| 107 "}\n" | |
| 108 "intervalId = setInterval(checkCookie, 10);\n" | |
| 109 "</script>\n" | |
| 110 "<title>Loaded</title>\n"; | |
| 111 | |
| 112 if (request.relative_url != kCookieHtmlPath) | |
| 113 return nullptr; | |
| 114 | |
| 115 int version = ++requests_counted_; | |
| 116 | |
| 117 scoped_ptr<BasicHttpResponse> http_response(StaleWhileRevalidateHeaders()); | |
| 118 http_response->AddCustomHeader("Set-Cookie", | |
| 119 base::StringPrintf("version=%d", version)); | |
| 120 http_response->set_content(kHtml); | |
| 121 return std::move(http_response); | |
| 122 } | |
| 123 | |
| 124 // Generate the standard response headers common to all request handlers. | |
| 125 scoped_ptr<BasicHttpResponse> StaleWhileRevalidateHeaders() { | |
| 126 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | |
| 127 http_response->set_code(net::HTTP_OK); | |
| 128 http_response->set_content_type("text/html; charset=utf-8"); | |
| 129 http_response->AddCustomHeader("Cache-Control", | |
| 130 "max-age=0, stale-while-revalidate=86400"); | |
| 131 // A validator is needed for revalidations, and hence | |
| 132 // stale-while-revalidate, to work. | |
| 133 std::string etag = base::StringPrintf( | |
| 134 "\"AsyncRevalidationManagerBrowserTest%d\"", requests_counted_); | |
| 135 http_response->AddCustomHeader("ETag", etag); | |
| 136 return http_response; | |
| 137 } | |
| 138 | |
| 139 // This is only used by the StaleWhileRevalidateIsApplied test. | |
| 140 base::RunLoop run_loop_; | |
| 141 int requests_counted_ = 0; | |
| 142 | |
| 143 DISALLOW_COPY_AND_ASSIGN(AsyncRevalidationManagerBrowserTest); | |
| 144 }; | |
| 145 | |
| 146 // Verify that the "Cache-Control: stale-while-revalidate" directive correctly | |
| 147 // triggers an async revalidation. | |
| 148 IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest, | |
| 149 StaleWhileRevalidateIsApplied) { | |
| 150 RegisterCountingRequestHandler(); | |
| 151 GURL url(embedded_test_server()->GetURL(kCountedHtmlPath)); | |
| 152 | |
| 153 CheckTitleTest(url, "Version 1"); | |
| 154 | |
| 155 // The first request happens synchronously. | |
| 156 EXPECT_EQ(1, requests_counted()); | |
| 157 | |
| 158 // Force the renderer to be destroyed so that the Blink cache doesn't | |
| 159 // interfere with the result. | |
| 160 NavigateToURL(shell(), GURL("about:blank")); | |
| 161 | |
| 162 // Load the page again. We should get the stale version from the cache. | |
| 163 CheckTitleTest(url, "Version 1"); | |
| 164 | |
| 165 // Wait for the async revalidation to complete. | |
| 166 run_loop()->Run(); | |
| 167 EXPECT_EQ(2, requests_counted()); | |
| 168 } | |
| 169 | |
| 170 // The fresh cache entry must become visible eventually. | |
| 171 IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest, CacheIsUpdated) { | |
| 172 using base::ASCIIToUTF16; | |
| 173 RegisterCountingRequestHandler(); | |
| 174 GURL url(embedded_test_server()->GetURL(kCountedHtmlPath)); | |
| 175 | |
| 176 CheckTitleTest(url, "Version 1"); | |
| 177 | |
| 178 // Reset the renderer cache. | |
| 179 NavigateToURL(shell(), GURL("about:blank")); | |
| 180 | |
| 181 // Load the page again. We should get the stale version from the cache. | |
| 182 CheckTitleTest(url, "Version 1"); | |
| 183 | |
| 184 // Load the page repeatedly until we see the new version. | |
| 185 const int kMaxLoads = 100; | |
| 186 bool matched = false; | |
| 187 | |
| 188 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
| |
| 189 TitleWatcher title_watcher(shell()->web_contents(), | |
| 190 ASCIIToUTF16("Version 2")); | |
| 191 title_watcher.AlsoWaitForTitle(ASCIIToUTF16("Version 1")); | |
| 192 NavigateToURL(shell(), GURL("about:blank")); | |
| 193 NavigateToURL(shell(), url); | |
| 194 matched = (title_watcher.WaitAndGetTitle() == ASCIIToUTF16("Version 2")); | |
| 195 } | |
| 196 | |
| 197 EXPECT_TRUE(matched); | |
| 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 CheckTitleTest(url, "PASS"); | |
| 216 } | |
| 217 | |
| 218 } // namespace | |
| 219 | |
| 220 } // namespace content | |
| OLD | NEW |