summaryrefslogtreecommitdiff
path: root/chromium/net/http/http_cache_lookup_manager_unittest.cc
blob: d3d8268a81d3d25198eab50a9a0694714d61e3a5 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright (c) 2016 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 "base/run_loop.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/http/http_cache_lookup_manager.h"
#include "net/http/http_transaction_test_util.h"
#include "net/http/mock_http_cache.h"
#include "net/test/gtest_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using net::test::IsOk;

namespace net {

namespace {

class MockServerPushHelper : public ServerPushDelegate::ServerPushHelper {
 public:
  explicit MockServerPushHelper(const GURL& url) : request_url_(url) {}

  const GURL& GetURL() const override { return request_url_; }

  MOCK_METHOD0(Cancel, void());

 private:
  const GURL request_url_;
};

std::unique_ptr<MockTransaction> CreateMockTransaction(const GURL& url) {
  MockTransaction mock_trans = {
      url.spec().c_str(), "GET", base::Time(), "", LOAD_NORMAL,
      "HTTP/1.1 200 OK",
      "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
      "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n",
      base::Time(), "<html><body>Google Blah Blah</body></html>",
      TEST_MODE_NORMAL, nullptr, nullptr, nullptr, 0, 0, OK};
  return std::make_unique<MockTransaction>(mock_trans);
}

void PopulateCacheEntry(HttpCache* cache, const GURL& request_url) {
  TestCompletionCallback callback;

  std::unique_ptr<MockTransaction> mock_trans =
      CreateMockTransaction(request_url);
  AddMockTransaction(mock_trans.get());

  MockHttpRequest request(*(mock_trans.get()));

  std::unique_ptr<HttpTransaction> trans;
  int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &trans);
  EXPECT_THAT(rv, IsOk());
  ASSERT_TRUE(trans.get());

  rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  base::RunLoop().RunUntilIdle();

  if (rv == ERR_IO_PENDING)
    rv = callback.WaitForResult();

  ASSERT_EQ(mock_trans->start_return_code, rv);
  if (OK != rv)
    return;

  const HttpResponseInfo* response = trans->GetResponseInfo();
  ASSERT_TRUE(response);

  std::string content;
  rv = ReadTransaction(trans.get(), &content);

  EXPECT_THAT(rv, IsOk());
  std::string expected(mock_trans->data);
  EXPECT_EQ(expected, content);
  RemoveMockTransaction(mock_trans.get());
}

}  // namespace

TEST(HttpCacheLookupManagerTest, ServerPushMissCache) {
  MockHttpCache mock_cache;
  HttpCacheLookupManager push_delegate(mock_cache.http_cache());
  GURL request_url("http://www.example.com/pushed.jpg");

  std::unique_ptr<MockServerPushHelper> push_helper =
      std::make_unique<MockServerPushHelper>(request_url);
  MockServerPushHelper* push_helper_ptr = push_helper.get();

  // Receive a server push and should not cancel the push.
  EXPECT_CALL(*push_helper_ptr, Cancel()).Times(0);
  push_delegate.OnPush(std::move(push_helper), NetLogWithSource());
  base::RunLoop().RunUntilIdle();

  // Make sure no network transaction is created.
  EXPECT_EQ(0, mock_cache.network_layer()->transaction_count());
  EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
  EXPECT_EQ(0, mock_cache.disk_cache()->create_count());
}

TEST(HttpCacheLookupManagerTest, ServerPushDoNotCreateCacheEntry) {
  MockHttpCache mock_cache;
  HttpCacheLookupManager push_delegate(mock_cache.http_cache());
  GURL request_url("http://www.example.com/pushed.jpg");

  std::unique_ptr<MockServerPushHelper> push_helper =
      std::make_unique<MockServerPushHelper>(request_url);
  MockServerPushHelper* push_helper_ptr = push_helper.get();

  // Receive a server push and should not cancel the push.
  EXPECT_CALL(*push_helper_ptr, Cancel()).Times(0);
  push_delegate.OnPush(std::move(push_helper), NetLogWithSource());
  base::RunLoop().RunUntilIdle();

  // Receive another server push for the same url.
  std::unique_ptr<MockServerPushHelper> push_helper2 =
      std::make_unique<MockServerPushHelper>(request_url);
  MockServerPushHelper* push_helper_ptr2 = push_helper2.get();
  EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(0);
  push_delegate.OnPush(std::move(push_helper2), NetLogWithSource());
  base::RunLoop().RunUntilIdle();

  // Verify no network transaction is created.
  EXPECT_EQ(0, mock_cache.network_layer()->transaction_count());
  EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
  // Verify no cache entry is created for server push lookup.
  EXPECT_EQ(0, mock_cache.disk_cache()->create_count());
}

TEST(HttpCacheLookupManagerTest, ServerPushHitCache) {
  MockHttpCache mock_cache;
  HttpCacheLookupManager push_delegate(mock_cache.http_cache());
  GURL request_url("http://www.example.com/pushed.jpg");

  // Populate the cache entry so that the cache lookup for server push hits.
  PopulateCacheEntry(mock_cache.http_cache(), request_url);
  EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
  EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
  EXPECT_EQ(1, mock_cache.disk_cache()->create_count());

  // Add another mock transaction since the OnPush will create a new cache
  // transaction.
  std::unique_ptr<MockTransaction> mock_trans =
      CreateMockTransaction(request_url);
  AddMockTransaction(mock_trans.get());

  std::unique_ptr<MockServerPushHelper> push_helper =
      std::make_unique<MockServerPushHelper>(request_url);
  MockServerPushHelper* push_helper_ptr = push_helper.get();

  // Receive a server push and should cancel the push.
  EXPECT_CALL(*push_helper_ptr, Cancel()).Times(1);
  push_delegate.OnPush(std::move(push_helper), NetLogWithSource());
  base::RunLoop().RunUntilIdle();

  // Make sure no new net layer transaction is created.
  EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
  EXPECT_EQ(1, mock_cache.disk_cache()->open_count());
  EXPECT_EQ(1, mock_cache.disk_cache()->create_count());

  RemoveMockTransaction(mock_trans.get());
}

