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

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: Add missing base:: prefix to UTF16ToUTF8 function. Created 4 years, 11 months 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 #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 ::testing::AssertionResult TitleBecomes(const GURL& url,
61 const std::string& expected_title) {
62 base::string16 expected_title16(base::ASCIIToUTF16(expected_title));
63 TitleWatcher title_watcher(shell()->web_contents(), expected_title16);
64 NavigateToURL(shell(), url);
65 std::string actual_title =
66 base::UTF16ToUTF8(title_watcher.WaitAndGetTitle());
67 if (actual_title == expected_title) {
68 return ::testing::AssertionSuccess();
69 } else {
70 return ::testing::AssertionFailure() << "Expected title '"
71 << expected_title << "', but got '"
72 << actual_title << "'";
kinuko 2016/01/06 08:17:13 I wonder it'd be probably more readable if this me
Adam Rice 2016/01/06 08:32:26 Unfortunately content::TitleWatcher can only wait
kinuko 2016/01/06 08:44:52 Hmm, but in either way aren't we calling title_wat
Adam Rice 2016/01/12 13:20:30 Good point. I have simplified it and added a comme
73 }
74 }
75
76 void RegisterCountingRequestHandler() {
77 embedded_test_server()->RegisterRequestHandler(base::Bind(
78 &AsyncRevalidationManagerBrowserTest::CountingRequestHandler, this));
79 }
80
81 void RegisterCookieRequestHandler() {
82 embedded_test_server()->RegisterRequestHandler(base::Bind(
83 &AsyncRevalidationManagerBrowserTest::CookieRequestHandler, this));
84 }
85
86 private:
87 // A request handler which increases the number in the title tag on every
88 // request.
89 scoped_ptr<HttpResponse> CountingRequestHandler(const HttpRequest& request) {
90 if (request.relative_url != kCountedHtmlPath)
91 return nullptr;
92
93 int version = ++requests_counted_;
94
95 scoped_ptr<BasicHttpResponse> http_response(StaleWhileRevalidateHeaders());
96 http_response->set_content(
97 base::StringPrintf("<title>Version %d</title>", version));
98
99 // The second time this handler is run is the async revalidation. Tests can
100 // use this for synchronisation.
101 if (version == 2)
102 run_loop_.Quit();
103 return std::move(http_response);
104 }
105
106 // A request handler which increases a cookie value on every request.
107 scoped_ptr<HttpResponse> CookieRequestHandler(const HttpRequest& request) {
108 static const char kHtml[] =
109 "<script>\n"
110 "var intervalId;\n"
111 "function checkCookie() {\n"
112 " if (document.cookie.search(/version=2/) != -1) {\n"
113 " clearInterval(intervalId);\n"
114 " document.title = \"PASS\";\n"
115 " }\n"
116 "}\n"
117 "intervalId = setInterval(checkCookie, 10);\n"
118 "</script>\n"
119 "<title>Loaded</title>\n";
120
121 if (request.relative_url != kCookieHtmlPath)
122 return nullptr;
123
124 int version = ++requests_counted_;
125
126 scoped_ptr<BasicHttpResponse> http_response(StaleWhileRevalidateHeaders());
127 http_response->AddCustomHeader("Set-Cookie",
128 base::StringPrintf("version=%d", version));
129 http_response->set_content(kHtml);
130
131 return std::move(http_response);
132 }
133
134 // Generate the standard response headers common to all request handlers.
135 scoped_ptr<BasicHttpResponse> StaleWhileRevalidateHeaders() {
136 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse);
137 http_response->set_code(net::HTTP_OK);
138 http_response->set_content_type("text/html; charset=utf-8");
139 http_response->AddCustomHeader("Cache-Control",
140 "max-age=0, stale-while-revalidate=86400");
141 // A validator is needed for revalidations, and hence
142 // stale-while-revalidate, to work.
143 std::string etag = base::StringPrintf(
144 "\"AsyncRevalidationManagerBrowserTest%d\"", requests_counted_);
145 http_response->AddCustomHeader("ETag", etag);
146 return http_response;
147 }
148
149 base::RunLoop run_loop_;
150 int requests_counted_ = 0;
151
152 DISALLOW_COPY_AND_ASSIGN(AsyncRevalidationManagerBrowserTest);
153 };
154
155 // Verify that the "Cache-Control: stale-while-revalidate" directive correctly
156 // triggers an async revalidation.
157 IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest,
158 StaleWhileRevalidateIsApplied) {
159 RegisterCountingRequestHandler();
160 GURL url(embedded_test_server()->GetURL(kCountedHtmlPath));
161
162 EXPECT_TRUE(TitleBecomes(url, "Version 1"));
163
164 // The first request happens synchronously.
165 EXPECT_EQ(1, requests_counted());
166
167 // Force the renderer to be destroyed so that the Blink cache doesn't
168 // interfere with the result.
169 NavigateToURL(shell(), GURL("about:blank"));
170
171 // Load the page again. We should get the stale version from the cache.
172 EXPECT_TRUE(TitleBecomes(url, "Version 1"));
173
174 // Wait for the async revalidation to complete.
175 run_loop()->Run();
176 EXPECT_EQ(2, requests_counted());
177 }
178
179 // The fresh cache entry must become visible once the async revalidation request
180 // has been sent.
181 IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest, CacheIsUpdated) {
182 using base::ASCIIToUTF16;
183 RegisterCountingRequestHandler();
184 GURL url(embedded_test_server()->GetURL(kCountedHtmlPath));
185
186 EXPECT_TRUE(TitleBecomes(url, "Version 1"));
187
188 // Reset the renderer cache.
189 NavigateToURL(shell(), GURL("about:blank"));
190
191 // Load the page again. We should get the stale version from the cache.
192 EXPECT_TRUE(TitleBecomes(url, "Version 1"));
193
194 // Wait for the async revalidation request to be processed by the
195 // EmbeddedTestServer.
196 run_loop()->Run();
197
198 // Reset the renderer cache.
199 NavigateToURL(shell(), GURL("about:blank"));
200
201 // Since the async revalidation request has been sent, the cache can no
202 // longer return the stale contents.
203 EXPECT_TRUE(TitleBecomes(url, "Version 2"));
204 }
205
206 // When the asynchronous revalidation arrives, any cookies it contains must be
207 // applied immediately.
208 IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest,
209 CookieSetAsynchronously) {
210 RegisterCookieRequestHandler();
211 GURL url(embedded_test_server()->GetURL(kCookieHtmlPath));
212
213 // Set cookie to version=1
214 NavigateToURL(shell(), url);
215
216 // Reset render cache.
217 NavigateToURL(shell(), GURL("about:blank"));
218
219 // The page will load from the cache, then when the async revalidation
220 // completes the cookie will update.
221 EXPECT_TRUE(TitleBecomes(url, "PASS"));
222 }
223
224 } // namespace
225
226 } // 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