Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Side by Side Diff: content/browser/loader/async_revalidation_manager_browsertest.cc

Issue 1308203003: content_browsertests for stale-while-revalidate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s-w-r-browser-content
Patch Set: Rebase. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | content/content_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.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 "content/public/test/browser_test_utils.h"
18 #include "content/public/test/content_browser_test.h"
19 #include "content/public/test/content_browser_test_utils.h"
20 #include "content/shell/browser/shell.h"
21 #include "net/test/embedded_test_server/embedded_test_server.h"
22 #include "net/test/embedded_test_server/http_request.h"
23 #include "net/test/embedded_test_server/http_response.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "url/gurl.h"
26
27 namespace content {
28
29 namespace {
30
31 using net::test_server::HttpResponse;
32 using net::test_server::HttpRequest;
33 using net::test_server::BasicHttpResponse;
34
35 class AsyncRevalidationManagerBrowserTest : public ContentBrowserTest {
36 protected:
37 AsyncRevalidationManagerBrowserTest() {}
38 ~AsyncRevalidationManagerBrowserTest() override {}
39
40 void SetUp() override {
41 ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
42 ContentBrowserTest::SetUp();
43 }
44
45 void SetUpOnMainThread() override {
46 embedded_test_server()->StartAcceptingConnections();
47 }
48
49 base::RunLoop* run_loop() { return &run_loop_; }
50 int requests_counted() { return requests_counted_; }
kinuko 2015/12/15 15:28:54 nit: can be a const method
Adam Rice 2016/01/05 09:21:44 Done.
51
52 void CheckTitleTest(const GURL& url, const std::string& expected_title) {
53 base::string16 expected_title16(base::ASCIIToUTF16(expected_title));
54 TitleWatcher title_watcher(shell()->web_contents(), expected_title16);
55 NavigateToURL(shell(), url);
56 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle());
57 }
58
59 void RegisterCountingRequestHandler() {
60 embedded_test_server()->RegisterRequestHandler(base::Bind(
61 &AsyncRevalidationManagerBrowserTest::CountingRequestHandler, this));
62 }
63
64 void RegisterCookieRequestHandler() {
65 embedded_test_server()->RegisterRequestHandler(base::Bind(
66 &AsyncRevalidationManagerBrowserTest::CookieRequestHandler, this));
67 }
68
69 void SetUpCommandLine(base::CommandLine* command_line) override {
kinuko 2015/12/15 15:28:54 nit: please place override methods from the same c
Adam Rice 2016/01/05 09:21:44 Done.
70 // TODO(ricea): Remove this once it becomes the default.
71 command_line->AppendSwitch("enable-stale-while-revalidate");
72 }
73
74 private:
75 // A request handler which increases the number in the title tag on every
76 // request.
77 scoped_ptr<HttpResponse> CountingRequestHandler(const HttpRequest& request) {
78 if (request.relative_url != "/counted.html")
79 return nullptr;
80
81 int version = ++requests_counted_;
82
83 scoped_ptr<BasicHttpResponse> http_response(StaleWhileRevalidateHeaders());
84 http_response->set_content(
85 base::StringPrintf("<title>Version %d</title>", version));
86
87 // This condition is appropriate for the StaleWhileRevalidateIsApplied
88 // test.
89 // TODO(ricea): Support a general condition if other tests need it.
kinuko 2015/12/15 15:28:54 nit: I don't think these TODO comments add some va
Adam Rice 2016/01/05 09:21:43 Done.
90 if (version == 2)
91 run_loop_.Quit();
92 return http_response.Pass();
kinuko 2015/12/15 15:28:54 nit: move?
Adam Rice 2016/01/05 09:21:43 Done.
93 }
94
95 // A request handler which increases a cookie value on every request.
96 scoped_ptr<HttpResponse> CookieRequestHandler(const HttpRequest& request) {
97 static const char kHtml[] =
98 "<script>\n"
99 "var intervalId;\n"
100 "function checkCookie() {\n"
101 " if (document.cookie.search(/version=2/) != -1) {\n"
102 " clearInterval(intervalId);\n"
103 " document.title = \"PASS\";\n"
104 " }\n"
105 "}\n"
106 "intervalId = setInterval(checkCookie, 10);\n"
107 "</script>\n"
108 "<title>Loaded</title>\n";
109
110 if (request.relative_url != "/cookie.html")
111 return nullptr;
112
113 int version = ++requests_counted_;
114
115 scoped_ptr<BasicHttpResponse> http_response(StaleWhileRevalidateHeaders());
116 http_response->AddCustomHeader("Set-Cookie",
117 base::StringPrintf("version=%d", version));
118 http_response->set_content(kHtml);
119 return http_response.Pass();
120 }
121
122 // Generate the standard response headers common to all request handlers.
123 scoped_ptr<BasicHttpResponse> StaleWhileRevalidateHeaders() {
124 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse);
125 http_response->set_code(net::HTTP_OK);
126 http_response->set_content_type("text/html; charset=utf-8");
127 http_response->AddCustomHeader("Cache-Control",
128 "max-age=0, stale-while-revalidate=86400");
129 // A validator is needed for revalidations, and hence
130 // stale-while-revalidate, to work.
131 std::string etag = base::StringPrintf(
132 "\"AsyncRevalidationManagerBrowserTest%d\"", requests_counted_);
133 http_response->AddCustomHeader("ETag", etag);
134 return http_response.Pass();
kinuko 2015/12/15 15:28:54 std::move
Adam Rice 2016/01/05 09:21:44 Done.
Adam Rice 2016/01/05 11:40:32 Actually, this caused a compiler error about std::
135 }
136
137 // This is only used by the StaleWhileRevalidateIsApplied test.
138 base::RunLoop run_loop_;
139 int requests_counted_ = 0;
140
141 DISALLOW_COPY_AND_ASSIGN(AsyncRevalidationManagerBrowserTest);
142 };
143
144 // Verify that the "Cache-Control: stale-while-revalidate" directive correctly
145 // triggers an async revalidation.
146 IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest,
147 StaleWhileRevalidateIsApplied) {
148 RegisterCountingRequestHandler();
149 GURL url(embedded_test_server()->GetURL("/counted.html"));
kinuko 2015/12/15 15:28:54 nit: should these URL be a static const variable i
Adam Rice 2016/01/05 09:21:44 I used namespace-scoped constants to avoid needing
150
151 CheckTitleTest(url, "Version 1");
152
153 // Force the renderer to be destroyed so that the Blink cache doesn't
154 // interfere with the result.
155 NavigateToURL(shell(), GURL("about:blank"));
156
157 // Load the page again. We should get the stale version from the cache.
158 CheckTitleTest(url, "Version 1");
159
160 // Wait for the async revalidation to complete.
161 run_loop()->Run();
162 EXPECT_EQ(2, requests_counted());
kinuko 2015/12/15 15:28:54 Also check EXPECT_EQ(1, requests_counted()) before
Adam Rice 2016/01/05 09:21:44 Done.
163 }
164
165 // The fresh cache entry must become visible eventually.
166 IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest, CacheIsUpdated) {
167 using base::ASCIIToUTF16;
168 RegisterCountingRequestHandler();
169 GURL url(embedded_test_server()->GetURL("/counted.html"));
170
171 CheckTitleTest(url, "Version 1");
172
173 // Reset the renderer cache.
174 NavigateToURL(shell(), GURL("about:blank"));
175
176 // Load the page again. We should get the stale version from the cache.
177 CheckTitleTest(url, "Version 1");
178
179 // Load the page repeatedly until we see the new version.
180 const int kMaxLoads = 100;
181 int actual_loads = 0;
182 bool matched = false;
183
184 for (; actual_loads < kMaxLoads && !matched; ++actual_loads) {
185 TitleWatcher title_watcher(shell()->web_contents(),
186 ASCIIToUTF16("Version 2"));
187 title_watcher.AlsoWaitForTitle(ASCIIToUTF16("Version 1"));
188 NavigateToURL(shell(), GURL("about:blank"));
189 NavigateToURL(shell(), url);
190 matched = (title_watcher.WaitAndGetTitle() == ASCIIToUTF16("Version 2"));
191 }
192
193 EXPECT_TRUE(matched);
194 // actual_loads is usually 1 but larger numbers are not an error.
195 DVLOG(1) << "Number of loads: " << actual_loads;
kinuko 2015/12/15 15:28:54 Having DVLOG(1) in tests... what usage are you ima
Adam Rice 2016/01/05 09:21:44 If the test became flaky, the log could be used to
196 }
197
198 // When the asynchronous revalidation arrives, any cookies it contains must be
199 // applied immediately.
200 IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest,
201 CookieSetAsynchronously) {
202 RegisterCookieRequestHandler();
203 GURL url(embedded_test_server()->GetURL("/cookie.html"));
204
205 // Set cookie to version=1
206 NavigateToURL(shell(), url);
207
208 // Reset render cache.
209 NavigateToURL(shell(), GURL("about:blank"));
210
211 // The page will load from the cache, then when the async revalidation
212 // completes the cookie will update.
213 CheckTitleTest(url, "PASS");
214 }
215
216 } // namespace
217
218 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/content_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698