// Test when a server push is received while the HttpCacheLookupManager has a
// pending lookup transaction for the same URL, the new server push will not
// send a new lookup transaction and should not be canceled.
TEST(HttpCacheLookupManagerTest, ServerPushPendingLookup) {
  MockHttpCache mock_cache;
  HttpCacheLookupManager push_delegate(mock_cache.http_cache());
  GURL request_url("http://www.example.com/pushed.jpg");

  // Populate the cache entry so that the cache lookup for server push hits.
  PopulateCacheEntry(mock_cache.http_cache(), request_url);
  EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
  EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
  EXPECT_EQ(1, mock_cache.disk_cache()->create_count());

  // Add another mock transaction since the OnPush will create a new cache
  // transaction.
  std::unique_ptr<MockTransaction> mock_trans =
      CreateMockTransaction(request_url);
  AddMockTransaction(mock_trans.get());

  std::unique_ptr<MockServerPushHelper> push_helper =
      std::make_unique<MockServerPushHelper>(request_url);
  MockServerPushHelper* push_helper_ptr = push_helper.get();

  // Receive a server push and should cancel the push eventually.
  EXPECT_CALL(*push_helper_ptr, Cancel()).Times(1);
  push_delegate.OnPush(std::move(push_helper), NetLogWithSource());

  std::unique_ptr<MockServerPushHelper> push_helper2 =
      std::make_unique<MockServerPushHelper>(request_url);
  MockServerPushHelper* push_helper_ptr2 = push_helper2.get();

  // Receive another server push and should not cancel the push.
  EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(0);
  push_delegate.OnPush(std::move(push_helper2), NetLogWithSource());

  base::RunLoop().RunUntilIdle();

  // Make sure no new net layer transaction is created.
  EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
  EXPECT_EQ(1, mock_cache.disk_cache()->open_count());
  EXPECT_EQ(1, mock_cache.disk_cache()->create_count());

  RemoveMockTransaction(mock_trans.get());
}

// Test the server push lookup is based on the full url.
TEST(HttpCacheLookupManagerTest, ServerPushLookupOnUrl) {
  MockHttpCache mock_cache;
  HttpCacheLookupManager push_delegate(mock_cache.http_cache());
  GURL request_url("http://www.example.com/pushed.jpg?u=0");
  GURL request_url2("http://www.example.com/pushed.jpg?u=1");

  // Populate the cache entry so that the cache lookup for server push hits.
  PopulateCacheEntry(mock_cache.http_cache(), request_url);
  EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
  EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
  EXPECT_EQ(1, mock_cache.disk_cache()->create_count());

  // Add another mock transaction since the OnPush will create a new cache
  // transaction.
  std::unique_ptr<MockTransaction> mock_trans =
      CreateMockTransaction(request_url);
  AddMockTransaction(mock_trans.get());

  std::unique_ptr<MockServerPushHelper> push_helper =
      std::make_unique<MockServerPushHelper>(request_url);
  MockServerPushHelper* push_helper_ptr = push_helper.get();

  // Receive a server push and should cancel the push eventually.
  EXPECT_CALL(*push_helper_ptr, Cancel()).Times(1);
  push_delegate.OnPush(std::move(push_helper), NetLogWithSource());
  // Run until the lookup transaction finishes for the first server push.
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
  EXPECT_EQ(1, mock_cache.disk_cache()->open_count());
  EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
  RemoveMockTransaction(mock_trans.get());

  AddMockTransaction(mock_trans.get());
  // Receive the second server push with same url after the first lookup
  // finishes, and should cancel the push.
  std::unique_ptr<MockServerPushHelper> push_helper2 =
      std::make_unique<MockServerPushHelper>(request_url);
  MockServerPushHelper* push_helper_ptr2 = push_helper2.get();

  EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(1);
  push_delegate.OnPush(std::move(push_helper2), NetLogWithSource());
  // Run until the lookup transaction finishes for the second server push.
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
  EXPECT_EQ(2, mock_cache.disk_cache()->open_count());
  EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
  RemoveMockTransaction(mock_trans.get());

  std::unique_ptr<MockTransaction> mock_trans3 =
      CreateMockTransaction(request_url2);
  AddMockTransaction(mock_trans3.get());
  // Receive the third server push with a different url after lookup for a
  // similar server push has been completed, should not cancel the push.
  std::unique_ptr<MockServerPushHelper> push_helper3 =
      std::make_unique<MockServerPushHelper>(request_url2);
  MockServerPushHelper* push_helper_ptr3 = push_helper3.get();

  EXPECT_CALL(*push_helper_ptr3, Cancel()).Times(0);
  push_delegate.OnPush(std::move(push_helper3), NetLogWithSource());

  base::RunLoop().RunUntilIdle();
  // Make sure no new net layer transaction is created.
  EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
  EXPECT_EQ(2, mock_cache.disk_cache()->open_count());
  EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
  RemoveMockTransaction(mock_trans3.get());
}

}  // namespace net