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