[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 1 | // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #include "net/http/http_cache.h" |
| 6 | |
[email protected] | 2314403 | 2008-09-08 20:51:30 | [diff] [blame] | 7 | #include "base/hash_tables.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 8 | #include "base/message_loop.h" |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 9 | #include "base/scoped_vector.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 10 | #include "base/string_util.h" |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 11 | #include "net/base/cache_type.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 12 | #include "net/base/net_errors.h" |
| 13 | #include "net/base/load_flags.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 14 | #include "net/base/net_log_unittest.h" |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 15 | #include "net/base/ssl_cert_request_info.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 16 | #include "net/disk_cache/disk_cache.h" |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 17 | #include "net/http/http_byte_range.h" |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 18 | #include "net/http/http_request_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 19 | #include "net/http/http_request_info.h" |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 20 | #include "net/http/http_response_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | #include "net/http/http_response_info.h" |
| 22 | #include "net/http/http_transaction.h" |
| 23 | #include "net/http/http_transaction_unittest.h" |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 24 | #include "net/http/http_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 25 | #include "testing/gtest/include/gtest/gtest.h" |
| 26 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 27 | using base::Time; |
| 28 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 29 | namespace { |
| 30 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 31 | int 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] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 51 | // We can override the test mode for a given operation by setting this global |
| 52 | // variable. Just remember to reset it after the test!. |
| 53 | int g_test_mode = 0; |
| 54 | |
| 55 | // Returns the test mode after considering the global override. |
| 56 | int GetEffectiveTestMode(int test_mode) { |
| 57 | if (!g_test_mode) |
| 58 | return test_mode; |
| 59 | |
| 60 | return g_test_mode; |
| 61 | } |
| 62 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 63 | //----------------------------------------------------------------------------- |
| 64 | // mock disk cache (a very basic memory cache implementation) |
| 65 | |
[email protected] | 91f4caa | 2010-04-19 16:50:50 | [diff] [blame] | 66 | static const int kNumCacheEntryDataIndices = 3; |
| 67 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 68 | class MockDiskEntry : public disk_cache::Entry, |
| 69 | public base::RefCounted<MockDiskEntry> { |
| 70 | public: |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 71 | MockDiskEntry() |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 72 | : test_mode_(0), doomed_(false), sparse_(false), fail_requests_(false), |
| 73 | busy_(false), delayed_(false) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | 6f40bf7 | 2009-07-23 17:52:37 | [diff] [blame] | 76 | explicit MockDiskEntry(const std::string& key) |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 77 | : key_(key), doomed_(false), sparse_(false), fail_requests_(false), |
| 78 | busy_(false), delayed_(false) { |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 79 | test_mode_ = GetTestModeForEntry(key); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 80 | } |
| 81 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 82 | 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] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 93 | if (fail_requests_) |
| 94 | return std::string(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 95 | 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] | 91f4caa | 2010-04-19 16:50:50 | [diff] [blame] | 107 | DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 108 | return static_cast<int32>(data_[index].size()); |
| 109 | } |
| 110 | |
[email protected] | 74a85ce | 2009-02-12 00:03:19 | [diff] [blame] | 111 | virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 112 | net::CompletionCallback* callback) { |
[email protected] | 91f4caa | 2010-04-19 16:50:50 | [diff] [blame] | 113 | DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 114 | DCHECK(callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 115 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 116 | if (fail_requests_) |
| 117 | return net::ERR_CACHE_READ_FAILURE; |
| 118 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 119 | if (offset < 0 || offset > static_cast<int>(data_[index].size())) |
| 120 | return net::ERR_FAILED; |
[email protected] | cad155b | 2008-09-23 14:44:27 | [diff] [blame] | 121 | if (static_cast<size_t>(offset) == data_[index].size()) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 122 | return 0; |
| 123 | |
| 124 | int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); |
[email protected] | 74a85ce | 2009-02-12 00:03:19 | [diff] [blame] | 125 | memcpy(buf->data(), &data_[index][offset], num); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 126 | |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 127 | if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 128 | return num; |
| 129 | |
| 130 | CallbackLater(callback, num); |
| 131 | return net::ERR_IO_PENDING; |
| 132 | } |
| 133 | |
[email protected] | 74a85ce | 2009-02-12 00:03:19 | [diff] [blame] | 134 | virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 135 | net::CompletionCallback* callback, bool truncate) { |
[email protected] | 91f4caa | 2010-04-19 16:50:50 | [diff] [blame] | 136 | DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 137 | DCHECK(callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 138 | DCHECK(truncate); |
| 139 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 140 | if (fail_requests_) |
| 141 | return net::ERR_CACHE_READ_FAILURE; |
| 142 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 143 | 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] | 74a85ce | 2009-02-12 00:03:19 | [diff] [blame] | 148 | memcpy(&data_[index][offset], buf->data(), buf_len); |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 149 | |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 150 | if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 151 | return buf_len; |
| 152 | |
| 153 | CallbackLater(callback, buf_len); |
| 154 | return net::ERR_IO_PENDING; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 157 | virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 158 | net::CompletionCallback* callback) { |
| 159 | DCHECK(callback); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 160 | if (!sparse_ || busy_) |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 161 | return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 162 | if (offset < 0) |
| 163 | return net::ERR_FAILED; |
| 164 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 165 | if (fail_requests_) |
| 166 | return net::ERR_CACHE_READ_FAILURE; |
| 167 | |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 168 | 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 177 | if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 178 | return num; |
| 179 | |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 180 | CallbackLater(callback, num); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 181 | busy_ = true; |
| 182 | delayed_ = false; |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 183 | return net::ERR_IO_PENDING; |
| 184 | } |
| 185 | |
| 186 | virtual int WriteSparseData(int64 offset, net::IOBuffer* buf, int buf_len, |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 187 | net::CompletionCallback* callback) { |
| 188 | DCHECK(callback); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 189 | if (busy_) |
| 190 | return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 191 | 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] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 201 | if (fail_requests_) |
| 202 | return net::ERR_CACHE_READ_FAILURE; |
| 203 | |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 204 | 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 211 | if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 212 | return buf_len; |
| 213 | |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 214 | CallbackLater(callback, buf_len); |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 215 | return net::ERR_IO_PENDING; |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 216 | } |
| 217 | |
[email protected] | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 218 | virtual int GetAvailableRange(int64 offset, int len, int64* start, |
| 219 | net::CompletionCallback* callback) { |
| 220 | DCHECK(callback); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 221 | if (!sparse_ || busy_) |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 222 | return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 223 | if (offset < 0) |
| 224 | return net::ERR_FAILED; |
| 225 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 226 | if (fail_requests_) |
| 227 | return net::ERR_CACHE_READ_FAILURE; |
| 228 | |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 229 | *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] | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 249 | 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] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 254 | } |
| 255 | |
[email protected] | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 256 | virtual bool CouldBeSparse() const { |
| 257 | return sparse_; |
[email protected] | 3cf35d9e | 2009-11-05 23:27:41 | [diff] [blame] | 258 | } |
| 259 | |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 260 | 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] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 268 | if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 269 | 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] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 277 | // Fail most subsequent requests. |
| 278 | void set_fail_requests() { fail_requests_ = true; } |
| 279 | |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 280 | // 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 291 | private: |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 292 | friend class base::RefCounted<MockDiskEntry>; |
| 293 | |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 294 | struct CallbackInfo { |
| 295 | scoped_refptr<MockDiskEntry> entry; |
| 296 | net::CompletionCallback* callback; |
| 297 | int result; |
| 298 | }; |
| 299 | |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 300 | ~MockDiskEntry() {} |
| 301 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 302 | // 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] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 306 | if (ignore_callbacks_) |
| 307 | return StoreAndDeliverCallbacks(true, this, callback, result); |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 308 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 309 | this, &MockDiskEntry::RunCallback, callback, result)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 310 | } |
| 311 | void RunCallback(net::CompletionCallback* callback, int result) { |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 312 | 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 327 | callback->Run(result); |
| 328 | } |
| 329 | |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 330 | // 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 348 | std::string key_; |
[email protected] | 91f4caa | 2010-04-19 16:50:50 | [diff] [blame] | 349 | std::vector<char> data_[kNumCacheEntryDataIndices]; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 350 | int test_mode_; |
| 351 | bool doomed_; |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 352 | bool sparse_; |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 353 | bool fail_requests_; |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 354 | bool busy_; |
| 355 | bool delayed_; |
| 356 | static bool cancel_; |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 357 | static bool ignore_callbacks_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 358 | }; |
| 359 | |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 360 | // Statics. |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 361 | bool MockDiskEntry::cancel_ = false; |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 362 | bool MockDiskEntry::ignore_callbacks_ = false; |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 363 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 364 | class MockDiskCache : public disk_cache::Backend { |
| 365 | public: |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 366 | MockDiskCache() |
| 367 | : open_count_(0), create_count_(0), fail_requests_(false), |
| 368 | soft_failures_(false) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | ~MockDiskCache() { |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame^] | 372 | ReleaseAll(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | virtual int32 GetEntryCount() const { |
| 376 | return static_cast<int32>(entries_.size()); |
| 377 | } |
| 378 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 379 | virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, |
| 380 | net::CompletionCallback* callback) { |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 381 | DCHECK(callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 382 | if (fail_requests_) |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 383 | return net::ERR_CACHE_OPEN_FAILURE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 384 | |
| 385 | EntryMap::iterator it = entries_.find(key); |
| 386 | if (it == entries_.end()) |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 387 | return net::ERR_CACHE_OPEN_FAILURE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 388 | |
| 389 | if (it->second->is_doomed()) { |
| 390 | it->second->Release(); |
| 391 | entries_.erase(it); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 392 | return net::ERR_CACHE_OPEN_FAILURE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | open_count_++; |
| 396 | |
| 397 | it->second->AddRef(); |
| 398 | *entry = it->second; |
| 399 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 400 | if (soft_failures_) |
| 401 | it->second->set_fail_requests(); |
| 402 | |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 403 | if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 404 | return net::OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 405 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 406 | CallbackLater(callback, net::OK); |
| 407 | return net::ERR_IO_PENDING; |
[email protected] | 3cf35d9e | 2009-11-05 23:27:41 | [diff] [blame] | 408 | } |
| 409 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 410 | virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, |
| 411 | net::CompletionCallback* callback) { |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 412 | DCHECK(callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 413 | if (fail_requests_) |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 414 | return net::ERR_CACHE_CREATE_FAILURE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 415 | |
| 416 | EntryMap::iterator it = entries_.find(key); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 417 | if (it != entries_.end()) { |
| 418 | DCHECK(it->second->is_doomed()); |
| 419 | it->second->Release(); |
| 420 | entries_.erase(it); |
| 421 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 422 | |
| 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] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 433 | if (soft_failures_) |
| 434 | new_entry->set_fail_requests(); |
| 435 | |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 436 | if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 437 | return net::OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 438 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 439 | CallbackLater(callback, net::OK); |
| 440 | return net::ERR_IO_PENDING; |
[email protected] | 3cf35d9e | 2009-11-05 23:27:41 | [diff] [blame] | 441 | } |
| 442 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 443 | virtual int DoomEntry(const std::string& key, |
| 444 | net::CompletionCallback* callback) { |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 445 | DCHECK(callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 446 | EntryMap::iterator it = entries_.find(key); |
| 447 | if (it != entries_.end()) { |
| 448 | it->second->Release(); |
| 449 | entries_.erase(it); |
| 450 | } |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 451 | |
| 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 457 | } |
| 458 | |
[email protected] | 3cf35d9e | 2009-11-05 23:27:41 | [diff] [blame] | 459 | virtual int DoomAllEntries(net::CompletionCallback* callback) { |
| 460 | return net::ERR_NOT_IMPLEMENTED; |
| 461 | } |
| 462 | |
[email protected] | 3cf35d9e | 2009-11-05 23:27:41 | [diff] [blame] | 463 | 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] | 3cf35d9e | 2009-11-05 23:27:41 | [diff] [blame] | 469 | virtual int DoomEntriesSince(const base::Time initial_time, |
| 470 | net::CompletionCallback* callback) { |
| 471 | return net::ERR_NOT_IMPLEMENTED; |
| 472 | } |
| 473 | |
[email protected] | 3cf35d9e | 2009-11-05 23:27:41 | [diff] [blame] | 474 | virtual int OpenNextEntry(void** iter, disk_cache::Entry** next_entry, |
| 475 | net::CompletionCallback* callback) { |
| 476 | return net::ERR_NOT_IMPLEMENTED; |
| 477 | } |
| 478 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 479 | 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] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 494 | // Return entries that fail some of their requests. |
| 495 | void set_soft_failures(bool value) { soft_failures_ = value; } |
| 496 | |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame^] | 497 | void ReleaseAll() { |
| 498 | EntryMap::iterator it = entries_.begin(); |
| 499 | for (; it != entries_.end(); ++it) |
| 500 | it->second->Release(); |
| 501 | entries_.clear(); |
| 502 | } |
| 503 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 504 | private: |
[email protected] | 2314403 | 2008-09-08 20:51:30 | [diff] [blame] | 505 | typedef base::hash_map<std::string, MockDiskEntry*> EntryMap; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 506 | |
| 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] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 516 | net::CompletionCallback* callback_; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 517 | 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 526 | EntryMap entries_; |
| 527 | int open_count_; |
| 528 | int create_count_; |
| 529 | bool fail_requests_; |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 530 | bool soft_failures_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 531 | }; |
| 532 | |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 533 | class 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 542 | class MockHttpCache { |
| 543 | public: |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 544 | MockHttpCache() |
| 545 | : http_cache_(new MockNetworkLayer(), new MockBackendFactory()) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 546 | } |
| 547 | |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 548 | explicit MockHttpCache(net::HttpCache::BackendFactory* disk_cache_factory) |
| 549 | : http_cache_(new MockNetworkLayer(), disk_cache_factory) { |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 550 | } |
| 551 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 552 | 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] | 6a98903 | 2010-06-14 19:05:33 | [diff] [blame] | 558 | 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 563 | } |
| 564 | |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 565 | // 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 615 | private: |
| 616 | net::HttpCache http_cache_; |
| 617 | }; |
| 618 | |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 619 | // This version of the disk cache doesn't invoke CreateEntry callbacks. |
| 620 | class 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 626 | |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 627 | class 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. |
| 637 | class 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 679 | //----------------------------------------------------------------------------- |
| 680 | // helpers |
| 681 | |
| 682 | void 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] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 688 | std::string expected(trans_info.data); |
| 689 | EXPECT_EQ(expected, content); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 690 | } |
| 691 | |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 692 | void RunTransactionTestWithRequestAndLog(net::HttpCache* cache, |
| 693 | const MockTransaction& trans_info, |
| 694 | const MockHttpRequest& request, |
| 695 | net::HttpResponseInfo* response_info, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 696 | const net::BoundNetLog& net_log) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 697 | TestCompletionCallback callback; |
| 698 | |
| 699 | // write to the cache |
| 700 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 701 | scoped_ptr<net::HttpTransaction> trans; |
| 702 | int rv = cache->CreateTransaction(&trans); |
| 703 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 704 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 705 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 706 | rv = trans->Start(&request, &callback, net_log); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 707 | 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] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 714 | if (response_info) |
| 715 | *response_info = *response; |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 716 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 717 | ReadAndVerifyTransaction(trans.get(), trans_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 718 | } |
| 719 | |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 720 | void 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] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 725 | response_info, net::BoundNetLog()); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | void RunTransactionTestWithLog(net::HttpCache* cache, |
| 729 | const MockTransaction& trans_info, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 730 | const net::BoundNetLog& log) { |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 731 | RunTransactionTestWithRequestAndLog( |
| 732 | cache, trans_info, MockHttpRequest(trans_info), NULL, log); |
| 733 | } |
| 734 | |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 735 | void RunTransactionTest(net::HttpCache* cache, |
| 736 | const MockTransaction& trans_info) { |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 737 | RunTransactionTestWithLog(cache, trans_info, net::BoundNetLog()); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 738 | } |
| 739 | |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 740 | void 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] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 747 | void RunTransactionTestWithResponse(net::HttpCache* cache, |
| 748 | const MockTransaction& trans_info, |
| 749 | std::string* response_headers) { |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 750 | net::HttpResponseInfo response; |
| 751 | RunTransactionTestWithResponseInfo(cache, trans_info, &response); |
| 752 | response.headers->GetNormalizedHeaders(response_headers); |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 753 | } |
| 754 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 755 | // This class provides a handler for kFastNoStoreGET_Transaction so that the |
| 756 | // no-store header can be included on demand. |
| 757 | class 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 | }; |
| 778 | bool FastTransactionServer::no_store; |
| 779 | |
| 780 | const MockTransaction kFastNoStoreGET_Transaction = { |
| 781 | "http://www.google.com/nostore", |
| 782 | "GET", |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 783 | base::Time(), |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 784 | "", |
| 785 | net::LOAD_VALIDATE_CACHE, |
| 786 | "HTTP/1.1 200 OK", |
| 787 | "Cache-Control: max-age=10000\n", |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 788 | base::Time(), |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 789 | "<html><body>Google Blah Blah</body></html>", |
| 790 | TEST_MODE_SYNC_NET_START, |
| 791 | &FastTransactionServer::FastNoStoreHandler, |
| 792 | 0 |
| 793 | }; |
| 794 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 795 | // This class provides a handler for kRangeGET_TransactionOK so that the range |
| 796 | // request can be served on demand. |
| 797 | class RangeTransactionServer { |
| 798 | public: |
| 799 | RangeTransactionServer() { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 800 | not_modified_ = false; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 801 | modified_ = false; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 802 | bad_200_ = false; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 803 | } |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 804 | ~RangeTransactionServer() { |
| 805 | not_modified_ = false; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 806 | modified_ = false; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 807 | bad_200_ = false; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 808 | } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 809 | |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 810 | // Returns only 416 or 304 when set. |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 811 | void set_not_modified(bool value) { not_modified_ = value; } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 812 | |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 813 | // Returns 206 when revalidating a range (instead of 304). |
| 814 | void set_modified(bool value) { modified_ = value; } |
| 815 | |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 816 | // Returns 200 instead of 206 (a malformed response overall). |
| 817 | void set_bad_200(bool value) { bad_200_ = value; } |
| 818 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 819 | 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] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 825 | static bool not_modified_; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 826 | static bool modified_; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 827 | static bool bad_200_; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 828 | DISALLOW_COPY_AND_ASSIGN(RangeTransactionServer); |
| 829 | }; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 830 | bool RangeTransactionServer::not_modified_ = false; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 831 | bool RangeTransactionServer::modified_ = false; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 832 | bool RangeTransactionServer::bad_200_ = false; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 833 | |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 834 | // A dummy extra header that must be preserved on a given request. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 835 | #define EXTRA_HEADER "Extra: header" |
| 836 | static const char kExtraHeaderKey[] = "Extra"; |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 837 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 838 | // Static. |
| 839 | void RangeTransactionServer::RangeHandler(const net::HttpRequestInfo* request, |
| 840 | std::string* response_status, |
| 841 | std::string* response_headers, |
| 842 | std::string* response_data) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 843 | if (request->extra_headers.IsEmpty()) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 844 | response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 845 | return; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 846 | } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 847 | |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 848 | // We want to make sure we don't delete extra headers. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 849 | EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 850 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 851 | if (not_modified_) { |
| 852 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 853 | return; |
| 854 | } |
| 855 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 856 | std::vector<net::HttpByteRange> ranges; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 857 | std::string range_header; |
| 858 | if (!request->extra_headers.GetHeader( |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 859 | net::HttpRequestHeaders::kRange, &range_header) || |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 860 | !net::HttpUtil::ParseRangeHeader(range_header, &ranges) || |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 861 | ranges.size() != 1) |
| 862 | return; |
| 863 | // We can handle this range request. |
| 864 | net::HttpByteRange byte_range = ranges[0]; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 865 | 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] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 870 | 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] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 876 | std::string content_range = StringPrintf("Content-Range: bytes %d-%d/80\n", |
| 877 | start, end); |
| 878 | response_headers->append(content_range); |
| 879 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 880 | if (!request->extra_headers.HasHeader("If-None-Match") || modified_) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 881 | 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] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 885 | *response_data = data; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 886 | |
| 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] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 895 | 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] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 899 | } else { |
| 900 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 901 | response_data->clear(); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | const MockTransaction kRangeGET_TransactionOK = { |
| 906 | "http://www.google.com/range", |
| 907 | "GET", |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 908 | base::Time(), |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 909 | "Range: bytes = 40-49\r\n" |
| 910 | EXTRA_HEADER, |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 911 | 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] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 917 | base::Time(), |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 918 | "rg: 40-49 ", |
| 919 | TEST_MODE_NORMAL, |
| 920 | &RangeTransactionServer::RangeHandler, |
| 921 | 0 |
| 922 | }; |
| 923 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 924 | // Verifies the response headers (|response|) match a partial content |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 925 | // response for the range starting at |start| and ending at |end|. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 926 | void Verify206Response(std::string response, int start, int end) { |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 927 | 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 932 | ASSERT_EQ(206, headers->response_code()); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 933 | |
| 934 | int64 range_start, range_end, object_size; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 935 | ASSERT_TRUE( |
| 936 | headers->GetContentRange(&range_start, &range_end, &object_size)); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 937 | int64 content_length = headers->GetContentLength(); |
| 938 | |
| 939 | int length = end - start + 1; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 940 | ASSERT_EQ(length, content_length); |
| 941 | ASSERT_EQ(start, range_start); |
| 942 | ASSERT_EQ(end, range_end); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 943 | } |
| 944 | |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 945 | // Helper to represent a network HTTP response. |
| 946 | struct 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] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 963 | struct Context { |
| 964 | Context() : result(net::ERR_IO_PENDING) {} |
| 965 | |
| 966 | int result; |
| 967 | TestCompletionCallback callback; |
| 968 | scoped_ptr<net::HttpTransaction> trans; |
| 969 | }; |
| 970 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 971 | } // namespace |
| 972 | |
| 973 | |
| 974 | //----------------------------------------------------------------------------- |
| 975 | // tests |
| 976 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 977 | TEST(HttpCache, CreateThenDestroy) { |
| 978 | MockHttpCache cache; |
| 979 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 980 | scoped_ptr<net::HttpTransaction> trans; |
| 981 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 982 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 983 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 984 | } |
| 985 | |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 986 | TEST(HttpCache, GetBackend) { |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 987 | MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(0)); |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 988 | |
[email protected] | 6a98903 | 2010-06-14 19:05:33 | [diff] [blame] | 989 | disk_cache::Backend* backend; |
| 990 | TestCompletionCallback cb; |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 991 | // This will lazily initialize the backend. |
[email protected] | 6a98903 | 2010-06-14 19:05:33 | [diff] [blame] | 992 | int rv = cache.http_cache()->GetBackend(&backend, &cb); |
| 993 | EXPECT_EQ(net::OK, cb.GetResult(rv)); |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 994 | } |
| 995 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 996 | TEST(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 | |
| 1007 | TEST(HttpCache, SimpleGETNoDiskCache) { |
| 1008 | MockHttpCache cache; |
| 1009 | |
| 1010 | cache.disk_cache()->set_fail_requests(); |
| 1011 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1012 | net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 1013 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1014 | // Read from the network, and don't use the cache. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1015 | RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, |
| 1016 | log.bound()); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 1017 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1018 | // Check that the NetLog was filled as expected. |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 1019 | // (We attempted to both Open and Create entries, but both failed). |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1020 | EXPECT_EQ(6u, log.entries().size()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1021 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1022 | log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1023 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1024 | log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1025 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1026 | log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1027 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1028 | 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1033 | |
| 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] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1039 | TEST(HttpCache, SimpleGETNoDiskCache2) { |
| 1040 | // This will initialize a cache object with NULL backend. |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1041 | MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
| 1042 | factory->set_fail(true); |
| 1043 | factory->FinishCreation(); // We'll complete synchronously. |
| 1044 | MockHttpCache cache(factory); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1045 | |
| 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] | 6a98903 | 2010-06-14 19:05:33 | [diff] [blame] | 1050 | EXPECT_FALSE(cache.http_cache()->GetCurrentBackend()); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1051 | } |
| 1052 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 1053 | TEST(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] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 1073 | // Tests that disk failures after the transaction has started don't cause the |
| 1074 | // request to fail. |
| 1075 | TEST(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] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1084 | rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 1085 | 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 1093 | ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &en)); |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 1094 | 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1111 | TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) { |
| 1112 | MockHttpCache cache; |
| 1113 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1114 | net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 1115 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1116 | // write to the cache |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1117 | RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, |
| 1118 | log.bound()); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 1119 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1120 | // Check that the NetLog was filled as expected. |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1121 | EXPECT_EQ(8u, log.entries().size()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1122 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1123 | log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1124 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1125 | log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1126 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1127 | log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1128 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1129 | log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1130 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1131 | log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1132 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1133 | 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1138 | |
| 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] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1143 | log.Clear(); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 1144 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1145 | RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 1146 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1147 | // Check that the NetLog was filled as expected. |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1148 | EXPECT_EQ(8u, log.entries().size()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1149 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1150 | log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1151 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1152 | log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1153 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1154 | log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1155 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1156 | log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1157 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1158 | log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1159 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1160 | 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1165 | |
| 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 | |
| 1171 | TEST(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] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1181 | scoped_ptr<net::HttpTransaction> trans; |
| 1182 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 1183 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1184 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1185 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1186 | rv = trans->Start(&request, &callback, net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1187 | if (rv == net::ERR_IO_PENDING) |
| 1188 | rv = callback.WaitForResult(); |
| 1189 | ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
| 1190 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1191 | trans.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1192 | |
| 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 | |
| 1198 | TEST(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 | |
| 1215 | TEST(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 | |
| 1229 | TEST(HttpCache, SimpleGET_LoadBypassCache) { |
| 1230 | MockHttpCache cache; |
| 1231 | |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1232 | // Write to the cache. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1233 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1234 | |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1235 | // Force this transaction to write to the cache again. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1236 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1237 | transaction.load_flags |= net::LOAD_BYPASS_CACHE; |
| 1238 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1239 | net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1240 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1241 | RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1242 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1243 | // Check that the NetLog was filled as expected. |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1244 | EXPECT_EQ(8u, log.entries().size()); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1245 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1246 | log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1247 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1248 | log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1249 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1250 | log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1251 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1252 | log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1253 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1254 | log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1255 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1256 | 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1261 | |
| 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 | |
| 1267 | TEST(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 | |
| 1284 | TEST(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 | |
| 1301 | TEST(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 | |
| 1321 | TEST(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] | a3eee21 | 2009-11-05 18:08:58 | [diff] [blame] | 1341 | static 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 1346 | EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); |
[email protected] | a3eee21 | 2009-11-05 18:08:58 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | // Tests that we don't remove extra headers for simple requests. |
| 1350 | TEST(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. |
| 1370 | TEST(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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 1378 | transaction.request_headers = "If-None-Match: \"foopy\"\r\n" |
[email protected] | a3eee21 | 2009-11-05 18:08:58 | [diff] [blame] | 1379 | 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1390 | TEST(HttpCache, SimpleGET_ManyReaders) { |
| 1391 | MockHttpCache cache; |
| 1392 | |
| 1393 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1394 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1395 | std::vector<Context*> context_list; |
| 1396 | const int kNumTransactions = 5; |
| 1397 | |
| 1398 | for (int i = 0; i < kNumTransactions; ++i) { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1399 | context_list.push_back(new Context()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1400 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1401 | |
| 1402 | c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1403 | EXPECT_EQ(net::OK, c->result); |
| 1404 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1405 | c->result = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1406 | } |
| 1407 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1408 | // 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1412 | // 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] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1422 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1423 | } |
| 1424 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1425 | // We should not have had to re-open the disk entry |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1426 | |
| 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1433 | delete c; |
| 1434 | } |
| 1435 | } |
| 1436 | |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1437 | // 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. |
| 1441 | TEST(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] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1452 | context_list.push_back(new Context()); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1453 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1454 | |
| 1455 | c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1456 | EXPECT_EQ(net::OK, c->result); |
| 1457 | |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1458 | MockHttpRequest* this_request = &request; |
| 1459 | if (i == 1 || i == 2) |
| 1460 | this_request = &reader_request; |
| 1461 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1462 | c->result = c->trans->Start(this_request, &c->callback, net::BoundNetLog()); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1463 | } |
| 1464 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1465 | // Allow all requests to move from the Create queue to the active entry. |
| 1466 | MessageLoop::current()->RunAllPending(); |
| 1467 | |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1468 | // 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] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 1485 | if (c->result == net::OK) |
| 1486 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1487 | |
| 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] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 1500 | if (c->result == net::OK) |
| 1501 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1502 | } |
| 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] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1516 | // 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 |
| 1519 | TEST(HttpCache, SimpleGET_DoomWithPending) { |
| 1520 | // We need simultaneous doomed / not_doomed entries so let's use a real cache. |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1521 | MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(1024 * 1024)); |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1522 | |
| 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] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1541 | c->result = c->trans->Start(this_request, &c->callback, net::BoundNetLog()); |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1542 | } |
| 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] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1563 | // 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. |
| 1566 | TEST(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] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1578 | context_list.push_back(new Context()); |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1579 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1580 | |
| 1581 | c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1582 | EXPECT_EQ(net::OK, c->result); |
| 1583 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1584 | c->result = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1585 | } |
| 1586 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1587 | // Allow all requests to move from the Create queue to the active entry. |
| 1588 | MessageLoop::current()->RunAllPending(); |
| 1589 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1590 | // 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1615 | TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { |
| 1616 | MockHttpCache cache; |
| 1617 | |
| 1618 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1619 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1620 | std::vector<Context*> context_list; |
| 1621 | const int kNumTransactions = 2; |
| 1622 | |
| 1623 | for (int i = 0; i < kNumTransactions; ++i) { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1624 | context_list.push_back(new Context()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1625 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1626 | |
| 1627 | c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1628 | EXPECT_EQ(net::OK, c->result); |
| 1629 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1630 | c->result = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1631 | } |
| 1632 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1633 | // 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1637 | // 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] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1647 | // Destroy only the first transaction. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1648 | if (i == 0) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1649 | delete c; |
| 1650 | context_list[i] = NULL; |
| 1651 | } |
| 1652 | } |
| 1653 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1654 | // Complete the rest of the transactions. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1655 | for (int i = 1; i < kNumTransactions; ++i) { |
| 1656 | Context* c = context_list[i]; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1657 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1658 | } |
| 1659 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1660 | // We should have had to re-open the disk entry. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1661 | |
| 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1668 | delete c; |
| 1669 | } |
| 1670 | } |
| 1671 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1672 | // Tests that we can cancel requests that are queued waiting to open the disk |
| 1673 | // cache entry. |
| 1674 | TEST(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] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1689 | c->result = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1690 | } |
| 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] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame^] | 1728 | // Tests that we can cancel a single request to open a disk cache entry. |
| 1729 | TEST(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] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1751 | // Tests that we delete/create entries even if multiple requests are queued. |
| 1752 | TEST(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] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1768 | c->result = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1769 | } |
| 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1796 | TEST(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] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1805 | scoped_ptr<net::HttpTransaction> trans; |
| 1806 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 1807 | EXPECT_EQ(net::OK, rv); |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1808 | rv = trans->Start(&request, &callback, net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1809 | if (rv == net::ERR_IO_PENDING) |
| 1810 | rv = callback.WaitForResult(); |
| 1811 | ASSERT_EQ(net::OK, rv); |
| 1812 | |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 1813 | scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(256); |
| 1814 | rv = trans->Read(buf, 256, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1815 | 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] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1819 | trans.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1820 | |
| 1821 | // Make sure we pump any pending events, which should include a call to |
| 1822 | // HttpCache::Transaction::OnCacheReadCompleted. |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 1823 | MessageLoop::current()->RunAllPending(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1824 | } |
| 1825 | |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1826 | // Tests that we can delete the HttpCache and deal with queued transactions |
| 1827 | // ("waiting for the backend" as opposed to Active or Doomed entries). |
| 1828 | TEST(HttpCache, SimpleGET_ManyWriters_DeleteCache) { |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1829 | scoped_ptr<MockHttpCache> cache(new MockHttpCache( |
| 1830 | new MockBackendNoCbFactory())); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1831 | |
| 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] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1863 | // Tests that we queue requests when initializing the backend. |
| 1864 | TEST(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. |
| 1910 | TEST(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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1963 | TEST(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 | |
| 1982 | static 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 1987 | EXPECT_TRUE( |
| 1988 | request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1989 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 1990 | response_headers->assign(kETagGET_Transaction.response_headers); |
| 1991 | response_data->clear(); |
| 1992 | } |
| 1993 | |
| 1994 | TEST(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] | b7d05ab | 2008-12-09 19:18:41 | [diff] [blame] | 2017 | static 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2022 | EXPECT_TRUE( |
| 2023 | request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch)); |
[email protected] | b7d05ab | 2008-12-09 19:18:41 | [diff] [blame] | 2024 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 2025 | response_headers->assign("Cache-Control: no-store\n"); |
| 2026 | response_data->clear(); |
| 2027 | } |
| 2028 | |
| 2029 | TEST(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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2061 | TEST(HttpCache, SimplePOST_SkipsCache) { |
| 2062 | MockHttpCache cache; |
| 2063 | |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2064 | // Test that we skip the cache for POST requests that do not have an upload |
| 2065 | // identifier. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2066 | |
| 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] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2074 | // 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. |
| 2082 | static 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] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2087 | MockHttpCache cache; |
| 2088 | |
| 2089 | // The URL we will be requesting. |
| 2090 | const char* kUrl = "http://foobar.com/main.css"; |
| 2091 | |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2092 | // 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] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2113 | net_response_1.AssignTo(&mock_network_response); // Network mock. |
| 2114 | net_response_1.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2115 | |
| 2116 | std::string response_headers; |
| 2117 | RunTransactionTestWithResponse( |
| 2118 | cache.http_cache(), request, &response_headers); |
| 2119 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2120 | EXPECT_EQ(net_response_1.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2121 | 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] | 6f40bf7 | 2009-07-23 17:52:37 | [diff] [blame] | 2125 | // Request |kUrl| a second time. Now |kNetResponse1| it is in the HTTP |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2126 | // cache, so we don't hit the network. |
| 2127 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2128 | request.load_flags = net::LOAD_ONLY_FROM_CACHE; |
| 2129 | |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2130 | kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2131 | net_response_1.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2132 | |
| 2133 | RunTransactionTestWithResponse( |
| 2134 | cache.http_cache(), request, &response_headers); |
| 2135 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2136 | EXPECT_EQ(net_response_1.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2137 | 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] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2146 | request.request_headers = extra_request_headers; |
| 2147 | request.load_flags = net::LOAD_NORMAL; |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2148 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2149 | net_response_2.AssignTo(&mock_network_response); // Network mock. |
| 2150 | net_response_2.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2151 | |
| 2152 | RunTransactionTestWithResponse( |
| 2153 | cache.http_cache(), request, &response_headers); |
| 2154 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2155 | EXPECT_EQ(net_response_2.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2156 | 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] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2166 | request.load_flags = net::LOAD_ONLY_FROM_CACHE; |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2167 | |
| 2168 | kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2169 | cached_response_2.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2170 | |
| 2171 | RunTransactionTestWithResponse( |
| 2172 | cache.http_cache(), request, &response_headers); |
| 2173 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2174 | EXPECT_EQ(cached_response_2.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2175 | 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] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2182 | // Check that when an "if-modified-since" header is attached |
| 2183 | // to the request, the result still updates the cached entry. |
| 2184 | TEST(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. |
| 2210 | TEST(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. |
| 2238 | TEST(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). |
| 2275 | TEST(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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2288 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2289 | |
| 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). |
| 2319 | TEST(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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2332 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2333 | |
| 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). |
| 2364 | TEST(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. |
| 2394 | TEST(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] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2419 | // Test that doing an externally conditionalized request with both if-none-match |
| 2420 | // and if-modified-since updates the cache. |
| 2421 | TEST(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] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 2440 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n" |
| 2441 | "If-None-Match: \"Foo1\"\r\n"; |
[email protected] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2442 | |
| 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. |
| 2449 | TEST(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] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 2469 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\n" |
| 2470 | "If-None-Match: \"Foo2\"\n"; |
[email protected] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2471 | |
| 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. |
| 2478 | TEST(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] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 2498 | "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\n" |
| 2499 | "If-None-Match: \"Foo1\"\n"; |
[email protected] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2500 | |
| 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. |
| 2507 | TEST(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] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 2527 | "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] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2529 | |
| 2530 | ConditionalizedRequestUpdatesCacheHelper( |
| 2531 | kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2532 | } |
| 2533 | |
[email protected] | 6f40bf7 | 2009-07-23 17:52:37 | [diff] [blame] | 2534 | TEST(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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2558 | TEST(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] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 2571 | scoped_ptr<net::HttpTransaction> trans; |
| 2572 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 2573 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 2574 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2575 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2576 | rv = trans->Start(&request, &callback, net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2577 | if (rv == net::ERR_IO_PENDING) |
| 2578 | rv = callback.WaitForResult(); |
| 2579 | ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
| 2580 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 2581 | trans.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2582 | |
| 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] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2588 | TEST(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] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2603 | RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2604 | |
| 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] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2611 | RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2612 | |
| 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2618 | TEST(HttpCache, RangeGET_SkipsCache) { |
| 2619 | MockHttpCache cache; |
| 2620 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2621 | // 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2624 | |
| 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] | 72d1e59 | 2009-03-10 17:39:46 | [diff] [blame] | 2639 | transaction.request_headers = |
| 2640 | "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2641 | 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] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 2648 | // Test that we skip the cache for range requests that include a validation |
| 2649 | // header. |
| 2650 | TEST(HttpCache, RangeGET_SkipsCache2) { |
| 2651 | MockHttpCache cache; |
| 2652 | cache.http_cache()->set_enable_range_support(true); |
| 2653 | |
| 2654 | MockTransaction transaction(kRangeGET_Transaction); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2655 | transaction.request_headers = "If-None-Match: foo\r\n" |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2656 | EXTRA_HEADER |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2657 | "\r\nRange: bytes = 40-49"; |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 2658 | 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2665 | "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n" |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2666 | EXTRA_HEADER |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2667 | "\r\nRange: bytes = 40-49"; |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 2668 | 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2674 | transaction.request_headers = "If-Range: bla\r\n" |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2675 | EXTRA_HEADER |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2676 | "\r\nRange: bytes = 40-49\n"; |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 2677 | 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] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2684 | // Tests that receiving 206 for a regular request is handled correctly. |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 2685 | TEST(HttpCache, GET_Crazy206) { |
| 2686 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2687 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 2688 | |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 2689 | // Write to the cache. |
| 2690 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2691 | AddMockTransaction(&transaction); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2692 | transaction.request_headers = EXTRA_HEADER; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2693 | transaction.handler = NULL; |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 2694 | 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] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2704 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2705 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2706 | RemoveMockTransaction(&transaction); |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 2707 | } |
| 2708 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2709 | // Tests that we can cache range requests and fetch random blocks from the |
| 2710 | // cache and the network. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2711 | TEST(HttpCache, RangeGET_OK) { |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2712 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2713 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2714 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2715 | std::string headers; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2716 | |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2717 | // Write to the cache (40-49). |
| 2718 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 2719 | &headers); |
| 2720 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2721 | Verify206Response(headers, 40, 49); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2722 | 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] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2727 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 2728 | &headers); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2729 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2730 | Verify206Response(headers, 40, 49); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 2731 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2732 | 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] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2740 | transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2741 | transaction.data = "rg: 30-39 "; |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2742 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2743 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2744 | Verify206Response(headers, 30, 39); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 2745 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2746 | 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] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2753 | transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2754 | transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2755 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2756 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2757 | Verify206Response(headers, 20, 59); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 2758 | EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2759 | 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] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 2765 | // Tests that we can cache range requests and fetch random blocks from the |
| 2766 | // cache and the network, with synchronous responses. |
| 2767 | TEST(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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2779 | Verify206Response(headers, 40, 49); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 2780 | 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2787 | Verify206Response(headers, 40, 49); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 2788 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 2789 | 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2800 | Verify206Response(headers, 30, 39); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 2801 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 2802 | 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2813 | Verify206Response(headers, 20, 59); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 2814 | EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 2815 | 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] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 2821 | // Tests that we don't revalidate an entry unless we are required to do so. |
| 2822 | TEST(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. |
| 2864 | TEST(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] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2896 | // Tests that we deal with 304s for range requests. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2897 | TEST(HttpCache, RangeGET_304) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2898 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2899 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2900 | 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2907 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2908 | 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] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 2915 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 2916 | transaction.load_flags |= net::LOAD_VALIDATE_CACHE; |
| 2917 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2918 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2919 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2920 | 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] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 2927 | // Tests that we deal with 206s when revalidating range requests. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2928 | TEST(HttpCache, RangeGET_ModifiedResult) { |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 2929 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2930 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 2931 | 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2938 | Verify206Response(headers, 40, 49); |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 2939 | 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] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 2946 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 2947 | transaction.load_flags |= net::LOAD_VALIDATE_CACHE; |
| 2948 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 2949 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2950 | Verify206Response(headers, 40, 49); |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 2951 | 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] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2964 | // 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] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2966 | TEST(HttpCache, UnknownRangeGET_1) { |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2967 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2968 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2969 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2970 | std::string headers; |
| 2971 | |
| 2972 | // Write to the cache (70-79). |
| 2973 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2974 | transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2975 | transaction.data = "rg: 70-79 "; |
| 2976 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2977 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2978 | Verify206Response(headers, 70, 79); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2979 | 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] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2987 | transaction.request_headers = "Range: bytes = 60-\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2988 | transaction.data = "rg: 60-69 rg: 70-79 "; |
| 2989 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2990 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2991 | Verify206Response(headers, 60, 79); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2992 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2993 | 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] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2999 | // 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] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3002 | TEST(HttpCache, UnknownRangeGET_2) { |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3003 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3004 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3005 | std::string headers; |
| 3006 | |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3007 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3008 | transaction.test_mode = TEST_MODE_SYNC_CACHE_START | |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 3009 | TEST_MODE_SYNC_CACHE_READ | |
| 3010 | TEST_MODE_SYNC_CACHE_WRITE; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3011 | AddMockTransaction(&transaction); |
| 3012 | |
| 3013 | // Write to the cache (70-79). |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3014 | transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3015 | transaction.data = "rg: 70-79 "; |
| 3016 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3017 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3018 | Verify206Response(headers, 70, 79); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3019 | 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] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3027 | transaction.request_headers = "Range: bytes = -20\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3028 | transaction.data = "rg: 60-69 rg: 70-79 "; |
| 3029 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3030 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3031 | Verify206Response(headers, 60, 79); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3032 | 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] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3039 | // Tests that receiving Not Modified when asking for an open range doesn't mess |
| 3040 | // up things. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3041 | TEST(HttpCache, UnknownRangeGET_304) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3042 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3043 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3044 | 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] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3053 | transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3054 | 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] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3070 | TEST(HttpCache, GET_Previous206) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3071 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3072 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3073 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3074 | std::string headers; |
| 3075 | |
| 3076 | // Write to the cache (40-49). |
| 3077 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 3078 | &headers); |
| 3079 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3080 | Verify206Response(headers, 40, 49); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3081 | 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] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3087 | transaction.request_headers = EXTRA_HEADER; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3088 | 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] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3093 | 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] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 3100 | // 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). |
| 3102 | TEST(HttpCache, GET_Previous206_NotModified) { |
| 3103 | MockHttpCache cache; |
| 3104 | cache.http_cache()->set_enable_range_support(true); |
| 3105 | |
| 3106 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3107 | transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 3108 | 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3115 | Verify206Response(headers, 0, 9); |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 3116 | 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] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3122 | transaction2.load_flags |= net::LOAD_VALIDATE_CACHE; |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3123 | transaction2.request_headers = "Foo: bar\r\n" EXTRA_HEADER; |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 3124 | 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] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 3136 | // Tests that we can handle a regular request to a sparse entry, that results in |
| 3137 | // new content provided by the server (206). |
| 3138 | TEST(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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3150 | Verify206Response(headers, 0, 9); |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 3151 | 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] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3161 | transaction2.load_flags |= net::LOAD_VALIDATE_CACHE; |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 3162 | 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] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3179 | // Tests that we can handle cached 206 responses that are not sparse. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3180 | TEST(HttpCache, GET_Previous206_NotSparse) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3181 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3182 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3183 | |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3184 | // Create a disk cache entry that stores 206 headers while not being sparse. |
| 3185 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3186 | ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3187 | |
| 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3196 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3197 | |
| 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3201 | TestCompletionCallback cb; |
| 3202 | int rv = entry->WriteData(1, 0, buf, len, &cb, true); |
| 3203 | EXPECT_EQ(len, cb.GetResult(rv)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3204 | 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] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3221 | // 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] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3223 | TEST(HttpCache, RangeGET_Previous206_NotSparse_2) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3224 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3225 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3226 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3227 | |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3228 | // Create a disk cache entry that stores 206 headers while not being sparse. |
| 3229 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3230 | ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3231 | |
| 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3240 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3241 | |
| 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3245 | TestCompletionCallback cb; |
| 3246 | int rv = entry->WriteData(1, 0, buf, len, &cb, true); |
| 3247 | EXPECT_EQ(len, cb.GetResult(rv)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3248 | 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3256 | Verify206Response(headers, 40, 49); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3257 | 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] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3264 | // Tests that we can handle range requests with cached 200 responses. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3265 | TEST(HttpCache, RangeGET_Previous200) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3266 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3267 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3268 | |
| 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3291 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3292 | 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] | 8f28d63 | 2009-10-01 22:09:21 | [diff] [blame] | 3296 | // The last transaction has finished so make sure the entry is deactivated. |
| 3297 | MessageLoop::current()->RunAllPending(); |
| 3298 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3299 | // 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3303 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3304 | 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] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3315 | TEST(HttpCache, RangeRequestResultsIn200) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3316 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3317 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3318 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3319 | std::string headers; |
| 3320 | |
| 3321 | // Write to the cache (70-79). |
| 3322 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3323 | transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3324 | transaction.data = "rg: 70-79 "; |
| 3325 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3326 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3327 | Verify206Response(headers, 70, 79); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3328 | 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] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3354 | // 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] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3356 | TEST(HttpCache, RangeGET_MoreThanCurrentSize) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3357 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3358 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3359 | 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3366 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3367 | 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] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3373 | transaction.request_headers = "Range: bytes = 120-\r\n" EXTRA_HEADER; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3374 | 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] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 3388 | // Tests that we don't delete a sparse entry when we cancel a request. |
| 3389 | TEST(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] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3396 | Context* c = new Context(); |
| 3397 | int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 3398 | EXPECT_EQ(net::OK, rv); |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 3399 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3400 | rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 3401 | 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3420 | ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 3421 | entry->Close(); |
| 3422 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3423 | } |
| 3424 | |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 3425 | // Tests that we don't delete a sparse entry when we start a new request after |
| 3426 | // cancelling the previous one. |
| 3427 | TEST(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] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3434 | request.load_flags |= net::LOAD_VALIDATE_CACHE; |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 3435 | |
| 3436 | Context* c = new Context(); |
| 3437 | int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 3438 | EXPECT_EQ(net::OK, rv); |
| 3439 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3440 | rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 3441 | 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] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3452 | EXPECT_EQ(5, c->callback.GetResult(rv)); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 3453 | 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] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3465 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 3466 | 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] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 3471 | // 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. |
| 3473 | TEST(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] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3480 | request.load_flags |= net::LOAD_VALIDATE_CACHE; |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 3481 | |
| 3482 | Context* c = new Context(); |
| 3483 | int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 3484 | EXPECT_EQ(net::OK, rv); |
| 3485 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3486 | rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 3487 | 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] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3498 | EXPECT_EQ(5, c->callback.GetResult(rv)); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 3499 | 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] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3513 | rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 3514 | 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] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3532 | // Tests that an invalid range response results in no cached entry. |
| 3533 | TEST(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] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 3555 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3556 | EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3557 | |
| 3558 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3559 | } |
| 3560 | |
| 3561 | // Tests that we reject a range that doesn't match the content-length. |
| 3562 | TEST(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] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 3584 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3585 | EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3586 | |
| 3587 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3588 | } |
| 3589 | |
| 3590 | // Tests that if a server tells us conflicting information about a resource we |
| 3591 | // ignore the response. |
| 3592 | TEST(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] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3599 | transaction.request_headers = "Range: bytes = 50-59\r\n" EXTRA_HEADER; |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3600 | 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3606 | Verify206Response(headers, 50, 59); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3607 | 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3619 | Verify206Response(headers, 40, 49); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3620 | 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3626 | ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &en)); |
[email protected] | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 3627 | |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3628 | int64 cached_start = 0; |
[email protected] | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 3629 | TestCompletionCallback cb; |
| 3630 | int rv = en->GetAvailableRange(40, 20, &cached_start, &cb); |
| 3631 | EXPECT_EQ(10, cb.GetResult(rv)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3632 | EXPECT_EQ(50, cached_start); |
| 3633 | en->Close(); |
| 3634 | |
| 3635 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3636 | } |
| 3637 | |
| 3638 | // Tests that we handle large range values properly. |
| 3639 | TEST(HttpCache, RangeGET_LargeValues) { |
| 3640 | // We need a real sparse cache for this test. |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 3641 | MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(1024 * 1024)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3642 | cache.http_cache()->set_enable_range_support(true); |
| 3643 | std::string headers; |
| 3644 | |
| 3645 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3646 | transaction.handler = NULL; |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3647 | transaction.request_headers = "Range: bytes = 4294967288-4294967297\r\n" |
| 3648 | EXTRA_HEADER; |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3649 | 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3664 | ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &en)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3665 | en->Close(); |
| 3666 | |
| 3667 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3668 | } |
| 3669 | |
[email protected] | 93e7844 | 2009-10-27 04:46:32 | [diff] [blame] | 3670 | // Tests that we don't crash with a range request if the disk cache was not |
| 3671 | // initialized properly. |
| 3672 | TEST(HttpCache, RangeGET_NoDiskCache) { |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 3673 | MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
| 3674 | factory->set_fail(true); |
| 3675 | factory->FinishCreation(); // We'll complete synchronously. |
| 3676 | MockHttpCache cache(factory); |
| 3677 | |
[email protected] | 93e7844 | 2009-10-27 04:46:32 | [diff] [blame] | 3678 | 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] | 793618a | 2009-11-03 23:08:12 | [diff] [blame] | 3687 | // Tests that we handle byte range requests that skip the cache. |
| 3688 | TEST(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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3701 | Verify206Response(headers, 70, 79); |
[email protected] | 793618a | 2009-11-03 23:08:12 | [diff] [blame] | 3702 | 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] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 3709 | // 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. |
| 3711 | TEST(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] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3718 | transaction.load_flags |= net::LOAD_VALIDATE_CACHE; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 3719 | 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] | c14117b9 | 2010-01-21 19:22:57 | [diff] [blame] | 3736 | // Tests that when the server gives us less data than expected, we don't keep |
| 3737 | // asking for more data. |
| 3738 | TEST(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] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3777 | #ifdef NDEBUG |
| 3778 | // This test hits a NOTREACHED so it is a release mode only test. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3779 | TEST(HttpCache, RangeGET_OK_LoadOnlyFromCache) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3780 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3781 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3782 | 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] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3797 | scoped_ptr<net::HttpTransaction> trans; |
| 3798 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 3799 | EXPECT_EQ(net::OK, rv); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3800 | ASSERT_TRUE(trans.get()); |
| 3801 | |
[email protected] | 7deb2341 | 2010-04-28 20:29:29 | [diff] [blame] | 3802 | rv = trans->Start(&request, &callback, net::BoundNetLog()); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3803 | 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] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3817 | // Tests the handling of the "truncation" flag. |
| 3818 | TEST(HttpCache, WriteResponseInfo_Truncated) { |
| 3819 | MockHttpCache cache; |
| 3820 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3821 | ASSERT_TRUE(cache.CreateBackendEntry("http://www.google.com", &entry)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3822 | |
| 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3829 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3830 | bool truncated = false; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3831 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3832 | EXPECT_TRUE(truncated); |
| 3833 | |
| 3834 | // And now test the opposite case. |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3835 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3836 | truncated = true; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3837 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3838 | 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. |
| 3844 | TEST(HttpCache, DoomOnDestruction) { |
| 3845 | MockHttpCache cache; |
| 3846 | cache.http_cache()->set_enable_range_support(true); |
| 3847 | |
| 3848 | MockHttpRequest request(kSimpleGET_Transaction); |
| 3849 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3850 | Context* c = new Context(); |
| 3851 | int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 3852 | EXPECT_EQ(net::OK, rv); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3853 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3854 | rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3855 | 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] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3873 | // Tests that we delete an entry when the request is cancelled if the response |
| 3874 | // does not have content-length and strong validators. |
| 3875 | TEST(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] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3885 | rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3886 | 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. |
| 3912 | TEST(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] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3929 | rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3930 | 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] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3956 | // Tests that we mark an entry as incomplete when the request is cancelled. |
| 3957 | TEST(HttpCache, Set_Truncated_Flag) { |
| 3958 | MockHttpCache cache; |
| 3959 | cache.http_cache()->set_enable_range_support(true); |
| 3960 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3961 | 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] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3968 | |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 3969 | scoped_ptr<Context> c(new Context()); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3970 | int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 3971 | EXPECT_EQ(net::OK, rv); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3972 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3973 | rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3974 | 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] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 3988 | // 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] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3995 | // Destroy the transaction. |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 3996 | 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] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4004 | |
| 4005 | // Verify that the entry is marked as incomplete. |
| 4006 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4007 | ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4008 | net::HttpResponseInfo response; |
| 4009 | bool truncated = false; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4010 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4011 | EXPECT_TRUE(truncated); |
| 4012 | entry->Close(); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4013 | |
| 4014 | RemoveMockTransaction(&transaction); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | // Tests that we can continue with a request that was interrupted. |
| 4018 | TEST(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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4025 | ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4026 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4027 | std::string raw_headers("HTTP/1.1 200 OK\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4028 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4029 | "ETag: \"foo\"\n" |
| 4030 | "Accept-Ranges: bytes\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4031 | "Content-Length: 80\n"); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4032 | 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4038 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4039 | |
| 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4043 | TestCompletionCallback cb; |
| 4044 | int rv = entry->WriteData(1, 0, buf, len, &cb, true); |
| 4045 | EXPECT_EQ(len, cb.GetResult(rv)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4046 | |
| 4047 | // Now make a regular request. |
| 4048 | std::string headers; |
| 4049 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 4050 | transaction.request_headers = EXTRA_HEADER; |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4051 | 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] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4061 | "Content-Length: 80\n"); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4062 | |
| 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4073 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4074 | EXPECT_FALSE(truncated); |
| 4075 | entry->Close(); |
| 4076 | } |
| 4077 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4078 | // Tests that we delete truncated entries if the server changes its mind midway. |
| 4079 | TEST(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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4086 | ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 4087 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4088 | |
| 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4101 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true)); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4102 | |
| 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4106 | TestCompletionCallback cb; |
| 4107 | int rv = entry->WriteData(1, 0, buf, len, &cb, true); |
| 4108 | EXPECT_EQ(len, cb.GetResult(rv)); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4109 | 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4134 | ASSERT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4135 | } |
| 4136 | |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 4137 | // Tests that when we cancel a request that was interrupted, we mark it again |
| 4138 | // as truncated. |
| 4139 | TEST(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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4146 | ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 4147 | |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 4148 | std::string raw_headers("HTTP/1.1 200 OK\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4149 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 4150 | "ETag: \"foo\"\n" |
| 4151 | "Accept-Ranges: bytes\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4152 | "Content-Length: 80\n"); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 4153 | 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4160 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 4161 | |
| 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4165 | TestCompletionCallback cb; |
| 4166 | int rv = entry->WriteData(1, 0, buf, len, &cb, true); |
| 4167 | EXPECT_EQ(len, cb.GetResult(rv)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 4168 | |
| 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4177 | rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4178 | EXPECT_EQ(net::OK, c->callback.GetResult(rv)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 4179 | |
| 4180 | // Read 20 bytes from the cache, and 10 from the net. |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4181 | 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] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 4185 | |
| 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4199 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 4200 | EXPECT_TRUE(truncated); |
| 4201 | entry->Close(); |
| 4202 | } |
| 4203 | |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 4204 | // Tests that we can handle range requests when we have a truncated entry. |
| 4205 | TEST(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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4212 | ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 4213 | |
| 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4226 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true)); |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 4227 | |
| 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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4231 | TestCompletionCallback cb; |
| 4232 | int rv = entry->WriteData(1, 0, buf, len, &cb, true); |
| 4233 | EXPECT_EQ(len, cb.GetResult(rv)); |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 4234 | 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] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4241 | Verify206Response(headers, 40, 49); |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 4242 | 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4249 | TEST(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] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 4257 | TEST_MODE_SYNC_CACHE_READ | |
| 4258 | TEST_MODE_SYNC_CACHE_WRITE); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4259 | |
| 4260 | MockHttpRequest r1(transaction), |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 4261 | r2(transaction), |
| 4262 | r3(transaction); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4263 | |
| 4264 | TestTransactionConsumer c1(cache.http_cache()), |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 4265 | c2(cache.http_cache()), |
| 4266 | c3(cache.http_cache()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4267 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4268 | c1.Start(&r1, net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4269 | |
| 4270 | r2.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4271 | c2.Start(&r2, net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4272 | |
| 4273 | r3.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4274 | c3.Start(&r3, net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4275 | |
| 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 | |
| 4287 | TEST(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 | |
| 4305 | TEST(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] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4317 | scoped_ptr<net::HttpTransaction> trans; |
| 4318 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 4319 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 4320 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4321 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4322 | rv = trans->Start(&request, &callback, net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4323 | 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] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 4336 | // 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4338 | } |
| 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] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4345 | scoped_ptr<net::HttpTransaction> trans; |
| 4346 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 4347 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 4348 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4349 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4350 | rv = trans->Start(&request, &callback, net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4351 | 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] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 4364 | // 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4366 | } |
| 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 | |
| 4372 | TEST(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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4393 | EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | TEST(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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4421 | EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | TEST(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] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4450 | EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | // Ensure that we don't cache requests served over bad HTTPS. |
| 4454 | TEST(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] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4470 | scoped_ptr<net::HttpTransaction> trans; |
| 4471 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 4472 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 4473 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4474 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4475 | rv = trans->Start(&request, &callback, net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4476 | if (rv == net::ERR_IO_PENDING) |
| 4477 | rv = callback.WaitForResult(); |
| 4478 | ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4479 | } |
[email protected] | 3e2d38d | 2009-02-14 02:01:18 | [diff] [blame] | 4480 | |
| 4481 | // Ensure that we don't crash by if left-behind transactions. |
| 4482 | TEST(HttpCache, OutlivedTransactions) { |
| 4483 | MockHttpCache* cache = new MockHttpCache; |
| 4484 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4485 | scoped_ptr<net::HttpTransaction> trans; |
| 4486 | int rv = cache->http_cache()->CreateTransaction(&trans); |
| 4487 | EXPECT_EQ(net::OK, rv); |
| 4488 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 4489 | delete cache; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4490 | trans.reset(); |
[email protected] | 3e2d38d | 2009-02-14 02:01:18 | [diff] [blame] | 4491 | } |
[email protected] | 98179700 | 2009-06-05 07:14:15 | [diff] [blame] | 4492 | |
| 4493 | // Test that the disabled mode works. |
| 4494 | TEST(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] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 4512 | |
| 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] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 4515 | // HttpResponseHeaders::request_time and HttpResponseHeaders::response_time |
| 4516 | // fields also gets updated. |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 4517 | // http://crbug.com/20594. |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 4518 | TEST(HttpCache, UpdatesRequestResponseTimeOn304) { |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 4519 | 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] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 4561 | 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] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 4566 | |
| 4567 | net::HttpResponseInfo response; |
| 4568 | RunTransactionTestWithResponseInfo(cache.http_cache(), request, &response); |
| 4569 | |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 4570 | // 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] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 4574 | 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] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 4586 | |
| 4587 | // Tests that we can write metadata to an entry. |
| 4588 | TEST(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. |
| 4625 | TEST(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. |
| 4657 | TEST(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 | } |