summaryrefslogtreecommitdiff
path: root/chromium/content/browser/loader/async_revalidation_manager_browsertest.cc
blob: 56b6a128b6293b8099e84daf8a84b5979e66a568 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <string>
#include <utility>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/feature_list.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/strings/string16.h"
#include "base/strings/string_piece.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/common/browser_side_navigation_policy.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace content {

namespace {

using net::test_server::HttpResponse;
using net::test_server::HttpRequest;
using net::test_server::BasicHttpResponse;

const char kCountedHtmlPath[] = "/counted.html";
const char kCookieHtmlPath[] = "/cookie.html";

class AsyncRevalidationManagerBrowserTest : public ContentBrowserTest {
 protected:
  AsyncRevalidationManagerBrowserTest() {}
  ~AsyncRevalidationManagerBrowserTest() override {}

  void SetUp() override {
    base::FeatureList::ClearInstanceForTesting();
    std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
    feature_list->InitializeFromCommandLine(
        "StaleWhileRevalidate2", std::string());
    base::FeatureList::SetInstance(std::move(feature_list));

    ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
    ContentBrowserTest::SetUp();
  }

  void SetUpOnMainThread() override {
    embedded_test_server()->StartAcceptingConnections();
  }

  base::RunLoop* run_loop() { return &run_loop_; }
  int requests_counted() const { return requests_counted_; }

  // This method lacks diagnostics for the failure case because TitleWatcher
  // will just wait until the test times out if |expected_title| does not
  // appear.
  bool TitleBecomes(const GURL& url, const std::string& expected_title) {
    base::string16 expected_title16(base::ASCIIToUTF16(expected_title));
    TitleWatcher title_watcher(shell()->web_contents(), expected_title16);
    NavigateToURL(shell(), url);
    return title_watcher.WaitAndGetTitle() == expected_title16;
  }

  void RegisterCountingRequestHandler() {
    embedded_test_server()->RegisterRequestHandler(base::Bind(
        &AsyncRevalidationManagerBrowserTest::CountingRequestHandler, this));
  }

  void RegisterCookieRequestHandler() {
    embedded_test_server()->RegisterRequestHandler(base::Bind(
        &AsyncRevalidationManagerBrowserTest::CookieRequestHandler, this));
  }

 private:
  // A request handler which increases the number in the title tag on every
  // request.
  std::unique_ptr<HttpResponse> CountingRequestHandler(
      const HttpRequest& request) {
    if (request.relative_url != kCountedHtmlPath)
      return nullptr;

    int version = ++requests_counted_;

    std::unique_ptr<BasicHttpResponse> http_response(
        StaleWhileRevalidateHeaders());
    http_response->set_content(
        base::StringPrintf("<title>Version %d</title>", version));

    // The second time this handler is run is the async revalidation. Tests can
    // use this for synchronisation.
    if (version == 2)
      run_loop_.Quit();
    return std::move(http_response);
  }

  // A request handler which increases a cookie value on every request.
  std::unique_ptr<HttpResponse> CookieRequestHandler(
      const HttpRequest& request) {
    static const char kHtml[] =
        "<script>\n"
        "var intervalId;\n"
        "function checkCookie() {\n"
        "  if (document.cookie.search(/version=2/) != -1) {\n"
        "    clearInterval(intervalId);\n"
        "    document.title = \"PASS\";\n"
        "  }\n"
        "}\n"
        "intervalId = setInterval(checkCookie, 10);\n"
        "</script>\n"
        "<title>Loaded</title>\n";

    if (request.relative_url != kCookieHtmlPath)
      return nullptr;

    int version = ++requests_counted_;

    std::unique_ptr<BasicHttpResponse> http_response(
        StaleWhileRevalidateHeaders());
    http_response->AddCustomHeader("Set-Cookie",
                                   base::StringPrintf("version=%d", version));
    http_response->set_content(kHtml);

    return std::move(http_response);
  }

