[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); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 114 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 115 | if (fail_requests_) |
| 116 | return net::ERR_CACHE_READ_FAILURE; |
| 117 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 118 | if (offset < 0 || offset > static_cast<int>(data_[index].size())) |
| 119 | return net::ERR_FAILED; |
[email protected] | cad155b | 2008-09-23 14:44:27 | [diff] [blame] | 120 | if (static_cast<size_t>(offset) == data_[index].size()) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 121 | return 0; |
| 122 | |
| 123 | int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); |
[email protected] | 74a85ce | 2009-02-12 00:03:19 | [diff] [blame] | 124 | memcpy(buf->data(), &data_[index][offset], num); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 125 | |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 126 | if (!callback || |
| 127 | (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); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 137 | DCHECK(truncate); |
| 138 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 139 | if (fail_requests_) |
| 140 | return net::ERR_CACHE_READ_FAILURE; |
| 141 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 142 | if (offset < 0 || offset > static_cast<int>(data_[index].size())) |
| 143 | return net::ERR_FAILED; |
| 144 | |
| 145 | data_[index].resize(offset + buf_len); |
| 146 | if (buf_len) |
[email protected] | 74a85ce | 2009-02-12 00:03:19 | [diff] [blame] | 147 | memcpy(&data_[index][offset], buf->data(), buf_len); |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 148 | |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 149 | if (!callback || |
| 150 | (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, |
| 158 | net::CompletionCallback* completion_callback) { |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 159 | if (!sparse_ || busy_) |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 160 | return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 161 | if (offset < 0) |
| 162 | return net::ERR_FAILED; |
| 163 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 164 | if (fail_requests_) |
| 165 | return net::ERR_CACHE_READ_FAILURE; |
| 166 | |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 167 | DCHECK(offset < kint32max); |
| 168 | int real_offset = static_cast<int>(offset); |
| 169 | if (!buf_len) |
| 170 | return 0; |
| 171 | |
| 172 | int num = std::min(static_cast<int>(data_[1].size()) - real_offset, |
| 173 | buf_len); |
| 174 | memcpy(buf->data(), &data_[1][real_offset], num); |
| 175 | |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 176 | if (!completion_callback || |
| 177 | (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)) |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 178 | return num; |
| 179 | |
| 180 | CallbackLater(completion_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, |
| 187 | net::CompletionCallback* completion_callback) { |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 188 | if (busy_) |
| 189 | return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 190 | if (!sparse_) { |
| 191 | if (data_[1].size()) |
| 192 | return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 193 | sparse_ = true; |
| 194 | } |
| 195 | if (offset < 0) |
| 196 | return net::ERR_FAILED; |
| 197 | if (!buf_len) |
| 198 | return 0; |
| 199 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 200 | if (fail_requests_) |
| 201 | return net::ERR_CACHE_READ_FAILURE; |
| 202 | |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 203 | DCHECK(offset < kint32max); |
| 204 | int real_offset = static_cast<int>(offset); |
| 205 | |
| 206 | if (static_cast<int>(data_[1].size()) < real_offset + buf_len) |
| 207 | data_[1].resize(real_offset + buf_len); |
| 208 | |
| 209 | memcpy(&data_[1][real_offset], buf->data(), buf_len); |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 210 | if (!completion_callback || |
| 211 | (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE)) |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 212 | return buf_len; |
| 213 | |
| 214 | CallbackLater(completion_callback, buf_len); |
| 215 | return net::ERR_IO_PENDING; |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | virtual int GetAvailableRange(int64 offset, int len, int64* start) { |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 219 | if (!sparse_ || busy_) |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 220 | return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 221 | if (offset < 0) |
| 222 | return net::ERR_FAILED; |
| 223 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 224 | if (fail_requests_) |
| 225 | return net::ERR_CACHE_READ_FAILURE; |
| 226 | |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 227 | *start = offset; |
| 228 | DCHECK(offset < kint32max); |
| 229 | int real_offset = static_cast<int>(offset); |
| 230 | if (static_cast<int>(data_[1].size()) < real_offset) |
| 231 | return 0; |
| 232 | |
| 233 | int num = std::min(static_cast<int>(data_[1].size()) - real_offset, len); |
| 234 | int count = 0; |
| 235 | for (; num > 0; num--, real_offset++) { |
| 236 | if (!count) { |
| 237 | if (data_[1][real_offset]) { |
| 238 | count++; |
| 239 | *start = real_offset; |
| 240 | } |
| 241 | } else { |
| 242 | if (!data_[1][real_offset]) |
| 243 | break; |
| 244 | count++; |
| 245 | } |
| 246 | } |
| 247 | return count; |
| 248 | } |
| 249 | |
[email protected] | 3cf35d9e | 2009-11-05 23:27:41 | [diff] [blame] | 250 | virtual int GetAvailableRange(int64 offset, int len, int64* start, |
| 251 | net::CompletionCallback* callback) { |
| 252 | return net::ERR_NOT_IMPLEMENTED; |
| 253 | } |
| 254 | |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 255 | virtual void CancelSparseIO() { cancel_ = true; } |
| 256 | |
| 257 | virtual int ReadyForSparseIO(net::CompletionCallback* completion_callback) { |
| 258 | if (!cancel_) |
| 259 | return net::OK; |
| 260 | |
| 261 | cancel_ = false; |
| 262 | DCHECK(completion_callback); |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 263 | if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 264 | return net::OK; |
| 265 | |
| 266 | // The pending operation is already in the message loop (and hopefuly |
| 267 | // already in the second pass). Just notify the caller that it finished. |
| 268 | CallbackLater(completion_callback, 0); |
| 269 | return net::ERR_IO_PENDING; |
| 270 | } |
| 271 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 272 | // Fail most subsequent requests. |
| 273 | void set_fail_requests() { fail_requests_ = true; } |
| 274 | |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 275 | // If |value| is true, don't deliver any completion callbacks until called |
| 276 | // again with |value| set to false. Caution: remember to enable callbacks |
| 277 | // again or all subsequent tests will fail. |
| 278 | static void IgnoreCallbacks(bool value) { |
| 279 | if (ignore_callbacks_ == value) |
| 280 | return; |
| 281 | ignore_callbacks_ = value; |
| 282 | if (!value) |
| 283 | StoreAndDeliverCallbacks(false, NULL, NULL, 0); |
| 284 | } |
| 285 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 286 | private: |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 287 | friend class base::RefCounted<MockDiskEntry>; |
| 288 | |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 289 | struct CallbackInfo { |
| 290 | scoped_refptr<MockDiskEntry> entry; |
| 291 | net::CompletionCallback* callback; |
| 292 | int result; |
| 293 | }; |
| 294 | |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 295 | ~MockDiskEntry() {} |
| 296 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 297 | // Unlike the callbacks for MockHttpTransaction, we want this one to run even |
| 298 | // if the consumer called Close on the MockDiskEntry. We achieve that by |
| 299 | // leveraging the fact that this class is reference counted. |
| 300 | void CallbackLater(net::CompletionCallback* callback, int result) { |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 301 | if (ignore_callbacks_) |
| 302 | return StoreAndDeliverCallbacks(true, this, callback, result); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 303 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(this, |
| 304 | &MockDiskEntry::RunCallback, callback, result)); |
| 305 | } |
| 306 | void RunCallback(net::CompletionCallback* callback, int result) { |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 307 | if (busy_) { |
| 308 | // This is kind of hacky, but controlling the behavior of just this entry |
| 309 | // from a test is sort of complicated. What we really want to do is |
| 310 | // delay the delivery of a sparse IO operation a little more so that the |
| 311 | // request start operation (async) will finish without seeing the end of |
| 312 | // this operation (already posted to the message loop)... and without |
| 313 | // just delaying for n mS (which may cause trouble with slow bots). So |
| 314 | // we re-post this operation (all async sparse IO operations will take two |
| 315 | // trips trhough the message loop instead of one). |
| 316 | if (!delayed_) { |
| 317 | delayed_ = true; |
| 318 | return CallbackLater(callback, result); |
| 319 | } |
| 320 | } |
| 321 | busy_ = false; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 322 | callback->Run(result); |
| 323 | } |
| 324 | |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 325 | // When |store| is true, stores the callback to be delivered later; otherwise |
| 326 | // delivers any callback previously stored. |
| 327 | static void StoreAndDeliverCallbacks(bool store, MockDiskEntry* entry, |
| 328 | net::CompletionCallback* callback, |
| 329 | int result) { |
| 330 | static std::vector<CallbackInfo> callback_list; |
| 331 | if (store) { |
| 332 | CallbackInfo c = {entry, callback, result}; |
| 333 | callback_list.push_back(c); |
| 334 | } else { |
| 335 | for (size_t i = 0; i < callback_list.size(); i++) { |
| 336 | CallbackInfo& c = callback_list[i]; |
| 337 | c.entry->CallbackLater(c.callback, c.result); |
| 338 | } |
| 339 | callback_list.clear(); |
| 340 | } |
| 341 | } |
| 342 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 343 | std::string key_; |
[email protected] | 91f4caa | 2010-04-19 16:50:50 | [diff] [blame] | 344 | std::vector<char> data_[kNumCacheEntryDataIndices]; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 345 | int test_mode_; |
| 346 | bool doomed_; |
[email protected] | a2068a61 | 2009-06-04 21:43:49 | [diff] [blame] | 347 | bool sparse_; |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 348 | bool fail_requests_; |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 349 | bool busy_; |
| 350 | bool delayed_; |
| 351 | static bool cancel_; |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 352 | static bool ignore_callbacks_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 353 | }; |
| 354 | |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 355 | // Statics. |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 356 | bool MockDiskEntry::cancel_ = false; |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 357 | bool MockDiskEntry::ignore_callbacks_ = false; |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 358 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 359 | class MockDiskCache : public disk_cache::Backend { |
| 360 | public: |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 361 | MockDiskCache() |
| 362 | : open_count_(0), create_count_(0), fail_requests_(false), |
| 363 | soft_failures_(false) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | ~MockDiskCache() { |
| 367 | EntryMap::iterator it = entries_.begin(); |
| 368 | for (; it != entries_.end(); ++it) |
| 369 | it->second->Release(); |
| 370 | } |
| 371 | |
| 372 | virtual int32 GetEntryCount() const { |
| 373 | return static_cast<int32>(entries_.size()); |
| 374 | } |
| 375 | |
| 376 | virtual bool OpenEntry(const std::string& key, disk_cache::Entry** entry) { |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 377 | NOTREACHED(); |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, |
| 382 | net::CompletionCallback* callback) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 383 | if (fail_requests_) |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 384 | return net::ERR_CACHE_OPEN_FAILURE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 385 | |
| 386 | EntryMap::iterator it = entries_.find(key); |
| 387 | if (it == entries_.end()) |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 388 | return net::ERR_CACHE_OPEN_FAILURE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 389 | |
| 390 | if (it->second->is_doomed()) { |
| 391 | it->second->Release(); |
| 392 | entries_.erase(it); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 393 | return net::ERR_CACHE_OPEN_FAILURE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | open_count_++; |
| 397 | |
| 398 | it->second->AddRef(); |
| 399 | *entry = it->second; |
| 400 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 401 | if (soft_failures_) |
| 402 | it->second->set_fail_requests(); |
| 403 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 404 | if (!callback || (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)) |
| 405 | return net::OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 406 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 407 | CallbackLater(callback, net::OK); |
| 408 | return net::ERR_IO_PENDING; |
[email protected] | 3cf35d9e | 2009-11-05 23:27:41 | [diff] [blame] | 409 | } |
| 410 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 411 | virtual bool CreateEntry(const std::string& key, disk_cache::Entry** entry) { |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 412 | NOTREACHED(); |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, |
| 417 | net::CompletionCallback* callback) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 418 | if (fail_requests_) |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 419 | return net::ERR_CACHE_CREATE_FAILURE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 420 | |
| 421 | EntryMap::iterator it = entries_.find(key); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 422 | if (it != entries_.end()) { |
| 423 | DCHECK(it->second->is_doomed()); |
| 424 | it->second->Release(); |
| 425 | entries_.erase(it); |
| 426 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 427 | |
| 428 | create_count_++; |
| 429 | |
| 430 | MockDiskEntry* new_entry = new MockDiskEntry(key); |
| 431 | |
| 432 | new_entry->AddRef(); |
| 433 | entries_[key] = new_entry; |
| 434 | |
| 435 | new_entry->AddRef(); |
| 436 | *entry = new_entry; |
| 437 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 438 | if (soft_failures_) |
| 439 | new_entry->set_fail_requests(); |
| 440 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 441 | if (!callback || (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)) |
| 442 | return net::OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 443 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 444 | CallbackLater(callback, net::OK); |
| 445 | return net::ERR_IO_PENDING; |
[email protected] | 3cf35d9e | 2009-11-05 23:27:41 | [diff] [blame] | 446 | } |
| 447 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 448 | virtual bool DoomEntry(const std::string& key) { |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 449 | return false; |
| 450 | } |
| 451 | |
| 452 | virtual int DoomEntry(const std::string& key, |
| 453 | net::CompletionCallback* callback) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 454 | EntryMap::iterator it = entries_.find(key); |
| 455 | if (it != entries_.end()) { |
| 456 | it->second->Release(); |
| 457 | entries_.erase(it); |
| 458 | } |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 459 | |
| 460 | if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) |
| 461 | return net::OK; |
| 462 | |
| 463 | CallbackLater(callback, net::OK); |
| 464 | return net::ERR_IO_PENDING; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | virtual bool DoomAllEntries() { |
| 468 | return false; |
| 469 | } |
| 470 | |
[email protected] | 3cf35d9e | 2009-11-05 23:27:41 | [diff] [blame] | 471 | virtual int DoomAllEntries(net::CompletionCallback* callback) { |
| 472 | return net::ERR_NOT_IMPLEMENTED; |
| 473 | } |
| 474 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 475 | virtual bool DoomEntriesBetween(const Time initial_time, |
| 476 | const Time end_time) { |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 477 | return false; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 478 | } |
| 479 | |
[email protected] | 3cf35d9e | 2009-11-05 23:27:41 | [diff] [blame] | 480 | virtual int DoomEntriesBetween(const base::Time initial_time, |
| 481 | const base::Time end_time, |
| 482 | net::CompletionCallback* callback) { |
| 483 | return net::ERR_NOT_IMPLEMENTED; |
| 484 | } |
| 485 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 486 | virtual bool DoomEntriesSince(const Time initial_time) { |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 487 | return false; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 488 | } |
| 489 | |
[email protected] | 3cf35d9e | 2009-11-05 23:27:41 | [diff] [blame] | 490 | virtual int DoomEntriesSince(const base::Time initial_time, |
| 491 | net::CompletionCallback* callback) { |
| 492 | return net::ERR_NOT_IMPLEMENTED; |
| 493 | } |
| 494 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 495 | virtual bool OpenNextEntry(void** iter, disk_cache::Entry** next_entry) { |
| 496 | return false; |
| 497 | } |
| 498 | |
[email protected] | 3cf35d9e | 2009-11-05 23:27:41 | [diff] [blame] | 499 | virtual int OpenNextEntry(void** iter, disk_cache::Entry** next_entry, |
| 500 | net::CompletionCallback* callback) { |
| 501 | return net::ERR_NOT_IMPLEMENTED; |
| 502 | } |
| 503 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 504 | virtual void EndEnumeration(void** iter) {} |
| 505 | |
| 506 | virtual void GetStats( |
| 507 | std::vector<std::pair<std::string, std::string> >* stats) { |
| 508 | } |
| 509 | |
| 510 | // returns number of times a cache entry was successfully opened |
| 511 | int open_count() const { return open_count_; } |
| 512 | |
| 513 | // returns number of times a cache entry was successfully created |
| 514 | int create_count() const { return create_count_; } |
| 515 | |
| 516 | // Fail any subsequent CreateEntry and OpenEntry. |
| 517 | void set_fail_requests() { fail_requests_ = true; } |
| 518 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 519 | // Return entries that fail some of their requests. |
| 520 | void set_soft_failures(bool value) { soft_failures_ = value; } |
| 521 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 522 | private: |
[email protected] | 2314403 | 2008-09-08 20:51:30 | [diff] [blame] | 523 | typedef base::hash_map<std::string, MockDiskEntry*> EntryMap; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 524 | |
| 525 | class CallbackRunner : public Task { |
| 526 | public: |
| 527 | CallbackRunner(net::CompletionCallback* callback, int result) |
| 528 | : callback_(callback), result_(result) {} |
| 529 | virtual void Run() { |
| 530 | callback_->Run(result_); |
| 531 | } |
| 532 | |
| 533 | private: |
| 534 | net::CompletionCallback* callback_; |
| 535 | int result_; |
| 536 | DISALLOW_COPY_AND_ASSIGN(CallbackRunner); |
| 537 | }; |
| 538 | |
| 539 | void CallbackLater(net::CompletionCallback* callback, int result) { |
| 540 | MessageLoop::current()->PostTask(FROM_HERE, |
| 541 | new CallbackRunner(callback, result)); |
| 542 | } |
| 543 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 544 | EntryMap entries_; |
| 545 | int open_count_; |
| 546 | int create_count_; |
| 547 | bool fail_requests_; |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 548 | bool soft_failures_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 549 | }; |
| 550 | |
| 551 | class MockHttpCache { |
| 552 | public: |
| 553 | MockHttpCache() : http_cache_(new MockNetworkLayer(), new MockDiskCache()) { |
| 554 | } |
| 555 | |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 556 | explicit MockHttpCache(disk_cache::Backend* disk_cache) |
| 557 | : http_cache_(new MockNetworkLayer(), disk_cache) { |
| 558 | } |
| 559 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 560 | net::HttpCache* http_cache() { return &http_cache_; } |
| 561 | |
| 562 | MockNetworkLayer* network_layer() { |
| 563 | return static_cast<MockNetworkLayer*>(http_cache_.network_layer()); |
| 564 | } |
| 565 | MockDiskCache* disk_cache() { |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 566 | return static_cast<MockDiskCache*>(http_cache_.GetBackend()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | private: |
| 570 | net::HttpCache http_cache_; |
| 571 | }; |
| 572 | |
| 573 | |
| 574 | //----------------------------------------------------------------------------- |
| 575 | // helpers |
| 576 | |
| 577 | void ReadAndVerifyTransaction(net::HttpTransaction* trans, |
| 578 | const MockTransaction& trans_info) { |
| 579 | std::string content; |
| 580 | int rv = ReadTransaction(trans, &content); |
| 581 | |
| 582 | EXPECT_EQ(net::OK, rv); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 583 | std::string expected(trans_info.data); |
| 584 | EXPECT_EQ(expected, content); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 585 | } |
| 586 | |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 587 | void RunTransactionTestWithRequestAndLog(net::HttpCache* cache, |
| 588 | const MockTransaction& trans_info, |
| 589 | const MockHttpRequest& request, |
| 590 | net::HttpResponseInfo* response_info, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 591 | const net::BoundNetLog& net_log) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 592 | TestCompletionCallback callback; |
| 593 | |
| 594 | // write to the cache |
| 595 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 596 | scoped_ptr<net::HttpTransaction> trans; |
| 597 | int rv = cache->CreateTransaction(&trans); |
| 598 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 599 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 600 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 601 | rv = trans->Start(&request, &callback, net_log); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 602 | if (rv == net::ERR_IO_PENDING) |
| 603 | rv = callback.WaitForResult(); |
| 604 | ASSERT_EQ(net::OK, rv); |
| 605 | |
| 606 | const net::HttpResponseInfo* response = trans->GetResponseInfo(); |
| 607 | ASSERT_TRUE(response); |
| 608 | |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 609 | if (response_info) |
| 610 | *response_info = *response; |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 611 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 612 | ReadAndVerifyTransaction(trans.get(), trans_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 613 | } |
| 614 | |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 615 | void RunTransactionTestWithRequest(net::HttpCache* cache, |
| 616 | const MockTransaction& trans_info, |
| 617 | const MockHttpRequest& request, |
| 618 | net::HttpResponseInfo* response_info) { |
| 619 | RunTransactionTestWithRequestAndLog(cache, trans_info, request, |
| 620 | response_info, NULL); |
| 621 | } |
| 622 | |
| 623 | void RunTransactionTestWithLog(net::HttpCache* cache, |
| 624 | const MockTransaction& trans_info, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 625 | const net::BoundNetLog& log) { |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 626 | RunTransactionTestWithRequestAndLog( |
| 627 | cache, trans_info, MockHttpRequest(trans_info), NULL, log); |
| 628 | } |
| 629 | |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 630 | void RunTransactionTest(net::HttpCache* cache, |
| 631 | const MockTransaction& trans_info) { |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 632 | RunTransactionTestWithLog(cache, trans_info, NULL); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 633 | } |
| 634 | |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 635 | void RunTransactionTestWithResponseInfo(net::HttpCache* cache, |
| 636 | const MockTransaction& trans_info, |
| 637 | net::HttpResponseInfo* response) { |
| 638 | RunTransactionTestWithRequest( |
| 639 | cache, trans_info, MockHttpRequest(trans_info), response); |
| 640 | } |
| 641 | |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 642 | void RunTransactionTestWithResponse(net::HttpCache* cache, |
| 643 | const MockTransaction& trans_info, |
| 644 | std::string* response_headers) { |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 645 | net::HttpResponseInfo response; |
| 646 | RunTransactionTestWithResponseInfo(cache, trans_info, &response); |
| 647 | response.headers->GetNormalizedHeaders(response_headers); |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 648 | } |
| 649 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 650 | // This class provides a handler for kFastNoStoreGET_Transaction so that the |
| 651 | // no-store header can be included on demand. |
| 652 | class FastTransactionServer { |
| 653 | public: |
| 654 | FastTransactionServer() { |
| 655 | no_store = false; |
| 656 | } |
| 657 | ~FastTransactionServer() {} |
| 658 | |
| 659 | void set_no_store(bool value) { no_store = value; } |
| 660 | |
| 661 | static void FastNoStoreHandler(const net::HttpRequestInfo* request, |
| 662 | std::string* response_status, |
| 663 | std::string* response_headers, |
| 664 | std::string* response_data) { |
| 665 | if (no_store) |
| 666 | *response_headers = "Cache-Control: no-store\n"; |
| 667 | } |
| 668 | |
| 669 | private: |
| 670 | static bool no_store; |
| 671 | DISALLOW_COPY_AND_ASSIGN(FastTransactionServer); |
| 672 | }; |
| 673 | bool FastTransactionServer::no_store; |
| 674 | |
| 675 | const MockTransaction kFastNoStoreGET_Transaction = { |
| 676 | "http://www.google.com/nostore", |
| 677 | "GET", |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 678 | base::Time(), |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 679 | "", |
| 680 | net::LOAD_VALIDATE_CACHE, |
| 681 | "HTTP/1.1 200 OK", |
| 682 | "Cache-Control: max-age=10000\n", |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 683 | base::Time(), |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 684 | "<html><body>Google Blah Blah</body></html>", |
| 685 | TEST_MODE_SYNC_NET_START, |
| 686 | &FastTransactionServer::FastNoStoreHandler, |
| 687 | 0 |
| 688 | }; |
| 689 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 690 | // This class provides a handler for kRangeGET_TransactionOK so that the range |
| 691 | // request can be served on demand. |
| 692 | class RangeTransactionServer { |
| 693 | public: |
| 694 | RangeTransactionServer() { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 695 | not_modified_ = false; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 696 | modified_ = false; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 697 | bad_200_ = false; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 698 | } |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 699 | ~RangeTransactionServer() { |
| 700 | not_modified_ = false; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 701 | modified_ = false; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 702 | bad_200_ = false; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 703 | } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 704 | |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 705 | // Returns only 416 or 304 when set. |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 706 | void set_not_modified(bool value) { not_modified_ = value; } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 707 | |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 708 | // Returns 206 when revalidating a range (instead of 304). |
| 709 | void set_modified(bool value) { modified_ = value; } |
| 710 | |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 711 | // Returns 200 instead of 206 (a malformed response overall). |
| 712 | void set_bad_200(bool value) { bad_200_ = value; } |
| 713 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 714 | static void RangeHandler(const net::HttpRequestInfo* request, |
| 715 | std::string* response_status, |
| 716 | std::string* response_headers, |
| 717 | std::string* response_data); |
| 718 | |
| 719 | private: |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 720 | static bool not_modified_; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 721 | static bool modified_; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 722 | static bool bad_200_; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 723 | DISALLOW_COPY_AND_ASSIGN(RangeTransactionServer); |
| 724 | }; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 725 | bool RangeTransactionServer::not_modified_ = false; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 726 | bool RangeTransactionServer::modified_ = false; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 727 | bool RangeTransactionServer::bad_200_ = false; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 728 | |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 729 | // A dummy extra header that must be preserved on a given request. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 730 | #define EXTRA_HEADER "Extra: header" |
| 731 | static const char kExtraHeaderKey[] = "Extra"; |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 732 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 733 | // Static. |
| 734 | void RangeTransactionServer::RangeHandler(const net::HttpRequestInfo* request, |
| 735 | std::string* response_status, |
| 736 | std::string* response_headers, |
| 737 | std::string* response_data) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 738 | if (request->extra_headers.IsEmpty()) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 739 | response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 740 | return; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 741 | } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 742 | |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 743 | // We want to make sure we don't delete extra headers. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 744 | EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 745 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 746 | if (not_modified_) { |
| 747 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 748 | return; |
| 749 | } |
| 750 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 751 | std::vector<net::HttpByteRange> ranges; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 752 | std::string range_header; |
| 753 | if (!request->extra_headers.GetHeader( |
| 754 | net::HttpRequestHeaders::kRange, &range_header) || |
| 755 | !net::HttpUtil::ParseRangeHeader(range_header, &ranges) || |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 756 | ranges.size() != 1) |
| 757 | return; |
| 758 | // We can handle this range request. |
| 759 | net::HttpByteRange byte_range = ranges[0]; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 760 | if (byte_range.first_byte_position() > 79) { |
| 761 | response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); |
| 762 | return; |
| 763 | } |
| 764 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 765 | EXPECT_TRUE(byte_range.ComputeBounds(80)); |
| 766 | int start = static_cast<int>(byte_range.first_byte_position()); |
| 767 | int end = static_cast<int>(byte_range.last_byte_position()); |
| 768 | |
| 769 | EXPECT_LT(end, 80); |
| 770 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 771 | std::string content_range = StringPrintf("Content-Range: bytes %d-%d/80\n", |
| 772 | start, end); |
| 773 | response_headers->append(content_range); |
| 774 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 775 | if (!request->extra_headers.HasHeader("If-None-Match") || modified_) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 776 | EXPECT_EQ(9, (end - start) % 10); |
| 777 | std::string data; |
| 778 | for (int block_start = start; block_start < end; block_start += 10) |
| 779 | StringAppendF(&data, "rg: %02d-%02d ", block_start, block_start + 9); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 780 | *response_data = data; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 781 | |
| 782 | if (end - start != 9) { |
| 783 | // We also have to fix content-length. |
| 784 | int len = end - start + 1; |
| 785 | EXPECT_EQ(0, len % 10); |
| 786 | std::string content_length = StringPrintf("Content-Length: %d\n", len); |
| 787 | response_headers->replace(response_headers->find("Content-Length:"), |
| 788 | content_length.size(), content_length); |
| 789 | } |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 790 | if (bad_200_) { |
| 791 | // We return a range, but with a response code of 200. |
| 792 | response_status->assign("HTTP/1.1 200 Success"); |
| 793 | } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 794 | } else { |
| 795 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 796 | response_data->clear(); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | const MockTransaction kRangeGET_TransactionOK = { |
| 801 | "http://www.google.com/range", |
| 802 | "GET", |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 803 | base::Time(), |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 804 | "Range: bytes = 40-49\r\n" |
| 805 | EXTRA_HEADER, |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 806 | net::LOAD_NORMAL, |
| 807 | "HTTP/1.1 206 Partial Content", |
| 808 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 809 | "ETag: \"foo\"\n" |
| 810 | "Accept-Ranges: bytes\n" |
| 811 | "Content-Length: 10\n", |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 812 | base::Time(), |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 813 | "rg: 40-49 ", |
| 814 | TEST_MODE_NORMAL, |
| 815 | &RangeTransactionServer::RangeHandler, |
| 816 | 0 |
| 817 | }; |
| 818 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 819 | // Verifies the response headers (|response|) match a partial content |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 820 | // response for the range starting at |start| and ending at |end|. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 821 | void Verify206Response(std::string response, int start, int end) { |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 822 | std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), |
| 823 | response.size())); |
| 824 | scoped_refptr<net::HttpResponseHeaders> headers = |
| 825 | new net::HttpResponseHeaders(raw_headers); |
| 826 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 827 | ASSERT_EQ(206, headers->response_code()); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 828 | |
| 829 | int64 range_start, range_end, object_size; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 830 | ASSERT_TRUE( |
| 831 | headers->GetContentRange(&range_start, &range_end, &object_size)); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 832 | int64 content_length = headers->GetContentLength(); |
| 833 | |
| 834 | int length = end - start + 1; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 835 | ASSERT_EQ(length, content_length); |
| 836 | ASSERT_EQ(start, range_start); |
| 837 | ASSERT_EQ(end, range_end); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 838 | } |
| 839 | |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 840 | // Helper to represent a network HTTP response. |
| 841 | struct Response { |
| 842 | // Set this response into |trans|. |
| 843 | void AssignTo(MockTransaction* trans) const { |
| 844 | trans->status = status; |
| 845 | trans->response_headers = headers; |
| 846 | trans->data = body; |
| 847 | } |
| 848 | |
| 849 | std::string status_and_headers() const { |
| 850 | return std::string(status) + "\n" + std::string(headers); |
| 851 | } |
| 852 | |
| 853 | const char* status; |
| 854 | const char* headers; |
| 855 | const char* body; |
| 856 | }; |
| 857 | |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 858 | struct Context { |
| 859 | Context() : result(net::ERR_IO_PENDING) {} |
| 860 | |
| 861 | int result; |
| 862 | TestCompletionCallback callback; |
| 863 | scoped_ptr<net::HttpTransaction> trans; |
| 864 | }; |
| 865 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 866 | } // namespace |
| 867 | |
| 868 | |
| 869 | //----------------------------------------------------------------------------- |
| 870 | // tests |
| 871 | |
| 872 | |
| 873 | TEST(HttpCache, CreateThenDestroy) { |
| 874 | MockHttpCache cache; |
| 875 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 876 | scoped_ptr<net::HttpTransaction> trans; |
| 877 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 878 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 879 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 880 | } |
| 881 | |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 882 | TEST(HttpCache, GetBackend) { |
| 883 | // This will initialize a cache object with NULL backend. |
| 884 | MockHttpCache cache(NULL); |
| 885 | |
| 886 | // This will lazily initialize the backend. |
| 887 | cache.http_cache()->set_type(net::MEMORY_CACHE); |
| 888 | EXPECT_TRUE(cache.http_cache()->GetBackend()); |
| 889 | } |
| 890 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 891 | TEST(HttpCache, SimpleGET) { |
| 892 | MockHttpCache cache; |
| 893 | |
| 894 | // write to the cache |
| 895 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 896 | |
| 897 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 898 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 899 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 900 | } |
| 901 | |
| 902 | TEST(HttpCache, SimpleGETNoDiskCache) { |
| 903 | MockHttpCache cache; |
| 904 | |
| 905 | cache.disk_cache()->set_fail_requests(); |
| 906 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 907 | net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 908 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 909 | // Read from the network, and don't use the cache. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 910 | RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, |
| 911 | log.bound()); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 912 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 913 | // Check that the NetLog was filled as expected. |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 914 | // (We attempted to both Open and Create entries, but both failed). |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 915 | EXPECT_EQ(4u, log.entries().size()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 916 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 917 | log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 918 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 919 | log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 920 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 921 | log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 922 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 923 | log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 924 | |
| 925 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 926 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 927 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 928 | } |
| 929 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 930 | TEST(HttpCache, SimpleGETWithDiskFailures) { |
| 931 | MockHttpCache cache; |
| 932 | |
| 933 | cache.disk_cache()->set_soft_failures(true); |
| 934 | |
| 935 | // Read from the network, and fail to write to the cache. |
| 936 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 937 | |
| 938 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 939 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 940 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 941 | |
| 942 | // This one should see an empty cache again. |
| 943 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 944 | |
| 945 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 946 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 947 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 948 | } |
| 949 | |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 950 | // Tests that disk failures after the transaction has started don't cause the |
| 951 | // request to fail. |
| 952 | TEST(HttpCache, SimpleGETWithDiskFailures2) { |
| 953 | MockHttpCache cache; |
| 954 | |
| 955 | MockHttpRequest request(kSimpleGET_Transaction); |
| 956 | |
| 957 | scoped_ptr<Context> c(new Context()); |
| 958 | int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 959 | EXPECT_EQ(net::OK, rv); |
| 960 | |
| 961 | rv = c->trans->Start(&request, &c->callback, NULL); |
| 962 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 963 | rv = c->callback.WaitForResult(); |
| 964 | |
| 965 | // Start failing request now. |
| 966 | cache.disk_cache()->set_soft_failures(true); |
| 967 | |
| 968 | // We have to open the entry again to propagate the failure flag. |
| 969 | disk_cache::Entry* en; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 970 | ASSERT_EQ(net::OK, cache.disk_cache()->OpenEntry(kSimpleGET_Transaction.url, |
| 971 | &en, NULL)); |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 972 | en->Close(); |
| 973 | |
| 974 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 975 | c.reset(); |
| 976 | |
| 977 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 978 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 979 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 980 | |
| 981 | // This one should see an empty cache again. |
| 982 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 983 | |
| 984 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 985 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 986 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 987 | } |
| 988 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 989 | TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) { |
| 990 | MockHttpCache cache; |
| 991 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 992 | net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 993 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 994 | // write to the cache |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 995 | RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, |
| 996 | log.bound()); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 997 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 998 | // Check that the NetLog was filled as expected. |
| 999 | EXPECT_EQ(6u, log.entries().size()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1000 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1001 | log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1002 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1003 | log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1004 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1005 | log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1006 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1007 | log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1008 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1009 | log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1010 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1011 | log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1012 | |
| 1013 | // force this transaction to read from the cache |
| 1014 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1015 | transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 1016 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1017 | log.Clear(); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 1018 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1019 | RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 1020 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1021 | // Check that the NetLog was filled as expected. |
| 1022 | EXPECT_EQ(6u, log.entries().size()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1023 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1024 | log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1025 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1026 | log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1027 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1028 | log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1029 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1030 | log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1031 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1032 | log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_READ_INFO)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1033 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1034 | log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_READ_INFO)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1035 | |
| 1036 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1037 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1038 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1039 | } |
| 1040 | |
| 1041 | TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { |
| 1042 | MockHttpCache cache; |
| 1043 | |
| 1044 | // force this transaction to read from the cache |
| 1045 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1046 | transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 1047 | |
| 1048 | MockHttpRequest request(transaction); |
| 1049 | TestCompletionCallback callback; |
| 1050 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1051 | scoped_ptr<net::HttpTransaction> trans; |
| 1052 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 1053 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1054 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1055 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1056 | rv = trans->Start(&request, &callback, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1057 | if (rv == net::ERR_IO_PENDING) |
| 1058 | rv = callback.WaitForResult(); |
| 1059 | ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
| 1060 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1061 | trans.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1062 | |
| 1063 | EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 1064 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1065 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 1066 | } |
| 1067 | |
| 1068 | TEST(HttpCache, SimpleGET_LoadPreferringCache_Hit) { |
| 1069 | MockHttpCache cache; |
| 1070 | |
| 1071 | // write to the cache |
| 1072 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1073 | |
| 1074 | // force this transaction to read from the cache if valid |
| 1075 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1076 | transaction.load_flags |= net::LOAD_PREFERRING_CACHE; |
| 1077 | |
| 1078 | RunTransactionTest(cache.http_cache(), transaction); |
| 1079 | |
| 1080 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1081 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1082 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1083 | } |
| 1084 | |
| 1085 | TEST(HttpCache, SimpleGET_LoadPreferringCache_Miss) { |
| 1086 | MockHttpCache cache; |
| 1087 | |
| 1088 | // force this transaction to read from the cache if valid |
| 1089 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1090 | transaction.load_flags |= net::LOAD_PREFERRING_CACHE; |
| 1091 | |
| 1092 | RunTransactionTest(cache.http_cache(), transaction); |
| 1093 | |
| 1094 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1095 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1096 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1097 | } |
| 1098 | |
| 1099 | TEST(HttpCache, SimpleGET_LoadBypassCache) { |
| 1100 | MockHttpCache cache; |
| 1101 | |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1102 | // Write to the cache. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1103 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1104 | |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1105 | // Force this transaction to write to the cache again. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1106 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1107 | transaction.load_flags |= net::LOAD_BYPASS_CACHE; |
| 1108 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1109 | net::CapturingBoundNetLog log(net::CapturingNetLog::kUnbounded); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1110 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1111 | RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1112 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1113 | // Check that the NetLog was filled as expected. |
| 1114 | EXPECT_EQ(6u, log.entries().size()); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1115 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1116 | log.entries(), 0, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1117 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1118 | log.entries(), 1, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1119 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1120 | log.entries(), 2, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1121 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1122 | log.entries(), 3, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1123 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1124 | log.entries(), 4, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1125 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1126 | log.entries(), 5, net::NetLog::TYPE_HTTP_CACHE_WAITING)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1127 | |
| 1128 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1129 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1130 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1131 | } |
| 1132 | |
| 1133 | TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit) { |
| 1134 | MockHttpCache cache; |
| 1135 | |
| 1136 | // write to the cache |
| 1137 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1138 | |
| 1139 | // force this transaction to write to the cache again |
| 1140 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1141 | transaction.request_headers = "pragma: no-cache"; |
| 1142 | |
| 1143 | RunTransactionTest(cache.http_cache(), transaction); |
| 1144 | |
| 1145 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1146 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1147 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1148 | } |
| 1149 | |
| 1150 | TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit2) { |
| 1151 | MockHttpCache cache; |
| 1152 | |
| 1153 | // write to the cache |
| 1154 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1155 | |
| 1156 | // force this transaction to write to the cache again |
| 1157 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1158 | transaction.request_headers = "cache-control: no-cache"; |
| 1159 | |
| 1160 | RunTransactionTest(cache.http_cache(), transaction); |
| 1161 | |
| 1162 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1163 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1164 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1165 | } |
| 1166 | |
| 1167 | TEST(HttpCache, SimpleGET_LoadValidateCache) { |
| 1168 | MockHttpCache cache; |
| 1169 | |
| 1170 | // write to the cache |
| 1171 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1172 | |
| 1173 | // read from the cache |
| 1174 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1175 | |
| 1176 | // force this transaction to validate the cache |
| 1177 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1178 | transaction.load_flags |= net::LOAD_VALIDATE_CACHE; |
| 1179 | |
| 1180 | RunTransactionTest(cache.http_cache(), transaction); |
| 1181 | |
| 1182 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1183 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1184 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1185 | } |
| 1186 | |
| 1187 | TEST(HttpCache, SimpleGET_LoadValidateCache_Implicit) { |
| 1188 | MockHttpCache cache; |
| 1189 | |
| 1190 | // write to the cache |
| 1191 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1192 | |
| 1193 | // read from the cache |
| 1194 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1195 | |
| 1196 | // force this transaction to validate the cache |
| 1197 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1198 | transaction.request_headers = "cache-control: max-age=0"; |
| 1199 | |
| 1200 | RunTransactionTest(cache.http_cache(), transaction); |
| 1201 | |
| 1202 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1203 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1204 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1205 | } |
| 1206 | |
[email protected] | a3eee21 | 2009-11-05 18:08:58 | [diff] [blame] | 1207 | static void PreserveRequestHeaders_Handler( |
| 1208 | const net::HttpRequestInfo* request, |
| 1209 | std::string* response_status, |
| 1210 | std::string* response_headers, |
| 1211 | std::string* response_data) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 1212 | EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); |
[email protected] | a3eee21 | 2009-11-05 18:08:58 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | // Tests that we don't remove extra headers for simple requests. |
| 1216 | TEST(HttpCache, SimpleGET_PreserveRequestHeaders) { |
| 1217 | MockHttpCache cache; |
| 1218 | |
| 1219 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1220 | transaction.handler = PreserveRequestHeaders_Handler; |
| 1221 | transaction.request_headers = EXTRA_HEADER; |
| 1222 | transaction.response_headers = "Cache-Control: max-age=0\n"; |
| 1223 | AddMockTransaction(&transaction); |
| 1224 | |
| 1225 | // Write, then revalidate the entry. |
| 1226 | RunTransactionTest(cache.http_cache(), transaction); |
| 1227 | RunTransactionTest(cache.http_cache(), transaction); |
| 1228 | |
| 1229 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1230 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1231 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1232 | RemoveMockTransaction(&transaction); |
| 1233 | } |
| 1234 | |
| 1235 | // Tests that we don't remove extra headers for conditionalized requests. |
| 1236 | TEST(HttpCache, ConditionalizedGET_PreserveRequestHeaders) { |
| 1237 | MockHttpCache cache; |
| 1238 | |
| 1239 | // Write to the cache. |
| 1240 | RunTransactionTest(cache.http_cache(), kETagGET_Transaction); |
| 1241 | |
| 1242 | MockTransaction transaction(kETagGET_Transaction); |
| 1243 | transaction.handler = PreserveRequestHeaders_Handler; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 1244 | transaction.request_headers = "If-None-Match: \"foopy\"\r\n" |
[email protected] | a3eee21 | 2009-11-05 18:08:58 | [diff] [blame] | 1245 | EXTRA_HEADER; |
| 1246 | AddMockTransaction(&transaction); |
| 1247 | |
| 1248 | RunTransactionTest(cache.http_cache(), transaction); |
| 1249 | |
| 1250 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1251 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1252 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1253 | RemoveMockTransaction(&transaction); |
| 1254 | } |
| 1255 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1256 | TEST(HttpCache, SimpleGET_ManyReaders) { |
| 1257 | MockHttpCache cache; |
| 1258 | |
| 1259 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1260 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1261 | std::vector<Context*> context_list; |
| 1262 | const int kNumTransactions = 5; |
| 1263 | |
| 1264 | for (int i = 0; i < kNumTransactions; ++i) { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1265 | context_list.push_back(new Context()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1266 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1267 | |
| 1268 | c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1269 | EXPECT_EQ(net::OK, c->result); |
| 1270 | |
| 1271 | c->result = c->trans->Start(&request, &c->callback, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1272 | } |
| 1273 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1274 | // Allow all requests to move from the Create queue to the active entry. |
| 1275 | MessageLoop::current()->RunAllPending(); |
| 1276 | |
| 1277 | // The first request should be a writer at this point, and the subsequent |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1278 | // requests should be pending. |
| 1279 | |
| 1280 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1281 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1282 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1283 | |
| 1284 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1285 | Context* c = context_list[i]; |
| 1286 | if (c->result == net::ERR_IO_PENDING) |
| 1287 | c->result = c->callback.WaitForResult(); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1288 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1289 | } |
| 1290 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1291 | // We should not have had to re-open the disk entry |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1292 | |
| 1293 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1294 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1295 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1296 | |
| 1297 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1298 | Context* c = context_list[i]; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1299 | delete c; |
| 1300 | } |
| 1301 | } |
| 1302 | |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1303 | // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. |
| 1304 | // If cancelling a request is racing with another request for the same resource |
| 1305 | // finishing, we have to make sure that we remove both transactions from the |
| 1306 | // entry. |
| 1307 | TEST(HttpCache, SimpleGET_RacingReaders) { |
| 1308 | MockHttpCache cache; |
| 1309 | |
| 1310 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1311 | MockHttpRequest reader_request(kSimpleGET_Transaction); |
| 1312 | reader_request.load_flags = net::LOAD_ONLY_FROM_CACHE; |
| 1313 | |
| 1314 | std::vector<Context*> context_list; |
| 1315 | const int kNumTransactions = 5; |
| 1316 | |
| 1317 | for (int i = 0; i < kNumTransactions; ++i) { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1318 | context_list.push_back(new Context()); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1319 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1320 | |
| 1321 | c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1322 | EXPECT_EQ(net::OK, c->result); |
| 1323 | |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1324 | MockHttpRequest* this_request = &request; |
| 1325 | if (i == 1 || i == 2) |
| 1326 | this_request = &reader_request; |
| 1327 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1328 | c->result = c->trans->Start(this_request, &c->callback, NULL); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1329 | } |
| 1330 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1331 | // Allow all requests to move from the Create queue to the active entry. |
| 1332 | MessageLoop::current()->RunAllPending(); |
| 1333 | |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1334 | // The first request should be a writer at this point, and the subsequent |
| 1335 | // requests should be pending. |
| 1336 | |
| 1337 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1338 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1339 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1340 | |
| 1341 | Context* c = context_list[0]; |
| 1342 | ASSERT_EQ(net::ERR_IO_PENDING, c->result); |
| 1343 | c->result = c->callback.WaitForResult(); |
| 1344 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1345 | |
| 1346 | // Now we have 2 active readers and two queued transactions. |
| 1347 | |
| 1348 | c = context_list[1]; |
| 1349 | ASSERT_EQ(net::ERR_IO_PENDING, c->result); |
| 1350 | c->result = c->callback.WaitForResult(); |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 1351 | if (c->result == net::OK) |
| 1352 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1353 | |
| 1354 | // At this point we have one reader, two pending transactions and a task on |
| 1355 | // the queue to move to the next transaction. Now we cancel the request that |
| 1356 | // is the current reader, and expect the queued task to be able to start the |
| 1357 | // next request. |
| 1358 | |
| 1359 | c = context_list[2]; |
| 1360 | c->trans.reset(); |
| 1361 | |
| 1362 | for (int i = 3; i < kNumTransactions; ++i) { |
| 1363 | Context* c = context_list[i]; |
| 1364 | if (c->result == net::ERR_IO_PENDING) |
| 1365 | c->result = c->callback.WaitForResult(); |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 1366 | if (c->result == net::OK) |
| 1367 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | // We should not have had to re-open the disk entry. |
| 1371 | |
| 1372 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1373 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1374 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1375 | |
| 1376 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1377 | Context* c = context_list[i]; |
| 1378 | delete c; |
| 1379 | } |
| 1380 | } |
| 1381 | |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1382 | // Tests that we can doom an entry with pending transactions and delete one of |
| 1383 | // the pending transactions before the first one completes. |
| 1384 | // See http://code.google.com/p/chromium/issues/detail?id=25588 |
| 1385 | TEST(HttpCache, SimpleGET_DoomWithPending) { |
| 1386 | // We need simultaneous doomed / not_doomed entries so let's use a real cache. |
| 1387 | disk_cache::Backend* disk_cache = |
| 1388 | disk_cache::CreateInMemoryCacheBackend(1024 * 1024); |
| 1389 | MockHttpCache cache(disk_cache); |
| 1390 | |
| 1391 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1392 | MockHttpRequest writer_request(kSimpleGET_Transaction); |
| 1393 | writer_request.load_flags = net::LOAD_BYPASS_CACHE; |
| 1394 | |
| 1395 | ScopedVector<Context> context_list; |
| 1396 | const int kNumTransactions = 4; |
| 1397 | |
| 1398 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1399 | context_list.push_back(new Context()); |
| 1400 | Context* c = context_list[i]; |
| 1401 | |
| 1402 | c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1403 | EXPECT_EQ(net::OK, c->result); |
| 1404 | |
| 1405 | MockHttpRequest* this_request = &request; |
| 1406 | if (i == 3) |
| 1407 | this_request = &writer_request; |
| 1408 | |
| 1409 | c->result = c->trans->Start(this_request, &c->callback, NULL); |
| 1410 | } |
| 1411 | |
| 1412 | // The first request should be a writer at this point, and the two subsequent |
| 1413 | // requests should be pending. The last request doomed the first entry. |
| 1414 | |
| 1415 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1416 | |
| 1417 | // Cancel the first queued transaction. |
| 1418 | delete context_list[1]; |
| 1419 | context_list.get()[1] = NULL; |
| 1420 | |
| 1421 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1422 | if (i == 1) |
| 1423 | continue; |
| 1424 | Context* c = context_list[i]; |
| 1425 | ASSERT_EQ(net::ERR_IO_PENDING, c->result); |
| 1426 | c->result = c->callback.WaitForResult(); |
| 1427 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1428 | } |
| 1429 | } |
| 1430 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1431 | // This is a test for http://code.google.com/p/chromium/issues/detail?id=4731. |
| 1432 | // We may attempt to delete an entry synchronously with the act of adding a new |
| 1433 | // transaction to said entry. |
| 1434 | TEST(HttpCache, FastNoStoreGET_DoneWithPending) { |
| 1435 | MockHttpCache cache; |
| 1436 | |
| 1437 | // The headers will be served right from the call to Start() the request. |
| 1438 | MockHttpRequest request(kFastNoStoreGET_Transaction); |
| 1439 | FastTransactionServer request_handler; |
| 1440 | AddMockTransaction(&kFastNoStoreGET_Transaction); |
| 1441 | |
| 1442 | std::vector<Context*> context_list; |
| 1443 | const int kNumTransactions = 3; |
| 1444 | |
| 1445 | for (int i = 0; i < kNumTransactions; ++i) { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1446 | context_list.push_back(new Context()); |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1447 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1448 | |
| 1449 | c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1450 | EXPECT_EQ(net::OK, c->result); |
| 1451 | |
| 1452 | c->result = c->trans->Start(&request, &c->callback, NULL); |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1453 | } |
| 1454 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1455 | // Allow all requests to move from the Create queue to the active entry. |
| 1456 | MessageLoop::current()->RunAllPending(); |
| 1457 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1458 | // The first request should be a writer at this point, and the subsequent |
| 1459 | // requests should be pending. |
| 1460 | |
| 1461 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1462 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1463 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1464 | |
| 1465 | // Now, make sure that the second request asks for the entry not to be stored. |
| 1466 | request_handler.set_no_store(true); |
| 1467 | |
| 1468 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1469 | Context* c = context_list[i]; |
| 1470 | if (c->result == net::ERR_IO_PENDING) |
| 1471 | c->result = c->callback.WaitForResult(); |
| 1472 | ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); |
| 1473 | delete c; |
| 1474 | } |
| 1475 | |
| 1476 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1477 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1478 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1479 | |
| 1480 | RemoveMockTransaction(&kFastNoStoreGET_Transaction); |
| 1481 | } |
| 1482 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1483 | TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { |
| 1484 | MockHttpCache cache; |
| 1485 | |
| 1486 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1487 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1488 | std::vector<Context*> context_list; |
| 1489 | const int kNumTransactions = 2; |
| 1490 | |
| 1491 | for (int i = 0; i < kNumTransactions; ++i) { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1492 | context_list.push_back(new Context()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1493 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1494 | |
| 1495 | c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1496 | EXPECT_EQ(net::OK, c->result); |
| 1497 | |
| 1498 | c->result = c->trans->Start(&request, &c->callback, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1499 | } |
| 1500 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1501 | // Allow all requests to move from the Create queue to the active entry. |
| 1502 | MessageLoop::current()->RunAllPending(); |
| 1503 | |
| 1504 | // The first request should be a writer at this point, and the subsequent |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1505 | // requests should be pending. |
| 1506 | |
| 1507 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1508 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1509 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1510 | |
| 1511 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1512 | Context* c = context_list[i]; |
| 1513 | if (c->result == net::ERR_IO_PENDING) |
| 1514 | c->result = c->callback.WaitForResult(); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1515 | // Destroy only the first transaction. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1516 | if (i == 0) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1517 | delete c; |
| 1518 | context_list[i] = NULL; |
| 1519 | } |
| 1520 | } |
| 1521 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1522 | // Complete the rest of the transactions. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1523 | for (int i = 1; i < kNumTransactions; ++i) { |
| 1524 | Context* c = context_list[i]; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1525 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1526 | } |
| 1527 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1528 | // We should have had to re-open the disk entry. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1529 | |
| 1530 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1531 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1532 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1533 | |
| 1534 | for (int i = 1; i < kNumTransactions; ++i) { |
| 1535 | Context* c = context_list[i]; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1536 | delete c; |
| 1537 | } |
| 1538 | } |
| 1539 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1540 | // Tests that we can cancel requests that are queued waiting to open the disk |
| 1541 | // cache entry. |
| 1542 | TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) { |
| 1543 | MockHttpCache cache; |
| 1544 | |
| 1545 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1546 | |
| 1547 | std::vector<Context*> context_list; |
| 1548 | const int kNumTransactions = 5; |
| 1549 | |
| 1550 | for (int i = 0; i < kNumTransactions; i++) { |
| 1551 | context_list.push_back(new Context()); |
| 1552 | Context* c = context_list[i]; |
| 1553 | |
| 1554 | c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1555 | EXPECT_EQ(net::OK, c->result); |
| 1556 | |
| 1557 | c->result = c->trans->Start(&request, &c->callback, NULL); |
| 1558 | } |
| 1559 | |
| 1560 | // The first request should be creating the disk cache entry and the others |
| 1561 | // should be pending. |
| 1562 | |
| 1563 | EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 1564 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1565 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1566 | |
| 1567 | // Cancel a request from the pending queue. |
| 1568 | delete context_list[3]; |
| 1569 | context_list[3] = NULL; |
| 1570 | |
| 1571 | // Cancel the request that is creating the entry. This will force the pending |
| 1572 | // operations to restart. |
| 1573 | delete context_list[0]; |
| 1574 | context_list[0] = NULL; |
| 1575 | |
| 1576 | // Complete the rest of the transactions. |
| 1577 | for (int i = 1; i < kNumTransactions; i++) { |
| 1578 | Context* c = context_list[i]; |
| 1579 | if (c) { |
| 1580 | c->result = c->callback.GetResult(c->result); |
| 1581 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | // We should have had to re-create the disk entry. |
| 1586 | |
| 1587 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1588 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1589 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1590 | |
| 1591 | for (int i = 1; i < kNumTransactions; ++i) { |
| 1592 | delete context_list[i]; |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | // Tests that we delete/create entries even if multiple requests are queued. |
| 1597 | TEST(HttpCache, SimpleGET_ManyWriters_BypassCache) { |
| 1598 | MockHttpCache cache; |
| 1599 | |
| 1600 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1601 | request.load_flags = net::LOAD_BYPASS_CACHE; |
| 1602 | |
| 1603 | std::vector<Context*> context_list; |
| 1604 | const int kNumTransactions = 5; |
| 1605 | |
| 1606 | for (int i = 0; i < kNumTransactions; i++) { |
| 1607 | context_list.push_back(new Context()); |
| 1608 | Context* c = context_list[i]; |
| 1609 | |
| 1610 | c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1611 | EXPECT_EQ(net::OK, c->result); |
| 1612 | |
| 1613 | c->result = c->trans->Start(&request, &c->callback, NULL); |
| 1614 | } |
| 1615 | |
| 1616 | // The first request should be deleting the disk cache entry and the others |
| 1617 | // should be pending. |
| 1618 | |
| 1619 | EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 1620 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1621 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 1622 | |
| 1623 | // Complete the transactions. |
| 1624 | for (int i = 0; i < kNumTransactions; i++) { |
| 1625 | Context* c = context_list[i]; |
| 1626 | c->result = c->callback.GetResult(c->result); |
| 1627 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1628 | } |
| 1629 | |
| 1630 | // We should have had to re-create the disk entry multiple times. |
| 1631 | |
| 1632 | EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
| 1633 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1634 | EXPECT_EQ(5, cache.disk_cache()->create_count()); |
| 1635 | |
| 1636 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1637 | delete context_list[i]; |
| 1638 | } |
| 1639 | } |
| 1640 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1641 | TEST(HttpCache, SimpleGET_AbandonedCacheRead) { |
| 1642 | MockHttpCache cache; |
| 1643 | |
| 1644 | // write to the cache |
| 1645 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1646 | |
| 1647 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1648 | TestCompletionCallback callback; |
| 1649 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1650 | scoped_ptr<net::HttpTransaction> trans; |
| 1651 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 1652 | EXPECT_EQ(net::OK, rv); |
| 1653 | rv = trans->Start(&request, &callback, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1654 | if (rv == net::ERR_IO_PENDING) |
| 1655 | rv = callback.WaitForResult(); |
| 1656 | ASSERT_EQ(net::OK, rv); |
| 1657 | |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 1658 | scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(256); |
| 1659 | rv = trans->Read(buf, 256, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1660 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 1661 | |
| 1662 | // Test that destroying the transaction while it is reading from the cache |
| 1663 | // works properly. |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1664 | trans.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1665 | |
| 1666 | // Make sure we pump any pending events, which should include a call to |
| 1667 | // HttpCache::Transaction::OnCacheReadCompleted. |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 1668 | MessageLoop::current()->RunAllPending(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | TEST(HttpCache, TypicalGET_ConditionalRequest) { |
| 1672 | MockHttpCache cache; |
| 1673 | |
| 1674 | // write to the cache |
| 1675 | RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction); |
| 1676 | |
| 1677 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1678 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1679 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1680 | |
| 1681 | // get the same URL again, but this time we expect it to result |
| 1682 | // in a conditional request. |
| 1683 | RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction); |
| 1684 | |
| 1685 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1686 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1687 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1688 | } |
| 1689 | |
| 1690 | static void ETagGet_ConditionalRequest_Handler( |
| 1691 | const net::HttpRequestInfo* request, |
| 1692 | std::string* response_status, |
| 1693 | std::string* response_headers, |
| 1694 | std::string* response_data) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 1695 | EXPECT_TRUE( |
| 1696 | request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1697 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 1698 | response_headers->assign(kETagGET_Transaction.response_headers); |
| 1699 | response_data->clear(); |
| 1700 | } |
| 1701 | |
| 1702 | TEST(HttpCache, ETagGET_ConditionalRequest_304) { |
| 1703 | MockHttpCache cache; |
| 1704 | |
| 1705 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 1706 | |
| 1707 | // write to the cache |
| 1708 | RunTransactionTest(cache.http_cache(), transaction); |
| 1709 | |
| 1710 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1711 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1712 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1713 | |
| 1714 | // get the same URL again, but this time we expect it to result |
| 1715 | // in a conditional request. |
| 1716 | transaction.load_flags = net::LOAD_VALIDATE_CACHE; |
| 1717 | transaction.handler = ETagGet_ConditionalRequest_Handler; |
| 1718 | RunTransactionTest(cache.http_cache(), transaction); |
| 1719 | |
| 1720 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1721 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1722 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1723 | } |
| 1724 | |
[email protected] | b7d05ab | 2008-12-09 19:18:41 | [diff] [blame] | 1725 | static void ETagGet_ConditionalRequest_NoStore_Handler( |
| 1726 | const net::HttpRequestInfo* request, |
| 1727 | std::string* response_status, |
| 1728 | std::string* response_headers, |
| 1729 | std::string* response_data) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 1730 | EXPECT_TRUE( |
| 1731 | request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch)); |
[email protected] | b7d05ab | 2008-12-09 19:18:41 | [diff] [blame] | 1732 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 1733 | response_headers->assign("Cache-Control: no-store\n"); |
| 1734 | response_data->clear(); |
| 1735 | } |
| 1736 | |
| 1737 | TEST(HttpCache, ETagGET_ConditionalRequest_304_NoStore) { |
| 1738 | MockHttpCache cache; |
| 1739 | |
| 1740 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 1741 | |
| 1742 | // Write to the cache. |
| 1743 | RunTransactionTest(cache.http_cache(), transaction); |
| 1744 | |
| 1745 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1746 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1747 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1748 | |
| 1749 | // Get the same URL again, but this time we expect it to result |
| 1750 | // in a conditional request. |
| 1751 | transaction.load_flags = net::LOAD_VALIDATE_CACHE; |
| 1752 | transaction.handler = ETagGet_ConditionalRequest_NoStore_Handler; |
| 1753 | RunTransactionTest(cache.http_cache(), transaction); |
| 1754 | |
| 1755 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1756 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1757 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1758 | |
| 1759 | ScopedMockTransaction transaction2(kETagGET_Transaction); |
| 1760 | |
| 1761 | // Write to the cache again. This should create a new entry. |
| 1762 | RunTransactionTest(cache.http_cache(), transaction2); |
| 1763 | |
| 1764 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1765 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1766 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1767 | } |
| 1768 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1769 | TEST(HttpCache, SimplePOST_SkipsCache) { |
| 1770 | MockHttpCache cache; |
| 1771 | |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 1772 | // Test that we skip the cache for POST requests that do not have an upload |
| 1773 | // identifier. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1774 | |
| 1775 | RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction); |
| 1776 | |
| 1777 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1778 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1779 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 1780 | } |
| 1781 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 1782 | // Helper that does 4 requests using HttpCache: |
| 1783 | // |
| 1784 | // (1) loads |kUrl| -- expects |net_response_1| to be returned. |
| 1785 | // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned. |
| 1786 | // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to |
| 1787 | // be returned. |
| 1788 | // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be |
| 1789 | // returned. |
| 1790 | static void ConditionalizedRequestUpdatesCacheHelper( |
| 1791 | const Response& net_response_1, |
| 1792 | const Response& net_response_2, |
| 1793 | const Response& cached_response_2, |
| 1794 | const char* extra_request_headers) { |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 1795 | MockHttpCache cache; |
| 1796 | |
| 1797 | // The URL we will be requesting. |
| 1798 | const char* kUrl = "http://foobar.com/main.css"; |
| 1799 | |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 1800 | // Junk network response. |
| 1801 | static const Response kUnexpectedResponse = { |
| 1802 | "HTTP/1.1 500 Unexpected", |
| 1803 | "Server: unexpected_header", |
| 1804 | "unexpected body" |
| 1805 | }; |
| 1806 | |
| 1807 | // We will control the network layer's responses for |kUrl| using |
| 1808 | // |mock_network_response|. |
| 1809 | MockTransaction mock_network_response = { 0 }; |
| 1810 | mock_network_response.url = kUrl; |
| 1811 | AddMockTransaction(&mock_network_response); |
| 1812 | |
| 1813 | // Request |kUrl| for the first time. It should hit the network and |
| 1814 | // receive |kNetResponse1|, which it saves into the HTTP cache. |
| 1815 | |
| 1816 | MockTransaction request = { 0 }; |
| 1817 | request.url = kUrl; |
| 1818 | request.method = "GET"; |
| 1819 | request.request_headers = ""; |
| 1820 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 1821 | net_response_1.AssignTo(&mock_network_response); // Network mock. |
| 1822 | net_response_1.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 1823 | |
| 1824 | std::string response_headers; |
| 1825 | RunTransactionTestWithResponse( |
| 1826 | cache.http_cache(), request, &response_headers); |
| 1827 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 1828 | EXPECT_EQ(net_response_1.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 1829 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1830 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1831 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1832 | |
[email protected] | 6f40bf7 | 2009-07-23 17:52:37 | [diff] [blame] | 1833 | // Request |kUrl| a second time. Now |kNetResponse1| it is in the HTTP |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 1834 | // cache, so we don't hit the network. |
| 1835 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 1836 | request.load_flags = net::LOAD_ONLY_FROM_CACHE; |
| 1837 | |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 1838 | kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 1839 | net_response_1.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 1840 | |
| 1841 | RunTransactionTestWithResponse( |
| 1842 | cache.http_cache(), request, &response_headers); |
| 1843 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 1844 | EXPECT_EQ(net_response_1.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 1845 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1846 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1847 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1848 | |
| 1849 | // Request |kUrl| yet again, but this time give the request an |
| 1850 | // "If-Modified-Since" header. This will cause the request to re-hit the |
| 1851 | // network. However now the network response is going to be |
| 1852 | // different -- this simulates a change made to the CSS file. |
| 1853 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 1854 | request.request_headers = extra_request_headers; |
| 1855 | request.load_flags = net::LOAD_NORMAL; |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 1856 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 1857 | net_response_2.AssignTo(&mock_network_response); // Network mock. |
| 1858 | net_response_2.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 1859 | |
| 1860 | RunTransactionTestWithResponse( |
| 1861 | cache.http_cache(), request, &response_headers); |
| 1862 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 1863 | EXPECT_EQ(net_response_2.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 1864 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1865 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1866 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1867 | |
| 1868 | // Finally, request |kUrl| again. This request should be serviced from |
| 1869 | // the cache. Moreover, the value in the cache should be |kNetResponse2| |
| 1870 | // and NOT |kNetResponse1|. The previous step should have replaced the |
| 1871 | // value in the cache with the modified response. |
| 1872 | |
| 1873 | request.request_headers = ""; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 1874 | request.load_flags = net::LOAD_ONLY_FROM_CACHE; |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 1875 | |
| 1876 | kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 1877 | cached_response_2.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 1878 | |
| 1879 | RunTransactionTestWithResponse( |
| 1880 | cache.http_cache(), request, &response_headers); |
| 1881 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 1882 | EXPECT_EQ(cached_response_2.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 1883 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1884 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 1885 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1886 | |
| 1887 | RemoveMockTransaction(&mock_network_response); |
| 1888 | } |
| 1889 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 1890 | // Check that when an "if-modified-since" header is attached |
| 1891 | // to the request, the result still updates the cached entry. |
| 1892 | TEST(HttpCache, ConditionalizedRequestUpdatesCache1) { |
| 1893 | // First network response for |kUrl|. |
| 1894 | static const Response kNetResponse1 = { |
| 1895 | "HTTP/1.1 200 OK", |
| 1896 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 1897 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 1898 | "body1" |
| 1899 | }; |
| 1900 | |
| 1901 | // Second network response for |kUrl|. |
| 1902 | static const Response kNetResponse2 = { |
| 1903 | "HTTP/1.1 200 OK", |
| 1904 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 1905 | "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 1906 | "body2" |
| 1907 | }; |
| 1908 | |
| 1909 | const char* extra_headers = |
| 1910 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\n"; |
| 1911 | |
| 1912 | ConditionalizedRequestUpdatesCacheHelper( |
| 1913 | kNetResponse1, kNetResponse2, kNetResponse2, extra_headers); |
| 1914 | } |
| 1915 | |
| 1916 | // Check that when an "if-none-match" header is attached |
| 1917 | // to the request, the result updates the cached entry. |
| 1918 | TEST(HttpCache, ConditionalizedRequestUpdatesCache2) { |
| 1919 | // First network response for |kUrl|. |
| 1920 | static const Response kNetResponse1 = { |
| 1921 | "HTTP/1.1 200 OK", |
| 1922 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 1923 | "Etag: \"ETAG1\"\n" |
| 1924 | "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire. |
| 1925 | "body1" |
| 1926 | }; |
| 1927 | |
| 1928 | // Second network response for |kUrl|. |
| 1929 | static const Response kNetResponse2 = { |
| 1930 | "HTTP/1.1 200 OK", |
| 1931 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 1932 | "Etag: \"ETAG2\"\n" |
| 1933 | "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire. |
| 1934 | "body2" |
| 1935 | }; |
| 1936 | |
| 1937 | const char* extra_headers = "If-None-Match: \"ETAG1\"\n"; |
| 1938 | |
| 1939 | ConditionalizedRequestUpdatesCacheHelper( |
| 1940 | kNetResponse1, kNetResponse2, kNetResponse2, extra_headers); |
| 1941 | } |
| 1942 | |
| 1943 | // Check that when an "if-modified-since" header is attached |
| 1944 | // to a request, the 304 (not modified result) result updates the cached |
| 1945 | // headers, and the 304 response is returned rather than the cached response. |
| 1946 | TEST(HttpCache, ConditionalizedRequestUpdatesCache3) { |
| 1947 | // First network response for |kUrl|. |
| 1948 | static const Response kNetResponse1 = { |
| 1949 | "HTTP/1.1 200 OK", |
| 1950 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 1951 | "Server: server1\n" |
| 1952 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 1953 | "body1" |
| 1954 | }; |
| 1955 | |
| 1956 | // Second network response for |kUrl|. |
| 1957 | static const Response kNetResponse2 = { |
| 1958 | "HTTP/1.1 304 Not Modified", |
| 1959 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 1960 | "Server: server2\n" |
| 1961 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 1962 | "" |
| 1963 | }; |
| 1964 | |
| 1965 | static const Response kCachedResponse2 = { |
| 1966 | "HTTP/1.1 200 OK", |
| 1967 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 1968 | "Server: server2\n" |
| 1969 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 1970 | "body1" |
| 1971 | }; |
| 1972 | |
| 1973 | const char* extra_headers = |
| 1974 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\n"; |
| 1975 | |
| 1976 | ConditionalizedRequestUpdatesCacheHelper( |
| 1977 | kNetResponse1, kNetResponse2, kCachedResponse2, extra_headers); |
| 1978 | } |
| 1979 | |
| 1980 | // Test that when doing an externally conditionalized if-modified-since |
| 1981 | // and there is no corresponding cache entry, a new cache entry is NOT |
| 1982 | // created (304 response). |
| 1983 | TEST(HttpCache, ConditionalizedRequestUpdatesCache4) { |
| 1984 | MockHttpCache cache; |
| 1985 | |
| 1986 | const char* kUrl = "http://foobar.com/main.css"; |
| 1987 | |
| 1988 | static const Response kNetResponse = { |
| 1989 | "HTTP/1.1 304 Not Modified", |
| 1990 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 1991 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 1992 | "" |
| 1993 | }; |
| 1994 | |
| 1995 | const char* kExtraRequestHeaders = |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 1996 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 1997 | |
| 1998 | // We will control the network layer's responses for |kUrl| using |
| 1999 | // |mock_network_response|. |
| 2000 | MockTransaction mock_network_response = { 0 }; |
| 2001 | mock_network_response.url = kUrl; |
| 2002 | AddMockTransaction(&mock_network_response); |
| 2003 | |
| 2004 | MockTransaction request = { 0 }; |
| 2005 | request.url = kUrl; |
| 2006 | request.method = "GET"; |
| 2007 | request.request_headers = kExtraRequestHeaders; |
| 2008 | |
| 2009 | kNetResponse.AssignTo(&mock_network_response); // Network mock. |
| 2010 | kNetResponse.AssignTo(&request); // Expected result. |
| 2011 | |
| 2012 | std::string response_headers; |
| 2013 | RunTransactionTestWithResponse( |
| 2014 | cache.http_cache(), request, &response_headers); |
| 2015 | |
| 2016 | EXPECT_EQ(kNetResponse.status_and_headers(), response_headers); |
| 2017 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2018 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2019 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2020 | |
| 2021 | RemoveMockTransaction(&mock_network_response); |
| 2022 | } |
| 2023 | |
| 2024 | // Test that when doing an externally conditionalized if-modified-since |
| 2025 | // and there is no corresponding cache entry, a new cache entry is NOT |
| 2026 | // created (200 response). |
| 2027 | TEST(HttpCache, ConditionalizedRequestUpdatesCache5) { |
| 2028 | MockHttpCache cache; |
| 2029 | |
| 2030 | const char* kUrl = "http://foobar.com/main.css"; |
| 2031 | |
| 2032 | static const Response kNetResponse = { |
| 2033 | "HTTP/1.1 200 OK", |
| 2034 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2035 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2036 | "foobar!!!" |
| 2037 | }; |
| 2038 | |
| 2039 | const char* kExtraRequestHeaders = |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2040 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2041 | |
| 2042 | // We will control the network layer's responses for |kUrl| using |
| 2043 | // |mock_network_response|. |
| 2044 | MockTransaction mock_network_response = { 0 }; |
| 2045 | mock_network_response.url = kUrl; |
| 2046 | AddMockTransaction(&mock_network_response); |
| 2047 | |
| 2048 | MockTransaction request = { 0 }; |
| 2049 | request.url = kUrl; |
| 2050 | request.method = "GET"; |
| 2051 | request.request_headers = kExtraRequestHeaders; |
| 2052 | |
| 2053 | kNetResponse.AssignTo(&mock_network_response); // Network mock. |
| 2054 | kNetResponse.AssignTo(&request); // Expected result. |
| 2055 | |
| 2056 | std::string response_headers; |
| 2057 | RunTransactionTestWithResponse( |
| 2058 | cache.http_cache(), request, &response_headers); |
| 2059 | |
| 2060 | EXPECT_EQ(kNetResponse.status_and_headers(), response_headers); |
| 2061 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2062 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2063 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2064 | |
| 2065 | RemoveMockTransaction(&mock_network_response); |
| 2066 | } |
| 2067 | |
| 2068 | // Test that when doing an externally conditionalized if-modified-since |
| 2069 | // if the date does not match the cache entry's last-modified date, |
| 2070 | // then we do NOT use the response (304) to update the cache. |
| 2071 | // (the if-modified-since date is 2 days AFTER the cache's modification date). |
| 2072 | TEST(HttpCache, ConditionalizedRequestUpdatesCache6) { |
| 2073 | static const Response kNetResponse1 = { |
| 2074 | "HTTP/1.1 200 OK", |
| 2075 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2076 | "Server: server1\n" |
| 2077 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2078 | "body1" |
| 2079 | }; |
| 2080 | |
| 2081 | // Second network response for |kUrl|. |
| 2082 | static const Response kNetResponse2 = { |
| 2083 | "HTTP/1.1 304 Not Modified", |
| 2084 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2085 | "Server: server2\n" |
| 2086 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2087 | "" |
| 2088 | }; |
| 2089 | |
| 2090 | // This is two days in the future from the original response's last-modified |
| 2091 | // date! |
| 2092 | const char* kExtraRequestHeaders = |
| 2093 | "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\n"; |
| 2094 | |
| 2095 | ConditionalizedRequestUpdatesCacheHelper( |
| 2096 | kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2097 | } |
| 2098 | |
| 2099 | // Test that when doing an externally conditionalized if-none-match |
| 2100 | // if the etag does not match the cache entry's etag, then we do not use the |
| 2101 | // response (304) to update the cache. |
| 2102 | TEST(HttpCache, ConditionalizedRequestUpdatesCache7) { |
| 2103 | static const Response kNetResponse1 = { |
| 2104 | "HTTP/1.1 200 OK", |
| 2105 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2106 | "Etag: \"Foo1\"\n" |
| 2107 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2108 | "body1" |
| 2109 | }; |
| 2110 | |
| 2111 | // Second network response for |kUrl|. |
| 2112 | static const Response kNetResponse2 = { |
| 2113 | "HTTP/1.1 304 Not Modified", |
| 2114 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2115 | "Etag: \"Foo2\"\n" |
| 2116 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2117 | "" |
| 2118 | }; |
| 2119 | |
| 2120 | // Different etag from original response. |
| 2121 | const char* kExtraRequestHeaders = "If-None-Match: \"Foo2\"\n"; |
| 2122 | |
| 2123 | ConditionalizedRequestUpdatesCacheHelper( |
| 2124 | kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2125 | } |
| 2126 | |
[email protected] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2127 | // Test that doing an externally conditionalized request with both if-none-match |
| 2128 | // and if-modified-since updates the cache. |
| 2129 | TEST(HttpCache, ConditionalizedRequestUpdatesCache8) { |
| 2130 | static const Response kNetResponse1 = { |
| 2131 | "HTTP/1.1 200 OK", |
| 2132 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2133 | "Etag: \"Foo1\"\n" |
| 2134 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2135 | "body1" |
| 2136 | }; |
| 2137 | |
| 2138 | // Second network response for |kUrl|. |
| 2139 | static const Response kNetResponse2 = { |
| 2140 | "HTTP/1.1 200 OK", |
| 2141 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2142 | "Etag: \"Foo2\"\n" |
| 2143 | "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2144 | "body2" |
| 2145 | }; |
| 2146 | |
| 2147 | const char* kExtraRequestHeaders = |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2148 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n" |
| 2149 | "If-None-Match: \"Foo1\"\r\n"; |
[email protected] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2150 | |
| 2151 | ConditionalizedRequestUpdatesCacheHelper( |
| 2152 | kNetResponse1, kNetResponse2, kNetResponse2, kExtraRequestHeaders); |
| 2153 | } |
| 2154 | |
| 2155 | // Test that doing an externally conditionalized request with both if-none-match |
| 2156 | // and if-modified-since does not update the cache with only one match. |
| 2157 | TEST(HttpCache, ConditionalizedRequestUpdatesCache9) { |
| 2158 | static const Response kNetResponse1 = { |
| 2159 | "HTTP/1.1 200 OK", |
| 2160 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2161 | "Etag: \"Foo1\"\n" |
| 2162 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2163 | "body1" |
| 2164 | }; |
| 2165 | |
| 2166 | // Second network response for |kUrl|. |
| 2167 | static const Response kNetResponse2 = { |
| 2168 | "HTTP/1.1 200 OK", |
| 2169 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2170 | "Etag: \"Foo2\"\n" |
| 2171 | "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2172 | "body2" |
| 2173 | }; |
| 2174 | |
| 2175 | // The etag doesn't match what we have stored. |
| 2176 | const char* kExtraRequestHeaders = |
| 2177 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\n" |
| 2178 | "If-None-Match: \"Foo2\"\n"; |
| 2179 | |
| 2180 | ConditionalizedRequestUpdatesCacheHelper( |
| 2181 | kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2182 | } |
| 2183 | |
| 2184 | // Test that doing an externally conditionalized request with both if-none-match |
| 2185 | // and if-modified-since does not update the cache with only one match. |
| 2186 | TEST(HttpCache, ConditionalizedRequestUpdatesCache10) { |
| 2187 | static const Response kNetResponse1 = { |
| 2188 | "HTTP/1.1 200 OK", |
| 2189 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2190 | "Etag: \"Foo1\"\n" |
| 2191 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2192 | "body1" |
| 2193 | }; |
| 2194 | |
| 2195 | // Second network response for |kUrl|. |
| 2196 | static const Response kNetResponse2 = { |
| 2197 | "HTTP/1.1 200 OK", |
| 2198 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2199 | "Etag: \"Foo2\"\n" |
| 2200 | "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2201 | "body2" |
| 2202 | }; |
| 2203 | |
| 2204 | // The modification date doesn't match what we have stored. |
| 2205 | const char* kExtraRequestHeaders = |
| 2206 | "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\n" |
| 2207 | "If-None-Match: \"Foo1\"\n"; |
| 2208 | |
| 2209 | ConditionalizedRequestUpdatesCacheHelper( |
| 2210 | kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2211 | } |
| 2212 | |
| 2213 | // Test that doing an externally conditionalized request with two conflicting |
| 2214 | // headers does not update the cache. |
| 2215 | TEST(HttpCache, ConditionalizedRequestUpdatesCache11) { |
| 2216 | static const Response kNetResponse1 = { |
| 2217 | "HTTP/1.1 200 OK", |
| 2218 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2219 | "Etag: \"Foo1\"\n" |
| 2220 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2221 | "body1" |
| 2222 | }; |
| 2223 | |
| 2224 | // Second network response for |kUrl|. |
| 2225 | static const Response kNetResponse2 = { |
| 2226 | "HTTP/1.1 200 OK", |
| 2227 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2228 | "Etag: \"Foo2\"\n" |
| 2229 | "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2230 | "body2" |
| 2231 | }; |
| 2232 | |
| 2233 | // Two dates, the second matches what we have stored. |
| 2234 | const char* kExtraRequestHeaders = |
| 2235 | "If-Modified-Since: Mon, 04 Feb 2008 22:38:21 GMT\n" |
| 2236 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\n"; |
| 2237 | |
| 2238 | ConditionalizedRequestUpdatesCacheHelper( |
| 2239 | kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2240 | } |
| 2241 | |
[email protected] | 6f40bf7 | 2009-07-23 17:52:37 | [diff] [blame] | 2242 | TEST(HttpCache, UrlContainingHash) { |
| 2243 | MockHttpCache cache; |
| 2244 | |
| 2245 | // Do a typical GET request -- should write an entry into our cache. |
| 2246 | MockTransaction trans(kTypicalGET_Transaction); |
| 2247 | RunTransactionTest(cache.http_cache(), trans); |
| 2248 | |
| 2249 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2250 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2251 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2252 | |
| 2253 | // Request the same URL, but this time with a reference section (hash). |
| 2254 | // Since the cache key strips the hash sections, this should be a cache hit. |
| 2255 | std::string url_with_hash = std::string(trans.url) + "#multiple#hashes"; |
| 2256 | trans.url = url_with_hash.c_str(); |
| 2257 | trans.load_flags = net::LOAD_ONLY_FROM_CACHE; |
| 2258 | |
| 2259 | RunTransactionTest(cache.http_cache(), trans); |
| 2260 | |
| 2261 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2262 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2263 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2264 | } |
| 2265 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2266 | TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) { |
| 2267 | MockHttpCache cache; |
| 2268 | |
| 2269 | // Test that we skip the cache for POST requests. Eventually, we will want |
| 2270 | // to cache these, but we'll still have cases where skipping the cache makes |
| 2271 | // sense, so we want to make sure that it works properly. |
| 2272 | |
| 2273 | MockTransaction transaction(kSimplePOST_Transaction); |
| 2274 | transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 2275 | |
| 2276 | MockHttpRequest request(transaction); |
| 2277 | TestCompletionCallback callback; |
| 2278 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 2279 | scoped_ptr<net::HttpTransaction> trans; |
| 2280 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 2281 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 2282 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2283 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 2284 | rv = trans->Start(&request, &callback, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2285 | if (rv == net::ERR_IO_PENDING) |
| 2286 | rv = callback.WaitForResult(); |
| 2287 | ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
| 2288 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 2289 | trans.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2290 | |
| 2291 | EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 2292 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2293 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2294 | } |
| 2295 | |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2296 | TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Hit) { |
| 2297 | MockHttpCache cache; |
| 2298 | |
| 2299 | // Test that we hit the cache for POST requests. |
| 2300 | |
| 2301 | MockTransaction transaction(kSimplePOST_Transaction); |
| 2302 | |
| 2303 | const int64 kUploadId = 1; // Just a dummy value. |
| 2304 | |
| 2305 | MockHttpRequest request(transaction); |
| 2306 | request.upload_data = new net::UploadData(); |
| 2307 | request.upload_data->set_identifier(kUploadId); |
| 2308 | request.upload_data->AppendBytes("hello", 5); |
| 2309 | |
| 2310 | // Populate the cache. |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2311 | RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2312 | |
| 2313 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2314 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2315 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2316 | |
| 2317 | // Load from cache. |
| 2318 | request.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2319 | RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2320 | |
| 2321 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2322 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2323 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2324 | } |
| 2325 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2326 | TEST(HttpCache, RangeGET_SkipsCache) { |
| 2327 | MockHttpCache cache; |
| 2328 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2329 | // Test that we skip the cache for range GET requests. Eventually, we will |
| 2330 | // want to cache these, but we'll still have cases where skipping the cache |
| 2331 | // makes sense, so we want to make sure that it works properly. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2332 | |
| 2333 | RunTransactionTest(cache.http_cache(), kRangeGET_Transaction); |
| 2334 | |
| 2335 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2336 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2337 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2338 | |
| 2339 | MockTransaction transaction(kSimpleGET_Transaction); |
| 2340 | transaction.request_headers = "If-None-Match: foo"; |
| 2341 | RunTransactionTest(cache.http_cache(), transaction); |
| 2342 | |
| 2343 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2344 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2345 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2346 | |
[email protected] | 72d1e59 | 2009-03-10 17:39:46 | [diff] [blame] | 2347 | transaction.request_headers = |
| 2348 | "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2349 | RunTransactionTest(cache.http_cache(), transaction); |
| 2350 | |
| 2351 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2352 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2353 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2354 | } |
| 2355 | |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 2356 | // Test that we skip the cache for range requests that include a validation |
| 2357 | // header. |
| 2358 | TEST(HttpCache, RangeGET_SkipsCache2) { |
| 2359 | MockHttpCache cache; |
| 2360 | cache.http_cache()->set_enable_range_support(true); |
| 2361 | |
| 2362 | MockTransaction transaction(kRangeGET_Transaction); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2363 | transaction.request_headers = "If-None-Match: foo\r\n" |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2364 | EXTRA_HEADER |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2365 | "\r\nRange: bytes = 40-49"; |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 2366 | RunTransactionTest(cache.http_cache(), transaction); |
| 2367 | |
| 2368 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2369 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2370 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2371 | |
| 2372 | transaction.request_headers = |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2373 | "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n" |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2374 | EXTRA_HEADER |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2375 | "\r\nRange: bytes = 40-49"; |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 2376 | RunTransactionTest(cache.http_cache(), transaction); |
| 2377 | |
| 2378 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2379 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2380 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2381 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2382 | transaction.request_headers = "If-Range: bla\r\n" |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2383 | EXTRA_HEADER |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2384 | "\r\nRange: bytes = 40-49\n"; |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 2385 | RunTransactionTest(cache.http_cache(), transaction); |
| 2386 | |
| 2387 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2388 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2389 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2390 | } |
| 2391 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2392 | // Tests that receiving 206 for a regular request is handled correctly. |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 2393 | TEST(HttpCache, GET_Crazy206) { |
| 2394 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2395 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 2396 | |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 2397 | // Write to the cache. |
| 2398 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2399 | AddMockTransaction(&transaction); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2400 | transaction.request_headers = EXTRA_HEADER; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2401 | transaction.handler = NULL; |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 2402 | RunTransactionTest(cache.http_cache(), transaction); |
| 2403 | |
| 2404 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2405 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2406 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2407 | |
| 2408 | // This should read again from the net. |
| 2409 | RunTransactionTest(cache.http_cache(), transaction); |
| 2410 | |
| 2411 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2412 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2413 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2414 | RemoveMockTransaction(&transaction); |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 2415 | } |
| 2416 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2417 | // Tests that we can cache range requests and fetch random blocks from the |
| 2418 | // cache and the network. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2419 | TEST(HttpCache, RangeGET_OK) { |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2420 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2421 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2422 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2423 | std::string headers; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2424 | |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2425 | // Write to the cache (40-49). |
| 2426 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 2427 | &headers); |
| 2428 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2429 | Verify206Response(headers, 40, 49); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2430 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2431 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2432 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2433 | |
| 2434 | // Read from the cache (40-49). |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2435 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 2436 | &headers); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2437 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2438 | Verify206Response(headers, 40, 49); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2439 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2440 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2441 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2442 | |
| 2443 | // Make sure we are done with the previous transaction. |
| 2444 | MessageLoop::current()->RunAllPending(); |
| 2445 | |
| 2446 | // Write to the cache (30-39). |
| 2447 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2448 | transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2449 | transaction.data = "rg: 30-39 "; |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2450 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2451 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2452 | Verify206Response(headers, 30, 39); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2453 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2454 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 2455 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2456 | |
| 2457 | // Make sure we are done with the previous transaction. |
| 2458 | MessageLoop::current()->RunAllPending(); |
| 2459 | |
| 2460 | // Write and read from the cache (20-59). |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2461 | transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2462 | 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] | 2463 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2464 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2465 | Verify206Response(headers, 20, 59); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2466 | EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2467 | EXPECT_EQ(3, cache.disk_cache()->open_count()); |
| 2468 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2469 | |
| 2470 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 2471 | } |
| 2472 | |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 2473 | // Tests that we can cache range requests and fetch random blocks from the |
| 2474 | // cache and the network, with synchronous responses. |
| 2475 | TEST(HttpCache, RangeGET_SyncOK) { |
| 2476 | MockHttpCache cache; |
| 2477 | cache.http_cache()->set_enable_range_support(true); |
| 2478 | |
| 2479 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 2480 | transaction.test_mode = TEST_MODE_SYNC_ALL; |
| 2481 | AddMockTransaction(&transaction); |
| 2482 | |
| 2483 | // Write to the cache (40-49). |
| 2484 | std::string headers; |
| 2485 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2486 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2487 | Verify206Response(headers, 40, 49); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 2488 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2489 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2490 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2491 | |
| 2492 | // Read from the cache (40-49). |
| 2493 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2494 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2495 | Verify206Response(headers, 40, 49); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 2496 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2497 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2498 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2499 | |
| 2500 | // Make sure we are done with the previous transaction. |
| 2501 | MessageLoop::current()->RunAllPending(); |
| 2502 | |
| 2503 | // Write to the cache (30-39). |
| 2504 | transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; |
| 2505 | transaction.data = "rg: 30-39 "; |
| 2506 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2507 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2508 | Verify206Response(headers, 30, 39); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 2509 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2510 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2511 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2512 | |
| 2513 | // Make sure we are done with the previous transaction. |
| 2514 | MessageLoop::current()->RunAllPending(); |
| 2515 | |
| 2516 | // Write and read from the cache (20-59). |
| 2517 | transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
| 2518 | transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
| 2519 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2520 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2521 | Verify206Response(headers, 20, 59); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 2522 | EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
| 2523 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 2524 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2525 | |
| 2526 | RemoveMockTransaction(&transaction); |
| 2527 | } |
| 2528 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2529 | // Tests that we deal with 304s for range requests. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2530 | TEST(HttpCache, RangeGET_304) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2531 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2532 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2533 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 2534 | std::string headers; |
| 2535 | |
| 2536 | // Write to the cache (40-49). |
| 2537 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 2538 | &headers); |
| 2539 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2540 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 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 | // Read from the cache (40-49). |
| 2546 | RangeTransactionServer handler; |
| 2547 | handler.set_not_modified(true); |
| 2548 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 2549 | &headers); |
| 2550 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2551 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2552 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2553 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2554 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2555 | |
| 2556 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 2557 | } |
| 2558 | |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 2559 | // Tests that we deal with 206s when revalidating range requests. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2560 | TEST(HttpCache, RangeGET_ModifiedResult) { |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 2561 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2562 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 2563 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 2564 | std::string headers; |
| 2565 | |
| 2566 | // Write to the cache (40-49). |
| 2567 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 2568 | &headers); |
| 2569 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2570 | Verify206Response(headers, 40, 49); |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 2571 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2572 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2573 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2574 | |
| 2575 | // Attempt to read from the cache (40-49). |
| 2576 | RangeTransactionServer handler; |
| 2577 | handler.set_modified(true); |
| 2578 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 2579 | &headers); |
| 2580 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2581 | Verify206Response(headers, 40, 49); |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 2582 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2583 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2584 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2585 | |
| 2586 | // And the entry should be gone. |
| 2587 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 2588 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2589 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2590 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2591 | |
| 2592 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 2593 | } |
| 2594 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2595 | // Tests that we can cache range requests when the start or end is unknown. |
| 2596 | // 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] | 2597 | TEST(HttpCache, UnknownRangeGET_1) { |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2598 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2599 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2600 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2601 | std::string headers; |
| 2602 | |
| 2603 | // Write to the cache (70-79). |
| 2604 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2605 | transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2606 | transaction.data = "rg: 70-79 "; |
| 2607 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2608 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2609 | Verify206Response(headers, 70, 79); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2610 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2611 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2612 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2613 | |
| 2614 | // Make sure we are done with the previous transaction. |
| 2615 | MessageLoop::current()->RunAllPending(); |
| 2616 | |
| 2617 | // Write and read from the cache (60-79). |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2618 | transaction.request_headers = "Range: bytes = 60-\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2619 | transaction.data = "rg: 60-69 rg: 70-79 "; |
| 2620 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2621 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2622 | Verify206Response(headers, 60, 79); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2623 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2624 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2625 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2626 | |
| 2627 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 2628 | } |
| 2629 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2630 | // Tests that we can cache range requests when the start or end is unknown. |
| 2631 | // We start with one request from a given point, followed by a suffix request. |
| 2632 | // We'll also verify that synchronous cache responses work as intended. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2633 | TEST(HttpCache, UnknownRangeGET_2) { |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2634 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2635 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2636 | std::string headers; |
| 2637 | |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2638 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2639 | transaction.test_mode = TEST_MODE_SYNC_CACHE_START | |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 2640 | TEST_MODE_SYNC_CACHE_READ | |
| 2641 | TEST_MODE_SYNC_CACHE_WRITE; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2642 | AddMockTransaction(&transaction); |
| 2643 | |
| 2644 | // Write to the cache (70-79). |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2645 | transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2646 | transaction.data = "rg: 70-79 "; |
| 2647 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2648 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2649 | Verify206Response(headers, 70, 79); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2650 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2651 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2652 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2653 | |
| 2654 | // Make sure we are done with the previous transaction. |
| 2655 | MessageLoop::current()->RunAllPending(); |
| 2656 | |
| 2657 | // Write and read from the cache (60-79). |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2658 | transaction.request_headers = "Range: bytes = -20\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2659 | transaction.data = "rg: 60-69 rg: 70-79 "; |
| 2660 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2661 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2662 | Verify206Response(headers, 60, 79); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2663 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2664 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2665 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2666 | |
| 2667 | RemoveMockTransaction(&transaction); |
| 2668 | } |
| 2669 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2670 | // Tests that receiving Not Modified when asking for an open range doesn't mess |
| 2671 | // up things. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2672 | TEST(HttpCache, UnknownRangeGET_304) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2673 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2674 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2675 | std::string headers; |
| 2676 | |
| 2677 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 2678 | AddMockTransaction(&transaction); |
| 2679 | |
| 2680 | RangeTransactionServer handler; |
| 2681 | handler.set_not_modified(true); |
| 2682 | |
| 2683 | // Ask for the end of the file, without knowing the length. |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2684 | transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2685 | transaction.data = "rg: 70-79 "; |
| 2686 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2687 | |
| 2688 | // We just bypass the cache. |
| 2689 | EXPECT_EQ(0U, headers.find("HTTP/1.1 304 Not Modified\n")); |
| 2690 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2691 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2692 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2693 | |
| 2694 | RunTransactionTest(cache.http_cache(), transaction); |
| 2695 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2696 | |
| 2697 | RemoveMockTransaction(&transaction); |
| 2698 | } |
| 2699 | |
| 2700 | // 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] | 2701 | TEST(HttpCache, GET_Previous206) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2702 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2703 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2704 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2705 | std::string headers; |
| 2706 | |
| 2707 | // Write to the cache (40-49). |
| 2708 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 2709 | &headers); |
| 2710 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2711 | Verify206Response(headers, 40, 49); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2712 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2713 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2714 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2715 | |
| 2716 | // Write and read from the cache (0-79), when not asked for a range. |
| 2717 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2718 | transaction.request_headers = EXTRA_HEADER; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2719 | transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " |
| 2720 | "rg: 50-59 rg: 60-69 rg: 70-79 "; |
| 2721 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2722 | |
| 2723 | EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 2724 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2725 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2726 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2727 | |
| 2728 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 2729 | } |
| 2730 | |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 2731 | // Tests that we can handle non-range requests when we have cached the first |
| 2732 | // part of the object and server replies with 304 (Not Modified). |
| 2733 | TEST(HttpCache, GET_Previous206_NotModified) { |
| 2734 | MockHttpCache cache; |
| 2735 | cache.http_cache()->set_enable_range_support(true); |
| 2736 | |
| 2737 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2738 | transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 2739 | transaction.data = "rg: 00-09 "; |
| 2740 | AddMockTransaction(&transaction); |
| 2741 | std::string headers; |
| 2742 | |
| 2743 | // Write to the cache (0-9). |
| 2744 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2745 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2746 | Verify206Response(headers, 0, 9); |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 2747 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2748 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2749 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2750 | |
| 2751 | // Read from the cache (0-9), write and read from cache (10 - 79), |
| 2752 | MockTransaction transaction2(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2753 | transaction2.request_headers = "Foo: bar\r\n" EXTRA_HEADER; |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 2754 | transaction2.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " |
| 2755 | "rg: 50-59 rg: 60-69 rg: 70-79 "; |
| 2756 | RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
| 2757 | |
| 2758 | EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); |
| 2759 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2760 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2761 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2762 | |
| 2763 | RemoveMockTransaction(&transaction); |
| 2764 | } |
| 2765 | |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 2766 | // Tests that we can handle a regular request to a sparse entry, that results in |
| 2767 | // new content provided by the server (206). |
| 2768 | TEST(HttpCache, GET_Previous206_NewContent) { |
| 2769 | MockHttpCache cache; |
| 2770 | cache.http_cache()->set_enable_range_support(true); |
| 2771 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 2772 | std::string headers; |
| 2773 | |
| 2774 | // Write to the cache (0-9). |
| 2775 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 2776 | transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; |
| 2777 | transaction.data = "rg: 00-09 "; |
| 2778 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2779 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2780 | Verify206Response(headers, 0, 9); |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 2781 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2782 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2783 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2784 | |
| 2785 | // Now we'll issue a request without any range that should result first in a |
| 2786 | // 206 (when revalidating), and then in a weird standard answer: the test |
| 2787 | // server will not modify the response so we'll get the default range... a |
| 2788 | // real server will answer with 200. |
| 2789 | MockTransaction transaction2(kRangeGET_TransactionOK); |
| 2790 | transaction2.request_headers = EXTRA_HEADER; |
| 2791 | transaction2.data = "rg: 40-49 "; |
| 2792 | RangeTransactionServer handler; |
| 2793 | handler.set_modified(true); |
| 2794 | RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
| 2795 | |
| 2796 | EXPECT_EQ(0U, headers.find("HTTP/1.1 206 Partial Content\n")); |
| 2797 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2798 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2799 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2800 | |
| 2801 | // Verify that the previous request deleted the entry. |
| 2802 | RunTransactionTest(cache.http_cache(), transaction); |
| 2803 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2804 | |
| 2805 | RemoveMockTransaction(&transaction); |
| 2806 | } |
| 2807 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2808 | // Tests that we can handle cached 206 responses that are not sparse. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2809 | TEST(HttpCache, GET_Previous206_NotSparse) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2810 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2811 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2812 | |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2813 | // Create a disk cache entry that stores 206 headers while not being sparse. |
| 2814 | disk_cache::Entry* entry; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 2815 | ASSERT_EQ(net::OK, cache.disk_cache()->CreateEntry(kSimpleGET_Transaction.url, |
| 2816 | &entry, NULL)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2817 | |
| 2818 | std::string raw_headers(kRangeGET_TransactionOK.status); |
| 2819 | raw_headers.append("\n"); |
| 2820 | raw_headers.append(kRangeGET_TransactionOK.response_headers); |
| 2821 | raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), |
| 2822 | raw_headers.size()); |
| 2823 | |
| 2824 | net::HttpResponseInfo response; |
| 2825 | response.headers = new net::HttpResponseHeaders(raw_headers); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 2826 | EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, false)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2827 | |
| 2828 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500)); |
| 2829 | int len = static_cast<int>(base::strlcpy(buf->data(), |
| 2830 | kRangeGET_TransactionOK.data, 500)); |
| 2831 | EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true)); |
| 2832 | entry->Close(); |
| 2833 | |
| 2834 | // Now see that we don't use the stored entry. |
| 2835 | std::string headers; |
| 2836 | RunTransactionTestWithResponse(cache.http_cache(), kSimpleGET_Transaction, |
| 2837 | &headers); |
| 2838 | |
| 2839 | // We are expecting a 200. |
| 2840 | std::string expected_headers(kSimpleGET_Transaction.status); |
| 2841 | expected_headers.append("\n"); |
| 2842 | expected_headers.append(kSimpleGET_Transaction.response_headers); |
| 2843 | EXPECT_EQ(expected_headers, headers); |
| 2844 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2845 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2846 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2847 | } |
| 2848 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2849 | // Tests that we can handle cached 206 responses that are not sparse. This time |
| 2850 | // we issue a range request and expect to receive a range. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2851 | TEST(HttpCache, RangeGET_Previous206_NotSparse_2) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2852 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2853 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2854 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 2855 | |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2856 | // Create a disk cache entry that stores 206 headers while not being sparse. |
| 2857 | disk_cache::Entry* entry; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 2858 | ASSERT_EQ(net::OK, |
| 2859 | cache.disk_cache()->CreateEntry(kRangeGET_TransactionOK.url, |
| 2860 | &entry, NULL)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2861 | |
| 2862 | std::string raw_headers(kRangeGET_TransactionOK.status); |
| 2863 | raw_headers.append("\n"); |
| 2864 | raw_headers.append(kRangeGET_TransactionOK.response_headers); |
| 2865 | raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), |
| 2866 | raw_headers.size()); |
| 2867 | |
| 2868 | net::HttpResponseInfo response; |
| 2869 | response.headers = new net::HttpResponseHeaders(raw_headers); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 2870 | EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, false)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2871 | |
| 2872 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500)); |
| 2873 | int len = static_cast<int>(base::strlcpy(buf->data(), |
| 2874 | kRangeGET_TransactionOK.data, 500)); |
| 2875 | EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true)); |
| 2876 | entry->Close(); |
| 2877 | |
| 2878 | // Now see that we don't use the stored entry. |
| 2879 | std::string headers; |
| 2880 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 2881 | &headers); |
| 2882 | |
| 2883 | // We are expecting a 206. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2884 | Verify206Response(headers, 40, 49); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2885 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2886 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2887 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2888 | |
| 2889 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 2890 | } |
| 2891 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2892 | // Tests that we can handle range requests with cached 200 responses. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2893 | TEST(HttpCache, RangeGET_Previous200) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2894 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2895 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2896 | |
| 2897 | // Store the whole thing with status 200. |
| 2898 | MockTransaction transaction(kTypicalGET_Transaction); |
| 2899 | transaction.url = kRangeGET_TransactionOK.url; |
| 2900 | transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " |
| 2901 | "rg: 50-59 rg: 60-69 rg: 70-79 "; |
| 2902 | AddMockTransaction(&transaction); |
| 2903 | RunTransactionTest(cache.http_cache(), transaction); |
| 2904 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2905 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2906 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2907 | |
| 2908 | RemoveMockTransaction(&transaction); |
| 2909 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 2910 | |
| 2911 | // Now see that we use the stored entry. |
| 2912 | std::string headers; |
| 2913 | MockTransaction transaction2(kRangeGET_TransactionOK); |
| 2914 | RangeTransactionServer handler; |
| 2915 | handler.set_not_modified(true); |
| 2916 | RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
| 2917 | |
| 2918 | // We are expecting a 206. |
[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 | |
[email protected] | 8f28d63 | 2009-10-01 22:09:21 | [diff] [blame] | 2924 | // The last transaction has finished so make sure the entry is deactivated. |
| 2925 | MessageLoop::current()->RunAllPending(); |
| 2926 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2927 | // Now we should receive a range from the server and drop the stored entry. |
| 2928 | handler.set_not_modified(false); |
| 2929 | transaction2.request_headers = kRangeGET_TransactionOK.request_headers; |
| 2930 | RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2931 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2932 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2933 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 2934 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2935 | |
| 2936 | RunTransactionTest(cache.http_cache(), transaction2); |
| 2937 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2938 | |
| 2939 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 2940 | } |
| 2941 | |
| 2942 | // Tests that we can handle a 200 response when dealing with sparse entries. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2943 | TEST(HttpCache, RangeRequestResultsIn200) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2944 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2945 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2946 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2947 | std::string headers; |
| 2948 | |
| 2949 | // Write to the cache (70-79). |
| 2950 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 2951 | transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2952 | transaction.data = "rg: 70-79 "; |
| 2953 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2954 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2955 | Verify206Response(headers, 70, 79); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 2956 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2957 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2958 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2959 | |
| 2960 | // Now we'll issue a request that results in a plain 200 response, but to |
| 2961 | // the to the same URL that we used to store sparse data, and making sure |
| 2962 | // that we ask for a range. |
| 2963 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 2964 | MockTransaction transaction2(kSimpleGET_Transaction); |
| 2965 | transaction2.url = kRangeGET_TransactionOK.url; |
| 2966 | transaction2.request_headers = kRangeGET_TransactionOK.request_headers; |
| 2967 | AddMockTransaction(&transaction2); |
| 2968 | |
| 2969 | RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
| 2970 | |
| 2971 | std::string expected_headers(kSimpleGET_Transaction.status); |
| 2972 | expected_headers.append("\n"); |
| 2973 | expected_headers.append(kSimpleGET_Transaction.response_headers); |
| 2974 | EXPECT_EQ(expected_headers, headers); |
| 2975 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2976 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2977 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2978 | |
| 2979 | RemoveMockTransaction(&transaction2); |
| 2980 | } |
| 2981 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2982 | // Tests that a range request that falls outside of the size that we know about |
| 2983 | // only deletes the entry if the resource has indeed changed. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2984 | TEST(HttpCache, RangeGET_MoreThanCurrentSize) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2985 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 2986 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2987 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 2988 | std::string headers; |
| 2989 | |
| 2990 | // Write to the cache (40-49). |
| 2991 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 2992 | &headers); |
| 2993 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 2994 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 2995 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2996 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2997 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2998 | |
| 2999 | // A weird request should not delete this entry. Ask for bytes 120-. |
| 3000 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3001 | transaction.request_headers = "Range: bytes = 120-\r\n" EXTRA_HEADER; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3002 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3003 | |
| 3004 | EXPECT_EQ(0U, headers.find("HTTP/1.1 416 ")); |
| 3005 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3006 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3007 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3008 | |
| 3009 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 3010 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 3011 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3012 | |
| 3013 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3014 | } |
| 3015 | |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 3016 | // Tests that we don't delete a sparse entry when we cancel a request. |
| 3017 | TEST(HttpCache, RangeGET_Cancel) { |
| 3018 | MockHttpCache cache; |
| 3019 | cache.http_cache()->set_enable_range_support(true); |
| 3020 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3021 | |
| 3022 | MockHttpRequest request(kRangeGET_TransactionOK); |
| 3023 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3024 | Context* c = new Context(); |
| 3025 | int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 3026 | EXPECT_EQ(net::OK, rv); |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 3027 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3028 | rv = c->trans->Start(&request, &c->callback, NULL); |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 3029 | if (rv == net::ERR_IO_PENDING) |
| 3030 | rv = c->callback.WaitForResult(); |
| 3031 | |
| 3032 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3033 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3034 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3035 | |
| 3036 | // Make sure that the entry has some data stored. |
| 3037 | scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10); |
| 3038 | rv = c->trans->Read(buf, buf->size(), &c->callback); |
| 3039 | if (rv == net::ERR_IO_PENDING) |
| 3040 | rv = c->callback.WaitForResult(); |
| 3041 | EXPECT_EQ(buf->size(), rv); |
| 3042 | |
| 3043 | // Destroy the transaction. |
| 3044 | delete c; |
| 3045 | |
| 3046 | // Verify that the entry has not been deleted. |
| 3047 | disk_cache::Entry* entry; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 3048 | ASSERT_EQ(net::OK, cache.disk_cache()->OpenEntry(kRangeGET_TransactionOK.url, |
| 3049 | &entry, NULL)); |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 3050 | entry->Close(); |
| 3051 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3052 | } |
| 3053 | |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 3054 | // Tests that we don't delete a sparse entry when we start a new request after |
| 3055 | // cancelling the previous one. |
| 3056 | TEST(HttpCache, RangeGET_Cancel2) { |
| 3057 | MockHttpCache cache; |
| 3058 | cache.http_cache()->set_enable_range_support(true); |
| 3059 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3060 | |
| 3061 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 3062 | MockHttpRequest request(kRangeGET_TransactionOK); |
| 3063 | |
| 3064 | Context* c = new Context(); |
| 3065 | int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 3066 | EXPECT_EQ(net::OK, rv); |
| 3067 | |
| 3068 | rv = c->trans->Start(&request, &c->callback, NULL); |
| 3069 | if (rv == net::ERR_IO_PENDING) |
| 3070 | rv = c->callback.WaitForResult(); |
| 3071 | |
| 3072 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3073 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3074 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3075 | |
| 3076 | // Make sure that we revalidate the entry and read from the cache (a single |
| 3077 | // read will return while waiting for the network). |
| 3078 | scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(5); |
| 3079 | rv = c->trans->Read(buf, buf->size(), &c->callback); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3080 | EXPECT_EQ(5, c->callback.GetResult(rv)); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 3081 | rv = c->trans->Read(buf, buf->size(), &c->callback); |
| 3082 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 3083 | |
| 3084 | // Destroy the transaction before completing the read. |
| 3085 | delete c; |
| 3086 | |
| 3087 | // We have the read and the delete (OnProcessPendingQueue) waiting on the |
| 3088 | // message loop. This means that a new transaction will just reuse the same |
| 3089 | // active entry (no open or create). |
| 3090 | |
| 3091 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 3092 | |
| 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 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3097 | } |
| 3098 | |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 3099 | // A slight variation of the previous test, this time we cancel two requests in |
| 3100 | // a row, making sure that the second is waiting for the entry to be ready. |
| 3101 | TEST(HttpCache, RangeGET_Cancel3) { |
| 3102 | MockHttpCache cache; |
| 3103 | cache.http_cache()->set_enable_range_support(true); |
| 3104 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3105 | |
| 3106 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 3107 | MockHttpRequest request(kRangeGET_TransactionOK); |
| 3108 | |
| 3109 | Context* c = new Context(); |
| 3110 | int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 3111 | EXPECT_EQ(net::OK, rv); |
| 3112 | |
| 3113 | rv = c->trans->Start(&request, &c->callback, NULL); |
| 3114 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 3115 | rv = c->callback.WaitForResult(); |
| 3116 | |
| 3117 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3118 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3119 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3120 | |
| 3121 | // Make sure that we revalidate the entry and read from the cache (a single |
| 3122 | // read will return while waiting for the network). |
| 3123 | scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(5); |
| 3124 | rv = c->trans->Read(buf, buf->size(), &c->callback); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3125 | EXPECT_EQ(5, c->callback.GetResult(rv)); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 3126 | rv = c->trans->Read(buf, buf->size(), &c->callback); |
| 3127 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 3128 | |
| 3129 | // Destroy the transaction before completing the read. |
| 3130 | delete c; |
| 3131 | |
| 3132 | // We have the read and the delete (OnProcessPendingQueue) waiting on the |
| 3133 | // message loop. This means that a new transaction will just reuse the same |
| 3134 | // active entry (no open or create). |
| 3135 | |
| 3136 | c = new Context(); |
| 3137 | rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 3138 | EXPECT_EQ(net::OK, rv); |
| 3139 | |
| 3140 | rv = c->trans->Start(&request, &c->callback, NULL); |
| 3141 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 3142 | |
| 3143 | MockDiskEntry::IgnoreCallbacks(true); |
| 3144 | MessageLoop::current()->RunAllPending(); |
| 3145 | MockDiskEntry::IgnoreCallbacks(false); |
| 3146 | |
| 3147 | // The new transaction is waiting for the query range callback. |
| 3148 | delete c; |
| 3149 | |
| 3150 | // And we should not crash when the callback is delivered. |
| 3151 | MessageLoop::current()->RunAllPending(); |
| 3152 | |
| 3153 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3154 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3155 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3156 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3157 | } |
| 3158 | |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3159 | // Tests that an invalid range response results in no cached entry. |
| 3160 | TEST(HttpCache, RangeGET_InvalidResponse1) { |
| 3161 | MockHttpCache cache; |
| 3162 | cache.http_cache()->set_enable_range_support(true); |
| 3163 | std::string headers; |
| 3164 | |
| 3165 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3166 | transaction.handler = NULL; |
| 3167 | transaction.response_headers = "Content-Range: bytes 40-49/45\n" |
| 3168 | "Content-Length: 10\n"; |
| 3169 | AddMockTransaction(&transaction); |
| 3170 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3171 | |
| 3172 | std::string expected(transaction.status); |
| 3173 | expected.append("\n"); |
| 3174 | expected.append(transaction.response_headers); |
| 3175 | EXPECT_EQ(expected, headers); |
| 3176 | |
| 3177 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3178 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3179 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3180 | |
| 3181 | // Verify that we don't have a cached entry. |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 3182 | disk_cache::Entry* entry; |
| 3183 | EXPECT_NE(net::OK, cache.disk_cache()->OpenEntry(kRangeGET_TransactionOK.url, |
| 3184 | &entry, NULL)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3185 | |
| 3186 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3187 | } |
| 3188 | |
| 3189 | // Tests that we reject a range that doesn't match the content-length. |
| 3190 | TEST(HttpCache, RangeGET_InvalidResponse2) { |
| 3191 | MockHttpCache cache; |
| 3192 | cache.http_cache()->set_enable_range_support(true); |
| 3193 | std::string headers; |
| 3194 | |
| 3195 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3196 | transaction.handler = NULL; |
| 3197 | transaction.response_headers = "Content-Range: bytes 40-49/80\n" |
| 3198 | "Content-Length: 20\n"; |
| 3199 | AddMockTransaction(&transaction); |
| 3200 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3201 | |
| 3202 | std::string expected(transaction.status); |
| 3203 | expected.append("\n"); |
| 3204 | expected.append(transaction.response_headers); |
| 3205 | EXPECT_EQ(expected, headers); |
| 3206 | |
| 3207 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3208 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3209 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3210 | |
| 3211 | // Verify that we don't have a cached entry. |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 3212 | disk_cache::Entry* entry; |
| 3213 | EXPECT_NE(net::OK, cache.disk_cache()->OpenEntry(kRangeGET_TransactionOK.url, |
| 3214 | &entry, NULL)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3215 | |
| 3216 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3217 | } |
| 3218 | |
| 3219 | // Tests that if a server tells us conflicting information about a resource we |
| 3220 | // ignore the response. |
| 3221 | TEST(HttpCache, RangeGET_InvalidResponse3) { |
| 3222 | MockHttpCache cache; |
| 3223 | cache.http_cache()->set_enable_range_support(true); |
| 3224 | std::string headers; |
| 3225 | |
| 3226 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3227 | transaction.handler = NULL; |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3228 | transaction.request_headers = "Range: bytes = 50-59\r\n" EXTRA_HEADER; |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3229 | std::string response_headers(transaction.response_headers); |
| 3230 | response_headers.append("Content-Range: bytes 50-59/160\n"); |
| 3231 | transaction.response_headers = response_headers.c_str(); |
| 3232 | AddMockTransaction(&transaction); |
| 3233 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3234 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 3235 | Verify206Response(headers, 50, 59); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3236 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3237 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3238 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3239 | |
| 3240 | RemoveMockTransaction(&transaction); |
| 3241 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3242 | |
| 3243 | // This transaction will report a resource size of 80 bytes, and we think it's |
| 3244 | // 160 so we should ignore the response. |
| 3245 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 3246 | &headers); |
| 3247 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 3248 | Verify206Response(headers, 40, 49); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3249 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3250 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3251 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3252 | |
| 3253 | // Verify that we cached the first response but not the second one. |
| 3254 | disk_cache::Entry* en; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 3255 | ASSERT_EQ(net::OK, cache.disk_cache()->OpenEntry(kRangeGET_TransactionOK.url, |
| 3256 | &en, NULL)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3257 | int64 cached_start = 0; |
| 3258 | EXPECT_EQ(10, en->GetAvailableRange(40, 20, &cached_start)); |
| 3259 | EXPECT_EQ(50, cached_start); |
| 3260 | en->Close(); |
| 3261 | |
| 3262 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3263 | } |
| 3264 | |
| 3265 | // Tests that we handle large range values properly. |
| 3266 | TEST(HttpCache, RangeGET_LargeValues) { |
| 3267 | // We need a real sparse cache for this test. |
| 3268 | disk_cache::Backend* disk_cache = |
| 3269 | disk_cache::CreateInMemoryCacheBackend(1024 * 1024); |
| 3270 | MockHttpCache cache(disk_cache); |
| 3271 | cache.http_cache()->set_enable_range_support(true); |
| 3272 | std::string headers; |
| 3273 | |
| 3274 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3275 | transaction.handler = NULL; |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3276 | transaction.request_headers = "Range: bytes = 4294967288-4294967297\r\n" |
| 3277 | EXTRA_HEADER; |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 3278 | transaction.response_headers = |
| 3279 | "Content-Range: bytes 4294967288-4294967297/4294967299\n" |
| 3280 | "Content-Length: 10\n"; |
| 3281 | AddMockTransaction(&transaction); |
| 3282 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3283 | |
| 3284 | std::string expected(transaction.status); |
| 3285 | expected.append("\n"); |
| 3286 | expected.append(transaction.response_headers); |
| 3287 | EXPECT_EQ(expected, headers); |
| 3288 | |
| 3289 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3290 | |
| 3291 | // Verify that we have a cached entry. |
| 3292 | disk_cache::Entry* en; |
| 3293 | ASSERT_TRUE(cache.disk_cache()->OpenEntry(kRangeGET_TransactionOK.url, &en)); |
| 3294 | en->Close(); |
| 3295 | |
| 3296 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3297 | } |
| 3298 | |
[email protected] | 93e7844 | 2009-10-27 04:46:32 | [diff] [blame] | 3299 | // Tests that we don't crash with a range request if the disk cache was not |
| 3300 | // initialized properly. |
| 3301 | TEST(HttpCache, RangeGET_NoDiskCache) { |
| 3302 | MockHttpCache cache(NULL); |
| 3303 | cache.http_cache()->set_enable_range_support(true); |
| 3304 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3305 | |
| 3306 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 3307 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3308 | |
| 3309 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3310 | } |
| 3311 | |
[email protected] | 793618a | 2009-11-03 23:08:12 | [diff] [blame] | 3312 | // Tests that we handle byte range requests that skip the cache. |
| 3313 | TEST(HttpCache, RangeHEAD) { |
| 3314 | MockHttpCache cache; |
| 3315 | cache.http_cache()->set_enable_range_support(true); |
| 3316 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3317 | |
| 3318 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3319 | transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; |
| 3320 | transaction.method = "HEAD"; |
| 3321 | transaction.data = "rg: 70-79 "; |
| 3322 | |
| 3323 | std::string headers; |
| 3324 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3325 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 3326 | Verify206Response(headers, 70, 79); |
[email protected] | 793618a | 2009-11-03 23:08:12 | [diff] [blame] | 3327 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3328 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3329 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3330 | |
| 3331 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3332 | } |
| 3333 | |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 3334 | // Tests that we don't crash when after reading from the cache we issue a |
| 3335 | // request for the next range and the server gives us a 200 synchronously. |
| 3336 | TEST(HttpCache, RangeGET_FastFlakyServer) { |
| 3337 | MockHttpCache cache; |
| 3338 | cache.http_cache()->set_enable_range_support(true); |
| 3339 | |
| 3340 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3341 | transaction.request_headers = "Range: bytes = 40-\r\n" EXTRA_HEADER; |
| 3342 | transaction.test_mode = TEST_MODE_SYNC_NET_START; |
| 3343 | AddMockTransaction(&transaction); |
| 3344 | |
| 3345 | // Write to the cache. |
| 3346 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 3347 | |
| 3348 | // And now read from the cache and the network. |
| 3349 | RangeTransactionServer handler; |
| 3350 | handler.set_bad_200(true); |
| 3351 | RunTransactionTest(cache.http_cache(), transaction); |
| 3352 | |
| 3353 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3354 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3355 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3356 | |
| 3357 | RemoveMockTransaction(&transaction); |
| 3358 | } |
| 3359 | |
[email protected] | c14117b9 | 2010-01-21 19:22:57 | [diff] [blame] | 3360 | // Tests that when the server gives us less data than expected, we don't keep |
| 3361 | // asking for more data. |
| 3362 | TEST(HttpCache, RangeGET_FastFlakyServer2) { |
| 3363 | MockHttpCache cache; |
| 3364 | cache.http_cache()->set_enable_range_support(true); |
| 3365 | |
| 3366 | // First, check with an empty cache (WRITE mode). |
| 3367 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3368 | transaction.request_headers = "Range: bytes = 40-49\r\n" EXTRA_HEADER; |
| 3369 | transaction.data = "rg: 40-"; // Less than expected. |
| 3370 | transaction.handler = NULL; |
| 3371 | std::string headers(transaction.response_headers); |
| 3372 | headers.append("Content-Range: bytes 40-49/80\n"); |
| 3373 | transaction.response_headers = headers.c_str(); |
| 3374 | |
| 3375 | AddMockTransaction(&transaction); |
| 3376 | |
| 3377 | // Write to the cache. |
| 3378 | RunTransactionTest(cache.http_cache(), transaction); |
| 3379 | |
| 3380 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3381 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3382 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3383 | |
| 3384 | // Now verify that even in READ_WRITE mode, we forward the bad response to |
| 3385 | // the caller. |
| 3386 | transaction.request_headers = "Range: bytes = 60-69\r\n" EXTRA_HEADER; |
| 3387 | transaction.data = "rg: 60-"; // Less than expected. |
| 3388 | headers = kRangeGET_TransactionOK.response_headers; |
| 3389 | headers.append("Content-Range: bytes 60-69/80\n"); |
| 3390 | transaction.response_headers = headers.c_str(); |
| 3391 | |
| 3392 | RunTransactionTest(cache.http_cache(), transaction); |
| 3393 | |
| 3394 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3395 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3396 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3397 | |
| 3398 | RemoveMockTransaction(&transaction); |
| 3399 | } |
| 3400 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3401 | #ifdef NDEBUG |
| 3402 | // This test hits a NOTREACHED so it is a release mode only test. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3403 | TEST(HttpCache, RangeGET_OK_LoadOnlyFromCache) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3404 | MockHttpCache cache; |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3405 | cache.http_cache()->set_enable_range_support(true); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3406 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3407 | |
| 3408 | // Write to the cache (40-49). |
| 3409 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 3410 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3411 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3412 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3413 | |
| 3414 | // Force this transaction to read from the cache. |
| 3415 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3416 | transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 3417 | |
| 3418 | MockHttpRequest request(transaction); |
| 3419 | TestCompletionCallback callback; |
| 3420 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3421 | scoped_ptr<net::HttpTransaction> trans; |
| 3422 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 3423 | EXPECT_EQ(net::OK, rv); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3424 | ASSERT_TRUE(trans.get()); |
| 3425 | |
[email protected] | 6330b6c | 2009-09-24 04:07:37 | [diff] [blame] | 3426 | rv = trans->Start(&request, &callback, NULL); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3427 | if (rv == net::ERR_IO_PENDING) |
| 3428 | rv = callback.WaitForResult(); |
| 3429 | ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
| 3430 | |
| 3431 | trans.reset(); |
| 3432 | |
| 3433 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3434 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3435 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3436 | |
| 3437 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3438 | } |
| 3439 | #endif |
| 3440 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3441 | // Tests the handling of the "truncation" flag. |
| 3442 | TEST(HttpCache, WriteResponseInfo_Truncated) { |
| 3443 | MockHttpCache cache; |
| 3444 | disk_cache::Entry* entry; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 3445 | ASSERT_EQ(net::OK, cache.disk_cache()->CreateEntry("http://www.google.com", |
| 3446 | &entry, NULL)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3447 | |
| 3448 | std::string headers("HTTP/1.1 200 OK"); |
| 3449 | headers = net::HttpUtil::AssembleRawHeaders(headers.data(), headers.size()); |
| 3450 | net::HttpResponseInfo response; |
| 3451 | response.headers = new net::HttpResponseHeaders(headers); |
| 3452 | |
| 3453 | // Set the last argument for this to be an incomplete request. |
| 3454 | EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, true)); |
| 3455 | bool truncated = false; |
| 3456 | EXPECT_TRUE(net::HttpCache::ReadResponseInfo(entry, &response, &truncated)); |
| 3457 | EXPECT_TRUE(truncated); |
| 3458 | |
| 3459 | // And now test the opposite case. |
| 3460 | EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, false)); |
| 3461 | truncated = true; |
| 3462 | EXPECT_TRUE(net::HttpCache::ReadResponseInfo(entry, &response, &truncated)); |
| 3463 | EXPECT_FALSE(truncated); |
| 3464 | entry->Close(); |
| 3465 | } |
| 3466 | |
| 3467 | // Tests that we delete an entry when the request is cancelled before starting |
| 3468 | // to read from the network. |
| 3469 | TEST(HttpCache, DoomOnDestruction) { |
| 3470 | MockHttpCache cache; |
| 3471 | cache.http_cache()->set_enable_range_support(true); |
| 3472 | |
| 3473 | MockHttpRequest request(kSimpleGET_Transaction); |
| 3474 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3475 | Context* c = new Context(); |
| 3476 | int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 3477 | EXPECT_EQ(net::OK, rv); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3478 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3479 | rv = c->trans->Start(&request, &c->callback, NULL); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3480 | if (rv == net::ERR_IO_PENDING) |
| 3481 | c->result = c->callback.WaitForResult(); |
| 3482 | |
| 3483 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3484 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3485 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3486 | |
| 3487 | // Destroy the transaction. We only have the headers so we should delete this |
| 3488 | // entry. |
| 3489 | delete c; |
| 3490 | |
| 3491 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 3492 | |
| 3493 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3494 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3495 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3496 | } |
| 3497 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3498 | // Tests that we delete an entry when the request is cancelled if the response |
| 3499 | // does not have content-length and strong validators. |
| 3500 | TEST(HttpCache, DoomOnDestruction2) { |
| 3501 | MockHttpCache cache; |
| 3502 | cache.http_cache()->set_enable_range_support(true); |
| 3503 | |
| 3504 | MockHttpRequest request(kSimpleGET_Transaction); |
| 3505 | |
| 3506 | Context* c = new Context(); |
| 3507 | int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 3508 | EXPECT_EQ(net::OK, rv); |
| 3509 | |
| 3510 | rv = c->trans->Start(&request, &c->callback, NULL); |
| 3511 | if (rv == net::ERR_IO_PENDING) |
| 3512 | rv = c->callback.WaitForResult(); |
| 3513 | |
| 3514 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3515 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3516 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3517 | |
| 3518 | // Make sure that the entry has some data stored. |
| 3519 | scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10); |
| 3520 | rv = c->trans->Read(buf, buf->size(), &c->callback); |
| 3521 | if (rv == net::ERR_IO_PENDING) |
| 3522 | rv = c->callback.WaitForResult(); |
| 3523 | EXPECT_EQ(buf->size(), rv); |
| 3524 | |
| 3525 | // Destroy the transaction. |
| 3526 | delete c; |
| 3527 | |
| 3528 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 3529 | |
| 3530 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3531 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3532 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3533 | } |
| 3534 | |
| 3535 | // Tests that we delete an entry when the request is cancelled if the response |
| 3536 | // has an "Accept-Ranges: none" header. |
| 3537 | TEST(HttpCache, DoomOnDestruction3) { |
| 3538 | MockHttpCache cache; |
| 3539 | cache.http_cache()->set_enable_range_support(true); |
| 3540 | |
| 3541 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3542 | transaction.response_headers = |
| 3543 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 3544 | "Content-Length: 22\n" |
| 3545 | "Accept-Ranges: none\n" |
| 3546 | "Etag: foopy\n"; |
| 3547 | AddMockTransaction(&transaction); |
| 3548 | MockHttpRequest request(transaction); |
| 3549 | |
| 3550 | Context* c = new Context(); |
| 3551 | int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 3552 | EXPECT_EQ(net::OK, rv); |
| 3553 | |
| 3554 | rv = c->trans->Start(&request, &c->callback, NULL); |
| 3555 | if (rv == net::ERR_IO_PENDING) |
| 3556 | rv = c->callback.WaitForResult(); |
| 3557 | |
| 3558 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3559 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3560 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3561 | |
| 3562 | // Make sure that the entry has some data stored. |
| 3563 | scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10); |
| 3564 | rv = c->trans->Read(buf, buf->size(), &c->callback); |
| 3565 | if (rv == net::ERR_IO_PENDING) |
| 3566 | rv = c->callback.WaitForResult(); |
| 3567 | EXPECT_EQ(buf->size(), rv); |
| 3568 | |
| 3569 | // Destroy the transaction. |
| 3570 | delete c; |
| 3571 | |
| 3572 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 3573 | |
| 3574 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3575 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3576 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3577 | |
| 3578 | RemoveMockTransaction(&transaction); |
| 3579 | } |
| 3580 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3581 | // Tests that we mark an entry as incomplete when the request is cancelled. |
| 3582 | TEST(HttpCache, Set_Truncated_Flag) { |
| 3583 | MockHttpCache cache; |
| 3584 | cache.http_cache()->set_enable_range_support(true); |
| 3585 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3586 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3587 | transaction.response_headers = |
| 3588 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 3589 | "Content-Length: 22\n" |
| 3590 | "Etag: foopy\n"; |
| 3591 | AddMockTransaction(&transaction); |
| 3592 | MockHttpRequest request(transaction); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3593 | |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 3594 | scoped_ptr<Context> c(new Context()); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3595 | int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 3596 | EXPECT_EQ(net::OK, rv); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3597 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3598 | rv = c->trans->Start(&request, &c->callback, NULL); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3599 | if (rv == net::ERR_IO_PENDING) |
| 3600 | rv = c->callback.WaitForResult(); |
| 3601 | |
| 3602 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3603 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3604 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3605 | |
| 3606 | // Make sure that the entry has some data stored. |
| 3607 | scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10); |
| 3608 | rv = c->trans->Read(buf, buf->size(), &c->callback); |
| 3609 | if (rv == net::ERR_IO_PENDING) |
| 3610 | rv = c->callback.WaitForResult(); |
| 3611 | EXPECT_EQ(buf->size(), rv); |
| 3612 | |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 3613 | // We want to cancel the request when the transaction is busy. |
| 3614 | rv = c->trans->Read(buf, buf->size(), &c->callback); |
| 3615 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 3616 | EXPECT_FALSE(c->callback.have_result()); |
| 3617 | |
| 3618 | g_test_mode = TEST_MODE_SYNC_ALL; |
| 3619 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3620 | // Destroy the transaction. |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 3621 | c->trans.reset(); |
| 3622 | g_test_mode = 0; |
| 3623 | |
| 3624 | // Make sure that we don't invoke the callback. We may have an issue if the |
| 3625 | // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we |
| 3626 | // could end up with the transaction being deleted twice if we send any |
| 3627 | // notification from the transaction destructor (see http://crbug.com/31723). |
| 3628 | EXPECT_FALSE(c->callback.have_result()); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3629 | |
| 3630 | // Verify that the entry is marked as incomplete. |
| 3631 | disk_cache::Entry* entry; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 3632 | ASSERT_EQ(net::OK, cache.disk_cache()->OpenEntry(kSimpleGET_Transaction.url, |
| 3633 | &entry, NULL)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3634 | net::HttpResponseInfo response; |
| 3635 | bool truncated = false; |
| 3636 | EXPECT_TRUE(net::HttpCache::ReadResponseInfo(entry, &response, &truncated)); |
| 3637 | EXPECT_TRUE(truncated); |
| 3638 | entry->Close(); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3639 | |
| 3640 | RemoveMockTransaction(&transaction); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3641 | } |
| 3642 | |
| 3643 | // Tests that we can continue with a request that was interrupted. |
| 3644 | TEST(HttpCache, GET_IncompleteResource) { |
| 3645 | MockHttpCache cache; |
| 3646 | cache.http_cache()->set_enable_range_support(true); |
| 3647 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3648 | |
| 3649 | // Create a disk cache entry that stores an incomplete resource. |
| 3650 | disk_cache::Entry* entry; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 3651 | ASSERT_EQ(net::OK, |
| 3652 | cache.disk_cache()->CreateEntry(kRangeGET_TransactionOK.url, &entry, |
| 3653 | NULL)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3654 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3655 | std::string raw_headers("HTTP/1.1 200 OK\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3656 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3657 | "ETag: \"foo\"\n" |
| 3658 | "Accept-Ranges: bytes\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3659 | "Content-Length: 80\n"); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3660 | raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), |
| 3661 | raw_headers.size()); |
| 3662 | |
| 3663 | net::HttpResponseInfo response; |
| 3664 | response.headers = new net::HttpResponseHeaders(raw_headers); |
| 3665 | // Set the last argument for this to be an incomplete request. |
| 3666 | EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, true)); |
| 3667 | |
| 3668 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100)); |
| 3669 | int len = static_cast<int>(base::strlcpy(buf->data(), |
| 3670 | "rg: 00-09 rg: 10-19 ", 100)); |
| 3671 | EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true)); |
| 3672 | |
| 3673 | // Now make a regular request. |
| 3674 | std::string headers; |
| 3675 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3676 | transaction.request_headers = EXTRA_HEADER; |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3677 | transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " |
| 3678 | "rg: 50-59 rg: 60-69 rg: 70-79 "; |
| 3679 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3680 | |
| 3681 | // We update the headers with the ones received while revalidating. |
| 3682 | std::string expected_headers( |
| 3683 | "HTTP/1.1 200 OK\n" |
| 3684 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 3685 | "Accept-Ranges: bytes\n" |
| 3686 | "ETag: \"foo\"\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3687 | "Content-Length: 80\n"); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 3688 | |
| 3689 | EXPECT_EQ(expected_headers, headers); |
| 3690 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3691 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3692 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3693 | |
| 3694 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3695 | |
| 3696 | // Verify that the disk entry was updated. |
| 3697 | EXPECT_EQ(80, entry->GetDataSize(1)); |
| 3698 | bool truncated = true; |
| 3699 | EXPECT_TRUE(net::HttpCache::ReadResponseInfo(entry, &response, &truncated)); |
| 3700 | EXPECT_FALSE(truncated); |
| 3701 | entry->Close(); |
| 3702 | } |
| 3703 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3704 | // Tests that we delete truncated entries if the server changes its mind midway. |
| 3705 | TEST(HttpCache, GET_IncompleteResource2) { |
| 3706 | MockHttpCache cache; |
| 3707 | cache.http_cache()->set_enable_range_support(true); |
| 3708 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3709 | |
| 3710 | // Create a disk cache entry that stores an incomplete resource. |
| 3711 | disk_cache::Entry* entry; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 3712 | ASSERT_EQ(net::OK, |
| 3713 | cache.disk_cache()->CreateEntry(kRangeGET_TransactionOK.url, &entry, |
| 3714 | NULL)); |
| 3715 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3716 | |
| 3717 | // Content-length will be intentionally bad. |
| 3718 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 3719 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 3720 | "ETag: \"foo\"\n" |
| 3721 | "Accept-Ranges: bytes\n" |
| 3722 | "Content-Length: 50\n"); |
| 3723 | raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), |
| 3724 | raw_headers.size()); |
| 3725 | |
| 3726 | net::HttpResponseInfo response; |
| 3727 | response.headers = new net::HttpResponseHeaders(raw_headers); |
| 3728 | // Set the last argument for this to be an incomplete request. |
| 3729 | EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, true)); |
| 3730 | |
| 3731 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100)); |
| 3732 | int len = static_cast<int>(base::strlcpy(buf->data(), |
| 3733 | "rg: 00-09 rg: 10-19 ", 100)); |
| 3734 | EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true)); |
| 3735 | entry->Close(); |
| 3736 | |
| 3737 | // Now make a regular request. |
| 3738 | std::string headers; |
| 3739 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3740 | transaction.request_headers = EXTRA_HEADER; |
| 3741 | transaction.data = "rg: 00-09 rg: 10-19 "; |
| 3742 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3743 | |
| 3744 | // We update the headers with the ones received while revalidating. |
| 3745 | std::string expected_headers( |
| 3746 | "HTTP/1.1 200 OK\n" |
| 3747 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 3748 | "Accept-Ranges: bytes\n" |
| 3749 | "ETag: \"foo\"\n" |
| 3750 | "Content-Length: 50\n"); |
| 3751 | |
| 3752 | EXPECT_EQ(expected_headers, headers); |
| 3753 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3754 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3755 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3756 | |
| 3757 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3758 | |
| 3759 | // Verify that the disk entry was deleted. |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 3760 | ASSERT_NE(net::OK, cache.disk_cache()->OpenEntry(kRangeGET_TransactionOK.url, |
| 3761 | &entry, NULL)); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3762 | } |
| 3763 | |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 3764 | // Tests that when we cancel a request that was interrupted, we mark it again |
| 3765 | // as truncated. |
| 3766 | TEST(HttpCache, GET_CancelIncompleteResource) { |
| 3767 | MockHttpCache cache; |
| 3768 | cache.http_cache()->set_enable_range_support(true); |
| 3769 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3770 | |
| 3771 | // Create a disk cache entry that stores an incomplete resource. |
| 3772 | disk_cache::Entry* entry; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 3773 | ASSERT_EQ(net::OK, |
| 3774 | cache.disk_cache()->CreateEntry(kRangeGET_TransactionOK.url, &entry, |
| 3775 | NULL)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 3776 | |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 3777 | std::string raw_headers("HTTP/1.1 200 OK\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3778 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 3779 | "ETag: \"foo\"\n" |
| 3780 | "Accept-Ranges: bytes\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 3781 | "Content-Length: 80\n"); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 3782 | raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), |
| 3783 | raw_headers.size()); |
| 3784 | |
| 3785 | net::HttpResponseInfo response; |
| 3786 | response.headers = new net::HttpResponseHeaders(raw_headers); |
| 3787 | |
| 3788 | // Set the last argument for this to be an incomplete request. |
| 3789 | EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, true)); |
| 3790 | |
| 3791 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(100)); |
| 3792 | int len = static_cast<int>(base::strlcpy(buf->data(), "rg: 00-09 rg: 10-19 ", |
| 3793 | buf->size())); |
| 3794 | EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true)); |
| 3795 | |
| 3796 | // Now make a regular request. |
| 3797 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3798 | transaction.request_headers = EXTRA_HEADER; |
| 3799 | |
| 3800 | MockHttpRequest request(transaction); |
| 3801 | Context* c = new Context(); |
| 3802 | EXPECT_EQ(net::OK, cache.http_cache()->CreateTransaction(&c->trans)); |
| 3803 | |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3804 | int rv = c->trans->Start(&request, &c->callback, NULL); |
| 3805 | EXPECT_EQ(net::OK, c->callback.GetResult(rv)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 3806 | |
| 3807 | // Read 20 bytes from the cache, and 10 from the net. |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3808 | rv = c->trans->Read(buf, len, &c->callback); |
| 3809 | EXPECT_EQ(len, c->callback.GetResult(rv)); |
| 3810 | rv = c->trans->Read(buf, 10, &c->callback); |
| 3811 | EXPECT_EQ(10, c->callback.GetResult(rv)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 3812 | |
| 3813 | // At this point, we are already reading so canceling the request should leave |
| 3814 | // a truncated one. |
| 3815 | delete c; |
| 3816 | |
| 3817 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3818 | |
| 3819 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3820 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3821 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3822 | |
| 3823 | // Verify that the disk entry was updated: now we have 30 bytes. |
| 3824 | EXPECT_EQ(30, entry->GetDataSize(1)); |
| 3825 | bool truncated = false; |
| 3826 | EXPECT_TRUE(net::HttpCache::ReadResponseInfo(entry, &response, &truncated)); |
| 3827 | EXPECT_TRUE(truncated); |
| 3828 | entry->Close(); |
| 3829 | } |
| 3830 | |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 3831 | // Tests that we can handle range requests when we have a truncated entry. |
| 3832 | TEST(HttpCache, RangeGET_IncompleteResource) { |
| 3833 | MockHttpCache cache; |
| 3834 | cache.http_cache()->set_enable_range_support(true); |
| 3835 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3836 | |
| 3837 | // Create a disk cache entry that stores an incomplete resource. |
| 3838 | disk_cache::Entry* entry; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 3839 | ASSERT_EQ(net::OK, |
| 3840 | cache.disk_cache()->CreateEntry(kRangeGET_TransactionOK.url, &entry, |
| 3841 | NULL)); |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 3842 | |
| 3843 | // Content-length will be intentionally bogus. |
| 3844 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 3845 | "Last-Modified: something\n" |
| 3846 | "ETag: \"foo\"\n" |
| 3847 | "Accept-Ranges: bytes\n" |
| 3848 | "Content-Length: 10\n"); |
| 3849 | raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), |
| 3850 | raw_headers.size()); |
| 3851 | |
| 3852 | net::HttpResponseInfo response; |
| 3853 | response.headers = new net::HttpResponseHeaders(raw_headers); |
| 3854 | // Set the last argument for this to be an incomplete request. |
| 3855 | EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, true)); |
| 3856 | |
| 3857 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100)); |
| 3858 | int len = static_cast<int>(base::strlcpy(buf->data(), |
| 3859 | "rg: 00-09 rg: 10-19 ", 100)); |
| 3860 | EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true)); |
| 3861 | entry->Close(); |
| 3862 | |
| 3863 | // Now make a range request. |
| 3864 | std::string headers; |
| 3865 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 3866 | &headers); |
| 3867 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 3868 | Verify206Response(headers, 40, 49); |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 3869 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3870 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3871 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3872 | |
| 3873 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3874 | } |
| 3875 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3876 | TEST(HttpCache, SyncRead) { |
| 3877 | MockHttpCache cache; |
| 3878 | |
| 3879 | // This test ensures that a read that completes synchronously does not cause |
| 3880 | // any problems. |
| 3881 | |
| 3882 | ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 3883 | transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START | |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 3884 | TEST_MODE_SYNC_CACHE_READ | |
| 3885 | TEST_MODE_SYNC_CACHE_WRITE); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3886 | |
| 3887 | MockHttpRequest r1(transaction), |
| 3888 | r2(transaction), |
| 3889 | r3(transaction); |
| 3890 | |
| 3891 | TestTransactionConsumer c1(cache.http_cache()), |
| 3892 | c2(cache.http_cache()), |
| 3893 | c3(cache.http_cache()); |
| 3894 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3895 | c1.Start(&r1, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3896 | |
| 3897 | r2.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3898 | c2.Start(&r2, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3899 | |
| 3900 | r3.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3901 | c3.Start(&r3, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3902 | |
| 3903 | MessageLoop::current()->Run(); |
| 3904 | |
| 3905 | EXPECT_TRUE(c1.is_done()); |
| 3906 | EXPECT_TRUE(c2.is_done()); |
| 3907 | EXPECT_TRUE(c3.is_done()); |
| 3908 | |
| 3909 | EXPECT_EQ(net::OK, c1.error()); |
| 3910 | EXPECT_EQ(net::OK, c2.error()); |
| 3911 | EXPECT_EQ(net::OK, c3.error()); |
| 3912 | } |
| 3913 | |
| 3914 | TEST(HttpCache, ValidationResultsIn200) { |
| 3915 | MockHttpCache cache; |
| 3916 | |
| 3917 | // This test ensures that a conditional request, which results in a 200 |
| 3918 | // instead of a 304, properly truncates the existing response data. |
| 3919 | |
| 3920 | // write to the cache |
| 3921 | RunTransactionTest(cache.http_cache(), kETagGET_Transaction); |
| 3922 | |
| 3923 | // force this transaction to validate the cache |
| 3924 | MockTransaction transaction(kETagGET_Transaction); |
| 3925 | transaction.load_flags |= net::LOAD_VALIDATE_CACHE; |
| 3926 | RunTransactionTest(cache.http_cache(), transaction); |
| 3927 | |
| 3928 | // read from the cache |
| 3929 | RunTransactionTest(cache.http_cache(), kETagGET_Transaction); |
| 3930 | } |
| 3931 | |
| 3932 | TEST(HttpCache, CachedRedirect) { |
| 3933 | MockHttpCache cache; |
| 3934 | |
| 3935 | ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); |
| 3936 | kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; |
| 3937 | kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; |
| 3938 | |
| 3939 | MockHttpRequest request(kTestTransaction); |
| 3940 | TestCompletionCallback callback; |
| 3941 | |
| 3942 | // write to the cache |
| 3943 | { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3944 | scoped_ptr<net::HttpTransaction> trans; |
| 3945 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 3946 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 3947 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3948 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3949 | rv = trans->Start(&request, &callback, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3950 | if (rv == net::ERR_IO_PENDING) |
| 3951 | rv = callback.WaitForResult(); |
| 3952 | ASSERT_EQ(net::OK, rv); |
| 3953 | |
| 3954 | const net::HttpResponseInfo* info = trans->GetResponseInfo(); |
| 3955 | ASSERT_TRUE(info); |
| 3956 | |
| 3957 | EXPECT_EQ(info->headers->response_code(), 301); |
| 3958 | |
| 3959 | std::string location; |
| 3960 | info->headers->EnumerateHeader(NULL, "Location", &location); |
| 3961 | EXPECT_EQ(location, "http://www.bar.com/"); |
| 3962 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 3963 | // Destroy transaction when going out of scope. We have not actually |
| 3964 | // read the response body -- want to test that it is still getting cached. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3965 | } |
| 3966 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3967 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3968 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3969 | |
| 3970 | // read from the cache |
| 3971 | { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3972 | scoped_ptr<net::HttpTransaction> trans; |
| 3973 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 3974 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 3975 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3976 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 3977 | rv = trans->Start(&request, &callback, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3978 | if (rv == net::ERR_IO_PENDING) |
| 3979 | rv = callback.WaitForResult(); |
| 3980 | ASSERT_EQ(net::OK, rv); |
| 3981 | |
| 3982 | const net::HttpResponseInfo* info = trans->GetResponseInfo(); |
| 3983 | ASSERT_TRUE(info); |
| 3984 | |
| 3985 | EXPECT_EQ(info->headers->response_code(), 301); |
| 3986 | |
| 3987 | std::string location; |
| 3988 | info->headers->EnumerateHeader(NULL, "Location", &location); |
| 3989 | EXPECT_EQ(location, "http://www.bar.com/"); |
| 3990 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 3991 | // Destroy transaction when going out of scope. We have not actually |
| 3992 | // read the response body -- want to test that it is still getting cached. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3993 | } |
| 3994 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3995 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3996 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3997 | } |
| 3998 | |
| 3999 | TEST(HttpCache, CacheControlNoStore) { |
| 4000 | MockHttpCache cache; |
| 4001 | |
| 4002 | ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 4003 | transaction.response_headers = "cache-control: no-store\n"; |
| 4004 | |
| 4005 | // initial load |
| 4006 | RunTransactionTest(cache.http_cache(), transaction); |
| 4007 | |
| 4008 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4009 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4010 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4011 | |
| 4012 | // try loading again; it should result in a network fetch |
| 4013 | RunTransactionTest(cache.http_cache(), transaction); |
| 4014 | |
| 4015 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4016 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4017 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4018 | |
| 4019 | disk_cache::Entry* entry; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 4020 | EXPECT_NE(net::OK, |
| 4021 | cache.disk_cache()->OpenEntry(transaction.url, &entry, NULL)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4022 | } |
| 4023 | |
| 4024 | TEST(HttpCache, CacheControlNoStore2) { |
| 4025 | // this test is similar to the above test, except that the initial response |
| 4026 | // is cachable, but when it is validated, no-store is received causing the |
| 4027 | // cached document to be deleted. |
| 4028 | MockHttpCache cache; |
| 4029 | |
| 4030 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 4031 | |
| 4032 | // initial load |
| 4033 | RunTransactionTest(cache.http_cache(), transaction); |
| 4034 | |
| 4035 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4036 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4037 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4038 | |
| 4039 | // try loading again; it should result in a network fetch |
| 4040 | transaction.load_flags = net::LOAD_VALIDATE_CACHE; |
| 4041 | transaction.response_headers = "cache-control: no-store\n"; |
| 4042 | RunTransactionTest(cache.http_cache(), transaction); |
| 4043 | |
| 4044 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4045 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4046 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4047 | |
| 4048 | disk_cache::Entry* entry; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 4049 | EXPECT_NE(net::OK, |
| 4050 | cache.disk_cache()->OpenEntry(transaction.url, &entry, NULL)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | TEST(HttpCache, CacheControlNoStore3) { |
| 4054 | // this test is similar to the above test, except that the response is a 304 |
| 4055 | // instead of a 200. this should never happen in practice, but it seems like |
| 4056 | // a good thing to verify that we still destroy the cache entry. |
| 4057 | MockHttpCache cache; |
| 4058 | |
| 4059 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 4060 | |
| 4061 | // initial load |
| 4062 | RunTransactionTest(cache.http_cache(), transaction); |
| 4063 | |
| 4064 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4065 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4066 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4067 | |
| 4068 | // try loading again; it should result in a network fetch |
| 4069 | transaction.load_flags = net::LOAD_VALIDATE_CACHE; |
| 4070 | transaction.response_headers = "cache-control: no-store\n"; |
| 4071 | transaction.status = "HTTP/1.1 304 Not Modified"; |
| 4072 | RunTransactionTest(cache.http_cache(), transaction); |
| 4073 | |
| 4074 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4075 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4076 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4077 | |
| 4078 | disk_cache::Entry* entry; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 4079 | EXPECT_NE(net::OK, |
| 4080 | cache.disk_cache()->OpenEntry(transaction.url, &entry, NULL)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | // Ensure that we don't cache requests served over bad HTTPS. |
| 4084 | TEST(HttpCache, SimpleGET_SSLError) { |
| 4085 | MockHttpCache cache; |
| 4086 | |
| 4087 | MockTransaction transaction = kSimpleGET_Transaction; |
| 4088 | transaction.cert_status = net::CERT_STATUS_REVOKED; |
| 4089 | ScopedMockTransaction scoped_transaction(transaction); |
| 4090 | |
| 4091 | // write to the cache |
| 4092 | RunTransactionTest(cache.http_cache(), transaction); |
| 4093 | |
| 4094 | // Test that it was not cached. |
| 4095 | transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 4096 | |
| 4097 | MockHttpRequest request(transaction); |
| 4098 | TestCompletionCallback callback; |
| 4099 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4100 | scoped_ptr<net::HttpTransaction> trans; |
| 4101 | int rv = cache.http_cache()->CreateTransaction(&trans); |
| 4102 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 4103 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4104 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4105 | rv = trans->Start(&request, &callback, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4106 | if (rv == net::ERR_IO_PENDING) |
| 4107 | rv = callback.WaitForResult(); |
| 4108 | ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4109 | } |
[email protected] | 3e2d38d | 2009-02-14 02:01:18 | [diff] [blame] | 4110 | |
| 4111 | // Ensure that we don't crash by if left-behind transactions. |
| 4112 | TEST(HttpCache, OutlivedTransactions) { |
| 4113 | MockHttpCache* cache = new MockHttpCache; |
| 4114 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4115 | scoped_ptr<net::HttpTransaction> trans; |
| 4116 | int rv = cache->http_cache()->CreateTransaction(&trans); |
| 4117 | EXPECT_EQ(net::OK, rv); |
| 4118 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 4119 | delete cache; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4120 | trans.reset(); |
[email protected] | 3e2d38d | 2009-02-14 02:01:18 | [diff] [blame] | 4121 | } |
[email protected] | 98179700 | 2009-06-05 07:14:15 | [diff] [blame] | 4122 | |
| 4123 | // Test that the disabled mode works. |
| 4124 | TEST(HttpCache, CacheDisabledMode) { |
| 4125 | MockHttpCache cache; |
| 4126 | |
| 4127 | // write to the cache |
| 4128 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 4129 | |
| 4130 | // go into disabled mode |
| 4131 | cache.http_cache()->set_mode(net::HttpCache::DISABLE); |
| 4132 | |
| 4133 | // force this transaction to write to the cache again |
| 4134 | MockTransaction transaction(kSimpleGET_Transaction); |
| 4135 | |
| 4136 | RunTransactionTest(cache.http_cache(), transaction); |
| 4137 | |
| 4138 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4139 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4140 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4141 | } |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 4142 | |
| 4143 | // Other tests check that the response headers of the cached response |
| 4144 | // get updated on 304. Here we specifically check that the |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 4145 | // HttpResponseHeaders::request_time and HttpResponseHeaders::response_time |
| 4146 | // fields also gets updated. |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 4147 | // http://crbug.com/20594. |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 4148 | TEST(HttpCache, UpdatesRequestResponseTimeOn304) { |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 4149 | MockHttpCache cache; |
| 4150 | |
| 4151 | const char* kUrl = "http://foobar"; |
| 4152 | const char* kData = "body"; |
| 4153 | |
| 4154 | MockTransaction mock_network_response = { 0 }; |
| 4155 | mock_network_response.url = kUrl; |
| 4156 | |
| 4157 | AddMockTransaction(&mock_network_response); |
| 4158 | |
| 4159 | // Request |kUrl|, causing |kNetResponse1| to be written to the cache. |
| 4160 | |
| 4161 | MockTransaction request = { 0 }; |
| 4162 | request.url = kUrl; |
| 4163 | request.method = "GET"; |
| 4164 | request.request_headers = ""; |
| 4165 | request.data = kData; |
| 4166 | |
| 4167 | static const Response kNetResponse1 = { |
| 4168 | "HTTP/1.1 200 OK", |
| 4169 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 4170 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 4171 | kData |
| 4172 | }; |
| 4173 | |
| 4174 | kNetResponse1.AssignTo(&mock_network_response); |
| 4175 | |
| 4176 | RunTransactionTest(cache.http_cache(), request); |
| 4177 | |
| 4178 | // Request |kUrl| again, this time validating the cache and getting |
| 4179 | // a 304 back. |
| 4180 | |
| 4181 | request.load_flags = net::LOAD_VALIDATE_CACHE; |
| 4182 | |
| 4183 | static const Response kNetResponse2 = { |
| 4184 | "HTTP/1.1 304 Not Modified", |
| 4185 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n", |
| 4186 | "" |
| 4187 | }; |
| 4188 | |
| 4189 | kNetResponse2.AssignTo(&mock_network_response); |
| 4190 | |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 4191 | base::Time request_time = base::Time() + base::TimeDelta::FromHours(1234); |
| 4192 | base::Time response_time = base::Time() + base::TimeDelta::FromHours(1235); |
| 4193 | |
| 4194 | mock_network_response.request_time = request_time; |
| 4195 | mock_network_response.response_time = response_time; |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 4196 | |
| 4197 | net::HttpResponseInfo response; |
| 4198 | RunTransactionTestWithResponseInfo(cache.http_cache(), request, &response); |
| 4199 | |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 4200 | // The request and response times should have been updated. |
| 4201 | EXPECT_EQ(request_time.ToInternalValue(), |
| 4202 | response.request_time.ToInternalValue()); |
| 4203 | EXPECT_EQ(response_time.ToInternalValue(), |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 4204 | response.response_time.ToInternalValue()); |
| 4205 | |
| 4206 | std::string headers; |
| 4207 | response.headers->GetNormalizedHeaders(&headers); |
| 4208 | |
| 4209 | EXPECT_EQ("HTTP/1.1 200 OK\n" |
| 4210 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 4211 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 4212 | headers); |
| 4213 | |
| 4214 | RemoveMockTransaction(&mock_network_response); |
| 4215 | } |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 4216 | |
| 4217 | // Tests that we can write metadata to an entry. |
| 4218 | TEST(HttpCache, WriteMetadata_OK) { |
| 4219 | MockHttpCache cache; |
| 4220 | |
| 4221 | // Write to the cache |
| 4222 | net::HttpResponseInfo response; |
| 4223 | RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 4224 | &response); |
| 4225 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 4226 | |
| 4227 | // Trivial call. |
| 4228 | cache.http_cache()->WriteMetadata(GURL("foo"), Time::Now(), NULL, 0); |
| 4229 | |
| 4230 | // Write meta data to the same entry. |
| 4231 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); |
| 4232 | memset(buf->data(), 0, buf->size()); |
| 4233 | base::strlcpy(buf->data(), "Hi there", buf->size()); |
| 4234 | cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url), |
| 4235 | response.response_time, buf, buf->size()); |
| 4236 | |
| 4237 | // Release the buffer before the operation takes place. |
| 4238 | buf = NULL; |
| 4239 | |
| 4240 | // Makes sure we finish pending operations. |
| 4241 | MessageLoop::current()->RunAllPending(); |
| 4242 | |
| 4243 | RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 4244 | &response); |
| 4245 | ASSERT_TRUE(response.metadata.get() != NULL); |
| 4246 | EXPECT_EQ(50, response.metadata->size()); |
| 4247 | EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there")); |
| 4248 | |
| 4249 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4250 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 4251 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4252 | } |
| 4253 | |
| 4254 | // Tests that we only write metadata to an entry if the time stamp matches. |
| 4255 | TEST(HttpCache, WriteMetadata_Fail) { |
| 4256 | MockHttpCache cache; |
| 4257 | |
| 4258 | // Write to the cache |
| 4259 | net::HttpResponseInfo response; |
| 4260 | RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 4261 | &response); |
| 4262 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 4263 | |
| 4264 | // Attempt to write meta data to the same entry. |
| 4265 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); |
| 4266 | memset(buf->data(), 0, buf->size()); |
| 4267 | base::strlcpy(buf->data(), "Hi there", buf->size()); |
| 4268 | base::Time expected_time = response.response_time - |
| 4269 | base::TimeDelta::FromMilliseconds(20); |
| 4270 | cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url), |
| 4271 | expected_time, buf, buf->size()); |
| 4272 | |
| 4273 | // Makes sure we finish pending operations. |
| 4274 | MessageLoop::current()->RunAllPending(); |
| 4275 | |
| 4276 | RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 4277 | &response); |
| 4278 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 4279 | |
| 4280 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4281 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 4282 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4283 | } |
| 4284 | |
| 4285 | // Tests that we can read metadata after validating the entry and with READ mode |
| 4286 | // transactions. |
| 4287 | TEST(HttpCache, ReadMetadata) { |
| 4288 | MockHttpCache cache; |
| 4289 | |
| 4290 | // Write to the cache |
| 4291 | net::HttpResponseInfo response; |
| 4292 | RunTransactionTestWithResponseInfo(cache.http_cache(), |
| 4293 | kTypicalGET_Transaction, &response); |
| 4294 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 4295 | |
| 4296 | // Write meta data to the same entry. |
| 4297 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); |
| 4298 | memset(buf->data(), 0, buf->size()); |
| 4299 | base::strlcpy(buf->data(), "Hi there", buf->size()); |
| 4300 | cache.http_cache()->WriteMetadata(GURL(kTypicalGET_Transaction.url), |
| 4301 | response.response_time, buf, buf->size()); |
| 4302 | |
| 4303 | // Makes sure we finish pending operations. |
| 4304 | MessageLoop::current()->RunAllPending(); |
| 4305 | |
| 4306 | // Start with a READ mode transaction. |
| 4307 | MockTransaction trans1(kTypicalGET_Transaction); |
| 4308 | trans1.load_flags = net::LOAD_ONLY_FROM_CACHE; |
| 4309 | |
| 4310 | RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response); |
| 4311 | ASSERT_TRUE(response.metadata.get() != NULL); |
| 4312 | EXPECT_EQ(50, response.metadata->size()); |
| 4313 | EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there")); |
| 4314 | |
| 4315 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4316 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 4317 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4318 | MessageLoop::current()->RunAllPending(); |
| 4319 | |
| 4320 | // Now make sure that the entry is re-validated with the server. |
| 4321 | trans1.load_flags = net::LOAD_VALIDATE_CACHE; |
| 4322 | trans1.status = "HTTP/1.1 304 Not Modified"; |
| 4323 | AddMockTransaction(&trans1); |
| 4324 | |
| 4325 | response.metadata = NULL; |
| 4326 | RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response); |
| 4327 | EXPECT_TRUE(response.metadata.get() != NULL); |
| 4328 | |
| 4329 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4330 | EXPECT_EQ(3, cache.disk_cache()->open_count()); |
| 4331 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4332 | MessageLoop::current()->RunAllPending(); |
| 4333 | RemoveMockTransaction(&trans1); |
| 4334 | |
| 4335 | // Now return 200 when validating the entry so the metadata will be lost. |
| 4336 | MockTransaction trans2(kTypicalGET_Transaction); |
| 4337 | trans2.load_flags = net::LOAD_VALIDATE_CACHE; |
| 4338 | RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); |
| 4339 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 4340 | |
| 4341 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 4342 | EXPECT_EQ(4, cache.disk_cache()->open_count()); |
| 4343 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4344 | } |