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

Side by Side Diff: net/http/http_cache_lookup_manager_unittest.cc

Issue 2503473004: Server push cancellation: add a new class HttpCacheLookupManager which implements ServerPushDelegate (Closed)
Patch Set: Merge branch 'master' of https://chromium.googlesource.com/chromium/src into SPC_CacheLookupDelegate Created 4 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 | « net/http/http_cache_lookup_manager.cc ('k') | net/net.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 (c) 2016 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/run_loop.h"
8 #include "net/base/net_errors.h"
9 #include "net/base/test_completion_callback.h"
10 #include "net/http/http_cache_lookup_manager.h"
11 #include "net/http/http_transaction_test_util.h"
12 #include "net/http/mock_http_cache.h"
13 #include "net/test/gtest_util.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 using net::test::IsOk;
18
19 namespace net {
20
21 namespace {
22
23 class MockServerPushHelper : public ServerPushDelegate::ServerPushHelper {
24 public:
25 explicit MockServerPushHelper(const GURL& url) : request_url_(url) {}
26
27 const GURL& GetURL() override { return request_url_; }
28
29 MOCK_METHOD0(Cancel, void());
30
31 private:
32 const GURL request_url_;
33 };
34
35 std::unique_ptr<MockTransaction> CreateMockTransaction(const GURL& url) {
36 MockTransaction mock_trans = {
37 url.spec().c_str(), "GET", base::Time(), "", LOAD_NORMAL,
38 "HTTP/1.1 200 OK",
39 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
40 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n",
41 base::Time(), "<html><body>Google Blah Blah</body></html>",
42 TEST_MODE_NORMAL, nullptr, nullptr, nullptr, 0, 0, OK};
43 return base::MakeUnique<MockTransaction>(mock_trans);
44 }
45
46 void PopulateCacheEntry(HttpCache* cache, const GURL& request_url) {
47 TestCompletionCallback callback;
48
49 std::unique_ptr<MockTransaction> mock_trans =
50 CreateMockTransaction(request_url);
51 AddMockTransaction(mock_trans.get());
52
53 MockHttpRequest request(*(mock_trans.get()));
54
55 std::unique_ptr<HttpTransaction> trans;
56 int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &trans);
57 EXPECT_THAT(rv, IsOk());
58 ASSERT_TRUE(trans.get());
59
60 rv = trans->Start(&request, callback.callback(), NetLogWithSource());
61 base::RunLoop().RunUntilIdle();
62
63 if (rv == ERR_IO_PENDING)
64 rv = callback.WaitForResult();
65
66 ASSERT_EQ(mock_trans->return_code, rv);
67 if (OK != rv)
68 return;
69
70 const HttpResponseInfo* response = trans->GetResponseInfo();
71 ASSERT_TRUE(response);
72
73 std::string content;
74 rv = ReadTransaction(trans.get(), &content);
75
76 EXPECT_THAT(rv, IsOk());
77 std::string expected(mock_trans->data);
78 EXPECT_EQ(expected, content);
79 RemoveMockTransaction(mock_trans.get());
80 }
81
82 } // namespace
83
84 TEST(HttpCacheLookupManagerTest, ServerPushMissCache) {
85 MockHttpCache mock_cache;
86 HttpCacheLookupManager push_delegate(mock_cache.http_cache(),
87 NetLogWithSource());
88 GURL request_url("http://www.example.com/pushed.jpg");
89
90 std::unique_ptr<MockServerPushHelper> push_helper =
91 base::MakeUnique<MockServerPushHelper>(request_url);
92 MockServerPushHelper* push_helper_ptr = push_helper.get();
93
94 // Receive a server push and should not cancel the push.
95 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(0);
96 push_delegate.OnPush(std::move(push_helper));
97 base::RunLoop().RunUntilIdle();
98
99 // Make sure no network transaction is created.
100 EXPECT_EQ(0, mock_cache.network_layer()->transaction_count());
101 EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
102 EXPECT_EQ(0, mock_cache.disk_cache()->create_count());
103 }
104
105 TEST(HttpCacheLookupManagerTest, ServerPushDoNotCreateCacheEntry) {
106 MockHttpCache mock_cache;
107 HttpCacheLookupManager push_delegate(mock_cache.http_cache(),
108 NetLogWithSource());
109 GURL request_url("http://www.example.com/pushed.jpg");
110
111 std::unique_ptr<MockServerPushHelper> push_helper =
112 base::MakeUnique<MockServerPushHelper>(request_url);
113 MockServerPushHelper* push_helper_ptr = push_helper.get();
114
115 // Receive a server push and should not cancel the push.
116 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(0);
117 push_delegate.OnPush(std::move(push_helper));
118 base::RunLoop().RunUntilIdle();
119
120 // Receive another server push for the same url.
121 std::unique_ptr<MockServerPushHelper> push_helper2 =
122 base::MakeUnique<MockServerPushHelper>(request_url);
123 MockServerPushHelper* push_helper_ptr2 = push_helper2.get();
124 EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(0);
125 push_delegate.OnPush(std::move(push_helper2));
126 base::RunLoop().RunUntilIdle();
127
128 // Verify no network transaction is created.
129 EXPECT_EQ(0, mock_cache.network_layer()->transaction_count());
130 EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
131 // Verify no cache entry is created for server push lookup.
132 EXPECT_EQ(0, mock_cache.disk_cache()->create_count());
133 }
134
135 TEST(HttpCacheLookupManagerTest, ServerPushHitCache) {
136 MockHttpCache mock_cache;
137 HttpCacheLookupManager push_delegate(mock_cache.http_cache(),
138 NetLogWithSource());
139 GURL request_url("http://www.example.com/pushed.jpg");
140
141 // Populate the cache entry so that the cache lookup for server push hits.
142 PopulateCacheEntry(mock_cache.http_cache(), request_url);
143 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
144 EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
145 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
146
147 // Add another mock transaction since the OnPush will create a new cache
148 // transaction.
149 std::unique_ptr<MockTransaction> mock_trans =
150 CreateMockTransaction(request_url);
151 AddMockTransaction(mock_trans.get());
152
153 std::unique_ptr<MockServerPushHelper> push_helper =
154 base::MakeUnique<MockServerPushHelper>(request_url);
155 MockServerPushHelper* push_helper_ptr = push_helper.get();
156
157 // Receive a server push and should cancel the push.
158 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(1);
159 push_delegate.OnPush(std::move(push_helper));
160 base::RunLoop().RunUntilIdle();
161
162 // Make sure no new net layer transaction is created.
163 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
164 EXPECT_EQ(1, mock_cache.disk_cache()->open_count());
165 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
166
167 RemoveMockTransaction(mock_trans.get());
168 }
169
170 // Test when a server push is received while the HttpCacheLookupManager has a
171 // pending lookup transaction for the same URL, the new server push will not
172 // send a new lookup transaction and should not be canceled.
173 TEST(HttpCacheLookupManagerTest, ServerPushPendingLookup) {
174 MockHttpCache mock_cache;
175 HttpCacheLookupManager push_delegate(mock_cache.http_cache(),
176 NetLogWithSource());
177 GURL request_url("http://www.example.com/pushed.jpg");
178
179 // Populate the cache entry so that the cache lookup for server push hits.
180 PopulateCacheEntry(mock_cache.http_cache(), request_url);
181 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
182 EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
183 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
184
185 // Add another mock transaction since the OnPush will create a new cache
186 // transaction.
187 std::unique_ptr<MockTransaction> mock_trans =
188 CreateMockTransaction(request_url);
189 AddMockTransaction(mock_trans.get());
190
191 std::unique_ptr<MockServerPushHelper> push_helper =
192 base::MakeUnique<MockServerPushHelper>(request_url);
193 MockServerPushHelper* push_helper_ptr = push_helper.get();
194
195 // Receive a server push and should cancel the push eventually.
196 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(1);
197 push_delegate.OnPush(std::move(push_helper));
198
199 std::unique_ptr<MockServerPushHelper> push_helper2 =
200 base::MakeUnique<MockServerPushHelper>(request_url);
201 MockServerPushHelper* push_helper_ptr2 = push_helper2.get();
202
203 // Receive another server push and should not cancel the push.
204 EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(0);
205 push_delegate.OnPush(std::move(push_helper2));
206
207 base::RunLoop().RunUntilIdle();
208
209 // Make sure no new net layer transaction is created.
210 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
211 EXPECT_EQ(1, mock_cache.disk_cache()->open_count());
212 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
213
214 RemoveMockTransaction(mock_trans.get());
215 }
216
217 // Test the server push lookup is based on the full url.
218 TEST(HttpCacheLookupManagerTest, ServerPushLookupOnUrl) {
219 MockHttpCache mock_cache;
220 HttpCacheLookupManager push_delegate(mock_cache.http_cache(),
221 NetLogWithSource());
222 GURL request_url("http://www.example.com/pushed.jpg?u=0");
223 GURL request_url2("http://www.example.com/pushed.jpg?u=1");
224
225 // Populate the cache entry so that the cache lookup for server push hits.
226 PopulateCacheEntry(mock_cache.http_cache(), request_url);
227 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
228 EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
229 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
230
231 // Add another mock transaction since the OnPush will create a new cache
232 // transaction.
233 std::unique_ptr<MockTransaction> mock_trans =
234 CreateMockTransaction(request_url);
235 AddMockTransaction(mock_trans.get());
236
237 std::unique_ptr<MockServerPushHelper> push_helper =
238 base::MakeUnique<MockServerPushHelper>(request_url);
239 MockServerPushHelper* push_helper_ptr = push_helper.get();
240
241 // Receive a server push and should cancel the push eventually.
242 EXPECT_CALL(*push_helper_ptr, Cancel()).Times(1);
243 push_delegate.OnPush(std::move(push_helper));
244 // Run until the lookup transaction finishes for the first server push.
245 base::RunLoop().RunUntilIdle();
246 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
247 EXPECT_EQ(1, mock_cache.disk_cache()->open_count());
248 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
249 RemoveMockTransaction(mock_trans.get());
250
251 AddMockTransaction(mock_trans.get());
252 // Receive the second server push with same url after the first lookup
253 // finishes, and should cancel the push.
254 std::unique_ptr<MockServerPushHelper> push_helper2 =
255 base::MakeUnique<MockServerPushHelper>(request_url);
256 MockServerPushHelper* push_helper_ptr2 = push_helper2.get();
257
258 EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(1);
259 push_delegate.OnPush(std::move(push_helper2));
260 // Run until the lookup transaction finishes for the second server push.
261 base::RunLoop().RunUntilIdle();
262 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
263 EXPECT_EQ(2, mock_cache.disk_cache()->open_count());
264 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
265 RemoveMockTransaction(mock_trans.get());
266
267 std::unique_ptr<MockTransaction> mock_trans3 =
268 CreateMockTransaction(request_url2);
269 AddMockTransaction(mock_trans3.get());
270 // Receive the third server push with a different url after lookup for a
271 // similar server push has been completed, should not cancel the push.
272 std::unique_ptr<MockServerPushHelper> push_helper3 =
273 base::MakeUnique<MockServerPushHelper>(request_url2);
274 MockServerPushHelper* push_helper_ptr3 = push_helper3.get();
275
276 EXPECT_CALL(*push_helper_ptr3, Cancel()).Times(0);
277 push_delegate.OnPush(std::move(push_helper3));
278
279 base::RunLoop().RunUntilIdle();
280 // Make sure no new net layer transaction is created.
281 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
282 EXPECT_EQ(2, mock_cache.disk_cache()->open_count());
283 EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
284 RemoveMockTransaction(mock_trans3.get());
285 }
286
287 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_cache_lookup_manager.cc ('k') | net/net.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698