OLD | NEW |
---|---|
(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( | |
jkarlin
2016/11/21 14:40:41
std::unique_ptr<MockServerPushHelper> push_helper
Zhongyi Shi
2016/11/22 00:09:30
Done.
| |
91 new 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 new 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 new 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 new 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 new 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 new 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 base on the full url. | |
jkarlin
2016/11/21 14:40:41
s/base/based/
Zhongyi Shi
2016/11/22 00:09:30
Done.
| |
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 new 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 | |
245 std::unique_ptr<MockServerPushHelper> push_helper2( | |
246 new MockServerPushHelper(request_url2)); | |
247 MockServerPushHelper* push_helper_ptr2 = push_helper2.get(); | |
248 | |
249 // Receive another server push and should not cancel the push. | |
jkarlin
2016/11/21 14:40:41
How is this testing the full URL? Even if you used
Zhongyi Shi
2016/11/22 00:09:29
Thanks for catching this, I added RunUntilIdle to
| |
250 EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(0); | |
251 push_delegate.OnPush(std::move(push_helper2)); | |
252 | |
253 base::RunLoop().RunUntilIdle(); | |
254 | |
255 // Make sure no new net layer transaction is created. | |
256 EXPECT_EQ(1, mock_cache.network_layer()->transaction_count()); | |
257 EXPECT_EQ(1, mock_cache.disk_cache()->open_count()); | |
258 EXPECT_EQ(1, mock_cache.disk_cache()->create_count()); | |
259 | |
260 RemoveMockTransaction(mock_trans.get()); | |
261 } | |
262 | |
263 } // namespace net | |
OLD | NEW |