  // Generate the standard response headers common to all request handlers.
  std::unique_ptr<BasicHttpResponse> StaleWhileRevalidateHeaders() {
    std::unique_ptr<BasicHttpResponse> http_response(new BasicHttpResponse);
    http_response->set_code(net::HTTP_OK);
    http_response->set_content_type("text/html; charset=utf-8");
    http_response->AddCustomHeader("Cache-Control",
                                   "max-age=0, stale-while-revalidate=86400");
    // A validator is needed for revalidations, and hence
    // stale-while-revalidate, to work.
    std::string etag = base::StringPrintf(
        "\"AsyncRevalidationManagerBrowserTest%d\"", requests_counted_);
    http_response->AddCustomHeader("ETag", etag);
    return http_response;
  }

  base::RunLoop run_loop_;
  int requests_counted_ = 0;

  DISALLOW_COPY_AND_ASSIGN(AsyncRevalidationManagerBrowserTest);
};

// Verify that the "Cache-Control: stale-while-revalidate" directive correctly
// triggers an async revalidation.
IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest,
                       StaleWhileRevalidateIsApplied) {
  // PlzNavigate: Stale while revalidate is disabled.
  // TODO(clamy): Re-enable the test when there is support.
  if (IsBrowserSideNavigationEnabled())
    return;
  RegisterCountingRequestHandler();
  GURL url(embedded_test_server()->GetURL(kCountedHtmlPath));

  EXPECT_TRUE(TitleBecomes(url, "Version 1"));

  // The first request happens synchronously.
  EXPECT_EQ(1, requests_counted());

  // Force the renderer to be destroyed so that the Blink cache doesn't
  // interfere with the result.
  NavigateToURL(shell(), GURL("about:blank"));

  // Load the page again. We should get the stale version from the cache.
  EXPECT_TRUE(TitleBecomes(url, "Version 1"));

  // Wait for the async revalidation to complete.
  run_loop()->Run();
  EXPECT_EQ(2, requests_counted());
}

// The fresh cache entry must become visible once the async revalidation request
// has been sent.
IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest, CacheIsUpdated) {
  // PlzNavigate: Stale while revalidate is disabled.
  // TODO(clamy): Re-enable the test when there is support.
  if (IsBrowserSideNavigationEnabled())
    return;
  using base::ASCIIToUTF16;
  RegisterCountingRequestHandler();
  GURL url(embedded_test_server()->GetURL(kCountedHtmlPath));

  EXPECT_TRUE(TitleBecomes(url, "Version 1"));

  // Reset the renderer cache.
  NavigateToURL(shell(), GURL("about:blank"));

  // Load the page again. We should get the stale version from the cache.
  EXPECT_TRUE(TitleBecomes(url, "Version 1"));

  // Wait for the async revalidation request to be processed by the
  // EmbeddedTestServer.
  run_loop()->Run();

  // Reset the renderer cache.
  NavigateToURL(shell(), GURL("about:blank"));

  // Since the async revalidation request has been sent, the cache can no
  // longer return the stale contents.
  EXPECT_TRUE(TitleBecomes(url, "Version 2"));
}

// When the asynchronous revalidation arrives, any cookies it contains must be
// applied immediately.
IN_PROC_BROWSER_TEST_F(AsyncRevalidationManagerBrowserTest,
                       CookieSetAsynchronously) {
  // PlzNavigate: Stale while revalidate is disabled.
  // TODO(clamy): Re-enable the test when there is support.
  if (IsBrowserSideNavigationEnabled())
    return;
  RegisterCookieRequestHandler();
  GURL url(embedded_test_server()->GetURL(kCookieHtmlPath));

  // Set cookie to version=1
  NavigateToURL(shell(), url);

  // Reset render cache.
  NavigateToURL(shell(), GURL("about:blank"));

  // The page will load from the cache, then when the async revalidation
  // completes the cookie will update.
  EXPECT_TRUE(TitleBecomes(url, "PASS"));
}

}  // namespace

}  // namespace content