blob: dc114dac8387de7073fe8adbbdaca31af082e20d [file] [log] [blame]
[email protected]6df35cc2010-02-10 00:53:061// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/http/http_cache.h"
6
[email protected]23144032008-09-08 20:51:307#include "base/hash_tables.h"
initial.commit586acc5fe2008-07-26 22:42:528#include "base/message_loop.h"
[email protected]d5b94c72009-10-26 16:51:109#include "base/scoped_vector.h"
initial.commit586acc5fe2008-07-26 22:42:5210#include "base/string_util.h"
[email protected]cfc076ec2009-11-07 02:27:2311#include "net/base/cache_type.h"
initial.commit586acc5fe2008-07-26 22:42:5212#include "net/base/net_errors.h"
13#include "net/base/load_flags.h"
[email protected]9e743cd2010-03-16 07:03:5314#include "net/base/net_log_unittest.h"
[email protected]207d58c72009-09-04 18:59:2915#include "net/base/ssl_cert_request_info.h"
initial.commit586acc5fe2008-07-26 22:42:5216#include "net/disk_cache/disk_cache.h"
[email protected]8bf26f49a2009-06-12 17:35:5017#include "net/http/http_byte_range.h"
[email protected]8c76ae22010-04-20 22:15:4318#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5219#include "net/http/http_request_info.h"
[email protected]95792eb12009-06-22 21:30:4020#include "net/http/http_response_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5221#include "net/http/http_response_info.h"
22#include "net/http/http_transaction.h"
23#include "net/http/http_transaction_unittest.h"
[email protected]8bf26f49a2009-06-12 17:35:5024#include "net/http/http_util.h"
initial.commit586acc5fe2008-07-26 22:42:5225#include "testing/gtest/include/gtest/gtest.h"
26
[email protected]e1acf6f2008-10-27 20:43:3327using base::Time;
28
initial.commit586acc5fe2008-07-26 22:42:5229namespace {
30
[email protected]7d7ad6e42010-01-14 01:30:5331int GetTestModeForEntry(const std::string& key) {
32 // 'key' is prefixed with an identifier if it corresponds to a cached POST.
33 // Skip past that to locate the actual URL.
34 //
35 // TODO(darin): It breaks the abstraction a bit that we assume 'key' is an
36 // URL corresponding to a registered MockTransaction. It would be good to
37 // have another way to access the test_mode.
38 GURL url;
39 if (isdigit(key[0])) {
40 size_t slash = key.find('/');
41 DCHECK(slash != std::string::npos);
42 url = GURL(key.substr(slash + 1));
43 } else {
44 url = GURL(key);
45 }
46 const MockTransaction* t = FindMockTransaction(url);
47 DCHECK(t);
48 return t->test_mode;
49}
50
[email protected]6df35cc2010-02-10 00:53:0651// We can override the test mode for a given operation by setting this global
52// variable. Just remember to reset it after the test!.
53int g_test_mode = 0;
54
55// Returns the test mode after considering the global override.
56int GetEffectiveTestMode(int test_mode) {
57 if (!g_test_mode)
58 return test_mode;
59
60 return g_test_mode;
61}
62
initial.commit586acc5fe2008-07-26 22:42:5263//-----------------------------------------------------------------------------
64// mock disk cache (a very basic memory cache implementation)
65
[email protected]91f4caa2010-04-19 16:50:5066static const int kNumCacheEntryDataIndices = 3;
67
initial.commit586acc5fe2008-07-26 22:42:5268class MockDiskEntry : public disk_cache::Entry,
69 public base::RefCounted<MockDiskEntry> {
70 public:
[email protected]e7f29642009-03-02 22:53:1871 MockDiskEntry()
[email protected]06e62ba2009-10-08 23:07:3972 : test_mode_(0), doomed_(false), sparse_(false), fail_requests_(false),
73 busy_(false), delayed_(false) {
initial.commit586acc5fe2008-07-26 22:42:5274 }
75
[email protected]6f40bf72009-07-23 17:52:3776 explicit MockDiskEntry(const std::string& key)
[email protected]06e62ba2009-10-08 23:07:3977 : key_(key), doomed_(false), sparse_(false), fail_requests_(false),
78 busy_(false), delayed_(false) {
[email protected]7d7ad6e42010-01-14 01:30:5379 test_mode_ = GetTestModeForEntry(key);
initial.commit586acc5fe2008-07-26 22:42:5280 }
81
initial.commit586acc5fe2008-07-26 22:42:5282 bool is_doomed() const { return doomed_; }
83
84 virtual void Doom() {
85 doomed_ = true;
86 }
87
88 virtual void Close() {
89 Release();
90 }
91
92 virtual std::string GetKey() const {
[email protected]37095fe2009-08-07 00:13:1293 if (fail_requests_)
94 return std::string();
initial.commit586acc5fe2008-07-26 22:42:5295 return key_;
96 }
97
98 virtual Time GetLastUsed() const {
99 return Time::FromInternalValue(0);
100 }
101
102 virtual Time GetLastModified() const {
103 return Time::FromInternalValue(0);
104 }
105
106 virtual int32 GetDataSize(int index) const {
[email protected]91f4caa2010-04-19 16:50:50107 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices);
initial.commit586acc5fe2008-07-26 22:42:52108 return static_cast<int32>(data_[index].size());
109 }
110
[email protected]74a85ce2009-02-12 00:03:19111 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len,
initial.commit586acc5fe2008-07-26 22:42:52112 net::CompletionCallback* callback) {
[email protected]91f4caa2010-04-19 16:50:50113 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices);
[email protected]02e7a012010-05-10 23:06:33114 DCHECK(callback);
initial.commit586acc5fe2008-07-26 22:42:52115
[email protected]37095fe2009-08-07 00:13:12116 if (fail_requests_)
117 return net::ERR_CACHE_READ_FAILURE;
118
initial.commit586acc5fe2008-07-26 22:42:52119 if (offset < 0 || offset > static_cast<int>(data_[index].size()))
120 return net::ERR_FAILED;
[email protected]cad155b2008-09-23 14:44:27121 if (static_cast<size_t>(offset) == data_[index].size())
initial.commit586acc5fe2008-07-26 22:42:52122 return 0;
123
124 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset);
[email protected]74a85ce2009-02-12 00:03:19125 memcpy(buf->data(), &data_[index][offset], num);
initial.commit586acc5fe2008-07-26 22:42:52126
[email protected]02e7a012010-05-10 23:06:33127 if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)
initial.commit586acc5fe2008-07-26 22:42:52128 return num;
129
130 CallbackLater(callback, num);
131 return net::ERR_IO_PENDING;
132 }
133
[email protected]74a85ce2009-02-12 00:03:19134 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len,
initial.commit586acc5fe2008-07-26 22:42:52135 net::CompletionCallback* callback, bool truncate) {
[email protected]91f4caa2010-04-19 16:50:50136 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices);
[email protected]02e7a012010-05-10 23:06:33137 DCHECK(callback);
initial.commit586acc5fe2008-07-26 22:42:52138 DCHECK(truncate);
139
[email protected]37095fe2009-08-07 00:13:12140 if (fail_requests_)
141 return net::ERR_CACHE_READ_FAILURE;
142
initial.commit586acc5fe2008-07-26 22:42:52143 if (offset < 0 || offset > static_cast<int>(data_[index].size()))
144 return net::ERR_FAILED;
145
146 data_[index].resize(offset + buf_len);
147 if (buf_len)
[email protected]74a85ce2009-02-12 00:03:19148 memcpy(&data_[index][offset], buf->data(), buf_len);
[email protected]73cae572009-10-22 18:36:19149
[email protected]02e7a012010-05-10 23:06:33150 if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE)
[email protected]73cae572009-10-22 18:36:19151 return buf_len;
152
153 CallbackLater(callback, buf_len);
154 return net::ERR_IO_PENDING;
initial.commit586acc5fe2008-07-26 22:42:52155 }
156
[email protected]a2068a612009-06-04 21:43:49157 virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len,
[email protected]02e7a012010-05-10 23:06:33158 net::CompletionCallback* callback) {
159 DCHECK(callback);
[email protected]06e62ba2009-10-08 23:07:39160 if (!sparse_ || busy_)
[email protected]a2068a612009-06-04 21:43:49161 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
162 if (offset < 0)
163 return net::ERR_FAILED;
164
[email protected]37095fe2009-08-07 00:13:12165 if (fail_requests_)
166 return net::ERR_CACHE_READ_FAILURE;
167
[email protected]a2068a612009-06-04 21:43:49168 DCHECK(offset < kint32max);
169 int real_offset = static_cast<int>(offset);
170 if (!buf_len)
171 return 0;
172
173 int num = std::min(static_cast<int>(data_[1].size()) - real_offset,
174 buf_len);
175 memcpy(buf->data(), &data_[1][real_offset], num);
176
[email protected]02e7a012010-05-10 23:06:33177 if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)
[email protected]a2068a612009-06-04 21:43:49178 return num;
179
[email protected]02e7a012010-05-10 23:06:33180 CallbackLater(callback, num);
[email protected]06e62ba2009-10-08 23:07:39181 busy_ = true;
182 delayed_ = false;
[email protected]a2068a612009-06-04 21:43:49183 return net::ERR_IO_PENDING;
184 }
185
186 virtual int WriteSparseData(int64 offset, net::IOBuffer* buf, int buf_len,
[email protected]02e7a012010-05-10 23:06:33187 net::CompletionCallback* callback) {
188 DCHECK(callback);
[email protected]06e62ba2009-10-08 23:07:39189 if (busy_)
190 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
[email protected]a2068a612009-06-04 21:43:49191 if (!sparse_) {
192 if (data_[1].size())
193 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
194 sparse_ = true;
195 }
196 if (offset < 0)
197 return net::ERR_FAILED;
198 if (!buf_len)
199 return 0;
200
[email protected]37095fe2009-08-07 00:13:12201 if (fail_requests_)
202 return net::ERR_CACHE_READ_FAILURE;
203
[email protected]a2068a612009-06-04 21:43:49204 DCHECK(offset < kint32max);
205 int real_offset = static_cast<int>(offset);
206
207 if (static_cast<int>(data_[1].size()) < real_offset + buf_len)
208 data_[1].resize(real_offset + buf_len);
209
210 memcpy(&data_[1][real_offset], buf->data(), buf_len);
[email protected]02e7a012010-05-10 23:06:33211 if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE)
[email protected]73cae572009-10-22 18:36:19212 return buf_len;
213
[email protected]02e7a012010-05-10 23:06:33214 CallbackLater(callback, buf_len);
[email protected]73cae572009-10-22 18:36:19215 return net::ERR_IO_PENDING;
[email protected]a2068a612009-06-04 21:43:49216 }
217
[email protected]034740a2010-06-11 17:16:48218 virtual int GetAvailableRange(int64 offset, int len, int64* start,
219 net::CompletionCallback* callback) {
220 DCHECK(callback);
[email protected]06e62ba2009-10-08 23:07:39221 if (!sparse_ || busy_)
[email protected]a2068a612009-06-04 21:43:49222 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
223 if (offset < 0)
224 return net::ERR_FAILED;
225
[email protected]37095fe2009-08-07 00:13:12226 if (fail_requests_)
227 return net::ERR_CACHE_READ_FAILURE;
228
[email protected]a2068a612009-06-04 21:43:49229 *start = offset;
230 DCHECK(offset < kint32max);
231 int real_offset = static_cast<int>(offset);
232 if (static_cast<int>(data_[1].size()) < real_offset)
233 return 0;
234
235 int num = std::min(static_cast<int>(data_[1].size()) - real_offset, len);
236 int count = 0;
237 for (; num > 0; num--, real_offset++) {
238 if (!count) {
239 if (data_[1][real_offset]) {
240 count++;
241 *start = real_offset;
242 }
243 } else {
244 if (!data_[1][real_offset])
245 break;
246 count++;
247 }
248 }
[email protected]034740a2010-06-11 17:16:48249 if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE)
250 return count;
251
252 CallbackLater(callback, count);
253 return net::ERR_IO_PENDING;
[email protected]a2068a612009-06-04 21:43:49254 }
255
[email protected]034740a2010-06-11 17:16:48256 virtual bool CouldBeSparse() const {
257 return sparse_;
[email protected]3cf35d9e2009-11-05 23:27:41258 }
259
[email protected]06e62ba2009-10-08 23:07:39260 virtual void CancelSparseIO() { cancel_ = true; }
261
262 virtual int ReadyForSparseIO(net::CompletionCallback* completion_callback) {
263 if (!cancel_)
264 return net::OK;
265
266 cancel_ = false;
267 DCHECK(completion_callback);
[email protected]6df35cc2010-02-10 00:53:06268 if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)
[email protected]06e62ba2009-10-08 23:07:39269 return net::OK;
270
271 // The pending operation is already in the message loop (and hopefuly
272 // already in the second pass). Just notify the caller that it finished.
273 CallbackLater(completion_callback, 0);
274 return net::ERR_IO_PENDING;
275 }
276
[email protected]37095fe2009-08-07 00:13:12277 // Fail most subsequent requests.
278 void set_fail_requests() { fail_requests_ = true; }
279
[email protected]24f46392009-11-19 18:45:23280 // If |value| is true, don't deliver any completion callbacks until called
281 // again with |value| set to false. Caution: remember to enable callbacks
282 // again or all subsequent tests will fail.
283 static void IgnoreCallbacks(bool value) {
284 if (ignore_callbacks_ == value)
285 return;
286 ignore_callbacks_ = value;
287 if (!value)
288 StoreAndDeliverCallbacks(false, NULL, NULL, 0);
289 }
290
initial.commit586acc5fe2008-07-26 22:42:52291 private:
[email protected]5389bc72009-11-05 23:34:24292 friend class base::RefCounted<MockDiskEntry>;
293
[email protected]24f46392009-11-19 18:45:23294 struct CallbackInfo {
295 scoped_refptr<MockDiskEntry> entry;
296 net::CompletionCallback* callback;
297 int result;
298 };
299
[email protected]5389bc72009-11-05 23:34:24300 ~MockDiskEntry() {}
301
initial.commit586acc5fe2008-07-26 22:42:52302 // Unlike the callbacks for MockHttpTransaction, we want this one to run even
303 // if the consumer called Close on the MockDiskEntry. We achieve that by
304 // leveraging the fact that this class is reference counted.
305 void CallbackLater(net::CompletionCallback* callback, int result) {
[email protected]24f46392009-11-19 18:45:23306 if (ignore_callbacks_)
307 return StoreAndDeliverCallbacks(true, this, callback, result);
[email protected]2227c692010-05-04 15:36:11308 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
309 this, &MockDiskEntry::RunCallback, callback, result));
initial.commit586acc5fe2008-07-26 22:42:52310 }
311 void RunCallback(net::CompletionCallback* callback, int result) {
[email protected]06e62ba2009-10-08 23:07:39312 if (busy_) {
313 // This is kind of hacky, but controlling the behavior of just this entry
314 // from a test is sort of complicated. What we really want to do is
315 // delay the delivery of a sparse IO operation a little more so that the
316 // request start operation (async) will finish without seeing the end of
317 // this operation (already posted to the message loop)... and without
318 // just delaying for n mS (which may cause trouble with slow bots). So
319 // we re-post this operation (all async sparse IO operations will take two
320 // trips trhough the message loop instead of one).
321 if (!delayed_) {
322 delayed_ = true;
323 return CallbackLater(callback, result);
324 }
325 }
326 busy_ = false;
initial.commit586acc5fe2008-07-26 22:42:52327 callback->Run(result);
328 }
329
[email protected]24f46392009-11-19 18:45:23330 // When |store| is true, stores the callback to be delivered later; otherwise
331 // delivers any callback previously stored.
332 static void StoreAndDeliverCallbacks(bool store, MockDiskEntry* entry,
333 net::CompletionCallback* callback,
334 int result) {
335 static std::vector<CallbackInfo> callback_list;
336 if (store) {
337 CallbackInfo c = {entry, callback, result};
338 callback_list.push_back(c);
339 } else {
340 for (size_t i = 0; i < callback_list.size(); i++) {
341 CallbackInfo& c = callback_list[i];
342 c.entry->CallbackLater(c.callback, c.result);
343 }
344 callback_list.clear();
345 }
346 }
347
initial.commit586acc5fe2008-07-26 22:42:52348 std::string key_;
[email protected]91f4caa2010-04-19 16:50:50349 std::vector<char> data_[kNumCacheEntryDataIndices];
initial.commit586acc5fe2008-07-26 22:42:52350 int test_mode_;
351 bool doomed_;
[email protected]a2068a612009-06-04 21:43:49352 bool sparse_;
[email protected]37095fe2009-08-07 00:13:12353 bool fail_requests_;
[email protected]06e62ba2009-10-08 23:07:39354 bool busy_;
355 bool delayed_;
356 static bool cancel_;
[email protected]24f46392009-11-19 18:45:23357 static bool ignore_callbacks_;
initial.commit586acc5fe2008-07-26 22:42:52358};
359
[email protected]24f46392009-11-19 18:45:23360// Statics.
[email protected]06e62ba2009-10-08 23:07:39361bool MockDiskEntry::cancel_ = false;
[email protected]24f46392009-11-19 18:45:23362bool MockDiskEntry::ignore_callbacks_ = false;
[email protected]06e62ba2009-10-08 23:07:39363
initial.commit586acc5fe2008-07-26 22:42:52364class MockDiskCache : public disk_cache::Backend {
365 public:
[email protected]37095fe2009-08-07 00:13:12366 MockDiskCache()
367 : open_count_(0), create_count_(0), fail_requests_(false),
368 soft_failures_(false) {
initial.commit586acc5fe2008-07-26 22:42:52369 }
370
371 ~MockDiskCache() {
[email protected]fb2622f2010-07-13 18:00:56372 ReleaseAll();
initial.commit586acc5fe2008-07-26 22:42:52373 }
374
375 virtual int32 GetEntryCount() const {
376 return static_cast<int32>(entries_.size());
377 }
378
[email protected]7d7ad6e42010-01-14 01:30:53379 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry,
380 net::CompletionCallback* callback) {
[email protected]02e7a012010-05-10 23:06:33381 DCHECK(callback);
initial.commit586acc5fe2008-07-26 22:42:52382 if (fail_requests_)
[email protected]7d7ad6e42010-01-14 01:30:53383 return net::ERR_CACHE_OPEN_FAILURE;
initial.commit586acc5fe2008-07-26 22:42:52384
385 EntryMap::iterator it = entries_.find(key);
386 if (it == entries_.end())
[email protected]7d7ad6e42010-01-14 01:30:53387 return net::ERR_CACHE_OPEN_FAILURE;
initial.commit586acc5fe2008-07-26 22:42:52388
389 if (it->second->is_doomed()) {
390 it->second->Release();
391 entries_.erase(it);
[email protected]7d7ad6e42010-01-14 01:30:53392 return net::ERR_CACHE_OPEN_FAILURE;
initial.commit586acc5fe2008-07-26 22:42:52393 }
394
395 open_count_++;
396
397 it->second->AddRef();
398 *entry = it->second;
399
[email protected]37095fe2009-08-07 00:13:12400 if (soft_failures_)
401 it->second->set_fail_requests();
402
[email protected]02e7a012010-05-10 23:06:33403 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
[email protected]7d7ad6e42010-01-14 01:30:53404 return net::OK;
initial.commit586acc5fe2008-07-26 22:42:52405
[email protected]7d7ad6e42010-01-14 01:30:53406 CallbackLater(callback, net::OK);
407 return net::ERR_IO_PENDING;
[email protected]3cf35d9e2009-11-05 23:27:41408 }
409
[email protected]7d7ad6e42010-01-14 01:30:53410 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry,
411 net::CompletionCallback* callback) {
[email protected]02e7a012010-05-10 23:06:33412 DCHECK(callback);
initial.commit586acc5fe2008-07-26 22:42:52413 if (fail_requests_)
[email protected]7d7ad6e42010-01-14 01:30:53414 return net::ERR_CACHE_CREATE_FAILURE;
initial.commit586acc5fe2008-07-26 22:42:52415
416 EntryMap::iterator it = entries_.find(key);
[email protected]7d7ad6e42010-01-14 01:30:53417 if (it != entries_.end()) {
418 DCHECK(it->second->is_doomed());
419 it->second->Release();
420 entries_.erase(it);
421 }
initial.commit586acc5fe2008-07-26 22:42:52422
423 create_count_++;
424
425 MockDiskEntry* new_entry = new MockDiskEntry(key);
426
427 new_entry->AddRef();
428 entries_[key] = new_entry;
429
430 new_entry->AddRef();
431 *entry = new_entry;
432
[email protected]37095fe2009-08-07 00:13:12433 if (soft_failures_)
434 new_entry->set_fail_requests();
435
[email protected]02e7a012010-05-10 23:06:33436 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
[email protected]7d7ad6e42010-01-14 01:30:53437 return net::OK;
initial.commit586acc5fe2008-07-26 22:42:52438
[email protected]7d7ad6e42010-01-14 01:30:53439 CallbackLater(callback, net::OK);
440 return net::ERR_IO_PENDING;
[email protected]3cf35d9e2009-11-05 23:27:41441 }
442
[email protected]7d7ad6e42010-01-14 01:30:53443 virtual int DoomEntry(const std::string& key,
444 net::CompletionCallback* callback) {
[email protected]02e7a012010-05-10 23:06:33445 DCHECK(callback);
initial.commit586acc5fe2008-07-26 22:42:52446 EntryMap::iterator it = entries_.find(key);
447 if (it != entries_.end()) {
448 it->second->Release();
449 entries_.erase(it);
450 }
[email protected]7d7ad6e42010-01-14 01:30:53451
452 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
453 return net::OK;
454
455 CallbackLater(callback, net::OK);
456 return net::ERR_IO_PENDING;
initial.commit586acc5fe2008-07-26 22:42:52457 }
458
[email protected]3cf35d9e2009-11-05 23:27:41459 virtual int DoomAllEntries(net::CompletionCallback* callback) {
460 return net::ERR_NOT_IMPLEMENTED;
461 }
462
[email protected]3cf35d9e2009-11-05 23:27:41463 virtual int DoomEntriesBetween(const base::Time initial_time,
464 const base::Time end_time,
465 net::CompletionCallback* callback) {
466 return net::ERR_NOT_IMPLEMENTED;
467 }
468
[email protected]3cf35d9e2009-11-05 23:27:41469 virtual int DoomEntriesSince(const base::Time initial_time,
470 net::CompletionCallback* callback) {
471 return net::ERR_NOT_IMPLEMENTED;
472 }
473
[email protected]3cf35d9e2009-11-05 23:27:41474 virtual int OpenNextEntry(void** iter, disk_cache::Entry** next_entry,
475 net::CompletionCallback* callback) {
476 return net::ERR_NOT_IMPLEMENTED;
477 }
478
initial.commit586acc5fe2008-07-26 22:42:52479 virtual void EndEnumeration(void** iter) {}
480
481 virtual void GetStats(
482 std::vector<std::pair<std::string, std::string> >* stats) {
483 }
484
485 // returns number of times a cache entry was successfully opened
486 int open_count() const { return open_count_; }
487
488 // returns number of times a cache entry was successfully created
489 int create_count() const { return create_count_; }
490
491 // Fail any subsequent CreateEntry and OpenEntry.
492 void set_fail_requests() { fail_requests_ = true; }
493
[email protected]37095fe2009-08-07 00:13:12494 // Return entries that fail some of their requests.
495 void set_soft_failures(bool value) { soft_failures_ = value; }
496
[email protected]fb2622f2010-07-13 18:00:56497 void ReleaseAll() {
498 EntryMap::iterator it = entries_.begin();
499 for (; it != entries_.end(); ++it)
500 it->second->Release();
501 entries_.clear();
502 }
503
initial.commit586acc5fe2008-07-26 22:42:52504 private:
[email protected]23144032008-09-08 20:51:30505 typedef base::hash_map<std::string, MockDiskEntry*> EntryMap;
[email protected]7d7ad6e42010-01-14 01:30:53506
507 class CallbackRunner : public Task {
508 public:
509 CallbackRunner(net::CompletionCallback* callback, int result)
510 : callback_(callback), result_(result) {}
511 virtual void Run() {
512 callback_->Run(result_);
513 }
514
515 private:
[email protected]2227c692010-05-04 15:36:11516 net::CompletionCallback* callback_;
[email protected]7d7ad6e42010-01-14 01:30:53517 int result_;
518 DISALLOW_COPY_AND_ASSIGN(CallbackRunner);
519 };
520
521 void CallbackLater(net::CompletionCallback* callback, int result) {
522 MessageLoop::current()->PostTask(FROM_HERE,
523 new CallbackRunner(callback, result));
524 }
525
initial.commit586acc5fe2008-07-26 22:42:52526 EntryMap entries_;
527 int open_count_;
528 int create_count_;
529 bool fail_requests_;
[email protected]37095fe2009-08-07 00:13:12530 bool soft_failures_;
initial.commit586acc5fe2008-07-26 22:42:52531};
532
[email protected]f8702522010-05-12 18:40:10533class MockBackendFactory : public net::HttpCache::BackendFactory {
534 public:
535 virtual int CreateBackend(disk_cache::Backend** backend,
536 net::CompletionCallback* callback) {
537 *backend = new MockDiskCache();
538 return net::OK;
539 }
540};
541
initial.commit586acc5fe2008-07-26 22:42:52542class MockHttpCache {
543 public:
[email protected]f8702522010-05-12 18:40:10544 MockHttpCache()
545 : http_cache_(new MockNetworkLayer(), new MockBackendFactory()) {
initial.commit586acc5fe2008-07-26 22:42:52546 }
547
[email protected]f8702522010-05-12 18:40:10548 explicit MockHttpCache(net::HttpCache::BackendFactory* disk_cache_factory)
549 : http_cache_(new MockNetworkLayer(), disk_cache_factory) {
[email protected]7eab0d2262009-10-14 22:05:54550 }
551
initial.commit586acc5fe2008-07-26 22:42:52552 net::HttpCache* http_cache() { return &http_cache_; }
553
554 MockNetworkLayer* network_layer() {
555 return static_cast<MockNetworkLayer*>(http_cache_.network_layer());
556 }
557 MockDiskCache* disk_cache() {
[email protected]6a989032010-06-14 19:05:33558 TestCompletionCallback cb;
559 disk_cache::Backend* backend;
560 int rv = http_cache_.GetBackend(&backend, &cb);
561 rv = cb.GetResult(rv);
562 return (rv == net::OK) ? static_cast<MockDiskCache*>(backend) : NULL;
initial.commit586acc5fe2008-07-26 22:42:52563 }
564
[email protected]02e7a012010-05-10 23:06:33565 // Helper function for reading response info from the disk cache.
566 static bool ReadResponseInfo(disk_cache::Entry* disk_entry,
567 net::HttpResponseInfo* response_info,
568 bool* response_truncated) {
569 int size = disk_entry->GetDataSize(0);
570
571 TestCompletionCallback cb;
572 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(size);
573 int rv = disk_entry->ReadData(0, 0, buffer, size, &cb);
574 rv = cb.GetResult(rv);
575 EXPECT_EQ(size, rv);
576
577 return net::HttpCache::ParseResponseInfo(buffer->data(), size,
578 response_info,
579 response_truncated);
580 }
581
582 // Helper function for writing response info into the disk cache.
583 static bool WriteResponseInfo(disk_cache::Entry* disk_entry,
584 const net::HttpResponseInfo* response_info,
585 bool skip_transient_headers,
586 bool response_truncated) {
587 Pickle pickle;
588 response_info->Persist(
589 &pickle, skip_transient_headers, response_truncated);
590
591 TestCompletionCallback cb;
592 scoped_refptr<net::WrappedIOBuffer> data = new net::WrappedIOBuffer(
593 reinterpret_cast<const char*>(pickle.data()));
594 int len = static_cast<int>(pickle.size());
595
596 int rv = disk_entry->WriteData(0, 0, data, len, &cb, true);
597 rv = cb.GetResult(rv);
598 return (rv == len);
599 }
600
601 // Helper function to synchronously open a backend entry.
602 bool OpenBackendEntry(const std::string& key, disk_cache::Entry** entry) {
603 TestCompletionCallback cb;
604 int rv = disk_cache()->OpenEntry(key, entry, &cb);
605 return (cb.GetResult(rv) == net::OK);
606 }
607
608 // Helper function to synchronously create a backend entry.
609 bool CreateBackendEntry(const std::string& key, disk_cache::Entry** entry) {
610 TestCompletionCallback cb;
611 int rv = disk_cache()->CreateEntry(key, entry, &cb);
612 return (cb.GetResult(rv) == net::OK);
613 }
614
initial.commit586acc5fe2008-07-26 22:42:52615 private:
616 net::HttpCache http_cache_;
617};
618
[email protected]46773162010-05-07 22:31:20619// This version of the disk cache doesn't invoke CreateEntry callbacks.
620class MockDiskCacheNoCB : public MockDiskCache {
621 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry,
622 net::CompletionCallback* callback) {
623 return net::ERR_IO_PENDING;
624 }
625};
initial.commit586acc5fe2008-07-26 22:42:52626
[email protected]f8702522010-05-12 18:40:10627class MockBackendNoCbFactory : public net::HttpCache::BackendFactory {
628 public:
629 virtual int CreateBackend(disk_cache::Backend** backend,
630 net::CompletionCallback* callback) {
631 *backend = new MockDiskCacheNoCB();
632 return net::OK;
633 }
634};
635
636// This backend factory allows us to control the backend instantiation.
637class MockBlockingBackendFactory : public net::HttpCache::BackendFactory {
638 public:
639 MockBlockingBackendFactory()
640 : backend_(NULL), callback_(NULL), block_(true), fail_(false) {}
641
642 virtual int CreateBackend(disk_cache::Backend** backend,
643 net::CompletionCallback* callback) {
644 if (!block_) {
645 if (!fail_)
646 *backend = new MockDiskCache();
647 return Result();
648 }
649
650 backend_ = backend;
651 callback_ = callback;
652 return net::ERR_IO_PENDING;
653 }
654
655 // Completes the backend creation. Any blocked call will be notified via the
656 // provided callback.
657 void FinishCreation() {
658 block_ = false;
659 if (callback_) {
660 if (!fail_)
661 *backend_ = new MockDiskCache();
662 net::CompletionCallback* cb = callback_;
663 callback_ = NULL;
664 cb->Run(Result()); // This object can be deleted here.
665 }
666 }
667
668 void set_fail(bool fail) { fail_ = fail; }
669
670 private:
671 int Result() { return fail_ ? net::ERR_FAILED : net::OK; }
672
673 disk_cache::Backend** backend_;
674 net::CompletionCallback* callback_;
675 bool block_;
676 bool fail_;
677};
678
initial.commit586acc5fe2008-07-26 22:42:52679//-----------------------------------------------------------------------------
680// helpers
681
682void ReadAndVerifyTransaction(net::HttpTransaction* trans,
683 const MockTransaction& trans_info) {
684 std::string content;
685 int rv = ReadTransaction(trans, &content);
686
687 EXPECT_EQ(net::OK, rv);
[email protected]bded84c2009-07-23 00:36:06688 std::string expected(trans_info.data);
689 EXPECT_EQ(expected, content);
initial.commit586acc5fe2008-07-26 22:42:52690}
691
[email protected]baff44a2009-09-06 00:48:10692void RunTransactionTestWithRequestAndLog(net::HttpCache* cache,
693 const MockTransaction& trans_info,
694 const MockHttpRequest& request,
695 net::HttpResponseInfo* response_info,
[email protected]9e743cd2010-03-16 07:03:53696 const net::BoundNetLog& net_log) {
initial.commit586acc5fe2008-07-26 22:42:52697 TestCompletionCallback callback;
698
699 // write to the cache
700
[email protected]1638d602009-09-24 03:49:17701 scoped_ptr<net::HttpTransaction> trans;
702 int rv = cache->CreateTransaction(&trans);
703 EXPECT_EQ(net::OK, rv);
[email protected]af4876d2008-10-21 23:10:57704 ASSERT_TRUE(trans.get());
initial.commit586acc5fe2008-07-26 22:42:52705
[email protected]9e743cd2010-03-16 07:03:53706 rv = trans->Start(&request, &callback, net_log);
initial.commit586acc5fe2008-07-26 22:42:52707 if (rv == net::ERR_IO_PENDING)
708 rv = callback.WaitForResult();
709 ASSERT_EQ(net::OK, rv);
710
711 const net::HttpResponseInfo* response = trans->GetResponseInfo();
712 ASSERT_TRUE(response);
713
[email protected]207d58c72009-09-04 18:59:29714 if (response_info)
715 *response_info = *response;
[email protected]95792eb12009-06-22 21:30:40716
[email protected]af4876d2008-10-21 23:10:57717 ReadAndVerifyTransaction(trans.get(), trans_info);
initial.commit586acc5fe2008-07-26 22:42:52718}
719
[email protected]baff44a2009-09-06 00:48:10720void RunTransactionTestWithRequest(net::HttpCache* cache,
721 const MockTransaction& trans_info,
722 const MockHttpRequest& request,
723 net::HttpResponseInfo* response_info) {
724 RunTransactionTestWithRequestAndLog(cache, trans_info, request,
[email protected]5a1d7ca2010-04-28 20:12:27725 response_info, net::BoundNetLog());
[email protected]baff44a2009-09-06 00:48:10726}
727
728void RunTransactionTestWithLog(net::HttpCache* cache,
729 const MockTransaction& trans_info,
[email protected]9e743cd2010-03-16 07:03:53730 const net::BoundNetLog& log) {
[email protected]baff44a2009-09-06 00:48:10731 RunTransactionTestWithRequestAndLog(
732 cache, trans_info, MockHttpRequest(trans_info), NULL, log);
733}
734
[email protected]96bac982009-03-24 18:20:06735void RunTransactionTest(net::HttpCache* cache,
736 const MockTransaction& trans_info) {
[email protected]5a1d7ca2010-04-28 20:12:27737 RunTransactionTestWithLog(cache, trans_info, net::BoundNetLog());
[email protected]95792eb12009-06-22 21:30:40738}
739
[email protected]207d58c72009-09-04 18:59:29740void RunTransactionTestWithResponseInfo(net::HttpCache* cache,
741 const MockTransaction& trans_info,
742 net::HttpResponseInfo* response) {
743 RunTransactionTestWithRequest(
744 cache, trans_info, MockHttpRequest(trans_info), response);
745}
746
[email protected]95792eb12009-06-22 21:30:40747void RunTransactionTestWithResponse(net::HttpCache* cache,
748 const MockTransaction& trans_info,
749 std::string* response_headers) {
[email protected]207d58c72009-09-04 18:59:29750 net::HttpResponseInfo response;
751 RunTransactionTestWithResponseInfo(cache, trans_info, &response);
752 response.headers->GetNormalizedHeaders(response_headers);
[email protected]96bac982009-03-24 18:20:06753}
754
[email protected]b367d9a52009-02-27 01:02:51755// This class provides a handler for kFastNoStoreGET_Transaction so that the
756// no-store header can be included on demand.
757class FastTransactionServer {
758 public:
759 FastTransactionServer() {
760 no_store = false;
761 }
762 ~FastTransactionServer() {}
763
764 void set_no_store(bool value) { no_store = value; }
765
766 static void FastNoStoreHandler(const net::HttpRequestInfo* request,
767 std::string* response_status,
768 std::string* response_headers,
769 std::string* response_data) {
770 if (no_store)
771 *response_headers = "Cache-Control: no-store\n";
772 }
773
774 private:
775 static bool no_store;
776 DISALLOW_COPY_AND_ASSIGN(FastTransactionServer);
777};
778bool FastTransactionServer::no_store;
779
780const MockTransaction kFastNoStoreGET_Transaction = {
781 "http://www.google.com/nostore",
782 "GET",
[email protected]ca2f19e2009-09-04 22:53:16783 base::Time(),
[email protected]b367d9a52009-02-27 01:02:51784 "",
785 net::LOAD_VALIDATE_CACHE,
786 "HTTP/1.1 200 OK",
787 "Cache-Control: max-age=10000\n",
[email protected]207d58c72009-09-04 18:59:29788 base::Time(),
[email protected]b367d9a52009-02-27 01:02:51789 "<html><body>Google Blah Blah</body></html>",
790 TEST_MODE_SYNC_NET_START,
791 &FastTransactionServer::FastNoStoreHandler,
792 0
793};
794
[email protected]8bf26f49a2009-06-12 17:35:50795// This class provides a handler for kRangeGET_TransactionOK so that the range
796// request can be served on demand.
797class RangeTransactionServer {
798 public:
799 RangeTransactionServer() {
[email protected]e5dad132009-08-18 00:53:41800 not_modified_ = false;
[email protected]a79837892009-08-20 21:18:29801 modified_ = false;
[email protected]fa59e6a2009-12-02 18:07:46802 bad_200_ = false;
[email protected]8bf26f49a2009-06-12 17:35:50803 }
[email protected]e5dad132009-08-18 00:53:41804 ~RangeTransactionServer() {
805 not_modified_ = false;
[email protected]a79837892009-08-20 21:18:29806 modified_ = false;
[email protected]fa59e6a2009-12-02 18:07:46807 bad_200_ = false;
[email protected]e5dad132009-08-18 00:53:41808 }
[email protected]8bf26f49a2009-06-12 17:35:50809
[email protected]a79837892009-08-20 21:18:29810 // Returns only 416 or 304 when set.
[email protected]e5dad132009-08-18 00:53:41811 void set_not_modified(bool value) { not_modified_ = value; }
[email protected]8bf26f49a2009-06-12 17:35:50812
[email protected]a79837892009-08-20 21:18:29813 // Returns 206 when revalidating a range (instead of 304).
814 void set_modified(bool value) { modified_ = value; }
815
[email protected]fa59e6a2009-12-02 18:07:46816 // Returns 200 instead of 206 (a malformed response overall).
817 void set_bad_200(bool value) { bad_200_ = value; }
818
[email protected]8bf26f49a2009-06-12 17:35:50819 static void RangeHandler(const net::HttpRequestInfo* request,
820 std::string* response_status,
821 std::string* response_headers,
822 std::string* response_data);
823
824 private:
[email protected]e5dad132009-08-18 00:53:41825 static bool not_modified_;
[email protected]a79837892009-08-20 21:18:29826 static bool modified_;
[email protected]fa59e6a2009-12-02 18:07:46827 static bool bad_200_;
[email protected]8bf26f49a2009-06-12 17:35:50828 DISALLOW_COPY_AND_ASSIGN(RangeTransactionServer);
829};
[email protected]e5dad132009-08-18 00:53:41830bool RangeTransactionServer::not_modified_ = false;
[email protected]a79837892009-08-20 21:18:29831bool RangeTransactionServer::modified_ = false;
[email protected]fa59e6a2009-12-02 18:07:46832bool RangeTransactionServer::bad_200_ = false;
[email protected]8bf26f49a2009-06-12 17:35:50833
[email protected]e75e8af2009-11-03 00:04:20834// A dummy extra header that must be preserved on a given request.
[email protected]8c76ae22010-04-20 22:15:43835#define EXTRA_HEADER "Extra: header"
836static const char kExtraHeaderKey[] = "Extra";
[email protected]e75e8af2009-11-03 00:04:20837
[email protected]8bf26f49a2009-06-12 17:35:50838// Static.
839void RangeTransactionServer::RangeHandler(const net::HttpRequestInfo* request,
840 std::string* response_status,
841 std::string* response_headers,
842 std::string* response_data) {
[email protected]8c76ae22010-04-20 22:15:43843 if (request->extra_headers.IsEmpty()) {
[email protected]44f873a62009-08-12 00:14:48844 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable");
[email protected]8bf26f49a2009-06-12 17:35:50845 return;
[email protected]44f873a62009-08-12 00:14:48846 }
[email protected]8bf26f49a2009-06-12 17:35:50847
[email protected]e75e8af2009-11-03 00:04:20848 // We want to make sure we don't delete extra headers.
[email protected]8c76ae22010-04-20 22:15:43849 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey));
[email protected]e75e8af2009-11-03 00:04:20850
[email protected]e5dad132009-08-18 00:53:41851 if (not_modified_) {
852 response_status->assign("HTTP/1.1 304 Not Modified");
853 return;
854 }
855
[email protected]8bf26f49a2009-06-12 17:35:50856 std::vector<net::HttpByteRange> ranges;
[email protected]8c76ae22010-04-20 22:15:43857 std::string range_header;
858 if (!request->extra_headers.GetHeader(
[email protected]2227c692010-05-04 15:36:11859 net::HttpRequestHeaders::kRange, &range_header) ||
[email protected]8c76ae22010-04-20 22:15:43860 !net::HttpUtil::ParseRangeHeader(range_header, &ranges) ||
[email protected]8bf26f49a2009-06-12 17:35:50861 ranges.size() != 1)
862 return;
863 // We can handle this range request.
864 net::HttpByteRange byte_range = ranges[0];
[email protected]e5dad132009-08-18 00:53:41865 if (byte_range.first_byte_position() > 79) {
866 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable");
867 return;
868 }
869
[email protected]8bf26f49a2009-06-12 17:35:50870 EXPECT_TRUE(byte_range.ComputeBounds(80));
871 int start = static_cast<int>(byte_range.first_byte_position());
872 int end = static_cast<int>(byte_range.last_byte_position());
873
874 EXPECT_LT(end, 80);
875
[email protected]8bf26f49a2009-06-12 17:35:50876 std::string content_range = StringPrintf("Content-Range: bytes %d-%d/80\n",
877 start, end);
878 response_headers->append(content_range);
879
[email protected]8c76ae22010-04-20 22:15:43880 if (!request->extra_headers.HasHeader("If-None-Match") || modified_) {
[email protected]44f873a62009-08-12 00:14:48881 EXPECT_EQ(9, (end - start) % 10);
882 std::string data;
883 for (int block_start = start; block_start < end; block_start += 10)
884 StringAppendF(&data, "rg: %02d-%02d ", block_start, block_start + 9);
[email protected]8bf26f49a2009-06-12 17:35:50885 *response_data = data;
[email protected]44f873a62009-08-12 00:14:48886
887 if (end - start != 9) {
888 // We also have to fix content-length.
889 int len = end - start + 1;
890 EXPECT_EQ(0, len % 10);
891 std::string content_length = StringPrintf("Content-Length: %d\n", len);
892 response_headers->replace(response_headers->find("Content-Length:"),
893 content_length.size(), content_length);
894 }
[email protected]fa59e6a2009-12-02 18:07:46895 if (bad_200_) {
896 // We return a range, but with a response code of 200.
897 response_status->assign("HTTP/1.1 200 Success");
898 }
[email protected]8bf26f49a2009-06-12 17:35:50899 } else {
900 response_status->assign("HTTP/1.1 304 Not Modified");
901 response_data->clear();
902 }
903}
904
905const MockTransaction kRangeGET_TransactionOK = {
906 "http://www.google.com/range",
907 "GET",
[email protected]ca2f19e2009-09-04 22:53:16908 base::Time(),
[email protected]e75e8af2009-11-03 00:04:20909 "Range: bytes = 40-49\r\n"
910 EXTRA_HEADER,
[email protected]8bf26f49a2009-06-12 17:35:50911 net::LOAD_NORMAL,
912 "HTTP/1.1 206 Partial Content",
913 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
914 "ETag: \"foo\"\n"
915 "Accept-Ranges: bytes\n"
916 "Content-Length: 10\n",
[email protected]207d58c72009-09-04 18:59:29917 base::Time(),
[email protected]8bf26f49a2009-06-12 17:35:50918 "rg: 40-49 ",
919 TEST_MODE_NORMAL,
920 &RangeTransactionServer::RangeHandler,
921 0
922};
923
[email protected]8c76ae22010-04-20 22:15:43924// Verifies the response headers (|response|) match a partial content
[email protected]95792eb12009-06-22 21:30:40925// response for the range starting at |start| and ending at |end|.
[email protected]8c76ae22010-04-20 22:15:43926void Verify206Response(std::string response, int start, int end) {
[email protected]95792eb12009-06-22 21:30:40927 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(),
928 response.size()));
929 scoped_refptr<net::HttpResponseHeaders> headers =
930 new net::HttpResponseHeaders(raw_headers);
931
[email protected]8c76ae22010-04-20 22:15:43932 ASSERT_EQ(206, headers->response_code());
[email protected]95792eb12009-06-22 21:30:40933
934 int64 range_start, range_end, object_size;
[email protected]8c76ae22010-04-20 22:15:43935 ASSERT_TRUE(
936 headers->GetContentRange(&range_start, &range_end, &object_size));
[email protected]95792eb12009-06-22 21:30:40937 int64 content_length = headers->GetContentLength();
938
939 int length = end - start + 1;
[email protected]8c76ae22010-04-20 22:15:43940 ASSERT_EQ(length, content_length);
941 ASSERT_EQ(start, range_start);
942 ASSERT_EQ(end, range_end);
[email protected]95792eb12009-06-22 21:30:40943}
944
[email protected]bded84c2009-07-23 00:36:06945// Helper to represent a network HTTP response.
946struct Response {
947 // Set this response into |trans|.
948 void AssignTo(MockTransaction* trans) const {
949 trans->status = status;
950 trans->response_headers = headers;
951 trans->data = body;
952 }
953
954 std::string status_and_headers() const {
955 return std::string(status) + "\n" + std::string(headers);
956 }
957
958 const char* status;
959 const char* headers;
960 const char* body;
961};
962
[email protected]73cae572009-10-22 18:36:19963struct Context {
964 Context() : result(net::ERR_IO_PENDING) {}
965
966 int result;
967 TestCompletionCallback callback;
968 scoped_ptr<net::HttpTransaction> trans;
969};
970
initial.commit586acc5fe2008-07-26 22:42:52971} // namespace
972
973
974//-----------------------------------------------------------------------------
975// tests
976
initial.commit586acc5fe2008-07-26 22:42:52977TEST(HttpCache, CreateThenDestroy) {
978 MockHttpCache cache;
979
[email protected]1638d602009-09-24 03:49:17980 scoped_ptr<net::HttpTransaction> trans;
981 int rv = cache.http_cache()->CreateTransaction(&trans);
982 EXPECT_EQ(net::OK, rv);
[email protected]af4876d2008-10-21 23:10:57983 ASSERT_TRUE(trans.get());
initial.commit586acc5fe2008-07-26 22:42:52984}
985
[email protected]cfc076ec2009-11-07 02:27:23986TEST(HttpCache, GetBackend) {
[email protected]f8702522010-05-12 18:40:10987 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(0));
[email protected]cfc076ec2009-11-07 02:27:23988
[email protected]6a989032010-06-14 19:05:33989 disk_cache::Backend* backend;
990 TestCompletionCallback cb;
[email protected]cfc076ec2009-11-07 02:27:23991 // This will lazily initialize the backend.
[email protected]6a989032010-06-14 19:05:33992 int rv = cache.http_cache()->GetBackend(&backend, &cb);
993 EXPECT_EQ(net::OK, cb.GetResult(rv));
[email protected]cfc076ec2009-11-07 02:27:23994}
995
initial.commit586acc5fe2008-07-26 22:42:52996TEST(HttpCache, SimpleGET) {
997 MockHttpCache cache;
998
999 // write to the cache
1000 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1001
1002 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1003 EXPECT_EQ(0, cache.disk_cache()->open_count());
1004 EXPECT_EQ(1, cache.disk_cache()->create_count());
1005}
1006
1007TEST(HttpCache, SimpleGETNoDiskCache) {
1008 MockHttpCache cache;
1009
1010 cache.disk_cache()->set_fail_requests();
1011
[email protected]9e743cd2010-03-16 07:03:531012 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded);
[email protected]baff44a2009-09-06 00:48:101013
initial.commit586acc5fe2008-07-26 22:42:521014 // Read from the network, and don't use the cache.
[email protected]9e743cd2010-03-16 07:03:531015 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction,
1016 log.bound());
[email protected]baff44a2009-09-06 00:48:101017
[email protected]9e743cd2010-03-16 07:03:531018 // Check that the NetLog was filled as expected.
[email protected]baff44a2009-09-06 00:48:101019 // (We attempted to both Open and Create entries, but both failed).
[email protected]46773162010-05-07 22:31:201020 EXPECT_EQ(6u, log.entries().size());
[email protected]e9002a92010-01-29 07:10:461021 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]46773162010-05-07 22:31:201022 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING));
[email protected]e9002a92010-01-29 07:10:461023 EXPECT_TRUE(net::LogContainsEndEvent(
[email protected]46773162010-05-07 22:31:201024 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING));
[email protected]e9002a92010-01-29 07:10:461025 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]46773162010-05-07 22:31:201026 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY));
[email protected]e9002a92010-01-29 07:10:461027 EXPECT_TRUE(net::LogContainsEndEvent(
[email protected]46773162010-05-07 22:31:201028 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY));
1029 EXPECT_TRUE(net::LogContainsBeginEvent(
1030 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY));
1031 EXPECT_TRUE(net::LogContainsEndEvent(
1032 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY));
initial.commit586acc5fe2008-07-26 22:42:521033
1034 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1035 EXPECT_EQ(0, cache.disk_cache()->open_count());
1036 EXPECT_EQ(0, cache.disk_cache()->create_count());
1037}
1038
[email protected]46773162010-05-07 22:31:201039TEST(HttpCache, SimpleGETNoDiskCache2) {
1040 // This will initialize a cache object with NULL backend.
[email protected]f8702522010-05-12 18:40:101041 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
1042 factory->set_fail(true);
1043 factory->FinishCreation(); // We'll complete synchronously.
1044 MockHttpCache cache(factory);
[email protected]46773162010-05-07 22:31:201045
1046 // Read from the network, and don't use the cache.
1047 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1048
1049 EXPECT_EQ(1, cache.network_layer()->transaction_count());
[email protected]6a989032010-06-14 19:05:331050 EXPECT_FALSE(cache.http_cache()->GetCurrentBackend());
[email protected]46773162010-05-07 22:31:201051}
1052
[email protected]37095fe2009-08-07 00:13:121053TEST(HttpCache, SimpleGETWithDiskFailures) {
1054 MockHttpCache cache;
1055
1056 cache.disk_cache()->set_soft_failures(true);
1057
1058 // Read from the network, and fail to write to the cache.
1059 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1060
1061 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1062 EXPECT_EQ(0, cache.disk_cache()->open_count());
1063 EXPECT_EQ(1, cache.disk_cache()->create_count());
1064
1065 // This one should see an empty cache again.
1066 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1067
1068 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1069 EXPECT_EQ(0, cache.disk_cache()->open_count());
1070 EXPECT_EQ(2, cache.disk_cache()->create_count());
1071}
1072
[email protected]73cae572009-10-22 18:36:191073// Tests that disk failures after the transaction has started don't cause the
1074// request to fail.
1075TEST(HttpCache, SimpleGETWithDiskFailures2) {
1076 MockHttpCache cache;
1077
1078 MockHttpRequest request(kSimpleGET_Transaction);
1079
1080 scoped_ptr<Context> c(new Context());
1081 int rv = cache.http_cache()->CreateTransaction(&c->trans);
1082 EXPECT_EQ(net::OK, rv);
1083
[email protected]5a1d7ca2010-04-28 20:12:271084 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog());
[email protected]73cae572009-10-22 18:36:191085 EXPECT_EQ(net::ERR_IO_PENDING, rv);
1086 rv = c->callback.WaitForResult();
1087
1088 // Start failing request now.
1089 cache.disk_cache()->set_soft_failures(true);
1090
1091 // We have to open the entry again to propagate the failure flag.
1092 disk_cache::Entry* en;
[email protected]02e7a012010-05-10 23:06:331093 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &en));
[email protected]73cae572009-10-22 18:36:191094 en->Close();
1095
1096 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1097 c.reset();
1098
1099 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1100 EXPECT_EQ(1, cache.disk_cache()->open_count());
1101 EXPECT_EQ(1, cache.disk_cache()->create_count());
1102
1103 // This one should see an empty cache again.
1104 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1105
1106 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1107 EXPECT_EQ(1, cache.disk_cache()->open_count());
1108 EXPECT_EQ(2, cache.disk_cache()->create_count());
1109}
1110
initial.commit586acc5fe2008-07-26 22:42:521111TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) {
1112 MockHttpCache cache;
1113
[email protected]9e743cd2010-03-16 07:03:531114 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded);
[email protected]baff44a2009-09-06 00:48:101115
initial.commit586acc5fe2008-07-26 22:42:521116 // write to the cache
[email protected]9e743cd2010-03-16 07:03:531117 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction,
1118 log.bound());
[email protected]baff44a2009-09-06 00:48:101119
[email protected]9e743cd2010-03-16 07:03:531120 // Check that the NetLog was filled as expected.
[email protected]46773162010-05-07 22:31:201121 EXPECT_EQ(8u, log.entries().size());
[email protected]e9002a92010-01-29 07:10:461122 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]46773162010-05-07 22:31:201123 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING));
[email protected]e9002a92010-01-29 07:10:461124 EXPECT_TRUE(net::LogContainsEndEvent(
[email protected]46773162010-05-07 22:31:201125 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING));
[email protected]e9002a92010-01-29 07:10:461126 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]46773162010-05-07 22:31:201127 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY));
[email protected]e9002a92010-01-29 07:10:461128 EXPECT_TRUE(net::LogContainsEndEvent(
[email protected]46773162010-05-07 22:31:201129 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY));
[email protected]e9002a92010-01-29 07:10:461130 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]46773162010-05-07 22:31:201131 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY));
[email protected]e9002a92010-01-29 07:10:461132 EXPECT_TRUE(net::LogContainsEndEvent(
[email protected]46773162010-05-07 22:31:201133 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY));
1134 EXPECT_TRUE(net::LogContainsBeginEvent(
1135 log.entries(), 6, net::NetLog::TYPE_HTTP_CACHE_WAITING));
1136 EXPECT_TRUE(net::LogContainsEndEvent(
1137 log.entries(), 7, net::NetLog::TYPE_HTTP_CACHE_WAITING));
initial.commit586acc5fe2008-07-26 22:42:521138
1139 // force this transaction to read from the cache
1140 MockTransaction transaction(kSimpleGET_Transaction);
1141 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
1142
[email protected]9e743cd2010-03-16 07:03:531143 log.Clear();
[email protected]baff44a2009-09-06 00:48:101144
[email protected]9e743cd2010-03-16 07:03:531145 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound());
[email protected]baff44a2009-09-06 00:48:101146
[email protected]9e743cd2010-03-16 07:03:531147 // Check that the NetLog was filled as expected.
[email protected]46773162010-05-07 22:31:201148 EXPECT_EQ(8u, log.entries().size());
[email protected]e9002a92010-01-29 07:10:461149 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]46773162010-05-07 22:31:201150 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING));
[email protected]e9002a92010-01-29 07:10:461151 EXPECT_TRUE(net::LogContainsEndEvent(
[email protected]46773162010-05-07 22:31:201152 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING));
[email protected]e9002a92010-01-29 07:10:461153 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]46773162010-05-07 22:31:201154 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY));
[email protected]e9002a92010-01-29 07:10:461155 EXPECT_TRUE(net::LogContainsEndEvent(
[email protected]46773162010-05-07 22:31:201156 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY));
[email protected]e9002a92010-01-29 07:10:461157 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]46773162010-05-07 22:31:201158 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_WAITING));
[email protected]e9002a92010-01-29 07:10:461159 EXPECT_TRUE(net::LogContainsEndEvent(
[email protected]46773162010-05-07 22:31:201160 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_WAITING));
1161 EXPECT_TRUE(net::LogContainsBeginEvent(
1162 log.entries(), 6, net::NetLog::TYPE_HTTP_CACHE_READ_INFO));
1163 EXPECT_TRUE(net::LogContainsEndEvent(
1164 log.entries(), 7, net::NetLog::TYPE_HTTP_CACHE_READ_INFO));
initial.commit586acc5fe2008-07-26 22:42:521165
1166 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1167 EXPECT_EQ(1, cache.disk_cache()->open_count());
1168 EXPECT_EQ(1, cache.disk_cache()->create_count());
1169}
1170
1171TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) {
1172 MockHttpCache cache;
1173
1174 // force this transaction to read from the cache
1175 MockTransaction transaction(kSimpleGET_Transaction);
1176 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
1177
1178 MockHttpRequest request(transaction);
1179 TestCompletionCallback callback;
1180
[email protected]1638d602009-09-24 03:49:171181 scoped_ptr<net::HttpTransaction> trans;
1182 int rv = cache.http_cache()->CreateTransaction(&trans);
1183 EXPECT_EQ(net::OK, rv);
[email protected]af4876d2008-10-21 23:10:571184 ASSERT_TRUE(trans.get());
initial.commit586acc5fe2008-07-26 22:42:521185
[email protected]5a1d7ca2010-04-28 20:12:271186 rv = trans->Start(&request, &callback, net::BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:521187 if (rv == net::ERR_IO_PENDING)
1188 rv = callback.WaitForResult();
1189 ASSERT_EQ(net::ERR_CACHE_MISS, rv);
1190
[email protected]af4876d2008-10-21 23:10:571191 trans.reset();
initial.commit586acc5fe2008-07-26 22:42:521192
1193 EXPECT_EQ(0, cache.network_layer()->transaction_count());
1194 EXPECT_EQ(0, cache.disk_cache()->open_count());
1195 EXPECT_EQ(0, cache.disk_cache()->create_count());
1196}
1197
1198TEST(HttpCache, SimpleGET_LoadPreferringCache_Hit) {
1199 MockHttpCache cache;
1200
1201 // write to the cache
1202 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1203
1204 // force this transaction to read from the cache if valid
1205 MockTransaction transaction(kSimpleGET_Transaction);
1206 transaction.load_flags |= net::LOAD_PREFERRING_CACHE;
1207
1208 RunTransactionTest(cache.http_cache(), transaction);
1209
1210 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1211 EXPECT_EQ(1, cache.disk_cache()->open_count());
1212 EXPECT_EQ(1, cache.disk_cache()->create_count());
1213}
1214
1215TEST(HttpCache, SimpleGET_LoadPreferringCache_Miss) {
1216 MockHttpCache cache;
1217
1218 // force this transaction to read from the cache if valid
1219 MockTransaction transaction(kSimpleGET_Transaction);
1220 transaction.load_flags |= net::LOAD_PREFERRING_CACHE;
1221
1222 RunTransactionTest(cache.http_cache(), transaction);
1223
1224 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1225 EXPECT_EQ(0, cache.disk_cache()->open_count());
1226 EXPECT_EQ(1, cache.disk_cache()->create_count());
1227}
1228
1229TEST(HttpCache, SimpleGET_LoadBypassCache) {
1230 MockHttpCache cache;
1231
[email protected]9393b7172010-02-11 00:12:151232 // Write to the cache.
initial.commit586acc5fe2008-07-26 22:42:521233 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1234
[email protected]9393b7172010-02-11 00:12:151235 // Force this transaction to write to the cache again.
initial.commit586acc5fe2008-07-26 22:42:521236 MockTransaction transaction(kSimpleGET_Transaction);
1237 transaction.load_flags |= net::LOAD_BYPASS_CACHE;
1238
[email protected]9e743cd2010-03-16 07:03:531239 net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded);
[email protected]9393b7172010-02-11 00:12:151240
[email protected]9e743cd2010-03-16 07:03:531241 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound());
[email protected]9393b7172010-02-11 00:12:151242
[email protected]9e743cd2010-03-16 07:03:531243 // Check that the NetLog was filled as expected.
[email protected]46773162010-05-07 22:31:201244 EXPECT_EQ(8u, log.entries().size());
[email protected]9393b7172010-02-11 00:12:151245 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]46773162010-05-07 22:31:201246 log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING));
[email protected]9393b7172010-02-11 00:12:151247 EXPECT_TRUE(net::LogContainsEndEvent(
[email protected]46773162010-05-07 22:31:201248 log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING));
[email protected]9393b7172010-02-11 00:12:151249 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]46773162010-05-07 22:31:201250 log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY));
[email protected]9393b7172010-02-11 00:12:151251 EXPECT_TRUE(net::LogContainsEndEvent(
[email protected]46773162010-05-07 22:31:201252 log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY));
[email protected]9393b7172010-02-11 00:12:151253 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]46773162010-05-07 22:31:201254 log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY));
[email protected]9393b7172010-02-11 00:12:151255 EXPECT_TRUE(net::LogContainsEndEvent(
[email protected]46773162010-05-07 22:31:201256 log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY));
1257 EXPECT_TRUE(net::LogContainsBeginEvent(
1258 log.entries(), 6, net::NetLog::TYPE_HTTP_CACHE_WAITING));
1259 EXPECT_TRUE(net::LogContainsEndEvent(
1260 log.entries(), 7, net::NetLog::TYPE_HTTP_CACHE_WAITING));
initial.commit586acc5fe2008-07-26 22:42:521261
1262 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1263 EXPECT_EQ(0, cache.disk_cache()->open_count());
1264 EXPECT_EQ(2, cache.disk_cache()->create_count());
1265}
1266
1267TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit) {
1268 MockHttpCache cache;
1269
1270 // write to the cache
1271 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1272
1273 // force this transaction to write to the cache again
1274 MockTransaction transaction(kSimpleGET_Transaction);
1275 transaction.request_headers = "pragma: no-cache";
1276
1277 RunTransactionTest(cache.http_cache(), transaction);
1278
1279 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1280 EXPECT_EQ(0, cache.disk_cache()->open_count());
1281 EXPECT_EQ(2, cache.disk_cache()->create_count());
1282}
1283
1284TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit2) {
1285 MockHttpCache cache;
1286
1287 // write to the cache
1288 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1289
1290 // force this transaction to write to the cache again
1291 MockTransaction transaction(kSimpleGET_Transaction);
1292 transaction.request_headers = "cache-control: no-cache";
1293
1294 RunTransactionTest(cache.http_cache(), transaction);
1295
1296 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1297 EXPECT_EQ(0, cache.disk_cache()->open_count());
1298 EXPECT_EQ(2, cache.disk_cache()->create_count());
1299}
1300
1301TEST(HttpCache, SimpleGET_LoadValidateCache) {
1302 MockHttpCache cache;
1303
1304 // write to the cache
1305 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1306
1307 // read from the cache
1308 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1309
1310 // force this transaction to validate the cache
1311 MockTransaction transaction(kSimpleGET_Transaction);
1312 transaction.load_flags |= net::LOAD_VALIDATE_CACHE;
1313
1314 RunTransactionTest(cache.http_cache(), transaction);
1315
1316 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1317 EXPECT_EQ(1, cache.disk_cache()->open_count());
1318 EXPECT_EQ(1, cache.disk_cache()->create_count());
1319}
1320
1321TEST(HttpCache, SimpleGET_LoadValidateCache_Implicit) {
1322 MockHttpCache cache;
1323
1324 // write to the cache
1325 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1326
1327 // read from the cache
1328 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1329
1330 // force this transaction to validate the cache
1331 MockTransaction transaction(kSimpleGET_Transaction);
1332 transaction.request_headers = "cache-control: max-age=0";
1333
1334 RunTransactionTest(cache.http_cache(), transaction);
1335
1336 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1337 EXPECT_EQ(1, cache.disk_cache()->open_count());
1338 EXPECT_EQ(1, cache.disk_cache()->create_count());
1339}
1340
[email protected]a3eee212009-11-05 18:08:581341static void PreserveRequestHeaders_Handler(
1342 const net::HttpRequestInfo* request,
1343 std::string* response_status,
1344 std::string* response_headers,
1345 std::string* response_data) {
[email protected]8c76ae22010-04-20 22:15:431346 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey));
[email protected]a3eee212009-11-05 18:08:581347}
1348
1349// Tests that we don't remove extra headers for simple requests.
1350TEST(HttpCache, SimpleGET_PreserveRequestHeaders) {
1351 MockHttpCache cache;
1352
1353 MockTransaction transaction(kSimpleGET_Transaction);
1354 transaction.handler = PreserveRequestHeaders_Handler;
1355 transaction.request_headers = EXTRA_HEADER;
1356 transaction.response_headers = "Cache-Control: max-age=0\n";
1357 AddMockTransaction(&transaction);
1358
1359 // Write, then revalidate the entry.
1360 RunTransactionTest(cache.http_cache(), transaction);
1361 RunTransactionTest(cache.http_cache(), transaction);
1362
1363 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1364 EXPECT_EQ(1, cache.disk_cache()->open_count());
1365 EXPECT_EQ(1, cache.disk_cache()->create_count());
1366 RemoveMockTransaction(&transaction);
1367}
1368
1369// Tests that we don't remove extra headers for conditionalized requests.
1370TEST(HttpCache, ConditionalizedGET_PreserveRequestHeaders) {
1371 MockHttpCache cache;
1372
1373 // Write to the cache.
1374 RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
1375
1376 MockTransaction transaction(kETagGET_Transaction);
1377 transaction.handler = PreserveRequestHeaders_Handler;
[email protected]8c76ae22010-04-20 22:15:431378 transaction.request_headers = "If-None-Match: \"foopy\"\r\n"
[email protected]a3eee212009-11-05 18:08:581379 EXTRA_HEADER;
1380 AddMockTransaction(&transaction);
1381
1382 RunTransactionTest(cache.http_cache(), transaction);
1383
1384 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1385 EXPECT_EQ(1, cache.disk_cache()->open_count());
1386 EXPECT_EQ(1, cache.disk_cache()->create_count());
1387 RemoveMockTransaction(&transaction);
1388}
1389
initial.commit586acc5fe2008-07-26 22:42:521390TEST(HttpCache, SimpleGET_ManyReaders) {
1391 MockHttpCache cache;
1392
1393 MockHttpRequest request(kSimpleGET_Transaction);
1394
initial.commit586acc5fe2008-07-26 22:42:521395 std::vector<Context*> context_list;
1396 const int kNumTransactions = 5;
1397
1398 for (int i = 0; i < kNumTransactions; ++i) {
[email protected]1638d602009-09-24 03:49:171399 context_list.push_back(new Context());
initial.commit586acc5fe2008-07-26 22:42:521400 Context* c = context_list[i];
[email protected]1638d602009-09-24 03:49:171401
1402 c->result = cache.http_cache()->CreateTransaction(&c->trans);
1403 EXPECT_EQ(net::OK, c->result);
1404
[email protected]5a1d7ca2010-04-28 20:12:271405 c->result = c->trans->Start(&request, &c->callback, net::BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:521406 }
1407
[email protected]7d7ad6e42010-01-14 01:30:531408 // Allow all requests to move from the Create queue to the active entry.
1409 MessageLoop::current()->RunAllPending();
1410
1411 // The first request should be a writer at this point, and the subsequent
initial.commit586acc5fe2008-07-26 22:42:521412 // requests should be pending.
1413
1414 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1415 EXPECT_EQ(0, cache.disk_cache()->open_count());
1416 EXPECT_EQ(1, cache.disk_cache()->create_count());
1417
1418 for (int i = 0; i < kNumTransactions; ++i) {
1419 Context* c = context_list[i];
1420 if (c->result == net::ERR_IO_PENDING)
1421 c->result = c->callback.WaitForResult();
[email protected]af4876d2008-10-21 23:10:571422 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
initial.commit586acc5fe2008-07-26 22:42:521423 }
1424
[email protected]7d7ad6e42010-01-14 01:30:531425 // We should not have had to re-open the disk entry
initial.commit586acc5fe2008-07-26 22:42:521426
1427 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1428 EXPECT_EQ(0, cache.disk_cache()->open_count());
1429 EXPECT_EQ(1, cache.disk_cache()->create_count());
1430
1431 for (int i = 0; i < kNumTransactions; ++i) {
1432 Context* c = context_list[i];
initial.commit586acc5fe2008-07-26 22:42:521433 delete c;
1434 }
1435}
1436
[email protected]e1891642009-01-07 18:30:571437// This is a test for http://code.google.com/p/chromium/issues/detail?id=4769.
1438// If cancelling a request is racing with another request for the same resource
1439// finishing, we have to make sure that we remove both transactions from the
1440// entry.
1441TEST(HttpCache, SimpleGET_RacingReaders) {
1442 MockHttpCache cache;
1443
1444 MockHttpRequest request(kSimpleGET_Transaction);
1445 MockHttpRequest reader_request(kSimpleGET_Transaction);
1446 reader_request.load_flags = net::LOAD_ONLY_FROM_CACHE;
1447
1448 std::vector<Context*> context_list;
1449 const int kNumTransactions = 5;
1450
1451 for (int i = 0; i < kNumTransactions; ++i) {
[email protected]1638d602009-09-24 03:49:171452 context_list.push_back(new Context());
[email protected]e1891642009-01-07 18:30:571453 Context* c = context_list[i];
[email protected]1638d602009-09-24 03:49:171454
1455 c->result = cache.http_cache()->CreateTransaction(&c->trans);
1456 EXPECT_EQ(net::OK, c->result);
1457
[email protected]e1891642009-01-07 18:30:571458 MockHttpRequest* this_request = &request;
1459 if (i == 1 || i == 2)
1460 this_request = &reader_request;
1461
[email protected]5a1d7ca2010-04-28 20:12:271462 c->result = c->trans->Start(this_request, &c->callback, net::BoundNetLog());
[email protected]e1891642009-01-07 18:30:571463 }
1464
[email protected]7d7ad6e42010-01-14 01:30:531465 // Allow all requests to move from the Create queue to the active entry.
1466 MessageLoop::current()->RunAllPending();
1467
[email protected]e1891642009-01-07 18:30:571468 // The first request should be a writer at this point, and the subsequent
1469 // requests should be pending.
1470
1471 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1472 EXPECT_EQ(0, cache.disk_cache()->open_count());
1473 EXPECT_EQ(1, cache.disk_cache()->create_count());
1474
1475 Context* c = context_list[0];
1476 ASSERT_EQ(net::ERR_IO_PENDING, c->result);
1477 c->result = c->callback.WaitForResult();
1478 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1479
1480 // Now we have 2 active readers and two queued transactions.
1481
1482 c = context_list[1];
1483 ASSERT_EQ(net::ERR_IO_PENDING, c->result);
1484 c->result = c->callback.WaitForResult();
[email protected]37095fe2009-08-07 00:13:121485 if (c->result == net::OK)
1486 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
[email protected]e1891642009-01-07 18:30:571487
1488 // At this point we have one reader, two pending transactions and a task on
1489 // the queue to move to the next transaction. Now we cancel the request that
1490 // is the current reader, and expect the queued task to be able to start the
1491 // next request.
1492
1493 c = context_list[2];
1494 c->trans.reset();
1495
1496 for (int i = 3; i < kNumTransactions; ++i) {
1497 Context* c = context_list[i];
1498 if (c->result == net::ERR_IO_PENDING)
1499 c->result = c->callback.WaitForResult();
[email protected]37095fe2009-08-07 00:13:121500 if (c->result == net::OK)
1501 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
[email protected]e1891642009-01-07 18:30:571502 }
1503
1504 // We should not have had to re-open the disk entry.
1505
1506 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1507 EXPECT_EQ(0, cache.disk_cache()->open_count());
1508 EXPECT_EQ(1, cache.disk_cache()->create_count());
1509
1510 for (int i = 0; i < kNumTransactions; ++i) {
1511 Context* c = context_list[i];
1512 delete c;
1513 }
1514}
1515
[email protected]d5b94c72009-10-26 16:51:101516// Tests that we can doom an entry with pending transactions and delete one of
1517// the pending transactions before the first one completes.
1518// See http://code.google.com/p/chromium/issues/detail?id=25588
1519TEST(HttpCache, SimpleGET_DoomWithPending) {
1520 // We need simultaneous doomed / not_doomed entries so let's use a real cache.
[email protected]f8702522010-05-12 18:40:101521 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(1024 * 1024));
[email protected]d5b94c72009-10-26 16:51:101522
1523 MockHttpRequest request(kSimpleGET_Transaction);
1524 MockHttpRequest writer_request(kSimpleGET_Transaction);
1525 writer_request.load_flags = net::LOAD_BYPASS_CACHE;
1526
1527 ScopedVector<Context> context_list;
1528 const int kNumTransactions = 4;
1529
1530 for (int i = 0; i < kNumTransactions; ++i) {
1531 context_list.push_back(new Context());
1532 Context* c = context_list[i];
1533
1534 c->result = cache.http_cache()->CreateTransaction(&c->trans);
1535 EXPECT_EQ(net::OK, c->result);
1536
1537 MockHttpRequest* this_request = &request;
1538 if (i == 3)
1539 this_request = &writer_request;
1540
[email protected]5a1d7ca2010-04-28 20:12:271541 c->result = c->trans->Start(this_request, &c->callback, net::BoundNetLog());
[email protected]d5b94c72009-10-26 16:51:101542 }
1543
1544 // The first request should be a writer at this point, and the two subsequent
1545 // requests should be pending. The last request doomed the first entry.
1546
1547 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1548
1549 // Cancel the first queued transaction.
1550 delete context_list[1];
1551 context_list.get()[1] = NULL;
1552
1553 for (int i = 0; i < kNumTransactions; ++i) {
1554 if (i == 1)
1555 continue;
1556 Context* c = context_list[i];
1557 ASSERT_EQ(net::ERR_IO_PENDING, c->result);
1558 c->result = c->callback.WaitForResult();
1559 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1560 }
1561}
1562
[email protected]b367d9a52009-02-27 01:02:511563// This is a test for http://code.google.com/p/chromium/issues/detail?id=4731.
1564// We may attempt to delete an entry synchronously with the act of adding a new
1565// transaction to said entry.
1566TEST(HttpCache, FastNoStoreGET_DoneWithPending) {
1567 MockHttpCache cache;
1568
1569 // The headers will be served right from the call to Start() the request.
1570 MockHttpRequest request(kFastNoStoreGET_Transaction);
1571 FastTransactionServer request_handler;
1572 AddMockTransaction(&kFastNoStoreGET_Transaction);
1573
1574 std::vector<Context*> context_list;
1575 const int kNumTransactions = 3;
1576
1577 for (int i = 0; i < kNumTransactions; ++i) {
[email protected]1638d602009-09-24 03:49:171578 context_list.push_back(new Context());
[email protected]b367d9a52009-02-27 01:02:511579 Context* c = context_list[i];
[email protected]1638d602009-09-24 03:49:171580
1581 c->result = cache.http_cache()->CreateTransaction(&c->trans);
1582 EXPECT_EQ(net::OK, c->result);
1583
[email protected]5a1d7ca2010-04-28 20:12:271584 c->result = c->trans->Start(&request, &c->callback, net::BoundNetLog());
[email protected]b367d9a52009-02-27 01:02:511585 }
1586
[email protected]7d7ad6e42010-01-14 01:30:531587 // Allow all requests to move from the Create queue to the active entry.
1588 MessageLoop::current()->RunAllPending();
1589
[email protected]b367d9a52009-02-27 01:02:511590 // The first request should be a writer at this point, and the subsequent
1591 // requests should be pending.
1592
1593 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1594 EXPECT_EQ(0, cache.disk_cache()->open_count());
1595 EXPECT_EQ(1, cache.disk_cache()->create_count());
1596
1597 // Now, make sure that the second request asks for the entry not to be stored.
1598 request_handler.set_no_store(true);
1599
1600 for (int i = 0; i < kNumTransactions; ++i) {
1601 Context* c = context_list[i];
1602 if (c->result == net::ERR_IO_PENDING)
1603 c->result = c->callback.WaitForResult();
1604 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction);
1605 delete c;
1606 }
1607
1608 EXPECT_EQ(3, cache.network_layer()->transaction_count());
1609 EXPECT_EQ(0, cache.disk_cache()->open_count());
1610 EXPECT_EQ(2, cache.disk_cache()->create_count());
1611
1612 RemoveMockTransaction(&kFastNoStoreGET_Transaction);
1613}
1614
initial.commit586acc5fe2008-07-26 22:42:521615TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) {
1616 MockHttpCache cache;
1617
1618 MockHttpRequest request(kSimpleGET_Transaction);
1619
initial.commit586acc5fe2008-07-26 22:42:521620 std::vector<Context*> context_list;
1621 const int kNumTransactions = 2;
1622
1623 for (int i = 0; i < kNumTransactions; ++i) {
[email protected]1638d602009-09-24 03:49:171624 context_list.push_back(new Context());
initial.commit586acc5fe2008-07-26 22:42:521625 Context* c = context_list[i];
[email protected]1638d602009-09-24 03:49:171626
1627 c->result = cache.http_cache()->CreateTransaction(&c->trans);
1628 EXPECT_EQ(net::OK, c->result);
1629
[email protected]5a1d7ca2010-04-28 20:12:271630 c->result = c->trans->Start(&request, &c->callback, net::BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:521631 }
1632
[email protected]7d7ad6e42010-01-14 01:30:531633 // Allow all requests to move from the Create queue to the active entry.
1634 MessageLoop::current()->RunAllPending();
1635
1636 // The first request should be a writer at this point, and the subsequent
initial.commit586acc5fe2008-07-26 22:42:521637 // requests should be pending.
1638
1639 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1640 EXPECT_EQ(0, cache.disk_cache()->open_count());
1641 EXPECT_EQ(1, cache.disk_cache()->create_count());
1642
1643 for (int i = 0; i < kNumTransactions; ++i) {
1644 Context* c = context_list[i];
1645 if (c->result == net::ERR_IO_PENDING)
1646 c->result = c->callback.WaitForResult();
[email protected]7d7ad6e42010-01-14 01:30:531647 // Destroy only the first transaction.
initial.commit586acc5fe2008-07-26 22:42:521648 if (i == 0) {
initial.commit586acc5fe2008-07-26 22:42:521649 delete c;
1650 context_list[i] = NULL;
1651 }
1652 }
1653
[email protected]7d7ad6e42010-01-14 01:30:531654 // Complete the rest of the transactions.
initial.commit586acc5fe2008-07-26 22:42:521655 for (int i = 1; i < kNumTransactions; ++i) {
1656 Context* c = context_list[i];
[email protected]af4876d2008-10-21 23:10:571657 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
initial.commit586acc5fe2008-07-26 22:42:521658 }
1659
[email protected]7d7ad6e42010-01-14 01:30:531660 // We should have had to re-open the disk entry.
initial.commit586acc5fe2008-07-26 22:42:521661
1662 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1663 EXPECT_EQ(0, cache.disk_cache()->open_count());
1664 EXPECT_EQ(2, cache.disk_cache()->create_count());
1665
1666 for (int i = 1; i < kNumTransactions; ++i) {
1667 Context* c = context_list[i];
initial.commit586acc5fe2008-07-26 22:42:521668 delete c;
1669 }
1670}
1671
[email protected]7d7ad6e42010-01-14 01:30:531672// Tests that we can cancel requests that are queued waiting to open the disk
1673// cache entry.
1674TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) {
1675 MockHttpCache cache;
1676
1677 MockHttpRequest request(kSimpleGET_Transaction);
1678
1679 std::vector<Context*> context_list;
1680 const int kNumTransactions = 5;
1681
1682 for (int i = 0; i < kNumTransactions; i++) {
1683 context_list.push_back(new Context());
1684 Context* c = context_list[i];
1685
1686 c->result = cache.http_cache()->CreateTransaction(&c->trans);
1687 EXPECT_EQ(net::OK, c->result);
1688
[email protected]5a1d7ca2010-04-28 20:12:271689 c->result = c->trans->Start(&request, &c->callback, net::BoundNetLog());
[email protected]7d7ad6e42010-01-14 01:30:531690 }
1691
1692 // The first request should be creating the disk cache entry and the others
1693 // should be pending.
1694
1695 EXPECT_EQ(0, cache.network_layer()->transaction_count());
1696 EXPECT_EQ(0, cache.disk_cache()->open_count());
1697 EXPECT_EQ(1, cache.disk_cache()->create_count());
1698
1699 // Cancel a request from the pending queue.
1700 delete context_list[3];
1701 context_list[3] = NULL;
1702
1703 // Cancel the request that is creating the entry. This will force the pending
1704 // operations to restart.
1705 delete context_list[0];
1706 context_list[0] = NULL;
1707
1708 // Complete the rest of the transactions.
1709 for (int i = 1; i < kNumTransactions; i++) {
1710 Context* c = context_list[i];
1711 if (c) {
1712 c->result = c->callback.GetResult(c->result);
1713 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1714 }
1715 }
1716
1717 // We should have had to re-create the disk entry.
1718
1719 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1720 EXPECT_EQ(0, cache.disk_cache()->open_count());
1721 EXPECT_EQ(2, cache.disk_cache()->create_count());
1722
1723 for (int i = 1; i < kNumTransactions; ++i) {
1724 delete context_list[i];
1725 }
1726}
1727
[email protected]fb2622f2010-07-13 18:00:561728// Tests that we can cancel a single request to open a disk cache entry.
1729TEST(HttpCache, SimpleGET_CancelCreate) {
1730 MockHttpCache cache;
1731
1732 MockHttpRequest request(kSimpleGET_Transaction);
1733
1734 Context* c = new Context();
1735
1736 c->result = cache.http_cache()->CreateTransaction(&c->trans);
1737 EXPECT_EQ(net::OK, c->result);
1738
1739 c->result = c->trans->Start(&request, &c->callback, net::BoundNetLog());
1740 EXPECT_EQ(net::ERR_IO_PENDING, c->result);
1741
1742 // Release the reference that the mock disk cache keeps for this entry, so
1743 // that we test that the http cache handles the cancelation correctly.
1744 cache.disk_cache()->ReleaseAll();
1745 delete c;
1746
1747 MessageLoop::current()->RunAllPending();
1748 EXPECT_EQ(1, cache.disk_cache()->create_count());
1749}
1750
[email protected]7d7ad6e42010-01-14 01:30:531751// Tests that we delete/create entries even if multiple requests are queued.
1752TEST(HttpCache, SimpleGET_ManyWriters_BypassCache) {
1753 MockHttpCache cache;
1754
1755 MockHttpRequest request(kSimpleGET_Transaction);
1756 request.load_flags = net::LOAD_BYPASS_CACHE;
1757
1758 std::vector<Context*> context_list;
1759 const int kNumTransactions = 5;
1760
1761 for (int i = 0; i < kNumTransactions; i++) {
1762 context_list.push_back(new Context());
1763 Context* c = context_list[i];
1764
1765 c->result = cache.http_cache()->CreateTransaction(&c->trans);
1766 EXPECT_EQ(net::OK, c->result);
1767
[email protected]5a1d7ca2010-04-28 20:12:271768 c->result = c->trans->Start(&request, &c->callback, net::BoundNetLog());
[email protected]7d7ad6e42010-01-14 01:30:531769 }
1770
1771 // The first request should be deleting the disk cache entry and the others
1772 // should be pending.
1773
1774 EXPECT_EQ(0, cache.network_layer()->transaction_count());
1775 EXPECT_EQ(0, cache.disk_cache()->open_count());
1776 EXPECT_EQ(0, cache.disk_cache()->create_count());
1777
1778 // Complete the transactions.
1779 for (int i = 0; i < kNumTransactions; i++) {
1780 Context* c = context_list[i];
1781 c->result = c->callback.GetResult(c->result);
1782 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1783 }
1784
1785 // We should have had to re-create the disk entry multiple times.
1786
1787 EXPECT_EQ(5, cache.network_layer()->transaction_count());
1788 EXPECT_EQ(0, cache.disk_cache()->open_count());
1789 EXPECT_EQ(5, cache.disk_cache()->create_count());
1790
1791 for (int i = 0; i < kNumTransactions; ++i) {
1792 delete context_list[i];
1793 }
1794}
1795
initial.commit586acc5fe2008-07-26 22:42:521796TEST(HttpCache, SimpleGET_AbandonedCacheRead) {
1797 MockHttpCache cache;
1798
1799 // write to the cache
1800 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1801
1802 MockHttpRequest request(kSimpleGET_Transaction);
1803 TestCompletionCallback callback;
1804
[email protected]1638d602009-09-24 03:49:171805 scoped_ptr<net::HttpTransaction> trans;
1806 int rv = cache.http_cache()->CreateTransaction(&trans);
1807 EXPECT_EQ(net::OK, rv);
[email protected]5a1d7ca2010-04-28 20:12:271808 rv = trans->Start(&request, &callback, net::BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:521809 if (rv == net::ERR_IO_PENDING)
1810 rv = callback.WaitForResult();
1811 ASSERT_EQ(net::OK, rv);
1812
[email protected]9dea9e1f2009-01-29 00:30:471813 scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(256);
1814 rv = trans->Read(buf, 256, &callback);
initial.commit586acc5fe2008-07-26 22:42:521815 EXPECT_EQ(net::ERR_IO_PENDING, rv);
1816
1817 // Test that destroying the transaction while it is reading from the cache
1818 // works properly.
[email protected]af4876d2008-10-21 23:10:571819 trans.reset();
initial.commit586acc5fe2008-07-26 22:42:521820
1821 // Make sure we pump any pending events, which should include a call to
1822 // HttpCache::Transaction::OnCacheReadCompleted.
[email protected]295039bd2008-08-15 04:32:571823 MessageLoop::current()->RunAllPending();
initial.commit586acc5fe2008-07-26 22:42:521824}
1825
[email protected]46773162010-05-07 22:31:201826// Tests that we can delete the HttpCache and deal with queued transactions
1827// ("waiting for the backend" as opposed to Active or Doomed entries).
1828TEST(HttpCache, SimpleGET_ManyWriters_DeleteCache) {
[email protected]f8702522010-05-12 18:40:101829 scoped_ptr<MockHttpCache> cache(new MockHttpCache(
1830 new MockBackendNoCbFactory()));
[email protected]46773162010-05-07 22:31:201831
1832 MockHttpRequest request(kSimpleGET_Transaction);
1833
1834 std::vector<Context*> context_list;
1835 const int kNumTransactions = 5;
1836
1837 for (int i = 0; i < kNumTransactions; i++) {
1838 context_list.push_back(new Context());
1839 Context* c = context_list[i];
1840
1841 c->result = cache->http_cache()->CreateTransaction(&c->trans);
1842 EXPECT_EQ(net::OK, c->result);
1843
1844 c->result = c->trans->Start(&request, &c->callback, net::BoundNetLog());
1845 }
1846
1847 // The first request should be creating the disk cache entry and the others
1848 // should be pending.
1849
1850 EXPECT_EQ(0, cache->network_layer()->transaction_count());
1851 EXPECT_EQ(0, cache->disk_cache()->open_count());
1852 EXPECT_EQ(0, cache->disk_cache()->create_count());
1853
1854 cache.reset();
1855
1856 // There is not much to do with the transactions at this point... they are
1857 // waiting for a callback that will not fire.
1858 for (int i = 0; i < kNumTransactions; ++i) {
1859 delete context_list[i];
1860 }
1861}
1862
[email protected]f8702522010-05-12 18:40:101863// Tests that we queue requests when initializing the backend.
1864TEST(HttpCache, SimpleGET_WaitForBackend) {
1865 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
1866 MockHttpCache cache(factory);
1867
1868 MockHttpRequest request0(kSimpleGET_Transaction);
1869 MockHttpRequest request1(kTypicalGET_Transaction);
1870 MockHttpRequest request2(kETagGET_Transaction);
1871
1872 std::vector<Context*> context_list;
1873 const int kNumTransactions = 3;
1874
1875 for (int i = 0; i < kNumTransactions; i++) {
1876 context_list.push_back(new Context());
1877 Context* c = context_list[i];
1878
1879 c->result = cache.http_cache()->CreateTransaction(&c->trans);
1880 EXPECT_EQ(net::OK, c->result);
1881 }
1882
1883 context_list[0]->result = context_list[0]->trans->Start(
1884 &request0, &context_list[0]->callback, net::BoundNetLog());
1885 context_list[1]->result = context_list[1]->trans->Start(
1886 &request1, &context_list[1]->callback, net::BoundNetLog());
1887 context_list[2]->result = context_list[2]->trans->Start(
1888 &request2, &context_list[2]->callback, net::BoundNetLog());
1889
1890 // Just to make sure that everything is still pending.
1891 MessageLoop::current()->RunAllPending();
1892
1893 // The first request should be creating the disk cache.
1894 EXPECT_FALSE(context_list[0]->callback.have_result());
1895
1896 factory->FinishCreation();
1897
1898 MessageLoop::current()->RunAllPending();
1899 EXPECT_EQ(3, cache.network_layer()->transaction_count());
1900 EXPECT_EQ(3, cache.disk_cache()->create_count());
1901
1902 for (int i = 0; i < kNumTransactions; ++i) {
1903 EXPECT_TRUE(context_list[i]->callback.have_result());
1904 delete context_list[i];
1905 }
1906}
1907
1908// Tests that we can cancel requests that are queued waiting for the backend
1909// to be initialized.
1910TEST(HttpCache, SimpleGET_WaitForBackend_CancelCreate) {
1911 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
1912 MockHttpCache cache(factory);
1913
1914 MockHttpRequest request0(kSimpleGET_Transaction);
1915 MockHttpRequest request1(kTypicalGET_Transaction);
1916 MockHttpRequest request2(kETagGET_Transaction);
1917
1918 std::vector<Context*> context_list;
1919 const int kNumTransactions = 3;
1920
1921 for (int i = 0; i < kNumTransactions; i++) {
1922 context_list.push_back(new Context());
1923 Context* c = context_list[i];
1924
1925 c->result = cache.http_cache()->CreateTransaction(&c->trans);
1926 EXPECT_EQ(net::OK, c->result);
1927 }
1928
1929 context_list[0]->result = context_list[0]->trans->Start(
1930 &request0, &context_list[0]->callback, net::BoundNetLog());
1931 context_list[1]->result = context_list[1]->trans->Start(
1932 &request1, &context_list[1]->callback, net::BoundNetLog());
1933 context_list[2]->result = context_list[2]->trans->Start(
1934 &request2, &context_list[2]->callback, net::BoundNetLog());
1935
1936 // Just to make sure that everything is still pending.
1937 MessageLoop::current()->RunAllPending();
1938
1939 // The first request should be creating the disk cache.
1940 EXPECT_FALSE(context_list[0]->callback.have_result());
1941
1942 // Cancel a request from the pending queue.
1943 delete context_list[1];
1944 context_list[1] = NULL;
1945
1946 // Cancel the request that is creating the entry.
1947 delete context_list[0];
1948 context_list[0] = NULL;
1949
1950 // Complete the last transaction.
1951 factory->FinishCreation();
1952
1953 context_list[2]->result =
1954 context_list[2]->callback.GetResult(context_list[2]->result);
1955 ReadAndVerifyTransaction(context_list[2]->trans.get(), kETagGET_Transaction);
1956
1957 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1958 EXPECT_EQ(1, cache.disk_cache()->create_count());
1959
1960 delete context_list[2];
1961}
1962
initial.commit586acc5fe2008-07-26 22:42:521963TEST(HttpCache, TypicalGET_ConditionalRequest) {
1964 MockHttpCache cache;
1965
1966 // write to the cache
1967 RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction);
1968
1969 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1970 EXPECT_EQ(0, cache.disk_cache()->open_count());
1971 EXPECT_EQ(1, cache.disk_cache()->create_count());
1972
1973 // get the same URL again, but this time we expect it to result
1974 // in a conditional request.
1975 RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction);
1976
1977 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1978 EXPECT_EQ(1, cache.disk_cache()->open_count());
1979 EXPECT_EQ(1, cache.disk_cache()->create_count());
1980}
1981
1982static void ETagGet_ConditionalRequest_Handler(
1983 const net::HttpRequestInfo* request,
1984 std::string* response_status,
1985 std::string* response_headers,
1986 std::string* response_data) {
[email protected]8c76ae22010-04-20 22:15:431987 EXPECT_TRUE(
1988 request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch));
initial.commit586acc5fe2008-07-26 22:42:521989 response_status->assign("HTTP/1.1 304 Not Modified");
1990 response_headers->assign(kETagGET_Transaction.response_headers);
1991 response_data->clear();
1992}
1993
1994TEST(HttpCache, ETagGET_ConditionalRequest_304) {
1995 MockHttpCache cache;
1996
1997 ScopedMockTransaction transaction(kETagGET_Transaction);
1998
1999 // write to the cache
2000 RunTransactionTest(cache.http_cache(), transaction);
2001
2002 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2003 EXPECT_EQ(0, cache.disk_cache()->open_count());
2004 EXPECT_EQ(1, cache.disk_cache()->create_count());
2005
2006 // get the same URL again, but this time we expect it to result
2007 // in a conditional request.
2008 transaction.load_flags = net::LOAD_VALIDATE_CACHE;
2009 transaction.handler = ETagGet_ConditionalRequest_Handler;
2010 RunTransactionTest(cache.http_cache(), transaction);
2011
2012 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2013 EXPECT_EQ(1, cache.disk_cache()->open_count());
2014 EXPECT_EQ(1, cache.disk_cache()->create_count());
2015}
2016
[email protected]b7d05ab2008-12-09 19:18:412017static void ETagGet_ConditionalRequest_NoStore_Handler(
2018 const net::HttpRequestInfo* request,
2019 std::string* response_status,
2020 std::string* response_headers,
2021 std::string* response_data) {
[email protected]8c76ae22010-04-20 22:15:432022 EXPECT_TRUE(
2023 request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch));
[email protected]b7d05ab2008-12-09 19:18:412024 response_status->assign("HTTP/1.1 304 Not Modified");
2025 response_headers->assign("Cache-Control: no-store\n");
2026 response_data->clear();
2027}
2028
2029TEST(HttpCache, ETagGET_ConditionalRequest_304_NoStore) {
2030 MockHttpCache cache;
2031
2032 ScopedMockTransaction transaction(kETagGET_Transaction);
2033
2034 // Write to the cache.
2035 RunTransactionTest(cache.http_cache(), transaction);
2036
2037 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2038 EXPECT_EQ(0, cache.disk_cache()->open_count());
2039 EXPECT_EQ(1, cache.disk_cache()->create_count());
2040
2041 // Get the same URL again, but this time we expect it to result
2042 // in a conditional request.
2043 transaction.load_flags = net::LOAD_VALIDATE_CACHE;
2044 transaction.handler = ETagGet_ConditionalRequest_NoStore_Handler;
2045 RunTransactionTest(cache.http_cache(), transaction);
2046
2047 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2048 EXPECT_EQ(1, cache.disk_cache()->open_count());
2049 EXPECT_EQ(1, cache.disk_cache()->create_count());
2050
2051 ScopedMockTransaction transaction2(kETagGET_Transaction);
2052
2053 // Write to the cache again. This should create a new entry.
2054 RunTransactionTest(cache.http_cache(), transaction2);
2055
2056 EXPECT_EQ(3, cache.network_layer()->transaction_count());
2057 EXPECT_EQ(1, cache.disk_cache()->open_count());
2058 EXPECT_EQ(2, cache.disk_cache()->create_count());
2059}
2060
initial.commit586acc5fe2008-07-26 22:42:522061TEST(HttpCache, SimplePOST_SkipsCache) {
2062 MockHttpCache cache;
2063
[email protected]96bac982009-03-24 18:20:062064 // Test that we skip the cache for POST requests that do not have an upload
2065 // identifier.
initial.commit586acc5fe2008-07-26 22:42:522066
2067 RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction);
2068
2069 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2070 EXPECT_EQ(0, cache.disk_cache()->open_count());
2071 EXPECT_EQ(0, cache.disk_cache()->create_count());
2072}
2073
[email protected]4de4fb12009-08-03 22:11:182074// Helper that does 4 requests using HttpCache:
2075//
2076// (1) loads |kUrl| -- expects |net_response_1| to be returned.
2077// (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned.
2078// (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to
2079// be returned.
2080// (4) loads |kUrl| from cache only -- expects |cached_response_2| to be
2081// returned.
2082static void ConditionalizedRequestUpdatesCacheHelper(
2083 const Response& net_response_1,
2084 const Response& net_response_2,
2085 const Response& cached_response_2,
2086 const char* extra_request_headers) {
[email protected]bded84c2009-07-23 00:36:062087 MockHttpCache cache;
2088
2089 // The URL we will be requesting.
2090 const char* kUrl = "http://foobar.com/main.css";
2091
[email protected]bded84c2009-07-23 00:36:062092 // Junk network response.
2093 static const Response kUnexpectedResponse = {
2094 "HTTP/1.1 500 Unexpected",
2095 "Server: unexpected_header",
2096 "unexpected body"
2097 };
2098
2099 // We will control the network layer's responses for |kUrl| using
2100 // |mock_network_response|.
2101 MockTransaction mock_network_response = { 0 };
2102 mock_network_response.url = kUrl;
2103 AddMockTransaction(&mock_network_response);
2104
2105 // Request |kUrl| for the first time. It should hit the network and
2106 // receive |kNetResponse1|, which it saves into the HTTP cache.
2107
2108 MockTransaction request = { 0 };
2109 request.url = kUrl;
2110 request.method = "GET";
2111 request.request_headers = "";
2112
[email protected]4de4fb12009-08-03 22:11:182113 net_response_1.AssignTo(&mock_network_response); // Network mock.
2114 net_response_1.AssignTo(&request); // Expected result.
[email protected]bded84c2009-07-23 00:36:062115
2116 std::string response_headers;
2117 RunTransactionTestWithResponse(
2118 cache.http_cache(), request, &response_headers);
2119
[email protected]4de4fb12009-08-03 22:11:182120 EXPECT_EQ(net_response_1.status_and_headers(), response_headers);
[email protected]bded84c2009-07-23 00:36:062121 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2122 EXPECT_EQ(0, cache.disk_cache()->open_count());
2123 EXPECT_EQ(1, cache.disk_cache()->create_count());
2124
[email protected]6f40bf72009-07-23 17:52:372125 // Request |kUrl| a second time. Now |kNetResponse1| it is in the HTTP
[email protected]bded84c2009-07-23 00:36:062126 // cache, so we don't hit the network.
2127
[email protected]4de4fb12009-08-03 22:11:182128 request.load_flags = net::LOAD_ONLY_FROM_CACHE;
2129
[email protected]bded84c2009-07-23 00:36:062130 kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock.
[email protected]4de4fb12009-08-03 22:11:182131 net_response_1.AssignTo(&request); // Expected result.
[email protected]bded84c2009-07-23 00:36:062132
2133 RunTransactionTestWithResponse(
2134 cache.http_cache(), request, &response_headers);
2135
[email protected]4de4fb12009-08-03 22:11:182136 EXPECT_EQ(net_response_1.status_and_headers(), response_headers);
[email protected]bded84c2009-07-23 00:36:062137 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2138 EXPECT_EQ(1, cache.disk_cache()->open_count());
2139 EXPECT_EQ(1, cache.disk_cache()->create_count());
2140
2141 // Request |kUrl| yet again, but this time give the request an
2142 // "If-Modified-Since" header. This will cause the request to re-hit the
2143 // network. However now the network response is going to be
2144 // different -- this simulates a change made to the CSS file.
2145
[email protected]4de4fb12009-08-03 22:11:182146 request.request_headers = extra_request_headers;
2147 request.load_flags = net::LOAD_NORMAL;
[email protected]bded84c2009-07-23 00:36:062148
[email protected]4de4fb12009-08-03 22:11:182149 net_response_2.AssignTo(&mock_network_response); // Network mock.
2150 net_response_2.AssignTo(&request); // Expected result.
[email protected]bded84c2009-07-23 00:36:062151
2152 RunTransactionTestWithResponse(
2153 cache.http_cache(), request, &response_headers);
2154
[email protected]4de4fb12009-08-03 22:11:182155 EXPECT_EQ(net_response_2.status_and_headers(), response_headers);
[email protected]bded84c2009-07-23 00:36:062156 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2157 EXPECT_EQ(1, cache.disk_cache()->open_count());
2158 EXPECT_EQ(1, cache.disk_cache()->create_count());
2159
2160 // Finally, request |kUrl| again. This request should be serviced from
2161 // the cache. Moreover, the value in the cache should be |kNetResponse2|
2162 // and NOT |kNetResponse1|. The previous step should have replaced the
2163 // value in the cache with the modified response.
2164
2165 request.request_headers = "";
[email protected]4de4fb12009-08-03 22:11:182166 request.load_flags = net::LOAD_ONLY_FROM_CACHE;
[email protected]bded84c2009-07-23 00:36:062167
2168 kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock.
[email protected]4de4fb12009-08-03 22:11:182169 cached_response_2.AssignTo(&request); // Expected result.
[email protected]bded84c2009-07-23 00:36:062170
2171 RunTransactionTestWithResponse(
2172 cache.http_cache(), request, &response_headers);
2173
[email protected]4de4fb12009-08-03 22:11:182174 EXPECT_EQ(cached_response_2.status_and_headers(), response_headers);
[email protected]bded84c2009-07-23 00:36:062175 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2176 EXPECT_EQ(2, cache.disk_cache()->open_count());
2177 EXPECT_EQ(1, cache.disk_cache()->create_count());
2178
2179 RemoveMockTransaction(&mock_network_response);
2180}
2181
[email protected]4de4fb12009-08-03 22:11:182182// Check that when an "if-modified-since" header is attached
2183// to the request, the result still updates the cached entry.
2184TEST(HttpCache, ConditionalizedRequestUpdatesCache1) {
2185 // First network response for |kUrl|.
2186 static const Response kNetResponse1 = {
2187 "HTTP/1.1 200 OK",
2188 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2189 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2190 "body1"
2191 };
2192
2193 // Second network response for |kUrl|.
2194 static const Response kNetResponse2 = {
2195 "HTTP/1.1 200 OK",
2196 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2197 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
2198 "body2"
2199 };
2200
2201 const char* extra_headers =
2202 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\n";
2203
2204 ConditionalizedRequestUpdatesCacheHelper(
2205 kNetResponse1, kNetResponse2, kNetResponse2, extra_headers);
2206}
2207
2208// Check that when an "if-none-match" header is attached
2209// to the request, the result updates the cached entry.
2210TEST(HttpCache, ConditionalizedRequestUpdatesCache2) {
2211 // First network response for |kUrl|.
2212 static const Response kNetResponse1 = {
2213 "HTTP/1.1 200 OK",
2214 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2215 "Etag: \"ETAG1\"\n"
2216 "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire.
2217 "body1"
2218 };
2219
2220 // Second network response for |kUrl|.
2221 static const Response kNetResponse2 = {
2222 "HTTP/1.1 200 OK",
2223 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2224 "Etag: \"ETAG2\"\n"
2225 "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire.
2226 "body2"
2227 };
2228
2229 const char* extra_headers = "If-None-Match: \"ETAG1\"\n";
2230
2231 ConditionalizedRequestUpdatesCacheHelper(
2232 kNetResponse1, kNetResponse2, kNetResponse2, extra_headers);
2233}
2234
2235// Check that when an "if-modified-since" header is attached
2236// to a request, the 304 (not modified result) result updates the cached
2237// headers, and the 304 response is returned rather than the cached response.
2238TEST(HttpCache, ConditionalizedRequestUpdatesCache3) {
2239 // First network response for |kUrl|.
2240 static const Response kNetResponse1 = {
2241 "HTTP/1.1 200 OK",
2242 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2243 "Server: server1\n"
2244 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2245 "body1"
2246 };
2247
2248 // Second network response for |kUrl|.
2249 static const Response kNetResponse2 = {
2250 "HTTP/1.1 304 Not Modified",
2251 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2252 "Server: server2\n"
2253 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2254 ""
2255 };
2256
2257 static const Response kCachedResponse2 = {
2258 "HTTP/1.1 200 OK",
2259 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2260 "Server: server2\n"
2261 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2262 "body1"
2263 };
2264
2265 const char* extra_headers =
2266 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\n";
2267
2268 ConditionalizedRequestUpdatesCacheHelper(
2269 kNetResponse1, kNetResponse2, kCachedResponse2, extra_headers);
2270}
2271
2272// Test that when doing an externally conditionalized if-modified-since
2273// and there is no corresponding cache entry, a new cache entry is NOT
2274// created (304 response).
2275TEST(HttpCache, ConditionalizedRequestUpdatesCache4) {
2276 MockHttpCache cache;
2277
2278 const char* kUrl = "http://foobar.com/main.css";
2279
2280 static const Response kNetResponse = {
2281 "HTTP/1.1 304 Not Modified",
2282 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2283 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2284 ""
2285 };
2286
2287 const char* kExtraRequestHeaders =
[email protected]8c76ae22010-04-20 22:15:432288 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT";
[email protected]4de4fb12009-08-03 22:11:182289
2290 // We will control the network layer's responses for |kUrl| using
2291 // |mock_network_response|.
2292 MockTransaction mock_network_response = { 0 };
2293 mock_network_response.url = kUrl;
2294 AddMockTransaction(&mock_network_response);
2295
2296 MockTransaction request = { 0 };
2297 request.url = kUrl;
2298 request.method = "GET";
2299 request.request_headers = kExtraRequestHeaders;
2300
2301 kNetResponse.AssignTo(&mock_network_response); // Network mock.
2302 kNetResponse.AssignTo(&request); // Expected result.
2303
2304 std::string response_headers;
2305 RunTransactionTestWithResponse(
2306 cache.http_cache(), request, &response_headers);
2307
2308 EXPECT_EQ(kNetResponse.status_and_headers(), response_headers);
2309 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2310 EXPECT_EQ(0, cache.disk_cache()->open_count());
2311 EXPECT_EQ(0, cache.disk_cache()->create_count());
2312
2313 RemoveMockTransaction(&mock_network_response);
2314}
2315
2316// Test that when doing an externally conditionalized if-modified-since
2317// and there is no corresponding cache entry, a new cache entry is NOT
2318// created (200 response).
2319TEST(HttpCache, ConditionalizedRequestUpdatesCache5) {
2320 MockHttpCache cache;
2321
2322 const char* kUrl = "http://foobar.com/main.css";
2323
2324 static const Response kNetResponse = {
2325 "HTTP/1.1 200 OK",
2326 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2327 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2328 "foobar!!!"
2329 };
2330
2331 const char* kExtraRequestHeaders =
[email protected]8c76ae22010-04-20 22:15:432332 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT";
[email protected]4de4fb12009-08-03 22:11:182333
2334 // We will control the network layer's responses for |kUrl| using
2335 // |mock_network_response|.
2336 MockTransaction mock_network_response = { 0 };
2337 mock_network_response.url = kUrl;
2338 AddMockTransaction(&mock_network_response);
2339
2340 MockTransaction request = { 0 };
2341 request.url = kUrl;
2342 request.method = "GET";
2343 request.request_headers = kExtraRequestHeaders;
2344
2345 kNetResponse.AssignTo(&mock_network_response); // Network mock.
2346 kNetResponse.AssignTo(&request); // Expected result.
2347
2348 std::string response_headers;
2349 RunTransactionTestWithResponse(
2350 cache.http_cache(), request, &response_headers);
2351
2352 EXPECT_EQ(kNetResponse.status_and_headers(), response_headers);
2353 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2354 EXPECT_EQ(0, cache.disk_cache()->open_count());
2355 EXPECT_EQ(0, cache.disk_cache()->create_count());
2356
2357 RemoveMockTransaction(&mock_network_response);
2358}
2359
2360// Test that when doing an externally conditionalized if-modified-since
2361// if the date does not match the cache entry's last-modified date,
2362// then we do NOT use the response (304) to update the cache.
2363// (the if-modified-since date is 2 days AFTER the cache's modification date).
2364TEST(HttpCache, ConditionalizedRequestUpdatesCache6) {
2365 static const Response kNetResponse1 = {
2366 "HTTP/1.1 200 OK",
2367 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2368 "Server: server1\n"
2369 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2370 "body1"
2371 };
2372
2373 // Second network response for |kUrl|.
2374 static const Response kNetResponse2 = {
2375 "HTTP/1.1 304 Not Modified",
2376 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2377 "Server: server2\n"
2378 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2379 ""
2380 };
2381
2382 // This is two days in the future from the original response's last-modified
2383 // date!
2384 const char* kExtraRequestHeaders =
2385 "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\n";
2386
2387 ConditionalizedRequestUpdatesCacheHelper(
2388 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
2389}
2390
2391// Test that when doing an externally conditionalized if-none-match
2392// if the etag does not match the cache entry's etag, then we do not use the
2393// response (304) to update the cache.
2394TEST(HttpCache, ConditionalizedRequestUpdatesCache7) {
2395 static const Response kNetResponse1 = {
2396 "HTTP/1.1 200 OK",
2397 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2398 "Etag: \"Foo1\"\n"
2399 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2400 "body1"
2401 };
2402
2403 // Second network response for |kUrl|.
2404 static const Response kNetResponse2 = {
2405 "HTTP/1.1 304 Not Modified",
2406 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2407 "Etag: \"Foo2\"\n"
2408 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2409 ""
2410 };
2411
2412 // Different etag from original response.
2413 const char* kExtraRequestHeaders = "If-None-Match: \"Foo2\"\n";
2414
2415 ConditionalizedRequestUpdatesCacheHelper(
2416 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
2417}
2418
[email protected]f2ee7452009-11-02 21:43:022419// Test that doing an externally conditionalized request with both if-none-match
2420// and if-modified-since updates the cache.
2421TEST(HttpCache, ConditionalizedRequestUpdatesCache8) {
2422 static const Response kNetResponse1 = {
2423 "HTTP/1.1 200 OK",
2424 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2425 "Etag: \"Foo1\"\n"
2426 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2427 "body1"
2428 };
2429
2430 // Second network response for |kUrl|.
2431 static const Response kNetResponse2 = {
2432 "HTTP/1.1 200 OK",
2433 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2434 "Etag: \"Foo2\"\n"
2435 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
2436 "body2"
2437 };
2438
2439 const char* kExtraRequestHeaders =
[email protected]2227c692010-05-04 15:36:112440 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"
2441 "If-None-Match: \"Foo1\"\r\n";
[email protected]f2ee7452009-11-02 21:43:022442
2443 ConditionalizedRequestUpdatesCacheHelper(
2444 kNetResponse1, kNetResponse2, kNetResponse2, kExtraRequestHeaders);
2445}
2446
2447// Test that doing an externally conditionalized request with both if-none-match
2448// and if-modified-since does not update the cache with only one match.
2449TEST(HttpCache, ConditionalizedRequestUpdatesCache9) {
2450 static const Response kNetResponse1 = {
2451 "HTTP/1.1 200 OK",
2452 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2453 "Etag: \"Foo1\"\n"
2454 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2455 "body1"
2456 };
2457
2458 // Second network response for |kUrl|.
2459 static const Response kNetResponse2 = {
2460 "HTTP/1.1 200 OK",
2461 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2462 "Etag: \"Foo2\"\n"
2463 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
2464 "body2"
2465 };
2466
2467 // The etag doesn't match what we have stored.
2468 const char* kExtraRequestHeaders =
[email protected]2227c692010-05-04 15:36:112469 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\n"
2470 "If-None-Match: \"Foo2\"\n";
[email protected]f2ee7452009-11-02 21:43:022471
2472 ConditionalizedRequestUpdatesCacheHelper(
2473 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
2474}
2475
2476// Test that doing an externally conditionalized request with both if-none-match
2477// and if-modified-since does not update the cache with only one match.
2478TEST(HttpCache, ConditionalizedRequestUpdatesCache10) {
2479 static const Response kNetResponse1 = {
2480 "HTTP/1.1 200 OK",
2481 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2482 "Etag: \"Foo1\"\n"
2483 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2484 "body1"
2485 };
2486
2487 // Second network response for |kUrl|.
2488 static const Response kNetResponse2 = {
2489 "HTTP/1.1 200 OK",
2490 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2491 "Etag: \"Foo2\"\n"
2492 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
2493 "body2"
2494 };
2495
2496 // The modification date doesn't match what we have stored.
2497 const char* kExtraRequestHeaders =
[email protected]2227c692010-05-04 15:36:112498 "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\n"
2499 "If-None-Match: \"Foo1\"\n";
[email protected]f2ee7452009-11-02 21:43:022500
2501 ConditionalizedRequestUpdatesCacheHelper(
2502 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
2503}
2504
2505// Test that doing an externally conditionalized request with two conflicting
2506// headers does not update the cache.
2507TEST(HttpCache, ConditionalizedRequestUpdatesCache11) {
2508 static const Response kNetResponse1 = {
2509 "HTTP/1.1 200 OK",
2510 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2511 "Etag: \"Foo1\"\n"
2512 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2513 "body1"
2514 };
2515
2516 // Second network response for |kUrl|.
2517 static const Response kNetResponse2 = {
2518 "HTTP/1.1 200 OK",
2519 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2520 "Etag: \"Foo2\"\n"
2521 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
2522 "body2"
2523 };
2524
2525 // Two dates, the second matches what we have stored.
2526 const char* kExtraRequestHeaders =
[email protected]2227c692010-05-04 15:36:112527 "If-Modified-Since: Mon, 04 Feb 2008 22:38:21 GMT\n"
2528 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\n";
[email protected]f2ee7452009-11-02 21:43:022529
2530 ConditionalizedRequestUpdatesCacheHelper(
2531 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
2532}
2533
[email protected]6f40bf72009-07-23 17:52:372534TEST(HttpCache, UrlContainingHash) {
2535 MockHttpCache cache;
2536
2537 // Do a typical GET request -- should write an entry into our cache.
2538 MockTransaction trans(kTypicalGET_Transaction);
2539 RunTransactionTest(cache.http_cache(), trans);
2540
2541 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2542 EXPECT_EQ(0, cache.disk_cache()->open_count());
2543 EXPECT_EQ(1, cache.disk_cache()->create_count());
2544
2545 // Request the same URL, but this time with a reference section (hash).
2546 // Since the cache key strips the hash sections, this should be a cache hit.
2547 std::string url_with_hash = std::string(trans.url) + "#multiple#hashes";
2548 trans.url = url_with_hash.c_str();
2549 trans.load_flags = net::LOAD_ONLY_FROM_CACHE;
2550
2551 RunTransactionTest(cache.http_cache(), trans);
2552
2553 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2554 EXPECT_EQ(1, cache.disk_cache()->open_count());
2555 EXPECT_EQ(1, cache.disk_cache()->create_count());
2556}
2557
initial.commit586acc5fe2008-07-26 22:42:522558TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) {
2559 MockHttpCache cache;
2560
2561 // Test that we skip the cache for POST requests. Eventually, we will want
2562 // to cache these, but we'll still have cases where skipping the cache makes
2563 // sense, so we want to make sure that it works properly.
2564
2565 MockTransaction transaction(kSimplePOST_Transaction);
2566 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
2567
2568 MockHttpRequest request(transaction);
2569 TestCompletionCallback callback;
2570
[email protected]1638d602009-09-24 03:49:172571 scoped_ptr<net::HttpTransaction> trans;
2572 int rv = cache.http_cache()->CreateTransaction(&trans);
2573 EXPECT_EQ(net::OK, rv);
[email protected]af4876d2008-10-21 23:10:572574 ASSERT_TRUE(trans.get());
initial.commit586acc5fe2008-07-26 22:42:522575
[email protected]5a1d7ca2010-04-28 20:12:272576 rv = trans->Start(&request, &callback, net::BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:522577 if (rv == net::ERR_IO_PENDING)
2578 rv = callback.WaitForResult();
2579 ASSERT_EQ(net::ERR_CACHE_MISS, rv);
2580
[email protected]af4876d2008-10-21 23:10:572581 trans.reset();
initial.commit586acc5fe2008-07-26 22:42:522582
2583 EXPECT_EQ(0, cache.network_layer()->transaction_count());
2584 EXPECT_EQ(0, cache.disk_cache()->open_count());
2585 EXPECT_EQ(0, cache.disk_cache()->create_count());
2586}
2587
[email protected]96bac982009-03-24 18:20:062588TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Hit) {
2589 MockHttpCache cache;
2590
2591 // Test that we hit the cache for POST requests.
2592
2593 MockTransaction transaction(kSimplePOST_Transaction);
2594
2595 const int64 kUploadId = 1; // Just a dummy value.
2596
2597 MockHttpRequest request(transaction);
2598 request.upload_data = new net::UploadData();
2599 request.upload_data->set_identifier(kUploadId);
2600 request.upload_data->AppendBytes("hello", 5);
2601
2602 // Populate the cache.
[email protected]95792eb12009-06-22 21:30:402603 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL);
[email protected]96bac982009-03-24 18:20:062604
2605 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2606 EXPECT_EQ(0, cache.disk_cache()->open_count());
2607 EXPECT_EQ(1, cache.disk_cache()->create_count());
2608
2609 // Load from cache.
2610 request.load_flags |= net::LOAD_ONLY_FROM_CACHE;
[email protected]95792eb12009-06-22 21:30:402611 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL);
[email protected]96bac982009-03-24 18:20:062612
2613 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2614 EXPECT_EQ(1, cache.disk_cache()->open_count());
2615 EXPECT_EQ(1, cache.disk_cache()->create_count());
2616}
2617
initial.commit586acc5fe2008-07-26 22:42:522618TEST(HttpCache, RangeGET_SkipsCache) {
2619 MockHttpCache cache;
2620
[email protected]8bf26f49a2009-06-12 17:35:502621 // Test that we skip the cache for range GET requests. Eventually, we will
2622 // want to cache these, but we'll still have cases where skipping the cache
2623 // makes sense, so we want to make sure that it works properly.
initial.commit586acc5fe2008-07-26 22:42:522624
2625 RunTransactionTest(cache.http_cache(), kRangeGET_Transaction);
2626
2627 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2628 EXPECT_EQ(0, cache.disk_cache()->open_count());
2629 EXPECT_EQ(0, cache.disk_cache()->create_count());
2630
2631 MockTransaction transaction(kSimpleGET_Transaction);
2632 transaction.request_headers = "If-None-Match: foo";
2633 RunTransactionTest(cache.http_cache(), transaction);
2634
2635 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2636 EXPECT_EQ(0, cache.disk_cache()->open_count());
2637 EXPECT_EQ(0, cache.disk_cache()->create_count());
2638
[email protected]72d1e592009-03-10 17:39:462639 transaction.request_headers =
2640 "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT";
initial.commit586acc5fe2008-07-26 22:42:522641 RunTransactionTest(cache.http_cache(), transaction);
2642
2643 EXPECT_EQ(3, cache.network_layer()->transaction_count());
2644 EXPECT_EQ(0, cache.disk_cache()->open_count());
2645 EXPECT_EQ(0, cache.disk_cache()->create_count());
2646}
2647
[email protected]86291440d2009-08-28 18:46:352648// Test that we skip the cache for range requests that include a validation
2649// header.
2650TEST(HttpCache, RangeGET_SkipsCache2) {
2651 MockHttpCache cache;
2652 cache.http_cache()->set_enable_range_support(true);
2653
2654 MockTransaction transaction(kRangeGET_Transaction);
[email protected]8c76ae22010-04-20 22:15:432655 transaction.request_headers = "If-None-Match: foo\r\n"
[email protected]e75e8af2009-11-03 00:04:202656 EXTRA_HEADER
[email protected]8c76ae22010-04-20 22:15:432657 "\r\nRange: bytes = 40-49";
[email protected]86291440d2009-08-28 18:46:352658 RunTransactionTest(cache.http_cache(), transaction);
2659
2660 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2661 EXPECT_EQ(0, cache.disk_cache()->open_count());
2662 EXPECT_EQ(0, cache.disk_cache()->create_count());
2663
2664 transaction.request_headers =
[email protected]8c76ae22010-04-20 22:15:432665 "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n"
[email protected]e75e8af2009-11-03 00:04:202666 EXTRA_HEADER
[email protected]8c76ae22010-04-20 22:15:432667 "\r\nRange: bytes = 40-49";
[email protected]86291440d2009-08-28 18:46:352668 RunTransactionTest(cache.http_cache(), transaction);
2669
2670 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2671 EXPECT_EQ(0, cache.disk_cache()->open_count());
2672 EXPECT_EQ(0, cache.disk_cache()->create_count());
2673
[email protected]8c76ae22010-04-20 22:15:432674 transaction.request_headers = "If-Range: bla\r\n"
[email protected]e75e8af2009-11-03 00:04:202675 EXTRA_HEADER
[email protected]8c76ae22010-04-20 22:15:432676 "\r\nRange: bytes = 40-49\n";
[email protected]86291440d2009-08-28 18:46:352677 RunTransactionTest(cache.http_cache(), transaction);
2678
2679 EXPECT_EQ(3, cache.network_layer()->transaction_count());
2680 EXPECT_EQ(0, cache.disk_cache()->open_count());
2681 EXPECT_EQ(0, cache.disk_cache()->create_count());
2682}
2683
[email protected]e5dad132009-08-18 00:53:412684// Tests that receiving 206 for a regular request is handled correctly.
[email protected]7ee4c4072009-06-30 18:49:472685TEST(HttpCache, GET_Crazy206) {
2686 MockHttpCache cache;
[email protected]21f659d2009-08-24 17:59:312687 cache.http_cache()->set_enable_range_support(true);
[email protected]7ee4c4072009-06-30 18:49:472688
[email protected]7ee4c4072009-06-30 18:49:472689 // Write to the cache.
2690 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]44f873a62009-08-12 00:14:482691 AddMockTransaction(&transaction);
[email protected]e75e8af2009-11-03 00:04:202692 transaction.request_headers = EXTRA_HEADER;
[email protected]44f873a62009-08-12 00:14:482693 transaction.handler = NULL;
[email protected]7ee4c4072009-06-30 18:49:472694 RunTransactionTest(cache.http_cache(), transaction);
2695
2696 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2697 EXPECT_EQ(0, cache.disk_cache()->open_count());
2698 EXPECT_EQ(1, cache.disk_cache()->create_count());
2699
2700 // This should read again from the net.
2701 RunTransactionTest(cache.http_cache(), transaction);
2702
2703 EXPECT_EQ(2, cache.network_layer()->transaction_count());
[email protected]21f659d2009-08-24 17:59:312704 EXPECT_EQ(0, cache.disk_cache()->open_count());
2705 EXPECT_EQ(2, cache.disk_cache()->create_count());
[email protected]44f873a62009-08-12 00:14:482706 RemoveMockTransaction(&transaction);
[email protected]7ee4c4072009-06-30 18:49:472707}
2708
[email protected]e5dad132009-08-18 00:53:412709// Tests that we can cache range requests and fetch random blocks from the
2710// cache and the network.
[email protected]21f659d2009-08-24 17:59:312711TEST(HttpCache, RangeGET_OK) {
[email protected]8bf26f49a2009-06-12 17:35:502712 MockHttpCache cache;
[email protected]21f659d2009-08-24 17:59:312713 cache.http_cache()->set_enable_range_support(true);
[email protected]8bf26f49a2009-06-12 17:35:502714 AddMockTransaction(&kRangeGET_TransactionOK);
[email protected]95792eb12009-06-22 21:30:402715 std::string headers;
[email protected]8bf26f49a2009-06-12 17:35:502716
[email protected]95792eb12009-06-22 21:30:402717 // Write to the cache (40-49).
2718 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
2719 &headers);
2720
[email protected]8c76ae22010-04-20 22:15:432721 Verify206Response(headers, 40, 49);
[email protected]8bf26f49a2009-06-12 17:35:502722 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2723 EXPECT_EQ(0, cache.disk_cache()->open_count());
2724 EXPECT_EQ(1, cache.disk_cache()->create_count());
2725
2726 // Read from the cache (40-49).
[email protected]95792eb12009-06-22 21:30:402727 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
2728 &headers);
[email protected]8bf26f49a2009-06-12 17:35:502729
[email protected]8c76ae22010-04-20 22:15:432730 Verify206Response(headers, 40, 49);
[email protected]5beca812010-06-24 17:55:242731 EXPECT_EQ(1, cache.network_layer()->transaction_count());
[email protected]8bf26f49a2009-06-12 17:35:502732 EXPECT_EQ(1, cache.disk_cache()->open_count());
2733 EXPECT_EQ(1, cache.disk_cache()->create_count());
2734
2735 // Make sure we are done with the previous transaction.
2736 MessageLoop::current()->RunAllPending();
2737
2738 // Write to the cache (30-39).
2739 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]e75e8af2009-11-03 00:04:202740 transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER;
[email protected]8bf26f49a2009-06-12 17:35:502741 transaction.data = "rg: 30-39 ";
[email protected]95792eb12009-06-22 21:30:402742 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
[email protected]8bf26f49a2009-06-12 17:35:502743
[email protected]8c76ae22010-04-20 22:15:432744 Verify206Response(headers, 30, 39);
[email protected]5beca812010-06-24 17:55:242745 EXPECT_EQ(2, cache.network_layer()->transaction_count());
[email protected]8bf26f49a2009-06-12 17:35:502746 EXPECT_EQ(2, cache.disk_cache()->open_count());
2747 EXPECT_EQ(1, cache.disk_cache()->create_count());
2748
2749 // Make sure we are done with the previous transaction.
2750 MessageLoop::current()->RunAllPending();
2751
2752 // Write and read from the cache (20-59).
[email protected]e75e8af2009-11-03 00:04:202753 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER;
[email protected]8bf26f49a2009-06-12 17:35:502754 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 ";
[email protected]95792eb12009-06-22 21:30:402755 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
[email protected]8bf26f49a2009-06-12 17:35:502756
[email protected]8c76ae22010-04-20 22:15:432757 Verify206Response(headers, 20, 59);
[email protected]5beca812010-06-24 17:55:242758 EXPECT_EQ(4, cache.network_layer()->transaction_count());
[email protected]8bf26f49a2009-06-12 17:35:502759 EXPECT_EQ(3, cache.disk_cache()->open_count());
2760 EXPECT_EQ(1, cache.disk_cache()->create_count());
2761
2762 RemoveMockTransaction(&kRangeGET_TransactionOK);
2763}
2764
[email protected]21e743202009-12-18 01:31:042765// Tests that we can cache range requests and fetch random blocks from the
2766// cache and the network, with synchronous responses.
2767TEST(HttpCache, RangeGET_SyncOK) {
2768 MockHttpCache cache;
2769 cache.http_cache()->set_enable_range_support(true);
2770
2771 MockTransaction transaction(kRangeGET_TransactionOK);
2772 transaction.test_mode = TEST_MODE_SYNC_ALL;
2773 AddMockTransaction(&transaction);
2774
2775 // Write to the cache (40-49).
2776 std::string headers;
2777 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2778
[email protected]8c76ae22010-04-20 22:15:432779 Verify206Response(headers, 40, 49);
[email protected]21e743202009-12-18 01:31:042780 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2781 EXPECT_EQ(0, cache.disk_cache()->open_count());
2782 EXPECT_EQ(1, cache.disk_cache()->create_count());
2783
2784 // Read from the cache (40-49).
2785 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2786
[email protected]8c76ae22010-04-20 22:15:432787 Verify206Response(headers, 40, 49);
[email protected]5beca812010-06-24 17:55:242788 EXPECT_EQ(1, cache.network_layer()->transaction_count());
[email protected]21e743202009-12-18 01:31:042789 EXPECT_EQ(0, cache.disk_cache()->open_count());
2790 EXPECT_EQ(1, cache.disk_cache()->create_count());
2791
2792 // Make sure we are done with the previous transaction.
2793 MessageLoop::current()->RunAllPending();
2794
2795 // Write to the cache (30-39).
2796 transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER;
2797 transaction.data = "rg: 30-39 ";
2798 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2799
[email protected]8c76ae22010-04-20 22:15:432800 Verify206Response(headers, 30, 39);
[email protected]5beca812010-06-24 17:55:242801 EXPECT_EQ(2, cache.network_layer()->transaction_count());
[email protected]21e743202009-12-18 01:31:042802 EXPECT_EQ(1, cache.disk_cache()->open_count());
2803 EXPECT_EQ(1, cache.disk_cache()->create_count());
2804
2805 // Make sure we are done with the previous transaction.
2806 MessageLoop::current()->RunAllPending();
2807
2808 // Write and read from the cache (20-59).
2809 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER;
2810 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 ";
2811 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2812
[email protected]8c76ae22010-04-20 22:15:432813 Verify206Response(headers, 20, 59);
[email protected]5beca812010-06-24 17:55:242814 EXPECT_EQ(4, cache.network_layer()->transaction_count());
[email protected]21e743202009-12-18 01:31:042815 EXPECT_EQ(2, cache.disk_cache()->open_count());
2816 EXPECT_EQ(1, cache.disk_cache()->create_count());
2817
2818 RemoveMockTransaction(&transaction);
2819}
2820
[email protected]5beca812010-06-24 17:55:242821// Tests that we don't revalidate an entry unless we are required to do so.
2822TEST(HttpCache, RangeGET_Revalidate1) {
2823 MockHttpCache cache;
2824 cache.http_cache()->set_enable_range_support(true);
2825 std::string headers;
2826
2827 // Write to the cache (40-49).
2828 MockTransaction transaction(kRangeGET_TransactionOK);
2829 transaction.response_headers =
2830 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
2831 "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n" // Should never expire.
2832 "ETag: \"foo\"\n"
2833 "Accept-Ranges: bytes\n"
2834 "Content-Length: 10\n";
2835 AddMockTransaction(&transaction);
2836 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2837
2838 Verify206Response(headers, 40, 49);
2839 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2840 EXPECT_EQ(0, cache.disk_cache()->open_count());
2841 EXPECT_EQ(1, cache.disk_cache()->create_count());
2842
2843 // Read from the cache (40-49).
2844 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2845 Verify206Response(headers, 40, 49);
2846
2847 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2848 EXPECT_EQ(1, cache.disk_cache()->open_count());
2849 EXPECT_EQ(1, cache.disk_cache()->create_count());
2850
2851 // Read again forcing the revalidation.
2852 transaction.load_flags |= net::LOAD_VALIDATE_CACHE;
2853 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2854
2855 Verify206Response(headers, 40, 49);
2856 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2857 EXPECT_EQ(1, cache.disk_cache()->open_count());
2858 EXPECT_EQ(1, cache.disk_cache()->create_count());
2859
2860 RemoveMockTransaction(&transaction);
2861}
2862
2863// Checks that we revalidate an entry when the headers say so.
2864TEST(HttpCache, RangeGET_Revalidate2) {
2865 MockHttpCache cache;
2866 cache.http_cache()->set_enable_range_support(true);
2867 std::string headers;
2868
2869 // Write to the cache (40-49).
2870 MockTransaction transaction(kRangeGET_TransactionOK);
2871 transaction.response_headers =
2872 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
2873 "Expires: Sat, 18 Apr 2009 01:10:43 GMT\n" // Expired.
2874 "ETag: \"foo\"\n"
2875 "Accept-Ranges: bytes\n"
2876 "Content-Length: 10\n";
2877 AddMockTransaction(&transaction);
2878 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2879
2880 Verify206Response(headers, 40, 49);
2881 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2882 EXPECT_EQ(0, cache.disk_cache()->open_count());
2883 EXPECT_EQ(1, cache.disk_cache()->create_count());
2884
2885 // Read from the cache (40-49).
2886 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2887 Verify206Response(headers, 40, 49);
2888
2889 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2890 EXPECT_EQ(1, cache.disk_cache()->open_count());
2891 EXPECT_EQ(1, cache.disk_cache()->create_count());
2892
2893 RemoveMockTransaction(&transaction);
2894}
2895
[email protected]e5dad132009-08-18 00:53:412896// Tests that we deal with 304s for range requests.
[email protected]21f659d2009-08-24 17:59:312897TEST(HttpCache, RangeGET_304) {
[email protected]e5dad132009-08-18 00:53:412898 MockHttpCache cache;
[email protected]21f659d2009-08-24 17:59:312899 cache.http_cache()->set_enable_range_support(true);
[email protected]e5dad132009-08-18 00:53:412900 AddMockTransaction(&kRangeGET_TransactionOK);
2901 std::string headers;
2902
2903 // Write to the cache (40-49).
2904 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
2905 &headers);
2906
[email protected]8c76ae22010-04-20 22:15:432907 Verify206Response(headers, 40, 49);
[email protected]e5dad132009-08-18 00:53:412908 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2909 EXPECT_EQ(0, cache.disk_cache()->open_count());
2910 EXPECT_EQ(1, cache.disk_cache()->create_count());
2911
2912 // Read from the cache (40-49).
2913 RangeTransactionServer handler;
2914 handler.set_not_modified(true);
[email protected]5beca812010-06-24 17:55:242915 MockTransaction transaction(kRangeGET_TransactionOK);
2916 transaction.load_flags |= net::LOAD_VALIDATE_CACHE;
2917 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
[email protected]e5dad132009-08-18 00:53:412918
[email protected]8c76ae22010-04-20 22:15:432919 Verify206Response(headers, 40, 49);
[email protected]e5dad132009-08-18 00:53:412920 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2921 EXPECT_EQ(1, cache.disk_cache()->open_count());
2922 EXPECT_EQ(1, cache.disk_cache()->create_count());
2923
2924 RemoveMockTransaction(&kRangeGET_TransactionOK);
2925}
2926
[email protected]a79837892009-08-20 21:18:292927// Tests that we deal with 206s when revalidating range requests.
[email protected]21f659d2009-08-24 17:59:312928TEST(HttpCache, RangeGET_ModifiedResult) {
[email protected]a79837892009-08-20 21:18:292929 MockHttpCache cache;
[email protected]21f659d2009-08-24 17:59:312930 cache.http_cache()->set_enable_range_support(true);
[email protected]a79837892009-08-20 21:18:292931 AddMockTransaction(&kRangeGET_TransactionOK);
2932 std::string headers;
2933
2934 // Write to the cache (40-49).
2935 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
2936 &headers);
2937
[email protected]8c76ae22010-04-20 22:15:432938 Verify206Response(headers, 40, 49);
[email protected]a79837892009-08-20 21:18:292939 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2940 EXPECT_EQ(0, cache.disk_cache()->open_count());
2941 EXPECT_EQ(1, cache.disk_cache()->create_count());
2942
2943 // Attempt to read from the cache (40-49).
2944 RangeTransactionServer handler;
2945 handler.set_modified(true);
[email protected]5beca812010-06-24 17:55:242946 MockTransaction transaction(kRangeGET_TransactionOK);
2947 transaction.load_flags |= net::LOAD_VALIDATE_CACHE;
2948 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
[email protected]a79837892009-08-20 21:18:292949
[email protected]8c76ae22010-04-20 22:15:432950 Verify206Response(headers, 40, 49);
[email protected]a79837892009-08-20 21:18:292951 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2952 EXPECT_EQ(1, cache.disk_cache()->open_count());
2953 EXPECT_EQ(1, cache.disk_cache()->create_count());
2954
2955 // And the entry should be gone.
2956 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
2957 EXPECT_EQ(3, cache.network_layer()->transaction_count());
2958 EXPECT_EQ(1, cache.disk_cache()->open_count());
2959 EXPECT_EQ(2, cache.disk_cache()->create_count());
2960
2961 RemoveMockTransaction(&kRangeGET_TransactionOK);
2962}
2963
[email protected]e5dad132009-08-18 00:53:412964// Tests that we can cache range requests when the start or end is unknown.
2965// We start with one suffix request, followed by a request from a given point.
[email protected]21f659d2009-08-24 17:59:312966TEST(HttpCache, UnknownRangeGET_1) {
[email protected]67fe45c2009-06-24 17:44:572967 MockHttpCache cache;
[email protected]21f659d2009-08-24 17:59:312968 cache.http_cache()->set_enable_range_support(true);
[email protected]67fe45c2009-06-24 17:44:572969 AddMockTransaction(&kRangeGET_TransactionOK);
[email protected]67fe45c2009-06-24 17:44:572970 std::string headers;
2971
2972 // Write to the cache (70-79).
2973 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]e75e8af2009-11-03 00:04:202974 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
[email protected]67fe45c2009-06-24 17:44:572975 transaction.data = "rg: 70-79 ";
2976 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2977
[email protected]8c76ae22010-04-20 22:15:432978 Verify206Response(headers, 70, 79);
[email protected]67fe45c2009-06-24 17:44:572979 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2980 EXPECT_EQ(0, cache.disk_cache()->open_count());
2981 EXPECT_EQ(1, cache.disk_cache()->create_count());
2982
2983 // Make sure we are done with the previous transaction.
2984 MessageLoop::current()->RunAllPending();
2985
2986 // Write and read from the cache (60-79).
[email protected]e75e8af2009-11-03 00:04:202987 transaction.request_headers = "Range: bytes = 60-\r\n" EXTRA_HEADER;
[email protected]67fe45c2009-06-24 17:44:572988 transaction.data = "rg: 60-69 rg: 70-79 ";
2989 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2990
[email protected]8c76ae22010-04-20 22:15:432991 Verify206Response(headers, 60, 79);
[email protected]44f873a62009-08-12 00:14:482992 EXPECT_EQ(2, cache.network_layer()->transaction_count());
[email protected]67fe45c2009-06-24 17:44:572993 EXPECT_EQ(1, cache.disk_cache()->open_count());
2994 EXPECT_EQ(1, cache.disk_cache()->create_count());
2995
2996 RemoveMockTransaction(&kRangeGET_TransactionOK);
2997}
2998
[email protected]e5dad132009-08-18 00:53:412999// Tests that we can cache range requests when the start or end is unknown.
3000// We start with one request from a given point, followed by a suffix request.
3001// We'll also verify that synchronous cache responses work as intended.
[email protected]21f659d2009-08-24 17:59:313002TEST(HttpCache, UnknownRangeGET_2) {
[email protected]67fe45c2009-06-24 17:44:573003 MockHttpCache cache;
[email protected]21f659d2009-08-24 17:59:313004 cache.http_cache()->set_enable_range_support(true);
[email protected]67fe45c2009-06-24 17:44:573005 std::string headers;
3006
[email protected]67fe45c2009-06-24 17:44:573007 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]44f873a62009-08-12 00:14:483008 transaction.test_mode = TEST_MODE_SYNC_CACHE_START |
[email protected]73cae572009-10-22 18:36:193009 TEST_MODE_SYNC_CACHE_READ |
3010 TEST_MODE_SYNC_CACHE_WRITE;
[email protected]44f873a62009-08-12 00:14:483011 AddMockTransaction(&transaction);
3012
3013 // Write to the cache (70-79).
[email protected]e75e8af2009-11-03 00:04:203014 transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER;
[email protected]67fe45c2009-06-24 17:44:573015 transaction.data = "rg: 70-79 ";
3016 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3017
[email protected]8c76ae22010-04-20 22:15:433018 Verify206Response(headers, 70, 79);
[email protected]67fe45c2009-06-24 17:44:573019 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3020 EXPECT_EQ(0, cache.disk_cache()->open_count());
3021 EXPECT_EQ(1, cache.disk_cache()->create_count());
3022
3023 // Make sure we are done with the previous transaction.
3024 MessageLoop::current()->RunAllPending();
3025
3026 // Write and read from the cache (60-79).
[email protected]e75e8af2009-11-03 00:04:203027 transaction.request_headers = "Range: bytes = -20\r\n" EXTRA_HEADER;
[email protected]67fe45c2009-06-24 17:44:573028 transaction.data = "rg: 60-69 rg: 70-79 ";
3029 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3030
[email protected]8c76ae22010-04-20 22:15:433031 Verify206Response(headers, 60, 79);
[email protected]44f873a62009-08-12 00:14:483032 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3033 EXPECT_EQ(1, cache.disk_cache()->open_count());
3034 EXPECT_EQ(1, cache.disk_cache()->create_count());
3035
3036 RemoveMockTransaction(&transaction);
3037}
3038
[email protected]e5dad132009-08-18 00:53:413039// Tests that receiving Not Modified when asking for an open range doesn't mess
3040// up things.
[email protected]21f659d2009-08-24 17:59:313041TEST(HttpCache, UnknownRangeGET_304) {
[email protected]e5dad132009-08-18 00:53:413042 MockHttpCache cache;
[email protected]21f659d2009-08-24 17:59:313043 cache.http_cache()->set_enable_range_support(true);
[email protected]e5dad132009-08-18 00:53:413044 std::string headers;
3045
3046 MockTransaction transaction(kRangeGET_TransactionOK);
3047 AddMockTransaction(&transaction);
3048
3049 RangeTransactionServer handler;
3050 handler.set_not_modified(true);
3051
3052 // Ask for the end of the file, without knowing the length.
[email protected]e75e8af2009-11-03 00:04:203053 transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER;
[email protected]e5dad132009-08-18 00:53:413054 transaction.data = "rg: 70-79 ";
3055 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3056
3057 // We just bypass the cache.
3058 EXPECT_EQ(0U, headers.find("HTTP/1.1 304 Not Modified\n"));
3059 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3060 EXPECT_EQ(0, cache.disk_cache()->open_count());
3061 EXPECT_EQ(1, cache.disk_cache()->create_count());
3062
3063 RunTransactionTest(cache.http_cache(), transaction);
3064 EXPECT_EQ(2, cache.disk_cache()->create_count());
3065
3066 RemoveMockTransaction(&transaction);
3067}
3068
3069// Tests that we can handle non-range requests when we have cached a range.
[email protected]21f659d2009-08-24 17:59:313070TEST(HttpCache, GET_Previous206) {
[email protected]44f873a62009-08-12 00:14:483071 MockHttpCache cache;
[email protected]21f659d2009-08-24 17:59:313072 cache.http_cache()->set_enable_range_support(true);
[email protected]44f873a62009-08-12 00:14:483073 AddMockTransaction(&kRangeGET_TransactionOK);
[email protected]44f873a62009-08-12 00:14:483074 std::string headers;
3075
3076 // Write to the cache (40-49).
3077 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
3078 &headers);
3079
[email protected]8c76ae22010-04-20 22:15:433080 Verify206Response(headers, 40, 49);
[email protected]44f873a62009-08-12 00:14:483081 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3082 EXPECT_EQ(0, cache.disk_cache()->open_count());
3083 EXPECT_EQ(1, cache.disk_cache()->create_count());
3084
3085 // Write and read from the cache (0-79), when not asked for a range.
3086 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]e75e8af2009-11-03 00:04:203087 transaction.request_headers = EXTRA_HEADER;
[email protected]44f873a62009-08-12 00:14:483088 transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
3089 "rg: 50-59 rg: 60-69 rg: 70-79 ";
3090 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3091
3092 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
[email protected]67fe45c2009-06-24 17:44:573093 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3094 EXPECT_EQ(1, cache.disk_cache()->open_count());
3095 EXPECT_EQ(1, cache.disk_cache()->create_count());
3096
3097 RemoveMockTransaction(&kRangeGET_TransactionOK);
3098}
3099
[email protected]d9adff2c2009-09-05 01:15:453100// Tests that we can handle non-range requests when we have cached the first
3101// part of the object and server replies with 304 (Not Modified).
3102TEST(HttpCache, GET_Previous206_NotModified) {
3103 MockHttpCache cache;
3104 cache.http_cache()->set_enable_range_support(true);
3105
3106 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]e75e8af2009-11-03 00:04:203107 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
[email protected]d9adff2c2009-09-05 01:15:453108 transaction.data = "rg: 00-09 ";
3109 AddMockTransaction(&transaction);
3110 std::string headers;
3111
3112 // Write to the cache (0-9).
3113 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3114
[email protected]8c76ae22010-04-20 22:15:433115 Verify206Response(headers, 0, 9);
[email protected]d9adff2c2009-09-05 01:15:453116 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3117 EXPECT_EQ(0, cache.disk_cache()->open_count());
3118 EXPECT_EQ(1, cache.disk_cache()->create_count());
3119
3120 // Read from the cache (0-9), write and read from cache (10 - 79),
3121 MockTransaction transaction2(kRangeGET_TransactionOK);
[email protected]5beca812010-06-24 17:55:243122 transaction2.load_flags |= net::LOAD_VALIDATE_CACHE;
[email protected]e75e8af2009-11-03 00:04:203123 transaction2.request_headers = "Foo: bar\r\n" EXTRA_HEADER;
[email protected]d9adff2c2009-09-05 01:15:453124 transaction2.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
3125 "rg: 50-59 rg: 60-69 rg: 70-79 ";
3126 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
3127
3128 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
3129 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3130 EXPECT_EQ(1, cache.disk_cache()->open_count());
3131 EXPECT_EQ(1, cache.disk_cache()->create_count());
3132
3133 RemoveMockTransaction(&transaction);
3134}
3135
[email protected]a189bce2009-12-01 01:59:123136// Tests that we can handle a regular request to a sparse entry, that results in
3137// new content provided by the server (206).
3138TEST(HttpCache, GET_Previous206_NewContent) {
3139 MockHttpCache cache;
3140 cache.http_cache()->set_enable_range_support(true);
3141 AddMockTransaction(&kRangeGET_TransactionOK);
3142 std::string headers;
3143
3144 // Write to the cache (0-9).
3145 MockTransaction transaction(kRangeGET_TransactionOK);
3146 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
3147 transaction.data = "rg: 00-09 ";
3148 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3149
[email protected]8c76ae22010-04-20 22:15:433150 Verify206Response(headers, 0, 9);
[email protected]a189bce2009-12-01 01:59:123151 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3152 EXPECT_EQ(0, cache.disk_cache()->open_count());
3153 EXPECT_EQ(1, cache.disk_cache()->create_count());
3154
3155 // Now we'll issue a request without any range that should result first in a
3156 // 206 (when revalidating), and then in a weird standard answer: the test
3157 // server will not modify the response so we'll get the default range... a
3158 // real server will answer with 200.
3159 MockTransaction transaction2(kRangeGET_TransactionOK);
3160 transaction2.request_headers = EXTRA_HEADER;
[email protected]5beca812010-06-24 17:55:243161 transaction2.load_flags |= net::LOAD_VALIDATE_CACHE;
[email protected]a189bce2009-12-01 01:59:123162 transaction2.data = "rg: 40-49 ";
3163 RangeTransactionServer handler;
3164 handler.set_modified(true);
3165 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
3166
3167 EXPECT_EQ(0U, headers.find("HTTP/1.1 206 Partial Content\n"));
3168 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3169 EXPECT_EQ(1, cache.disk_cache()->open_count());
3170 EXPECT_EQ(1, cache.disk_cache()->create_count());
3171
3172 // Verify that the previous request deleted the entry.
3173 RunTransactionTest(cache.http_cache(), transaction);
3174 EXPECT_EQ(2, cache.disk_cache()->create_count());
3175
3176 RemoveMockTransaction(&transaction);
3177}
3178
[email protected]e5dad132009-08-18 00:53:413179// Tests that we can handle cached 206 responses that are not sparse.
[email protected]21f659d2009-08-24 17:59:313180TEST(HttpCache, GET_Previous206_NotSparse) {
[email protected]44f873a62009-08-12 00:14:483181 MockHttpCache cache;
[email protected]21f659d2009-08-24 17:59:313182 cache.http_cache()->set_enable_range_support(true);
[email protected]44f873a62009-08-12 00:14:483183
[email protected]44f873a62009-08-12 00:14:483184 // Create a disk cache entry that stores 206 headers while not being sparse.
3185 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:333186 ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry));
[email protected]44f873a62009-08-12 00:14:483187
3188 std::string raw_headers(kRangeGET_TransactionOK.status);
3189 raw_headers.append("\n");
3190 raw_headers.append(kRangeGET_TransactionOK.response_headers);
3191 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
3192 raw_headers.size());
3193
3194 net::HttpResponseInfo response;
3195 response.headers = new net::HttpResponseHeaders(raw_headers);
[email protected]02e7a012010-05-10 23:06:333196 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
[email protected]44f873a62009-08-12 00:14:483197
3198 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500));
3199 int len = static_cast<int>(base::strlcpy(buf->data(),
3200 kRangeGET_TransactionOK.data, 500));
[email protected]02e7a012010-05-10 23:06:333201 TestCompletionCallback cb;
3202 int rv = entry->WriteData(1, 0, buf, len, &cb, true);
3203 EXPECT_EQ(len, cb.GetResult(rv));
[email protected]44f873a62009-08-12 00:14:483204 entry->Close();
3205
3206 // Now see that we don't use the stored entry.
3207 std::string headers;
3208 RunTransactionTestWithResponse(cache.http_cache(), kSimpleGET_Transaction,
3209 &headers);
3210
3211 // We are expecting a 200.
3212 std::string expected_headers(kSimpleGET_Transaction.status);
3213 expected_headers.append("\n");
3214 expected_headers.append(kSimpleGET_Transaction.response_headers);
3215 EXPECT_EQ(expected_headers, headers);
3216 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3217 EXPECT_EQ(1, cache.disk_cache()->open_count());
3218 EXPECT_EQ(2, cache.disk_cache()->create_count());
3219}
3220
[email protected]e5dad132009-08-18 00:53:413221// Tests that we can handle cached 206 responses that are not sparse. This time
3222// we issue a range request and expect to receive a range.
[email protected]21f659d2009-08-24 17:59:313223TEST(HttpCache, RangeGET_Previous206_NotSparse_2) {
[email protected]44f873a62009-08-12 00:14:483224 MockHttpCache cache;
[email protected]21f659d2009-08-24 17:59:313225 cache.http_cache()->set_enable_range_support(true);
[email protected]44f873a62009-08-12 00:14:483226 AddMockTransaction(&kRangeGET_TransactionOK);
3227
[email protected]44f873a62009-08-12 00:14:483228 // Create a disk cache entry that stores 206 headers while not being sparse.
3229 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:333230 ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry));
[email protected]44f873a62009-08-12 00:14:483231
3232 std::string raw_headers(kRangeGET_TransactionOK.status);
3233 raw_headers.append("\n");
3234 raw_headers.append(kRangeGET_TransactionOK.response_headers);
3235 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
3236 raw_headers.size());
3237
3238 net::HttpResponseInfo response;
3239 response.headers = new net::HttpResponseHeaders(raw_headers);
[email protected]02e7a012010-05-10 23:06:333240 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
[email protected]44f873a62009-08-12 00:14:483241
3242 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500));
3243 int len = static_cast<int>(base::strlcpy(buf->data(),
3244 kRangeGET_TransactionOK.data, 500));
[email protected]02e7a012010-05-10 23:06:333245 TestCompletionCallback cb;
3246 int rv = entry->WriteData(1, 0, buf, len, &cb, true);
3247 EXPECT_EQ(len, cb.GetResult(rv));
[email protected]44f873a62009-08-12 00:14:483248 entry->Close();
3249
3250 // Now see that we don't use the stored entry.
3251 std::string headers;
3252 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
3253 &headers);
3254
3255 // We are expecting a 206.
[email protected]8c76ae22010-04-20 22:15:433256 Verify206Response(headers, 40, 49);
[email protected]44f873a62009-08-12 00:14:483257 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3258 EXPECT_EQ(1, cache.disk_cache()->open_count());
3259 EXPECT_EQ(2, cache.disk_cache()->create_count());
3260
3261 RemoveMockTransaction(&kRangeGET_TransactionOK);
3262}
3263
[email protected]e5dad132009-08-18 00:53:413264// Tests that we can handle range requests with cached 200 responses.
[email protected]21f659d2009-08-24 17:59:313265TEST(HttpCache, RangeGET_Previous200) {
[email protected]e5dad132009-08-18 00:53:413266 MockHttpCache cache;
[email protected]21f659d2009-08-24 17:59:313267 cache.http_cache()->set_enable_range_support(true);
[email protected]e5dad132009-08-18 00:53:413268
3269 // Store the whole thing with status 200.
3270 MockTransaction transaction(kTypicalGET_Transaction);
3271 transaction.url = kRangeGET_TransactionOK.url;
3272 transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
3273 "rg: 50-59 rg: 60-69 rg: 70-79 ";
3274 AddMockTransaction(&transaction);
3275 RunTransactionTest(cache.http_cache(), transaction);
3276 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3277 EXPECT_EQ(0, cache.disk_cache()->open_count());
3278 EXPECT_EQ(1, cache.disk_cache()->create_count());
3279
3280 RemoveMockTransaction(&transaction);
3281 AddMockTransaction(&kRangeGET_TransactionOK);
3282
3283 // Now see that we use the stored entry.
3284 std::string headers;
3285 MockTransaction transaction2(kRangeGET_TransactionOK);
3286 RangeTransactionServer handler;
3287 handler.set_not_modified(true);
3288 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
3289
3290 // We are expecting a 206.
[email protected]8c76ae22010-04-20 22:15:433291 Verify206Response(headers, 40, 49);
[email protected]e5dad132009-08-18 00:53:413292 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3293 EXPECT_EQ(1, cache.disk_cache()->open_count());
3294 EXPECT_EQ(1, cache.disk_cache()->create_count());
3295
[email protected]8f28d632009-10-01 22:09:213296 // The last transaction has finished so make sure the entry is deactivated.
3297 MessageLoop::current()->RunAllPending();
3298
[email protected]e5dad132009-08-18 00:53:413299 // Now we should receive a range from the server and drop the stored entry.
3300 handler.set_not_modified(false);
3301 transaction2.request_headers = kRangeGET_TransactionOK.request_headers;
3302 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
[email protected]8c76ae22010-04-20 22:15:433303 Verify206Response(headers, 40, 49);
[email protected]e5dad132009-08-18 00:53:413304 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3305 EXPECT_EQ(2, cache.disk_cache()->open_count());
3306 EXPECT_EQ(1, cache.disk_cache()->create_count());
3307
3308 RunTransactionTest(cache.http_cache(), transaction2);
3309 EXPECT_EQ(2, cache.disk_cache()->create_count());
3310
3311 RemoveMockTransaction(&kRangeGET_TransactionOK);
3312}
3313
3314// Tests that we can handle a 200 response when dealing with sparse entries.
[email protected]21f659d2009-08-24 17:59:313315TEST(HttpCache, RangeRequestResultsIn200) {
[email protected]44f873a62009-08-12 00:14:483316 MockHttpCache cache;
[email protected]21f659d2009-08-24 17:59:313317 cache.http_cache()->set_enable_range_support(true);
[email protected]44f873a62009-08-12 00:14:483318 AddMockTransaction(&kRangeGET_TransactionOK);
[email protected]44f873a62009-08-12 00:14:483319 std::string headers;
3320
3321 // Write to the cache (70-79).
3322 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]e75e8af2009-11-03 00:04:203323 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
[email protected]44f873a62009-08-12 00:14:483324 transaction.data = "rg: 70-79 ";
3325 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3326
[email protected]8c76ae22010-04-20 22:15:433327 Verify206Response(headers, 70, 79);
[email protected]44f873a62009-08-12 00:14:483328 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3329 EXPECT_EQ(0, cache.disk_cache()->open_count());
3330 EXPECT_EQ(1, cache.disk_cache()->create_count());
3331
3332 // Now we'll issue a request that results in a plain 200 response, but to
3333 // the to the same URL that we used to store sparse data, and making sure
3334 // that we ask for a range.
3335 RemoveMockTransaction(&kRangeGET_TransactionOK);
3336 MockTransaction transaction2(kSimpleGET_Transaction);
3337 transaction2.url = kRangeGET_TransactionOK.url;
3338 transaction2.request_headers = kRangeGET_TransactionOK.request_headers;
3339 AddMockTransaction(&transaction2);
3340
3341 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
3342
3343 std::string expected_headers(kSimpleGET_Transaction.status);
3344 expected_headers.append("\n");
3345 expected_headers.append(kSimpleGET_Transaction.response_headers);
3346 EXPECT_EQ(expected_headers, headers);
3347 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3348 EXPECT_EQ(1, cache.disk_cache()->open_count());
3349 EXPECT_EQ(1, cache.disk_cache()->create_count());
3350
3351 RemoveMockTransaction(&transaction2);
3352}
3353
[email protected]e5dad132009-08-18 00:53:413354// Tests that a range request that falls outside of the size that we know about
3355// only deletes the entry if the resource has indeed changed.
[email protected]21f659d2009-08-24 17:59:313356TEST(HttpCache, RangeGET_MoreThanCurrentSize) {
[email protected]e5dad132009-08-18 00:53:413357 MockHttpCache cache;
[email protected]21f659d2009-08-24 17:59:313358 cache.http_cache()->set_enable_range_support(true);
[email protected]e5dad132009-08-18 00:53:413359 AddMockTransaction(&kRangeGET_TransactionOK);
3360 std::string headers;
3361
3362 // Write to the cache (40-49).
3363 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
3364 &headers);
3365
[email protected]8c76ae22010-04-20 22:15:433366 Verify206Response(headers, 40, 49);
[email protected]e5dad132009-08-18 00:53:413367 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3368 EXPECT_EQ(0, cache.disk_cache()->open_count());
3369 EXPECT_EQ(1, cache.disk_cache()->create_count());
3370
3371 // A weird request should not delete this entry. Ask for bytes 120-.
3372 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]e75e8af2009-11-03 00:04:203373 transaction.request_headers = "Range: bytes = 120-\r\n" EXTRA_HEADER;
[email protected]e5dad132009-08-18 00:53:413374 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3375
3376 EXPECT_EQ(0U, headers.find("HTTP/1.1 416 "));
3377 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3378 EXPECT_EQ(1, cache.disk_cache()->open_count());
3379 EXPECT_EQ(1, cache.disk_cache()->create_count());
3380
3381 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
3382 EXPECT_EQ(2, cache.disk_cache()->open_count());
3383 EXPECT_EQ(1, cache.disk_cache()->create_count());
3384
3385 RemoveMockTransaction(&kRangeGET_TransactionOK);
3386}
3387
[email protected]2c8528532009-09-09 16:55:223388// Tests that we don't delete a sparse entry when we cancel a request.
3389TEST(HttpCache, RangeGET_Cancel) {
3390 MockHttpCache cache;
3391 cache.http_cache()->set_enable_range_support(true);
3392 AddMockTransaction(&kRangeGET_TransactionOK);
3393
3394 MockHttpRequest request(kRangeGET_TransactionOK);
3395
[email protected]1638d602009-09-24 03:49:173396 Context* c = new Context();
3397 int rv = cache.http_cache()->CreateTransaction(&c->trans);
3398 EXPECT_EQ(net::OK, rv);
[email protected]2c8528532009-09-09 16:55:223399
[email protected]5a1d7ca2010-04-28 20:12:273400 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog());
[email protected]2c8528532009-09-09 16:55:223401 if (rv == net::ERR_IO_PENDING)
3402 rv = c->callback.WaitForResult();
3403
3404 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3405 EXPECT_EQ(0, cache.disk_cache()->open_count());
3406 EXPECT_EQ(1, cache.disk_cache()->create_count());
3407
3408 // Make sure that the entry has some data stored.
3409 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10);
3410 rv = c->trans->Read(buf, buf->size(), &c->callback);
3411 if (rv == net::ERR_IO_PENDING)
3412 rv = c->callback.WaitForResult();
3413 EXPECT_EQ(buf->size(), rv);
3414
3415 // Destroy the transaction.
3416 delete c;
3417
3418 // Verify that the entry has not been deleted.
3419 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:333420 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
[email protected]2c8528532009-09-09 16:55:223421 entry->Close();
3422 RemoveMockTransaction(&kRangeGET_TransactionOK);
3423}
3424
[email protected]06e62ba2009-10-08 23:07:393425// Tests that we don't delete a sparse entry when we start a new request after
3426// cancelling the previous one.
3427TEST(HttpCache, RangeGET_Cancel2) {
3428 MockHttpCache cache;
3429 cache.http_cache()->set_enable_range_support(true);
3430 AddMockTransaction(&kRangeGET_TransactionOK);
3431
3432 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
3433 MockHttpRequest request(kRangeGET_TransactionOK);
[email protected]5beca812010-06-24 17:55:243434 request.load_flags |= net::LOAD_VALIDATE_CACHE;
[email protected]06e62ba2009-10-08 23:07:393435
3436 Context* c = new Context();
3437 int rv = cache.http_cache()->CreateTransaction(&c->trans);
3438 EXPECT_EQ(net::OK, rv);
3439
[email protected]5a1d7ca2010-04-28 20:12:273440 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog());
[email protected]06e62ba2009-10-08 23:07:393441 if (rv == net::ERR_IO_PENDING)
3442 rv = c->callback.WaitForResult();
3443
3444 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3445 EXPECT_EQ(1, cache.disk_cache()->open_count());
3446 EXPECT_EQ(1, cache.disk_cache()->create_count());
3447
3448 // Make sure that we revalidate the entry and read from the cache (a single
3449 // read will return while waiting for the network).
3450 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(5);
3451 rv = c->trans->Read(buf, buf->size(), &c->callback);
[email protected]21e743202009-12-18 01:31:043452 EXPECT_EQ(5, c->callback.GetResult(rv));
[email protected]06e62ba2009-10-08 23:07:393453 rv = c->trans->Read(buf, buf->size(), &c->callback);
3454 EXPECT_EQ(net::ERR_IO_PENDING, rv);
3455
3456 // Destroy the transaction before completing the read.
3457 delete c;
3458
3459 // We have the read and the delete (OnProcessPendingQueue) waiting on the
3460 // message loop. This means that a new transaction will just reuse the same
3461 // active entry (no open or create).
3462
3463 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
3464
[email protected]5beca812010-06-24 17:55:243465 EXPECT_EQ(2, cache.network_layer()->transaction_count());
[email protected]06e62ba2009-10-08 23:07:393466 EXPECT_EQ(1, cache.disk_cache()->open_count());
3467 EXPECT_EQ(1, cache.disk_cache()->create_count());
3468 RemoveMockTransaction(&kRangeGET_TransactionOK);
3469}
3470
[email protected]24f46392009-11-19 18:45:233471// A slight variation of the previous test, this time we cancel two requests in
3472// a row, making sure that the second is waiting for the entry to be ready.
3473TEST(HttpCache, RangeGET_Cancel3) {
3474 MockHttpCache cache;
3475 cache.http_cache()->set_enable_range_support(true);
3476 AddMockTransaction(&kRangeGET_TransactionOK);
3477
3478 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
3479 MockHttpRequest request(kRangeGET_TransactionOK);
[email protected]5beca812010-06-24 17:55:243480 request.load_flags |= net::LOAD_VALIDATE_CACHE;
[email protected]24f46392009-11-19 18:45:233481
3482 Context* c = new Context();
3483 int rv = cache.http_cache()->CreateTransaction(&c->trans);
3484 EXPECT_EQ(net::OK, rv);
3485
[email protected]5a1d7ca2010-04-28 20:12:273486 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog());
[email protected]24f46392009-11-19 18:45:233487 EXPECT_EQ(net::ERR_IO_PENDING, rv);
3488 rv = c->callback.WaitForResult();
3489
3490 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3491 EXPECT_EQ(1, cache.disk_cache()->open_count());
3492 EXPECT_EQ(1, cache.disk_cache()->create_count());
3493
3494 // Make sure that we revalidate the entry and read from the cache (a single
3495 // read will return while waiting for the network).
3496 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(5);
3497 rv = c->trans->Read(buf, buf->size(), &c->callback);
[email protected]21e743202009-12-18 01:31:043498 EXPECT_EQ(5, c->callback.GetResult(rv));
[email protected]24f46392009-11-19 18:45:233499 rv = c->trans->Read(buf, buf->size(), &c->callback);
3500 EXPECT_EQ(net::ERR_IO_PENDING, rv);
3501
3502 // Destroy the transaction before completing the read.
3503 delete c;
3504
3505 // We have the read and the delete (OnProcessPendingQueue) waiting on the
3506 // message loop. This means that a new transaction will just reuse the same
3507 // active entry (no open or create).
3508
3509 c = new Context();
3510 rv = cache.http_cache()->CreateTransaction(&c->trans);
3511 EXPECT_EQ(net::OK, rv);
3512
[email protected]5a1d7ca2010-04-28 20:12:273513 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog());
[email protected]24f46392009-11-19 18:45:233514 EXPECT_EQ(net::ERR_IO_PENDING, rv);
3515
3516 MockDiskEntry::IgnoreCallbacks(true);
3517 MessageLoop::current()->RunAllPending();
3518 MockDiskEntry::IgnoreCallbacks(false);
3519
3520 // The new transaction is waiting for the query range callback.
3521 delete c;
3522
3523 // And we should not crash when the callback is delivered.
3524 MessageLoop::current()->RunAllPending();
3525
3526 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3527 EXPECT_EQ(1, cache.disk_cache()->open_count());
3528 EXPECT_EQ(1, cache.disk_cache()->create_count());
3529 RemoveMockTransaction(&kRangeGET_TransactionOK);
3530}
3531
[email protected]7eab0d2262009-10-14 22:05:543532// Tests that an invalid range response results in no cached entry.
3533TEST(HttpCache, RangeGET_InvalidResponse1) {
3534 MockHttpCache cache;
3535 cache.http_cache()->set_enable_range_support(true);
3536 std::string headers;
3537
3538 MockTransaction transaction(kRangeGET_TransactionOK);
3539 transaction.handler = NULL;
3540 transaction.response_headers = "Content-Range: bytes 40-49/45\n"
3541 "Content-Length: 10\n";
3542 AddMockTransaction(&transaction);
3543 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3544
3545 std::string expected(transaction.status);
3546 expected.append("\n");
3547 expected.append(transaction.response_headers);
3548 EXPECT_EQ(expected, headers);
3549
3550 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3551 EXPECT_EQ(0, cache.disk_cache()->open_count());
3552 EXPECT_EQ(1, cache.disk_cache()->create_count());
3553
3554 // Verify that we don't have a cached entry.
[email protected]7d7ad6e42010-01-14 01:30:533555 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:333556 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
[email protected]7eab0d2262009-10-14 22:05:543557
3558 RemoveMockTransaction(&kRangeGET_TransactionOK);
3559}
3560
3561// Tests that we reject a range that doesn't match the content-length.
3562TEST(HttpCache, RangeGET_InvalidResponse2) {
3563 MockHttpCache cache;
3564 cache.http_cache()->set_enable_range_support(true);
3565 std::string headers;
3566
3567 MockTransaction transaction(kRangeGET_TransactionOK);
3568 transaction.handler = NULL;
3569 transaction.response_headers = "Content-Range: bytes 40-49/80\n"
3570 "Content-Length: 20\n";
3571 AddMockTransaction(&transaction);
3572 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3573
3574 std::string expected(transaction.status);
3575 expected.append("\n");
3576 expected.append(transaction.response_headers);
3577 EXPECT_EQ(expected, headers);
3578
3579 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3580 EXPECT_EQ(0, cache.disk_cache()->open_count());
3581 EXPECT_EQ(1, cache.disk_cache()->create_count());
3582
3583 // Verify that we don't have a cached entry.
[email protected]7d7ad6e42010-01-14 01:30:533584 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:333585 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
[email protected]7eab0d2262009-10-14 22:05:543586
3587 RemoveMockTransaction(&kRangeGET_TransactionOK);
3588}
3589
3590// Tests that if a server tells us conflicting information about a resource we
3591// ignore the response.
3592TEST(HttpCache, RangeGET_InvalidResponse3) {
3593 MockHttpCache cache;
3594 cache.http_cache()->set_enable_range_support(true);
3595 std::string headers;
3596
3597 MockTransaction transaction(kRangeGET_TransactionOK);
3598 transaction.handler = NULL;
[email protected]e75e8af2009-11-03 00:04:203599 transaction.request_headers = "Range: bytes = 50-59\r\n" EXTRA_HEADER;
[email protected]7eab0d2262009-10-14 22:05:543600 std::string response_headers(transaction.response_headers);
3601 response_headers.append("Content-Range: bytes 50-59/160\n");
3602 transaction.response_headers = response_headers.c_str();
3603 AddMockTransaction(&transaction);
3604 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3605
[email protected]8c76ae22010-04-20 22:15:433606 Verify206Response(headers, 50, 59);
[email protected]7eab0d2262009-10-14 22:05:543607 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3608 EXPECT_EQ(0, cache.disk_cache()->open_count());
3609 EXPECT_EQ(1, cache.disk_cache()->create_count());
3610
3611 RemoveMockTransaction(&transaction);
3612 AddMockTransaction(&kRangeGET_TransactionOK);
3613
3614 // This transaction will report a resource size of 80 bytes, and we think it's
3615 // 160 so we should ignore the response.
3616 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
3617 &headers);
3618
[email protected]8c76ae22010-04-20 22:15:433619 Verify206Response(headers, 40, 49);
[email protected]7eab0d2262009-10-14 22:05:543620 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3621 EXPECT_EQ(1, cache.disk_cache()->open_count());
3622 EXPECT_EQ(1, cache.disk_cache()->create_count());
3623
3624 // Verify that we cached the first response but not the second one.
3625 disk_cache::Entry* en;
[email protected]02e7a012010-05-10 23:06:333626 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &en));
[email protected]034740a2010-06-11 17:16:483627
[email protected]7eab0d2262009-10-14 22:05:543628 int64 cached_start = 0;
[email protected]034740a2010-06-11 17:16:483629 TestCompletionCallback cb;
3630 int rv = en->GetAvailableRange(40, 20, &cached_start, &cb);
3631 EXPECT_EQ(10, cb.GetResult(rv));
[email protected]7eab0d2262009-10-14 22:05:543632 EXPECT_EQ(50, cached_start);
3633 en->Close();
3634
3635 RemoveMockTransaction(&kRangeGET_TransactionOK);
3636}
3637
3638// Tests that we handle large range values properly.
3639TEST(HttpCache, RangeGET_LargeValues) {
3640 // We need a real sparse cache for this test.
[email protected]f8702522010-05-12 18:40:103641 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(1024 * 1024));
[email protected]7eab0d2262009-10-14 22:05:543642 cache.http_cache()->set_enable_range_support(true);
3643 std::string headers;
3644
3645 MockTransaction transaction(kRangeGET_TransactionOK);
3646 transaction.handler = NULL;
[email protected]e75e8af2009-11-03 00:04:203647 transaction.request_headers = "Range: bytes = 4294967288-4294967297\r\n"
3648 EXTRA_HEADER;
[email protected]7eab0d2262009-10-14 22:05:543649 transaction.response_headers =
3650 "Content-Range: bytes 4294967288-4294967297/4294967299\n"
3651 "Content-Length: 10\n";
3652 AddMockTransaction(&transaction);
3653 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3654
3655 std::string expected(transaction.status);
3656 expected.append("\n");
3657 expected.append(transaction.response_headers);
3658 EXPECT_EQ(expected, headers);
3659
3660 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3661
3662 // Verify that we have a cached entry.
3663 disk_cache::Entry* en;
[email protected]02e7a012010-05-10 23:06:333664 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &en));
[email protected]7eab0d2262009-10-14 22:05:543665 en->Close();
3666
3667 RemoveMockTransaction(&kRangeGET_TransactionOK);
3668}
3669
[email protected]93e78442009-10-27 04:46:323670// Tests that we don't crash with a range request if the disk cache was not
3671// initialized properly.
3672TEST(HttpCache, RangeGET_NoDiskCache) {
[email protected]f8702522010-05-12 18:40:103673 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
3674 factory->set_fail(true);
3675 factory->FinishCreation(); // We'll complete synchronously.
3676 MockHttpCache cache(factory);
3677
[email protected]93e78442009-10-27 04:46:323678 cache.http_cache()->set_enable_range_support(true);
3679 AddMockTransaction(&kRangeGET_TransactionOK);
3680
3681 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
3682 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3683
3684 RemoveMockTransaction(&kRangeGET_TransactionOK);
3685}
3686
[email protected]793618a2009-11-03 23:08:123687// Tests that we handle byte range requests that skip the cache.
3688TEST(HttpCache, RangeHEAD) {
3689 MockHttpCache cache;
3690 cache.http_cache()->set_enable_range_support(true);
3691 AddMockTransaction(&kRangeGET_TransactionOK);
3692
3693 MockTransaction transaction(kRangeGET_TransactionOK);
3694 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
3695 transaction.method = "HEAD";
3696 transaction.data = "rg: 70-79 ";
3697
3698 std::string headers;
3699 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3700
[email protected]8c76ae22010-04-20 22:15:433701 Verify206Response(headers, 70, 79);
[email protected]793618a2009-11-03 23:08:123702 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3703 EXPECT_EQ(0, cache.disk_cache()->open_count());
3704 EXPECT_EQ(0, cache.disk_cache()->create_count());
3705
3706 RemoveMockTransaction(&kRangeGET_TransactionOK);
3707}
3708
[email protected]fa59e6a2009-12-02 18:07:463709// Tests that we don't crash when after reading from the cache we issue a
3710// request for the next range and the server gives us a 200 synchronously.
3711TEST(HttpCache, RangeGET_FastFlakyServer) {
3712 MockHttpCache cache;
3713 cache.http_cache()->set_enable_range_support(true);
3714
3715 MockTransaction transaction(kRangeGET_TransactionOK);
3716 transaction.request_headers = "Range: bytes = 40-\r\n" EXTRA_HEADER;
3717 transaction.test_mode = TEST_MODE_SYNC_NET_START;
[email protected]5beca812010-06-24 17:55:243718 transaction.load_flags |= net::LOAD_VALIDATE_CACHE;
[email protected]fa59e6a2009-12-02 18:07:463719 AddMockTransaction(&transaction);
3720
3721 // Write to the cache.
3722 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
3723
3724 // And now read from the cache and the network.
3725 RangeTransactionServer handler;
3726 handler.set_bad_200(true);
3727 RunTransactionTest(cache.http_cache(), transaction);
3728
3729 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3730 EXPECT_EQ(1, cache.disk_cache()->open_count());
3731 EXPECT_EQ(1, cache.disk_cache()->create_count());
3732
3733 RemoveMockTransaction(&transaction);
3734}
3735
[email protected]c14117b92010-01-21 19:22:573736// Tests that when the server gives us less data than expected, we don't keep
3737// asking for more data.
3738TEST(HttpCache, RangeGET_FastFlakyServer2) {
3739 MockHttpCache cache;
3740 cache.http_cache()->set_enable_range_support(true);
3741
3742 // First, check with an empty cache (WRITE mode).
3743 MockTransaction transaction(kRangeGET_TransactionOK);
3744 transaction.request_headers = "Range: bytes = 40-49\r\n" EXTRA_HEADER;
3745 transaction.data = "rg: 40-"; // Less than expected.
3746 transaction.handler = NULL;
3747 std::string headers(transaction.response_headers);
3748 headers.append("Content-Range: bytes 40-49/80\n");
3749 transaction.response_headers = headers.c_str();
3750
3751 AddMockTransaction(&transaction);
3752
3753 // Write to the cache.
3754 RunTransactionTest(cache.http_cache(), transaction);
3755
3756 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3757 EXPECT_EQ(0, cache.disk_cache()->open_count());
3758 EXPECT_EQ(1, cache.disk_cache()->create_count());
3759
3760 // Now verify that even in READ_WRITE mode, we forward the bad response to
3761 // the caller.
3762 transaction.request_headers = "Range: bytes = 60-69\r\n" EXTRA_HEADER;
3763 transaction.data = "rg: 60-"; // Less than expected.
3764 headers = kRangeGET_TransactionOK.response_headers;
3765 headers.append("Content-Range: bytes 60-69/80\n");
3766 transaction.response_headers = headers.c_str();
3767
3768 RunTransactionTest(cache.http_cache(), transaction);
3769
3770 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3771 EXPECT_EQ(1, cache.disk_cache()->open_count());
3772 EXPECT_EQ(1, cache.disk_cache()->create_count());
3773
3774 RemoveMockTransaction(&transaction);
3775}
3776
[email protected]e5dad132009-08-18 00:53:413777#ifdef NDEBUG
3778// This test hits a NOTREACHED so it is a release mode only test.
[email protected]21f659d2009-08-24 17:59:313779TEST(HttpCache, RangeGET_OK_LoadOnlyFromCache) {
[email protected]e5dad132009-08-18 00:53:413780 MockHttpCache cache;
[email protected]21f659d2009-08-24 17:59:313781 cache.http_cache()->set_enable_range_support(true);
[email protected]e5dad132009-08-18 00:53:413782 AddMockTransaction(&kRangeGET_TransactionOK);
3783
3784 // Write to the cache (40-49).
3785 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
3786 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3787 EXPECT_EQ(0, cache.disk_cache()->open_count());
3788 EXPECT_EQ(1, cache.disk_cache()->create_count());
3789
3790 // Force this transaction to read from the cache.
3791 MockTransaction transaction(kRangeGET_TransactionOK);
3792 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
3793
3794 MockHttpRequest request(transaction);
3795 TestCompletionCallback callback;
3796
[email protected]1638d602009-09-24 03:49:173797 scoped_ptr<net::HttpTransaction> trans;
3798 int rv = cache.http_cache()->CreateTransaction(&trans);
3799 EXPECT_EQ(net::OK, rv);
[email protected]e5dad132009-08-18 00:53:413800 ASSERT_TRUE(trans.get());
3801
[email protected]7deb23412010-04-28 20:29:293802 rv = trans->Start(&request, &callback, net::BoundNetLog());
[email protected]e5dad132009-08-18 00:53:413803 if (rv == net::ERR_IO_PENDING)
3804 rv = callback.WaitForResult();
3805 ASSERT_EQ(net::ERR_CACHE_MISS, rv);
3806
3807 trans.reset();
3808
3809 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3810 EXPECT_EQ(1, cache.disk_cache()->open_count());
3811 EXPECT_EQ(1, cache.disk_cache()->create_count());
3812
3813 RemoveMockTransaction(&kRangeGET_TransactionOK);
3814}
3815#endif
3816
[email protected]28accfe2009-09-04 23:36:333817// Tests the handling of the "truncation" flag.
3818TEST(HttpCache, WriteResponseInfo_Truncated) {
3819 MockHttpCache cache;
3820 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:333821 ASSERT_TRUE(cache.CreateBackendEntry("http://www.google.com", &entry));
[email protected]28accfe2009-09-04 23:36:333822
3823 std::string headers("HTTP/1.1 200 OK");
3824 headers = net::HttpUtil::AssembleRawHeaders(headers.data(), headers.size());
3825 net::HttpResponseInfo response;
3826 response.headers = new net::HttpResponseHeaders(headers);
3827
3828 // Set the last argument for this to be an incomplete request.
[email protected]02e7a012010-05-10 23:06:333829 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true));
[email protected]28accfe2009-09-04 23:36:333830 bool truncated = false;
[email protected]02e7a012010-05-10 23:06:333831 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
[email protected]28accfe2009-09-04 23:36:333832 EXPECT_TRUE(truncated);
3833
3834 // And now test the opposite case.
[email protected]02e7a012010-05-10 23:06:333835 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
[email protected]28accfe2009-09-04 23:36:333836 truncated = true;
[email protected]02e7a012010-05-10 23:06:333837 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
[email protected]28accfe2009-09-04 23:36:333838 EXPECT_FALSE(truncated);
3839 entry->Close();
3840}
3841
3842// Tests that we delete an entry when the request is cancelled before starting
3843// to read from the network.
3844TEST(HttpCache, DoomOnDestruction) {
3845 MockHttpCache cache;
3846 cache.http_cache()->set_enable_range_support(true);
3847
3848 MockHttpRequest request(kSimpleGET_Transaction);
3849
[email protected]1638d602009-09-24 03:49:173850 Context* c = new Context();
3851 int rv = cache.http_cache()->CreateTransaction(&c->trans);
3852 EXPECT_EQ(net::OK, rv);
[email protected]28accfe2009-09-04 23:36:333853
[email protected]5a1d7ca2010-04-28 20:12:273854 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog());
[email protected]28accfe2009-09-04 23:36:333855 if (rv == net::ERR_IO_PENDING)
3856 c->result = c->callback.WaitForResult();
3857
3858 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3859 EXPECT_EQ(0, cache.disk_cache()->open_count());
3860 EXPECT_EQ(1, cache.disk_cache()->create_count());
3861
3862 // Destroy the transaction. We only have the headers so we should delete this
3863 // entry.
3864 delete c;
3865
3866 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
3867
3868 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3869 EXPECT_EQ(0, cache.disk_cache()->open_count());
3870 EXPECT_EQ(2, cache.disk_cache()->create_count());
3871}
3872
[email protected]dbd39fb2010-01-08 01:13:363873// Tests that we delete an entry when the request is cancelled if the response
3874// does not have content-length and strong validators.
3875TEST(HttpCache, DoomOnDestruction2) {
3876 MockHttpCache cache;
3877 cache.http_cache()->set_enable_range_support(true);
3878
3879 MockHttpRequest request(kSimpleGET_Transaction);
3880
3881 Context* c = new Context();
3882 int rv = cache.http_cache()->CreateTransaction(&c->trans);
3883 EXPECT_EQ(net::OK, rv);
3884
[email protected]5a1d7ca2010-04-28 20:12:273885 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog());
[email protected]dbd39fb2010-01-08 01:13:363886 if (rv == net::ERR_IO_PENDING)
3887 rv = c->callback.WaitForResult();
3888
3889 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3890 EXPECT_EQ(0, cache.disk_cache()->open_count());
3891 EXPECT_EQ(1, cache.disk_cache()->create_count());
3892
3893 // Make sure that the entry has some data stored.
3894 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10);
3895 rv = c->trans->Read(buf, buf->size(), &c->callback);
3896 if (rv == net::ERR_IO_PENDING)
3897 rv = c->callback.WaitForResult();
3898 EXPECT_EQ(buf->size(), rv);
3899
3900 // Destroy the transaction.
3901 delete c;
3902
3903 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
3904
3905 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3906 EXPECT_EQ(0, cache.disk_cache()->open_count());
3907 EXPECT_EQ(2, cache.disk_cache()->create_count());
3908}
3909
3910// Tests that we delete an entry when the request is cancelled if the response
3911// has an "Accept-Ranges: none" header.
3912TEST(HttpCache, DoomOnDestruction3) {
3913 MockHttpCache cache;
3914 cache.http_cache()->set_enable_range_support(true);
3915
3916 MockTransaction transaction(kSimpleGET_Transaction);
3917 transaction.response_headers =
3918 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
3919 "Content-Length: 22\n"
3920 "Accept-Ranges: none\n"
3921 "Etag: foopy\n";
3922 AddMockTransaction(&transaction);
3923 MockHttpRequest request(transaction);
3924
3925 Context* c = new Context();
3926 int rv = cache.http_cache()->CreateTransaction(&c->trans);
3927 EXPECT_EQ(net::OK, rv);
3928
[email protected]5a1d7ca2010-04-28 20:12:273929 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog());
[email protected]dbd39fb2010-01-08 01:13:363930 if (rv == net::ERR_IO_PENDING)
3931 rv = c->callback.WaitForResult();
3932
3933 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3934 EXPECT_EQ(0, cache.disk_cache()->open_count());
3935 EXPECT_EQ(1, cache.disk_cache()->create_count());
3936
3937 // Make sure that the entry has some data stored.
3938 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10);
3939 rv = c->trans->Read(buf, buf->size(), &c->callback);
3940 if (rv == net::ERR_IO_PENDING)
3941 rv = c->callback.WaitForResult();
3942 EXPECT_EQ(buf->size(), rv);
3943
3944 // Destroy the transaction.
3945 delete c;
3946
3947 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
3948
3949 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3950 EXPECT_EQ(0, cache.disk_cache()->open_count());
3951 EXPECT_EQ(2, cache.disk_cache()->create_count());
3952
3953 RemoveMockTransaction(&transaction);
3954}
3955
[email protected]28accfe2009-09-04 23:36:333956// Tests that we mark an entry as incomplete when the request is cancelled.
3957TEST(HttpCache, Set_Truncated_Flag) {
3958 MockHttpCache cache;
3959 cache.http_cache()->set_enable_range_support(true);
3960
[email protected]dbd39fb2010-01-08 01:13:363961 MockTransaction transaction(kSimpleGET_Transaction);
3962 transaction.response_headers =
3963 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
3964 "Content-Length: 22\n"
3965 "Etag: foopy\n";
3966 AddMockTransaction(&transaction);
3967 MockHttpRequest request(transaction);
[email protected]28accfe2009-09-04 23:36:333968
[email protected]6df35cc2010-02-10 00:53:063969 scoped_ptr<Context> c(new Context());
[email protected]1638d602009-09-24 03:49:173970 int rv = cache.http_cache()->CreateTransaction(&c->trans);
3971 EXPECT_EQ(net::OK, rv);
[email protected]28accfe2009-09-04 23:36:333972
[email protected]5a1d7ca2010-04-28 20:12:273973 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog());
[email protected]28accfe2009-09-04 23:36:333974 if (rv == net::ERR_IO_PENDING)
3975 rv = c->callback.WaitForResult();
3976
3977 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3978 EXPECT_EQ(0, cache.disk_cache()->open_count());
3979 EXPECT_EQ(1, cache.disk_cache()->create_count());
3980
3981 // Make sure that the entry has some data stored.
3982 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10);
3983 rv = c->trans->Read(buf, buf->size(), &c->callback);
3984 if (rv == net::ERR_IO_PENDING)
3985 rv = c->callback.WaitForResult();
3986 EXPECT_EQ(buf->size(), rv);
3987
[email protected]6df35cc2010-02-10 00:53:063988 // We want to cancel the request when the transaction is busy.
3989 rv = c->trans->Read(buf, buf->size(), &c->callback);
3990 EXPECT_EQ(net::ERR_IO_PENDING, rv);
3991 EXPECT_FALSE(c->callback.have_result());
3992
3993 g_test_mode = TEST_MODE_SYNC_ALL;
3994
[email protected]28accfe2009-09-04 23:36:333995 // Destroy the transaction.
[email protected]6df35cc2010-02-10 00:53:063996 c->trans.reset();
3997 g_test_mode = 0;
3998
3999 // Make sure that we don't invoke the callback. We may have an issue if the
4000 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we
4001 // could end up with the transaction being deleted twice if we send any
4002 // notification from the transaction destructor (see http://crbug.com/31723).
4003 EXPECT_FALSE(c->callback.have_result());
[email protected]28accfe2009-09-04 23:36:334004
4005 // Verify that the entry is marked as incomplete.
4006 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:334007 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry));
[email protected]28accfe2009-09-04 23:36:334008 net::HttpResponseInfo response;
4009 bool truncated = false;
[email protected]02e7a012010-05-10 23:06:334010 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
[email protected]28accfe2009-09-04 23:36:334011 EXPECT_TRUE(truncated);
4012 entry->Close();
[email protected]dbd39fb2010-01-08 01:13:364013
4014 RemoveMockTransaction(&transaction);
[email protected]28accfe2009-09-04 23:36:334015}
4016
4017// Tests that we can continue with a request that was interrupted.
4018TEST(HttpCache, GET_IncompleteResource) {
4019 MockHttpCache cache;
4020 cache.http_cache()->set_enable_range_support(true);
4021 AddMockTransaction(&kRangeGET_TransactionOK);
4022
4023 // Create a disk cache entry that stores an incomplete resource.
4024 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:334025 ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry));
[email protected]28accfe2009-09-04 23:36:334026
[email protected]28accfe2009-09-04 23:36:334027 std::string raw_headers("HTTP/1.1 200 OK\n"
[email protected]dbd39fb2010-01-08 01:13:364028 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
[email protected]28accfe2009-09-04 23:36:334029 "ETag: \"foo\"\n"
4030 "Accept-Ranges: bytes\n"
[email protected]dbd39fb2010-01-08 01:13:364031 "Content-Length: 80\n");
[email protected]28accfe2009-09-04 23:36:334032 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
4033 raw_headers.size());
4034
4035 net::HttpResponseInfo response;
4036 response.headers = new net::HttpResponseHeaders(raw_headers);
4037 // Set the last argument for this to be an incomplete request.
[email protected]02e7a012010-05-10 23:06:334038 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true));
[email protected]28accfe2009-09-04 23:36:334039
4040 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100));
4041 int len = static_cast<int>(base::strlcpy(buf->data(),
4042 "rg: 00-09 rg: 10-19 ", 100));
[email protected]02e7a012010-05-10 23:06:334043 TestCompletionCallback cb;
4044 int rv = entry->WriteData(1, 0, buf, len, &cb, true);
4045 EXPECT_EQ(len, cb.GetResult(rv));
[email protected]28accfe2009-09-04 23:36:334046
4047 // Now make a regular request.
4048 std::string headers;
4049 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]e75e8af2009-11-03 00:04:204050 transaction.request_headers = EXTRA_HEADER;
[email protected]28accfe2009-09-04 23:36:334051 transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
4052 "rg: 50-59 rg: 60-69 rg: 70-79 ";
4053 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4054
4055 // We update the headers with the ones received while revalidating.
4056 std::string expected_headers(
4057 "HTTP/1.1 200 OK\n"
4058 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
4059 "Accept-Ranges: bytes\n"
4060 "ETag: \"foo\"\n"
[email protected]dbd39fb2010-01-08 01:13:364061 "Content-Length: 80\n");
[email protected]28accfe2009-09-04 23:36:334062
4063 EXPECT_EQ(expected_headers, headers);
4064 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4065 EXPECT_EQ(1, cache.disk_cache()->open_count());
4066 EXPECT_EQ(1, cache.disk_cache()->create_count());
4067
4068 RemoveMockTransaction(&kRangeGET_TransactionOK);
4069
4070 // Verify that the disk entry was updated.
4071 EXPECT_EQ(80, entry->GetDataSize(1));
4072 bool truncated = true;
[email protected]02e7a012010-05-10 23:06:334073 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
[email protected]28accfe2009-09-04 23:36:334074 EXPECT_FALSE(truncated);
4075 entry->Close();
4076}
4077
[email protected]dbd39fb2010-01-08 01:13:364078// Tests that we delete truncated entries if the server changes its mind midway.
4079TEST(HttpCache, GET_IncompleteResource2) {
4080 MockHttpCache cache;
4081 cache.http_cache()->set_enable_range_support(true);
4082 AddMockTransaction(&kRangeGET_TransactionOK);
4083
4084 // Create a disk cache entry that stores an incomplete resource.
4085 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:334086 ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry));
[email protected]7d7ad6e42010-01-14 01:30:534087
[email protected]dbd39fb2010-01-08 01:13:364088
4089 // Content-length will be intentionally bad.
4090 std::string raw_headers("HTTP/1.1 200 OK\n"
4091 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
4092 "ETag: \"foo\"\n"
4093 "Accept-Ranges: bytes\n"
4094 "Content-Length: 50\n");
4095 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
4096 raw_headers.size());
4097
4098 net::HttpResponseInfo response;
4099 response.headers = new net::HttpResponseHeaders(raw_headers);
4100 // Set the last argument for this to be an incomplete request.
[email protected]02e7a012010-05-10 23:06:334101 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true));
[email protected]dbd39fb2010-01-08 01:13:364102
4103 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100));
4104 int len = static_cast<int>(base::strlcpy(buf->data(),
4105 "rg: 00-09 rg: 10-19 ", 100));
[email protected]02e7a012010-05-10 23:06:334106 TestCompletionCallback cb;
4107 int rv = entry->WriteData(1, 0, buf, len, &cb, true);
4108 EXPECT_EQ(len, cb.GetResult(rv));
[email protected]dbd39fb2010-01-08 01:13:364109 entry->Close();
4110
4111 // Now make a regular request.
4112 std::string headers;
4113 MockTransaction transaction(kRangeGET_TransactionOK);
4114 transaction.request_headers = EXTRA_HEADER;
4115 transaction.data = "rg: 00-09 rg: 10-19 ";
4116 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4117
4118 // We update the headers with the ones received while revalidating.
4119 std::string expected_headers(
4120 "HTTP/1.1 200 OK\n"
4121 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
4122 "Accept-Ranges: bytes\n"
4123 "ETag: \"foo\"\n"
4124 "Content-Length: 50\n");
4125
4126 EXPECT_EQ(expected_headers, headers);
4127 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4128 EXPECT_EQ(1, cache.disk_cache()->open_count());
4129 EXPECT_EQ(1, cache.disk_cache()->create_count());
4130
4131 RemoveMockTransaction(&kRangeGET_TransactionOK);
4132
4133 // Verify that the disk entry was deleted.
[email protected]02e7a012010-05-10 23:06:334134 ASSERT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
[email protected]dbd39fb2010-01-08 01:13:364135}
4136
[email protected]8a925552009-11-20 23:16:004137// Tests that when we cancel a request that was interrupted, we mark it again
4138// as truncated.
4139TEST(HttpCache, GET_CancelIncompleteResource) {
4140 MockHttpCache cache;
4141 cache.http_cache()->set_enable_range_support(true);
4142 AddMockTransaction(&kRangeGET_TransactionOK);
4143
4144 // Create a disk cache entry that stores an incomplete resource.
4145 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:334146 ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry));
[email protected]8a925552009-11-20 23:16:004147
[email protected]8a925552009-11-20 23:16:004148 std::string raw_headers("HTTP/1.1 200 OK\n"
[email protected]dbd39fb2010-01-08 01:13:364149 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
[email protected]8a925552009-11-20 23:16:004150 "ETag: \"foo\"\n"
4151 "Accept-Ranges: bytes\n"
[email protected]dbd39fb2010-01-08 01:13:364152 "Content-Length: 80\n");
[email protected]8a925552009-11-20 23:16:004153 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
4154 raw_headers.size());
4155
4156 net::HttpResponseInfo response;
4157 response.headers = new net::HttpResponseHeaders(raw_headers);
4158
4159 // Set the last argument for this to be an incomplete request.
[email protected]02e7a012010-05-10 23:06:334160 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true));
[email protected]8a925552009-11-20 23:16:004161
4162 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(100));
4163 int len = static_cast<int>(base::strlcpy(buf->data(), "rg: 00-09 rg: 10-19 ",
4164 buf->size()));
[email protected]02e7a012010-05-10 23:06:334165 TestCompletionCallback cb;
4166 int rv = entry->WriteData(1, 0, buf, len, &cb, true);
4167 EXPECT_EQ(len, cb.GetResult(rv));
[email protected]8a925552009-11-20 23:16:004168
4169 // Now make a regular request.
4170 MockTransaction transaction(kRangeGET_TransactionOK);
4171 transaction.request_headers = EXTRA_HEADER;
4172
4173 MockHttpRequest request(transaction);
4174 Context* c = new Context();
4175 EXPECT_EQ(net::OK, cache.http_cache()->CreateTransaction(&c->trans));
4176
[email protected]02e7a012010-05-10 23:06:334177 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog());
[email protected]21e743202009-12-18 01:31:044178 EXPECT_EQ(net::OK, c->callback.GetResult(rv));
[email protected]8a925552009-11-20 23:16:004179
4180 // Read 20 bytes from the cache, and 10 from the net.
[email protected]21e743202009-12-18 01:31:044181 rv = c->trans->Read(buf, len, &c->callback);
4182 EXPECT_EQ(len, c->callback.GetResult(rv));
4183 rv = c->trans->Read(buf, 10, &c->callback);
4184 EXPECT_EQ(10, c->callback.GetResult(rv));
[email protected]8a925552009-11-20 23:16:004185
4186 // At this point, we are already reading so canceling the request should leave
4187 // a truncated one.
4188 delete c;
4189
4190 RemoveMockTransaction(&kRangeGET_TransactionOK);
4191
4192 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4193 EXPECT_EQ(1, cache.disk_cache()->open_count());
4194 EXPECT_EQ(1, cache.disk_cache()->create_count());
4195
4196 // Verify that the disk entry was updated: now we have 30 bytes.
4197 EXPECT_EQ(30, entry->GetDataSize(1));
4198 bool truncated = false;
[email protected]02e7a012010-05-10 23:06:334199 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
[email protected]8a925552009-11-20 23:16:004200 EXPECT_TRUE(truncated);
4201 entry->Close();
4202}
4203
[email protected]ecd8becb2009-10-02 17:57:454204// Tests that we can handle range requests when we have a truncated entry.
4205TEST(HttpCache, RangeGET_IncompleteResource) {
4206 MockHttpCache cache;
4207 cache.http_cache()->set_enable_range_support(true);
4208 AddMockTransaction(&kRangeGET_TransactionOK);
4209
4210 // Create a disk cache entry that stores an incomplete resource.
4211 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:334212 ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry));
[email protected]ecd8becb2009-10-02 17:57:454213
4214 // Content-length will be intentionally bogus.
4215 std::string raw_headers("HTTP/1.1 200 OK\n"
4216 "Last-Modified: something\n"
4217 "ETag: \"foo\"\n"
4218 "Accept-Ranges: bytes\n"
4219 "Content-Length: 10\n");
4220 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
4221 raw_headers.size());
4222
4223 net::HttpResponseInfo response;
4224 response.headers = new net::HttpResponseHeaders(raw_headers);
4225 // Set the last argument for this to be an incomplete request.
[email protected]02e7a012010-05-10 23:06:334226 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true));
[email protected]ecd8becb2009-10-02 17:57:454227
4228 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100));
4229 int len = static_cast<int>(base::strlcpy(buf->data(),
4230 "rg: 00-09 rg: 10-19 ", 100));
[email protected]02e7a012010-05-10 23:06:334231 TestCompletionCallback cb;
4232 int rv = entry->WriteData(1, 0, buf, len, &cb, true);
4233 EXPECT_EQ(len, cb.GetResult(rv));
[email protected]ecd8becb2009-10-02 17:57:454234 entry->Close();
4235
4236 // Now make a range request.
4237 std::string headers;
4238 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
4239 &headers);
4240
[email protected]8c76ae22010-04-20 22:15:434241 Verify206Response(headers, 40, 49);
[email protected]ecd8becb2009-10-02 17:57:454242 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4243 EXPECT_EQ(1, cache.disk_cache()->open_count());
4244 EXPECT_EQ(2, cache.disk_cache()->create_count());
4245
4246 RemoveMockTransaction(&kRangeGET_TransactionOK);
4247}
4248
initial.commit586acc5fe2008-07-26 22:42:524249TEST(HttpCache, SyncRead) {
4250 MockHttpCache cache;
4251
4252 // This test ensures that a read that completes synchronously does not cause
4253 // any problems.
4254
4255 ScopedMockTransaction transaction(kSimpleGET_Transaction);
4256 transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START |
[email protected]73cae572009-10-22 18:36:194257 TEST_MODE_SYNC_CACHE_READ |
4258 TEST_MODE_SYNC_CACHE_WRITE);
initial.commit586acc5fe2008-07-26 22:42:524259
4260 MockHttpRequest r1(transaction),
[email protected]46773162010-05-07 22:31:204261 r2(transaction),
4262 r3(transaction);
initial.commit586acc5fe2008-07-26 22:42:524263
4264 TestTransactionConsumer c1(cache.http_cache()),
[email protected]46773162010-05-07 22:31:204265 c2(cache.http_cache()),
4266 c3(cache.http_cache());
initial.commit586acc5fe2008-07-26 22:42:524267
[email protected]5a1d7ca2010-04-28 20:12:274268 c1.Start(&r1, net::BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:524269
4270 r2.load_flags |= net::LOAD_ONLY_FROM_CACHE;
[email protected]5a1d7ca2010-04-28 20:12:274271 c2.Start(&r2, net::BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:524272
4273 r3.load_flags |= net::LOAD_ONLY_FROM_CACHE;
[email protected]5a1d7ca2010-04-28 20:12:274274 c3.Start(&r3, net::BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:524275
4276 MessageLoop::current()->Run();
4277
4278 EXPECT_TRUE(c1.is_done());
4279 EXPECT_TRUE(c2.is_done());
4280 EXPECT_TRUE(c3.is_done());
4281
4282 EXPECT_EQ(net::OK, c1.error());
4283 EXPECT_EQ(net::OK, c2.error());
4284 EXPECT_EQ(net::OK, c3.error());
4285}
4286
4287TEST(HttpCache, ValidationResultsIn200) {
4288 MockHttpCache cache;
4289
4290 // This test ensures that a conditional request, which results in a 200
4291 // instead of a 304, properly truncates the existing response data.
4292
4293 // write to the cache
4294 RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
4295
4296 // force this transaction to validate the cache
4297 MockTransaction transaction(kETagGET_Transaction);
4298 transaction.load_flags |= net::LOAD_VALIDATE_CACHE;
4299 RunTransactionTest(cache.http_cache(), transaction);
4300
4301 // read from the cache
4302 RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
4303}
4304
4305TEST(HttpCache, CachedRedirect) {
4306 MockHttpCache cache;
4307
4308 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction);
4309 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently";
4310 kTestTransaction.response_headers = "Location: http://www.bar.com/\n";
4311
4312 MockHttpRequest request(kTestTransaction);
4313 TestCompletionCallback callback;
4314
4315 // write to the cache
4316 {
[email protected]1638d602009-09-24 03:49:174317 scoped_ptr<net::HttpTransaction> trans;
4318 int rv = cache.http_cache()->CreateTransaction(&trans);
4319 EXPECT_EQ(net::OK, rv);
[email protected]af4876d2008-10-21 23:10:574320 ASSERT_TRUE(trans.get());
initial.commit586acc5fe2008-07-26 22:42:524321
[email protected]5a1d7ca2010-04-28 20:12:274322 rv = trans->Start(&request, &callback, net::BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:524323 if (rv == net::ERR_IO_PENDING)
4324 rv = callback.WaitForResult();
4325 ASSERT_EQ(net::OK, rv);
4326
4327 const net::HttpResponseInfo* info = trans->GetResponseInfo();
4328 ASSERT_TRUE(info);
4329
4330 EXPECT_EQ(info->headers->response_code(), 301);
4331
4332 std::string location;
4333 info->headers->EnumerateHeader(NULL, "Location", &location);
4334 EXPECT_EQ(location, "http://www.bar.com/");
4335
[email protected]af4876d2008-10-21 23:10:574336 // Destroy transaction when going out of scope. We have not actually
4337 // read the response body -- want to test that it is still getting cached.
initial.commit586acc5fe2008-07-26 22:42:524338 }
4339 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4340 EXPECT_EQ(0, cache.disk_cache()->open_count());
4341 EXPECT_EQ(1, cache.disk_cache()->create_count());
4342
4343 // read from the cache
4344 {
[email protected]1638d602009-09-24 03:49:174345 scoped_ptr<net::HttpTransaction> trans;
4346 int rv = cache.http_cache()->CreateTransaction(&trans);
4347 EXPECT_EQ(net::OK, rv);
[email protected]af4876d2008-10-21 23:10:574348 ASSERT_TRUE(trans.get());
initial.commit586acc5fe2008-07-26 22:42:524349
[email protected]5a1d7ca2010-04-28 20:12:274350 rv = trans->Start(&request, &callback, net::BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:524351 if (rv == net::ERR_IO_PENDING)
4352 rv = callback.WaitForResult();
4353 ASSERT_EQ(net::OK, rv);
4354
4355 const net::HttpResponseInfo* info = trans->GetResponseInfo();
4356 ASSERT_TRUE(info);
4357
4358 EXPECT_EQ(info->headers->response_code(), 301);
4359
4360 std::string location;
4361 info->headers->EnumerateHeader(NULL, "Location", &location);
4362 EXPECT_EQ(location, "http://www.bar.com/");
4363
[email protected]af4876d2008-10-21 23:10:574364 // Destroy transaction when going out of scope. We have not actually
4365 // read the response body -- want to test that it is still getting cached.
initial.commit586acc5fe2008-07-26 22:42:524366 }
4367 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4368 EXPECT_EQ(1, cache.disk_cache()->open_count());
4369 EXPECT_EQ(1, cache.disk_cache()->create_count());
4370}
4371
4372TEST(HttpCache, CacheControlNoStore) {
4373 MockHttpCache cache;
4374
4375 ScopedMockTransaction transaction(kSimpleGET_Transaction);
4376 transaction.response_headers = "cache-control: no-store\n";
4377
4378 // initial load
4379 RunTransactionTest(cache.http_cache(), transaction);
4380
4381 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4382 EXPECT_EQ(0, cache.disk_cache()->open_count());
4383 EXPECT_EQ(1, cache.disk_cache()->create_count());
4384
4385 // try loading again; it should result in a network fetch
4386 RunTransactionTest(cache.http_cache(), transaction);
4387
4388 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4389 EXPECT_EQ(0, cache.disk_cache()->open_count());
4390 EXPECT_EQ(2, cache.disk_cache()->create_count());
4391
4392 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:334393 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry));
initial.commit586acc5fe2008-07-26 22:42:524394}
4395
4396TEST(HttpCache, CacheControlNoStore2) {
4397 // this test is similar to the above test, except that the initial response
4398 // is cachable, but when it is validated, no-store is received causing the
4399 // cached document to be deleted.
4400 MockHttpCache cache;
4401
4402 ScopedMockTransaction transaction(kETagGET_Transaction);
4403
4404 // initial load
4405 RunTransactionTest(cache.http_cache(), transaction);
4406
4407 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4408 EXPECT_EQ(0, cache.disk_cache()->open_count());
4409 EXPECT_EQ(1, cache.disk_cache()->create_count());
4410
4411 // try loading again; it should result in a network fetch
4412 transaction.load_flags = net::LOAD_VALIDATE_CACHE;
4413 transaction.response_headers = "cache-control: no-store\n";
4414 RunTransactionTest(cache.http_cache(), transaction);
4415
4416 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4417 EXPECT_EQ(1, cache.disk_cache()->open_count());
4418 EXPECT_EQ(1, cache.disk_cache()->create_count());
4419
4420 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:334421 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry));
initial.commit586acc5fe2008-07-26 22:42:524422}
4423
4424TEST(HttpCache, CacheControlNoStore3) {
4425 // this test is similar to the above test, except that the response is a 304
4426 // instead of a 200. this should never happen in practice, but it seems like
4427 // a good thing to verify that we still destroy the cache entry.
4428 MockHttpCache cache;
4429
4430 ScopedMockTransaction transaction(kETagGET_Transaction);
4431
4432 // initial load
4433 RunTransactionTest(cache.http_cache(), transaction);
4434
4435 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4436 EXPECT_EQ(0, cache.disk_cache()->open_count());
4437 EXPECT_EQ(1, cache.disk_cache()->create_count());
4438
4439 // try loading again; it should result in a network fetch
4440 transaction.load_flags = net::LOAD_VALIDATE_CACHE;
4441 transaction.response_headers = "cache-control: no-store\n";
4442 transaction.status = "HTTP/1.1 304 Not Modified";
4443 RunTransactionTest(cache.http_cache(), transaction);
4444
4445 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4446 EXPECT_EQ(1, cache.disk_cache()->open_count());
4447 EXPECT_EQ(1, cache.disk_cache()->create_count());
4448
4449 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:334450 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry));
initial.commit586acc5fe2008-07-26 22:42:524451}
4452
4453// Ensure that we don't cache requests served over bad HTTPS.
4454TEST(HttpCache, SimpleGET_SSLError) {
4455 MockHttpCache cache;
4456
4457 MockTransaction transaction = kSimpleGET_Transaction;
4458 transaction.cert_status = net::CERT_STATUS_REVOKED;
4459 ScopedMockTransaction scoped_transaction(transaction);
4460
4461 // write to the cache
4462 RunTransactionTest(cache.http_cache(), transaction);
4463
4464 // Test that it was not cached.
4465 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
4466
4467 MockHttpRequest request(transaction);
4468 TestCompletionCallback callback;
4469
[email protected]1638d602009-09-24 03:49:174470 scoped_ptr<net::HttpTransaction> trans;
4471 int rv = cache.http_cache()->CreateTransaction(&trans);
4472 EXPECT_EQ(net::OK, rv);
[email protected]af4876d2008-10-21 23:10:574473 ASSERT_TRUE(trans.get());
initial.commit586acc5fe2008-07-26 22:42:524474
[email protected]5a1d7ca2010-04-28 20:12:274475 rv = trans->Start(&request, &callback, net::BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:524476 if (rv == net::ERR_IO_PENDING)
4477 rv = callback.WaitForResult();
4478 ASSERT_EQ(net::ERR_CACHE_MISS, rv);
initial.commit586acc5fe2008-07-26 22:42:524479}
[email protected]3e2d38d2009-02-14 02:01:184480
4481// Ensure that we don't crash by if left-behind transactions.
4482TEST(HttpCache, OutlivedTransactions) {
4483 MockHttpCache* cache = new MockHttpCache;
4484
[email protected]1638d602009-09-24 03:49:174485 scoped_ptr<net::HttpTransaction> trans;
4486 int rv = cache->http_cache()->CreateTransaction(&trans);
4487 EXPECT_EQ(net::OK, rv);
4488
[email protected]b367d9a52009-02-27 01:02:514489 delete cache;
[email protected]1638d602009-09-24 03:49:174490 trans.reset();
[email protected]3e2d38d2009-02-14 02:01:184491}
[email protected]981797002009-06-05 07:14:154492
4493// Test that the disabled mode works.
4494TEST(HttpCache, CacheDisabledMode) {
4495 MockHttpCache cache;
4496
4497 // write to the cache
4498 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
4499
4500 // go into disabled mode
4501 cache.http_cache()->set_mode(net::HttpCache::DISABLE);
4502
4503 // force this transaction to write to the cache again
4504 MockTransaction transaction(kSimpleGET_Transaction);
4505
4506 RunTransactionTest(cache.http_cache(), transaction);
4507
4508 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4509 EXPECT_EQ(0, cache.disk_cache()->open_count());
4510 EXPECT_EQ(1, cache.disk_cache()->create_count());
4511}
[email protected]207d58c72009-09-04 18:59:294512
4513// Other tests check that the response headers of the cached response
4514// get updated on 304. Here we specifically check that the
[email protected]ca2f19e2009-09-04 22:53:164515// HttpResponseHeaders::request_time and HttpResponseHeaders::response_time
4516// fields also gets updated.
[email protected]207d58c72009-09-04 18:59:294517// http://crbug.com/20594.
[email protected]ca2f19e2009-09-04 22:53:164518TEST(HttpCache, UpdatesRequestResponseTimeOn304) {
[email protected]207d58c72009-09-04 18:59:294519 MockHttpCache cache;
4520
4521 const char* kUrl = "http://foobar";
4522 const char* kData = "body";
4523
4524 MockTransaction mock_network_response = { 0 };
4525 mock_network_response.url = kUrl;
4526
4527 AddMockTransaction(&mock_network_response);
4528
4529 // Request |kUrl|, causing |kNetResponse1| to be written to the cache.
4530
4531 MockTransaction request = { 0 };
4532 request.url = kUrl;
4533 request.method = "GET";
4534 request.request_headers = "";
4535 request.data = kData;
4536
4537 static const Response kNetResponse1 = {
4538 "HTTP/1.1 200 OK",
4539 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
4540 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
4541 kData
4542 };
4543
4544 kNetResponse1.AssignTo(&mock_network_response);
4545
4546 RunTransactionTest(cache.http_cache(), request);
4547
4548 // Request |kUrl| again, this time validating the cache and getting
4549 // a 304 back.
4550
4551 request.load_flags = net::LOAD_VALIDATE_CACHE;
4552
4553 static const Response kNetResponse2 = {
4554 "HTTP/1.1 304 Not Modified",
4555 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n",
4556 ""
4557 };
4558
4559 kNetResponse2.AssignTo(&mock_network_response);
4560
[email protected]ca2f19e2009-09-04 22:53:164561 base::Time request_time = base::Time() + base::TimeDelta::FromHours(1234);
4562 base::Time response_time = base::Time() + base::TimeDelta::FromHours(1235);
4563
4564 mock_network_response.request_time = request_time;
4565 mock_network_response.response_time = response_time;
[email protected]207d58c72009-09-04 18:59:294566
4567 net::HttpResponseInfo response;
4568 RunTransactionTestWithResponseInfo(cache.http_cache(), request, &response);
4569
[email protected]ca2f19e2009-09-04 22:53:164570 // The request and response times should have been updated.
4571 EXPECT_EQ(request_time.ToInternalValue(),
4572 response.request_time.ToInternalValue());
4573 EXPECT_EQ(response_time.ToInternalValue(),
[email protected]207d58c72009-09-04 18:59:294574 response.response_time.ToInternalValue());
4575
4576 std::string headers;
4577 response.headers->GetNormalizedHeaders(&headers);
4578
4579 EXPECT_EQ("HTTP/1.1 200 OK\n"
4580 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
4581 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
4582 headers);
4583
4584 RemoveMockTransaction(&mock_network_response);
4585}
[email protected]47b95052010-03-02 19:10:074586
4587// Tests that we can write metadata to an entry.
4588TEST(HttpCache, WriteMetadata_OK) {
4589 MockHttpCache cache;
4590
4591 // Write to the cache
4592 net::HttpResponseInfo response;
4593 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
4594 &response);
4595 EXPECT_TRUE(response.metadata.get() == NULL);
4596
4597 // Trivial call.
4598 cache.http_cache()->WriteMetadata(GURL("foo"), Time::Now(), NULL, 0);
4599
4600 // Write meta data to the same entry.
4601 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50));
4602 memset(buf->data(), 0, buf->size());
4603 base::strlcpy(buf->data(), "Hi there", buf->size());
4604 cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url),
4605 response.response_time, buf, buf->size());
4606
4607 // Release the buffer before the operation takes place.
4608 buf = NULL;
4609
4610 // Makes sure we finish pending operations.
4611 MessageLoop::current()->RunAllPending();
4612
4613 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
4614 &response);
4615 ASSERT_TRUE(response.metadata.get() != NULL);
4616 EXPECT_EQ(50, response.metadata->size());
4617 EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there"));
4618
4619 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4620 EXPECT_EQ(2, cache.disk_cache()->open_count());
4621 EXPECT_EQ(1, cache.disk_cache()->create_count());
4622}
4623
4624// Tests that we only write metadata to an entry if the time stamp matches.
4625TEST(HttpCache, WriteMetadata_Fail) {
4626 MockHttpCache cache;
4627
4628 // Write to the cache
4629 net::HttpResponseInfo response;
4630 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
4631 &response);
4632 EXPECT_TRUE(response.metadata.get() == NULL);
4633
4634 // Attempt to write meta data to the same entry.
4635 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50));
4636 memset(buf->data(), 0, buf->size());
4637 base::strlcpy(buf->data(), "Hi there", buf->size());
4638 base::Time expected_time = response.response_time -
4639 base::TimeDelta::FromMilliseconds(20);
4640 cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url),
4641 expected_time, buf, buf->size());
4642
4643 // Makes sure we finish pending operations.
4644 MessageLoop::current()->RunAllPending();
4645
4646 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
4647 &response);
4648 EXPECT_TRUE(response.metadata.get() == NULL);
4649
4650 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4651 EXPECT_EQ(2, cache.disk_cache()->open_count());
4652 EXPECT_EQ(1, cache.disk_cache()->create_count());
4653}
4654
4655// Tests that we can read metadata after validating the entry and with READ mode
4656// transactions.
4657TEST(HttpCache, ReadMetadata) {
4658 MockHttpCache cache;
4659
4660 // Write to the cache
4661 net::HttpResponseInfo response;
4662 RunTransactionTestWithResponseInfo(cache.http_cache(),
4663 kTypicalGET_Transaction, &response);
4664 EXPECT_TRUE(response.metadata.get() == NULL);
4665
4666 // Write meta data to the same entry.
4667 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50));
4668 memset(buf->data(), 0, buf->size());
4669 base::strlcpy(buf->data(), "Hi there", buf->size());
4670 cache.http_cache()->WriteMetadata(GURL(kTypicalGET_Transaction.url),
4671 response.response_time, buf, buf->size());
4672
4673 // Makes sure we finish pending operations.
4674 MessageLoop::current()->RunAllPending();
4675
4676 // Start with a READ mode transaction.
4677 MockTransaction trans1(kTypicalGET_Transaction);
4678 trans1.load_flags = net::LOAD_ONLY_FROM_CACHE;
4679
4680 RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response);
4681 ASSERT_TRUE(response.metadata.get() != NULL);
4682 EXPECT_EQ(50, response.metadata->size());
4683 EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there"));
4684
4685 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4686 EXPECT_EQ(2, cache.disk_cache()->open_count());
4687 EXPECT_EQ(1, cache.disk_cache()->create_count());
4688 MessageLoop::current()->RunAllPending();
4689
4690 // Now make sure that the entry is re-validated with the server.
4691 trans1.load_flags = net::LOAD_VALIDATE_CACHE;
4692 trans1.status = "HTTP/1.1 304 Not Modified";
4693 AddMockTransaction(&trans1);
4694
4695 response.metadata = NULL;
4696 RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response);
4697 EXPECT_TRUE(response.metadata.get() != NULL);
4698
4699 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4700 EXPECT_EQ(3, cache.disk_cache()->open_count());
4701 EXPECT_EQ(1, cache.disk_cache()->create_count());
4702 MessageLoop::current()->RunAllPending();
4703 RemoveMockTransaction(&trans1);
4704
4705 // Now return 200 when validating the entry so the metadata will be lost.
4706 MockTransaction trans2(kTypicalGET_Transaction);
4707 trans2.load_flags = net::LOAD_VALIDATE_CACHE;
4708 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response);
4709 EXPECT_TRUE(response.metadata.get() == NULL);
4710
4711 EXPECT_EQ(3, cache.network_layer()->transaction_count());
4712 EXPECT_EQ(4, cache.disk_cache()->open_count());
4713 EXPECT_EQ(1, cache.disk_cache()->create_count());
4714}