[email protected] | 93fe7516 | 2012-02-09 21:51:31 | [diff] [blame] | 1 | // Copyright (c) 2012 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] | f98ead6 | 2011-10-20 01:24:20 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 8 | #include "base/bind_helpers.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 9 | #include "base/memory/scoped_vector.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 10 | #include "base/message_loop.h" |
| 11 | #include "base/string_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 12 | #include "base/stringprintf.h" |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 13 | #include "net/base/cache_type.h" |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 14 | #include "net/base/host_port_pair.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 15 | #include "net/base/load_flags.h" |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 16 | #include "net/base/load_timing_info.h" |
| 17 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 18 | #include "net/base/net_errors.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 19 | #include "net/base/net_log_unittest.h" |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 20 | #include "net/base/upload_bytes_element_reader.h" |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 21 | #include "net/base/upload_data_stream.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 22 | #include "net/cert/cert_status_flags.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 23 | #include "net/disk_cache/disk_cache.h" |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 24 | #include "net/http/http_byte_range.h" |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 25 | #include "net/http/http_request_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 26 | #include "net/http/http_request_info.h" |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 27 | #include "net/http/http_response_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 28 | #include "net/http/http_response_info.h" |
| 29 | #include "net/http/http_transaction.h" |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 30 | #include "net/http/http_transaction_delegate.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 31 | #include "net/http/http_transaction_unittest.h" |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 32 | #include "net/http/http_util.h" |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 33 | #include "net/http/mock_http_cache.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 34 | #include "net/ssl/ssl_cert_request_info.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 35 | #include "testing/gtest/include/gtest/gtest.h" |
| 36 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 37 | using base::Time; |
| 38 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 39 | namespace { |
| 40 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 41 | // Tests the load timing values of a request that goes through a |
| 42 | // MockNetworkTransaction. |
| 43 | void TestLoadTimingNetworkRequest(const net::LoadTimingInfo& load_timing_info) { |
| 44 | EXPECT_FALSE(load_timing_info.socket_reused); |
| 45 | EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
| 46 | |
| 47 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 48 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 49 | |
| 50 | net::ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 51 | net::CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 52 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 53 | load_timing_info.send_start); |
| 54 | |
| 55 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 56 | |
| 57 | // Set by URLRequest / URLRequestHttpJob, at a higher level. |
| 58 | EXPECT_TRUE(load_timing_info.request_start_time.is_null()); |
| 59 | EXPECT_TRUE(load_timing_info.request_start.is_null()); |
| 60 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 61 | } |
| 62 | |
| 63 | // Tests the load timing values of a request that receives a cached response. |
| 64 | void TestLoadTimingCachedResponse(const net::LoadTimingInfo& load_timing_info) { |
| 65 | EXPECT_FALSE(load_timing_info.socket_reused); |
| 66 | EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
| 67 | |
| 68 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 69 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 70 | |
| 71 | net::ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 72 | |
| 73 | // Only the send start / end times should be sent, and they should have the |
| 74 | // same value. |
| 75 | EXPECT_FALSE(load_timing_info.send_start.is_null()); |
| 76 | EXPECT_EQ(load_timing_info.send_start, load_timing_info.send_end); |
| 77 | |
| 78 | // Set by URLRequest / URLRequestHttpJob, at a higher level. |
| 79 | EXPECT_TRUE(load_timing_info.request_start_time.is_null()); |
| 80 | EXPECT_TRUE(load_timing_info.request_start.is_null()); |
| 81 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 82 | } |
| 83 | |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 84 | class DeleteCacheCompletionCallback : public net::TestCompletionCallbackBase { |
[email protected] | 6a9c55c | 2010-08-21 02:04:08 | [diff] [blame] | 85 | public: |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 86 | explicit DeleteCacheCompletionCallback(MockHttpCache* cache) |
| 87 | : cache_(cache), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 88 | callback_(base::Bind(&DeleteCacheCompletionCallback::OnComplete, |
| 89 | base::Unretained(this))) { |
[email protected] | 2c5c9d5 | 2011-12-07 14:15:36 | [diff] [blame] | 90 | } |
[email protected] | 8ebf9b13 | 2011-12-06 19:11:51 | [diff] [blame] | 91 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 92 | const net::CompletionCallback& callback() const { return callback_; } |
| 93 | |
[email protected] | 6a9c55c | 2010-08-21 02:04:08 | [diff] [blame] | 94 | private: |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 95 | void OnComplete(int result) { |
| 96 | delete cache_; |
| 97 | SetResult(result); |
| 98 | } |
| 99 | |
[email protected] | 6a9c55c | 2010-08-21 02:04:08 | [diff] [blame] | 100 | MockHttpCache* cache_; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 101 | net::CompletionCallback callback_; |
| 102 | |
| 103 | DISALLOW_COPY_AND_ASSIGN(DeleteCacheCompletionCallback); |
[email protected] | 6a9c55c | 2010-08-21 02:04:08 | [diff] [blame] | 104 | }; |
| 105 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 106 | //----------------------------------------------------------------------------- |
| 107 | // helpers |
| 108 | |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 109 | class TestHttpTransactionDelegate : public net::HttpTransactionDelegate { |
| 110 | public: |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 111 | TestHttpTransactionDelegate(int num_cache_actions_to_observe, |
| 112 | int num_network_actions_to_observe) |
| 113 | : num_callbacks_observed_(0), |
| 114 | num_remaining_cache_actions_to_observe_(num_cache_actions_to_observe), |
| 115 | num_remaining_network_actions_to_observe_( |
| 116 | num_network_actions_to_observe), |
| 117 | cache_action_in_progress_(false), |
| 118 | network_action_in_progress_(false) { |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 119 | } |
| 120 | virtual ~TestHttpTransactionDelegate() { |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 121 | EXPECT_EQ(0, num_remaining_cache_actions_to_observe_); |
| 122 | EXPECT_EQ(0, num_remaining_network_actions_to_observe_); |
| 123 | EXPECT_FALSE(cache_action_in_progress_); |
| 124 | EXPECT_FALSE(network_action_in_progress_); |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 125 | } |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 126 | virtual void OnCacheActionStart() OVERRIDE { |
| 127 | num_callbacks_observed_++; |
| 128 | EXPECT_FALSE(cache_action_in_progress_); |
| 129 | EXPECT_FALSE(network_action_in_progress_); |
| 130 | EXPECT_GT(num_remaining_cache_actions_to_observe_, 0); |
| 131 | num_remaining_cache_actions_to_observe_--; |
| 132 | cache_action_in_progress_ = true; |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 133 | } |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 134 | virtual void OnCacheActionFinish() OVERRIDE { |
| 135 | num_callbacks_observed_++; |
| 136 | EXPECT_TRUE(cache_action_in_progress_); |
| 137 | cache_action_in_progress_ = false; |
| 138 | } |
| 139 | virtual void OnNetworkActionStart() OVERRIDE { |
| 140 | num_callbacks_observed_++; |
| 141 | EXPECT_FALSE(cache_action_in_progress_); |
| 142 | EXPECT_FALSE(network_action_in_progress_); |
| 143 | EXPECT_GT(num_remaining_network_actions_to_observe_, 0); |
| 144 | num_remaining_network_actions_to_observe_--; |
| 145 | network_action_in_progress_ = true; |
| 146 | } |
| 147 | virtual void OnNetworkActionFinish() OVERRIDE { |
| 148 | num_callbacks_observed_++; |
| 149 | EXPECT_TRUE(network_action_in_progress_); |
| 150 | network_action_in_progress_ = false; |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 151 | } |
| 152 | |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 153 | int num_callbacks_observed() { return num_callbacks_observed_; } |
| 154 | |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 155 | private: |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 156 | int num_callbacks_observed_; |
| 157 | int num_remaining_cache_actions_to_observe_; |
| 158 | int num_remaining_network_actions_to_observe_; |
| 159 | bool cache_action_in_progress_; |
| 160 | bool network_action_in_progress_; |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 161 | }; |
| 162 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 163 | void ReadAndVerifyTransaction(net::HttpTransaction* trans, |
| 164 | const MockTransaction& trans_info) { |
| 165 | std::string content; |
| 166 | int rv = ReadTransaction(trans, &content); |
| 167 | |
| 168 | EXPECT_EQ(net::OK, rv); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 169 | std::string expected(trans_info.data); |
| 170 | EXPECT_EQ(expected, content); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 173 | const int kNoDelegateTransactionCheck = -1; |
| 174 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 175 | void RunTransactionTestWithRequestAndDelegateAndGetTiming( |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 176 | net::HttpCache* cache, |
| 177 | const MockTransaction& trans_info, |
| 178 | const MockHttpRequest& request, |
| 179 | net::HttpResponseInfo* response_info, |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 180 | int num_cache_delegate_actions, |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 181 | int num_network_delegate_actions, |
| 182 | const net::BoundNetLog& net_log, |
| 183 | net::LoadTimingInfo* load_timing_info) { |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 184 | net::TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 185 | |
| 186 | // write to the cache |
| 187 | |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 188 | scoped_ptr<TestHttpTransactionDelegate> delegate; |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 189 | if (num_cache_delegate_actions != kNoDelegateTransactionCheck && |
| 190 | num_network_delegate_actions != kNoDelegateTransactionCheck) { |
| 191 | delegate.reset( |
| 192 | new TestHttpTransactionDelegate(num_cache_delegate_actions, |
| 193 | num_network_delegate_actions)); |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 194 | } |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 195 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 196 | int rv = cache->CreateTransaction( |
| 197 | net::DEFAULT_PRIORITY, &trans, delegate.get()); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 198 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 199 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 200 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 201 | rv = trans->Start(&request, callback.callback(), net_log); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 202 | if (rv == net::ERR_IO_PENDING) |
| 203 | rv = callback.WaitForResult(); |
[email protected] | 2ef5d00e | 2013-03-23 16:17:27 | [diff] [blame] | 204 | ASSERT_EQ(trans_info.return_code, rv); |
| 205 | |
| 206 | if (net::OK != rv) |
| 207 | return; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 208 | |
| 209 | const net::HttpResponseInfo* response = trans->GetResponseInfo(); |
| 210 | ASSERT_TRUE(response); |
| 211 | |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 212 | if (response_info) |
| 213 | *response_info = *response; |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 214 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 215 | if (load_timing_info) { |
| 216 | // If a fake network connection is used, need a NetLog to get a fake socket |
| 217 | // ID. |
| 218 | EXPECT_TRUE(net_log.net_log()); |
| 219 | *load_timing_info = net::LoadTimingInfo(); |
| 220 | trans->GetLoadTimingInfo(load_timing_info); |
| 221 | } |
| 222 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 223 | ReadAndVerifyTransaction(trans.get(), trans_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 224 | } |
| 225 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 226 | void RunTransactionTestWithRequestAndDelegate( |
| 227 | net::HttpCache* cache, |
| 228 | const MockTransaction& trans_info, |
| 229 | const MockHttpRequest& request, |
| 230 | net::HttpResponseInfo* response_info, |
| 231 | int num_cache_delegate_actions, |
| 232 | int num_network_delegate_actions) { |
| 233 | RunTransactionTestWithRequestAndDelegateAndGetTiming( |
| 234 | cache, trans_info, request, response_info, num_cache_delegate_actions, |
| 235 | num_network_delegate_actions, net::BoundNetLog(), NULL); |
| 236 | } |
| 237 | |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 238 | void RunTransactionTestWithRequest(net::HttpCache* cache, |
| 239 | const MockTransaction& trans_info, |
| 240 | const MockHttpRequest& request, |
| 241 | net::HttpResponseInfo* response_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 242 | RunTransactionTestWithRequestAndDelegate( |
| 243 | cache, trans_info, request, response_info, kNoDelegateTransactionCheck, |
| 244 | kNoDelegateTransactionCheck); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 247 | void RunTransactionTestAndGetTiming( |
| 248 | net::HttpCache* cache, |
| 249 | const MockTransaction& trans_info, |
| 250 | const net::BoundNetLog& log, |
| 251 | net::LoadTimingInfo* load_timing_info) { |
| 252 | RunTransactionTestWithRequestAndDelegateAndGetTiming( |
| 253 | cache, trans_info, MockHttpRequest(trans_info), NULL, |
| 254 | kNoDelegateTransactionCheck, kNoDelegateTransactionCheck, log, |
| 255 | load_timing_info); |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void RunTransactionTestWithDelegate(net::HttpCache* cache, |
| 259 | const MockTransaction& trans_info, |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 260 | int num_cache_delegate_actions, |
| 261 | int num_network_delegate_actions) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 262 | RunTransactionTestWithRequestAndDelegate( |
| 263 | cache, trans_info, MockHttpRequest(trans_info), NULL, |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 264 | num_cache_delegate_actions, num_network_delegate_actions); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 267 | void RunTransactionTest(net::HttpCache* cache, |
| 268 | const MockTransaction& trans_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 269 | RunTransactionTestAndGetTiming(cache, trans_info, net::BoundNetLog(), NULL); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 270 | } |
| 271 | |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 272 | void RunTransactionTestWithResponseInfo(net::HttpCache* cache, |
| 273 | const MockTransaction& trans_info, |
| 274 | net::HttpResponseInfo* response) { |
| 275 | RunTransactionTestWithRequest( |
| 276 | cache, trans_info, MockHttpRequest(trans_info), response); |
| 277 | } |
| 278 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 279 | void RunTransactionTestWithResponseInfoAndGetTiming( |
| 280 | net::HttpCache* cache, |
| 281 | const MockTransaction& trans_info, |
| 282 | net::HttpResponseInfo* response, |
| 283 | const net::BoundNetLog& log, |
| 284 | net::LoadTimingInfo* load_timing_info) { |
| 285 | RunTransactionTestWithRequestAndDelegateAndGetTiming( |
| 286 | cache, trans_info, MockHttpRequest(trans_info), response, |
| 287 | kNoDelegateTransactionCheck, kNoDelegateTransactionCheck, log, |
| 288 | load_timing_info); |
| 289 | } |
| 290 | |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 291 | void RunTransactionTestWithResponse(net::HttpCache* cache, |
| 292 | const MockTransaction& trans_info, |
| 293 | std::string* response_headers) { |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 294 | net::HttpResponseInfo response; |
| 295 | RunTransactionTestWithResponseInfo(cache, trans_info, &response); |
| 296 | response.headers->GetNormalizedHeaders(response_headers); |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 297 | } |
| 298 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 299 | void RunTransactionTestWithResponseAndGetTiming( |
| 300 | net::HttpCache* cache, |
| 301 | const MockTransaction& trans_info, |
| 302 | std::string* response_headers, |
| 303 | const net::BoundNetLog& log, |
| 304 | net::LoadTimingInfo* load_timing_info) { |
| 305 | net::HttpResponseInfo response; |
| 306 | RunTransactionTestWithRequestAndDelegateAndGetTiming( |
| 307 | cache, trans_info, MockHttpRequest(trans_info), &response, |
| 308 | kNoDelegateTransactionCheck, kNoDelegateTransactionCheck, |
| 309 | log, load_timing_info); |
| 310 | response.headers->GetNormalizedHeaders(response_headers); |
| 311 | } |
| 312 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 313 | // This class provides a handler for kFastNoStoreGET_Transaction so that the |
| 314 | // no-store header can be included on demand. |
| 315 | class FastTransactionServer { |
| 316 | public: |
| 317 | FastTransactionServer() { |
| 318 | no_store = false; |
| 319 | } |
| 320 | ~FastTransactionServer() {} |
| 321 | |
| 322 | void set_no_store(bool value) { no_store = value; } |
| 323 | |
| 324 | static void FastNoStoreHandler(const net::HttpRequestInfo* request, |
| 325 | std::string* response_status, |
| 326 | std::string* response_headers, |
| 327 | std::string* response_data) { |
| 328 | if (no_store) |
| 329 | *response_headers = "Cache-Control: no-store\n"; |
| 330 | } |
| 331 | |
| 332 | private: |
| 333 | static bool no_store; |
| 334 | DISALLOW_COPY_AND_ASSIGN(FastTransactionServer); |
| 335 | }; |
| 336 | bool FastTransactionServer::no_store; |
| 337 | |
| 338 | const MockTransaction kFastNoStoreGET_Transaction = { |
| 339 | "http://www.google.com/nostore", |
| 340 | "GET", |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 341 | base::Time(), |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 342 | "", |
| 343 | net::LOAD_VALIDATE_CACHE, |
| 344 | "HTTP/1.1 200 OK", |
| 345 | "Cache-Control: max-age=10000\n", |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 346 | base::Time(), |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 347 | "<html><body>Google Blah Blah</body></html>", |
| 348 | TEST_MODE_SYNC_NET_START, |
| 349 | &FastTransactionServer::FastNoStoreHandler, |
[email protected] | 2ef5d00e | 2013-03-23 16:17:27 | [diff] [blame] | 350 | 0, |
| 351 | net::OK |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 352 | }; |
| 353 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 354 | // This class provides a handler for kRangeGET_TransactionOK so that the range |
| 355 | // request can be served on demand. |
| 356 | class RangeTransactionServer { |
| 357 | public: |
| 358 | RangeTransactionServer() { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 359 | not_modified_ = false; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 360 | modified_ = false; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 361 | bad_200_ = false; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 362 | } |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 363 | ~RangeTransactionServer() { |
| 364 | not_modified_ = false; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 365 | modified_ = false; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 366 | bad_200_ = false; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 367 | } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 368 | |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 369 | // Returns only 416 or 304 when set. |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 370 | void set_not_modified(bool value) { not_modified_ = value; } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 371 | |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 372 | // Returns 206 when revalidating a range (instead of 304). |
| 373 | void set_modified(bool value) { modified_ = value; } |
| 374 | |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 375 | // Returns 200 instead of 206 (a malformed response overall). |
| 376 | void set_bad_200(bool value) { bad_200_ = value; } |
| 377 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 378 | static void RangeHandler(const net::HttpRequestInfo* request, |
| 379 | std::string* response_status, |
| 380 | std::string* response_headers, |
| 381 | std::string* response_data); |
| 382 | |
| 383 | private: |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 384 | static bool not_modified_; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 385 | static bool modified_; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 386 | static bool bad_200_; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 387 | DISALLOW_COPY_AND_ASSIGN(RangeTransactionServer); |
| 388 | }; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 389 | bool RangeTransactionServer::not_modified_ = false; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 390 | bool RangeTransactionServer::modified_ = false; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 391 | bool RangeTransactionServer::bad_200_ = false; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 392 | |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 393 | // A dummy extra header that must be preserved on a given request. |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 394 | |
| 395 | // EXTRA_HEADER_LINE doesn't include a line terminator because it |
| 396 | // will be passed to AddHeaderFromString() which doesn't accept them. |
| 397 | #define EXTRA_HEADER_LINE "Extra: header" |
| 398 | |
| 399 | // EXTRA_HEADER contains a line terminator, as expected by |
| 400 | // AddHeadersFromString() (_not_ AddHeaderFromString()). |
| 401 | #define EXTRA_HEADER EXTRA_HEADER_LINE "\r\n" |
| 402 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 403 | static const char kExtraHeaderKey[] = "Extra"; |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 404 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 405 | // Static. |
| 406 | void RangeTransactionServer::RangeHandler(const net::HttpRequestInfo* request, |
| 407 | std::string* response_status, |
| 408 | std::string* response_headers, |
| 409 | std::string* response_data) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 410 | if (request->extra_headers.IsEmpty()) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 411 | response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 412 | response_data->clear(); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 413 | return; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 414 | } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 415 | |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 416 | // We want to make sure we don't delete extra headers. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 417 | EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 418 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 419 | if (not_modified_) { |
| 420 | response_status->assign("HTTP/1.1 304 Not Modified"); |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 421 | response_data->clear(); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 425 | std::vector<net::HttpByteRange> ranges; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 426 | std::string range_header; |
| 427 | if (!request->extra_headers.GetHeader( |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 428 | net::HttpRequestHeaders::kRange, &range_header) || |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 429 | !net::HttpUtil::ParseRangeHeader(range_header, &ranges) || bad_200_ || |
| 430 | ranges.size() != 1) { |
| 431 | // This is not a byte range request. We return 200. |
| 432 | response_status->assign("HTTP/1.1 200 OK"); |
| 433 | response_headers->assign("Date: Wed, 28 Nov 2007 09:40:09 GMT"); |
| 434 | response_data->assign("Not a range"); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 435 | return; |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 436 | } |
| 437 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 438 | // We can handle this range request. |
| 439 | net::HttpByteRange byte_range = ranges[0]; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 440 | if (byte_range.first_byte_position() > 79) { |
| 441 | response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 442 | response_data->clear(); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 443 | return; |
| 444 | } |
| 445 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 446 | EXPECT_TRUE(byte_range.ComputeBounds(80)); |
| 447 | int start = static_cast<int>(byte_range.first_byte_position()); |
| 448 | int end = static_cast<int>(byte_range.last_byte_position()); |
| 449 | |
| 450 | EXPECT_LT(end, 80); |
| 451 | |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 452 | std::string content_range = base::StringPrintf( |
| 453 | "Content-Range: bytes %d-%d/80\n", start, end); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 454 | response_headers->append(content_range); |
| 455 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 456 | if (!request->extra_headers.HasHeader("If-None-Match") || modified_) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 457 | std::string data; |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 458 | if (end == start) { |
| 459 | EXPECT_EQ(0, end % 10); |
| 460 | data = "r"; |
| 461 | } else { |
| 462 | EXPECT_EQ(9, (end - start) % 10); |
| 463 | for (int block_start = start; block_start < end; block_start += 10) { |
| 464 | base::StringAppendF(&data, "rg: %02d-%02d ", |
| 465 | block_start, block_start + 9); |
| 466 | } |
[email protected] | a77fa2dc | 2010-11-15 12:11:11 | [diff] [blame] | 467 | } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 468 | *response_data = data; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 469 | |
| 470 | if (end - start != 9) { |
| 471 | // We also have to fix content-length. |
| 472 | int len = end - start + 1; |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 473 | std::string content_length = base::StringPrintf("Content-Length: %d\n", |
| 474 | len); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 475 | response_headers->replace(response_headers->find("Content-Length:"), |
| 476 | content_length.size(), content_length); |
| 477 | } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 478 | } else { |
| 479 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 480 | response_data->clear(); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | const MockTransaction kRangeGET_TransactionOK = { |
| 485 | "http://www.google.com/range", |
| 486 | "GET", |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 487 | base::Time(), |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 488 | "Range: bytes = 40-49\r\n" |
| 489 | EXTRA_HEADER, |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 490 | net::LOAD_NORMAL, |
| 491 | "HTTP/1.1 206 Partial Content", |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 492 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 493 | "ETag: \"foo\"\n" |
| 494 | "Accept-Ranges: bytes\n" |
| 495 | "Content-Length: 10\n", |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 496 | base::Time(), |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 497 | "rg: 40-49 ", |
| 498 | TEST_MODE_NORMAL, |
| 499 | &RangeTransactionServer::RangeHandler, |
[email protected] | 2ef5d00e | 2013-03-23 16:17:27 | [diff] [blame] | 500 | 0, |
| 501 | net::OK |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 502 | }; |
| 503 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 504 | // Verifies the response headers (|response|) match a partial content |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 505 | // response for the range starting at |start| and ending at |end|. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 506 | void Verify206Response(std::string response, int start, int end) { |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 507 | std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), |
| 508 | response.size())); |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 509 | scoped_refptr<net::HttpResponseHeaders> headers( |
| 510 | new net::HttpResponseHeaders(raw_headers)); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 511 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 512 | ASSERT_EQ(206, headers->response_code()); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 513 | |
| 514 | int64 range_start, range_end, object_size; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 515 | ASSERT_TRUE( |
| 516 | headers->GetContentRange(&range_start, &range_end, &object_size)); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 517 | int64 content_length = headers->GetContentLength(); |
| 518 | |
| 519 | int length = end - start + 1; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 520 | ASSERT_EQ(length, content_length); |
| 521 | ASSERT_EQ(start, range_start); |
| 522 | ASSERT_EQ(end, range_end); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 523 | } |
| 524 | |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 525 | // Creates a truncated entry that can be resumed using byte ranges. |
| 526 | void CreateTruncatedEntry(std::string raw_headers, MockHttpCache* cache) { |
| 527 | // Create a disk cache entry that stores an incomplete resource. |
| 528 | disk_cache::Entry* entry; |
| 529 | ASSERT_TRUE(cache->CreateBackendEntry(kRangeGET_TransactionOK.url, &entry, |
| 530 | NULL)); |
| 531 | |
| 532 | raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), |
| 533 | raw_headers.size()); |
| 534 | |
| 535 | net::HttpResponseInfo response; |
| 536 | response.response_time = base::Time::Now(); |
| 537 | response.request_time = base::Time::Now(); |
| 538 | response.headers = new net::HttpResponseHeaders(raw_headers); |
| 539 | // Set the last argument for this to be an incomplete request. |
| 540 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true)); |
| 541 | |
| 542 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100)); |
| 543 | int len = static_cast<int>(base::strlcpy(buf->data(), |
| 544 | "rg: 00-09 rg: 10-19 ", 100)); |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 545 | net::TestCompletionCallback cb; |
| 546 | int rv = entry->WriteData(1, 0, buf, len, cb.callback(), true); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 547 | EXPECT_EQ(len, cb.GetResult(rv)); |
| 548 | entry->Close(); |
| 549 | } |
| 550 | |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 551 | // Helper to represent a network HTTP response. |
| 552 | struct Response { |
| 553 | // Set this response into |trans|. |
| 554 | void AssignTo(MockTransaction* trans) const { |
| 555 | trans->status = status; |
| 556 | trans->response_headers = headers; |
| 557 | trans->data = body; |
| 558 | } |
| 559 | |
| 560 | std::string status_and_headers() const { |
| 561 | return std::string(status) + "\n" + std::string(headers); |
| 562 | } |
| 563 | |
| 564 | const char* status; |
| 565 | const char* headers; |
| 566 | const char* body; |
| 567 | }; |
| 568 | |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 569 | struct Context { |
| 570 | Context() : result(net::ERR_IO_PENDING) {} |
| 571 | |
| 572 | int result; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 573 | net::TestCompletionCallback callback; |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 574 | scoped_ptr<net::HttpTransaction> trans; |
| 575 | }; |
| 576 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 577 | } // namespace |
| 578 | |
| 579 | |
| 580 | //----------------------------------------------------------------------------- |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 581 | // Tests. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 582 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 583 | TEST(HttpCache, CreateThenDestroy) { |
| 584 | MockHttpCache cache; |
| 585 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 586 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 587 | int rv = cache.http_cache()->CreateTransaction( |
| 588 | net::DEFAULT_PRIORITY, &trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 589 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 590 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 591 | } |
| 592 | |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 593 | TEST(HttpCache, GetBackend) { |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 594 | MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(0)); |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 595 | |
[email protected] | 6a98903 | 2010-06-14 19:05:33 | [diff] [blame] | 596 | disk_cache::Backend* backend; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 597 | net::TestCompletionCallback cb; |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 598 | // This will lazily initialize the backend. |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 599 | int rv = cache.http_cache()->GetBackend(&backend, cb.callback()); |
[email protected] | 6a98903 | 2010-06-14 19:05:33 | [diff] [blame] | 600 | EXPECT_EQ(net::OK, cb.GetResult(rv)); |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 601 | } |
| 602 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 603 | TEST(HttpCache, SimpleGET) { |
| 604 | MockHttpCache cache; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 605 | net::CapturingBoundNetLog log; |
| 606 | net::LoadTimingInfo load_timing_info; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 607 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 608 | // Write to the cache. |
| 609 | RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction, |
| 610 | log.bound(), &load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 611 | |
| 612 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 613 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 614 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 615 | TestLoadTimingNetworkRequest(load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | TEST(HttpCache, SimpleGETNoDiskCache) { |
| 619 | MockHttpCache cache; |
| 620 | |
| 621 | cache.disk_cache()->set_fail_requests(); |
| 622 | |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 623 | net::CapturingBoundNetLog log; |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 624 | log.SetLogLevel(net::NetLog::LOG_BASIC); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 625 | net::LoadTimingInfo load_timing_info; |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 626 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 627 | // Read from the network, and don't use the cache. |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 628 | RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction, |
| 629 | log.bound(), &load_timing_info); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 630 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 631 | // Check that the NetLog was filled as expected. |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 632 | // (We attempted to both Open and Create entries, but both failed). |
[email protected] | f3da152d | 2012-06-02 01:00:57 | [diff] [blame] | 633 | net::CapturingNetLog::CapturedEntryList entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 634 | log.GetEntries(&entries); |
| 635 | |
| 636 | EXPECT_EQ(6u, entries.size()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 637 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 638 | entries, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 639 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 640 | entries, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 641 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 642 | entries, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 643 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 644 | entries, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 645 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 646 | entries, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 647 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 648 | entries, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 649 | |
| 650 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 651 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 652 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 653 | TestLoadTimingNetworkRequest(load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 654 | } |
| 655 | |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 656 | TEST(HttpCache, SimpleGETNoDiskCache2) { |
| 657 | // This will initialize a cache object with NULL backend. |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 658 | MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
| 659 | factory->set_fail(true); |
| 660 | factory->FinishCreation(); // We'll complete synchronously. |
| 661 | MockHttpCache cache(factory); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 662 | |
| 663 | // Read from the network, and don't use the cache. |
| 664 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 665 | |
| 666 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
[email protected] | 6a98903 | 2010-06-14 19:05:33 | [diff] [blame] | 667 | EXPECT_FALSE(cache.http_cache()->GetCurrentBackend()); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 668 | } |
| 669 | |
[email protected] | f6c9d56 | 2013-01-15 19:28:13 | [diff] [blame] | 670 | // Tests that IOBuffers are not referenced after IO completes. |
| 671 | TEST(HttpCache, ReleaseBuffer) { |
| 672 | MockHttpCache cache; |
| 673 | |
| 674 | // Write to the cache. |
| 675 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 676 | |
| 677 | MockHttpRequest request(kSimpleGET_Transaction); |
| 678 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 679 | int rv = cache.http_cache()->CreateTransaction( |
| 680 | net::DEFAULT_PRIORITY, &trans, NULL); |
[email protected] | f6c9d56 | 2013-01-15 19:28:13 | [diff] [blame] | 681 | ASSERT_EQ(net::OK, rv); |
| 682 | |
| 683 | const int kBufferSize = 10; |
| 684 | scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); |
| 685 | net::ReleaseBufferCompletionCallback cb(buffer); |
| 686 | |
| 687 | rv = trans->Start(&request, cb.callback(), net::BoundNetLog()); |
| 688 | EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 689 | |
| 690 | rv = trans->Read(buffer, kBufferSize, cb.callback()); |
| 691 | EXPECT_EQ(kBufferSize, cb.GetResult(rv)); |
| 692 | } |
| 693 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 694 | TEST(HttpCache, SimpleGETWithDiskFailures) { |
| 695 | MockHttpCache cache; |
| 696 | |
| 697 | cache.disk_cache()->set_soft_failures(true); |
| 698 | |
| 699 | // Read from the network, and fail to write to the cache. |
| 700 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 701 | |
| 702 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 703 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 704 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 705 | |
| 706 | // This one should see an empty cache again. |
| 707 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 708 | |
| 709 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 710 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 711 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 712 | } |
| 713 | |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 714 | // Tests that disk failures after the transaction has started don't cause the |
| 715 | // request to fail. |
| 716 | TEST(HttpCache, SimpleGETWithDiskFailures2) { |
| 717 | MockHttpCache cache; |
| 718 | |
| 719 | MockHttpRequest request(kSimpleGET_Transaction); |
| 720 | |
| 721 | scoped_ptr<Context> c(new Context()); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 722 | int rv = cache.http_cache()->CreateTransaction( |
| 723 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 724 | EXPECT_EQ(net::OK, rv); |
| 725 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 726 | rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 727 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 728 | rv = c->callback.WaitForResult(); |
| 729 | |
| 730 | // Start failing request now. |
| 731 | cache.disk_cache()->set_soft_failures(true); |
| 732 | |
| 733 | // We have to open the entry again to propagate the failure flag. |
| 734 | disk_cache::Entry* en; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 735 | ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &en)); |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 736 | en->Close(); |
| 737 | |
| 738 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 739 | c.reset(); |
| 740 | |
| 741 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 742 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 743 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 744 | |
| 745 | // This one should see an empty cache again. |
| 746 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 747 | |
| 748 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 749 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 750 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 751 | } |
| 752 | |
[email protected] | 93fe7516 | 2012-02-09 21:51:31 | [diff] [blame] | 753 | // Tests that we handle failures to read from the cache. |
[email protected] | 4a24453 | 2011-04-04 02:10:33 | [diff] [blame] | 754 | TEST(HttpCache, SimpleGETWithDiskFailures3) { |
| 755 | MockHttpCache cache; |
| 756 | |
| 757 | // Read from the network, and write to the cache. |
| 758 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 759 | |
| 760 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 761 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 762 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 763 | |
| 764 | cache.disk_cache()->set_soft_failures(true); |
| 765 | |
| 766 | // Now fail to read from the cache. |
| 767 | scoped_ptr<Context> c(new Context()); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 768 | int rv = cache.http_cache()->CreateTransaction( |
| 769 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 4a24453 | 2011-04-04 02:10:33 | [diff] [blame] | 770 | EXPECT_EQ(net::OK, rv); |
| 771 | |
| 772 | MockHttpRequest request(kSimpleGET_Transaction); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 773 | rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 40caa4c | 2012-03-20 20:42:58 | [diff] [blame] | 774 | EXPECT_EQ(net::OK, c->callback.GetResult(rv)); |
[email protected] | 93fe7516 | 2012-02-09 21:51:31 | [diff] [blame] | 775 | |
| 776 | // Now verify that the entry was removed from the cache. |
| 777 | cache.disk_cache()->set_soft_failures(false); |
| 778 | |
[email protected] | 93fe7516 | 2012-02-09 21:51:31 | [diff] [blame] | 779 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 780 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 781 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | 40caa4c | 2012-03-20 20:42:58 | [diff] [blame] | 782 | |
| 783 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 784 | |
| 785 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 786 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 787 | EXPECT_EQ(3, cache.disk_cache()->create_count()); |
[email protected] | 4a24453 | 2011-04-04 02:10:33 | [diff] [blame] | 788 | } |
| 789 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 790 | TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) { |
| 791 | MockHttpCache cache; |
| 792 | |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 793 | net::CapturingBoundNetLog log; |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 794 | |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 795 | // This prevents a number of write events from being logged. |
| 796 | log.SetLogLevel(net::NetLog::LOG_BASIC); |
| 797 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 798 | net::LoadTimingInfo load_timing_info; |
| 799 | |
| 800 | // Write to the cache. |
| 801 | RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction, |
| 802 | log.bound(), &load_timing_info); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 803 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 804 | // Check that the NetLog was filled as expected. |
[email protected] | f3da152d | 2012-06-02 01:00:57 | [diff] [blame] | 805 | net::CapturingNetLog::CapturedEntryList entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 806 | log.GetEntries(&entries); |
| 807 | |
| 808 | EXPECT_EQ(8u, entries.size()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 809 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 810 | entries, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 811 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 812 | entries, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 813 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 814 | entries, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 815 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 816 | entries, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 817 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 818 | entries, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 819 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 820 | entries, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 821 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 822 | entries, 6, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 823 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 824 | entries, 7, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 825 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 826 | TestLoadTimingNetworkRequest(load_timing_info); |
| 827 | |
| 828 | // Force this transaction to read from the cache. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 829 | MockTransaction transaction(kSimpleGET_Transaction); |
| 830 | transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 831 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 832 | log.Clear(); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 833 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 834 | RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 835 | &load_timing_info); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 836 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 837 | // Check that the NetLog was filled as expected. |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 838 | log.GetEntries(&entries); |
| 839 | |
| 840 | EXPECT_EQ(8u, entries.size()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 841 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 842 | entries, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 843 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 844 | entries, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 845 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 846 | entries, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 847 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 848 | entries, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 849 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 850 | entries, 4, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 851 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 852 | entries, 5, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 853 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 854 | entries, 6, net::NetLog::TYPE_HTTP_CACHE_READ_INFO)); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 855 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 856 | entries, 7, net::NetLog::TYPE_HTTP_CACHE_READ_INFO)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 857 | |
| 858 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 859 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 860 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 861 | TestLoadTimingCachedResponse(load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { |
| 865 | MockHttpCache cache; |
| 866 | |
| 867 | // force this transaction to read from the cache |
| 868 | MockTransaction transaction(kSimpleGET_Transaction); |
| 869 | transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 870 | |
| 871 | MockHttpRequest request(transaction); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 872 | net::TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 873 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 874 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 875 | int rv = cache.http_cache()->CreateTransaction( |
| 876 | net::DEFAULT_PRIORITY, &trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 877 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 878 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 879 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 880 | rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 881 | if (rv == net::ERR_IO_PENDING) |
| 882 | rv = callback.WaitForResult(); |
| 883 | ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
| 884 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 885 | trans.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 886 | |
| 887 | EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 888 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 889 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 890 | } |
| 891 | |
| 892 | TEST(HttpCache, SimpleGET_LoadPreferringCache_Hit) { |
| 893 | MockHttpCache cache; |
| 894 | |
| 895 | // write to the cache |
| 896 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 897 | |
| 898 | // force this transaction to read from the cache if valid |
| 899 | MockTransaction transaction(kSimpleGET_Transaction); |
| 900 | transaction.load_flags |= net::LOAD_PREFERRING_CACHE; |
| 901 | |
| 902 | RunTransactionTest(cache.http_cache(), transaction); |
| 903 | |
| 904 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 905 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 906 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 907 | } |
| 908 | |
| 909 | TEST(HttpCache, SimpleGET_LoadPreferringCache_Miss) { |
| 910 | MockHttpCache cache; |
| 911 | |
| 912 | // force this transaction to read from the cache if valid |
| 913 | MockTransaction transaction(kSimpleGET_Transaction); |
| 914 | transaction.load_flags |= net::LOAD_PREFERRING_CACHE; |
| 915 | |
| 916 | RunTransactionTest(cache.http_cache(), transaction); |
| 917 | |
| 918 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 919 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 920 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 921 | } |
| 922 | |
[email protected] | 528e778 | 2012-11-16 22:36:17 | [diff] [blame] | 923 | // Tests LOAD_PREFERRING_CACHE in the presence of vary headers. |
| 924 | TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMatch) { |
| 925 | MockHttpCache cache; |
| 926 | |
| 927 | // Write to the cache. |
| 928 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 929 | transaction.request_headers = "Foo: bar\r\n"; |
[email protected] | 528e778 | 2012-11-16 22:36:17 | [diff] [blame] | 930 | transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 931 | "Vary: Foo\n"; |
| 932 | AddMockTransaction(&transaction); |
| 933 | RunTransactionTest(cache.http_cache(), transaction); |
| 934 | |
| 935 | // Read from the cache. |
| 936 | transaction.load_flags |= net::LOAD_PREFERRING_CACHE; |
| 937 | RunTransactionTest(cache.http_cache(), transaction); |
| 938 | |
| 939 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 940 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 941 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 942 | RemoveMockTransaction(&transaction); |
| 943 | } |
| 944 | |
| 945 | // Tests LOAD_PREFERRING_CACHE in the presence of vary headers. |
| 946 | TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMismatch) { |
| 947 | MockHttpCache cache; |
| 948 | |
| 949 | // Write to the cache. |
| 950 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 951 | transaction.request_headers = "Foo: bar\r\n"; |
[email protected] | 528e778 | 2012-11-16 22:36:17 | [diff] [blame] | 952 | transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 953 | "Vary: Foo\n"; |
| 954 | AddMockTransaction(&transaction); |
| 955 | RunTransactionTest(cache.http_cache(), transaction); |
| 956 | |
| 957 | // Attempt to read from the cache... this is a vary mismatch that must reach |
| 958 | // the network again. |
| 959 | transaction.load_flags |= net::LOAD_PREFERRING_CACHE; |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 960 | transaction.request_headers = "Foo: none\r\n"; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 961 | net::CapturingBoundNetLog log; |
| 962 | net::LoadTimingInfo load_timing_info; |
| 963 | RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 964 | &load_timing_info); |
[email protected] | 528e778 | 2012-11-16 22:36:17 | [diff] [blame] | 965 | |
| 966 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 967 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 968 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 969 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 528e778 | 2012-11-16 22:36:17 | [diff] [blame] | 970 | RemoveMockTransaction(&transaction); |
| 971 | } |
| 972 | |
[email protected] | 2ef5d00e | 2013-03-23 16:17:27 | [diff] [blame] | 973 | // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on |
| 974 | // network success |
| 975 | TEST(HttpCache, SimpleGET_CacheOverride_Network) { |
| 976 | MockHttpCache cache; |
| 977 | |
| 978 | // Prime cache. |
| 979 | MockTransaction transaction(kSimpleGET_Transaction); |
| 980 | transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE; |
| 981 | transaction.response_headers = "Cache-Control: no-cache\n"; |
| 982 | |
| 983 | AddMockTransaction(&transaction); |
| 984 | RunTransactionTest(cache.http_cache(), transaction); |
| 985 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 986 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 987 | RemoveMockTransaction(&transaction); |
| 988 | |
| 989 | // Re-run transaction; make sure the result came from the network, |
| 990 | // not the cache. |
| 991 | transaction.data = "Changed data."; |
| 992 | AddMockTransaction(&transaction); |
| 993 | net::HttpResponseInfo response_info; |
| 994 | RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
| 995 | &response_info); |
| 996 | |
| 997 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 998 | EXPECT_FALSE(response_info.server_data_unavailable); |
[email protected] | 7cf7ccb | 2013-04-20 02:53:08 | [diff] [blame] | 999 | EXPECT_TRUE(response_info.network_accessed); |
[email protected] | 2ef5d00e | 2013-03-23 16:17:27 | [diff] [blame] | 1000 | |
| 1001 | RemoveMockTransaction(&transaction); |
| 1002 | } |
| 1003 | |
| 1004 | // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on |
| 1005 | // offline failure |
| 1006 | TEST(HttpCache, SimpleGET_CacheOverride_Offline) { |
| 1007 | MockHttpCache cache; |
| 1008 | |
| 1009 | // Prime cache. |
| 1010 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1011 | transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE; |
| 1012 | transaction.response_headers = "Cache-Control: no-cache\n"; |
| 1013 | |
| 1014 | AddMockTransaction(&transaction); |
| 1015 | RunTransactionTest(cache.http_cache(), transaction); |
| 1016 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1017 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1018 | RemoveMockTransaction(&transaction); |
| 1019 | |
| 1020 | // Network failure with offline error; should return cache entry above + |
| 1021 | // flag signalling stale data. |
| 1022 | transaction.return_code = net::ERR_NAME_NOT_RESOLVED; |
| 1023 | AddMockTransaction(&transaction); |
| 1024 | |
| 1025 | MockHttpRequest request(transaction); |
| 1026 | net::TestCompletionCallback callback; |
| 1027 | scoped_ptr<net::HttpTransaction> trans; |
| 1028 | int rv = cache.http_cache()->CreateTransaction( |
| 1029 | net::DEFAULT_PRIORITY, &trans, NULL); |
| 1030 | EXPECT_EQ(net::OK, rv); |
| 1031 | ASSERT_TRUE(trans.get()); |
| 1032 | rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
| 1033 | EXPECT_EQ(net::OK, callback.GetResult(rv)); |
| 1034 | |
| 1035 | const net::HttpResponseInfo* response_info = trans->GetResponseInfo(); |
| 1036 | ASSERT_TRUE(response_info); |
| 1037 | EXPECT_TRUE(response_info->server_data_unavailable); |
| 1038 | EXPECT_TRUE(response_info->was_cached); |
[email protected] | 7cf7ccb | 2013-04-20 02:53:08 | [diff] [blame] | 1039 | EXPECT_FALSE(response_info->network_accessed); |
[email protected] | 2ef5d00e | 2013-03-23 16:17:27 | [diff] [blame] | 1040 | ReadAndVerifyTransaction(trans.get(), transaction); |
| 1041 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1042 | |
| 1043 | RemoveMockTransaction(&transaction); |
| 1044 | } |
| 1045 | |
| 1046 | // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on |
[email protected] | 7cf7ccb | 2013-04-20 02:53:08 | [diff] [blame] | 1047 | // non-offline failure. |
[email protected] | 2ef5d00e | 2013-03-23 16:17:27 | [diff] [blame] | 1048 | TEST(HttpCache, SimpleGET_CacheOverride_NonOffline) { |
| 1049 | MockHttpCache cache; |
| 1050 | |
| 1051 | // Prime cache. |
| 1052 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1053 | transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE; |
| 1054 | transaction.response_headers = "Cache-Control: no-cache\n"; |
| 1055 | |
| 1056 | AddMockTransaction(&transaction); |
| 1057 | RunTransactionTest(cache.http_cache(), transaction); |
| 1058 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1059 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1060 | RemoveMockTransaction(&transaction); |
| 1061 | |
| 1062 | // Network failure with non-offline error; should fail with that error. |
| 1063 | transaction.return_code = net::ERR_PROXY_CONNECTION_FAILED; |
| 1064 | AddMockTransaction(&transaction); |
| 1065 | |
| 1066 | net::HttpResponseInfo response_info2; |
| 1067 | RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
| 1068 | &response_info2); |
| 1069 | |
| 1070 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1071 | EXPECT_FALSE(response_info2.server_data_unavailable); |
| 1072 | |
| 1073 | RemoveMockTransaction(&transaction); |
| 1074 | } |
| 1075 | |
[email protected] | 7cf7ccb | 2013-04-20 02:53:08 | [diff] [blame] | 1076 | // Confirm if we have an empty cache, a read is marked as network verified. |
| 1077 | TEST(HttpCache, SimpleGET_NetworkAccessed_Network) { |
| 1078 | MockHttpCache cache; |
| 1079 | |
| 1080 | // write to the cache |
| 1081 | net::HttpResponseInfo response_info; |
| 1082 | RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 1083 | &response_info); |
| 1084 | |
| 1085 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1086 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1087 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1088 | EXPECT_TRUE(response_info.network_accessed); |
| 1089 | } |
| 1090 | |
| 1091 | // Confirm if we have a fresh entry in cache, it isn't marked as |
| 1092 | // network verified. |
| 1093 | TEST(HttpCache, SimpleGET_NetworkAccessed_Cache) { |
| 1094 | MockHttpCache cache; |
| 1095 | |
| 1096 | // Prime cache. |
| 1097 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1098 | |
| 1099 | RunTransactionTest(cache.http_cache(), transaction); |
| 1100 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1101 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1102 | |
| 1103 | // Re-run transaction; make sure we don't mark the network as accessed. |
| 1104 | net::HttpResponseInfo response_info; |
| 1105 | RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
| 1106 | &response_info); |
| 1107 | |
| 1108 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1109 | EXPECT_FALSE(response_info.server_data_unavailable); |
| 1110 | EXPECT_FALSE(response_info.network_accessed); |
| 1111 | } |
| 1112 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1113 | TEST(HttpCache, SimpleGET_LoadBypassCache) { |
| 1114 | MockHttpCache cache; |
| 1115 | |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1116 | // Write to the cache. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1117 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1118 | |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1119 | // Force this transaction to write to the cache again. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1120 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1121 | transaction.load_flags |= net::LOAD_BYPASS_CACHE; |
| 1122 | |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 1123 | net::CapturingBoundNetLog log; |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1124 | |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 1125 | // This prevents a number of write events from being logged. |
| 1126 | log.SetLogLevel(net::NetLog::LOG_BASIC); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1127 | net::LoadTimingInfo load_timing_info; |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 1128 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1129 | // Write to the cache. |
| 1130 | RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 1131 | &load_timing_info); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1132 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1133 | // Check that the NetLog was filled as expected. |
[email protected] | f3da152d | 2012-06-02 01:00:57 | [diff] [blame] | 1134 | net::CapturingNetLog::CapturedEntryList entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1135 | log.GetEntries(&entries); |
| 1136 | |
| 1137 | EXPECT_EQ(8u, entries.size()); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1138 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 1139 | entries, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1140 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 1141 | entries, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1142 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1143 | entries, 2, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1144 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1145 | entries, 3, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1146 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1147 | entries, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1148 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1149 | entries, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1150 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 1151 | entries, 6, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1152 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 1153 | entries, 7, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1154 | |
| 1155 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1156 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1157 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1158 | TestLoadTimingNetworkRequest(load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit) { |
| 1162 | MockHttpCache cache; |
| 1163 | |
| 1164 | // write to the cache |
| 1165 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1166 | |
| 1167 | // force this transaction to write to the cache again |
| 1168 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 1169 | transaction.request_headers = "pragma: no-cache\r\n"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1170 | |
| 1171 | RunTransactionTest(cache.http_cache(), transaction); |
| 1172 | |
| 1173 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1174 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1175 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1176 | } |
| 1177 | |
| 1178 | TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit2) { |
| 1179 | MockHttpCache cache; |
| 1180 | |
| 1181 | // write to the cache |
| 1182 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1183 | |
| 1184 | // force this transaction to write to the cache again |
| 1185 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 1186 | transaction.request_headers = "cache-control: no-cache\r\n"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1187 | |
| 1188 | RunTransactionTest(cache.http_cache(), transaction); |
| 1189 | |
| 1190 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1191 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1192 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1193 | } |
| 1194 | |
| 1195 | TEST(HttpCache, SimpleGET_LoadValidateCache) { |
| 1196 | MockHttpCache cache; |
| 1197 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1198 | // Write to the cache. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1199 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1200 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1201 | // Read from the cache. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1202 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1203 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1204 | // Force this transaction to validate the cache. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1205 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1206 | transaction.load_flags |= net::LOAD_VALIDATE_CACHE; |
| 1207 | |
[email protected] | 7cf7ccb | 2013-04-20 02:53:08 | [diff] [blame] | 1208 | net::HttpResponseInfo response_info; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1209 | net::CapturingBoundNetLog log; |
| 1210 | net::LoadTimingInfo load_timing_info; |
| 1211 | RunTransactionTestWithResponseInfoAndGetTiming( |
| 1212 | cache.http_cache(), transaction, &response_info, log.bound(), |
| 1213 | &load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1214 | |
| 1215 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1216 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1217 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 7cf7ccb | 2013-04-20 02:53:08 | [diff] [blame] | 1218 | EXPECT_TRUE(response_info.network_accessed); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1219 | TestLoadTimingNetworkRequest(load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | TEST(HttpCache, SimpleGET_LoadValidateCache_Implicit) { |
| 1223 | MockHttpCache cache; |
| 1224 | |
| 1225 | // write to the cache |
| 1226 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1227 | |
| 1228 | // read from the cache |
| 1229 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1230 | |
| 1231 | // force this transaction to validate the cache |
| 1232 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 1233 | transaction.request_headers = "cache-control: max-age=0\r\n"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1234 | |
| 1235 | RunTransactionTest(cache.http_cache(), transaction); |
| 1236 | |
| 1237 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1238 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1239 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1240 | } |
| 1241 | |
[email protected] | a3eee21 | 2009-11-05 18:08:58 | [diff] [blame] | 1242 | static void PreserveRequestHeaders_Handler( |
| 1243 | const net::HttpRequestInfo* request, |
| 1244 | std::string* response_status, |
| 1245 | std::string* response_headers, |
| 1246 | std::string* response_data) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 1247 | EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); |
[email protected] | a3eee21 | 2009-11-05 18:08:58 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | // Tests that we don't remove extra headers for simple requests. |
| 1251 | TEST(HttpCache, SimpleGET_PreserveRequestHeaders) { |
| 1252 | MockHttpCache cache; |
| 1253 | |
| 1254 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1255 | transaction.handler = PreserveRequestHeaders_Handler; |
| 1256 | transaction.request_headers = EXTRA_HEADER; |
| 1257 | transaction.response_headers = "Cache-Control: max-age=0\n"; |
| 1258 | AddMockTransaction(&transaction); |
| 1259 | |
| 1260 | // Write, then revalidate the entry. |
| 1261 | RunTransactionTest(cache.http_cache(), transaction); |
| 1262 | RunTransactionTest(cache.http_cache(), transaction); |
| 1263 | |
| 1264 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1265 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1266 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1267 | RemoveMockTransaction(&transaction); |
| 1268 | } |
| 1269 | |
| 1270 | // Tests that we don't remove extra headers for conditionalized requests. |
| 1271 | TEST(HttpCache, ConditionalizedGET_PreserveRequestHeaders) { |
| 1272 | MockHttpCache cache; |
| 1273 | |
| 1274 | // Write to the cache. |
| 1275 | RunTransactionTest(cache.http_cache(), kETagGET_Transaction); |
| 1276 | |
| 1277 | MockTransaction transaction(kETagGET_Transaction); |
| 1278 | transaction.handler = PreserveRequestHeaders_Handler; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 1279 | transaction.request_headers = "If-None-Match: \"foopy\"\r\n" |
[email protected] | a3eee21 | 2009-11-05 18:08:58 | [diff] [blame] | 1280 | EXTRA_HEADER; |
| 1281 | AddMockTransaction(&transaction); |
| 1282 | |
| 1283 | RunTransactionTest(cache.http_cache(), transaction); |
| 1284 | |
| 1285 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1286 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1287 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1288 | RemoveMockTransaction(&transaction); |
| 1289 | } |
| 1290 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1291 | TEST(HttpCache, SimpleGET_ManyReaders) { |
| 1292 | MockHttpCache cache; |
| 1293 | |
| 1294 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1295 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1296 | std::vector<Context*> context_list; |
| 1297 | const int kNumTransactions = 5; |
| 1298 | |
| 1299 | for (int i = 0; i < kNumTransactions; ++i) { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1300 | context_list.push_back(new Context()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1301 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1302 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1303 | c->result = cache.http_cache()->CreateTransaction( |
| 1304 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1305 | EXPECT_EQ(net::OK, c->result); |
[email protected] | fbf5047 | 2010-07-15 22:53:53 | [diff] [blame] | 1306 | EXPECT_EQ(net::LOAD_STATE_IDLE, c->trans->GetLoadState()); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1307 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1308 | c->result = c->trans->Start( |
| 1309 | &request, c->callback.callback(), net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1310 | } |
| 1311 | |
[email protected] | fbf5047 | 2010-07-15 22:53:53 | [diff] [blame] | 1312 | // All requests are waiting for the active entry. |
| 1313 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1314 | Context* c = context_list[i]; |
| 1315 | EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); |
| 1316 | } |
| 1317 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1318 | // Allow all requests to move from the Create queue to the active entry. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 1319 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1320 | |
| 1321 | // The first request should be a writer at this point, and the subsequent |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1322 | // requests should be pending. |
| 1323 | |
| 1324 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1325 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1326 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1327 | |
[email protected] | fbf5047 | 2010-07-15 22:53:53 | [diff] [blame] | 1328 | // All requests depend on the writer, and the writer is between Start and |
| 1329 | // Read, i.e. idle. |
| 1330 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1331 | Context* c = context_list[i]; |
| 1332 | EXPECT_EQ(net::LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1333 | } |
| 1334 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1335 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1336 | Context* c = context_list[i]; |
| 1337 | if (c->result == net::ERR_IO_PENDING) |
| 1338 | c->result = c->callback.WaitForResult(); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1339 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1340 | } |
| 1341 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1342 | // We should not have had to re-open the disk entry |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1343 | |
| 1344 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1345 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1346 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1347 | |
| 1348 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1349 | Context* c = context_list[i]; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1350 | delete c; |
| 1351 | } |
| 1352 | } |
| 1353 | |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1354 | // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. |
| 1355 | // If cancelling a request is racing with another request for the same resource |
| 1356 | // finishing, we have to make sure that we remove both transactions from the |
| 1357 | // entry. |
| 1358 | TEST(HttpCache, SimpleGET_RacingReaders) { |
| 1359 | MockHttpCache cache; |
| 1360 | |
| 1361 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1362 | MockHttpRequest reader_request(kSimpleGET_Transaction); |
| 1363 | reader_request.load_flags = net::LOAD_ONLY_FROM_CACHE; |
| 1364 | |
| 1365 | std::vector<Context*> context_list; |
| 1366 | const int kNumTransactions = 5; |
| 1367 | |
| 1368 | for (int i = 0; i < kNumTransactions; ++i) { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1369 | context_list.push_back(new Context()); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1370 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1371 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1372 | c->result = cache.http_cache()->CreateTransaction( |
| 1373 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1374 | EXPECT_EQ(net::OK, c->result); |
| 1375 | |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1376 | MockHttpRequest* this_request = &request; |
| 1377 | if (i == 1 || i == 2) |
| 1378 | this_request = &reader_request; |
| 1379 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1380 | c->result = c->trans->Start( |
| 1381 | this_request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1382 | } |
| 1383 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1384 | // Allow all requests to move from the Create queue to the active entry. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 1385 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1386 | |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1387 | // The first request should be a writer at this point, and the subsequent |
| 1388 | // requests should be pending. |
| 1389 | |
| 1390 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1391 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1392 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1393 | |
| 1394 | Context* c = context_list[0]; |
| 1395 | ASSERT_EQ(net::ERR_IO_PENDING, c->result); |
| 1396 | c->result = c->callback.WaitForResult(); |
| 1397 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1398 | |
| 1399 | // Now we have 2 active readers and two queued transactions. |
| 1400 | |
[email protected] | fbf5047 | 2010-07-15 22:53:53 | [diff] [blame] | 1401 | EXPECT_EQ(net::LOAD_STATE_IDLE, |
| 1402 | context_list[2]->trans->GetLoadState()); |
| 1403 | EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, |
| 1404 | context_list[3]->trans->GetLoadState()); |
| 1405 | |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1406 | c = context_list[1]; |
| 1407 | ASSERT_EQ(net::ERR_IO_PENDING, c->result); |
| 1408 | c->result = c->callback.WaitForResult(); |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 1409 | if (c->result == net::OK) |
| 1410 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1411 | |
| 1412 | // At this point we have one reader, two pending transactions and a task on |
| 1413 | // the queue to move to the next transaction. Now we cancel the request that |
| 1414 | // is the current reader, and expect the queued task to be able to start the |
| 1415 | // next request. |
| 1416 | |
| 1417 | c = context_list[2]; |
| 1418 | c->trans.reset(); |
| 1419 | |
| 1420 | for (int i = 3; i < kNumTransactions; ++i) { |
| 1421 | Context* c = context_list[i]; |
| 1422 | if (c->result == net::ERR_IO_PENDING) |
| 1423 | c->result = c->callback.WaitForResult(); |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 1424 | if (c->result == net::OK) |
| 1425 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | // We should not have had to re-open the disk entry. |
| 1429 | |
| 1430 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1431 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1432 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1433 | |
| 1434 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1435 | Context* c = context_list[i]; |
| 1436 | delete c; |
| 1437 | } |
| 1438 | } |
| 1439 | |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1440 | // Tests that we can doom an entry with pending transactions and delete one of |
| 1441 | // the pending transactions before the first one completes. |
| 1442 | // See http://code.google.com/p/chromium/issues/detail?id=25588 |
| 1443 | TEST(HttpCache, SimpleGET_DoomWithPending) { |
| 1444 | // We need simultaneous doomed / not_doomed entries so let's use a real cache. |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1445 | MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(1024 * 1024)); |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1446 | |
| 1447 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1448 | MockHttpRequest writer_request(kSimpleGET_Transaction); |
| 1449 | writer_request.load_flags = net::LOAD_BYPASS_CACHE; |
| 1450 | |
| 1451 | ScopedVector<Context> context_list; |
| 1452 | const int kNumTransactions = 4; |
| 1453 | |
| 1454 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1455 | context_list.push_back(new Context()); |
| 1456 | Context* c = context_list[i]; |
| 1457 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1458 | c->result = cache.http_cache()->CreateTransaction( |
| 1459 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1460 | EXPECT_EQ(net::OK, c->result); |
| 1461 | |
| 1462 | MockHttpRequest* this_request = &request; |
| 1463 | if (i == 3) |
| 1464 | this_request = &writer_request; |
| 1465 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1466 | c->result = c->trans->Start( |
| 1467 | this_request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | // The first request should be a writer at this point, and the two subsequent |
| 1471 | // requests should be pending. The last request doomed the first entry. |
| 1472 | |
| 1473 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1474 | |
| 1475 | // Cancel the first queued transaction. |
| 1476 | delete context_list[1]; |
| 1477 | context_list.get()[1] = NULL; |
| 1478 | |
| 1479 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1480 | if (i == 1) |
| 1481 | continue; |
| 1482 | Context* c = context_list[i]; |
| 1483 | ASSERT_EQ(net::ERR_IO_PENDING, c->result); |
| 1484 | c->result = c->callback.WaitForResult(); |
| 1485 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1486 | } |
| 1487 | } |
| 1488 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1489 | // This is a test for http://code.google.com/p/chromium/issues/detail?id=4731. |
| 1490 | // We may attempt to delete an entry synchronously with the act of adding a new |
| 1491 | // transaction to said entry. |
| 1492 | TEST(HttpCache, FastNoStoreGET_DoneWithPending) { |
| 1493 | MockHttpCache cache; |
| 1494 | |
| 1495 | // The headers will be served right from the call to Start() the request. |
| 1496 | MockHttpRequest request(kFastNoStoreGET_Transaction); |
| 1497 | FastTransactionServer request_handler; |
| 1498 | AddMockTransaction(&kFastNoStoreGET_Transaction); |
| 1499 | |
| 1500 | std::vector<Context*> context_list; |
| 1501 | const int kNumTransactions = 3; |
| 1502 | |
| 1503 | for (int i = 0; i < kNumTransactions; ++i) { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1504 | context_list.push_back(new Context()); |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1505 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1506 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1507 | c->result = cache.http_cache()->CreateTransaction( |
| 1508 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1509 | EXPECT_EQ(net::OK, c->result); |
| 1510 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1511 | c->result = c->trans->Start( |
| 1512 | &request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1513 | } |
| 1514 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1515 | // Allow all requests to move from the Create queue to the active entry. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 1516 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1517 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1518 | // The first request should be a writer at this point, and the subsequent |
| 1519 | // requests should be pending. |
| 1520 | |
| 1521 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1522 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1523 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1524 | |
| 1525 | // Now, make sure that the second request asks for the entry not to be stored. |
| 1526 | request_handler.set_no_store(true); |
| 1527 | |
| 1528 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1529 | Context* c = context_list[i]; |
| 1530 | if (c->result == net::ERR_IO_PENDING) |
| 1531 | c->result = c->callback.WaitForResult(); |
| 1532 | ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); |
| 1533 | delete c; |
| 1534 | } |
| 1535 | |
| 1536 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1537 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1538 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1539 | |
| 1540 | RemoveMockTransaction(&kFastNoStoreGET_Transaction); |
| 1541 | } |
| 1542 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1543 | TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { |
| 1544 | MockHttpCache cache; |
| 1545 | |
| 1546 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1547 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1548 | std::vector<Context*> context_list; |
| 1549 | const int kNumTransactions = 2; |
| 1550 | |
| 1551 | for (int i = 0; i < kNumTransactions; ++i) { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1552 | context_list.push_back(new Context()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1553 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1554 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1555 | c->result = cache.http_cache()->CreateTransaction( |
| 1556 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1557 | EXPECT_EQ(net::OK, c->result); |
| 1558 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1559 | c->result = c->trans->Start( |
| 1560 | &request, c->callback.callback(), net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1561 | } |
| 1562 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1563 | // Allow all requests to move from the Create queue to the active entry. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 1564 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1565 | |
| 1566 | // The first request should be a writer at this point, and the subsequent |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1567 | // requests should be pending. |
| 1568 | |
| 1569 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1570 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1571 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1572 | |
| 1573 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1574 | Context* c = context_list[i]; |
| 1575 | if (c->result == net::ERR_IO_PENDING) |
| 1576 | c->result = c->callback.WaitForResult(); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1577 | // Destroy only the first transaction. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1578 | if (i == 0) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1579 | delete c; |
| 1580 | context_list[i] = NULL; |
| 1581 | } |
| 1582 | } |
| 1583 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1584 | // Complete the rest of the transactions. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1585 | for (int i = 1; i < kNumTransactions; ++i) { |
| 1586 | Context* c = context_list[i]; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1587 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1588 | } |
| 1589 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1590 | // We should have had to re-open the disk entry. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1591 | |
| 1592 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1593 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1594 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1595 | |
| 1596 | for (int i = 1; i < kNumTransactions; ++i) { |
| 1597 | Context* c = context_list[i]; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1598 | delete c; |
| 1599 | } |
| 1600 | } |
| 1601 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1602 | // Tests that we can cancel requests that are queued waiting to open the disk |
| 1603 | // cache entry. |
| 1604 | TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) { |
| 1605 | MockHttpCache cache; |
| 1606 | |
| 1607 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1608 | |
| 1609 | std::vector<Context*> context_list; |
| 1610 | const int kNumTransactions = 5; |
| 1611 | |
| 1612 | for (int i = 0; i < kNumTransactions; i++) { |
| 1613 | context_list.push_back(new Context()); |
| 1614 | Context* c = context_list[i]; |
| 1615 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1616 | c->result = cache.http_cache()->CreateTransaction( |
| 1617 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1618 | EXPECT_EQ(net::OK, c->result); |
| 1619 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1620 | c->result = c->trans->Start( |
| 1621 | &request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | // The first request should be creating the disk cache entry and the others |
| 1625 | // should be pending. |
| 1626 | |
| 1627 | EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 1628 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1629 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1630 | |
| 1631 | // Cancel a request from the pending queue. |
| 1632 | delete context_list[3]; |
| 1633 | context_list[3] = NULL; |
| 1634 | |
| 1635 | // Cancel the request that is creating the entry. This will force the pending |
| 1636 | // operations to restart. |
| 1637 | delete context_list[0]; |
| 1638 | context_list[0] = NULL; |
| 1639 | |
| 1640 | // Complete the rest of the transactions. |
| 1641 | for (int i = 1; i < kNumTransactions; i++) { |
| 1642 | Context* c = context_list[i]; |
| 1643 | if (c) { |
| 1644 | c->result = c->callback.GetResult(c->result); |
| 1645 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | // We should have had to re-create the disk entry. |
| 1650 | |
| 1651 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1652 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1653 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1654 | |
| 1655 | for (int i = 1; i < kNumTransactions; ++i) { |
| 1656 | delete context_list[i]; |
| 1657 | } |
| 1658 | } |
| 1659 | |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 1660 | // Tests that we can cancel a single request to open a disk cache entry. |
| 1661 | TEST(HttpCache, SimpleGET_CancelCreate) { |
| 1662 | MockHttpCache cache; |
| 1663 | |
| 1664 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1665 | |
| 1666 | Context* c = new Context(); |
| 1667 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1668 | c->result = cache.http_cache()->CreateTransaction( |
| 1669 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 1670 | EXPECT_EQ(net::OK, c->result); |
| 1671 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1672 | c->result = c->trans->Start( |
| 1673 | &request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 1674 | EXPECT_EQ(net::ERR_IO_PENDING, c->result); |
| 1675 | |
| 1676 | // Release the reference that the mock disk cache keeps for this entry, so |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1677 | // that we test that the http cache handles the cancellation correctly. |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 1678 | cache.disk_cache()->ReleaseAll(); |
| 1679 | delete c; |
| 1680 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 1681 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 1682 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1683 | } |
| 1684 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1685 | // Tests that we delete/create entries even if multiple requests are queued. |
| 1686 | TEST(HttpCache, SimpleGET_ManyWriters_BypassCache) { |
| 1687 | MockHttpCache cache; |
| 1688 | |
| 1689 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1690 | request.load_flags = net::LOAD_BYPASS_CACHE; |
| 1691 | |
| 1692 | std::vector<Context*> context_list; |
| 1693 | const int kNumTransactions = 5; |
| 1694 | |
| 1695 | for (int i = 0; i < kNumTransactions; i++) { |
| 1696 | context_list.push_back(new Context()); |
| 1697 | Context* c = context_list[i]; |
| 1698 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1699 | c->result = cache.http_cache()->CreateTransaction( |
| 1700 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1701 | EXPECT_EQ(net::OK, c->result); |
| 1702 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1703 | c->result = c->trans->Start( |
| 1704 | &request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | // The first request should be deleting the disk cache entry and the others |
| 1708 | // should be pending. |
| 1709 | |
| 1710 | EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 1711 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1712 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 1713 | |
| 1714 | // Complete the transactions. |
| 1715 | for (int i = 0; i < kNumTransactions; i++) { |
| 1716 | Context* c = context_list[i]; |
| 1717 | c->result = c->callback.GetResult(c->result); |
| 1718 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1719 | } |
| 1720 | |
| 1721 | // We should have had to re-create the disk entry multiple times. |
| 1722 | |
| 1723 | EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
| 1724 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1725 | EXPECT_EQ(5, cache.disk_cache()->create_count()); |
| 1726 | |
| 1727 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1728 | delete context_list[i]; |
| 1729 | } |
| 1730 | } |
| 1731 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1732 | TEST(HttpCache, SimpleGET_AbandonedCacheRead) { |
| 1733 | MockHttpCache cache; |
| 1734 | |
| 1735 | // write to the cache |
| 1736 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1737 | |
| 1738 | MockHttpRequest request(kSimpleGET_Transaction); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1739 | net::TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1740 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1741 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1742 | int rv = cache.http_cache()->CreateTransaction( |
| 1743 | net::DEFAULT_PRIORITY, &trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1744 | EXPECT_EQ(net::OK, rv); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1745 | rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1746 | if (rv == net::ERR_IO_PENDING) |
| 1747 | rv = callback.WaitForResult(); |
| 1748 | ASSERT_EQ(net::OK, rv); |
| 1749 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1750 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1751 | rv = trans->Read(buf, 256, callback.callback()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1752 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 1753 | |
| 1754 | // Test that destroying the transaction while it is reading from the cache |
| 1755 | // works properly. |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1756 | trans.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1757 | |
| 1758 | // Make sure we pump any pending events, which should include a call to |
| 1759 | // HttpCache::Transaction::OnCacheReadCompleted. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 1760 | base::MessageLoop::current()->RunUntilIdle(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1761 | } |
| 1762 | |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1763 | // Tests that we can delete the HttpCache and deal with queued transactions |
| 1764 | // ("waiting for the backend" as opposed to Active or Doomed entries). |
| 1765 | TEST(HttpCache, SimpleGET_ManyWriters_DeleteCache) { |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1766 | scoped_ptr<MockHttpCache> cache(new MockHttpCache( |
| 1767 | new MockBackendNoCbFactory())); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1768 | |
| 1769 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1770 | |
| 1771 | std::vector<Context*> context_list; |
| 1772 | const int kNumTransactions = 5; |
| 1773 | |
| 1774 | for (int i = 0; i < kNumTransactions; i++) { |
| 1775 | context_list.push_back(new Context()); |
| 1776 | Context* c = context_list[i]; |
| 1777 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1778 | c->result = cache->http_cache()->CreateTransaction( |
| 1779 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1780 | EXPECT_EQ(net::OK, c->result); |
| 1781 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1782 | c->result = c->trans->Start( |
| 1783 | &request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | // The first request should be creating the disk cache entry and the others |
| 1787 | // should be pending. |
| 1788 | |
| 1789 | EXPECT_EQ(0, cache->network_layer()->transaction_count()); |
| 1790 | EXPECT_EQ(0, cache->disk_cache()->open_count()); |
| 1791 | EXPECT_EQ(0, cache->disk_cache()->create_count()); |
| 1792 | |
| 1793 | cache.reset(); |
| 1794 | |
| 1795 | // There is not much to do with the transactions at this point... they are |
| 1796 | // waiting for a callback that will not fire. |
| 1797 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1798 | delete context_list[i]; |
| 1799 | } |
| 1800 | } |
| 1801 | |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1802 | // Tests that we queue requests when initializing the backend. |
| 1803 | TEST(HttpCache, SimpleGET_WaitForBackend) { |
| 1804 | MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
| 1805 | MockHttpCache cache(factory); |
| 1806 | |
| 1807 | MockHttpRequest request0(kSimpleGET_Transaction); |
| 1808 | MockHttpRequest request1(kTypicalGET_Transaction); |
| 1809 | MockHttpRequest request2(kETagGET_Transaction); |
| 1810 | |
| 1811 | std::vector<Context*> context_list; |
| 1812 | const int kNumTransactions = 3; |
| 1813 | |
| 1814 | for (int i = 0; i < kNumTransactions; i++) { |
| 1815 | context_list.push_back(new Context()); |
| 1816 | Context* c = context_list[i]; |
| 1817 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1818 | c->result = cache.http_cache()->CreateTransaction( |
| 1819 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1820 | EXPECT_EQ(net::OK, c->result); |
| 1821 | } |
| 1822 | |
| 1823 | context_list[0]->result = context_list[0]->trans->Start( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1824 | &request0, context_list[0]->callback.callback(), net::BoundNetLog()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1825 | context_list[1]->result = context_list[1]->trans->Start( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1826 | &request1, context_list[1]->callback.callback(), net::BoundNetLog()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1827 | context_list[2]->result = context_list[2]->trans->Start( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1828 | &request2, context_list[2]->callback.callback(), net::BoundNetLog()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1829 | |
| 1830 | // Just to make sure that everything is still pending. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 1831 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1832 | |
| 1833 | // The first request should be creating the disk cache. |
| 1834 | EXPECT_FALSE(context_list[0]->callback.have_result()); |
| 1835 | |
| 1836 | factory->FinishCreation(); |
| 1837 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 1838 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1839 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1840 | EXPECT_EQ(3, cache.disk_cache()->create_count()); |
| 1841 | |
| 1842 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1843 | EXPECT_TRUE(context_list[i]->callback.have_result()); |
| 1844 | delete context_list[i]; |
| 1845 | } |
| 1846 | } |
| 1847 | |
| 1848 | // Tests that we can cancel requests that are queued waiting for the backend |
| 1849 | // to be initialized. |
| 1850 | TEST(HttpCache, SimpleGET_WaitForBackend_CancelCreate) { |
| 1851 | MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
| 1852 | MockHttpCache cache(factory); |
| 1853 | |
| 1854 | MockHttpRequest request0(kSimpleGET_Transaction); |
| 1855 | MockHttpRequest request1(kTypicalGET_Transaction); |
| 1856 | MockHttpRequest request2(kETagGET_Transaction); |
| 1857 | |
| 1858 | std::vector<Context*> context_list; |
| 1859 | const int kNumTransactions = 3; |
| 1860 | |
| 1861 | for (int i = 0; i < kNumTransactions; i++) { |
| 1862 | context_list.push_back(new Context()); |
| 1863 | Context* c = context_list[i]; |
| 1864 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1865 | c->result = cache.http_cache()->CreateTransaction( |
| 1866 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1867 | EXPECT_EQ(net::OK, c->result); |
| 1868 | } |
| 1869 | |
| 1870 | context_list[0]->result = context_list[0]->trans->Start( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1871 | &request0, context_list[0]->callback.callback(), net::BoundNetLog()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1872 | context_list[1]->result = context_list[1]->trans->Start( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1873 | &request1, context_list[1]->callback.callback(), net::BoundNetLog()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1874 | context_list[2]->result = context_list[2]->trans->Start( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1875 | &request2, context_list[2]->callback.callback(), net::BoundNetLog()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1876 | |
| 1877 | // Just to make sure that everything is still pending. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 1878 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1879 | |
| 1880 | // The first request should be creating the disk cache. |
| 1881 | EXPECT_FALSE(context_list[0]->callback.have_result()); |
| 1882 | |
| 1883 | // Cancel a request from the pending queue. |
| 1884 | delete context_list[1]; |
| 1885 | context_list[1] = NULL; |
| 1886 | |
| 1887 | // Cancel the request that is creating the entry. |
| 1888 | delete context_list[0]; |
| 1889 | context_list[0] = NULL; |
| 1890 | |
| 1891 | // Complete the last transaction. |
| 1892 | factory->FinishCreation(); |
| 1893 | |
| 1894 | context_list[2]->result = |
| 1895 | context_list[2]->callback.GetResult(context_list[2]->result); |
| 1896 | ReadAndVerifyTransaction(context_list[2]->trans.get(), kETagGET_Transaction); |
| 1897 | |
| 1898 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1899 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1900 | |
| 1901 | delete context_list[2]; |
| 1902 | } |
| 1903 | |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1904 | // Tests that we can delete the cache while creating the backend. |
| 1905 | TEST(HttpCache, DeleteCacheWaitingForBackend) { |
| 1906 | MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
| 1907 | scoped_ptr<MockHttpCache> cache(new MockHttpCache(factory)); |
| 1908 | |
| 1909 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1910 | |
| 1911 | scoped_ptr<Context> c(new Context()); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1912 | c->result = cache->http_cache()->CreateTransaction( |
| 1913 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1914 | EXPECT_EQ(net::OK, c->result); |
| 1915 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1916 | c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1917 | |
| 1918 | // Just to make sure that everything is still pending. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 1919 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1920 | |
| 1921 | // The request should be creating the disk cache. |
| 1922 | EXPECT_FALSE(c->callback.have_result()); |
| 1923 | |
| 1924 | // We cannot call FinishCreation because the factory itself will go away with |
| 1925 | // the cache, so grab the callback and attempt to use it. |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 1926 | net::CompletionCallback callback = factory->callback(); |
[email protected] | 4e53c2c | 2010-08-24 18:48:56 | [diff] [blame] | 1927 | disk_cache::Backend** backend = factory->backend(); |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1928 | |
| 1929 | cache.reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 1930 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1931 | |
[email protected] | 4e53c2c | 2010-08-24 18:48:56 | [diff] [blame] | 1932 | *backend = NULL; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 1933 | callback.Run(net::ERR_ABORTED); |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1934 | } |
| 1935 | |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1936 | // Tests that we can delete the cache while creating the backend, from within |
| 1937 | // one of the callbacks. |
| 1938 | TEST(HttpCache, DeleteCacheWaitingForBackend2) { |
| 1939 | MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
| 1940 | MockHttpCache* cache = new MockHttpCache(factory); |
| 1941 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 1942 | DeleteCacheCompletionCallback cb(cache); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1943 | disk_cache::Backend* backend; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 1944 | int rv = cache->http_cache()->GetBackend(&backend, cb.callback()); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1945 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 1946 | |
| 1947 | // Now let's queue a regular transaction |
| 1948 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1949 | |
| 1950 | scoped_ptr<Context> c(new Context()); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1951 | c->result = cache->http_cache()->CreateTransaction( |
| 1952 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1953 | EXPECT_EQ(net::OK, c->result); |
| 1954 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1955 | c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1956 | |
| 1957 | // And another direct backend request. |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 1958 | net::TestCompletionCallback cb2; |
| 1959 | rv = cache->http_cache()->GetBackend(&backend, cb2.callback()); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1960 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 1961 | |
| 1962 | // Just to make sure that everything is still pending. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 1963 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1964 | |
| 1965 | // The request should be queued. |
| 1966 | EXPECT_FALSE(c->callback.have_result()); |
| 1967 | |
| 1968 | // Generate the callback. |
| 1969 | factory->FinishCreation(); |
| 1970 | rv = cb.WaitForResult(); |
| 1971 | |
| 1972 | // The cache should be gone by now. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 1973 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1974 | EXPECT_EQ(net::OK, c->callback.GetResult(c->result)); |
| 1975 | EXPECT_FALSE(cb2.have_result()); |
| 1976 | } |
| 1977 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1978 | TEST(HttpCache, TypicalGET_ConditionalRequest) { |
| 1979 | MockHttpCache cache; |
| 1980 | |
| 1981 | // write to the cache |
| 1982 | RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction); |
| 1983 | |
| 1984 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1985 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1986 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1987 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1988 | // Get the same URL again, but this time we expect it to result |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1989 | // in a conditional request. |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1990 | net::CapturingBoundNetLog log; |
| 1991 | net::LoadTimingInfo load_timing_info; |
| 1992 | RunTransactionTestAndGetTiming(cache.http_cache(), kTypicalGET_Transaction, |
| 1993 | log.bound(), &load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1994 | |
| 1995 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1996 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1997 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1998 | TestLoadTimingNetworkRequest(load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | static void ETagGet_ConditionalRequest_Handler( |
| 2002 | const net::HttpRequestInfo* request, |
| 2003 | std::string* response_status, |
| 2004 | std::string* response_headers, |
| 2005 | std::string* response_data) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2006 | EXPECT_TRUE( |
| 2007 | request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2008 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 2009 | response_headers->assign(kETagGET_Transaction.response_headers); |
| 2010 | response_data->clear(); |
| 2011 | } |
| 2012 | |
| 2013 | TEST(HttpCache, ETagGET_ConditionalRequest_304) { |
| 2014 | MockHttpCache cache; |
| 2015 | |
| 2016 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 2017 | |
| 2018 | // write to the cache |
| 2019 | RunTransactionTest(cache.http_cache(), transaction); |
| 2020 | |
| 2021 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2022 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2023 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2024 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2025 | // Get the same URL again, but this time we expect it to result |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2026 | // in a conditional request. |
| 2027 | transaction.load_flags = net::LOAD_VALIDATE_CACHE; |
| 2028 | transaction.handler = ETagGet_ConditionalRequest_Handler; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2029 | net::CapturingBoundNetLog log; |
| 2030 | net::LoadTimingInfo load_timing_info; |
| 2031 | RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 2032 | &load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2033 | |
| 2034 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2035 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2036 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2037 | TestLoadTimingNetworkRequest(load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2038 | } |
| 2039 | |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2040 | class RevalidationServer { |
| 2041 | public: |
| 2042 | RevalidationServer() { |
| 2043 | s_etag_used_ = false; |
| 2044 | s_last_modified_used_ = false; |
| 2045 | } |
| 2046 | |
| 2047 | bool EtagUsed() { return s_etag_used_; } |
| 2048 | bool LastModifiedUsed() { return s_last_modified_used_; } |
| 2049 | |
| 2050 | static void Handler(const net::HttpRequestInfo* request, |
| 2051 | std::string* response_status, |
| 2052 | std::string* response_headers, |
| 2053 | std::string* response_data); |
| 2054 | |
| 2055 | private: |
| 2056 | static bool s_etag_used_; |
| 2057 | static bool s_last_modified_used_; |
| 2058 | }; |
| 2059 | bool RevalidationServer::s_etag_used_ = false; |
| 2060 | bool RevalidationServer::s_last_modified_used_ = false; |
| 2061 | |
| 2062 | void RevalidationServer::Handler(const net::HttpRequestInfo* request, |
| 2063 | std::string* response_status, |
| 2064 | std::string* response_headers, |
| 2065 | std::string* response_data) { |
| 2066 | if (request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch)) |
| 2067 | s_etag_used_ = true; |
| 2068 | |
| 2069 | if (request->extra_headers.HasHeader( |
| 2070 | net::HttpRequestHeaders::kIfModifiedSince)) { |
| 2071 | s_last_modified_used_ = true; |
| 2072 | } |
| 2073 | |
| 2074 | if (s_etag_used_ || s_last_modified_used_) { |
| 2075 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 2076 | response_headers->assign(kTypicalGET_Transaction.response_headers); |
| 2077 | response_data->clear(); |
| 2078 | } else { |
| 2079 | response_status->assign(kTypicalGET_Transaction.status); |
| 2080 | response_headers->assign(kTypicalGET_Transaction.response_headers); |
| 2081 | response_data->assign(kTypicalGET_Transaction.data); |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | // Tests revalidation after a vary match. |
| 2086 | TEST(HttpCache, SimpleGET_LoadValidateCache_VaryMatch) { |
| 2087 | MockHttpCache cache; |
| 2088 | |
| 2089 | // Write to the cache. |
| 2090 | MockTransaction transaction(kTypicalGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2091 | transaction.request_headers = "Foo: bar\r\n"; |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2092 | transaction.response_headers = |
| 2093 | "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
| 2094 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 2095 | "Etag: \"foopy\"\n" |
| 2096 | "Cache-Control: max-age=0\n" |
| 2097 | "Vary: Foo\n"; |
| 2098 | AddMockTransaction(&transaction); |
| 2099 | RunTransactionTest(cache.http_cache(), transaction); |
| 2100 | |
| 2101 | // Read from the cache. |
| 2102 | RevalidationServer server; |
| 2103 | transaction.handler = server.Handler; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2104 | net::CapturingBoundNetLog log; |
| 2105 | net::LoadTimingInfo load_timing_info; |
| 2106 | RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 2107 | &load_timing_info); |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2108 | |
| 2109 | EXPECT_TRUE(server.EtagUsed()); |
| 2110 | EXPECT_TRUE(server.LastModifiedUsed()); |
| 2111 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2112 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2113 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2114 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2115 | RemoveMockTransaction(&transaction); |
| 2116 | } |
| 2117 | |
| 2118 | // Tests revalidation after a vary mismatch if etag is present. |
| 2119 | TEST(HttpCache, SimpleGET_LoadValidateCache_VaryMismatch) { |
| 2120 | MockHttpCache cache; |
| 2121 | |
| 2122 | // Write to the cache. |
| 2123 | MockTransaction transaction(kTypicalGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2124 | transaction.request_headers = "Foo: bar\r\n"; |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2125 | transaction.response_headers = |
| 2126 | "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
| 2127 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 2128 | "Etag: \"foopy\"\n" |
| 2129 | "Cache-Control: max-age=0\n" |
| 2130 | "Vary: Foo\n"; |
| 2131 | AddMockTransaction(&transaction); |
| 2132 | RunTransactionTest(cache.http_cache(), transaction); |
| 2133 | |
| 2134 | // Read from the cache and revalidate the entry. |
| 2135 | RevalidationServer server; |
| 2136 | transaction.handler = server.Handler; |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2137 | transaction.request_headers = "Foo: none\r\n"; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2138 | net::CapturingBoundNetLog log; |
| 2139 | net::LoadTimingInfo load_timing_info; |
| 2140 | RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 2141 | &load_timing_info); |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2142 | |
| 2143 | EXPECT_TRUE(server.EtagUsed()); |
| 2144 | EXPECT_FALSE(server.LastModifiedUsed()); |
| 2145 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2146 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2147 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2148 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2149 | RemoveMockTransaction(&transaction); |
| 2150 | } |
| 2151 | |
| 2152 | // Tests lack of revalidation after a vary mismatch and no etag. |
| 2153 | TEST(HttpCache, SimpleGET_LoadDontValidateCache_VaryMismatch) { |
| 2154 | MockHttpCache cache; |
| 2155 | |
| 2156 | // Write to the cache. |
| 2157 | MockTransaction transaction(kTypicalGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2158 | transaction.request_headers = "Foo: bar\r\n"; |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2159 | transaction.response_headers = |
| 2160 | "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
| 2161 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 2162 | "Cache-Control: max-age=0\n" |
| 2163 | "Vary: Foo\n"; |
| 2164 | AddMockTransaction(&transaction); |
| 2165 | RunTransactionTest(cache.http_cache(), transaction); |
| 2166 | |
| 2167 | // Read from the cache and don't revalidate the entry. |
| 2168 | RevalidationServer server; |
| 2169 | transaction.handler = server.Handler; |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2170 | transaction.request_headers = "Foo: none\r\n"; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2171 | net::CapturingBoundNetLog log; |
| 2172 | net::LoadTimingInfo load_timing_info; |
| 2173 | RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 2174 | &load_timing_info); |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2175 | |
| 2176 | EXPECT_FALSE(server.EtagUsed()); |
| 2177 | EXPECT_FALSE(server.LastModifiedUsed()); |
| 2178 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2179 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2180 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2181 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2182 | RemoveMockTransaction(&transaction); |
| 2183 | } |
| 2184 | |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 2185 | static void ETagGet_UnconditionalRequest_Handler( |
| 2186 | const net::HttpRequestInfo* request, |
| 2187 | std::string* response_status, |
| 2188 | std::string* response_headers, |
| 2189 | std::string* response_data) { |
| 2190 | EXPECT_FALSE( |
| 2191 | request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch)); |
| 2192 | } |
| 2193 | |
| 2194 | TEST(HttpCache, ETagGET_Http10) { |
| 2195 | MockHttpCache cache; |
| 2196 | |
| 2197 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 2198 | transaction.status = "HTTP/1.0 200 OK"; |
| 2199 | |
| 2200 | // Write to the cache. |
| 2201 | RunTransactionTest(cache.http_cache(), transaction); |
| 2202 | |
| 2203 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2204 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2205 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2206 | |
| 2207 | // Get the same URL again, without generating a conditional request. |
| 2208 | transaction.load_flags = net::LOAD_VALIDATE_CACHE; |
| 2209 | transaction.handler = ETagGet_UnconditionalRequest_Handler; |
| 2210 | RunTransactionTest(cache.http_cache(), transaction); |
| 2211 | |
| 2212 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2213 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2214 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2215 | } |
| 2216 | |
| 2217 | TEST(HttpCache, ETagGET_Http10_Range) { |
| 2218 | MockHttpCache cache; |
| 2219 | |
| 2220 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 2221 | transaction.status = "HTTP/1.0 200 OK"; |
| 2222 | |
| 2223 | // Write to the cache. |
| 2224 | RunTransactionTest(cache.http_cache(), transaction); |
| 2225 | |
| 2226 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2227 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2228 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2229 | |
| 2230 | // Get the same URL again, but use a byte range request. |
| 2231 | transaction.load_flags = net::LOAD_VALIDATE_CACHE; |
| 2232 | transaction.handler = ETagGet_UnconditionalRequest_Handler; |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2233 | transaction.request_headers = "Range: bytes = 5-\r\n"; |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 2234 | RunTransactionTest(cache.http_cache(), transaction); |
| 2235 | |
[email protected] | 4a62071 | 2011-07-22 17:41:09 | [diff] [blame] | 2236 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 2237 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2238 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2239 | } |
| 2240 | |
[email protected] | b7d05ab | 2008-12-09 19:18:41 | [diff] [blame] | 2241 | static void ETagGet_ConditionalRequest_NoStore_Handler( |
| 2242 | const net::HttpRequestInfo* request, |
| 2243 | std::string* response_status, |
| 2244 | std::string* response_headers, |
| 2245 | std::string* response_data) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2246 | EXPECT_TRUE( |
| 2247 | request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch)); |
[email protected] | b7d05ab | 2008-12-09 19:18:41 | [diff] [blame] | 2248 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 2249 | response_headers->assign("Cache-Control: no-store\n"); |
| 2250 | response_data->clear(); |
| 2251 | } |
| 2252 | |
| 2253 | TEST(HttpCache, ETagGET_ConditionalRequest_304_NoStore) { |
| 2254 | MockHttpCache cache; |
| 2255 | |
| 2256 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 2257 | |
| 2258 | // Write to the cache. |
| 2259 | RunTransactionTest(cache.http_cache(), transaction); |
| 2260 | |
| 2261 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2262 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2263 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2264 | |
| 2265 | // Get the same URL again, but this time we expect it to result |
| 2266 | // in a conditional request. |
| 2267 | transaction.load_flags = net::LOAD_VALIDATE_CACHE; |
| 2268 | transaction.handler = ETagGet_ConditionalRequest_NoStore_Handler; |
| 2269 | RunTransactionTest(cache.http_cache(), transaction); |
| 2270 | |
| 2271 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2272 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2273 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2274 | |
| 2275 | ScopedMockTransaction transaction2(kETagGET_Transaction); |
| 2276 | |
| 2277 | // Write to the cache again. This should create a new entry. |
| 2278 | RunTransactionTest(cache.http_cache(), transaction2); |
| 2279 | |
| 2280 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2281 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2282 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2283 | } |
| 2284 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2285 | // Helper that does 4 requests using HttpCache: |
| 2286 | // |
| 2287 | // (1) loads |kUrl| -- expects |net_response_1| to be returned. |
| 2288 | // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned. |
| 2289 | // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to |
| 2290 | // be returned. |
| 2291 | // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be |
| 2292 | // returned. |
| 2293 | static void ConditionalizedRequestUpdatesCacheHelper( |
| 2294 | const Response& net_response_1, |
| 2295 | const Response& net_response_2, |
| 2296 | const Response& cached_response_2, |
| 2297 | const char* extra_request_headers) { |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2298 | MockHttpCache cache; |
| 2299 | |
| 2300 | // The URL we will be requesting. |
| 2301 | const char* kUrl = "http://foobar.com/main.css"; |
| 2302 | |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2303 | // Junk network response. |
| 2304 | static const Response kUnexpectedResponse = { |
| 2305 | "HTTP/1.1 500 Unexpected", |
| 2306 | "Server: unexpected_header", |
| 2307 | "unexpected body" |
| 2308 | }; |
| 2309 | |
| 2310 | // We will control the network layer's responses for |kUrl| using |
| 2311 | // |mock_network_response|. |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 2312 | MockTransaction mock_network_response = { 0 }; |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2313 | mock_network_response.url = kUrl; |
| 2314 | AddMockTransaction(&mock_network_response); |
| 2315 | |
| 2316 | // Request |kUrl| for the first time. It should hit the network and |
| 2317 | // receive |kNetResponse1|, which it saves into the HTTP cache. |
| 2318 | |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 2319 | MockTransaction request = { 0 }; |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2320 | request.url = kUrl; |
| 2321 | request.method = "GET"; |
| 2322 | request.request_headers = ""; |
| 2323 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2324 | net_response_1.AssignTo(&mock_network_response); // Network mock. |
| 2325 | net_response_1.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2326 | |
| 2327 | std::string response_headers; |
| 2328 | RunTransactionTestWithResponse( |
| 2329 | cache.http_cache(), request, &response_headers); |
| 2330 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2331 | EXPECT_EQ(net_response_1.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2332 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2333 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2334 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2335 | |
[email protected] | 6f40bf7 | 2009-07-23 17:52:37 | [diff] [blame] | 2336 | // Request |kUrl| a second time. Now |kNetResponse1| it is in the HTTP |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2337 | // cache, so we don't hit the network. |
| 2338 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2339 | request.load_flags = net::LOAD_ONLY_FROM_CACHE; |
| 2340 | |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2341 | kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2342 | net_response_1.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2343 | |
| 2344 | RunTransactionTestWithResponse( |
| 2345 | cache.http_cache(), request, &response_headers); |
| 2346 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2347 | EXPECT_EQ(net_response_1.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2348 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2349 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2350 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2351 | |
| 2352 | // Request |kUrl| yet again, but this time give the request an |
| 2353 | // "If-Modified-Since" header. This will cause the request to re-hit the |
| 2354 | // network. However now the network response is going to be |
| 2355 | // different -- this simulates a change made to the CSS file. |
| 2356 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2357 | request.request_headers = extra_request_headers; |
| 2358 | request.load_flags = net::LOAD_NORMAL; |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2359 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2360 | net_response_2.AssignTo(&mock_network_response); // Network mock. |
| 2361 | net_response_2.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2362 | |
| 2363 | RunTransactionTestWithResponse( |
| 2364 | cache.http_cache(), request, &response_headers); |
| 2365 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2366 | EXPECT_EQ(net_response_2.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2367 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2368 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2369 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2370 | |
| 2371 | // Finally, request |kUrl| again. This request should be serviced from |
| 2372 | // the cache. Moreover, the value in the cache should be |kNetResponse2| |
| 2373 | // and NOT |kNetResponse1|. The previous step should have replaced the |
| 2374 | // value in the cache with the modified response. |
| 2375 | |
| 2376 | request.request_headers = ""; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2377 | request.load_flags = net::LOAD_ONLY_FROM_CACHE; |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2378 | |
| 2379 | kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2380 | cached_response_2.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2381 | |
| 2382 | RunTransactionTestWithResponse( |
| 2383 | cache.http_cache(), request, &response_headers); |
| 2384 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2385 | EXPECT_EQ(cached_response_2.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2386 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2387 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 2388 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2389 | |
| 2390 | RemoveMockTransaction(&mock_network_response); |
| 2391 | } |
| 2392 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2393 | // Check that when an "if-modified-since" header is attached |
| 2394 | // to the request, the result still updates the cached entry. |
| 2395 | TEST(HttpCache, ConditionalizedRequestUpdatesCache1) { |
| 2396 | // First network response for |kUrl|. |
| 2397 | static const Response kNetResponse1 = { |
| 2398 | "HTTP/1.1 200 OK", |
| 2399 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2400 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2401 | "body1" |
| 2402 | }; |
| 2403 | |
| 2404 | // Second network response for |kUrl|. |
| 2405 | static const Response kNetResponse2 = { |
| 2406 | "HTTP/1.1 200 OK", |
| 2407 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2408 | "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2409 | "body2" |
| 2410 | }; |
| 2411 | |
| 2412 | const char* extra_headers = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2413 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2414 | |
| 2415 | ConditionalizedRequestUpdatesCacheHelper( |
| 2416 | kNetResponse1, kNetResponse2, kNetResponse2, extra_headers); |
| 2417 | } |
| 2418 | |
| 2419 | // Check that when an "if-none-match" header is attached |
| 2420 | // to the request, the result updates the cached entry. |
| 2421 | TEST(HttpCache, ConditionalizedRequestUpdatesCache2) { |
| 2422 | // First network response for |kUrl|. |
| 2423 | static const Response kNetResponse1 = { |
| 2424 | "HTTP/1.1 200 OK", |
| 2425 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2426 | "Etag: \"ETAG1\"\n" |
| 2427 | "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire. |
| 2428 | "body1" |
| 2429 | }; |
| 2430 | |
| 2431 | // Second network response for |kUrl|. |
| 2432 | static const Response kNetResponse2 = { |
| 2433 | "HTTP/1.1 200 OK", |
| 2434 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2435 | "Etag: \"ETAG2\"\n" |
| 2436 | "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire. |
| 2437 | "body2" |
| 2438 | }; |
| 2439 | |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2440 | const char* extra_headers = "If-None-Match: \"ETAG1\"\r\n"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2441 | |
| 2442 | ConditionalizedRequestUpdatesCacheHelper( |
| 2443 | kNetResponse1, kNetResponse2, kNetResponse2, extra_headers); |
| 2444 | } |
| 2445 | |
| 2446 | // Check that when an "if-modified-since" header is attached |
| 2447 | // to a request, the 304 (not modified result) result updates the cached |
| 2448 | // headers, and the 304 response is returned rather than the cached response. |
| 2449 | TEST(HttpCache, ConditionalizedRequestUpdatesCache3) { |
| 2450 | // First network response for |kUrl|. |
| 2451 | static const Response kNetResponse1 = { |
| 2452 | "HTTP/1.1 200 OK", |
| 2453 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2454 | "Server: server1\n" |
| 2455 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2456 | "body1" |
| 2457 | }; |
| 2458 | |
| 2459 | // Second network response for |kUrl|. |
| 2460 | static const Response kNetResponse2 = { |
| 2461 | "HTTP/1.1 304 Not Modified", |
| 2462 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2463 | "Server: server2\n" |
| 2464 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2465 | "" |
| 2466 | }; |
| 2467 | |
| 2468 | static const Response kCachedResponse2 = { |
| 2469 | "HTTP/1.1 200 OK", |
| 2470 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2471 | "Server: server2\n" |
| 2472 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2473 | "body1" |
| 2474 | }; |
| 2475 | |
| 2476 | const char* extra_headers = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2477 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2478 | |
| 2479 | ConditionalizedRequestUpdatesCacheHelper( |
| 2480 | kNetResponse1, kNetResponse2, kCachedResponse2, extra_headers); |
| 2481 | } |
| 2482 | |
| 2483 | // Test that when doing an externally conditionalized if-modified-since |
| 2484 | // and there is no corresponding cache entry, a new cache entry is NOT |
| 2485 | // created (304 response). |
| 2486 | TEST(HttpCache, ConditionalizedRequestUpdatesCache4) { |
| 2487 | MockHttpCache cache; |
| 2488 | |
| 2489 | const char* kUrl = "http://foobar.com/main.css"; |
| 2490 | |
| 2491 | static const Response kNetResponse = { |
| 2492 | "HTTP/1.1 304 Not Modified", |
| 2493 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2494 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2495 | "" |
| 2496 | }; |
| 2497 | |
| 2498 | const char* kExtraRequestHeaders = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2499 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2500 | |
| 2501 | // We will control the network layer's responses for |kUrl| using |
| 2502 | // |mock_network_response|. |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 2503 | MockTransaction mock_network_response = { 0 }; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2504 | mock_network_response.url = kUrl; |
| 2505 | AddMockTransaction(&mock_network_response); |
| 2506 | |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 2507 | MockTransaction request = { 0 }; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2508 | request.url = kUrl; |
| 2509 | request.method = "GET"; |
| 2510 | request.request_headers = kExtraRequestHeaders; |
| 2511 | |
| 2512 | kNetResponse.AssignTo(&mock_network_response); // Network mock. |
| 2513 | kNetResponse.AssignTo(&request); // Expected result. |
| 2514 | |
| 2515 | std::string response_headers; |
| 2516 | RunTransactionTestWithResponse( |
| 2517 | cache.http_cache(), request, &response_headers); |
| 2518 | |
| 2519 | EXPECT_EQ(kNetResponse.status_and_headers(), response_headers); |
| 2520 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2521 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2522 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2523 | |
| 2524 | RemoveMockTransaction(&mock_network_response); |
| 2525 | } |
| 2526 | |
| 2527 | // Test that when doing an externally conditionalized if-modified-since |
| 2528 | // and there is no corresponding cache entry, a new cache entry is NOT |
| 2529 | // created (200 response). |
| 2530 | TEST(HttpCache, ConditionalizedRequestUpdatesCache5) { |
| 2531 | MockHttpCache cache; |
| 2532 | |
| 2533 | const char* kUrl = "http://foobar.com/main.css"; |
| 2534 | |
| 2535 | static const Response kNetResponse = { |
| 2536 | "HTTP/1.1 200 OK", |
| 2537 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2538 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2539 | "foobar!!!" |
| 2540 | }; |
| 2541 | |
| 2542 | const char* kExtraRequestHeaders = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2543 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2544 | |
| 2545 | // We will control the network layer's responses for |kUrl| using |
| 2546 | // |mock_network_response|. |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 2547 | MockTransaction mock_network_response = { 0 }; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2548 | mock_network_response.url = kUrl; |
| 2549 | AddMockTransaction(&mock_network_response); |
| 2550 | |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 2551 | MockTransaction request = { 0 }; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2552 | request.url = kUrl; |
| 2553 | request.method = "GET"; |
| 2554 | request.request_headers = kExtraRequestHeaders; |
| 2555 | |
| 2556 | kNetResponse.AssignTo(&mock_network_response); // Network mock. |
| 2557 | kNetResponse.AssignTo(&request); // Expected result. |
| 2558 | |
| 2559 | std::string response_headers; |
| 2560 | RunTransactionTestWithResponse( |
| 2561 | cache.http_cache(), request, &response_headers); |
| 2562 | |
| 2563 | EXPECT_EQ(kNetResponse.status_and_headers(), response_headers); |
| 2564 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2565 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2566 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2567 | |
| 2568 | RemoveMockTransaction(&mock_network_response); |
| 2569 | } |
| 2570 | |
| 2571 | // Test that when doing an externally conditionalized if-modified-since |
| 2572 | // if the date does not match the cache entry's last-modified date, |
| 2573 | // then we do NOT use the response (304) to update the cache. |
| 2574 | // (the if-modified-since date is 2 days AFTER the cache's modification date). |
| 2575 | TEST(HttpCache, ConditionalizedRequestUpdatesCache6) { |
| 2576 | static const Response kNetResponse1 = { |
| 2577 | "HTTP/1.1 200 OK", |
| 2578 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2579 | "Server: server1\n" |
| 2580 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2581 | "body1" |
| 2582 | }; |
| 2583 | |
| 2584 | // Second network response for |kUrl|. |
| 2585 | static const Response kNetResponse2 = { |
| 2586 | "HTTP/1.1 304 Not Modified", |
| 2587 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2588 | "Server: server2\n" |
| 2589 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2590 | "" |
| 2591 | }; |
| 2592 | |
| 2593 | // This is two days in the future from the original response's last-modified |
| 2594 | // date! |
| 2595 | const char* kExtraRequestHeaders = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2596 | "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2597 | |
| 2598 | ConditionalizedRequestUpdatesCacheHelper( |
| 2599 | kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2600 | } |
| 2601 | |
| 2602 | // Test that when doing an externally conditionalized if-none-match |
| 2603 | // if the etag does not match the cache entry's etag, then we do not use the |
| 2604 | // response (304) to update the cache. |
| 2605 | TEST(HttpCache, ConditionalizedRequestUpdatesCache7) { |
| 2606 | static const Response kNetResponse1 = { |
| 2607 | "HTTP/1.1 200 OK", |
| 2608 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2609 | "Etag: \"Foo1\"\n" |
| 2610 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2611 | "body1" |
| 2612 | }; |
| 2613 | |
| 2614 | // Second network response for |kUrl|. |
| 2615 | static const Response kNetResponse2 = { |
| 2616 | "HTTP/1.1 304 Not Modified", |
| 2617 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2618 | "Etag: \"Foo2\"\n" |
| 2619 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2620 | "" |
| 2621 | }; |
| 2622 | |
| 2623 | // Different etag from original response. |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2624 | const char* kExtraRequestHeaders = "If-None-Match: \"Foo2\"\r\n"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2625 | |
| 2626 | ConditionalizedRequestUpdatesCacheHelper( |
| 2627 | kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2628 | } |
| 2629 | |
[email protected] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2630 | // Test that doing an externally conditionalized request with both if-none-match |
| 2631 | // and if-modified-since updates the cache. |
| 2632 | TEST(HttpCache, ConditionalizedRequestUpdatesCache8) { |
| 2633 | static const Response kNetResponse1 = { |
| 2634 | "HTTP/1.1 200 OK", |
| 2635 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2636 | "Etag: \"Foo1\"\n" |
| 2637 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2638 | "body1" |
| 2639 | }; |
| 2640 | |
| 2641 | // Second network response for |kUrl|. |
| 2642 | static const Response kNetResponse2 = { |
| 2643 | "HTTP/1.1 200 OK", |
| 2644 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2645 | "Etag: \"Foo2\"\n" |
| 2646 | "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2647 | "body2" |
| 2648 | }; |
| 2649 | |
| 2650 | const char* kExtraRequestHeaders = |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 2651 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n" |
| 2652 | "If-None-Match: \"Foo1\"\r\n"; |
[email protected] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2653 | |
| 2654 | ConditionalizedRequestUpdatesCacheHelper( |
| 2655 | kNetResponse1, kNetResponse2, kNetResponse2, kExtraRequestHeaders); |
| 2656 | } |
| 2657 | |
| 2658 | // Test that doing an externally conditionalized request with both if-none-match |
| 2659 | // and if-modified-since does not update the cache with only one match. |
| 2660 | TEST(HttpCache, ConditionalizedRequestUpdatesCache9) { |
| 2661 | static const Response kNetResponse1 = { |
| 2662 | "HTTP/1.1 200 OK", |
| 2663 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2664 | "Etag: \"Foo1\"\n" |
| 2665 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2666 | "body1" |
| 2667 | }; |
| 2668 | |
| 2669 | // Second network response for |kUrl|. |
| 2670 | static const Response kNetResponse2 = { |
| 2671 | "HTTP/1.1 200 OK", |
| 2672 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2673 | "Etag: \"Foo2\"\n" |
| 2674 | "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2675 | "body2" |
| 2676 | }; |
| 2677 | |
| 2678 | // The etag doesn't match what we have stored. |
| 2679 | const char* kExtraRequestHeaders = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2680 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n" |
| 2681 | "If-None-Match: \"Foo2\"\r\n"; |
[email protected] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2682 | |
| 2683 | ConditionalizedRequestUpdatesCacheHelper( |
| 2684 | kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2685 | } |
| 2686 | |
| 2687 | // Test that doing an externally conditionalized request with both if-none-match |
| 2688 | // and if-modified-since does not update the cache with only one match. |
| 2689 | TEST(HttpCache, ConditionalizedRequestUpdatesCache10) { |
| 2690 | static const Response kNetResponse1 = { |
| 2691 | "HTTP/1.1 200 OK", |
| 2692 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2693 | "Etag: \"Foo1\"\n" |
| 2694 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2695 | "body1" |
| 2696 | }; |
| 2697 | |
| 2698 | // Second network response for |kUrl|. |
| 2699 | static const Response kNetResponse2 = { |
| 2700 | "HTTP/1.1 200 OK", |
| 2701 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2702 | "Etag: \"Foo2\"\n" |
| 2703 | "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2704 | "body2" |
| 2705 | }; |
| 2706 | |
| 2707 | // The modification date doesn't match what we have stored. |
| 2708 | const char* kExtraRequestHeaders = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2709 | "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n" |
| 2710 | "If-None-Match: \"Foo1\"\r\n"; |
[email protected] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2711 | |
| 2712 | ConditionalizedRequestUpdatesCacheHelper( |
| 2713 | kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2714 | } |
| 2715 | |
[email protected] | 6f40bf7 | 2009-07-23 17:52:37 | [diff] [blame] | 2716 | TEST(HttpCache, UrlContainingHash) { |
| 2717 | MockHttpCache cache; |
| 2718 | |
| 2719 | // Do a typical GET request -- should write an entry into our cache. |
| 2720 | MockTransaction trans(kTypicalGET_Transaction); |
| 2721 | RunTransactionTest(cache.http_cache(), trans); |
| 2722 | |
| 2723 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2724 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2725 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2726 | |
| 2727 | // Request the same URL, but this time with a reference section (hash). |
| 2728 | // Since the cache key strips the hash sections, this should be a cache hit. |
| 2729 | std::string url_with_hash = std::string(trans.url) + "#multiple#hashes"; |
| 2730 | trans.url = url_with_hash.c_str(); |
| 2731 | trans.load_flags = net::LOAD_ONLY_FROM_CACHE; |
| 2732 | |
| 2733 | RunTransactionTest(cache.http_cache(), trans); |
| 2734 | |
| 2735 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2736 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2737 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2738 | } |
| 2739 | |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2740 | // Tests that we skip the cache for POST requests that do not have an upload |
| 2741 | // identifier. |
| 2742 | TEST(HttpCache, SimplePOST_SkipsCache) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2743 | MockHttpCache cache; |
| 2744 | |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2745 | RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction); |
| 2746 | |
| 2747 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2748 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2749 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2750 | } |
| 2751 | |
| 2752 | TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) { |
| 2753 | MockHttpCache cache; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2754 | |
| 2755 | MockTransaction transaction(kSimplePOST_Transaction); |
| 2756 | transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 2757 | |
| 2758 | MockHttpRequest request(transaction); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2759 | net::TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2760 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 2761 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 2762 | int rv = cache.http_cache()->CreateTransaction( |
| 2763 | net::DEFAULT_PRIORITY, &trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 2764 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 2765 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2766 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2767 | rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2768 | ASSERT_EQ(net::ERR_CACHE_MISS, callback.GetResult(rv)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2769 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 2770 | trans.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2771 | |
| 2772 | EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 2773 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2774 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2775 | } |
| 2776 | |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2777 | TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Hit) { |
| 2778 | MockHttpCache cache; |
| 2779 | |
| 2780 | // Test that we hit the cache for POST requests. |
| 2781 | |
| 2782 | MockTransaction transaction(kSimplePOST_Transaction); |
| 2783 | |
| 2784 | const int64 kUploadId = 1; // Just a dummy value. |
| 2785 | |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 2786 | ScopedVector<net::UploadElementReader> element_readers; |
| 2787 | element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); |
| 2788 | net::UploadDataStream upload_data_stream(&element_readers, kUploadId); |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2789 | MockHttpRequest request(transaction); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2790 | request.upload_data_stream = &upload_data_stream; |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2791 | |
| 2792 | // Populate the cache. |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2793 | RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2794 | |
| 2795 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2796 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2797 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2798 | |
| 2799 | // Load from cache. |
| 2800 | request.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2801 | RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2802 | |
| 2803 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2804 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2805 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2806 | } |
| 2807 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 2808 | // Test that we don't hit the cache for POST requests if there is a byte range. |
| 2809 | TEST(HttpCache, SimplePOST_WithRanges) { |
| 2810 | MockHttpCache cache; |
| 2811 | |
| 2812 | MockTransaction transaction(kSimplePOST_Transaction); |
| 2813 | transaction.request_headers = "Range: bytes = 0-4\r\n"; |
| 2814 | |
| 2815 | const int64 kUploadId = 1; // Just a dummy value. |
| 2816 | |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 2817 | ScopedVector<net::UploadElementReader> element_readers; |
| 2818 | element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); |
| 2819 | net::UploadDataStream upload_data_stream(&element_readers, kUploadId); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2820 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 2821 | MockHttpRequest request(transaction); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2822 | request.upload_data_stream = &upload_data_stream; |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 2823 | |
| 2824 | // Attempt to populate the cache. |
| 2825 | RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
| 2826 | |
| 2827 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2828 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2829 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2830 | } |
| 2831 | |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2832 | // Tests that a POST is cached separately from a previously cached GET. |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 2833 | TEST(HttpCache, SimplePOST_SeparateCache) { |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2834 | MockHttpCache cache; |
| 2835 | |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 2836 | ScopedVector<net::UploadElementReader> element_readers; |
| 2837 | element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); |
| 2838 | net::UploadDataStream upload_data_stream(&element_readers, 1); |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2839 | |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 2840 | MockTransaction transaction(kSimplePOST_Transaction); |
| 2841 | MockHttpRequest req1(transaction); |
| 2842 | req1.upload_data_stream = &upload_data_stream; |
| 2843 | |
| 2844 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 2845 | |
| 2846 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2847 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2848 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2849 | |
| 2850 | transaction.method = "GET"; |
| 2851 | MockHttpRequest req2(transaction); |
| 2852 | |
| 2853 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 2854 | |
| 2855 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2856 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2857 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2858 | } |
| 2859 | |
| 2860 | // Tests that a successful POST invalidates a previously cached GET. |
| 2861 | TEST(HttpCache, SimplePOST_Invalidate_205) { |
| 2862 | MockHttpCache cache; |
| 2863 | |
| 2864 | MockTransaction transaction(kSimpleGET_Transaction); |
| 2865 | AddMockTransaction(&transaction); |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2866 | MockHttpRequest req1(transaction); |
| 2867 | |
| 2868 | // Attempt to populate the cache. |
| 2869 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 2870 | |
| 2871 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2872 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2873 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2874 | |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 2875 | ScopedVector<net::UploadElementReader> element_readers; |
| 2876 | element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); |
| 2877 | net::UploadDataStream upload_data_stream(&element_readers, 1); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2878 | |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2879 | transaction.method = "POST"; |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 2880 | transaction.status = "HTTP/1.1 205 No Content"; |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2881 | MockHttpRequest req2(transaction); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2882 | req2.upload_data_stream = &upload_data_stream; |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2883 | |
| 2884 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 2885 | |
| 2886 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2887 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2888 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 2889 | |
| 2890 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 2891 | |
| 2892 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2893 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2894 | EXPECT_EQ(3, cache.disk_cache()->create_count()); |
| 2895 | RemoveMockTransaction(&transaction); |
| 2896 | } |
| 2897 | |
| 2898 | // Tests that we don't invalidate entries as a result of a failed POST. |
| 2899 | TEST(HttpCache, SimplePOST_DontInvalidate_100) { |
| 2900 | MockHttpCache cache; |
| 2901 | |
| 2902 | MockTransaction transaction(kSimpleGET_Transaction); |
| 2903 | AddMockTransaction(&transaction); |
| 2904 | MockHttpRequest req1(transaction); |
| 2905 | |
| 2906 | // Attempt to populate the cache. |
| 2907 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 2908 | |
| 2909 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2910 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2911 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2912 | |
| 2913 | ScopedVector<net::UploadElementReader> element_readers; |
| 2914 | element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); |
| 2915 | net::UploadDataStream upload_data_stream(&element_readers, 1); |
| 2916 | |
| 2917 | transaction.method = "POST"; |
| 2918 | transaction.status = "HTTP/1.1 100 Continue"; |
| 2919 | MockHttpRequest req2(transaction); |
| 2920 | req2.upload_data_stream = &upload_data_stream; |
| 2921 | |
| 2922 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 2923 | |
| 2924 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2925 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2926 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2927 | |
| 2928 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 2929 | |
| 2930 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2931 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2932 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2933 | RemoveMockTransaction(&transaction); |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2934 | } |
| 2935 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 2936 | // Tests that we do not cache the response of a PUT. |
| 2937 | TEST(HttpCache, SimplePUT_Miss) { |
| 2938 | MockHttpCache cache; |
| 2939 | |
| 2940 | MockTransaction transaction(kSimplePOST_Transaction); |
| 2941 | transaction.method = "PUT"; |
| 2942 | |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 2943 | ScopedVector<net::UploadElementReader> element_readers; |
| 2944 | element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); |
| 2945 | net::UploadDataStream upload_data_stream(&element_readers, 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2946 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 2947 | MockHttpRequest request(transaction); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2948 | request.upload_data_stream = &upload_data_stream; |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 2949 | |
| 2950 | // Attempt to populate the cache. |
| 2951 | RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
| 2952 | |
| 2953 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2954 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2955 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2956 | } |
| 2957 | |
| 2958 | // Tests that we invalidate entries as a result of a PUT. |
| 2959 | TEST(HttpCache, SimplePUT_Invalidate) { |
| 2960 | MockHttpCache cache; |
| 2961 | |
[email protected] | f5648e9 | 2012-08-02 17:13:04 | [diff] [blame] | 2962 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 2963 | MockHttpRequest req1(transaction); |
| 2964 | |
| 2965 | // Attempt to populate the cache. |
| 2966 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 2967 | |
| 2968 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2969 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2970 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2971 | |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 2972 | ScopedVector<net::UploadElementReader> element_readers; |
| 2973 | element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); |
| 2974 | net::UploadDataStream upload_data_stream(&element_readers, 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2975 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 2976 | transaction.method = "PUT"; |
| 2977 | MockHttpRequest req2(transaction); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2978 | req2.upload_data_stream = &upload_data_stream; |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 2979 | |
| 2980 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 2981 | |
| 2982 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2983 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2984 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2985 | |
| 2986 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 2987 | |
| 2988 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2989 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2990 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2991 | } |
| 2992 | |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 2993 | // Tests that we invalidate entries as a result of a PUT. |
| 2994 | TEST(HttpCache, SimplePUT_Invalidate_305) { |
| 2995 | MockHttpCache cache; |
| 2996 | |
| 2997 | MockTransaction transaction(kSimpleGET_Transaction); |
| 2998 | AddMockTransaction(&transaction); |
| 2999 | MockHttpRequest req1(transaction); |
| 3000 | |
| 3001 | // Attempt to populate the cache. |
| 3002 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3003 | |
| 3004 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3005 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3006 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3007 | |
| 3008 | ScopedVector<net::UploadElementReader> element_readers; |
| 3009 | element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); |
| 3010 | net::UploadDataStream upload_data_stream(&element_readers, 0); |
| 3011 | |
| 3012 | transaction.method = "PUT"; |
| 3013 | transaction.status = "HTTP/1.1 305 Use Proxy"; |
| 3014 | MockHttpRequest req2(transaction); |
| 3015 | req2.upload_data_stream = &upload_data_stream; |
| 3016 | |
| 3017 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3018 | |
| 3019 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3020 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3021 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3022 | |
| 3023 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3024 | |
| 3025 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3026 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3027 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3028 | RemoveMockTransaction(&transaction); |
| 3029 | } |
| 3030 | |
| 3031 | // Tests that we don't invalidate entries as a result of a failed PUT. |
| 3032 | TEST(HttpCache, SimplePUT_DontInvalidate_404) { |
| 3033 | MockHttpCache cache; |
| 3034 | |
| 3035 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3036 | AddMockTransaction(&transaction); |
| 3037 | MockHttpRequest req1(transaction); |
| 3038 | |
| 3039 | // Attempt to populate the cache. |
| 3040 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3041 | |
| 3042 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3043 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3044 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3045 | |
| 3046 | ScopedVector<net::UploadElementReader> element_readers; |
| 3047 | element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); |
| 3048 | net::UploadDataStream upload_data_stream(&element_readers, 0); |
| 3049 | |
| 3050 | transaction.method = "PUT"; |
| 3051 | transaction.status = "HTTP/1.1 404 Not Found"; |
| 3052 | MockHttpRequest req2(transaction); |
| 3053 | req2.upload_data_stream = &upload_data_stream; |
| 3054 | |
| 3055 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3056 | |
| 3057 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3058 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3059 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3060 | |
| 3061 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3062 | |
| 3063 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3064 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 3065 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3066 | RemoveMockTransaction(&transaction); |
| 3067 | } |
| 3068 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3069 | // Tests that we do not cache the response of a DELETE. |
| 3070 | TEST(HttpCache, SimpleDELETE_Miss) { |
| 3071 | MockHttpCache cache; |
| 3072 | |
| 3073 | MockTransaction transaction(kSimplePOST_Transaction); |
| 3074 | transaction.method = "DELETE"; |
| 3075 | |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 3076 | ScopedVector<net::UploadElementReader> element_readers; |
| 3077 | element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); |
| 3078 | net::UploadDataStream upload_data_stream(&element_readers, 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3079 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3080 | MockHttpRequest request(transaction); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3081 | request.upload_data_stream = &upload_data_stream; |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3082 | |
| 3083 | // Attempt to populate the cache. |
| 3084 | RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
| 3085 | |
| 3086 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3087 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3088 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3089 | } |
| 3090 | |
| 3091 | // Tests that we invalidate entries as a result of a DELETE. |
| 3092 | TEST(HttpCache, SimpleDELETE_Invalidate) { |
| 3093 | MockHttpCache cache; |
| 3094 | |
[email protected] | f5648e9 | 2012-08-02 17:13:04 | [diff] [blame] | 3095 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3096 | MockHttpRequest req1(transaction); |
| 3097 | |
| 3098 | // Attempt to populate the cache. |
| 3099 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3100 | |
| 3101 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3102 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3103 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3104 | |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 3105 | ScopedVector<net::UploadElementReader> element_readers; |
| 3106 | element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); |
| 3107 | net::UploadDataStream upload_data_stream(&element_readers, 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3108 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3109 | transaction.method = "DELETE"; |
| 3110 | MockHttpRequest req2(transaction); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3111 | req2.upload_data_stream = &upload_data_stream; |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3112 | |
| 3113 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3114 | |
| 3115 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3116 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3117 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3118 | |
| 3119 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3120 | |
| 3121 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3122 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3123 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3124 | } |
| 3125 | |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 3126 | // Tests that we invalidate entries as a result of a DELETE. |
| 3127 | TEST(HttpCache, SimpleDELETE_Invalidate_301) { |
| 3128 | MockHttpCache cache; |
| 3129 | |
| 3130 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3131 | AddMockTransaction(&transaction); |
| 3132 | |
| 3133 | // Attempt to populate the cache. |
| 3134 | RunTransactionTest(cache.http_cache(), transaction); |
| 3135 | |
| 3136 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3137 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3138 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3139 | |
| 3140 | transaction.method = "DELETE"; |
| 3141 | transaction.status = "HTTP/1.1 301 Moved Permanently "; |
| 3142 | |
| 3143 | RunTransactionTest(cache.http_cache(), transaction); |
| 3144 | |
| 3145 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3146 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3147 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3148 | |
| 3149 | transaction.method = "GET"; |
| 3150 | RunTransactionTest(cache.http_cache(), transaction); |
| 3151 | |
| 3152 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3153 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3154 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3155 | RemoveMockTransaction(&transaction); |
| 3156 | } |
| 3157 | |
| 3158 | // Tests that we don't invalidate entries as a result of a failed DELETE. |
| 3159 | TEST(HttpCache, SimpleDELETE_DontInvalidate_416) { |
| 3160 | MockHttpCache cache; |
| 3161 | |
| 3162 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3163 | AddMockTransaction(&transaction); |
| 3164 | |
| 3165 | // Attempt to populate the cache. |
| 3166 | RunTransactionTest(cache.http_cache(), transaction); |
| 3167 | |
| 3168 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3169 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3170 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3171 | |
| 3172 | transaction.method = "DELETE"; |
| 3173 | transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable"; |
| 3174 | |
| 3175 | RunTransactionTest(cache.http_cache(), transaction); |
| 3176 | |
| 3177 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3178 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3179 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3180 | |
| 3181 | transaction.method = "GET"; |
| 3182 | transaction.status = "HTTP/1.1 200 OK"; |
| 3183 | RunTransactionTest(cache.http_cache(), transaction); |
| 3184 | |
| 3185 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3186 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 3187 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3188 | RemoveMockTransaction(&transaction); |
| 3189 | } |
| 3190 | |
[email protected] | 7d84e64 | 2013-04-11 00:04:07 | [diff] [blame] | 3191 | // Tests that we don't invalidate entries after a failed network transaction. |
| 3192 | TEST(HttpCache, SimpleGET_DontInvalidateOnFailure) { |
| 3193 | MockHttpCache cache; |
| 3194 | |
| 3195 | // Populate the cache. |
| 3196 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 3197 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3198 | |
| 3199 | // Fail the network request. |
| 3200 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3201 | transaction.return_code = net::ERR_FAILED; |
| 3202 | transaction.load_flags |= net::LOAD_VALIDATE_CACHE; |
| 3203 | |
| 3204 | AddMockTransaction(&transaction); |
| 3205 | RunTransactionTest(cache.http_cache(), transaction); |
| 3206 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3207 | RemoveMockTransaction(&transaction); |
| 3208 | |
| 3209 | transaction.load_flags = net::LOAD_ONLY_FROM_CACHE; |
| 3210 | transaction.return_code = net::OK; |
| 3211 | AddMockTransaction(&transaction); |
| 3212 | RunTransactionTest(cache.http_cache(), transaction); |
| 3213 | |
| 3214 | // Make sure the transaction didn't reach the network. |
| 3215 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3216 | RemoveMockTransaction(&transaction); |
| 3217 | } |
| 3218 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3219 | TEST(HttpCache, RangeGET_SkipsCache) { |
| 3220 | MockHttpCache cache; |
| 3221 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3222 | // Test that we skip the cache for range GET requests. Eventually, we will |
| 3223 | // want to cache these, but we'll still have cases where skipping the cache |
| 3224 | // makes sense, so we want to make sure that it works properly. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3225 | |
| 3226 | RunTransactionTest(cache.http_cache(), kRangeGET_Transaction); |
| 3227 | |
| 3228 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3229 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3230 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3231 | |
| 3232 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 3233 | transaction.request_headers = "If-None-Match: foo\r\n"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3234 | RunTransactionTest(cache.http_cache(), transaction); |
| 3235 | |
| 3236 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3237 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3238 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3239 | |
[email protected] | 72d1e59 | 2009-03-10 17:39:46 | [diff] [blame] | 3240 | transaction.request_headers = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 3241 | "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3242 | RunTransactionTest(cache.http_cache(), transaction); |
| 3243 | |
| 3244 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3245 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3246 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3247 | } |
| 3248 | |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 3249 | // Test that we skip the cache for range requests that include a validation |
| 3250 | // header. |
| 3251 | TEST(HttpCache, RangeGET_SkipsCache2) { |
| 3252 | MockHttpCache cache; |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 3253 | |
| 3254 | MockTransaction transaction(kRangeGET_Transaction); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3255 | transaction.request_headers = "If-None-Match: foo\r\n" |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3256 | EXTRA_HEADER |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 3257 | "Range: bytes = 40-49\r\n"; |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 3258 | RunTransactionTest(cache.http_cache(), transaction); |
| 3259 | |
| 3260 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3261 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3262 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3263 | |
| 3264 | transaction.request_headers = |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3265 | "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n" |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3266 | EXTRA_HEADER |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 3267 | "Range: bytes = 40-49\r\n"; |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 3268 | RunTransactionTest(cache.http_cache(), transaction); |
| 3269 | |
| 3270 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3271 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3272 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3273 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3274 | transaction.request_headers = "If-Range: bla\r\n" |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3275 | EXTRA_HEADER |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 3276 | "Range: bytes = 40-49\r\n"; |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 3277 | RunTransactionTest(cache.http_cache(), transaction); |
| 3278 | |
| 3279 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3280 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3281 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3282 | } |
| 3283 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3284 | // Tests that receiving 206 for a regular request is handled correctly. |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 3285 | TEST(HttpCache, GET_Crazy206) { |
| 3286 | MockHttpCache cache; |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 3287 | |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 3288 | // Write to the cache. |
| 3289 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3290 | AddMockTransaction(&transaction); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3291 | transaction.request_headers = EXTRA_HEADER; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3292 | transaction.handler = NULL; |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 3293 | RunTransactionTest(cache.http_cache(), transaction); |
| 3294 | |
| 3295 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3296 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3297 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3298 | |
| 3299 | // This should read again from the net. |
| 3300 | RunTransactionTest(cache.http_cache(), transaction); |
| 3301 | |
| 3302 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3303 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3304 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3305 | RemoveMockTransaction(&transaction); |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 3306 | } |
| 3307 | |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 3308 | // Tests that we don't cache partial responses that can't be validated. |
| 3309 | TEST(HttpCache, RangeGET_NoStrongValidators) { |
| 3310 | MockHttpCache cache; |
| 3311 | std::string headers; |
| 3312 | |
| 3313 | // Attempt to write to the cache (40-49). |
| 3314 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3315 | AddMockTransaction(&transaction); |
| 3316 | transaction.response_headers = "Content-Length: 10\n" |
| 3317 | "ETag: w/\"foo\"\n"; |
| 3318 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3319 | |
| 3320 | Verify206Response(headers, 40, 49); |
| 3321 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3322 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3323 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3324 | |
| 3325 | // Now verify that there's no cached data. |
| 3326 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 3327 | &headers); |
| 3328 | |
| 3329 | Verify206Response(headers, 40, 49); |
| 3330 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3331 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3332 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3333 | |
| 3334 | RemoveMockTransaction(&transaction); |
| 3335 | } |
| 3336 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3337 | // Tests that we can cache range requests and fetch random blocks from the |
| 3338 | // cache and the network. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3339 | TEST(HttpCache, RangeGET_OK) { |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3340 | MockHttpCache cache; |
| 3341 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 3342 | std::string headers; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3343 | |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 3344 | // Write to the cache (40-49). |
| 3345 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 3346 | &headers); |
| 3347 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3348 | Verify206Response(headers, 40, 49); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3349 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3350 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3351 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3352 | |
| 3353 | // Read from the cache (40-49). |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 3354 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 3355 | &headers); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3356 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3357 | Verify206Response(headers, 40, 49); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3358 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3359 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3360 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3361 | |
| 3362 | // Make sure we are done with the previous transaction. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 3363 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3364 | |
| 3365 | // Write to the cache (30-39). |
| 3366 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3367 | transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3368 | transaction.data = "rg: 30-39 "; |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 3369 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3370 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3371 | Verify206Response(headers, 30, 39); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3372 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3373 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 3374 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3375 | |
| 3376 | // Make sure we are done with the previous transaction. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 3377 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3378 | |
| 3379 | // Write and read from the cache (20-59). |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3380 | transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3381 | transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3382 | net::CapturingBoundNetLog log; |
| 3383 | net::LoadTimingInfo load_timing_info; |
| 3384 | RunTransactionTestWithResponseAndGetTiming( |
| 3385 | cache.http_cache(), transaction, &headers, log.bound(), |
| 3386 | &load_timing_info); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3387 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3388 | Verify206Response(headers, 20, 59); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3389 | EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3390 | EXPECT_EQ(3, cache.disk_cache()->open_count()); |
| 3391 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3392 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3393 | |
| 3394 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3395 | } |
| 3396 | |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3397 | // Tests that we can cache range requests and fetch random blocks from the |
| 3398 | // cache and the network, with synchronous responses. |
| 3399 | TEST(HttpCache, RangeGET_SyncOK) { |
| 3400 | MockHttpCache cache; |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3401 | |
| 3402 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3403 | transaction.test_mode = TEST_MODE_SYNC_ALL; |
| 3404 | AddMockTransaction(&transaction); |
| 3405 | |
| 3406 | // Write to the cache (40-49). |
| 3407 | std::string headers; |
| 3408 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3409 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3410 | Verify206Response(headers, 40, 49); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3411 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3412 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3413 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3414 | |
| 3415 | // Read from the cache (40-49). |
| 3416 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3417 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3418 | Verify206Response(headers, 40, 49); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3419 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3420 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3421 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3422 | |
| 3423 | // Make sure we are done with the previous transaction. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 3424 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3425 | |
| 3426 | // Write to the cache (30-39). |
| 3427 | transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; |
| 3428 | transaction.data = "rg: 30-39 "; |
| 3429 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3430 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3431 | Verify206Response(headers, 30, 39); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3432 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3433 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3434 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3435 | |
| 3436 | // Make sure we are done with the previous transaction. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 3437 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3438 | |
| 3439 | // Write and read from the cache (20-59). |
| 3440 | transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
| 3441 | transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3442 | net::CapturingBoundNetLog log; |
| 3443 | net::LoadTimingInfo load_timing_info; |
| 3444 | RunTransactionTestWithResponseAndGetTiming( |
| 3445 | cache.http_cache(), transaction, &headers, log.bound(), |
| 3446 | &load_timing_info); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3447 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3448 | Verify206Response(headers, 20, 59); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3449 | EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3450 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 3451 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3452 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 3453 | |
| 3454 | RemoveMockTransaction(&transaction); |
| 3455 | } |
| 3456 | |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3457 | // Tests that we don't revalidate an entry unless we are required to do so. |
| 3458 | TEST(HttpCache, RangeGET_Revalidate1) { |
| 3459 | MockHttpCache cache; |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3460 | std::string headers; |
| 3461 | |
| 3462 | // Write to the cache (40-49). |
| 3463 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3464 | transaction.response_headers = |
| 3465 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 3466 | "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n" // Should never expire. |
| 3467 | "ETag: \"foo\"\n" |
| 3468 | "Accept-Ranges: bytes\n" |
| 3469 | "Content-Length: 10\n"; |
| 3470 | AddMockTransaction(&transaction); |
| 3471 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3472 | |
| 3473 | Verify206Response(headers, 40, 49); |
| 3474 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3475 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3476 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3477 | |
| 3478 | // Read from the cache (40-49). |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3479 | net::CapturingBoundNetLog log; |
| 3480 | net::LoadTimingInfo load_timing_info; |
| 3481 | RunTransactionTestWithResponseAndGetTiming( |
| 3482 | cache.http_cache(), transaction, &headers, log.bound(), |
| 3483 | &load_timing_info); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3484 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3485 | Verify206Response(headers, 40, 49); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3486 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3487 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3488 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3489 | TestLoadTimingCachedResponse(load_timing_info); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3490 | |
| 3491 | // Read again forcing the revalidation. |
| 3492 | transaction.load_flags |= net::LOAD_VALIDATE_CACHE; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3493 | RunTransactionTestWithResponseAndGetTiming( |
| 3494 | cache.http_cache(), transaction, &headers, log.bound(), |
| 3495 | &load_timing_info); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3496 | |
| 3497 | Verify206Response(headers, 40, 49); |
| 3498 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3499 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3500 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3501 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3502 | |
| 3503 | RemoveMockTransaction(&transaction); |
| 3504 | } |
| 3505 | |
| 3506 | // Checks that we revalidate an entry when the headers say so. |
| 3507 | TEST(HttpCache, RangeGET_Revalidate2) { |
| 3508 | MockHttpCache cache; |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3509 | std::string headers; |
| 3510 | |
| 3511 | // Write to the cache (40-49). |
| 3512 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3513 | transaction.response_headers = |
| 3514 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 3515 | "Expires: Sat, 18 Apr 2009 01:10:43 GMT\n" // Expired. |
| 3516 | "ETag: \"foo\"\n" |
| 3517 | "Accept-Ranges: bytes\n" |
| 3518 | "Content-Length: 10\n"; |
| 3519 | AddMockTransaction(&transaction); |
| 3520 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3521 | |
| 3522 | Verify206Response(headers, 40, 49); |
| 3523 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3524 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3525 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3526 | |
| 3527 | // Read from the cache (40-49). |
| 3528 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3529 | Verify206Response(headers, 40, 49); |
| 3530 | |
| 3531 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3532 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3533 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3534 | |
| 3535 | RemoveMockTransaction(&transaction); |
| 3536 | } |
| 3537 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3538 | // Tests that we deal with 304s for range requests. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3539 | TEST(HttpCache, RangeGET_304) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3540 | MockHttpCache cache; |
| 3541 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3542 | std::string headers; |
| 3543 | |
| 3544 | // Write to the cache (40-49). |
| 3545 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 3546 | &headers); |
| 3547 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3548 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3549 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3550 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3551 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3552 | |
| 3553 | // Read from the cache (40-49). |
| 3554 | RangeTransactionServer handler; |
| 3555 | handler.set_not_modified(true); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3556 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3557 | transaction.load_flags |= net::LOAD_VALIDATE_CACHE; |
| 3558 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3559 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3560 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3561 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3562 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3563 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3564 | |
| 3565 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3566 | } |
| 3567 | |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 3568 | // Tests that we deal with 206s when revalidating range requests. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3569 | TEST(HttpCache, RangeGET_ModifiedResult) { |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 3570 | MockHttpCache cache; |
| 3571 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3572 | std::string headers; |
| 3573 | |
| 3574 | // Write to the cache (40-49). |
| 3575 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 3576 | &headers); |
| 3577 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3578 | Verify206Response(headers, 40, 49); |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 3579 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3580 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3581 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3582 | |
| 3583 | // Attempt to read from the cache (40-49). |
| 3584 | RangeTransactionServer handler; |
| 3585 | handler.set_modified(true); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3586 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3587 | transaction.load_flags |= net::LOAD_VALIDATE_CACHE; |
| 3588 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 3589 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3590 | Verify206Response(headers, 40, 49); |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 3591 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3592 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3593 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3594 | |
| 3595 | // And the entry should be gone. |
| 3596 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 3597 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3598 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3599 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3600 | |
| 3601 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3602 | } |
| 3603 | |
[email protected] | 9f03cb7a | 2012-07-30 23:15:20 | [diff] [blame] | 3604 | // Tests that we cache 301s for range requests. |
| 3605 | TEST(HttpCache, RangeGET_301) { |
| 3606 | MockHttpCache cache; |
| 3607 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 3608 | transaction.status = "HTTP/1.1 301 Moved Permanently"; |
| 3609 | transaction.response_headers = "Location: http://www.bar.com/\n"; |
| 3610 | transaction.data = ""; |
| 3611 | transaction.handler = NULL; |
| 3612 | AddMockTransaction(&transaction); |
| 3613 | |
| 3614 | // Write to the cache. |
| 3615 | RunTransactionTest(cache.http_cache(), transaction); |
| 3616 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3617 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3618 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3619 | |
| 3620 | // Read from the cache. |
| 3621 | RunTransactionTest(cache.http_cache(), transaction); |
| 3622 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3623 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3624 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3625 | |
| 3626 | RemoveMockTransaction(&transaction); |
| 3627 | } |
| 3628 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3629 | // Tests that we can cache range requests when the start or end is unknown. |
| 3630 | // 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] | 3631 | TEST(HttpCache, UnknownRangeGET_1) { |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3632 | MockHttpCache cache; |
| 3633 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3634 | std::string headers; |
| 3635 | |
| 3636 | // Write to the cache (70-79). |
| 3637 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3638 | transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3639 | transaction.data = "rg: 70-79 "; |
| 3640 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3641 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3642 | Verify206Response(headers, 70, 79); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3643 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3644 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3645 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3646 | |
| 3647 | // Make sure we are done with the previous transaction. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 3648 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3649 | |
| 3650 | // Write and read from the cache (60-79). |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3651 | transaction.request_headers = "Range: bytes = 60-\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3652 | transaction.data = "rg: 60-69 rg: 70-79 "; |
| 3653 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3654 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3655 | Verify206Response(headers, 60, 79); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3656 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3657 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3658 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3659 | |
| 3660 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3661 | } |
| 3662 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3663 | // Tests that we can cache range requests when the start or end is unknown. |
| 3664 | // We start with one request from a given point, followed by a suffix request. |
| 3665 | // We'll also verify that synchronous cache responses work as intended. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3666 | TEST(HttpCache, UnknownRangeGET_2) { |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3667 | MockHttpCache cache; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3668 | std::string headers; |
| 3669 | |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3670 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3671 | transaction.test_mode = TEST_MODE_SYNC_CACHE_START | |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 3672 | TEST_MODE_SYNC_CACHE_READ | |
| 3673 | TEST_MODE_SYNC_CACHE_WRITE; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3674 | AddMockTransaction(&transaction); |
| 3675 | |
| 3676 | // Write to the cache (70-79). |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3677 | transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3678 | transaction.data = "rg: 70-79 "; |
| 3679 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3680 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3681 | Verify206Response(headers, 70, 79); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3682 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3683 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3684 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3685 | |
| 3686 | // Make sure we are done with the previous transaction. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 3687 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3688 | |
| 3689 | // Write and read from the cache (60-79). |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3690 | transaction.request_headers = "Range: bytes = -20\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3691 | transaction.data = "rg: 60-69 rg: 70-79 "; |
| 3692 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3693 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3694 | Verify206Response(headers, 60, 79); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3695 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3696 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3697 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3698 | |
| 3699 | RemoveMockTransaction(&transaction); |
| 3700 | } |
| 3701 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3702 | // Tests that receiving Not Modified when asking for an open range doesn't mess |
| 3703 | // up things. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3704 | TEST(HttpCache, UnknownRangeGET_304) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3705 | MockHttpCache cache; |
| 3706 | std::string headers; |
| 3707 | |
| 3708 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3709 | AddMockTransaction(&transaction); |
| 3710 | |
| 3711 | RangeTransactionServer handler; |
| 3712 | handler.set_not_modified(true); |
| 3713 | |
| 3714 | // Ask for the end of the file, without knowing the length. |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3715 | transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER; |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 3716 | transaction.data = ""; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3717 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3718 | |
| 3719 | // We just bypass the cache. |
| 3720 | EXPECT_EQ(0U, headers.find("HTTP/1.1 304 Not Modified\n")); |
| 3721 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3722 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3723 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3724 | |
| 3725 | RunTransactionTest(cache.http_cache(), transaction); |
| 3726 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3727 | |
| 3728 | RemoveMockTransaction(&transaction); |
| 3729 | } |
| 3730 | |
| 3731 | // 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] | 3732 | TEST(HttpCache, GET_Previous206) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3733 | MockHttpCache cache; |
| 3734 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3735 | std::string headers; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3736 | net::CapturingBoundNetLog log; |
| 3737 | net::LoadTimingInfo load_timing_info; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3738 | |
| 3739 | // Write to the cache (40-49). |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3740 | RunTransactionTestWithResponseAndGetTiming( |
| 3741 | cache.http_cache(), kRangeGET_TransactionOK, &headers, log.bound(), |
| 3742 | &load_timing_info); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3743 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3744 | Verify206Response(headers, 40, 49); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3745 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3746 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3747 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3748 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3749 | |
| 3750 | // Write and read from the cache (0-79), when not asked for a range. |
| 3751 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3752 | transaction.request_headers = EXTRA_HEADER; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3753 | transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " |
| 3754 | "rg: 50-59 rg: 60-69 rg: 70-79 "; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3755 | RunTransactionTestWithResponseAndGetTiming( |
| 3756 | cache.http_cache(), transaction, &headers, log.bound(), |
| 3757 | &load_timing_info); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3758 | |
| 3759 | EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3760 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3761 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3762 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3763 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 3764 | |
| 3765 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3766 | } |
| 3767 | |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 3768 | // Tests that we can handle non-range requests when we have cached the first |
[email protected] | 06c351a | 2010-12-03 19:11:29 | [diff] [blame] | 3769 | // part of the object and the server replies with 304 (Not Modified). |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 3770 | TEST(HttpCache, GET_Previous206_NotModified) { |
| 3771 | MockHttpCache cache; |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 3772 | |
| 3773 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 3774 | AddMockTransaction(&transaction); |
| 3775 | std::string headers; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3776 | net::CapturingBoundNetLog log; |
| 3777 | net::LoadTimingInfo load_timing_info; |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 3778 | |
| 3779 | // Write to the cache (0-9). |
[email protected] | 06c351a | 2010-12-03 19:11:29 | [diff] [blame] | 3780 | transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; |
| 3781 | transaction.data = "rg: 00-09 "; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3782 | RunTransactionTestWithResponseAndGetTiming( |
| 3783 | cache.http_cache(), transaction, &headers, log.bound(), |
| 3784 | &load_timing_info); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3785 | Verify206Response(headers, 0, 9); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3786 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 06c351a | 2010-12-03 19:11:29 | [diff] [blame] | 3787 | |
| 3788 | // Write to the cache (70-79). |
| 3789 | transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER; |
| 3790 | transaction.data = "rg: 70-79 "; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3791 | RunTransactionTestWithResponseAndGetTiming( |
| 3792 | cache.http_cache(), transaction, &headers, log.bound(), |
| 3793 | &load_timing_info); |
[email protected] | 06c351a | 2010-12-03 19:11:29 | [diff] [blame] | 3794 | Verify206Response(headers, 70, 79); |
| 3795 | |
| 3796 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3797 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 3798 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3799 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 3800 | |
[email protected] | 06c351a | 2010-12-03 19:11:29 | [diff] [blame] | 3801 | // Read from the cache (0-9), write and read from cache (10 - 79). |
| 3802 | transaction.load_flags |= net::LOAD_VALIDATE_CACHE; |
| 3803 | transaction.request_headers = "Foo: bar\r\n" EXTRA_HEADER; |
| 3804 | transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3805 | "rg: 50-59 rg: 60-69 rg: 70-79 "; |
| 3806 | RunTransactionTestWithResponseAndGetTiming( |
| 3807 | cache.http_cache(), transaction, &headers, log.bound(), |
| 3808 | &load_timing_info); |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 3809 | |
| 3810 | EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); |
[email protected] | 06c351a | 2010-12-03 19:11:29 | [diff] [blame] | 3811 | EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
| 3812 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 3813 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3814 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 3815 | |
| 3816 | RemoveMockTransaction(&transaction); |
| 3817 | } |
| 3818 | |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 3819 | // Tests that we can handle a regular request to a sparse entry, that results in |
| 3820 | // new content provided by the server (206). |
| 3821 | TEST(HttpCache, GET_Previous206_NewContent) { |
| 3822 | MockHttpCache cache; |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 3823 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3824 | std::string headers; |
| 3825 | |
| 3826 | // Write to the cache (0-9). |
| 3827 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 3828 | transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; |
| 3829 | transaction.data = "rg: 00-09 "; |
| 3830 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3831 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3832 | Verify206Response(headers, 0, 9); |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 3833 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3834 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3835 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3836 | |
| 3837 | // Now we'll issue a request without any range that should result first in a |
| 3838 | // 206 (when revalidating), and then in a weird standard answer: the test |
| 3839 | // server will not modify the response so we'll get the default range... a |
| 3840 | // real server will answer with 200. |
| 3841 | MockTransaction transaction2(kRangeGET_TransactionOK); |
| 3842 | transaction2.request_headers = EXTRA_HEADER; |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 3843 | transaction2.load_flags |= net::LOAD_VALIDATE_CACHE; |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 3844 | transaction2.data = "Not a range"; |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 3845 | RangeTransactionServer handler; |
| 3846 | handler.set_modified(true); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3847 | net::CapturingBoundNetLog log; |
| 3848 | net::LoadTimingInfo load_timing_info; |
| 3849 | RunTransactionTestWithResponseAndGetTiming( |
| 3850 | cache.http_cache(), transaction2, &headers, log.bound(), |
| 3851 | &load_timing_info); |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 3852 | |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 3853 | EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 3854 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3855 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3856 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3857 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 3858 | |
| 3859 | // Verify that the previous request deleted the entry. |
| 3860 | RunTransactionTest(cache.http_cache(), transaction); |
| 3861 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3862 | |
| 3863 | RemoveMockTransaction(&transaction); |
| 3864 | } |
| 3865 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3866 | // Tests that we can handle cached 206 responses that are not sparse. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3867 | TEST(HttpCache, GET_Previous206_NotSparse) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3868 | MockHttpCache cache; |
| 3869 | |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3870 | // Create a disk cache entry that stores 206 headers while not being sparse. |
| 3871 | disk_cache::Entry* entry; |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 3872 | ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry, |
| 3873 | NULL)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3874 | |
| 3875 | std::string raw_headers(kRangeGET_TransactionOK.status); |
| 3876 | raw_headers.append("\n"); |
| 3877 | raw_headers.append(kRangeGET_TransactionOK.response_headers); |
| 3878 | raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), |
| 3879 | raw_headers.size()); |
| 3880 | |
| 3881 | net::HttpResponseInfo response; |
| 3882 | response.headers = new net::HttpResponseHeaders(raw_headers); |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3883 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3884 | |
| 3885 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500)); |
| 3886 | int len = static_cast<int>(base::strlcpy(buf->data(), |
| 3887 | kRangeGET_TransactionOK.data, 500)); |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 3888 | net::TestCompletionCallback cb; |
| 3889 | int rv = entry->WriteData(1, 0, buf, len, cb.callback(), true); |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3890 | EXPECT_EQ(len, cb.GetResult(rv)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3891 | entry->Close(); |
| 3892 | |
| 3893 | // Now see that we don't use the stored entry. |
| 3894 | std::string headers; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3895 | net::CapturingBoundNetLog log; |
| 3896 | net::LoadTimingInfo load_timing_info; |
| 3897 | RunTransactionTestWithResponseAndGetTiming( |
| 3898 | cache.http_cache(), kSimpleGET_Transaction, &headers, log.bound(), |
| 3899 | &load_timing_info); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3900 | |
| 3901 | // We are expecting a 200. |
| 3902 | std::string expected_headers(kSimpleGET_Transaction.status); |
| 3903 | expected_headers.append("\n"); |
| 3904 | expected_headers.append(kSimpleGET_Transaction.response_headers); |
| 3905 | EXPECT_EQ(expected_headers, headers); |
| 3906 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3907 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3908 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 3909 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3910 | } |
| 3911 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3912 | // Tests that we can handle cached 206 responses that are not sparse. This time |
| 3913 | // we issue a range request and expect to receive a range. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3914 | TEST(HttpCache, RangeGET_Previous206_NotSparse_2) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3915 | MockHttpCache cache; |
| 3916 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3917 | |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3918 | // Create a disk cache entry that stores 206 headers while not being sparse. |
| 3919 | disk_cache::Entry* entry; |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 3920 | ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry, |
| 3921 | NULL)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3922 | |
| 3923 | std::string raw_headers(kRangeGET_TransactionOK.status); |
| 3924 | raw_headers.append("\n"); |
| 3925 | raw_headers.append(kRangeGET_TransactionOK.response_headers); |
| 3926 | raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), |
| 3927 | raw_headers.size()); |
| 3928 | |
| 3929 | net::HttpResponseInfo response; |
| 3930 | response.headers = new net::HttpResponseHeaders(raw_headers); |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3931 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3932 | |
| 3933 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500)); |
| 3934 | int len = static_cast<int>(base::strlcpy(buf->data(), |
| 3935 | kRangeGET_TransactionOK.data, 500)); |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 3936 | net::TestCompletionCallback cb; |
| 3937 | int rv = entry->WriteData(1, 0, buf, len, cb.callback(), true); |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 3938 | EXPECT_EQ(len, cb.GetResult(rv)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3939 | entry->Close(); |
| 3940 | |
| 3941 | // Now see that we don't use the stored entry. |
| 3942 | std::string headers; |
| 3943 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 3944 | &headers); |
| 3945 | |
| 3946 | // We are expecting a 206. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3947 | Verify206Response(headers, 40, 49); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3948 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3949 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3950 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3951 | |
| 3952 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3953 | } |
| 3954 | |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 3955 | // Tests that we can handle cached 206 responses that can't be validated. |
| 3956 | TEST(HttpCache, GET_Previous206_NotValidation) { |
| 3957 | MockHttpCache cache; |
| 3958 | |
| 3959 | // Create a disk cache entry that stores 206 headers. |
| 3960 | disk_cache::Entry* entry; |
| 3961 | ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry, |
| 3962 | NULL)); |
| 3963 | |
| 3964 | // Make sure that the headers cannot be validated with the server. |
| 3965 | std::string raw_headers(kRangeGET_TransactionOK.status); |
| 3966 | raw_headers.append("\n"); |
| 3967 | raw_headers.append("Content-Length: 80\n"); |
| 3968 | raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), |
| 3969 | raw_headers.size()); |
| 3970 | |
| 3971 | net::HttpResponseInfo response; |
| 3972 | response.headers = new net::HttpResponseHeaders(raw_headers); |
| 3973 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); |
| 3974 | |
| 3975 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500)); |
| 3976 | int len = static_cast<int>(base::strlcpy(buf->data(), |
| 3977 | kRangeGET_TransactionOK.data, 500)); |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 3978 | net::TestCompletionCallback cb; |
| 3979 | int rv = entry->WriteData(1, 0, buf, len, cb.callback(), true); |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 3980 | EXPECT_EQ(len, cb.GetResult(rv)); |
| 3981 | entry->Close(); |
| 3982 | |
| 3983 | // Now see that we don't use the stored entry. |
| 3984 | std::string headers; |
| 3985 | RunTransactionTestWithResponse(cache.http_cache(), kSimpleGET_Transaction, |
| 3986 | &headers); |
| 3987 | |
| 3988 | // We are expecting a 200. |
| 3989 | std::string expected_headers(kSimpleGET_Transaction.status); |
| 3990 | expected_headers.append("\n"); |
| 3991 | expected_headers.append(kSimpleGET_Transaction.response_headers); |
| 3992 | EXPECT_EQ(expected_headers, headers); |
| 3993 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3994 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3995 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3996 | } |
| 3997 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3998 | // Tests that we can handle range requests with cached 200 responses. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3999 | TEST(HttpCache, RangeGET_Previous200) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4000 | MockHttpCache cache; |
| 4001 | |
| 4002 | // Store the whole thing with status 200. |
| 4003 | MockTransaction transaction(kTypicalGET_Transaction); |
| 4004 | transaction.url = kRangeGET_TransactionOK.url; |
| 4005 | transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " |
| 4006 | "rg: 50-59 rg: 60-69 rg: 70-79 "; |
| 4007 | AddMockTransaction(&transaction); |
| 4008 | RunTransactionTest(cache.http_cache(), transaction); |
| 4009 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4010 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4011 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4012 | |
| 4013 | RemoveMockTransaction(&transaction); |
| 4014 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4015 | |
| 4016 | // Now see that we use the stored entry. |
| 4017 | std::string headers; |
| 4018 | MockTransaction transaction2(kRangeGET_TransactionOK); |
| 4019 | RangeTransactionServer handler; |
| 4020 | handler.set_not_modified(true); |
| 4021 | RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
| 4022 | |
| 4023 | // We are expecting a 206. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4024 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4025 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4026 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4027 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4028 | |
[email protected] | 8f28d63 | 2009-10-01 22:09:21 | [diff] [blame] | 4029 | // The last transaction has finished so make sure the entry is deactivated. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 4030 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 8f28d63 | 2009-10-01 22:09:21 | [diff] [blame] | 4031 | |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 4032 | // Make a request for an invalid range. |
| 4033 | MockTransaction transaction3(kRangeGET_TransactionOK); |
| 4034 | transaction3.request_headers = "Range: bytes = 80-90\r\n" EXTRA_HEADER; |
[email protected] | 9f03cb7a | 2012-07-30 23:15:20 | [diff] [blame] | 4035 | transaction3.data = transaction.data; |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 4036 | transaction3.load_flags = net::LOAD_PREFERRING_CACHE; |
| 4037 | RunTransactionTestWithResponse(cache.http_cache(), transaction3, &headers); |
| 4038 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
[email protected] | 9f03cb7a | 2012-07-30 23:15:20 | [diff] [blame] | 4039 | EXPECT_EQ(0U, headers.find("HTTP/1.1 200 ")); |
| 4040 | EXPECT_EQ(std::string::npos, headers.find("Content-Range:")); |
| 4041 | EXPECT_EQ(std::string::npos, headers.find("Content-Length: 80")); |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 4042 | |
| 4043 | // Make sure the entry is deactivated. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 4044 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 4045 | |
| 4046 | // Even though the request was invalid, we should have the entry. |
| 4047 | RunTransactionTest(cache.http_cache(), transaction2); |
| 4048 | EXPECT_EQ(3, cache.disk_cache()->open_count()); |
| 4049 | |
| 4050 | // Make sure the entry is deactivated. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 4051 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 4052 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4053 | // Now we should receive a range from the server and drop the stored entry. |
| 4054 | handler.set_not_modified(false); |
| 4055 | transaction2.request_headers = kRangeGET_TransactionOK.request_headers; |
| 4056 | RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4057 | Verify206Response(headers, 40, 49); |
[email protected] | 9f03cb7a | 2012-07-30 23:15:20 | [diff] [blame] | 4058 | EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 4059 | EXPECT_EQ(4, cache.disk_cache()->open_count()); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4060 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4061 | |
| 4062 | RunTransactionTest(cache.http_cache(), transaction2); |
| 4063 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4064 | |
| 4065 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4066 | } |
| 4067 | |
| 4068 | // Tests that we can handle a 200 response when dealing with sparse entries. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 4069 | TEST(HttpCache, RangeRequestResultsIn200) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4070 | MockHttpCache cache; |
| 4071 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4072 | std::string headers; |
| 4073 | |
| 4074 | // Write to the cache (70-79). |
| 4075 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 4076 | transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4077 | transaction.data = "rg: 70-79 "; |
| 4078 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4079 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4080 | Verify206Response(headers, 70, 79); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4081 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4082 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4083 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4084 | |
| 4085 | // Now we'll issue a request that results in a plain 200 response, but to |
| 4086 | // the to the same URL that we used to store sparse data, and making sure |
| 4087 | // that we ask for a range. |
| 4088 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4089 | MockTransaction transaction2(kSimpleGET_Transaction); |
| 4090 | transaction2.url = kRangeGET_TransactionOK.url; |
| 4091 | transaction2.request_headers = kRangeGET_TransactionOK.request_headers; |
| 4092 | AddMockTransaction(&transaction2); |
| 4093 | |
| 4094 | RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
| 4095 | |
| 4096 | std::string expected_headers(kSimpleGET_Transaction.status); |
| 4097 | expected_headers.append("\n"); |
| 4098 | expected_headers.append(kSimpleGET_Transaction.response_headers); |
| 4099 | EXPECT_EQ(expected_headers, headers); |
| 4100 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4101 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4102 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4103 | |
| 4104 | RemoveMockTransaction(&transaction2); |
| 4105 | } |
| 4106 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4107 | // Tests that a range request that falls outside of the size that we know about |
| 4108 | // only deletes the entry if the resource has indeed changed. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 4109 | TEST(HttpCache, RangeGET_MoreThanCurrentSize) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4110 | MockHttpCache cache; |
| 4111 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4112 | std::string headers; |
| 4113 | |
| 4114 | // Write to the cache (40-49). |
| 4115 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 4116 | &headers); |
| 4117 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4118 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4119 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4120 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4121 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4122 | |
| 4123 | // A weird request should not delete this entry. Ask for bytes 120-. |
| 4124 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 4125 | transaction.request_headers = "Range: bytes = 120-\r\n" EXTRA_HEADER; |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 4126 | transaction.data = ""; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4127 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4128 | |
| 4129 | EXPECT_EQ(0U, headers.find("HTTP/1.1 416 ")); |
| 4130 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4131 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4132 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4133 | |
| 4134 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 4135 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 4136 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4137 | |
| 4138 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4139 | } |
| 4140 | |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 4141 | // Tests that we don't delete a sparse entry when we cancel a request. |
| 4142 | TEST(HttpCache, RangeGET_Cancel) { |
| 4143 | MockHttpCache cache; |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 4144 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4145 | |
| 4146 | MockHttpRequest request(kRangeGET_TransactionOK); |
| 4147 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4148 | Context* c = new Context(); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 4149 | int rv = cache.http_cache()->CreateTransaction( |
| 4150 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4151 | EXPECT_EQ(net::OK, rv); |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 4152 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4153 | rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 4154 | if (rv == net::ERR_IO_PENDING) |
| 4155 | rv = c->callback.WaitForResult(); |
| 4156 | |
| 4157 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4158 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4159 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4160 | |
| 4161 | // Make sure that the entry has some data stored. |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 4162 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4163 | rv = c->trans->Read(buf, buf->size(), c->callback.callback()); |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 4164 | if (rv == net::ERR_IO_PENDING) |
| 4165 | rv = c->callback.WaitForResult(); |
| 4166 | EXPECT_EQ(buf->size(), rv); |
| 4167 | |
| 4168 | // Destroy the transaction. |
| 4169 | delete c; |
| 4170 | |
| 4171 | // Verify that the entry has not been deleted. |
| 4172 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4173 | ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 4174 | entry->Close(); |
| 4175 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4176 | } |
| 4177 | |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 4178 | // Tests that we don't delete a sparse entry when we start a new request after |
| 4179 | // cancelling the previous one. |
| 4180 | TEST(HttpCache, RangeGET_Cancel2) { |
| 4181 | MockHttpCache cache; |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 4182 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4183 | |
| 4184 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 4185 | MockHttpRequest request(kRangeGET_TransactionOK); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4186 | request.load_flags |= net::LOAD_VALIDATE_CACHE; |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 4187 | |
| 4188 | Context* c = new Context(); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 4189 | int rv = cache.http_cache()->CreateTransaction( |
| 4190 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 4191 | EXPECT_EQ(net::OK, rv); |
| 4192 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4193 | rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 4194 | if (rv == net::ERR_IO_PENDING) |
| 4195 | rv = c->callback.WaitForResult(); |
| 4196 | |
| 4197 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4198 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4199 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4200 | |
| 4201 | // Make sure that we revalidate the entry and read from the cache (a single |
| 4202 | // read will return while waiting for the network). |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 4203 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(5)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4204 | rv = c->trans->Read(buf, buf->size(), c->callback.callback()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4205 | EXPECT_EQ(5, c->callback.GetResult(rv)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4206 | rv = c->trans->Read(buf, buf->size(), c->callback.callback()); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 4207 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 4208 | |
| 4209 | // Destroy the transaction before completing the read. |
| 4210 | delete c; |
| 4211 | |
| 4212 | // We have the read and the delete (OnProcessPendingQueue) waiting on the |
| 4213 | // message loop. This means that a new transaction will just reuse the same |
| 4214 | // active entry (no open or create). |
| 4215 | |
| 4216 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 4217 | |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4218 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 4219 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4220 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4221 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4222 | } |
| 4223 | |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 4224 | // A slight variation of the previous test, this time we cancel two requests in |
| 4225 | // a row, making sure that the second is waiting for the entry to be ready. |
| 4226 | TEST(HttpCache, RangeGET_Cancel3) { |
| 4227 | MockHttpCache cache; |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 4228 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4229 | |
| 4230 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 4231 | MockHttpRequest request(kRangeGET_TransactionOK); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4232 | request.load_flags |= net::LOAD_VALIDATE_CACHE; |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 4233 | |
| 4234 | Context* c = new Context(); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 4235 | int rv = cache.http_cache()->CreateTransaction( |
| 4236 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 4237 | EXPECT_EQ(net::OK, rv); |
| 4238 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4239 | rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 4240 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 4241 | rv = c->callback.WaitForResult(); |
| 4242 | |
| 4243 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4244 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4245 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4246 | |
| 4247 | // Make sure that we revalidate the entry and read from the cache (a single |
| 4248 | // read will return while waiting for the network). |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 4249 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(5)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4250 | rv = c->trans->Read(buf, buf->size(), c->callback.callback()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4251 | EXPECT_EQ(5, c->callback.GetResult(rv)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4252 | rv = c->trans->Read(buf, buf->size(), c->callback.callback()); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 4253 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 4254 | |
| 4255 | // Destroy the transaction before completing the read. |
| 4256 | delete c; |
| 4257 | |
| 4258 | // We have the read and the delete (OnProcessPendingQueue) waiting on the |
| 4259 | // message loop. This means that a new transaction will just reuse the same |
| 4260 | // active entry (no open or create). |
| 4261 | |
| 4262 | c = new Context(); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 4263 | rv = cache.http_cache()->CreateTransaction( |
| 4264 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 4265 | EXPECT_EQ(net::OK, rv); |
| 4266 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4267 | rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 4268 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 4269 | |
| 4270 | MockDiskEntry::IgnoreCallbacks(true); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 4271 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 4272 | MockDiskEntry::IgnoreCallbacks(false); |
| 4273 | |
| 4274 | // The new transaction is waiting for the query range callback. |
| 4275 | delete c; |
| 4276 | |
| 4277 | // And we should not crash when the callback is delivered. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 4278 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 4279 | |
| 4280 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4281 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4282 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4283 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4284 | } |
| 4285 | |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4286 | // Tests that an invalid range response results in no cached entry. |
| 4287 | TEST(HttpCache, RangeGET_InvalidResponse1) { |
| 4288 | MockHttpCache cache; |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4289 | std::string headers; |
| 4290 | |
| 4291 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4292 | transaction.handler = NULL; |
| 4293 | transaction.response_headers = "Content-Range: bytes 40-49/45\n" |
| 4294 | "Content-Length: 10\n"; |
| 4295 | AddMockTransaction(&transaction); |
| 4296 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4297 | |
| 4298 | std::string expected(transaction.status); |
| 4299 | expected.append("\n"); |
| 4300 | expected.append(transaction.response_headers); |
| 4301 | EXPECT_EQ(expected, headers); |
| 4302 | |
| 4303 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4304 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4305 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4306 | |
| 4307 | // Verify that we don't have a cached entry. |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 4308 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4309 | EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4310 | |
| 4311 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4312 | } |
| 4313 | |
| 4314 | // Tests that we reject a range that doesn't match the content-length. |
| 4315 | TEST(HttpCache, RangeGET_InvalidResponse2) { |
| 4316 | MockHttpCache cache; |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4317 | std::string headers; |
| 4318 | |
| 4319 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4320 | transaction.handler = NULL; |
| 4321 | transaction.response_headers = "Content-Range: bytes 40-49/80\n" |
| 4322 | "Content-Length: 20\n"; |
| 4323 | AddMockTransaction(&transaction); |
| 4324 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4325 | |
| 4326 | std::string expected(transaction.status); |
| 4327 | expected.append("\n"); |
| 4328 | expected.append(transaction.response_headers); |
| 4329 | EXPECT_EQ(expected, headers); |
| 4330 | |
| 4331 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4332 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4333 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4334 | |
| 4335 | // Verify that we don't have a cached entry. |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 4336 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4337 | EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4338 | |
| 4339 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4340 | } |
| 4341 | |
| 4342 | // Tests that if a server tells us conflicting information about a resource we |
| 4343 | // ignore the response. |
| 4344 | TEST(HttpCache, RangeGET_InvalidResponse3) { |
| 4345 | MockHttpCache cache; |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4346 | std::string headers; |
| 4347 | |
| 4348 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4349 | transaction.handler = NULL; |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 4350 | transaction.request_headers = "Range: bytes = 50-59\r\n" EXTRA_HEADER; |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4351 | std::string response_headers(transaction.response_headers); |
| 4352 | response_headers.append("Content-Range: bytes 50-59/160\n"); |
| 4353 | transaction.response_headers = response_headers.c_str(); |
| 4354 | AddMockTransaction(&transaction); |
| 4355 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4356 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4357 | Verify206Response(headers, 50, 59); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4358 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4359 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4360 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4361 | |
| 4362 | RemoveMockTransaction(&transaction); |
| 4363 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4364 | |
| 4365 | // This transaction will report a resource size of 80 bytes, and we think it's |
| 4366 | // 160 so we should ignore the response. |
| 4367 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 4368 | &headers); |
| 4369 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4370 | Verify206Response(headers, 40, 49); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4371 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4372 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4373 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4374 | |
| 4375 | // Verify that we cached the first response but not the second one. |
| 4376 | disk_cache::Entry* en; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4377 | ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &en)); |
[email protected] | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 4378 | |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4379 | int64 cached_start = 0; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 4380 | net::TestCompletionCallback cb; |
| 4381 | int rv = en->GetAvailableRange(40, 20, &cached_start, cb.callback()); |
[email protected] | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 4382 | EXPECT_EQ(10, cb.GetResult(rv)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4383 | EXPECT_EQ(50, cached_start); |
| 4384 | en->Close(); |
| 4385 | |
| 4386 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4387 | } |
| 4388 | |
| 4389 | // Tests that we handle large range values properly. |
| 4390 | TEST(HttpCache, RangeGET_LargeValues) { |
| 4391 | // We need a real sparse cache for this test. |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 4392 | MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(1024 * 1024)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4393 | std::string headers; |
| 4394 | |
| 4395 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4396 | transaction.handler = NULL; |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 4397 | transaction.request_headers = "Range: bytes = 4294967288-4294967297\r\n" |
| 4398 | EXTRA_HEADER; |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4399 | transaction.response_headers = |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 4400 | "ETag: \"foo\"\n" |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4401 | "Content-Range: bytes 4294967288-4294967297/4294967299\n" |
| 4402 | "Content-Length: 10\n"; |
| 4403 | AddMockTransaction(&transaction); |
| 4404 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4405 | |
| 4406 | std::string expected(transaction.status); |
| 4407 | expected.append("\n"); |
| 4408 | expected.append(transaction.response_headers); |
| 4409 | EXPECT_EQ(expected, headers); |
| 4410 | |
| 4411 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4412 | |
| 4413 | // Verify that we have a cached entry. |
| 4414 | disk_cache::Entry* en; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4415 | ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &en)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 4416 | en->Close(); |
| 4417 | |
| 4418 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4419 | } |
| 4420 | |
[email protected] | 93e7844 | 2009-10-27 04:46:32 | [diff] [blame] | 4421 | // Tests that we don't crash with a range request if the disk cache was not |
| 4422 | // initialized properly. |
| 4423 | TEST(HttpCache, RangeGET_NoDiskCache) { |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 4424 | MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
| 4425 | factory->set_fail(true); |
| 4426 | factory->FinishCreation(); // We'll complete synchronously. |
| 4427 | MockHttpCache cache(factory); |
| 4428 | |
[email protected] | 93e7844 | 2009-10-27 04:46:32 | [diff] [blame] | 4429 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4430 | |
| 4431 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 4432 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4433 | |
| 4434 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4435 | } |
| 4436 | |
[email protected] | 793618a | 2009-11-03 23:08:12 | [diff] [blame] | 4437 | // Tests that we handle byte range requests that skip the cache. |
| 4438 | TEST(HttpCache, RangeHEAD) { |
| 4439 | MockHttpCache cache; |
[email protected] | 793618a | 2009-11-03 23:08:12 | [diff] [blame] | 4440 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4441 | |
| 4442 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4443 | transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; |
| 4444 | transaction.method = "HEAD"; |
| 4445 | transaction.data = "rg: 70-79 "; |
| 4446 | |
| 4447 | std::string headers; |
| 4448 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4449 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4450 | Verify206Response(headers, 70, 79); |
[email protected] | 793618a | 2009-11-03 23:08:12 | [diff] [blame] | 4451 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4452 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4453 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 4454 | |
| 4455 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4456 | } |
| 4457 | |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 4458 | // Tests that we don't crash when after reading from the cache we issue a |
| 4459 | // request for the next range and the server gives us a 200 synchronously. |
| 4460 | TEST(HttpCache, RangeGET_FastFlakyServer) { |
| 4461 | MockHttpCache cache; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 4462 | |
| 4463 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4464 | transaction.request_headers = "Range: bytes = 40-\r\n" EXTRA_HEADER; |
| 4465 | transaction.test_mode = TEST_MODE_SYNC_NET_START; |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4466 | transaction.load_flags |= net::LOAD_VALIDATE_CACHE; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 4467 | AddMockTransaction(&transaction); |
| 4468 | |
| 4469 | // Write to the cache. |
| 4470 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 4471 | |
| 4472 | // And now read from the cache and the network. |
| 4473 | RangeTransactionServer handler; |
| 4474 | handler.set_bad_200(true); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 4475 | transaction.data = "Not a range"; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 4476 | RunTransactionTest(cache.http_cache(), transaction); |
| 4477 | |
| 4478 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 4479 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4480 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4481 | |
| 4482 | RemoveMockTransaction(&transaction); |
| 4483 | } |
| 4484 | |
[email protected] | c14117b9 | 2010-01-21 19:22:57 | [diff] [blame] | 4485 | // Tests that when the server gives us less data than expected, we don't keep |
| 4486 | // asking for more data. |
| 4487 | TEST(HttpCache, RangeGET_FastFlakyServer2) { |
| 4488 | MockHttpCache cache; |
[email protected] | c14117b9 | 2010-01-21 19:22:57 | [diff] [blame] | 4489 | |
| 4490 | // First, check with an empty cache (WRITE mode). |
| 4491 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4492 | transaction.request_headers = "Range: bytes = 40-49\r\n" EXTRA_HEADER; |
| 4493 | transaction.data = "rg: 40-"; // Less than expected. |
| 4494 | transaction.handler = NULL; |
| 4495 | std::string headers(transaction.response_headers); |
| 4496 | headers.append("Content-Range: bytes 40-49/80\n"); |
| 4497 | transaction.response_headers = headers.c_str(); |
| 4498 | |
| 4499 | AddMockTransaction(&transaction); |
| 4500 | |
| 4501 | // Write to the cache. |
| 4502 | RunTransactionTest(cache.http_cache(), transaction); |
| 4503 | |
| 4504 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4505 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4506 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4507 | |
| 4508 | // Now verify that even in READ_WRITE mode, we forward the bad response to |
| 4509 | // the caller. |
| 4510 | transaction.request_headers = "Range: bytes = 60-69\r\n" EXTRA_HEADER; |
| 4511 | transaction.data = "rg: 60-"; // Less than expected. |
| 4512 | headers = kRangeGET_TransactionOK.response_headers; |
| 4513 | headers.append("Content-Range: bytes 60-69/80\n"); |
| 4514 | transaction.response_headers = headers.c_str(); |
| 4515 | |
| 4516 | RunTransactionTest(cache.http_cache(), transaction); |
| 4517 | |
| 4518 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4519 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4520 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4521 | |
| 4522 | RemoveMockTransaction(&transaction); |
| 4523 | } |
| 4524 | |
[email protected] | 20960e07 | 2011-09-20 20:59:01 | [diff] [blame] | 4525 | #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4526 | // This test hits a NOTREACHED so it is a release mode only test. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 4527 | TEST(HttpCache, RangeGET_OK_LoadOnlyFromCache) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4528 | MockHttpCache cache; |
| 4529 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4530 | |
| 4531 | // Write to the cache (40-49). |
| 4532 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 4533 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4534 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4535 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4536 | |
| 4537 | // Force this transaction to read from the cache. |
| 4538 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4539 | transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 4540 | |
| 4541 | MockHttpRequest request(transaction); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4542 | net::TestCompletionCallback callback; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4543 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4544 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 4545 | int rv = cache.http_cache()->CreateTransaction( |
| 4546 | net::DEFAULT_PRIORITY, &trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4547 | EXPECT_EQ(net::OK, rv); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4548 | ASSERT_TRUE(trans.get()); |
| 4549 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4550 | rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4551 | if (rv == net::ERR_IO_PENDING) |
| 4552 | rv = callback.WaitForResult(); |
| 4553 | ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
| 4554 | |
| 4555 | trans.reset(); |
| 4556 | |
| 4557 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4558 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4559 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4560 | |
| 4561 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4562 | } |
| 4563 | #endif |
| 4564 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4565 | // Tests the handling of the "truncation" flag. |
| 4566 | TEST(HttpCache, WriteResponseInfo_Truncated) { |
| 4567 | MockHttpCache cache; |
| 4568 | disk_cache::Entry* entry; |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 4569 | ASSERT_TRUE(cache.CreateBackendEntry("http://www.google.com", &entry, |
| 4570 | NULL)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4571 | |
| 4572 | std::string headers("HTTP/1.1 200 OK"); |
| 4573 | headers = net::HttpUtil::AssembleRawHeaders(headers.data(), headers.size()); |
| 4574 | net::HttpResponseInfo response; |
| 4575 | response.headers = new net::HttpResponseHeaders(headers); |
| 4576 | |
| 4577 | // Set the last argument for this to be an incomplete request. |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4578 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4579 | bool truncated = false; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4580 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4581 | EXPECT_TRUE(truncated); |
| 4582 | |
| 4583 | // And now test the opposite case. |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4584 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4585 | truncated = true; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4586 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4587 | EXPECT_FALSE(truncated); |
| 4588 | entry->Close(); |
| 4589 | } |
| 4590 | |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 4591 | // Tests basic pickling/unpickling of HttpResponseInfo. |
| 4592 | TEST(HttpCache, PersistHttpResponseInfo) { |
| 4593 | // Set some fields (add more if needed.) |
| 4594 | net::HttpResponseInfo response1; |
| 4595 | response1.was_cached = false; |
| 4596 | response1.socket_address = net::HostPortPair("1.2.3.4", 80); |
| 4597 | response1.headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK"); |
| 4598 | |
| 4599 | // Pickle. |
| 4600 | Pickle pickle; |
| 4601 | response1.Persist(&pickle, false, false); |
| 4602 | |
| 4603 | // Unpickle. |
| 4604 | net::HttpResponseInfo response2; |
| 4605 | bool response_truncated; |
| 4606 | EXPECT_TRUE(response2.InitFromPickle(pickle, &response_truncated)); |
| 4607 | EXPECT_FALSE(response_truncated); |
| 4608 | |
| 4609 | // Verify fields. |
| 4610 | EXPECT_TRUE(response2.was_cached); // InitFromPickle sets this flag. |
| 4611 | EXPECT_EQ("1.2.3.4", response2.socket_address.host()); |
| 4612 | EXPECT_EQ(80, response2.socket_address.port()); |
| 4613 | EXPECT_EQ("HTTP/1.1 200 OK", response2.headers->GetStatusLine()); |
| 4614 | } |
| 4615 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4616 | // Tests that we delete an entry when the request is cancelled before starting |
| 4617 | // to read from the network. |
| 4618 | TEST(HttpCache, DoomOnDestruction) { |
| 4619 | MockHttpCache cache; |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4620 | |
| 4621 | MockHttpRequest request(kSimpleGET_Transaction); |
| 4622 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4623 | Context* c = new Context(); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 4624 | int rv = cache.http_cache()->CreateTransaction( |
| 4625 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4626 | EXPECT_EQ(net::OK, rv); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4627 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4628 | rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4629 | if (rv == net::ERR_IO_PENDING) |
| 4630 | c->result = c->callback.WaitForResult(); |
| 4631 | |
| 4632 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4633 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4634 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4635 | |
| 4636 | // Destroy the transaction. We only have the headers so we should delete this |
| 4637 | // entry. |
| 4638 | delete c; |
| 4639 | |
| 4640 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 4641 | |
| 4642 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4643 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4644 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4645 | } |
| 4646 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4647 | // Tests that we delete an entry when the request is cancelled if the response |
| 4648 | // does not have content-length and strong validators. |
| 4649 | TEST(HttpCache, DoomOnDestruction2) { |
| 4650 | MockHttpCache cache; |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4651 | |
| 4652 | MockHttpRequest request(kSimpleGET_Transaction); |
| 4653 | |
| 4654 | Context* c = new Context(); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 4655 | int rv = cache.http_cache()->CreateTransaction( |
| 4656 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4657 | EXPECT_EQ(net::OK, rv); |
| 4658 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4659 | rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4660 | if (rv == net::ERR_IO_PENDING) |
| 4661 | rv = c->callback.WaitForResult(); |
| 4662 | |
| 4663 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4664 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4665 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4666 | |
| 4667 | // Make sure that the entry has some data stored. |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 4668 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4669 | rv = c->trans->Read(buf, buf->size(), c->callback.callback()); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4670 | if (rv == net::ERR_IO_PENDING) |
| 4671 | rv = c->callback.WaitForResult(); |
| 4672 | EXPECT_EQ(buf->size(), rv); |
| 4673 | |
| 4674 | // Destroy the transaction. |
| 4675 | delete c; |
| 4676 | |
| 4677 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 4678 | |
| 4679 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4680 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4681 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4682 | } |
| 4683 | |
| 4684 | // Tests that we delete an entry when the request is cancelled if the response |
| 4685 | // has an "Accept-Ranges: none" header. |
| 4686 | TEST(HttpCache, DoomOnDestruction3) { |
| 4687 | MockHttpCache cache; |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4688 | |
| 4689 | MockTransaction transaction(kSimpleGET_Transaction); |
| 4690 | transaction.response_headers = |
| 4691 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 4692 | "Content-Length: 22\n" |
| 4693 | "Accept-Ranges: none\n" |
[email protected] | 29cc1ce4 | 2012-07-22 18:39:35 | [diff] [blame] | 4694 | "Etag: \"foopy\"\n"; |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4695 | AddMockTransaction(&transaction); |
| 4696 | MockHttpRequest request(transaction); |
| 4697 | |
| 4698 | Context* c = new Context(); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 4699 | int rv = cache.http_cache()->CreateTransaction( |
| 4700 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4701 | EXPECT_EQ(net::OK, rv); |
| 4702 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4703 | rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4704 | if (rv == net::ERR_IO_PENDING) |
| 4705 | rv = c->callback.WaitForResult(); |
| 4706 | |
| 4707 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4708 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4709 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4710 | |
| 4711 | // Make sure that the entry has some data stored. |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 4712 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4713 | rv = c->trans->Read(buf, buf->size(), c->callback.callback()); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4714 | if (rv == net::ERR_IO_PENDING) |
| 4715 | rv = c->callback.WaitForResult(); |
| 4716 | EXPECT_EQ(buf->size(), rv); |
| 4717 | |
| 4718 | // Destroy the transaction. |
| 4719 | delete c; |
| 4720 | |
| 4721 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 4722 | |
| 4723 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4724 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4725 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4726 | |
| 4727 | RemoveMockTransaction(&transaction); |
| 4728 | } |
| 4729 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4730 | // Tests that we mark an entry as incomplete when the request is cancelled. |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 4731 | TEST(HttpCache, SetTruncatedFlag) { |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4732 | MockHttpCache cache; |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4733 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4734 | MockTransaction transaction(kSimpleGET_Transaction); |
| 4735 | transaction.response_headers = |
| 4736 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 4737 | "Content-Length: 22\n" |
[email protected] | 29cc1ce4 | 2012-07-22 18:39:35 | [diff] [blame] | 4738 | "Etag: \"foopy\"\n"; |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4739 | AddMockTransaction(&transaction); |
| 4740 | MockHttpRequest request(transaction); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4741 | |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 4742 | scoped_ptr<Context> c(new Context()); |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 4743 | // We use a test delegate to ensure that after initiating destruction |
| 4744 | // of the transaction, no further delegate callbacks happen. |
| 4745 | // We initialize the TestHttpTransactionDelegate with the correct number of |
| 4746 | // cache actions and network actions to be reported. |
| 4747 | scoped_ptr<TestHttpTransactionDelegate> delegate( |
| 4748 | new TestHttpTransactionDelegate(7, 3)); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 4749 | int rv = cache.http_cache()->CreateTransaction( |
| 4750 | net::DEFAULT_PRIORITY, &c->trans, delegate.get()); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 4751 | EXPECT_EQ(net::OK, rv); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4752 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4753 | rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4754 | if (rv == net::ERR_IO_PENDING) |
| 4755 | rv = c->callback.WaitForResult(); |
| 4756 | |
| 4757 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4758 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4759 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4760 | |
| 4761 | // Make sure that the entry has some data stored. |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 4762 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4763 | rv = c->trans->Read(buf, buf->size(), c->callback.callback()); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4764 | if (rv == net::ERR_IO_PENDING) |
| 4765 | rv = c->callback.WaitForResult(); |
| 4766 | EXPECT_EQ(buf->size(), rv); |
| 4767 | |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 4768 | // We want to cancel the request when the transaction is busy. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4769 | rv = c->trans->Read(buf, buf->size(), c->callback.callback()); |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 4770 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 4771 | EXPECT_FALSE(c->callback.have_result()); |
| 4772 | |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 4773 | MockHttpCache::SetTestMode(TEST_MODE_SYNC_ALL); |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 4774 | int num_delegate_callbacks_before_destruction = |
| 4775 | delegate->num_callbacks_observed(); |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 4776 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4777 | // Destroy the transaction. |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 4778 | c->trans.reset(); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 4779 | MockHttpCache::SetTestMode(0); |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 4780 | |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 4781 | // Ensure the delegate received no callbacks during destruction. |
| 4782 | EXPECT_EQ(num_delegate_callbacks_before_destruction, |
| 4783 | delegate->num_callbacks_observed()); |
| 4784 | |
| 4785 | // Since the transaction was aborted in the middle of network I/O, we will |
| 4786 | // manually call the delegate so that its pending I/O operation will be |
| 4787 | // closed (which is what the test delegate is expecting). |
| 4788 | delegate->OnNetworkActionFinish(); |
| 4789 | |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 4790 | // Make sure that we don't invoke the callback. We may have an issue if the |
| 4791 | // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we |
| 4792 | // could end up with the transaction being deleted twice if we send any |
| 4793 | // notification from the transaction destructor (see http://crbug.com/31723). |
| 4794 | EXPECT_FALSE(c->callback.have_result()); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4795 | |
| 4796 | // Verify that the entry is marked as incomplete. |
| 4797 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4798 | ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4799 | net::HttpResponseInfo response; |
| 4800 | bool truncated = false; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4801 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4802 | EXPECT_TRUE(truncated); |
| 4803 | entry->Close(); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4804 | |
| 4805 | RemoveMockTransaction(&transaction); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4806 | } |
| 4807 | |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 4808 | // Tests that we don't mark an entry as truncated when we read everything. |
| 4809 | TEST(HttpCache, DontSetTruncatedFlag) { |
| 4810 | MockHttpCache cache; |
| 4811 | |
| 4812 | MockTransaction transaction(kSimpleGET_Transaction); |
| 4813 | transaction.response_headers = |
| 4814 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 4815 | "Content-Length: 22\n" |
[email protected] | 29cc1ce4 | 2012-07-22 18:39:35 | [diff] [blame] | 4816 | "Etag: \"foopy\"\n"; |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 4817 | AddMockTransaction(&transaction); |
| 4818 | MockHttpRequest request(transaction); |
| 4819 | |
| 4820 | scoped_ptr<Context> c(new Context()); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 4821 | int rv = cache.http_cache()->CreateTransaction( |
| 4822 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 4823 | EXPECT_EQ(net::OK, rv); |
| 4824 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4825 | rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 4826 | EXPECT_EQ(net::OK, c->callback.GetResult(rv)); |
| 4827 | |
| 4828 | // Read everything. |
| 4829 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(22)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4830 | rv = c->trans->Read(buf, buf->size(), c->callback.callback()); |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 4831 | EXPECT_EQ(buf->size(), c->callback.GetResult(rv)); |
| 4832 | |
| 4833 | // Destroy the transaction. |
| 4834 | c->trans.reset(); |
| 4835 | |
| 4836 | // Verify that the entry is not marked as truncated. |
| 4837 | disk_cache::Entry* entry; |
| 4838 | ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); |
| 4839 | net::HttpResponseInfo response; |
| 4840 | bool truncated = true; |
| 4841 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
| 4842 | EXPECT_FALSE(truncated); |
| 4843 | entry->Close(); |
| 4844 | |
| 4845 | RemoveMockTransaction(&transaction); |
| 4846 | } |
| 4847 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4848 | // Tests that we can continue with a request that was interrupted. |
| 4849 | TEST(HttpCache, GET_IncompleteResource) { |
| 4850 | MockHttpCache cache; |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4851 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4852 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4853 | std::string raw_headers("HTTP/1.1 200 OK\n" |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 4854 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4855 | "ETag: \"foo\"\n" |
| 4856 | "Accept-Ranges: bytes\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4857 | "Content-Length: 80\n"); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 4858 | CreateTruncatedEntry(raw_headers, &cache); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4859 | |
| 4860 | // Now make a regular request. |
| 4861 | std::string headers; |
| 4862 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 4863 | transaction.request_headers = EXTRA_HEADER; |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4864 | transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " |
| 4865 | "rg: 50-59 rg: 60-69 rg: 70-79 "; |
| 4866 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4867 | |
| 4868 | // We update the headers with the ones received while revalidating. |
| 4869 | std::string expected_headers( |
| 4870 | "HTTP/1.1 200 OK\n" |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 4871 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4872 | "Accept-Ranges: bytes\n" |
| 4873 | "ETag: \"foo\"\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 4874 | "Content-Length: 80\n"); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4875 | |
| 4876 | EXPECT_EQ(expected_headers, headers); |
| 4877 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4878 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4879 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4880 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4881 | // Verify that the disk entry was updated. |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 4882 | disk_cache::Entry* entry; |
| 4883 | ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4884 | EXPECT_EQ(80, entry->GetDataSize(1)); |
| 4885 | bool truncated = true; |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 4886 | net::HttpResponseInfo response; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4887 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4888 | EXPECT_FALSE(truncated); |
| 4889 | entry->Close(); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 4890 | |
| 4891 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 4892 | } |
| 4893 | |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 4894 | // Tests the handling of no-store when revalidating a truncated entry. |
| 4895 | TEST(HttpCache, GET_IncompleteResource_NoStore) { |
| 4896 | MockHttpCache cache; |
| 4897 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4898 | |
| 4899 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 4900 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 4901 | "ETag: \"foo\"\n" |
| 4902 | "Accept-Ranges: bytes\n" |
| 4903 | "Content-Length: 80\n"); |
| 4904 | CreateTruncatedEntry(raw_headers, &cache); |
| 4905 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4906 | |
| 4907 | // Now make a regular request. |
| 4908 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4909 | transaction.request_headers = EXTRA_HEADER; |
| 4910 | std::string response_headers(transaction.response_headers); |
| 4911 | response_headers += ("Cache-Control: no-store\n"); |
| 4912 | transaction.response_headers = response_headers.c_str(); |
| 4913 | transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " |
| 4914 | "rg: 50-59 rg: 60-69 rg: 70-79 "; |
| 4915 | AddMockTransaction(&transaction); |
| 4916 | |
| 4917 | std::string headers; |
| 4918 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4919 | |
| 4920 | // We update the headers with the ones received while revalidating. |
| 4921 | std::string expected_headers( |
| 4922 | "HTTP/1.1 200 OK\n" |
| 4923 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 4924 | "Accept-Ranges: bytes\n" |
| 4925 | "Cache-Control: no-store\n" |
| 4926 | "ETag: \"foo\"\n" |
| 4927 | "Content-Length: 80\n"); |
| 4928 | |
| 4929 | EXPECT_EQ(expected_headers, headers); |
| 4930 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4931 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4932 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4933 | |
| 4934 | // Verify that the disk entry was deleted. |
| 4935 | disk_cache::Entry* entry; |
| 4936 | EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
| 4937 | RemoveMockTransaction(&transaction); |
| 4938 | } |
| 4939 | |
| 4940 | // Tests cancelling a request after the server sent no-store. |
| 4941 | TEST(HttpCache, GET_IncompleteResource_Cancel) { |
| 4942 | MockHttpCache cache; |
| 4943 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4944 | |
| 4945 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 4946 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 4947 | "ETag: \"foo\"\n" |
| 4948 | "Accept-Ranges: bytes\n" |
| 4949 | "Content-Length: 80\n"); |
| 4950 | CreateTruncatedEntry(raw_headers, &cache); |
| 4951 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4952 | |
| 4953 | // Now make a regular request. |
| 4954 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4955 | transaction.request_headers = EXTRA_HEADER; |
| 4956 | std::string response_headers(transaction.response_headers); |
| 4957 | response_headers += ("Cache-Control: no-store\n"); |
| 4958 | transaction.response_headers = response_headers.c_str(); |
| 4959 | transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " |
| 4960 | "rg: 50-59 rg: 60-69 rg: 70-79 "; |
| 4961 | AddMockTransaction(&transaction); |
| 4962 | |
| 4963 | MockHttpRequest request(transaction); |
| 4964 | Context* c = new Context(); |
| 4965 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 4966 | int rv = cache.http_cache()->CreateTransaction( |
| 4967 | net::DEFAULT_PRIORITY, &c->trans, NULL); |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 4968 | EXPECT_EQ(net::OK, rv); |
| 4969 | |
[email protected] | 9f4633c6 | 2012-05-29 18:38:57 | [diff] [blame] | 4970 | // Queue another request to this transaction. We have to start this request |
| 4971 | // before the first one gets the response from the server and dooms the entry, |
| 4972 | // otherwise it will just create a new entry without being queued to the first |
| 4973 | // request. |
| 4974 | Context* pending = new Context(); |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 4975 | EXPECT_EQ(net::OK, |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 4976 | cache.http_cache()->CreateTransaction( |
| 4977 | net::DEFAULT_PRIORITY, &pending->trans, NULL)); |
[email protected] | 9f4633c6 | 2012-05-29 18:38:57 | [diff] [blame] | 4978 | |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 4979 | rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 9f4633c6 | 2012-05-29 18:38:57 | [diff] [blame] | 4980 | EXPECT_EQ(net::ERR_IO_PENDING, |
| 4981 | pending->trans->Start(&request, pending->callback.callback(), |
| 4982 | net::BoundNetLog())); |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 4983 | EXPECT_EQ(net::OK, c->callback.GetResult(rv)); |
| 4984 | |
| 4985 | // Make sure that the entry has some data stored. |
| 4986 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(5)); |
| 4987 | rv = c->trans->Read(buf, buf->size(), c->callback.callback()); |
| 4988 | EXPECT_EQ(5, c->callback.GetResult(rv)); |
| 4989 | |
[email protected] | 9f4633c6 | 2012-05-29 18:38:57 | [diff] [blame] | 4990 | // Cancel the requests. |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 4991 | delete c; |
[email protected] | 9f4633c6 | 2012-05-29 18:38:57 | [diff] [blame] | 4992 | delete pending; |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 4993 | |
| 4994 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4995 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
[email protected] | 9f4633c6 | 2012-05-29 18:38:57 | [diff] [blame] | 4996 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 4997 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 4998 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 4999 | RemoveMockTransaction(&transaction); |
| 5000 | } |
| 5001 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5002 | // Tests that we delete truncated entries if the server changes its mind midway. |
| 5003 | TEST(HttpCache, GET_IncompleteResource2) { |
| 5004 | MockHttpCache cache; |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5005 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5006 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5007 | // Content-length will be intentionally bad. |
| 5008 | std::string raw_headers("HTTP/1.1 200 OK\n" |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 5009 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5010 | "ETag: \"foo\"\n" |
| 5011 | "Accept-Ranges: bytes\n" |
| 5012 | "Content-Length: 50\n"); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5013 | CreateTruncatedEntry(raw_headers, &cache); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5014 | |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5015 | // Now make a regular request. We expect the code to fail the validation and |
| 5016 | // retry the request without using byte ranges. |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5017 | std::string headers; |
| 5018 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 5019 | transaction.request_headers = EXTRA_HEADER; |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5020 | transaction.data = "Not a range"; |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5021 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 5022 | |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5023 | // The server will return 200 instead of a byte range. |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5024 | std::string expected_headers( |
| 5025 | "HTTP/1.1 200 OK\n" |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5026 | "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5027 | |
| 5028 | EXPECT_EQ(expected_headers, headers); |
| 5029 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5030 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5031 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5032 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5033 | // Verify that the disk entry was deleted. |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5034 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 5035 | ASSERT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5036 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5037 | } |
| 5038 | |
| 5039 | // Tests that we always validate a truncated request. |
| 5040 | TEST(HttpCache, GET_IncompleteResource3) { |
| 5041 | MockHttpCache cache; |
| 5042 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5043 | |
| 5044 | // This should not require validation for 10 hours. |
| 5045 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 5046 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 5047 | "ETag: \"foo\"\n" |
| 5048 | "Cache-Control: max-age= 36000\n" |
| 5049 | "Accept-Ranges: bytes\n" |
| 5050 | "Content-Length: 80\n"); |
| 5051 | CreateTruncatedEntry(raw_headers, &cache); |
| 5052 | |
| 5053 | // Now make a regular request. |
| 5054 | std::string headers; |
| 5055 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 5056 | transaction.request_headers = EXTRA_HEADER; |
| 5057 | transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " |
| 5058 | "rg: 50-59 rg: 60-69 rg: 70-79 "; |
| 5059 | |
| 5060 | scoped_ptr<Context> c(new Context); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5061 | EXPECT_EQ(net::OK, cache.http_cache()->CreateTransaction( |
| 5062 | net::DEFAULT_PRIORITY, &c->trans, NULL)); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5063 | |
| 5064 | MockHttpRequest request(transaction); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5065 | int rv = c->trans->Start( |
| 5066 | &request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5067 | EXPECT_EQ(net::OK, c->callback.GetResult(rv)); |
| 5068 | |
| 5069 | // We should have checked with the server before finishing Start(). |
| 5070 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5071 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5072 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5073 | |
| 5074 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5075 | } |
| 5076 | |
| 5077 | // Tests that we cache a 200 response to the validation request. |
| 5078 | TEST(HttpCache, GET_IncompleteResource4) { |
| 5079 | MockHttpCache cache; |
| 5080 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5081 | |
| 5082 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 5083 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 5084 | "ETag: \"foo\"\n" |
| 5085 | "Accept-Ranges: bytes\n" |
| 5086 | "Content-Length: 80\n"); |
| 5087 | CreateTruncatedEntry(raw_headers, &cache); |
| 5088 | |
| 5089 | // Now make a regular request. |
| 5090 | std::string headers; |
| 5091 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 5092 | transaction.request_headers = EXTRA_HEADER; |
| 5093 | transaction.data = "Not a range"; |
| 5094 | RangeTransactionServer handler; |
| 5095 | handler.set_bad_200(true); |
| 5096 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 5097 | |
| 5098 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5099 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5100 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5101 | |
| 5102 | // Verify that the disk entry was updated. |
| 5103 | disk_cache::Entry* entry; |
| 5104 | ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
| 5105 | EXPECT_EQ(11, entry->GetDataSize(1)); |
| 5106 | bool truncated = true; |
| 5107 | net::HttpResponseInfo response; |
| 5108 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
| 5109 | EXPECT_FALSE(truncated); |
| 5110 | entry->Close(); |
| 5111 | |
| 5112 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5113 | } |
| 5114 | |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 5115 | // Tests that when we cancel a request that was interrupted, we mark it again |
| 5116 | // as truncated. |
| 5117 | TEST(HttpCache, GET_CancelIncompleteResource) { |
| 5118 | MockHttpCache cache; |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 5119 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5120 | |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 5121 | std::string raw_headers("HTTP/1.1 200 OK\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5122 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 5123 | "ETag: \"foo\"\n" |
| 5124 | "Accept-Ranges: bytes\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5125 | "Content-Length: 80\n"); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5126 | CreateTruncatedEntry(raw_headers, &cache); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 5127 | |
| 5128 | // Now make a regular request. |
| 5129 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 5130 | transaction.request_headers = EXTRA_HEADER; |
| 5131 | |
| 5132 | MockHttpRequest request(transaction); |
| 5133 | Context* c = new Context(); |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5134 | EXPECT_EQ(net::OK, cache.http_cache()->CreateTransaction( |
| 5135 | net::DEFAULT_PRIORITY, &c->trans, NULL)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 5136 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5137 | int rv = c->trans->Start( |
| 5138 | &request, c->callback.callback(), net::BoundNetLog()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 5139 | EXPECT_EQ(net::OK, c->callback.GetResult(rv)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 5140 | |
| 5141 | // Read 20 bytes from the cache, and 10 from the net. |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5142 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5143 | rv = c->trans->Read(buf, 20, c->callback.callback()); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5144 | EXPECT_EQ(20, c->callback.GetResult(rv)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5145 | rv = c->trans->Read(buf, 10, c->callback.callback()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 5146 | EXPECT_EQ(10, c->callback.GetResult(rv)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 5147 | |
| 5148 | // At this point, we are already reading so canceling the request should leave |
| 5149 | // a truncated one. |
| 5150 | delete c; |
| 5151 | |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 5152 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5153 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5154 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5155 | |
| 5156 | // Verify that the disk entry was updated: now we have 30 bytes. |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5157 | disk_cache::Entry* entry; |
| 5158 | ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 5159 | EXPECT_EQ(30, entry->GetDataSize(1)); |
| 5160 | bool truncated = false; |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5161 | net::HttpResponseInfo response; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 5162 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 5163 | EXPECT_TRUE(truncated); |
| 5164 | entry->Close(); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5165 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 5166 | } |
| 5167 | |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 5168 | // Tests that we can handle range requests when we have a truncated entry. |
| 5169 | TEST(HttpCache, RangeGET_IncompleteResource) { |
| 5170 | MockHttpCache cache; |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 5171 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5172 | |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 5173 | // Content-length will be intentionally bogus. |
| 5174 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 5175 | "Last-Modified: something\n" |
| 5176 | "ETag: \"foo\"\n" |
| 5177 | "Accept-Ranges: bytes\n" |
| 5178 | "Content-Length: 10\n"); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5179 | CreateTruncatedEntry(raw_headers, &cache); |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 5180 | |
| 5181 | // Now make a range request. |
| 5182 | std::string headers; |
| 5183 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 5184 | &headers); |
| 5185 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 5186 | Verify206Response(headers, 40, 49); |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 5187 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5188 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5189 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5190 | |
| 5191 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5192 | } |
| 5193 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5194 | TEST(HttpCache, SyncRead) { |
| 5195 | MockHttpCache cache; |
| 5196 | |
| 5197 | // This test ensures that a read that completes synchronously does not cause |
| 5198 | // any problems. |
| 5199 | |
| 5200 | ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 5201 | transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START | |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 5202 | TEST_MODE_SYNC_CACHE_READ | |
| 5203 | TEST_MODE_SYNC_CACHE_WRITE); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5204 | |
| 5205 | MockHttpRequest r1(transaction), |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 5206 | r2(transaction), |
| 5207 | r3(transaction); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5208 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5209 | TestTransactionConsumer c1(net::DEFAULT_PRIORITY, cache.http_cache()), |
| 5210 | c2(net::DEFAULT_PRIORITY, cache.http_cache()), |
| 5211 | c3(net::DEFAULT_PRIORITY, cache.http_cache()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5212 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5213 | c1.Start(&r1, net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5214 | |
| 5215 | r2.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5216 | c2.Start(&r2, net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5217 | |
| 5218 | r3.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5219 | c3.Start(&r3, net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5220 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 5221 | base::MessageLoop::current()->Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5222 | |
| 5223 | EXPECT_TRUE(c1.is_done()); |
| 5224 | EXPECT_TRUE(c2.is_done()); |
| 5225 | EXPECT_TRUE(c3.is_done()); |
| 5226 | |
| 5227 | EXPECT_EQ(net::OK, c1.error()); |
| 5228 | EXPECT_EQ(net::OK, c2.error()); |
| 5229 | EXPECT_EQ(net::OK, c3.error()); |
| 5230 | } |
| 5231 | |
| 5232 | TEST(HttpCache, ValidationResultsIn200) { |
| 5233 | MockHttpCache cache; |
| 5234 | |
| 5235 | // This test ensures that a conditional request, which results in a 200 |
| 5236 | // instead of a 304, properly truncates the existing response data. |
| 5237 | |
| 5238 | // write to the cache |
| 5239 | RunTransactionTest(cache.http_cache(), kETagGET_Transaction); |
| 5240 | |
| 5241 | // force this transaction to validate the cache |
| 5242 | MockTransaction transaction(kETagGET_Transaction); |
| 5243 | transaction.load_flags |= net::LOAD_VALIDATE_CACHE; |
| 5244 | RunTransactionTest(cache.http_cache(), transaction); |
| 5245 | |
| 5246 | // read from the cache |
| 5247 | RunTransactionTest(cache.http_cache(), kETagGET_Transaction); |
| 5248 | } |
| 5249 | |
| 5250 | TEST(HttpCache, CachedRedirect) { |
| 5251 | MockHttpCache cache; |
| 5252 | |
| 5253 | ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); |
| 5254 | kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; |
| 5255 | kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; |
| 5256 | |
| 5257 | MockHttpRequest request(kTestTransaction); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5258 | net::TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5259 | |
| 5260 | // write to the cache |
| 5261 | { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 5262 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5263 | int rv = cache.http_cache()->CreateTransaction( |
| 5264 | net::DEFAULT_PRIORITY, &trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 5265 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 5266 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5267 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5268 | rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5269 | if (rv == net::ERR_IO_PENDING) |
| 5270 | rv = callback.WaitForResult(); |
| 5271 | ASSERT_EQ(net::OK, rv); |
| 5272 | |
| 5273 | const net::HttpResponseInfo* info = trans->GetResponseInfo(); |
| 5274 | ASSERT_TRUE(info); |
| 5275 | |
| 5276 | EXPECT_EQ(info->headers->response_code(), 301); |
| 5277 | |
| 5278 | std::string location; |
| 5279 | info->headers->EnumerateHeader(NULL, "Location", &location); |
| 5280 | EXPECT_EQ(location, "http://www.bar.com/"); |
| 5281 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 5282 | // Destroy transaction when going out of scope. We have not actually |
| 5283 | // read the response body -- want to test that it is still getting cached. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5284 | } |
| 5285 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5286 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5287 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5288 | |
| 5289 | // read from the cache |
| 5290 | { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 5291 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5292 | int rv = cache.http_cache()->CreateTransaction( |
| 5293 | net::DEFAULT_PRIORITY, &trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 5294 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 5295 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5296 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5297 | rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5298 | if (rv == net::ERR_IO_PENDING) |
| 5299 | rv = callback.WaitForResult(); |
| 5300 | ASSERT_EQ(net::OK, rv); |
| 5301 | |
| 5302 | const net::HttpResponseInfo* info = trans->GetResponseInfo(); |
| 5303 | ASSERT_TRUE(info); |
| 5304 | |
| 5305 | EXPECT_EQ(info->headers->response_code(), 301); |
| 5306 | |
| 5307 | std::string location; |
| 5308 | info->headers->EnumerateHeader(NULL, "Location", &location); |
| 5309 | EXPECT_EQ(location, "http://www.bar.com/"); |
| 5310 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 5311 | // Destroy transaction when going out of scope. We have not actually |
| 5312 | // read the response body -- want to test that it is still getting cached. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5313 | } |
| 5314 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5315 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5316 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5317 | } |
| 5318 | |
| 5319 | TEST(HttpCache, CacheControlNoStore) { |
| 5320 | MockHttpCache cache; |
| 5321 | |
| 5322 | ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 5323 | transaction.response_headers = "cache-control: no-store\n"; |
| 5324 | |
| 5325 | // initial load |
| 5326 | RunTransactionTest(cache.http_cache(), transaction); |
| 5327 | |
| 5328 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5329 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5330 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5331 | |
| 5332 | // try loading again; it should result in a network fetch |
| 5333 | RunTransactionTest(cache.http_cache(), transaction); |
| 5334 | |
| 5335 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5336 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5337 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5338 | |
| 5339 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 5340 | EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5341 | } |
| 5342 | |
| 5343 | TEST(HttpCache, CacheControlNoStore2) { |
| 5344 | // this test is similar to the above test, except that the initial response |
| 5345 | // is cachable, but when it is validated, no-store is received causing the |
| 5346 | // cached document to be deleted. |
| 5347 | MockHttpCache cache; |
| 5348 | |
| 5349 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 5350 | |
| 5351 | // initial load |
| 5352 | RunTransactionTest(cache.http_cache(), transaction); |
| 5353 | |
| 5354 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5355 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5356 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5357 | |
| 5358 | // try loading again; it should result in a network fetch |
| 5359 | transaction.load_flags = net::LOAD_VALIDATE_CACHE; |
| 5360 | transaction.response_headers = "cache-control: no-store\n"; |
| 5361 | RunTransactionTest(cache.http_cache(), transaction); |
| 5362 | |
| 5363 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5364 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5365 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5366 | |
| 5367 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 5368 | EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | TEST(HttpCache, CacheControlNoStore3) { |
| 5372 | // this test is similar to the above test, except that the response is a 304 |
| 5373 | // instead of a 200. this should never happen in practice, but it seems like |
| 5374 | // a good thing to verify that we still destroy the cache entry. |
| 5375 | MockHttpCache cache; |
| 5376 | |
| 5377 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 5378 | |
| 5379 | // initial load |
| 5380 | RunTransactionTest(cache.http_cache(), transaction); |
| 5381 | |
| 5382 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5383 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5384 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5385 | |
| 5386 | // try loading again; it should result in a network fetch |
| 5387 | transaction.load_flags = net::LOAD_VALIDATE_CACHE; |
| 5388 | transaction.response_headers = "cache-control: no-store\n"; |
| 5389 | transaction.status = "HTTP/1.1 304 Not Modified"; |
| 5390 | RunTransactionTest(cache.http_cache(), transaction); |
| 5391 | |
| 5392 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5393 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5394 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5395 | |
| 5396 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 5397 | EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5398 | } |
| 5399 | |
| 5400 | // Ensure that we don't cache requests served over bad HTTPS. |
| 5401 | TEST(HttpCache, SimpleGET_SSLError) { |
| 5402 | MockHttpCache cache; |
| 5403 | |
| 5404 | MockTransaction transaction = kSimpleGET_Transaction; |
| 5405 | transaction.cert_status = net::CERT_STATUS_REVOKED; |
| 5406 | ScopedMockTransaction scoped_transaction(transaction); |
| 5407 | |
| 5408 | // write to the cache |
| 5409 | RunTransactionTest(cache.http_cache(), transaction); |
| 5410 | |
| 5411 | // Test that it was not cached. |
| 5412 | transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 5413 | |
| 5414 | MockHttpRequest request(transaction); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5415 | net::TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5416 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 5417 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5418 | int rv = cache.http_cache()->CreateTransaction( |
| 5419 | net::DEFAULT_PRIORITY, &trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 5420 | EXPECT_EQ(net::OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 5421 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5422 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5423 | rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5424 | if (rv == net::ERR_IO_PENDING) |
| 5425 | rv = callback.WaitForResult(); |
| 5426 | ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5427 | } |
[email protected] | 3e2d38d | 2009-02-14 02:01:18 | [diff] [blame] | 5428 | |
| 5429 | // Ensure that we don't crash by if left-behind transactions. |
| 5430 | TEST(HttpCache, OutlivedTransactions) { |
| 5431 | MockHttpCache* cache = new MockHttpCache; |
| 5432 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 5433 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5434 | int rv = cache->http_cache()->CreateTransaction( |
| 5435 | net::DEFAULT_PRIORITY, &trans, NULL); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 5436 | EXPECT_EQ(net::OK, rv); |
| 5437 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 5438 | delete cache; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 5439 | trans.reset(); |
[email protected] | 3e2d38d | 2009-02-14 02:01:18 | [diff] [blame] | 5440 | } |
[email protected] | 98179700 | 2009-06-05 07:14:15 | [diff] [blame] | 5441 | |
| 5442 | // Test that the disabled mode works. |
| 5443 | TEST(HttpCache, CacheDisabledMode) { |
| 5444 | MockHttpCache cache; |
| 5445 | |
| 5446 | // write to the cache |
| 5447 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 5448 | |
| 5449 | // go into disabled mode |
| 5450 | cache.http_cache()->set_mode(net::HttpCache::DISABLE); |
| 5451 | |
| 5452 | // force this transaction to write to the cache again |
| 5453 | MockTransaction transaction(kSimpleGET_Transaction); |
| 5454 | |
| 5455 | RunTransactionTest(cache.http_cache(), transaction); |
| 5456 | |
| 5457 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5458 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5459 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5460 | } |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 5461 | |
| 5462 | // Other tests check that the response headers of the cached response |
| 5463 | // get updated on 304. Here we specifically check that the |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 5464 | // HttpResponseHeaders::request_time and HttpResponseHeaders::response_time |
| 5465 | // fields also gets updated. |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 5466 | // http://crbug.com/20594. |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 5467 | TEST(HttpCache, UpdatesRequestResponseTimeOn304) { |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 5468 | MockHttpCache cache; |
| 5469 | |
| 5470 | const char* kUrl = "http://foobar"; |
| 5471 | const char* kData = "body"; |
| 5472 | |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 5473 | MockTransaction mock_network_response = { 0 }; |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 5474 | mock_network_response.url = kUrl; |
| 5475 | |
| 5476 | AddMockTransaction(&mock_network_response); |
| 5477 | |
| 5478 | // Request |kUrl|, causing |kNetResponse1| to be written to the cache. |
| 5479 | |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 5480 | MockTransaction request = { 0 }; |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 5481 | request.url = kUrl; |
| 5482 | request.method = "GET"; |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 5483 | request.request_headers = "\r\n"; |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 5484 | request.data = kData; |
| 5485 | |
| 5486 | static const Response kNetResponse1 = { |
| 5487 | "HTTP/1.1 200 OK", |
| 5488 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 5489 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 5490 | kData |
| 5491 | }; |
| 5492 | |
| 5493 | kNetResponse1.AssignTo(&mock_network_response); |
| 5494 | |
| 5495 | RunTransactionTest(cache.http_cache(), request); |
| 5496 | |
| 5497 | // Request |kUrl| again, this time validating the cache and getting |
| 5498 | // a 304 back. |
| 5499 | |
| 5500 | request.load_flags = net::LOAD_VALIDATE_CACHE; |
| 5501 | |
| 5502 | static const Response kNetResponse2 = { |
| 5503 | "HTTP/1.1 304 Not Modified", |
| 5504 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n", |
| 5505 | "" |
| 5506 | }; |
| 5507 | |
| 5508 | kNetResponse2.AssignTo(&mock_network_response); |
| 5509 | |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 5510 | base::Time request_time = base::Time() + base::TimeDelta::FromHours(1234); |
| 5511 | base::Time response_time = base::Time() + base::TimeDelta::FromHours(1235); |
| 5512 | |
| 5513 | mock_network_response.request_time = request_time; |
| 5514 | mock_network_response.response_time = response_time; |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 5515 | |
| 5516 | net::HttpResponseInfo response; |
| 5517 | RunTransactionTestWithResponseInfo(cache.http_cache(), request, &response); |
| 5518 | |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 5519 | // The request and response times should have been updated. |
| 5520 | EXPECT_EQ(request_time.ToInternalValue(), |
| 5521 | response.request_time.ToInternalValue()); |
| 5522 | EXPECT_EQ(response_time.ToInternalValue(), |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 5523 | response.response_time.ToInternalValue()); |
| 5524 | |
| 5525 | std::string headers; |
| 5526 | response.headers->GetNormalizedHeaders(&headers); |
| 5527 | |
| 5528 | EXPECT_EQ("HTTP/1.1 200 OK\n" |
| 5529 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 5530 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 5531 | headers); |
| 5532 | |
| 5533 | RemoveMockTransaction(&mock_network_response); |
| 5534 | } |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 5535 | |
| 5536 | // Tests that we can write metadata to an entry. |
| 5537 | TEST(HttpCache, WriteMetadata_OK) { |
| 5538 | MockHttpCache cache; |
| 5539 | |
| 5540 | // Write to the cache |
| 5541 | net::HttpResponseInfo response; |
| 5542 | RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 5543 | &response); |
| 5544 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 5545 | |
| 5546 | // Trivial call. |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5547 | cache.http_cache()->WriteMetadata(GURL("foo"), net::DEFAULT_PRIORITY, |
| 5548 | Time::Now(), NULL, 0); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 5549 | |
| 5550 | // Write meta data to the same entry. |
| 5551 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); |
| 5552 | memset(buf->data(), 0, buf->size()); |
| 5553 | base::strlcpy(buf->data(), "Hi there", buf->size()); |
| 5554 | cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url), |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5555 | net::DEFAULT_PRIORITY, |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 5556 | response.response_time, buf, buf->size()); |
| 5557 | |
| 5558 | // Release the buffer before the operation takes place. |
| 5559 | buf = NULL; |
| 5560 | |
| 5561 | // Makes sure we finish pending operations. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 5562 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 5563 | |
| 5564 | RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 5565 | &response); |
| 5566 | ASSERT_TRUE(response.metadata.get() != NULL); |
| 5567 | EXPECT_EQ(50, response.metadata->size()); |
| 5568 | EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there")); |
| 5569 | |
| 5570 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5571 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 5572 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5573 | } |
| 5574 | |
| 5575 | // Tests that we only write metadata to an entry if the time stamp matches. |
| 5576 | TEST(HttpCache, WriteMetadata_Fail) { |
| 5577 | MockHttpCache cache; |
| 5578 | |
| 5579 | // Write to the cache |
| 5580 | net::HttpResponseInfo response; |
| 5581 | RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 5582 | &response); |
| 5583 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 5584 | |
| 5585 | // Attempt to write meta data to the same entry. |
| 5586 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); |
| 5587 | memset(buf->data(), 0, buf->size()); |
| 5588 | base::strlcpy(buf->data(), "Hi there", buf->size()); |
| 5589 | base::Time expected_time = response.response_time - |
| 5590 | base::TimeDelta::FromMilliseconds(20); |
| 5591 | cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url), |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5592 | net::DEFAULT_PRIORITY, |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 5593 | expected_time, buf, buf->size()); |
| 5594 | |
| 5595 | // Makes sure we finish pending operations. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 5596 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 5597 | |
| 5598 | RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 5599 | &response); |
| 5600 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 5601 | |
| 5602 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5603 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 5604 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5605 | } |
| 5606 | |
| 5607 | // Tests that we can read metadata after validating the entry and with READ mode |
| 5608 | // transactions. |
| 5609 | TEST(HttpCache, ReadMetadata) { |
| 5610 | MockHttpCache cache; |
| 5611 | |
| 5612 | // Write to the cache |
| 5613 | net::HttpResponseInfo response; |
| 5614 | RunTransactionTestWithResponseInfo(cache.http_cache(), |
| 5615 | kTypicalGET_Transaction, &response); |
| 5616 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 5617 | |
| 5618 | // Write meta data to the same entry. |
| 5619 | scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); |
| 5620 | memset(buf->data(), 0, buf->size()); |
| 5621 | base::strlcpy(buf->data(), "Hi there", buf->size()); |
| 5622 | cache.http_cache()->WriteMetadata(GURL(kTypicalGET_Transaction.url), |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5623 | net::DEFAULT_PRIORITY, |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 5624 | response.response_time, buf, buf->size()); |
| 5625 | |
| 5626 | // Makes sure we finish pending operations. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 5627 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 5628 | |
| 5629 | // Start with a READ mode transaction. |
| 5630 | MockTransaction trans1(kTypicalGET_Transaction); |
| 5631 | trans1.load_flags = net::LOAD_ONLY_FROM_CACHE; |
| 5632 | |
| 5633 | RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response); |
| 5634 | ASSERT_TRUE(response.metadata.get() != NULL); |
| 5635 | EXPECT_EQ(50, response.metadata->size()); |
| 5636 | EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there")); |
| 5637 | |
| 5638 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5639 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 5640 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 5641 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 5642 | |
| 5643 | // Now make sure that the entry is re-validated with the server. |
| 5644 | trans1.load_flags = net::LOAD_VALIDATE_CACHE; |
| 5645 | trans1.status = "HTTP/1.1 304 Not Modified"; |
| 5646 | AddMockTransaction(&trans1); |
| 5647 | |
| 5648 | response.metadata = NULL; |
| 5649 | RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response); |
| 5650 | EXPECT_TRUE(response.metadata.get() != NULL); |
| 5651 | |
| 5652 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5653 | EXPECT_EQ(3, cache.disk_cache()->open_count()); |
| 5654 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 5655 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 5656 | RemoveMockTransaction(&trans1); |
| 5657 | |
| 5658 | // Now return 200 when validating the entry so the metadata will be lost. |
| 5659 | MockTransaction trans2(kTypicalGET_Transaction); |
| 5660 | trans2.load_flags = net::LOAD_VALIDATE_CACHE; |
| 5661 | RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); |
| 5662 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 5663 | |
| 5664 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 5665 | EXPECT_EQ(4, cache.disk_cache()->open_count()); |
| 5666 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5667 | } |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 5668 | |
| 5669 | // Tests that we don't mark entries as truncated when a filter detects the end |
| 5670 | // of the stream. |
| 5671 | TEST(HttpCache, FilterCompletion) { |
| 5672 | MockHttpCache cache; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5673 | net::TestCompletionCallback callback; |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 5674 | |
| 5675 | { |
| 5676 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5677 | int rv = cache.http_cache()->CreateTransaction( |
| 5678 | net::DEFAULT_PRIORITY, &trans, NULL); |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 5679 | EXPECT_EQ(net::OK, rv); |
| 5680 | |
| 5681 | MockHttpRequest request(kSimpleGET_Transaction); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5682 | rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 5683 | EXPECT_EQ(net::OK, callback.GetResult(rv)); |
| 5684 | |
| 5685 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5686 | rv = trans->Read(buf, 256, callback.callback()); |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 5687 | EXPECT_GT(callback.GetResult(rv), 0); |
| 5688 | |
| 5689 | // Now make sure that the entry is preserved. |
| 5690 | trans->DoneReading(); |
| 5691 | } |
| 5692 | |
[email protected] | c85316f | 2011-08-18 23:27:36 | [diff] [blame] | 5693 | // Make sure that the ActiveEntry is gone. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 5694 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 5695 | |
| 5696 | // Read from the cache. |
| 5697 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 5698 | |
| 5699 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5700 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5701 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5702 | } |
[email protected] | c85316f | 2011-08-18 23:27:36 | [diff] [blame] | 5703 | |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5704 | // Tests that we stop cachining when told. |
| 5705 | TEST(HttpCache, StopCachingDeletesEntry) { |
| 5706 | MockHttpCache cache; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5707 | net::TestCompletionCallback callback; |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5708 | MockHttpRequest request(kSimpleGET_Transaction); |
| 5709 | |
| 5710 | { |
| 5711 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5712 | int rv = cache.http_cache()->CreateTransaction( |
| 5713 | net::DEFAULT_PRIORITY, &trans, NULL); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5714 | EXPECT_EQ(net::OK, rv); |
| 5715 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5716 | rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5717 | EXPECT_EQ(net::OK, callback.GetResult(rv)); |
| 5718 | |
| 5719 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5720 | rv = trans->Read(buf, 10, callback.callback()); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5721 | EXPECT_EQ(callback.GetResult(rv), 10); |
| 5722 | |
| 5723 | trans->StopCaching(); |
| 5724 | |
| 5725 | // We should be able to keep reading. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5726 | rv = trans->Read(buf, 256, callback.callback()); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5727 | EXPECT_GT(callback.GetResult(rv), 0); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5728 | rv = trans->Read(buf, 256, callback.callback()); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5729 | EXPECT_EQ(callback.GetResult(rv), 0); |
| 5730 | } |
| 5731 | |
| 5732 | // Make sure that the ActiveEntry is gone. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame^] | 5733 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5734 | |
| 5735 | // Verify that the entry is gone. |
| 5736 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 5737 | |
| 5738 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5739 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5740 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5741 | } |
| 5742 | |
| 5743 | // Tests that when we are told to stop caching we don't throw away valid data. |
| 5744 | TEST(HttpCache, StopCachingSavesEntry) { |
| 5745 | MockHttpCache cache; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5746 | net::TestCompletionCallback callback; |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5747 | MockHttpRequest request(kSimpleGET_Transaction); |
| 5748 | |
| 5749 | { |
| 5750 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5751 | int rv = cache.http_cache()->CreateTransaction( |
| 5752 | net::DEFAULT_PRIORITY, &trans, NULL); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5753 | EXPECT_EQ(net::OK, rv); |
| 5754 | |
| 5755 | // Force a response that can be resumed. |
| 5756 | MockTransaction mock_transaction(kSimpleGET_Transaction); |
| 5757 | AddMockTransaction(&mock_transaction); |
| 5758 | mock_transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 5759 | "Content-Length: 42\n" |
[email protected] | 29cc1ce4 | 2012-07-22 18:39:35 | [diff] [blame] | 5760 | "Etag: \"foo\"\n"; |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5761 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5762 | rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5763 | EXPECT_EQ(net::OK, callback.GetResult(rv)); |
| 5764 | |
| 5765 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5766 | rv = trans->Read(buf, 10, callback.callback()); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5767 | EXPECT_EQ(callback.GetResult(rv), 10); |
| 5768 | |
| 5769 | trans->StopCaching(); |
| 5770 | |
| 5771 | // We should be able to keep reading. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5772 | rv = trans->Read(buf, 256, callback.callback()); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5773 | EXPECT_GT(callback.GetResult(rv), 0); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5774 | rv = trans->Read(buf, 256, callback.callback()); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 5775 | EXPECT_EQ(callback.GetResult(rv), 0); |
| 5776 | |
| 5777 | RemoveMockTransaction(&mock_transaction); |
| 5778 | } |
| 5779 | |
| 5780 | // Verify that the entry is marked as incomplete. |
| 5781 | disk_cache::Entry* entry; |
| 5782 | ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); |
| 5783 | net::HttpResponseInfo response; |
| 5784 | bool truncated = false; |
| 5785 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
| 5786 | EXPECT_TRUE(truncated); |
| 5787 | entry->Close(); |
| 5788 | } |
| 5789 | |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 5790 | // Tests that we handle truncated enries when StopCaching is called. |
| 5791 | TEST(HttpCache, StopCachingTruncatedEntry) { |
| 5792 | MockHttpCache cache; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5793 | net::TestCompletionCallback callback; |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 5794 | MockHttpRequest request(kRangeGET_TransactionOK); |
| 5795 | request.extra_headers.Clear(); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 5796 | request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 5797 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5798 | |
| 5799 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 5800 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 5801 | "ETag: \"foo\"\n" |
| 5802 | "Accept-Ranges: bytes\n" |
| 5803 | "Content-Length: 80\n"); |
| 5804 | CreateTruncatedEntry(raw_headers, &cache); |
| 5805 | |
| 5806 | { |
| 5807 | // Now make a regular request. |
| 5808 | scoped_ptr<net::HttpTransaction> trans; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 5809 | int rv = cache.http_cache()->CreateTransaction( |
| 5810 | net::DEFAULT_PRIORITY, &trans, NULL); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 5811 | EXPECT_EQ(net::OK, rv); |
| 5812 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5813 | rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 5814 | EXPECT_EQ(net::OK, callback.GetResult(rv)); |
| 5815 | |
| 5816 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5817 | rv = trans->Read(buf, 10, callback.callback()); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 5818 | EXPECT_EQ(callback.GetResult(rv), 10); |
| 5819 | |
| 5820 | // This is actually going to do nothing. |
| 5821 | trans->StopCaching(); |
| 5822 | |
| 5823 | // We should be able to keep reading. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5824 | rv = trans->Read(buf, 256, callback.callback()); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 5825 | EXPECT_GT(callback.GetResult(rv), 0); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5826 | rv = trans->Read(buf, 256, callback.callback()); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 5827 | EXPECT_GT(callback.GetResult(rv), 0); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5828 | rv = trans->Read(buf, 256, callback.callback()); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 5829 | EXPECT_EQ(callback.GetResult(rv), 0); |
| 5830 | } |
| 5831 | |
| 5832 | // Verify that the disk entry was updated. |
| 5833 | disk_cache::Entry* entry; |
| 5834 | ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
| 5835 | EXPECT_EQ(80, entry->GetDataSize(1)); |
| 5836 | bool truncated = true; |
| 5837 | net::HttpResponseInfo response; |
| 5838 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
| 5839 | EXPECT_FALSE(truncated); |
| 5840 | entry->Close(); |
| 5841 | |
| 5842 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5843 | } |
| 5844 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 5845 | // Tests that we detect truncated resources from the net when there is |
[email protected] | c85316f | 2011-08-18 23:27:36 | [diff] [blame] | 5846 | // a Content-Length header. |
| 5847 | TEST(HttpCache, TruncatedByContentLength) { |
| 5848 | MockHttpCache cache; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5849 | net::TestCompletionCallback callback; |
[email protected] | c85316f | 2011-08-18 23:27:36 | [diff] [blame] | 5850 | |
| 5851 | MockTransaction transaction(kSimpleGET_Transaction); |
| 5852 | AddMockTransaction(&transaction); |
| 5853 | transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 5854 | "Content-Length: 100\n"; |
| 5855 | RunTransactionTest(cache.http_cache(), transaction); |
| 5856 | RemoveMockTransaction(&transaction); |
| 5857 | |
| 5858 | // Read from the cache. |
| 5859 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 5860 | |
| 5861 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5862 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5863 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5864 | } |
| 5865 | |
| 5866 | // Tests that we actually flag entries as truncated when we detect an error |
| 5867 | // from the net. |
| 5868 | TEST(HttpCache, TruncatedByContentLength2) { |
| 5869 | MockHttpCache cache; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5870 | net::TestCompletionCallback callback; |
[email protected] | c85316f | 2011-08-18 23:27:36 | [diff] [blame] | 5871 | |
| 5872 | MockTransaction transaction(kSimpleGET_Transaction); |
| 5873 | AddMockTransaction(&transaction); |
| 5874 | transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 5875 | "Content-Length: 100\n" |
[email protected] | 29cc1ce4 | 2012-07-22 18:39:35 | [diff] [blame] | 5876 | "Etag: \"foo\"\n"; |
[email protected] | c85316f | 2011-08-18 23:27:36 | [diff] [blame] | 5877 | RunTransactionTest(cache.http_cache(), transaction); |
| 5878 | RemoveMockTransaction(&transaction); |
| 5879 | |
| 5880 | // Verify that the entry is marked as incomplete. |
| 5881 | disk_cache::Entry* entry; |
| 5882 | ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); |
| 5883 | net::HttpResponseInfo response; |
| 5884 | bool truncated = false; |
| 5885 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
| 5886 | EXPECT_TRUE(truncated); |
| 5887 | entry->Close(); |
| 5888 | } |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 5889 | |
| 5890 | TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit_TransactionDelegate) { |
| 5891 | MockHttpCache cache; |
| 5892 | |
| 5893 | // Write to the cache. |
| 5894 | RunTransactionTestWithDelegate(cache.http_cache(), |
| 5895 | kSimpleGET_Transaction, |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 5896 | 8, |
| 5897 | 3); |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 5898 | |
| 5899 | // Force this transaction to read from the cache. |
| 5900 | MockTransaction transaction(kSimpleGET_Transaction); |
| 5901 | transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 5902 | |
| 5903 | RunTransactionTestWithDelegate(cache.http_cache(), |
| 5904 | kSimpleGET_Transaction, |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 5905 | 5, |
| 5906 | 0); |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 5907 | } |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 5908 | |
| 5909 | // Make sure that calling SetPriority on a cache transaction passes on |
| 5910 | // its priority updates to its underlying network transaction. |
| 5911 | TEST(HttpCache, SetPriority) { |
| 5912 | MockHttpCache cache; |
| 5913 | |
| 5914 | scoped_ptr<net::HttpTransaction> trans; |
| 5915 | EXPECT_EQ(net::OK, cache.http_cache()->CreateTransaction( |
| 5916 | net::IDLE, &trans, NULL)); |
| 5917 | ASSERT_TRUE(trans.get()); |
| 5918 | |
| 5919 | // Shouldn't crash, but doesn't do anything either. |
| 5920 | trans->SetPriority(net::LOW); |
| 5921 | |
| 5922 | EXPECT_FALSE(cache.network_layer()->last_transaction()); |
| 5923 | EXPECT_EQ(net::DEFAULT_PRIORITY, |
| 5924 | cache.network_layer()->last_create_transaction_priority()); |
| 5925 | |
| 5926 | net::HttpRequestInfo info; |
| 5927 | info.url = GURL(kSimpleGET_Transaction.url); |
| 5928 | net::TestCompletionCallback callback; |
| 5929 | EXPECT_EQ(net::ERR_IO_PENDING, |
| 5930 | trans->Start(&info, callback.callback(), net::BoundNetLog())); |
| 5931 | |
| 5932 | ASSERT_TRUE(cache.network_layer()->last_transaction()); |
| 5933 | EXPECT_EQ(net::LOW, |
| 5934 | cache.network_layer()->last_create_transaction_priority()); |
| 5935 | EXPECT_EQ(net::LOW, |
| 5936 | cache.network_layer()->last_transaction()->priority()); |
| 5937 | |
| 5938 | trans->SetPriority(net::HIGHEST); |
| 5939 | EXPECT_EQ(net::LOW, |
| 5940 | cache.network_layer()->last_create_transaction_priority()); |
| 5941 | EXPECT_EQ(net::HIGHEST, |
| 5942 | cache.network_layer()->last_transaction()->priority()); |
| 5943 | |
| 5944 | EXPECT_EQ(net::OK, callback.WaitForResult()); |
| 5945 | } |
| 5946 | |
| 5947 | // Make sure that a cache transaction passes on its priority to |
| 5948 | // newly-created network transactions. |
| 5949 | TEST(HttpCache, SetPriorityNewTransaction) { |
| 5950 | MockHttpCache cache; |
| 5951 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5952 | |
| 5953 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 5954 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 5955 | "ETag: \"foo\"\n" |
| 5956 | "Accept-Ranges: bytes\n" |
| 5957 | "Content-Length: 80\n"); |
| 5958 | CreateTruncatedEntry(raw_headers, &cache); |
| 5959 | |
| 5960 | // Now make a regular request. |
| 5961 | std::string headers; |
| 5962 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 5963 | transaction.request_headers = EXTRA_HEADER; |
| 5964 | transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " |
| 5965 | "rg: 50-59 rg: 60-69 rg: 70-79 "; |
| 5966 | |
| 5967 | scoped_ptr<net::HttpTransaction> trans; |
| 5968 | EXPECT_EQ(net::OK, cache.http_cache()->CreateTransaction( |
| 5969 | net::MEDIUM, &trans, NULL)); |
| 5970 | ASSERT_TRUE(trans.get()); |
| 5971 | EXPECT_EQ(net::DEFAULT_PRIORITY, |
| 5972 | cache.network_layer()->last_create_transaction_priority()); |
| 5973 | |
| 5974 | MockHttpRequest info(transaction); |
| 5975 | net::TestCompletionCallback callback; |
| 5976 | EXPECT_EQ(net::ERR_IO_PENDING, |
| 5977 | trans->Start(&info, callback.callback(), net::BoundNetLog())); |
| 5978 | EXPECT_EQ(net::OK, callback.WaitForResult()); |
| 5979 | |
| 5980 | EXPECT_EQ(net::MEDIUM, |
| 5981 | cache.network_layer()->last_create_transaction_priority()); |
| 5982 | |
| 5983 | trans->SetPriority(net::HIGHEST); |
| 5984 | // Should trigger a new network transaction and pick up the new |
| 5985 | // priority. |
| 5986 | ReadAndVerifyTransaction(trans.get(), transaction); |
| 5987 | |
| 5988 | EXPECT_EQ(net::HIGHEST, |
| 5989 | cache.network_layer()->last_create_transaction_priority()); |
| 5990 | |
| 5991 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5992 | } |