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

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

Issue 1987973002: Reload cache control: remove redundant header insertion from Blink Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: (just rebase to see bots' results again) Created 4 years, 6 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 | third_party/WebKit/Source/core/fetch/ResourceFetcher.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <memory> 5 #include <memory>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/feature_list.h" 9 #include "base/feature_list.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "content/public/common/content_features.h" 11 #include "content/public/common/content_features.h"
12 #include "content/public/test/content_browser_test.h" 12 #include "content/public/test/content_browser_test.h"
13 #include "content/public/test/content_browser_test_utils.h" 13 #include "content/public/test/content_browser_test_utils.h"
14 #include "content/shell/browser/shell.h" 14 #include "content/shell/browser/shell.h"
15 #include "net/test/embedded_test_server/embedded_test_server.h" 15 #include "net/test/embedded_test_server/embedded_test_server.h"
16 #include "net/test/embedded_test_server/http_request.h" 16 #include "net/test/embedded_test_server/http_request.h"
17 #include "net/test/embedded_test_server/http_response.h" 17 #include "net/test/embedded_test_server/http_response.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "url/gurl.h" 19 #include "url/gurl.h"
20 20
21 namespace content { 21 namespace content {
22 22
23 namespace { 23 namespace {
24 24
25 using net::test_server::BasicHttpResponse;
25 using net::test_server::HttpRequest; 26 using net::test_server::HttpRequest;
26 using net::test_server::HttpResponse; 27 using net::test_server::HttpResponse;
27 28
28 const char kReloadTestPath[] = "/loader/reload.html"; 29 const char kReloadTestPath[] = "/loader/reload.html";
30 const char kPseudoReloadTestPath[] = "/loader/_reload.html";
29 const char kReloadImagePath[] = "/loader/empty16x16.png"; 31 const char kReloadImagePath[] = "/loader/empty16x16.png";
30 32
31 const char kNoCacheControl[] = ""; 33 const char kNoCacheControl[] = "";
32 const char kMaxAgeCacheControl[] = "max-age=0"; 34 const char kMaxAgeCacheControl[] = "max-age=0";
33 const char kNoCacheCacheControl[] = "no-cache"; 35 const char kNoCacheCacheControl[] = "no-cache";
34 36
35 struct RequestLog { 37 struct RequestLog {
36 std::string relative_url; 38 std::string relative_url;
37 std::string cache_control; 39 std::string cache_control;
40 bool has_if_none_match;
41 bool has_if_modified_since;
38 }; 42 };
39 43
40 class ReloadCacheControlBrowserTest : public ContentBrowserTest { 44 class ReloadCacheControlBrowserTest : public ContentBrowserTest {
41 protected: 45 protected:
42 ReloadCacheControlBrowserTest() = default; 46 ReloadCacheControlBrowserTest() = default;
43 ~ReloadCacheControlBrowserTest() override = default; 47 ~ReloadCacheControlBrowserTest() override = default;
44 48
45 void SetUpOnMainThread() override { 49 void SetUpOnMainThread() override {
46 // ContentBrowserTest creates embedded_test_server instance with 50 // ContentBrowserTest creates embedded_test_server instance with
47 // a registered HandleFileRequest for "content/test/data". 51 // a registered HandleFileRequest for "content/test/data".
48 // Because the handler is registered as the first handler, MonitorHandler 52 // Because the handler is registered as the first handler, MonitorHandler
49 // is needed to capture all requests. 53 // is needed to capture all requests.
50 embedded_test_server()->RegisterRequestMonitor(base::Bind( 54 embedded_test_server()->RegisterRequestMonitor(
51 &ReloadCacheControlBrowserTest::MonitorRequestHandler, this)); 55 base::Bind(&ReloadCacheControlBrowserTest::RequestMonitor, this));
56
57 embedded_test_server()->RegisterRequestHandler(
58 base::Bind(&ReloadCacheControlBrowserTest::RequestHandler, this));
52 59
53 ASSERT_TRUE(embedded_test_server()->Start()); 60 ASSERT_TRUE(embedded_test_server()->Start());
54 } 61 }
55 62
56 protected: 63 protected:
57 std::vector<RequestLog> request_log_; 64 std::vector<RequestLog> request_log_;
58 65
59 private: 66 private:
60 void MonitorRequestHandler(const HttpRequest& request) { 67 void RequestMonitor(const HttpRequest& request) {
61 RequestLog log; 68 RequestLog log;
62 log.relative_url = request.relative_url; 69 log.relative_url = request.relative_url;
63 auto cache_control = request.headers.find("Cache-Control"); 70 const auto cache_control = request.headers.find("Cache-Control");
64 log.cache_control = cache_control == request.headers.end() 71 log.cache_control = cache_control == request.headers.end()
65 ? kNoCacheControl 72 ? kNoCacheControl
66 : cache_control->second; 73 : cache_control->second;
74 log.has_if_none_match =
75 request.headers.find("If-None-Match") != request.headers.end();
76 log.has_if_modified_since =
77 request.headers.find("If-Modified-Since") != request.headers.end();
67 request_log_.push_back(log); 78 request_log_.push_back(log);
68 } 79 }
69 80
81 std::unique_ptr<HttpResponse> RequestHandler(const HttpRequest& request) {
82 if (request.relative_url != kPseudoReloadTestPath)
83 return nullptr;
84
85 std::unique_ptr<BasicHttpResponse> response(new BasicHttpResponse);
86 response->set_code(net::HTTP_OK);
87 response->set_content_type("text/html; charset=utf-8");
88 response->AddCustomHeader("Last-Modified", "Tue, 15 Nov 1994 12:45:26 GMT");
89 response->set_content("<html></html>");
90 return std::move(response);
91 }
92
70 DISALLOW_COPY_AND_ASSIGN(ReloadCacheControlBrowserTest); 93 DISALLOW_COPY_AND_ASSIGN(ReloadCacheControlBrowserTest);
71 }; 94 };
72 95
73 class ReloadCacheControlWithAnExperimentBrowserTest 96 class ReloadCacheControlWithAnExperimentBrowserTest
74 : public ReloadCacheControlBrowserTest { 97 : public ReloadCacheControlBrowserTest {
75 protected: 98 protected:
76 ReloadCacheControlWithAnExperimentBrowserTest() = default; 99 ReloadCacheControlWithAnExperimentBrowserTest() = default;
77 ~ReloadCacheControlWithAnExperimentBrowserTest() override = default; 100 ~ReloadCacheControlWithAnExperimentBrowserTest() override = default;
78 101
79 void SetUpOnMainThread() override { 102 void SetUpOnMainThread() override {
(...skipping 11 matching lines...) Expand all
91 114
92 IN_PROC_BROWSER_TEST_F(ReloadCacheControlBrowserTest, NormalReload) { 115 IN_PROC_BROWSER_TEST_F(ReloadCacheControlBrowserTest, NormalReload) {
93 GURL url(embedded_test_server()->GetURL(kReloadTestPath)); 116 GURL url(embedded_test_server()->GetURL(kReloadTestPath));
94 117
95 EXPECT_TRUE(NavigateToURL(shell(), url)); 118 EXPECT_TRUE(NavigateToURL(shell(), url));
96 ReloadBlockUntilNavigationsComplete(shell(), 1); 119 ReloadBlockUntilNavigationsComplete(shell(), 1);
97 120
98 ASSERT_EQ(4UL, request_log_.size()); 121 ASSERT_EQ(4UL, request_log_.size());
99 EXPECT_EQ(kReloadTestPath, request_log_[0].relative_url); 122 EXPECT_EQ(kReloadTestPath, request_log_[0].relative_url);
100 EXPECT_EQ(kNoCacheControl, request_log_[0].cache_control); 123 EXPECT_EQ(kNoCacheControl, request_log_[0].cache_control);
124 EXPECT_FALSE(request_log_[0].has_if_none_match);
125 EXPECT_FALSE(request_log_[0].has_if_modified_since);
101 EXPECT_EQ(kReloadImagePath, request_log_[1].relative_url); 126 EXPECT_EQ(kReloadImagePath, request_log_[1].relative_url);
102 EXPECT_EQ(kNoCacheControl, request_log_[1].cache_control); 127 EXPECT_EQ(kNoCacheControl, request_log_[1].cache_control);
128 EXPECT_FALSE(request_log_[1].has_if_none_match);
129 EXPECT_FALSE(request_log_[1].has_if_modified_since);
103 130
131 // The test server uses ETag to serve files by default, and it results in
132 // using If-None-Match header for reloads.
104 EXPECT_EQ(kReloadTestPath, request_log_[2].relative_url); 133 EXPECT_EQ(kReloadTestPath, request_log_[2].relative_url);
105 EXPECT_EQ(kMaxAgeCacheControl, request_log_[2].cache_control); 134 EXPECT_EQ(kMaxAgeCacheControl, request_log_[2].cache_control);
135 EXPECT_TRUE(request_log_[2].has_if_none_match);
136 EXPECT_FALSE(request_log_[2].has_if_modified_since);
106 EXPECT_EQ(kReloadImagePath, request_log_[3].relative_url); 137 EXPECT_EQ(kReloadImagePath, request_log_[3].relative_url);
107 EXPECT_EQ(kMaxAgeCacheControl, request_log_[3].cache_control); 138 EXPECT_EQ(kMaxAgeCacheControl, request_log_[3].cache_control);
139 EXPECT_TRUE(request_log_[3].has_if_none_match);
140 EXPECT_FALSE(request_log_[3].has_if_modified_since);
141 }
142
143 IN_PROC_BROWSER_TEST_F(ReloadCacheControlBrowserTest, ReloadWithLastModified) {
144 GURL url(embedded_test_server()->GetURL(kPseudoReloadTestPath));
145
146 EXPECT_TRUE(NavigateToURL(shell(), url));
147 ReloadBlockUntilNavigationsComplete(shell(), 1);
148
149 ASSERT_EQ(2UL, request_log_.size());
150 EXPECT_EQ(kPseudoReloadTestPath, request_log_[0].relative_url);
151 EXPECT_EQ(kNoCacheControl, request_log_[0].cache_control);
152 EXPECT_FALSE(request_log_[0].has_if_none_match);
153 EXPECT_FALSE(request_log_[0].has_if_modified_since);
154
155 // kPseudoReloadTestPath is handled by RequestHandler() that returns
156 // a content with Last-Modified header, and it results in using
157 // using If-Modified-Since header for reloads.
158 EXPECT_EQ(kPseudoReloadTestPath, request_log_[1].relative_url);
159 EXPECT_EQ(kMaxAgeCacheControl, request_log_[1].cache_control);
160 EXPECT_FALSE(request_log_[1].has_if_none_match);
161 EXPECT_TRUE(request_log_[1].has_if_modified_since);
108 } 162 }
109 163
110 IN_PROC_BROWSER_TEST_F(ReloadCacheControlBrowserTest, BypassingReload) { 164 IN_PROC_BROWSER_TEST_F(ReloadCacheControlBrowserTest, BypassingReload) {
111 GURL url(embedded_test_server()->GetURL(kReloadTestPath)); 165 GURL url(embedded_test_server()->GetURL(kReloadTestPath));
112 166
113 EXPECT_TRUE(NavigateToURL(shell(), url)); 167 EXPECT_TRUE(NavigateToURL(shell(), url));
114 ReloadBypassingCacheBlockUntilNavigationsComplete(shell(), 1); 168 ReloadBypassingCacheBlockUntilNavigationsComplete(shell(), 1);
115 169
116 ASSERT_EQ(4UL, request_log_.size()); 170 ASSERT_EQ(4UL, request_log_.size());
117 EXPECT_EQ(kReloadTestPath, request_log_[0].relative_url); 171 EXPECT_EQ(kReloadTestPath, request_log_[0].relative_url);
118 EXPECT_EQ(kNoCacheControl, request_log_[0].cache_control); 172 EXPECT_EQ(kNoCacheControl, request_log_[0].cache_control);
173 EXPECT_FALSE(request_log_[0].has_if_none_match);
174 EXPECT_FALSE(request_log_[0].has_if_modified_since);
119 EXPECT_EQ(kReloadImagePath, request_log_[1].relative_url); 175 EXPECT_EQ(kReloadImagePath, request_log_[1].relative_url);
120 EXPECT_EQ(kNoCacheControl, request_log_[1].cache_control); 176 EXPECT_EQ(kNoCacheControl, request_log_[1].cache_control);
177 EXPECT_FALSE(request_log_[1].has_if_none_match);
178 EXPECT_FALSE(request_log_[1].has_if_modified_since);
121 179
180 // Bypassing reload should not use If-None-Match and If-Modified-Since headers
181 // because such requests imply no cache system exist and have no chance to
182 // handle 304 responses.
122 EXPECT_EQ(kReloadTestPath, request_log_[2].relative_url); 183 EXPECT_EQ(kReloadTestPath, request_log_[2].relative_url);
123 EXPECT_EQ(kNoCacheCacheControl, request_log_[2].cache_control); 184 EXPECT_EQ(kNoCacheCacheControl, request_log_[2].cache_control);
185 EXPECT_FALSE(request_log_[2].has_if_none_match);
186 EXPECT_FALSE(request_log_[2].has_if_modified_since);
124 EXPECT_EQ(kReloadImagePath, request_log_[3].relative_url); 187 EXPECT_EQ(kReloadImagePath, request_log_[3].relative_url);
125 EXPECT_EQ(kNoCacheCacheControl, request_log_[3].cache_control); 188 EXPECT_EQ(kNoCacheCacheControl, request_log_[3].cache_control);
189 EXPECT_FALSE(request_log_[3].has_if_none_match);
190 EXPECT_FALSE(request_log_[3].has_if_modified_since);
126 } 191 }
127 192
128 IN_PROC_BROWSER_TEST_F(ReloadCacheControlWithAnExperimentBrowserTest, 193 IN_PROC_BROWSER_TEST_F(ReloadCacheControlWithAnExperimentBrowserTest,
129 ReloadMainResource) { 194 ReloadMainResource) {
130 GURL url(embedded_test_server()->GetURL(kReloadTestPath)); 195 GURL url(embedded_test_server()->GetURL(kReloadTestPath));
131 196
132 EXPECT_TRUE(NavigateToURL(shell(), url)); 197 EXPECT_TRUE(NavigateToURL(shell(), url));
133 ReloadBlockUntilNavigationsComplete(shell(), 1); 198 ReloadBlockUntilNavigationsComplete(shell(), 1);
134 199
135 ASSERT_EQ(4UL, request_log_.size()); 200 ASSERT_EQ(4UL, request_log_.size());
136 EXPECT_EQ(kReloadTestPath, request_log_[0].relative_url); 201 EXPECT_EQ(kReloadTestPath, request_log_[0].relative_url);
137 EXPECT_EQ(kNoCacheControl, request_log_[0].cache_control); 202 EXPECT_EQ(kNoCacheControl, request_log_[0].cache_control);
203 EXPECT_FALSE(request_log_[0].has_if_none_match);
204 EXPECT_FALSE(request_log_[0].has_if_modified_since);
138 EXPECT_EQ(kReloadImagePath, request_log_[1].relative_url); 205 EXPECT_EQ(kReloadImagePath, request_log_[1].relative_url);
139 EXPECT_EQ(kNoCacheControl, request_log_[1].cache_control); 206 EXPECT_EQ(kNoCacheControl, request_log_[1].cache_control);
207 EXPECT_FALSE(request_log_[1].has_if_none_match);
208 EXPECT_FALSE(request_log_[1].has_if_modified_since);
140 209
141 EXPECT_EQ(kReloadTestPath, request_log_[2].relative_url); 210 EXPECT_EQ(kReloadTestPath, request_log_[2].relative_url);
142 EXPECT_EQ(kMaxAgeCacheControl, request_log_[2].cache_control); 211 EXPECT_EQ(kMaxAgeCacheControl, request_log_[2].cache_control);
212 EXPECT_TRUE(request_log_[2].has_if_none_match);
213 EXPECT_FALSE(request_log_[2].has_if_modified_since);
143 EXPECT_EQ(kReloadImagePath, request_log_[3].relative_url); 214 EXPECT_EQ(kReloadImagePath, request_log_[3].relative_url);
144 EXPECT_EQ(kNoCacheControl, request_log_[3].cache_control); 215 EXPECT_EQ(kNoCacheControl, request_log_[3].cache_control);
216 EXPECT_TRUE(request_log_[3].has_if_none_match);
217 EXPECT_FALSE(request_log_[3].has_if_modified_since);
145 } 218 }
146 219
147 // TODO(toyoshim): Add another set of reload tests with DevTools open. 220 // TODO(toyoshim): Add another set of reload tests with DevTools open.
148 221
149 } // namespace 222 } // namespace
150 223
151 } // namespace content 224 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/core/fetch/ResourceFetcher.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698