[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 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
[email protected] | c47c037 | 2014-03-12 23:07:02 | [diff] [blame] | 9 | #include <algorithm> |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 10 | #include <vector> |
[email protected] | c47c037 | 2014-03-12 23:07:02 | [diff] [blame] | 11 | |
[email protected] | f98ead6 | 2011-10-20 01:24:20 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 13 | #include "base/bind_helpers.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 14 | #include "base/macros.h" |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 15 | #include "base/memory/scoped_ptr.h" |
[email protected] | 18b57741 | 2013-07-18 04:19:15 | [diff] [blame] | 16 | #include "base/message_loop/message_loop.h" |
ricea | 64c07d79 | 2014-10-08 03:37:00 | [diff] [blame] | 17 | #include "base/run_loop.h" |
[email protected] | 125ef48 | 2013-06-11 18:32:47 | [diff] [blame] | 18 | #include "base/strings/string_util.h" |
| 19 | #include "base/strings/stringprintf.h" |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 20 | #include "base/test/simple_test_clock.h" |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 21 | #include "net/base/cache_type.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 22 | #include "net/base/elements_upload_data_stream.h" |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 23 | #include "net/base/host_port_pair.h" |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 24 | #include "net/base/ip_endpoint.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 25 | #include "net/base/load_flags.h" |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 26 | #include "net/base/load_timing_info.h" |
| 27 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 28 | #include "net/base/net_errors.h" |
davidben | 2ec0ed34 | 2015-06-08 21:17:28 | [diff] [blame] | 29 | #include "net/base/test_data_directory.h" |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 30 | #include "net/base/upload_bytes_element_reader.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 31 | #include "net/cert/cert_status_flags.h" |
davidben | 2ec0ed34 | 2015-06-08 21:17:28 | [diff] [blame] | 32 | #include "net/cert/x509_certificate.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 33 | #include "net/disk_cache/disk_cache.h" |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 34 | #include "net/http/http_byte_range.h" |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 35 | #include "net/http/http_cache_transaction.h" |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 36 | #include "net/http/http_request_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 37 | #include "net/http/http_request_info.h" |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 38 | #include "net/http/http_response_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 39 | #include "net/http/http_response_info.h" |
| 40 | #include "net/http/http_transaction.h" |
[email protected] | c41737d | 2014-05-14 07:47:19 | [diff] [blame] | 41 | #include "net/http/http_transaction_test_util.h" |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 42 | #include "net/http/http_util.h" |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 43 | #include "net/http/mock_http_cache.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 44 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 45 | #include "net/log/test_net_log_entry.h" |
| 46 | #include "net/log/test_net_log_util.h" |
[email protected] | 7e841a5 | 2013-11-22 09:04:21 | [diff] [blame] | 47 | #include "net/socket/client_socket_handle.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 48 | #include "net/ssl/ssl_cert_request_info.h" |
davidben | 2ec0ed34 | 2015-06-08 21:17:28 | [diff] [blame] | 49 | #include "net/ssl/ssl_connection_status_flags.h" |
| 50 | #include "net/test/cert_test_util.h" |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 51 | #include "net/websockets/websocket_handshake_stream_base.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 52 | #include "testing/gtest/include/gtest/gtest.h" |
| 53 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 54 | using base::Time; |
| 55 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 56 | namespace net { |
| 57 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 58 | namespace { |
| 59 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 60 | // Tests the load timing values of a request that goes through a |
| 61 | // MockNetworkTransaction. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 62 | void TestLoadTimingNetworkRequest(const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 63 | EXPECT_FALSE(load_timing_info.socket_reused); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 64 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 65 | |
| 66 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 67 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 68 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 69 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 70 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 71 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 72 | load_timing_info.send_start); |
| 73 | |
| 74 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 75 | |
| 76 | // Set by URLRequest / URLRequestHttpJob, at a higher level. |
| 77 | EXPECT_TRUE(load_timing_info.request_start_time.is_null()); |
| 78 | EXPECT_TRUE(load_timing_info.request_start.is_null()); |
| 79 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 80 | } |
| 81 | |
| 82 | // Tests the load timing values of a request that receives a cached response. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 83 | void TestLoadTimingCachedResponse(const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 84 | EXPECT_FALSE(load_timing_info.socket_reused); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 85 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 86 | |
| 87 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 88 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 89 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 90 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 91 | |
| 92 | // Only the send start / end times should be sent, and they should have the |
| 93 | // same value. |
| 94 | EXPECT_FALSE(load_timing_info.send_start.is_null()); |
| 95 | EXPECT_EQ(load_timing_info.send_start, load_timing_info.send_end); |
| 96 | |
| 97 | // Set by URLRequest / URLRequestHttpJob, at a higher level. |
| 98 | EXPECT_TRUE(load_timing_info.request_start_time.is_null()); |
| 99 | EXPECT_TRUE(load_timing_info.request_start.is_null()); |
| 100 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 101 | } |
| 102 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 103 | class DeleteCacheCompletionCallback : public TestCompletionCallbackBase { |
[email protected] | 6a9c55c | 2010-08-21 02:04:08 | [diff] [blame] | 104 | public: |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 105 | explicit DeleteCacheCompletionCallback(MockHttpCache* cache) |
| 106 | : cache_(cache), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 107 | callback_(base::Bind(&DeleteCacheCompletionCallback::OnComplete, |
| 108 | base::Unretained(this))) { |
[email protected] | 2c5c9d5 | 2011-12-07 14:15:36 | [diff] [blame] | 109 | } |
[email protected] | 8ebf9b13 | 2011-12-06 19:11:51 | [diff] [blame] | 110 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 111 | const CompletionCallback& callback() const { return callback_; } |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 112 | |
[email protected] | 6a9c55c | 2010-08-21 02:04:08 | [diff] [blame] | 113 | private: |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 114 | void OnComplete(int result) { |
| 115 | delete cache_; |
| 116 | SetResult(result); |
| 117 | } |
| 118 | |
[email protected] | 6a9c55c | 2010-08-21 02:04:08 | [diff] [blame] | 119 | MockHttpCache* cache_; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 120 | CompletionCallback callback_; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 121 | |
| 122 | DISALLOW_COPY_AND_ASSIGN(DeleteCacheCompletionCallback); |
[email protected] | 6a9c55c | 2010-08-21 02:04:08 | [diff] [blame] | 123 | }; |
| 124 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 125 | //----------------------------------------------------------------------------- |
| 126 | // helpers |
| 127 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 128 | void ReadAndVerifyTransaction(HttpTransaction* trans, |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 129 | const MockTransaction& trans_info) { |
| 130 | std::string content; |
| 131 | int rv = ReadTransaction(trans, &content); |
| 132 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 133 | EXPECT_EQ(OK, rv); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 134 | std::string expected(trans_info.data); |
| 135 | EXPECT_EQ(expected, content); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 136 | } |
| 137 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 138 | void RunTransactionTestBase(HttpCache* cache, |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 139 | const MockTransaction& trans_info, |
| 140 | const MockHttpRequest& request, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 141 | HttpResponseInfo* response_info, |
| 142 | const BoundNetLog& net_log, |
| 143 | LoadTimingInfo* load_timing_info, |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 144 | int64_t* sent_bytes, |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 145 | int64_t* received_bytes, |
| 146 | IPEndPoint* remote_endpoint) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 147 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 148 | |
| 149 | // write to the cache |
| 150 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 151 | scoped_ptr<HttpTransaction> trans; |
| 152 | int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &trans); |
| 153 | EXPECT_EQ(OK, rv); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 154 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 155 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 156 | rv = trans->Start(&request, callback.callback(), net_log); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 157 | if (rv == ERR_IO_PENDING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 158 | rv = callback.WaitForResult(); |
[email protected] | 2ef5d00e | 2013-03-23 16:17:27 | [diff] [blame] | 159 | ASSERT_EQ(trans_info.return_code, rv); |
| 160 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 161 | if (OK != rv) |
[email protected] | 2ef5d00e | 2013-03-23 16:17:27 | [diff] [blame] | 162 | return; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 163 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 164 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 165 | ASSERT_TRUE(response); |
| 166 | |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 167 | if (response_info) |
| 168 | *response_info = *response; |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 169 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 170 | if (load_timing_info) { |
| 171 | // If a fake network connection is used, need a NetLog to get a fake socket |
| 172 | // ID. |
| 173 | EXPECT_TRUE(net_log.net_log()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 174 | *load_timing_info = LoadTimingInfo(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 175 | trans->GetLoadTimingInfo(load_timing_info); |
| 176 | } |
| 177 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 178 | if (remote_endpoint) |
| 179 | ASSERT_TRUE(trans->GetRemoteEndpoint(remote_endpoint)); |
| 180 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 181 | ReadAndVerifyTransaction(trans.get(), trans_info); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 182 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 183 | if (sent_bytes) |
| 184 | *sent_bytes = trans->GetTotalSentBytes(); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 185 | if (received_bytes) |
| 186 | *received_bytes = trans->GetTotalReceivedBytes(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 187 | } |
| 188 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 189 | void RunTransactionTestWithRequest(HttpCache* cache, |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 190 | const MockTransaction& trans_info, |
| 191 | const MockHttpRequest& request, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 192 | HttpResponseInfo* response_info) { |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 193 | RunTransactionTestBase(cache, trans_info, request, response_info, |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 194 | BoundNetLog(), nullptr, nullptr, nullptr, nullptr); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 195 | } |
| 196 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 197 | void RunTransactionTestAndGetTiming(HttpCache* cache, |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 198 | const MockTransaction& trans_info, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 199 | const BoundNetLog& log, |
| 200 | LoadTimingInfo* load_timing_info) { |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 201 | RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 202 | nullptr, log, load_timing_info, nullptr, nullptr, |
| 203 | nullptr); |
| 204 | } |
| 205 | |
| 206 | void RunTransactionTestAndGetTimingAndConnectedSocketAddress( |
| 207 | HttpCache* cache, |
| 208 | const MockTransaction& trans_info, |
| 209 | const BoundNetLog& log, |
| 210 | LoadTimingInfo* load_timing_info, |
| 211 | IPEndPoint* remote_endpoint) { |
| 212 | RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), |
| 213 | nullptr, log, load_timing_info, nullptr, nullptr, |
| 214 | remote_endpoint); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 215 | } |
| 216 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 217 | void RunTransactionTest(HttpCache* cache, const MockTransaction& trans_info) { |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 218 | RunTransactionTestAndGetTiming(cache, trans_info, BoundNetLog(), nullptr); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 219 | } |
| 220 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 221 | void RunTransactionTestWithLog(HttpCache* cache, |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 222 | const MockTransaction& trans_info, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 223 | const BoundNetLog& log) { |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 224 | RunTransactionTestAndGetTiming(cache, trans_info, log, nullptr); |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 225 | } |
| 226 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 227 | void RunTransactionTestWithResponseInfo(HttpCache* cache, |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 228 | const MockTransaction& trans_info, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 229 | HttpResponseInfo* response) { |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 230 | RunTransactionTestWithRequest(cache, trans_info, MockHttpRequest(trans_info), |
| 231 | response); |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 234 | void RunTransactionTestWithResponseInfoAndGetTiming( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 235 | HttpCache* cache, |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 236 | const MockTransaction& trans_info, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 237 | HttpResponseInfo* response, |
| 238 | const BoundNetLog& log, |
| 239 | LoadTimingInfo* load_timing_info) { |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 240 | RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 241 | response, log, load_timing_info, nullptr, nullptr, |
| 242 | nullptr); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 243 | } |
| 244 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 245 | void RunTransactionTestWithResponse(HttpCache* cache, |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 246 | const MockTransaction& trans_info, |
| 247 | std::string* response_headers) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 248 | HttpResponseInfo response; |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 249 | RunTransactionTestWithResponseInfo(cache, trans_info, &response); |
| 250 | response.headers->GetNormalizedHeaders(response_headers); |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 251 | } |
| 252 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 253 | void RunTransactionTestWithResponseAndGetTiming( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 254 | HttpCache* cache, |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 255 | const MockTransaction& trans_info, |
| 256 | std::string* response_headers, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 257 | const BoundNetLog& log, |
| 258 | LoadTimingInfo* load_timing_info) { |
| 259 | HttpResponseInfo response; |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 260 | RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 261 | &response, log, load_timing_info, nullptr, nullptr, |
| 262 | nullptr); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 263 | response.headers->GetNormalizedHeaders(response_headers); |
| 264 | } |
| 265 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 266 | // This class provides a handler for kFastNoStoreGET_Transaction so that the |
| 267 | // no-store header can be included on demand. |
| 268 | class FastTransactionServer { |
| 269 | public: |
| 270 | FastTransactionServer() { |
| 271 | no_store = false; |
| 272 | } |
| 273 | ~FastTransactionServer() {} |
| 274 | |
| 275 | void set_no_store(bool value) { no_store = value; } |
| 276 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 277 | static void FastNoStoreHandler(const HttpRequestInfo* request, |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 278 | std::string* response_status, |
| 279 | std::string* response_headers, |
| 280 | std::string* response_data) { |
| 281 | if (no_store) |
| 282 | *response_headers = "Cache-Control: no-store\n"; |
| 283 | } |
| 284 | |
| 285 | private: |
| 286 | static bool no_store; |
| 287 | DISALLOW_COPY_AND_ASSIGN(FastTransactionServer); |
| 288 | }; |
| 289 | bool FastTransactionServer::no_store; |
| 290 | |
| 291 | const MockTransaction kFastNoStoreGET_Transaction = { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 292 | "http://www.google.com/nostore", |
| 293 | "GET", |
| 294 | base::Time(), |
| 295 | "", |
| 296 | LOAD_VALIDATE_CACHE, |
| 297 | "HTTP/1.1 200 OK", |
| 298 | "Cache-Control: max-age=10000\n", |
| 299 | base::Time(), |
| 300 | "<html><body>Google Blah Blah</body></html>", |
| 301 | TEST_MODE_SYNC_NET_START, |
| 302 | &FastTransactionServer::FastNoStoreHandler, |
davidben | 2ec0ed34 | 2015-06-08 21:17:28 | [diff] [blame] | 303 | nullptr, |
| 304 | 0, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 305 | 0, |
| 306 | OK}; |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 307 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 308 | // This class provides a handler for kRangeGET_TransactionOK so that the range |
| 309 | // request can be served on demand. |
| 310 | class RangeTransactionServer { |
| 311 | public: |
| 312 | RangeTransactionServer() { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 313 | not_modified_ = false; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 314 | modified_ = false; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 315 | bad_200_ = false; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 316 | } |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 317 | ~RangeTransactionServer() { |
| 318 | not_modified_ = false; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 319 | modified_ = false; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 320 | bad_200_ = false; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 321 | } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 322 | |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 323 | // Returns only 416 or 304 when set. |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 324 | void set_not_modified(bool value) { not_modified_ = value; } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 325 | |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 326 | // Returns 206 when revalidating a range (instead of 304). |
| 327 | void set_modified(bool value) { modified_ = value; } |
| 328 | |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 329 | // Returns 200 instead of 206 (a malformed response overall). |
| 330 | void set_bad_200(bool value) { bad_200_ = value; } |
| 331 | |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 332 | // Other than regular range related behavior (and the flags mentioned above), |
| 333 | // the server reacts to requests headers like so: |
| 334 | // X-Require-Mock-Auth -> return 401. |
liberato | 1f59bb3f | 2015-05-29 14:19:10 | [diff] [blame] | 335 | // X-Require-Mock-Auth-Alt -> return 401. |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 336 | // X-Return-Default-Range -> assume 40-49 was requested. |
liberato | 1f59bb3f | 2015-05-29 14:19:10 | [diff] [blame] | 337 | // The -Alt variant doesn't cause the MockNetworkTransaction to |
| 338 | // report that it IsReadyToRestartForAuth(). |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 339 | static void RangeHandler(const HttpRequestInfo* request, |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 340 | std::string* response_status, |
| 341 | std::string* response_headers, |
| 342 | std::string* response_data); |
| 343 | |
| 344 | private: |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 345 | static bool not_modified_; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 346 | static bool modified_; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 347 | static bool bad_200_; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 348 | DISALLOW_COPY_AND_ASSIGN(RangeTransactionServer); |
| 349 | }; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 350 | bool RangeTransactionServer::not_modified_ = false; |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 351 | bool RangeTransactionServer::modified_ = false; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 352 | bool RangeTransactionServer::bad_200_ = false; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 353 | |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 354 | // A dummy extra header that must be preserved on a given request. |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 355 | |
| 356 | // EXTRA_HEADER_LINE doesn't include a line terminator because it |
| 357 | // will be passed to AddHeaderFromString() which doesn't accept them. |
| 358 | #define EXTRA_HEADER_LINE "Extra: header" |
| 359 | |
| 360 | // EXTRA_HEADER contains a line terminator, as expected by |
| 361 | // AddHeadersFromString() (_not_ AddHeaderFromString()). |
| 362 | #define EXTRA_HEADER EXTRA_HEADER_LINE "\r\n" |
| 363 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 364 | static const char kExtraHeaderKey[] = "Extra"; |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 365 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 366 | // Static. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 367 | void RangeTransactionServer::RangeHandler(const HttpRequestInfo* request, |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 368 | std::string* response_status, |
| 369 | std::string* response_headers, |
| 370 | std::string* response_data) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 371 | if (request->extra_headers.IsEmpty()) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 372 | response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 373 | response_data->clear(); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 374 | return; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 375 | } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 376 | |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 377 | // We want to make sure we don't delete extra headers. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 378 | EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 379 | |
liberato | 1f59bb3f | 2015-05-29 14:19:10 | [diff] [blame] | 380 | bool require_auth = |
| 381 | request->extra_headers.HasHeader("X-Require-Mock-Auth") || |
| 382 | request->extra_headers.HasHeader("X-Require-Mock-Auth-Alt"); |
| 383 | |
| 384 | if (require_auth && !request->extra_headers.HasHeader("Authorization")) { |
[email protected] | d7358ba | 2014-01-04 01:39:49 | [diff] [blame] | 385 | response_status->assign("HTTP/1.1 401 Unauthorized"); |
| 386 | response_data->assign("WWW-Authenticate: Foo\n"); |
| 387 | return; |
| 388 | } |
| 389 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 390 | if (not_modified_) { |
| 391 | response_status->assign("HTTP/1.1 304 Not Modified"); |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 392 | response_data->clear(); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 393 | return; |
| 394 | } |
| 395 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 396 | std::vector<HttpByteRange> ranges; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 397 | std::string range_header; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 398 | if (!request->extra_headers.GetHeader(HttpRequestHeaders::kRange, |
| 399 | &range_header) || |
| 400 | !HttpUtil::ParseRangeHeader(range_header, &ranges) || bad_200_ || |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 401 | ranges.size() != 1) { |
| 402 | // This is not a byte range request. We return 200. |
| 403 | response_status->assign("HTTP/1.1 200 OK"); |
| 404 | response_headers->assign("Date: Wed, 28 Nov 2007 09:40:09 GMT"); |
| 405 | response_data->assign("Not a range"); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 406 | return; |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 407 | } |
| 408 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 409 | // We can handle this range request. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 410 | HttpByteRange byte_range = ranges[0]; |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 411 | |
| 412 | if (request->extra_headers.HasHeader("X-Return-Default-Range")) { |
| 413 | byte_range.set_first_byte_position(40); |
| 414 | byte_range.set_last_byte_position(49); |
| 415 | } |
| 416 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 417 | if (byte_range.first_byte_position() > 79) { |
| 418 | response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 419 | response_data->clear(); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 420 | return; |
| 421 | } |
| 422 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 423 | EXPECT_TRUE(byte_range.ComputeBounds(80)); |
| 424 | int start = static_cast<int>(byte_range.first_byte_position()); |
| 425 | int end = static_cast<int>(byte_range.last_byte_position()); |
| 426 | |
| 427 | EXPECT_LT(end, 80); |
| 428 | |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 429 | std::string content_range = base::StringPrintf( |
| 430 | "Content-Range: bytes %d-%d/80\n", start, end); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 431 | response_headers->append(content_range); |
| 432 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 433 | if (!request->extra_headers.HasHeader("If-None-Match") || modified_) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 434 | std::string data; |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 435 | if (end == start) { |
| 436 | EXPECT_EQ(0, end % 10); |
| 437 | data = "r"; |
| 438 | } else { |
| 439 | EXPECT_EQ(9, (end - start) % 10); |
| 440 | for (int block_start = start; block_start < end; block_start += 10) { |
| 441 | base::StringAppendF(&data, "rg: %02d-%02d ", |
| 442 | block_start, block_start + 9); |
| 443 | } |
[email protected] | a77fa2dc | 2010-11-15 12:11:11 | [diff] [blame] | 444 | } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 445 | *response_data = data; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 446 | |
| 447 | if (end - start != 9) { |
| 448 | // We also have to fix content-length. |
| 449 | int len = end - start + 1; |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 450 | std::string content_length = base::StringPrintf("Content-Length: %d\n", |
| 451 | len); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 452 | response_headers->replace(response_headers->find("Content-Length:"), |
| 453 | content_length.size(), content_length); |
| 454 | } |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 455 | } else { |
| 456 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 457 | response_data->clear(); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | const MockTransaction kRangeGET_TransactionOK = { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 462 | "http://www.google.com/range", |
| 463 | "GET", |
| 464 | base::Time(), |
| 465 | "Range: bytes = 40-49\r\n" EXTRA_HEADER, |
| 466 | LOAD_NORMAL, |
| 467 | "HTTP/1.1 206 Partial Content", |
| 468 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 469 | "ETag: \"foo\"\n" |
| 470 | "Accept-Ranges: bytes\n" |
| 471 | "Content-Length: 10\n", |
| 472 | base::Time(), |
| 473 | "rg: 40-49 ", |
| 474 | TEST_MODE_NORMAL, |
| 475 | &RangeTransactionServer::RangeHandler, |
davidben | 2ec0ed34 | 2015-06-08 21:17:28 | [diff] [blame] | 476 | nullptr, |
| 477 | 0, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 478 | 0, |
| 479 | OK}; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 480 | |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 481 | const char kFullRangeData[] = |
| 482 | "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 " |
| 483 | "rg: 40-49 rg: 50-59 rg: 60-69 rg: 70-79 "; |
| 484 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 485 | // Verifies the response headers (|response|) match a partial content |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 486 | // response for the range starting at |start| and ending at |end|. |
ki.stfu | 144dce1 | 2015-09-22 01:07:57 | [diff] [blame] | 487 | void Verify206Response(const std::string& response, int start, int end) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 488 | std::string raw_headers( |
| 489 | HttpUtil::AssembleRawHeaders(response.data(), response.size())); |
| 490 | scoped_refptr<HttpResponseHeaders> headers( |
| 491 | new HttpResponseHeaders(raw_headers)); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 492 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 493 | ASSERT_EQ(206, headers->response_code()); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 494 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 495 | int64_t range_start, range_end, object_size; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 496 | ASSERT_TRUE( |
| 497 | headers->GetContentRange(&range_start, &range_end, &object_size)); |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 498 | int64_t content_length = headers->GetContentLength(); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 499 | |
| 500 | int length = end - start + 1; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 501 | ASSERT_EQ(length, content_length); |
| 502 | ASSERT_EQ(start, range_start); |
| 503 | ASSERT_EQ(end, range_end); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 504 | } |
| 505 | |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 506 | // Creates a truncated entry that can be resumed using byte ranges. |
| 507 | void CreateTruncatedEntry(std::string raw_headers, MockHttpCache* cache) { |
| 508 | // Create a disk cache entry that stores an incomplete resource. |
| 509 | disk_cache::Entry* entry; |
| 510 | ASSERT_TRUE(cache->CreateBackendEntry(kRangeGET_TransactionOK.url, &entry, |
| 511 | NULL)); |
| 512 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 513 | raw_headers = |
| 514 | HttpUtil::AssembleRawHeaders(raw_headers.data(), raw_headers.size()); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 515 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 516 | HttpResponseInfo response; |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 517 | response.response_time = base::Time::Now(); |
| 518 | response.request_time = base::Time::Now(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 519 | response.headers = new HttpResponseHeaders(raw_headers); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 520 | // Set the last argument for this to be an incomplete request. |
| 521 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true)); |
| 522 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 523 | scoped_refptr<IOBuffer> buf(new IOBuffer(100)); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 524 | int len = static_cast<int>(base::strlcpy(buf->data(), |
| 525 | "rg: 00-09 rg: 10-19 ", 100)); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 526 | TestCompletionCallback cb; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 527 | int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 528 | EXPECT_EQ(len, cb.GetResult(rv)); |
| 529 | entry->Close(); |
| 530 | } |
| 531 | |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 532 | // Verifies that there's an entry with this |key| with the truncated flag set to |
| 533 | // |flag_value|, and with an optional |data_size| (if not zero). |
| 534 | void VerifyTruncatedFlag(MockHttpCache* cache, |
| 535 | const std::string& key, |
| 536 | bool flag_value, |
| 537 | int data_size) { |
| 538 | disk_cache::Entry* entry; |
| 539 | ASSERT_TRUE(cache->OpenBackendEntry(key, &entry)); |
| 540 | disk_cache::ScopedEntryPtr closer(entry); |
| 541 | |
| 542 | HttpResponseInfo response; |
| 543 | bool truncated = !flag_value; |
| 544 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
| 545 | EXPECT_EQ(flag_value, truncated); |
| 546 | if (data_size) |
| 547 | EXPECT_EQ(data_size, entry->GetDataSize(1)); |
| 548 | } |
| 549 | |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 550 | // Helper to represent a network HTTP response. |
| 551 | struct Response { |
| 552 | // Set this response into |trans|. |
| 553 | void AssignTo(MockTransaction* trans) const { |
| 554 | trans->status = status; |
| 555 | trans->response_headers = headers; |
| 556 | trans->data = body; |
| 557 | } |
| 558 | |
| 559 | std::string status_and_headers() const { |
| 560 | return std::string(status) + "\n" + std::string(headers); |
| 561 | } |
| 562 | |
| 563 | const char* status; |
| 564 | const char* headers; |
| 565 | const char* body; |
| 566 | }; |
| 567 | |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 568 | struct Context { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 569 | Context() : result(ERR_IO_PENDING) {} |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 570 | |
| 571 | int result; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 572 | TestCompletionCallback callback; |
| 573 | scoped_ptr<HttpTransaction> trans; |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 574 | }; |
| 575 | |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 576 | class FakeWebSocketHandshakeStreamCreateHelper |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 577 | : public WebSocketHandshakeStreamBase::CreateHelper { |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 578 | public: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 579 | ~FakeWebSocketHandshakeStreamCreateHelper() override {} |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 580 | WebSocketHandshakeStreamBase* CreateBasicStream( |
| 581 | scoped_ptr<ClientSocketHandle> connect, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 582 | bool using_proxy) override { |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 583 | return NULL; |
| 584 | } |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 585 | WebSocketHandshakeStreamBase* CreateSpdyStream( |
| 586 | const base::WeakPtr<SpdySession>& session, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 587 | bool use_relative_url) override { |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 588 | return NULL; |
| 589 | } |
| 590 | }; |
| 591 | |
[email protected] | c47c037 | 2014-03-12 23:07:02 | [diff] [blame] | 592 | // Returns true if |entry| is not one of the log types paid attention to in this |
| 593 | // test. Note that TYPE_HTTP_CACHE_WRITE_INFO and TYPE_HTTP_CACHE_*_DATA are |
| 594 | // ignored. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 595 | bool ShouldIgnoreLogEntry(const TestNetLogEntry& entry) { |
[email protected] | c47c037 | 2014-03-12 23:07:02 | [diff] [blame] | 596 | switch (entry.type) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 597 | case NetLog::TYPE_HTTP_CACHE_GET_BACKEND: |
| 598 | case NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY: |
| 599 | case NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY: |
| 600 | case NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY: |
| 601 | case NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY: |
| 602 | case NetLog::TYPE_HTTP_CACHE_READ_INFO: |
[email protected] | c47c037 | 2014-03-12 23:07:02 | [diff] [blame] | 603 | return false; |
| 604 | default: |
| 605 | return true; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | // Modifies |entries| to only include log entries created by the cache layer and |
| 610 | // asserted on in these tests. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 611 | void FilterLogEntries(TestNetLogEntry::List* entries) { |
[email protected] | c47c037 | 2014-03-12 23:07:02 | [diff] [blame] | 612 | entries->erase(std::remove_if(entries->begin(), entries->end(), |
| 613 | &ShouldIgnoreLogEntry), |
| 614 | entries->end()); |
| 615 | } |
| 616 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 617 | bool LogContainsEventType(const BoundTestNetLog& log, |
| 618 | NetLog::EventType expected) { |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 619 | TestNetLogEntry::List entries; |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 620 | log.GetEntries(&entries); |
| 621 | for (size_t i = 0; i < entries.size(); i++) { |
| 622 | if (entries[i].type == expected) |
| 623 | return true; |
| 624 | } |
| 625 | return false; |
| 626 | } |
| 627 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 628 | } // namespace |
| 629 | |
| 630 | |
| 631 | //----------------------------------------------------------------------------- |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 632 | // Tests. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 633 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 634 | TEST(HttpCache, CreateThenDestroy) { |
| 635 | MockHttpCache cache; |
| 636 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 637 | scoped_ptr<HttpTransaction> trans; |
| 638 | EXPECT_EQ(OK, cache.CreateTransaction(&trans)); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 639 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 640 | } |
| 641 | |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 642 | TEST(HttpCache, GetBackend) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 643 | MockHttpCache cache(HttpCache::DefaultBackend::InMemory(0)); |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 644 | |
[email protected] | 6a98903 | 2010-06-14 19:05:33 | [diff] [blame] | 645 | disk_cache::Backend* backend; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 646 | TestCompletionCallback cb; |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 647 | // This will lazily initialize the backend. |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 648 | int rv = cache.http_cache()->GetBackend(&backend, cb.callback()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 649 | EXPECT_EQ(OK, cb.GetResult(rv)); |
[email protected] | cfc076ec | 2009-11-07 02:27:23 | [diff] [blame] | 650 | } |
| 651 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 652 | TEST(HttpCache, SimpleGET) { |
| 653 | MockHttpCache cache; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 654 | BoundTestNetLog log; |
| 655 | LoadTimingInfo load_timing_info; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 656 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 657 | // Write to the cache. |
| 658 | RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction, |
| 659 | log.bound(), &load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 660 | |
| 661 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 662 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 663 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 664 | TestLoadTimingNetworkRequest(load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | TEST(HttpCache, SimpleGETNoDiskCache) { |
| 668 | MockHttpCache cache; |
| 669 | |
| 670 | cache.disk_cache()->set_fail_requests(); |
| 671 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 672 | BoundTestNetLog log; |
| 673 | LoadTimingInfo load_timing_info; |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 674 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 675 | // Read from the network, and don't use the cache. |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 676 | RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction, |
| 677 | log.bound(), &load_timing_info); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 678 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 679 | // Check that the NetLog was filled as expected. |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 680 | // (We attempted to both Open and Create entries, but both failed). |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 681 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 682 | log.GetEntries(&entries); |
[email protected] | c47c037 | 2014-03-12 23:07:02 | [diff] [blame] | 683 | FilterLogEntries(&entries); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 684 | |
| 685 | EXPECT_EQ(6u, entries.size()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 686 | EXPECT_TRUE( |
| 687 | LogContainsBeginEvent(entries, 0, NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
| 688 | EXPECT_TRUE( |
| 689 | LogContainsEndEvent(entries, 1, NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
| 690 | EXPECT_TRUE( |
| 691 | LogContainsBeginEvent(entries, 2, NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
| 692 | EXPECT_TRUE( |
| 693 | LogContainsEndEvent(entries, 3, NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
| 694 | EXPECT_TRUE( |
| 695 | LogContainsBeginEvent(entries, 4, NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
| 696 | EXPECT_TRUE( |
| 697 | LogContainsEndEvent(entries, 5, NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 698 | |
| 699 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 700 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 701 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 702 | TestLoadTimingNetworkRequest(load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 703 | } |
| 704 | |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 705 | TEST(HttpCache, SimpleGETNoDiskCache2) { |
| 706 | // This will initialize a cache object with NULL backend. |
mmenke | bc31a2c | 2015-10-29 13:44:45 | [diff] [blame] | 707 | scoped_ptr<MockBlockingBackendFactory> factory( |
| 708 | new MockBlockingBackendFactory()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 709 | factory->set_fail(true); |
| 710 | factory->FinishCreation(); // We'll complete synchronously. |
mmenke | bc31a2c | 2015-10-29 13:44:45 | [diff] [blame] | 711 | MockHttpCache cache(factory.Pass()); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 712 | |
| 713 | // Read from the network, and don't use the cache. |
| 714 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 715 | |
| 716 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
[email protected] | 6a98903 | 2010-06-14 19:05:33 | [diff] [blame] | 717 | EXPECT_FALSE(cache.http_cache()->GetCurrentBackend()); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 718 | } |
| 719 | |
[email protected] | f6c9d56 | 2013-01-15 19:28:13 | [diff] [blame] | 720 | // Tests that IOBuffers are not referenced after IO completes. |
| 721 | TEST(HttpCache, ReleaseBuffer) { |
| 722 | MockHttpCache cache; |
| 723 | |
| 724 | // Write to the cache. |
| 725 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 726 | |
| 727 | MockHttpRequest request(kSimpleGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 728 | scoped_ptr<HttpTransaction> trans; |
| 729 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
[email protected] | f6c9d56 | 2013-01-15 19:28:13 | [diff] [blame] | 730 | |
| 731 | const int kBufferSize = 10; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 732 | scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); |
| 733 | ReleaseBufferCompletionCallback cb(buffer.get()); |
[email protected] | f6c9d56 | 2013-01-15 19:28:13 | [diff] [blame] | 734 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 735 | int rv = trans->Start(&request, cb.callback(), BoundNetLog()); |
| 736 | EXPECT_EQ(OK, cb.GetResult(rv)); |
[email protected] | f6c9d56 | 2013-01-15 19:28:13 | [diff] [blame] | 737 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 738 | rv = trans->Read(buffer.get(), kBufferSize, cb.callback()); |
[email protected] | f6c9d56 | 2013-01-15 19:28:13 | [diff] [blame] | 739 | EXPECT_EQ(kBufferSize, cb.GetResult(rv)); |
| 740 | } |
| 741 | |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 742 | TEST(HttpCache, SimpleGETWithDiskFailures) { |
| 743 | MockHttpCache cache; |
| 744 | |
| 745 | cache.disk_cache()->set_soft_failures(true); |
| 746 | |
| 747 | // Read from the network, and fail to write to the cache. |
| 748 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 749 | |
| 750 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 751 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 752 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 753 | |
| 754 | // This one should see an empty cache again. |
| 755 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 756 | |
| 757 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 758 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 759 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 760 | } |
| 761 | |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 762 | // Tests that disk failures after the transaction has started don't cause the |
| 763 | // request to fail. |
| 764 | TEST(HttpCache, SimpleGETWithDiskFailures2) { |
| 765 | MockHttpCache cache; |
| 766 | |
| 767 | MockHttpRequest request(kSimpleGET_Transaction); |
| 768 | |
| 769 | scoped_ptr<Context> c(new Context()); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 770 | int rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 771 | ASSERT_EQ(OK, rv); |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 772 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 773 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 774 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 775 | rv = c->callback.WaitForResult(); |
| 776 | |
| 777 | // Start failing request now. |
| 778 | cache.disk_cache()->set_soft_failures(true); |
| 779 | |
| 780 | // We have to open the entry again to propagate the failure flag. |
| 781 | disk_cache::Entry* en; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 782 | ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &en)); |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 783 | en->Close(); |
| 784 | |
| 785 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 786 | c.reset(); |
| 787 | |
| 788 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 789 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 790 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 791 | |
| 792 | // This one should see an empty cache again. |
| 793 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 794 | |
| 795 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 796 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 797 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 798 | } |
| 799 | |
[email protected] | 93fe7516 | 2012-02-09 21:51:31 | [diff] [blame] | 800 | // Tests that we handle failures to read from the cache. |
[email protected] | 4a24453 | 2011-04-04 02:10:33 | [diff] [blame] | 801 | TEST(HttpCache, SimpleGETWithDiskFailures3) { |
| 802 | MockHttpCache cache; |
| 803 | |
| 804 | // Read from the network, and write to the cache. |
| 805 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 806 | |
| 807 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 808 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 809 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 810 | |
| 811 | cache.disk_cache()->set_soft_failures(true); |
| 812 | |
| 813 | // Now fail to read from the cache. |
| 814 | scoped_ptr<Context> c(new Context()); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 815 | int rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 816 | ASSERT_EQ(OK, rv); |
[email protected] | 4a24453 | 2011-04-04 02:10:33 | [diff] [blame] | 817 | |
| 818 | MockHttpRequest request(kSimpleGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 819 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 820 | EXPECT_EQ(OK, c->callback.GetResult(rv)); |
[email protected] | 93fe7516 | 2012-02-09 21:51:31 | [diff] [blame] | 821 | |
| 822 | // Now verify that the entry was removed from the cache. |
| 823 | cache.disk_cache()->set_soft_failures(false); |
| 824 | |
[email protected] | 93fe7516 | 2012-02-09 21:51:31 | [diff] [blame] | 825 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 826 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 827 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | 40caa4c | 2012-03-20 20:42:58 | [diff] [blame] | 828 | |
| 829 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 830 | |
| 831 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 832 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 833 | EXPECT_EQ(3, cache.disk_cache()->create_count()); |
[email protected] | 4a24453 | 2011-04-04 02:10:33 | [diff] [blame] | 834 | } |
| 835 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 836 | TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) { |
| 837 | MockHttpCache cache; |
| 838 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 839 | BoundTestNetLog log; |
| 840 | LoadTimingInfo load_timing_info; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 841 | |
| 842 | // Write to the cache. |
| 843 | RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction, |
| 844 | log.bound(), &load_timing_info); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 845 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 846 | // Check that the NetLog was filled as expected. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 847 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 848 | log.GetEntries(&entries); |
[email protected] | c47c037 | 2014-03-12 23:07:02 | [diff] [blame] | 849 | FilterLogEntries(&entries); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 850 | |
| 851 | EXPECT_EQ(8u, entries.size()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 852 | EXPECT_TRUE( |
| 853 | LogContainsBeginEvent(entries, 0, NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
| 854 | EXPECT_TRUE( |
| 855 | LogContainsEndEvent(entries, 1, NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
| 856 | EXPECT_TRUE( |
| 857 | LogContainsBeginEvent(entries, 2, NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
| 858 | EXPECT_TRUE( |
| 859 | LogContainsEndEvent(entries, 3, NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
| 860 | EXPECT_TRUE( |
| 861 | LogContainsBeginEvent(entries, 4, NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
| 862 | EXPECT_TRUE( |
| 863 | LogContainsEndEvent(entries, 5, NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
| 864 | EXPECT_TRUE( |
| 865 | LogContainsBeginEvent(entries, 6, NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); |
| 866 | EXPECT_TRUE( |
| 867 | LogContainsEndEvent(entries, 7, NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 868 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 869 | TestLoadTimingNetworkRequest(load_timing_info); |
| 870 | |
| 871 | // Force this transaction to read from the cache. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 872 | MockTransaction transaction(kSimpleGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 873 | transaction.load_flags |= LOAD_ONLY_FROM_CACHE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 874 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 875 | log.Clear(); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 876 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 877 | RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 878 | &load_timing_info); |
[email protected] | baff44a | 2009-09-06 00:48:10 | [diff] [blame] | 879 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 880 | // Check that the NetLog was filled as expected. |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 881 | log.GetEntries(&entries); |
[email protected] | c47c037 | 2014-03-12 23:07:02 | [diff] [blame] | 882 | FilterLogEntries(&entries); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 883 | |
| 884 | EXPECT_EQ(8u, entries.size()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 885 | EXPECT_TRUE( |
| 886 | LogContainsBeginEvent(entries, 0, NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
| 887 | EXPECT_TRUE( |
| 888 | LogContainsEndEvent(entries, 1, NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
| 889 | EXPECT_TRUE( |
| 890 | LogContainsBeginEvent(entries, 2, NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
| 891 | EXPECT_TRUE( |
| 892 | LogContainsEndEvent(entries, 3, NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); |
| 893 | EXPECT_TRUE( |
| 894 | LogContainsBeginEvent(entries, 4, NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); |
| 895 | EXPECT_TRUE( |
| 896 | LogContainsEndEvent(entries, 5, NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); |
| 897 | EXPECT_TRUE( |
| 898 | LogContainsBeginEvent(entries, 6, NetLog::TYPE_HTTP_CACHE_READ_INFO)); |
| 899 | EXPECT_TRUE( |
| 900 | LogContainsEndEvent(entries, 7, NetLog::TYPE_HTTP_CACHE_READ_INFO)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 901 | |
| 902 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 903 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 904 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 905 | TestLoadTimingCachedResponse(load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { |
| 909 | MockHttpCache cache; |
| 910 | |
| 911 | // force this transaction to read from the cache |
| 912 | MockTransaction transaction(kSimpleGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 913 | transaction.load_flags |= LOAD_ONLY_FROM_CACHE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 914 | |
| 915 | MockHttpRequest request(transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 916 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 917 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 918 | scoped_ptr<HttpTransaction> trans; |
| 919 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 920 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 921 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 922 | if (rv == ERR_IO_PENDING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 923 | rv = callback.WaitForResult(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 924 | ASSERT_EQ(ERR_CACHE_MISS, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 925 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 926 | trans.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 927 | |
| 928 | EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 929 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 930 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 931 | } |
| 932 | |
| 933 | TEST(HttpCache, SimpleGET_LoadPreferringCache_Hit) { |
| 934 | MockHttpCache cache; |
| 935 | |
| 936 | // write to the cache |
| 937 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 938 | |
| 939 | // force this transaction to read from the cache if valid |
| 940 | MockTransaction transaction(kSimpleGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 941 | transaction.load_flags |= LOAD_PREFERRING_CACHE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 942 | |
| 943 | RunTransactionTest(cache.http_cache(), transaction); |
| 944 | |
| 945 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 946 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 947 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 948 | } |
| 949 | |
| 950 | TEST(HttpCache, SimpleGET_LoadPreferringCache_Miss) { |
| 951 | MockHttpCache cache; |
| 952 | |
| 953 | // force this transaction to read from the cache if valid |
| 954 | MockTransaction transaction(kSimpleGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 955 | transaction.load_flags |= LOAD_PREFERRING_CACHE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 956 | |
| 957 | RunTransactionTest(cache.http_cache(), transaction); |
| 958 | |
| 959 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 960 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 961 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 962 | } |
| 963 | |
[email protected] | 528e778 | 2012-11-16 22:36:17 | [diff] [blame] | 964 | // Tests LOAD_PREFERRING_CACHE in the presence of vary headers. |
| 965 | TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMatch) { |
| 966 | MockHttpCache cache; |
| 967 | |
| 968 | // Write to the cache. |
| 969 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 970 | transaction.request_headers = "Foo: bar\r\n"; |
[email protected] | 528e778 | 2012-11-16 22:36:17 | [diff] [blame] | 971 | transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 972 | "Vary: Foo\n"; |
| 973 | AddMockTransaction(&transaction); |
| 974 | RunTransactionTest(cache.http_cache(), transaction); |
| 975 | |
| 976 | // Read from the cache. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 977 | transaction.load_flags |= LOAD_PREFERRING_CACHE; |
[email protected] | 528e778 | 2012-11-16 22:36:17 | [diff] [blame] | 978 | RunTransactionTest(cache.http_cache(), transaction); |
| 979 | |
| 980 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 981 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 982 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 983 | RemoveMockTransaction(&transaction); |
| 984 | } |
| 985 | |
| 986 | // Tests LOAD_PREFERRING_CACHE in the presence of vary headers. |
| 987 | TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMismatch) { |
| 988 | MockHttpCache cache; |
| 989 | |
| 990 | // Write to the cache. |
| 991 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 992 | transaction.request_headers = "Foo: bar\r\n"; |
[email protected] | 528e778 | 2012-11-16 22:36:17 | [diff] [blame] | 993 | transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 994 | "Vary: Foo\n"; |
| 995 | AddMockTransaction(&transaction); |
| 996 | RunTransactionTest(cache.http_cache(), transaction); |
| 997 | |
| 998 | // Attempt to read from the cache... this is a vary mismatch that must reach |
| 999 | // the network again. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1000 | transaction.load_flags |= LOAD_PREFERRING_CACHE; |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 1001 | transaction.request_headers = "Foo: none\r\n"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1002 | BoundTestNetLog log; |
| 1003 | LoadTimingInfo load_timing_info; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1004 | RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 1005 | &load_timing_info); |
[email protected] | 528e778 | 2012-11-16 22:36:17 | [diff] [blame] | 1006 | |
| 1007 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1008 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1009 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1010 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 528e778 | 2012-11-16 22:36:17 | [diff] [blame] | 1011 | RemoveMockTransaction(&transaction); |
| 1012 | } |
| 1013 | |
[email protected] | 419704c | 2014-01-14 11:18:06 | [diff] [blame] | 1014 | // Tests that was_cached was set properly on a failure, even if the cached |
| 1015 | // response wasn't returned. |
| 1016 | TEST(HttpCache, SimpleGET_CacheSignal_Failure) { |
| 1017 | MockHttpCache cache; |
| 1018 | |
| 1019 | // Prime cache. |
| 1020 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1021 | transaction.response_headers = "Cache-Control: no-cache\n"; |
| 1022 | |
| 1023 | AddMockTransaction(&transaction); |
| 1024 | RunTransactionTest(cache.http_cache(), transaction); |
| 1025 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1026 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1027 | RemoveMockTransaction(&transaction); |
| 1028 | |
| 1029 | // Network failure with error; should fail but have was_cached set. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1030 | transaction.return_code = ERR_FAILED; |
[email protected] | 419704c | 2014-01-14 11:18:06 | [diff] [blame] | 1031 | AddMockTransaction(&transaction); |
| 1032 | |
| 1033 | MockHttpRequest request(transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1034 | TestCompletionCallback callback; |
| 1035 | scoped_ptr<HttpTransaction> trans; |
| 1036 | int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); |
| 1037 | EXPECT_EQ(OK, rv); |
[email protected] | 419704c | 2014-01-14 11:18:06 | [diff] [blame] | 1038 | ASSERT_TRUE(trans.get()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1039 | rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 1040 | EXPECT_EQ(ERR_FAILED, callback.GetResult(rv)); |
[email protected] | 419704c | 2014-01-14 11:18:06 | [diff] [blame] | 1041 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1042 | const HttpResponseInfo* response_info = trans->GetResponseInfo(); |
[email protected] | 419704c | 2014-01-14 11:18:06 | [diff] [blame] | 1043 | ASSERT_TRUE(response_info); |
| 1044 | EXPECT_TRUE(response_info->was_cached); |
| 1045 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1046 | |
| 1047 | RemoveMockTransaction(&transaction); |
| 1048 | } |
| 1049 | |
[email protected] | 7cf7ccb | 2013-04-20 02:53:08 | [diff] [blame] | 1050 | // Confirm if we have an empty cache, a read is marked as network verified. |
| 1051 | TEST(HttpCache, SimpleGET_NetworkAccessed_Network) { |
| 1052 | MockHttpCache cache; |
| 1053 | |
| 1054 | // write to the cache |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1055 | HttpResponseInfo response_info; |
[email protected] | 7cf7ccb | 2013-04-20 02:53:08 | [diff] [blame] | 1056 | RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 1057 | &response_info); |
| 1058 | |
| 1059 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1060 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1061 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1062 | EXPECT_TRUE(response_info.network_accessed); |
| 1063 | } |
| 1064 | |
| 1065 | // Confirm if we have a fresh entry in cache, it isn't marked as |
| 1066 | // network verified. |
| 1067 | TEST(HttpCache, SimpleGET_NetworkAccessed_Cache) { |
| 1068 | MockHttpCache cache; |
| 1069 | |
| 1070 | // Prime cache. |
| 1071 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1072 | |
| 1073 | RunTransactionTest(cache.http_cache(), transaction); |
| 1074 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1075 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1076 | |
| 1077 | // Re-run transaction; make sure we don't mark the network as accessed. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1078 | HttpResponseInfo response_info; |
[email protected] | 7cf7ccb | 2013-04-20 02:53:08 | [diff] [blame] | 1079 | RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
| 1080 | &response_info); |
| 1081 | |
| 1082 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1083 | EXPECT_FALSE(response_info.server_data_unavailable); |
| 1084 | EXPECT_FALSE(response_info.network_accessed); |
| 1085 | } |
| 1086 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1087 | TEST(HttpCache, SimpleGET_LoadBypassCache) { |
| 1088 | MockHttpCache cache; |
| 1089 | |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1090 | // Write to the cache. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1091 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1092 | |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1093 | // Force this transaction to write to the cache again. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1094 | MockTransaction transaction(kSimpleGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1095 | transaction.load_flags |= LOAD_BYPASS_CACHE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1096 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1097 | BoundTestNetLog log; |
| 1098 | LoadTimingInfo load_timing_info; |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 1099 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1100 | // Write to the cache. |
| 1101 | RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 1102 | &load_timing_info); |
[email protected] | 9393b717 | 2010-02-11 00:12:15 | [diff] [blame] | 1103 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1104 | // Check that the NetLog was filled as expected. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 1105 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1106 | log.GetEntries(&entries); |
[email protected] | c47c037 | 2014-03-12 23:07:02 | [diff] [blame] | 1107 | FilterLogEntries(&entries); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1108 | |
| 1109 | EXPECT_EQ(8u, entries.size()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1110 | EXPECT_TRUE( |
| 1111 | LogContainsBeginEvent(entries, 0, NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
| 1112 | EXPECT_TRUE( |
| 1113 | LogContainsEndEvent(entries, 1, NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); |
| 1114 | EXPECT_TRUE( |
| 1115 | LogContainsBeginEvent(entries, 2, NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); |
| 1116 | EXPECT_TRUE( |
| 1117 | LogContainsEndEvent(entries, 3, NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); |
| 1118 | EXPECT_TRUE( |
| 1119 | LogContainsBeginEvent(entries, 4, NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
| 1120 | EXPECT_TRUE( |
| 1121 | LogContainsEndEvent(entries, 5, NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); |
| 1122 | EXPECT_TRUE( |
| 1123 | LogContainsBeginEvent(entries, 6, NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); |
| 1124 | EXPECT_TRUE( |
| 1125 | LogContainsEndEvent(entries, 7, NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1126 | |
| 1127 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1128 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1129 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1130 | TestLoadTimingNetworkRequest(load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit) { |
| 1134 | MockHttpCache cache; |
| 1135 | |
| 1136 | // write to the cache |
| 1137 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1138 | |
| 1139 | // force this transaction to write to the cache again |
| 1140 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 1141 | transaction.request_headers = "pragma: no-cache\r\n"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1142 | |
| 1143 | RunTransactionTest(cache.http_cache(), transaction); |
| 1144 | |
| 1145 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1146 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1147 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1148 | } |
| 1149 | |
| 1150 | TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit2) { |
| 1151 | MockHttpCache cache; |
| 1152 | |
| 1153 | // write to the cache |
| 1154 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1155 | |
| 1156 | // force this transaction to write to the cache again |
| 1157 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 1158 | transaction.request_headers = "cache-control: no-cache\r\n"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1159 | |
| 1160 | RunTransactionTest(cache.http_cache(), transaction); |
| 1161 | |
| 1162 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1163 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1164 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1165 | } |
| 1166 | |
| 1167 | TEST(HttpCache, SimpleGET_LoadValidateCache) { |
| 1168 | MockHttpCache cache; |
| 1169 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1170 | // Write to the cache. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1171 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1172 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1173 | // Read from the cache. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1174 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1175 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1176 | // Force this transaction to validate the cache. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1177 | MockTransaction transaction(kSimpleGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1178 | transaction.load_flags |= LOAD_VALIDATE_CACHE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1179 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1180 | HttpResponseInfo response_info; |
| 1181 | BoundTestNetLog log; |
| 1182 | LoadTimingInfo load_timing_info; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1183 | RunTransactionTestWithResponseInfoAndGetTiming( |
| 1184 | cache.http_cache(), transaction, &response_info, log.bound(), |
| 1185 | &load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1186 | |
| 1187 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1188 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1189 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 7cf7ccb | 2013-04-20 02:53:08 | [diff] [blame] | 1190 | EXPECT_TRUE(response_info.network_accessed); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1191 | TestLoadTimingNetworkRequest(load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | TEST(HttpCache, SimpleGET_LoadValidateCache_Implicit) { |
| 1195 | MockHttpCache cache; |
| 1196 | |
| 1197 | // write to the cache |
| 1198 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1199 | |
| 1200 | // read from the cache |
| 1201 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1202 | |
| 1203 | // force this transaction to validate the cache |
| 1204 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 1205 | transaction.request_headers = "cache-control: max-age=0\r\n"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1206 | |
| 1207 | RunTransactionTest(cache.http_cache(), transaction); |
| 1208 | |
| 1209 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1210 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1211 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1212 | } |
| 1213 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1214 | static void PreserveRequestHeaders_Handler(const HttpRequestInfo* request, |
| 1215 | std::string* response_status, |
| 1216 | std::string* response_headers, |
| 1217 | std::string* response_data) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 1218 | EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); |
[email protected] | a3eee21 | 2009-11-05 18:08:58 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | // Tests that we don't remove extra headers for simple requests. |
| 1222 | TEST(HttpCache, SimpleGET_PreserveRequestHeaders) { |
| 1223 | MockHttpCache cache; |
| 1224 | |
| 1225 | MockTransaction transaction(kSimpleGET_Transaction); |
| 1226 | transaction.handler = PreserveRequestHeaders_Handler; |
| 1227 | transaction.request_headers = EXTRA_HEADER; |
| 1228 | transaction.response_headers = "Cache-Control: max-age=0\n"; |
| 1229 | AddMockTransaction(&transaction); |
| 1230 | |
| 1231 | // Write, then revalidate the entry. |
| 1232 | RunTransactionTest(cache.http_cache(), transaction); |
| 1233 | RunTransactionTest(cache.http_cache(), transaction); |
| 1234 | |
| 1235 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1236 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1237 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1238 | RemoveMockTransaction(&transaction); |
| 1239 | } |
| 1240 | |
| 1241 | // Tests that we don't remove extra headers for conditionalized requests. |
| 1242 | TEST(HttpCache, ConditionalizedGET_PreserveRequestHeaders) { |
| 1243 | MockHttpCache cache; |
| 1244 | |
| 1245 | // Write to the cache. |
| 1246 | RunTransactionTest(cache.http_cache(), kETagGET_Transaction); |
| 1247 | |
| 1248 | MockTransaction transaction(kETagGET_Transaction); |
| 1249 | transaction.handler = PreserveRequestHeaders_Handler; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 1250 | transaction.request_headers = "If-None-Match: \"foopy\"\r\n" |
[email protected] | a3eee21 | 2009-11-05 18:08:58 | [diff] [blame] | 1251 | EXTRA_HEADER; |
| 1252 | AddMockTransaction(&transaction); |
| 1253 | |
| 1254 | RunTransactionTest(cache.http_cache(), transaction); |
| 1255 | |
| 1256 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1257 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1258 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1259 | RemoveMockTransaction(&transaction); |
| 1260 | } |
| 1261 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1262 | TEST(HttpCache, SimpleGET_ManyReaders) { |
| 1263 | MockHttpCache cache; |
| 1264 | |
| 1265 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1266 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1267 | std::vector<Context*> context_list; |
| 1268 | const int kNumTransactions = 5; |
| 1269 | |
| 1270 | for (int i = 0; i < kNumTransactions; ++i) { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1271 | context_list.push_back(new Context()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1272 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1273 | |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 1274 | c->result = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1275 | ASSERT_EQ(OK, c->result); |
| 1276 | EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1277 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1278 | c->result = |
| 1279 | c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1280 | } |
| 1281 | |
[email protected] | fbf5047 | 2010-07-15 22:53:53 | [diff] [blame] | 1282 | // All requests are waiting for the active entry. |
| 1283 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1284 | Context* c = context_list[i]; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1285 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); |
[email protected] | fbf5047 | 2010-07-15 22:53:53 | [diff] [blame] | 1286 | } |
| 1287 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1288 | // Allow all requests to move from the Create queue to the active entry. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1289 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1290 | |
| 1291 | // The first request should be a writer at this point, and the subsequent |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1292 | // requests should be pending. |
| 1293 | |
| 1294 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1295 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1296 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1297 | |
[email protected] | fbf5047 | 2010-07-15 22:53:53 | [diff] [blame] | 1298 | // All requests depend on the writer, and the writer is between Start and |
| 1299 | // Read, i.e. idle. |
| 1300 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1301 | Context* c = context_list[i]; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1302 | EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
[email protected] | fbf5047 | 2010-07-15 22:53:53 | [diff] [blame] | 1303 | } |
| 1304 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1305 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1306 | Context* c = context_list[i]; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1307 | if (c->result == ERR_IO_PENDING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1308 | c->result = c->callback.WaitForResult(); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1309 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1310 | } |
| 1311 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1312 | // We should not have had to re-open the disk entry |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1313 | |
| 1314 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1315 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1316 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1317 | |
| 1318 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1319 | Context* c = context_list[i]; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1320 | delete c; |
| 1321 | } |
| 1322 | } |
| 1323 | |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1324 | // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. |
| 1325 | // If cancelling a request is racing with another request for the same resource |
| 1326 | // finishing, we have to make sure that we remove both transactions from the |
| 1327 | // entry. |
| 1328 | TEST(HttpCache, SimpleGET_RacingReaders) { |
| 1329 | MockHttpCache cache; |
| 1330 | |
| 1331 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1332 | MockHttpRequest reader_request(kSimpleGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1333 | reader_request.load_flags = LOAD_ONLY_FROM_CACHE; |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1334 | |
| 1335 | std::vector<Context*> context_list; |
| 1336 | const int kNumTransactions = 5; |
| 1337 | |
| 1338 | for (int i = 0; i < kNumTransactions; ++i) { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1339 | context_list.push_back(new Context()); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1340 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1341 | |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 1342 | c->result = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1343 | ASSERT_EQ(OK, c->result); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1344 | |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1345 | MockHttpRequest* this_request = &request; |
| 1346 | if (i == 1 || i == 2) |
| 1347 | this_request = &reader_request; |
| 1348 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1349 | c->result = |
| 1350 | c->trans->Start(this_request, c->callback.callback(), BoundNetLog()); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1351 | } |
| 1352 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1353 | // Allow all requests to move from the Create queue to the active entry. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1354 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1355 | |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1356 | // The first request should be a writer at this point, and the subsequent |
| 1357 | // requests should be pending. |
| 1358 | |
| 1359 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1360 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1361 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1362 | |
| 1363 | Context* c = context_list[0]; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1364 | ASSERT_EQ(ERR_IO_PENDING, c->result); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1365 | c->result = c->callback.WaitForResult(); |
| 1366 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1367 | |
| 1368 | // Now we have 2 active readers and two queued transactions. |
| 1369 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1370 | EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); |
| 1371 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, |
[email protected] | fbf5047 | 2010-07-15 22:53:53 | [diff] [blame] | 1372 | context_list[3]->trans->GetLoadState()); |
| 1373 | |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1374 | c = context_list[1]; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1375 | ASSERT_EQ(ERR_IO_PENDING, c->result); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1376 | c->result = c->callback.WaitForResult(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1377 | if (c->result == OK) |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 1378 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1379 | |
| 1380 | // At this point we have one reader, two pending transactions and a task on |
| 1381 | // the queue to move to the next transaction. Now we cancel the request that |
| 1382 | // is the current reader, and expect the queued task to be able to start the |
| 1383 | // next request. |
| 1384 | |
| 1385 | c = context_list[2]; |
| 1386 | c->trans.reset(); |
| 1387 | |
| 1388 | for (int i = 3; i < kNumTransactions; ++i) { |
| 1389 | Context* c = context_list[i]; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1390 | if (c->result == ERR_IO_PENDING) |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1391 | c->result = c->callback.WaitForResult(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1392 | if (c->result == OK) |
[email protected] | 37095fe | 2009-08-07 00:13:12 | [diff] [blame] | 1393 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
[email protected] | e189164 | 2009-01-07 18:30:57 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | // We should not have had to re-open the disk entry. |
| 1397 | |
| 1398 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1399 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1400 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1401 | |
| 1402 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1403 | Context* c = context_list[i]; |
| 1404 | delete c; |
| 1405 | } |
| 1406 | } |
| 1407 | |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1408 | // Tests that we can doom an entry with pending transactions and delete one of |
| 1409 | // the pending transactions before the first one completes. |
| 1410 | // See http://code.google.com/p/chromium/issues/detail?id=25588 |
| 1411 | TEST(HttpCache, SimpleGET_DoomWithPending) { |
| 1412 | // We need simultaneous doomed / not_doomed entries so let's use a real cache. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1413 | MockHttpCache cache(HttpCache::DefaultBackend::InMemory(1024 * 1024)); |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1414 | |
| 1415 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1416 | MockHttpRequest writer_request(kSimpleGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1417 | writer_request.load_flags = LOAD_BYPASS_CACHE; |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1418 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 1419 | std::vector<scoped_ptr<Context>> context_list; |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1420 | const int kNumTransactions = 4; |
| 1421 | |
| 1422 | for (int i = 0; i < kNumTransactions; ++i) { |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 1423 | context_list.push_back(make_scoped_ptr(new Context())); |
| 1424 | Context* c = context_list[i].get(); |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1425 | |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 1426 | c->result = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1427 | ASSERT_EQ(OK, c->result); |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1428 | |
| 1429 | MockHttpRequest* this_request = &request; |
| 1430 | if (i == 3) |
| 1431 | this_request = &writer_request; |
| 1432 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1433 | c->result = |
| 1434 | c->trans->Start(this_request, c->callback.callback(), BoundNetLog()); |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | // The first request should be a writer at this point, and the two subsequent |
| 1438 | // requests should be pending. The last request doomed the first entry. |
| 1439 | |
| 1440 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1441 | |
| 1442 | // Cancel the first queued transaction. |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 1443 | context_list[1].reset(); |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1444 | |
| 1445 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1446 | if (i == 1) |
| 1447 | continue; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 1448 | Context* c = context_list[i].get(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1449 | ASSERT_EQ(ERR_IO_PENDING, c->result); |
[email protected] | d5b94c7 | 2009-10-26 16:51:10 | [diff] [blame] | 1450 | c->result = c->callback.WaitForResult(); |
| 1451 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1452 | } |
| 1453 | } |
| 1454 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1455 | // This is a test for http://code.google.com/p/chromium/issues/detail?id=4731. |
| 1456 | // We may attempt to delete an entry synchronously with the act of adding a new |
| 1457 | // transaction to said entry. |
| 1458 | TEST(HttpCache, FastNoStoreGET_DoneWithPending) { |
| 1459 | MockHttpCache cache; |
| 1460 | |
| 1461 | // The headers will be served right from the call to Start() the request. |
| 1462 | MockHttpRequest request(kFastNoStoreGET_Transaction); |
| 1463 | FastTransactionServer request_handler; |
| 1464 | AddMockTransaction(&kFastNoStoreGET_Transaction); |
| 1465 | |
| 1466 | std::vector<Context*> context_list; |
| 1467 | const int kNumTransactions = 3; |
| 1468 | |
| 1469 | for (int i = 0; i < kNumTransactions; ++i) { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1470 | context_list.push_back(new Context()); |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1471 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1472 | |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 1473 | c->result = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1474 | ASSERT_EQ(OK, c->result); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1475 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1476 | c->result = |
| 1477 | c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1478 | } |
| 1479 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1480 | // Allow all requests to move from the Create queue to the active entry. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1481 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1482 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1483 | // The first request should be a writer at this point, and the subsequent |
| 1484 | // requests should be pending. |
| 1485 | |
| 1486 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1487 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1488 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1489 | |
| 1490 | // Now, make sure that the second request asks for the entry not to be stored. |
| 1491 | request_handler.set_no_store(true); |
| 1492 | |
| 1493 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1494 | Context* c = context_list[i]; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1495 | if (c->result == ERR_IO_PENDING) |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 1496 | c->result = c->callback.WaitForResult(); |
| 1497 | ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); |
| 1498 | delete c; |
| 1499 | } |
| 1500 | |
| 1501 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1502 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1503 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1504 | |
| 1505 | RemoveMockTransaction(&kFastNoStoreGET_Transaction); |
| 1506 | } |
| 1507 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1508 | TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { |
| 1509 | MockHttpCache cache; |
| 1510 | |
| 1511 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1512 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1513 | std::vector<Context*> context_list; |
| 1514 | const int kNumTransactions = 2; |
| 1515 | |
| 1516 | for (int i = 0; i < kNumTransactions; ++i) { |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1517 | context_list.push_back(new Context()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1518 | Context* c = context_list[i]; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1519 | |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 1520 | c->result = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1521 | ASSERT_EQ(OK, c->result); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 1522 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1523 | c->result = |
| 1524 | c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1525 | } |
| 1526 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1527 | // Allow all requests to move from the Create queue to the active entry. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1528 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1529 | |
| 1530 | // The first request should be a writer at this point, and the subsequent |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1531 | // requests should be pending. |
| 1532 | |
| 1533 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1534 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1535 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1536 | |
| 1537 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1538 | Context* c = context_list[i]; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1539 | if (c->result == ERR_IO_PENDING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1540 | c->result = c->callback.WaitForResult(); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1541 | // Destroy only the first transaction. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1542 | if (i == 0) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1543 | delete c; |
| 1544 | context_list[i] = NULL; |
| 1545 | } |
| 1546 | } |
| 1547 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1548 | // Complete the rest of the transactions. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1549 | for (int i = 1; i < kNumTransactions; ++i) { |
| 1550 | Context* c = context_list[i]; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1551 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1552 | } |
| 1553 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1554 | // We should have had to re-open the disk entry. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1555 | |
| 1556 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1557 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1558 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1559 | |
| 1560 | for (int i = 1; i < kNumTransactions; ++i) { |
| 1561 | Context* c = context_list[i]; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1562 | delete c; |
| 1563 | } |
| 1564 | } |
| 1565 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1566 | // Tests that we can cancel requests that are queued waiting to open the disk |
| 1567 | // cache entry. |
| 1568 | TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) { |
| 1569 | MockHttpCache cache; |
| 1570 | |
| 1571 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1572 | |
| 1573 | std::vector<Context*> context_list; |
| 1574 | const int kNumTransactions = 5; |
| 1575 | |
| 1576 | for (int i = 0; i < kNumTransactions; i++) { |
| 1577 | context_list.push_back(new Context()); |
| 1578 | Context* c = context_list[i]; |
| 1579 | |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 1580 | c->result = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1581 | ASSERT_EQ(OK, c->result); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1582 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1583 | c->result = |
| 1584 | c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | // The first request should be creating the disk cache entry and the others |
| 1588 | // should be pending. |
| 1589 | |
| 1590 | EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 1591 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1592 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1593 | |
| 1594 | // Cancel a request from the pending queue. |
| 1595 | delete context_list[3]; |
| 1596 | context_list[3] = NULL; |
| 1597 | |
| 1598 | // Cancel the request that is creating the entry. This will force the pending |
| 1599 | // operations to restart. |
| 1600 | delete context_list[0]; |
| 1601 | context_list[0] = NULL; |
| 1602 | |
| 1603 | // Complete the rest of the transactions. |
| 1604 | for (int i = 1; i < kNumTransactions; i++) { |
| 1605 | Context* c = context_list[i]; |
| 1606 | if (c) { |
| 1607 | c->result = c->callback.GetResult(c->result); |
| 1608 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | // We should have had to re-create the disk entry. |
| 1613 | |
| 1614 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1615 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1616 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1617 | |
| 1618 | for (int i = 1; i < kNumTransactions; ++i) { |
| 1619 | delete context_list[i]; |
| 1620 | } |
| 1621 | } |
| 1622 | |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 1623 | // Tests that we can cancel a single request to open a disk cache entry. |
| 1624 | TEST(HttpCache, SimpleGET_CancelCreate) { |
| 1625 | MockHttpCache cache; |
| 1626 | |
| 1627 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1628 | |
| 1629 | Context* c = new Context(); |
| 1630 | |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 1631 | c->result = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1632 | ASSERT_EQ(OK, c->result); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 1633 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1634 | c->result = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 1635 | EXPECT_EQ(ERR_IO_PENDING, c->result); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 1636 | |
| 1637 | // Release the reference that the mock disk cache keeps for this entry, so |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1638 | // that we test that the http cache handles the cancellation correctly. |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 1639 | cache.disk_cache()->ReleaseAll(); |
| 1640 | delete c; |
| 1641 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1642 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 1643 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1644 | } |
| 1645 | |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1646 | // Tests that we delete/create entries even if multiple requests are queued. |
| 1647 | TEST(HttpCache, SimpleGET_ManyWriters_BypassCache) { |
| 1648 | MockHttpCache cache; |
| 1649 | |
| 1650 | MockHttpRequest request(kSimpleGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1651 | request.load_flags = LOAD_BYPASS_CACHE; |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1652 | |
| 1653 | std::vector<Context*> context_list; |
| 1654 | const int kNumTransactions = 5; |
| 1655 | |
| 1656 | for (int i = 0; i < kNumTransactions; i++) { |
| 1657 | context_list.push_back(new Context()); |
| 1658 | Context* c = context_list[i]; |
| 1659 | |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 1660 | c->result = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1661 | ASSERT_EQ(OK, c->result); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1662 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1663 | c->result = |
| 1664 | c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | // The first request should be deleting the disk cache entry and the others |
| 1668 | // should be pending. |
| 1669 | |
| 1670 | EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 1671 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1672 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 1673 | |
| 1674 | // Complete the transactions. |
| 1675 | for (int i = 0; i < kNumTransactions; i++) { |
| 1676 | Context* c = context_list[i]; |
| 1677 | c->result = c->callback.GetResult(c->result); |
| 1678 | ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1679 | } |
| 1680 | |
| 1681 | // We should have had to re-create the disk entry multiple times. |
| 1682 | |
| 1683 | EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
| 1684 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1685 | EXPECT_EQ(5, cache.disk_cache()->create_count()); |
| 1686 | |
| 1687 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1688 | delete context_list[i]; |
| 1689 | } |
| 1690 | } |
| 1691 | |
[email protected] | 8aacaf38 | 2014-06-24 05:33:41 | [diff] [blame] | 1692 | // Tests that a (simulated) timeout allows transactions waiting on the cache |
| 1693 | // lock to continue. |
| 1694 | TEST(HttpCache, SimpleGET_WriterTimeout) { |
| 1695 | MockHttpCache cache; |
| 1696 | cache.BypassCacheLock(); |
| 1697 | |
| 1698 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1699 | Context c1, c2; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1700 | ASSERT_EQ(OK, cache.CreateTransaction(&c1.trans)); |
| 1701 | ASSERT_EQ(ERR_IO_PENDING, |
| 1702 | c1.trans->Start(&request, c1.callback.callback(), BoundNetLog())); |
| 1703 | ASSERT_EQ(OK, cache.CreateTransaction(&c2.trans)); |
| 1704 | ASSERT_EQ(ERR_IO_PENDING, |
| 1705 | c2.trans->Start(&request, c2.callback.callback(), BoundNetLog())); |
[email protected] | 8aacaf38 | 2014-06-24 05:33:41 | [diff] [blame] | 1706 | |
| 1707 | // The second request is queued after the first one. |
| 1708 | |
| 1709 | c2.callback.WaitForResult(); |
| 1710 | ReadAndVerifyTransaction(c2.trans.get(), kSimpleGET_Transaction); |
| 1711 | |
| 1712 | // Complete the first transaction. |
| 1713 | c1.callback.WaitForResult(); |
| 1714 | ReadAndVerifyTransaction(c1.trans.get(), kSimpleGET_Transaction); |
| 1715 | } |
| 1716 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1717 | TEST(HttpCache, SimpleGET_AbandonedCacheRead) { |
| 1718 | MockHttpCache cache; |
| 1719 | |
| 1720 | // write to the cache |
| 1721 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1722 | |
| 1723 | MockHttpRequest request(kSimpleGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1724 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1725 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1726 | scoped_ptr<HttpTransaction> trans; |
| 1727 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
| 1728 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 1729 | if (rv == ERR_IO_PENDING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1730 | rv = callback.WaitForResult(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1731 | ASSERT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1732 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1733 | scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1734 | rv = trans->Read(buf.get(), 256, callback.callback()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1735 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1736 | |
| 1737 | // Test that destroying the transaction while it is reading from the cache |
| 1738 | // works properly. |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1739 | trans.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1740 | |
| 1741 | // Make sure we pump any pending events, which should include a call to |
| 1742 | // HttpCache::Transaction::OnCacheReadCompleted. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1743 | base::MessageLoop::current()->RunUntilIdle(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1744 | } |
| 1745 | |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1746 | // Tests that we can delete the HttpCache and deal with queued transactions |
| 1747 | // ("waiting for the backend" as opposed to Active or Doomed entries). |
| 1748 | TEST(HttpCache, SimpleGET_ManyWriters_DeleteCache) { |
mmenke | bc31a2c | 2015-10-29 13:44:45 | [diff] [blame] | 1749 | scoped_ptr<MockHttpCache> cache( |
| 1750 | new MockHttpCache(make_scoped_ptr(new MockBackendNoCbFactory()))); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1751 | |
| 1752 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1753 | |
| 1754 | std::vector<Context*> context_list; |
| 1755 | const int kNumTransactions = 5; |
| 1756 | |
| 1757 | for (int i = 0; i < kNumTransactions; i++) { |
| 1758 | context_list.push_back(new Context()); |
| 1759 | Context* c = context_list[i]; |
| 1760 | |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 1761 | c->result = cache->CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1762 | ASSERT_EQ(OK, c->result); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1763 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1764 | c->result = |
| 1765 | c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | // The first request should be creating the disk cache entry and the others |
| 1769 | // should be pending. |
| 1770 | |
| 1771 | EXPECT_EQ(0, cache->network_layer()->transaction_count()); |
| 1772 | EXPECT_EQ(0, cache->disk_cache()->open_count()); |
| 1773 | EXPECT_EQ(0, cache->disk_cache()->create_count()); |
| 1774 | |
| 1775 | cache.reset(); |
| 1776 | |
| 1777 | // There is not much to do with the transactions at this point... they are |
| 1778 | // waiting for a callback that will not fire. |
| 1779 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1780 | delete context_list[i]; |
| 1781 | } |
| 1782 | } |
| 1783 | |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1784 | // Tests that we queue requests when initializing the backend. |
| 1785 | TEST(HttpCache, SimpleGET_WaitForBackend) { |
| 1786 | MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
mmenke | bc31a2c | 2015-10-29 13:44:45 | [diff] [blame] | 1787 | MockHttpCache cache(make_scoped_ptr(factory)); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1788 | |
| 1789 | MockHttpRequest request0(kSimpleGET_Transaction); |
| 1790 | MockHttpRequest request1(kTypicalGET_Transaction); |
| 1791 | MockHttpRequest request2(kETagGET_Transaction); |
| 1792 | |
| 1793 | std::vector<Context*> context_list; |
| 1794 | const int kNumTransactions = 3; |
| 1795 | |
| 1796 | for (int i = 0; i < kNumTransactions; i++) { |
| 1797 | context_list.push_back(new Context()); |
| 1798 | Context* c = context_list[i]; |
| 1799 | |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 1800 | c->result = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1801 | ASSERT_EQ(OK, c->result); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | context_list[0]->result = context_list[0]->trans->Start( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1805 | &request0, context_list[0]->callback.callback(), BoundNetLog()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1806 | context_list[1]->result = context_list[1]->trans->Start( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1807 | &request1, context_list[1]->callback.callback(), BoundNetLog()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1808 | context_list[2]->result = context_list[2]->trans->Start( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1809 | &request2, context_list[2]->callback.callback(), BoundNetLog()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1810 | |
| 1811 | // Just to make sure that everything is still pending. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1812 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1813 | |
| 1814 | // The first request should be creating the disk cache. |
| 1815 | EXPECT_FALSE(context_list[0]->callback.have_result()); |
| 1816 | |
| 1817 | factory->FinishCreation(); |
| 1818 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1819 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1820 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1821 | EXPECT_EQ(3, cache.disk_cache()->create_count()); |
| 1822 | |
| 1823 | for (int i = 0; i < kNumTransactions; ++i) { |
| 1824 | EXPECT_TRUE(context_list[i]->callback.have_result()); |
| 1825 | delete context_list[i]; |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | // Tests that we can cancel requests that are queued waiting for the backend |
| 1830 | // to be initialized. |
| 1831 | TEST(HttpCache, SimpleGET_WaitForBackend_CancelCreate) { |
| 1832 | MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
mmenke | bc31a2c | 2015-10-29 13:44:45 | [diff] [blame] | 1833 | MockHttpCache cache(make_scoped_ptr(factory)); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1834 | |
| 1835 | MockHttpRequest request0(kSimpleGET_Transaction); |
| 1836 | MockHttpRequest request1(kTypicalGET_Transaction); |
| 1837 | MockHttpRequest request2(kETagGET_Transaction); |
| 1838 | |
| 1839 | std::vector<Context*> context_list; |
| 1840 | const int kNumTransactions = 3; |
| 1841 | |
| 1842 | for (int i = 0; i < kNumTransactions; i++) { |
| 1843 | context_list.push_back(new Context()); |
| 1844 | Context* c = context_list[i]; |
| 1845 | |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 1846 | c->result = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1847 | ASSERT_EQ(OK, c->result); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | context_list[0]->result = context_list[0]->trans->Start( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1851 | &request0, context_list[0]->callback.callback(), BoundNetLog()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1852 | context_list[1]->result = context_list[1]->trans->Start( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1853 | &request1, context_list[1]->callback.callback(), BoundNetLog()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1854 | context_list[2]->result = context_list[2]->trans->Start( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1855 | &request2, context_list[2]->callback.callback(), BoundNetLog()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1856 | |
| 1857 | // Just to make sure that everything is still pending. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1858 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 1859 | |
| 1860 | // The first request should be creating the disk cache. |
| 1861 | EXPECT_FALSE(context_list[0]->callback.have_result()); |
| 1862 | |
| 1863 | // Cancel a request from the pending queue. |
| 1864 | delete context_list[1]; |
| 1865 | context_list[1] = NULL; |
| 1866 | |
| 1867 | // Cancel the request that is creating the entry. |
| 1868 | delete context_list[0]; |
| 1869 | context_list[0] = NULL; |
| 1870 | |
| 1871 | // Complete the last transaction. |
| 1872 | factory->FinishCreation(); |
| 1873 | |
| 1874 | context_list[2]->result = |
| 1875 | context_list[2]->callback.GetResult(context_list[2]->result); |
| 1876 | ReadAndVerifyTransaction(context_list[2]->trans.get(), kETagGET_Transaction); |
| 1877 | |
| 1878 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1879 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1880 | |
| 1881 | delete context_list[2]; |
| 1882 | } |
| 1883 | |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1884 | // Tests that we can delete the cache while creating the backend. |
| 1885 | TEST(HttpCache, DeleteCacheWaitingForBackend) { |
| 1886 | MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
mmenke | bc31a2c | 2015-10-29 13:44:45 | [diff] [blame] | 1887 | scoped_ptr<MockHttpCache> cache(new MockHttpCache(make_scoped_ptr(factory))); |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1888 | |
| 1889 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1890 | |
| 1891 | scoped_ptr<Context> c(new Context()); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 1892 | c->result = cache->CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1893 | ASSERT_EQ(OK, c->result); |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1894 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1895 | c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1896 | |
| 1897 | // Just to make sure that everything is still pending. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1898 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1899 | |
| 1900 | // The request should be creating the disk cache. |
| 1901 | EXPECT_FALSE(c->callback.have_result()); |
| 1902 | |
| 1903 | // We cannot call FinishCreation because the factory itself will go away with |
| 1904 | // the cache, so grab the callback and attempt to use it. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1905 | CompletionCallback callback = factory->callback(); |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 1906 | scoped_ptr<disk_cache::Backend>* backend = factory->backend(); |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1907 | |
| 1908 | cache.reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1909 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1910 | |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 1911 | backend->reset(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1912 | callback.Run(ERR_ABORTED); |
[email protected] | e86e79d3 | 2010-07-17 00:29:25 | [diff] [blame] | 1913 | } |
| 1914 | |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1915 | // Tests that we can delete the cache while creating the backend, from within |
| 1916 | // one of the callbacks. |
| 1917 | TEST(HttpCache, DeleteCacheWaitingForBackend2) { |
| 1918 | MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
mmenke | bc31a2c | 2015-10-29 13:44:45 | [diff] [blame] | 1919 | MockHttpCache* cache = new MockHttpCache(make_scoped_ptr(factory)); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1920 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 1921 | DeleteCacheCompletionCallback cb(cache); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1922 | disk_cache::Backend* backend; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 1923 | int rv = cache->http_cache()->GetBackend(&backend, cb.callback()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1924 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1925 | |
| 1926 | // Now let's queue a regular transaction |
| 1927 | MockHttpRequest request(kSimpleGET_Transaction); |
| 1928 | |
| 1929 | scoped_ptr<Context> c(new Context()); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 1930 | c->result = cache->CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1931 | ASSERT_EQ(OK, c->result); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1932 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1933 | c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1934 | |
| 1935 | // And another direct backend request. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1936 | TestCompletionCallback cb2; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 1937 | rv = cache->http_cache()->GetBackend(&backend, cb2.callback()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1938 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1939 | |
| 1940 | // Just to make sure that everything is still pending. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1941 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1942 | |
| 1943 | // The request should be queued. |
| 1944 | EXPECT_FALSE(c->callback.have_result()); |
| 1945 | |
| 1946 | // Generate the callback. |
| 1947 | factory->FinishCreation(); |
| 1948 | rv = cb.WaitForResult(); |
| 1949 | |
| 1950 | // The cache should be gone by now. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1951 | base::MessageLoop::current()->RunUntilIdle(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1952 | EXPECT_EQ(OK, c->callback.GetResult(c->result)); |
[email protected] | ccf175c | 2010-08-21 01:41:59 | [diff] [blame] | 1953 | EXPECT_FALSE(cb2.have_result()); |
| 1954 | } |
| 1955 | |
tfarina | e04a95b | 2015-09-18 22:48:12 | [diff] [blame] | 1956 | // Fails only on bots. crbug.com/533640 |
| 1957 | #if defined(OS_ANDROID) |
| 1958 | #define MAYBE_TypicalGET_ConditionalRequest \ |
| 1959 | DISABLED_TypicalGET_ConditionalRequest |
| 1960 | #else |
| 1961 | #define MAYBE_TypicalGET_ConditionalRequest TypicalGET_ConditionalRequest |
| 1962 | #endif |
| 1963 | TEST(HttpCache, MAYBE_TypicalGET_ConditionalRequest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1964 | MockHttpCache cache; |
| 1965 | |
| 1966 | // write to the cache |
| 1967 | RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction); |
| 1968 | |
| 1969 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1970 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1971 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1972 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1973 | // Get the same URL again, but this time we expect it to result |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1974 | // in a conditional request. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1975 | BoundTestNetLog log; |
| 1976 | LoadTimingInfo load_timing_info; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1977 | RunTransactionTestAndGetTiming(cache.http_cache(), kTypicalGET_Transaction, |
| 1978 | log.bound(), &load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1979 | |
| 1980 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1981 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1982 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 1983 | TestLoadTimingNetworkRequest(load_timing_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1984 | } |
| 1985 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1986 | static void ETagGet_ConditionalRequest_Handler(const HttpRequestInfo* request, |
| 1987 | std::string* response_status, |
| 1988 | std::string* response_headers, |
| 1989 | std::string* response_data) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 1990 | EXPECT_TRUE( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1991 | request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1992 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 1993 | response_headers->assign(kETagGET_Transaction.response_headers); |
| 1994 | response_data->clear(); |
| 1995 | } |
| 1996 | |
| 1997 | TEST(HttpCache, ETagGET_ConditionalRequest_304) { |
| 1998 | MockHttpCache cache; |
| 1999 | |
| 2000 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 2001 | |
| 2002 | // write to the cache |
| 2003 | RunTransactionTest(cache.http_cache(), transaction); |
| 2004 | |
| 2005 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2006 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2007 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2008 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2009 | // Get the same URL again, but this time we expect it to result |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2010 | // in a conditional request. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2011 | transaction.load_flags = LOAD_VALIDATE_CACHE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2012 | transaction.handler = ETagGet_ConditionalRequest_Handler; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2013 | BoundTestNetLog log; |
| 2014 | LoadTimingInfo load_timing_info; |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2015 | IPEndPoint remote_endpoint; |
| 2016 | RunTransactionTestAndGetTimingAndConnectedSocketAddress( |
| 2017 | cache.http_cache(), transaction, log.bound(), &load_timing_info, |
| 2018 | &remote_endpoint); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2019 | |
| 2020 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2021 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2022 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2023 | TestLoadTimingNetworkRequest(load_timing_info); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2024 | |
| 2025 | EXPECT_FALSE(remote_endpoint.address().empty()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2026 | } |
| 2027 | |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2028 | class RevalidationServer { |
| 2029 | public: |
| 2030 | RevalidationServer() { |
| 2031 | s_etag_used_ = false; |
| 2032 | s_last_modified_used_ = false; |
| 2033 | } |
| 2034 | |
| 2035 | bool EtagUsed() { return s_etag_used_; } |
| 2036 | bool LastModifiedUsed() { return s_last_modified_used_; } |
| 2037 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2038 | static void Handler(const HttpRequestInfo* request, |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2039 | std::string* response_status, |
| 2040 | std::string* response_headers, |
| 2041 | std::string* response_data); |
| 2042 | |
| 2043 | private: |
| 2044 | static bool s_etag_used_; |
| 2045 | static bool s_last_modified_used_; |
| 2046 | }; |
| 2047 | bool RevalidationServer::s_etag_used_ = false; |
| 2048 | bool RevalidationServer::s_last_modified_used_ = false; |
| 2049 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2050 | void RevalidationServer::Handler(const HttpRequestInfo* request, |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2051 | std::string* response_status, |
| 2052 | std::string* response_headers, |
| 2053 | std::string* response_data) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2054 | if (request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch)) |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2055 | s_etag_used_ = true; |
| 2056 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2057 | if (request->extra_headers.HasHeader(HttpRequestHeaders::kIfModifiedSince)) { |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2058 | s_last_modified_used_ = true; |
| 2059 | } |
| 2060 | |
| 2061 | if (s_etag_used_ || s_last_modified_used_) { |
| 2062 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 2063 | response_headers->assign(kTypicalGET_Transaction.response_headers); |
| 2064 | response_data->clear(); |
| 2065 | } else { |
| 2066 | response_status->assign(kTypicalGET_Transaction.status); |
| 2067 | response_headers->assign(kTypicalGET_Transaction.response_headers); |
| 2068 | response_data->assign(kTypicalGET_Transaction.data); |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | // Tests revalidation after a vary match. |
rvargas | 28904d86 | 2015-03-09 19:21:09 | [diff] [blame] | 2073 | TEST(HttpCache, GET_ValidateCache_VaryMatch) { |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2074 | MockHttpCache cache; |
| 2075 | |
| 2076 | // Write to the cache. |
| 2077 | MockTransaction transaction(kTypicalGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2078 | transaction.request_headers = "Foo: bar\r\n"; |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2079 | transaction.response_headers = |
| 2080 | "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
| 2081 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 2082 | "Etag: \"foopy\"\n" |
| 2083 | "Cache-Control: max-age=0\n" |
| 2084 | "Vary: Foo\n"; |
| 2085 | AddMockTransaction(&transaction); |
| 2086 | RunTransactionTest(cache.http_cache(), transaction); |
| 2087 | |
| 2088 | // Read from the cache. |
| 2089 | RevalidationServer server; |
| 2090 | transaction.handler = server.Handler; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2091 | BoundTestNetLog log; |
| 2092 | LoadTimingInfo load_timing_info; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2093 | RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 2094 | &load_timing_info); |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2095 | |
| 2096 | EXPECT_TRUE(server.EtagUsed()); |
| 2097 | EXPECT_TRUE(server.LastModifiedUsed()); |
| 2098 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2099 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2100 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2101 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2102 | RemoveMockTransaction(&transaction); |
| 2103 | } |
| 2104 | |
| 2105 | // Tests revalidation after a vary mismatch if etag is present. |
rvargas | 28904d86 | 2015-03-09 19:21:09 | [diff] [blame] | 2106 | TEST(HttpCache, GET_ValidateCache_VaryMismatch) { |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2107 | MockHttpCache cache; |
| 2108 | |
| 2109 | // Write to the cache. |
| 2110 | MockTransaction transaction(kTypicalGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2111 | transaction.request_headers = "Foo: bar\r\n"; |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2112 | transaction.response_headers = |
| 2113 | "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
| 2114 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 2115 | "Etag: \"foopy\"\n" |
| 2116 | "Cache-Control: max-age=0\n" |
| 2117 | "Vary: Foo\n"; |
| 2118 | AddMockTransaction(&transaction); |
| 2119 | RunTransactionTest(cache.http_cache(), transaction); |
| 2120 | |
| 2121 | // Read from the cache and revalidate the entry. |
| 2122 | RevalidationServer server; |
| 2123 | transaction.handler = server.Handler; |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2124 | transaction.request_headers = "Foo: none\r\n"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2125 | BoundTestNetLog log; |
| 2126 | LoadTimingInfo load_timing_info; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2127 | RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 2128 | &load_timing_info); |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2129 | |
| 2130 | EXPECT_TRUE(server.EtagUsed()); |
| 2131 | EXPECT_FALSE(server.LastModifiedUsed()); |
| 2132 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2133 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2134 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2135 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2136 | RemoveMockTransaction(&transaction); |
| 2137 | } |
| 2138 | |
| 2139 | // Tests lack of revalidation after a vary mismatch and no etag. |
rvargas | 28904d86 | 2015-03-09 19:21:09 | [diff] [blame] | 2140 | TEST(HttpCache, GET_DontValidateCache_VaryMismatch) { |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2141 | MockHttpCache cache; |
| 2142 | |
| 2143 | // Write to the cache. |
| 2144 | MockTransaction transaction(kTypicalGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2145 | transaction.request_headers = "Foo: bar\r\n"; |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2146 | transaction.response_headers = |
| 2147 | "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
| 2148 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 2149 | "Cache-Control: max-age=0\n" |
| 2150 | "Vary: Foo\n"; |
| 2151 | AddMockTransaction(&transaction); |
| 2152 | RunTransactionTest(cache.http_cache(), transaction); |
| 2153 | |
| 2154 | // Read from the cache and don't revalidate the entry. |
| 2155 | RevalidationServer server; |
| 2156 | transaction.handler = server.Handler; |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2157 | transaction.request_headers = "Foo: none\r\n"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2158 | BoundTestNetLog log; |
| 2159 | LoadTimingInfo load_timing_info; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2160 | RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 2161 | &load_timing_info); |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2162 | |
| 2163 | EXPECT_FALSE(server.EtagUsed()); |
| 2164 | EXPECT_FALSE(server.LastModifiedUsed()); |
| 2165 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2166 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2167 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 2168 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 9bb9f99 | 2013-01-11 01:43:16 | [diff] [blame] | 2169 | RemoveMockTransaction(&transaction); |
| 2170 | } |
| 2171 | |
rvargas | 28904d86 | 2015-03-09 19:21:09 | [diff] [blame] | 2172 | // Tests that a new vary header provided when revalidating an entry is saved. |
| 2173 | TEST(HttpCache, GET_ValidateCache_VaryMatch_UpdateVary) { |
| 2174 | MockHttpCache cache; |
| 2175 | |
| 2176 | // Write to the cache. |
| 2177 | ScopedMockTransaction transaction(kTypicalGET_Transaction); |
| 2178 | transaction.request_headers = "Foo: bar\r\n Name: bar\r\n"; |
| 2179 | transaction.response_headers = |
| 2180 | "Etag: \"foopy\"\n" |
| 2181 | "Cache-Control: max-age=0\n" |
| 2182 | "Vary: Foo\n"; |
| 2183 | RunTransactionTest(cache.http_cache(), transaction); |
| 2184 | |
| 2185 | // Validate the entry and change the vary field in the response. |
| 2186 | transaction.request_headers = "Foo: bar\r\n Name: none\r\n"; |
| 2187 | transaction.status = "HTTP/1.1 304 Not Modified"; |
| 2188 | transaction.response_headers = |
| 2189 | "Etag: \"foopy\"\n" |
| 2190 | "Cache-Control: max-age=3600\n" |
| 2191 | "Vary: Name\n"; |
| 2192 | RunTransactionTest(cache.http_cache(), transaction); |
| 2193 | |
| 2194 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2195 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2196 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2197 | |
| 2198 | // Make sure that the ActiveEntry is gone. |
| 2199 | base::RunLoop().RunUntilIdle(); |
| 2200 | |
| 2201 | // Generate a vary mismatch. |
| 2202 | transaction.request_headers = "Foo: bar\r\n Name: bar\r\n"; |
| 2203 | RunTransactionTest(cache.http_cache(), transaction); |
| 2204 | |
| 2205 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2206 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 2207 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2208 | } |
| 2209 | |
| 2210 | // Tests that new request headers causing a vary mismatch are paired with the |
| 2211 | // new response when the server says the old response can be used. |
| 2212 | TEST(HttpCache, GET_ValidateCache_VaryMismatch_UpdateRequestHeader) { |
| 2213 | MockHttpCache cache; |
| 2214 | |
| 2215 | // Write to the cache. |
| 2216 | ScopedMockTransaction transaction(kTypicalGET_Transaction); |
| 2217 | transaction.request_headers = "Foo: bar\r\n"; |
| 2218 | transaction.response_headers = |
| 2219 | "Etag: \"foopy\"\n" |
| 2220 | "Cache-Control: max-age=3600\n" |
| 2221 | "Vary: Foo\n"; |
| 2222 | RunTransactionTest(cache.http_cache(), transaction); |
| 2223 | |
| 2224 | // Vary-mismatch validation receives 304. |
| 2225 | transaction.request_headers = "Foo: none\r\n"; |
| 2226 | transaction.status = "HTTP/1.1 304 Not Modified"; |
| 2227 | RunTransactionTest(cache.http_cache(), transaction); |
| 2228 | |
| 2229 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2230 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2231 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2232 | |
| 2233 | // Make sure that the ActiveEntry is gone. |
| 2234 | base::RunLoop().RunUntilIdle(); |
| 2235 | |
| 2236 | // Generate a vary mismatch. |
| 2237 | transaction.request_headers = "Foo: bar\r\n"; |
| 2238 | RunTransactionTest(cache.http_cache(), transaction); |
| 2239 | |
| 2240 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2241 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 2242 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2243 | } |
| 2244 | |
| 2245 | // Tests that a 304 without vary headers doesn't delete the previously stored |
| 2246 | // vary data after a vary match revalidation. |
| 2247 | TEST(HttpCache, GET_ValidateCache_VaryMatch_DontDeleteVary) { |
| 2248 | MockHttpCache cache; |
| 2249 | |
| 2250 | // Write to the cache. |
| 2251 | ScopedMockTransaction transaction(kTypicalGET_Transaction); |
| 2252 | transaction.request_headers = "Foo: bar\r\n"; |
| 2253 | transaction.response_headers = |
| 2254 | "Etag: \"foopy\"\n" |
| 2255 | "Cache-Control: max-age=0\n" |
| 2256 | "Vary: Foo\n"; |
| 2257 | RunTransactionTest(cache.http_cache(), transaction); |
| 2258 | |
| 2259 | // Validate the entry and remove the vary field in the response. |
| 2260 | transaction.status = "HTTP/1.1 304 Not Modified"; |
| 2261 | transaction.response_headers = |
| 2262 | "Etag: \"foopy\"\n" |
| 2263 | "Cache-Control: max-age=3600\n"; |
| 2264 | RunTransactionTest(cache.http_cache(), transaction); |
| 2265 | |
| 2266 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2267 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2268 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2269 | |
| 2270 | // Make sure that the ActiveEntry is gone. |
| 2271 | base::RunLoop().RunUntilIdle(); |
| 2272 | |
| 2273 | // Generate a vary mismatch. |
| 2274 | transaction.request_headers = "Foo: none\r\n"; |
| 2275 | RunTransactionTest(cache.http_cache(), transaction); |
| 2276 | |
| 2277 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2278 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 2279 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2280 | } |
| 2281 | |
| 2282 | // Tests that a 304 without vary headers doesn't delete the previously stored |
| 2283 | // vary data after a vary mismatch. |
| 2284 | TEST(HttpCache, GET_ValidateCache_VaryMismatch_DontDeleteVary) { |
| 2285 | MockHttpCache cache; |
| 2286 | |
| 2287 | // Write to the cache. |
| 2288 | ScopedMockTransaction transaction(kTypicalGET_Transaction); |
| 2289 | transaction.request_headers = "Foo: bar\r\n"; |
| 2290 | transaction.response_headers = |
| 2291 | "Etag: \"foopy\"\n" |
| 2292 | "Cache-Control: max-age=3600\n" |
| 2293 | "Vary: Foo\n"; |
| 2294 | RunTransactionTest(cache.http_cache(), transaction); |
| 2295 | |
| 2296 | // Vary-mismatch validation receives 304 and no vary header. |
| 2297 | transaction.request_headers = "Foo: none\r\n"; |
| 2298 | transaction.status = "HTTP/1.1 304 Not Modified"; |
| 2299 | transaction.response_headers = |
| 2300 | "Etag: \"foopy\"\n" |
| 2301 | "Cache-Control: max-age=3600\n"; |
| 2302 | RunTransactionTest(cache.http_cache(), transaction); |
| 2303 | |
| 2304 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2305 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2306 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2307 | |
| 2308 | // Make sure that the ActiveEntry is gone. |
| 2309 | base::RunLoop().RunUntilIdle(); |
| 2310 | |
| 2311 | // Generate a vary mismatch. |
| 2312 | transaction.request_headers = "Foo: bar\r\n"; |
| 2313 | RunTransactionTest(cache.http_cache(), transaction); |
| 2314 | |
| 2315 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2316 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 2317 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2318 | } |
| 2319 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2320 | static void ETagGet_UnconditionalRequest_Handler(const HttpRequestInfo* request, |
| 2321 | std::string* response_status, |
| 2322 | std::string* response_headers, |
| 2323 | std::string* response_data) { |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 2324 | EXPECT_FALSE( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2325 | request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch)); |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | TEST(HttpCache, ETagGET_Http10) { |
| 2329 | MockHttpCache cache; |
| 2330 | |
| 2331 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 2332 | transaction.status = "HTTP/1.0 200 OK"; |
| 2333 | |
| 2334 | // Write to the cache. |
| 2335 | RunTransactionTest(cache.http_cache(), transaction); |
| 2336 | |
| 2337 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2338 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2339 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2340 | |
| 2341 | // Get the same URL again, without generating a conditional request. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2342 | transaction.load_flags = LOAD_VALIDATE_CACHE; |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 2343 | transaction.handler = ETagGet_UnconditionalRequest_Handler; |
| 2344 | RunTransactionTest(cache.http_cache(), transaction); |
| 2345 | |
| 2346 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2347 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2348 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2349 | } |
| 2350 | |
| 2351 | TEST(HttpCache, ETagGET_Http10_Range) { |
| 2352 | MockHttpCache cache; |
| 2353 | |
| 2354 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 2355 | transaction.status = "HTTP/1.0 200 OK"; |
| 2356 | |
| 2357 | // Write to the cache. |
| 2358 | RunTransactionTest(cache.http_cache(), transaction); |
| 2359 | |
| 2360 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2361 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2362 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2363 | |
| 2364 | // Get the same URL again, but use a byte range request. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2365 | transaction.load_flags = LOAD_VALIDATE_CACHE; |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 2366 | transaction.handler = ETagGet_UnconditionalRequest_Handler; |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2367 | transaction.request_headers = "Range: bytes = 5-\r\n"; |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 2368 | RunTransactionTest(cache.http_cache(), transaction); |
| 2369 | |
[email protected] | 4a62071 | 2011-07-22 17:41:09 | [diff] [blame] | 2370 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 2371 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2372 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2373 | } |
| 2374 | |
[email protected] | b7d05ab | 2008-12-09 19:18:41 | [diff] [blame] | 2375 | static void ETagGet_ConditionalRequest_NoStore_Handler( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2376 | const HttpRequestInfo* request, |
[email protected] | b7d05ab | 2008-12-09 19:18:41 | [diff] [blame] | 2377 | std::string* response_status, |
| 2378 | std::string* response_headers, |
| 2379 | std::string* response_data) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 2380 | EXPECT_TRUE( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2381 | request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch)); |
[email protected] | b7d05ab | 2008-12-09 19:18:41 | [diff] [blame] | 2382 | response_status->assign("HTTP/1.1 304 Not Modified"); |
| 2383 | response_headers->assign("Cache-Control: no-store\n"); |
| 2384 | response_data->clear(); |
| 2385 | } |
| 2386 | |
| 2387 | TEST(HttpCache, ETagGET_ConditionalRequest_304_NoStore) { |
| 2388 | MockHttpCache cache; |
| 2389 | |
| 2390 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 2391 | |
| 2392 | // Write to the cache. |
| 2393 | RunTransactionTest(cache.http_cache(), transaction); |
| 2394 | |
| 2395 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2396 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2397 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2398 | |
| 2399 | // Get the same URL again, but this time we expect it to result |
| 2400 | // in a conditional request. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2401 | transaction.load_flags = LOAD_VALIDATE_CACHE; |
[email protected] | b7d05ab | 2008-12-09 19:18:41 | [diff] [blame] | 2402 | transaction.handler = ETagGet_ConditionalRequest_NoStore_Handler; |
| 2403 | RunTransactionTest(cache.http_cache(), transaction); |
| 2404 | |
| 2405 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2406 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2407 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2408 | |
| 2409 | ScopedMockTransaction transaction2(kETagGET_Transaction); |
| 2410 | |
| 2411 | // Write to the cache again. This should create a new entry. |
| 2412 | RunTransactionTest(cache.http_cache(), transaction2); |
| 2413 | |
| 2414 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2415 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2416 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2417 | } |
| 2418 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2419 | // Helper that does 4 requests using HttpCache: |
| 2420 | // |
| 2421 | // (1) loads |kUrl| -- expects |net_response_1| to be returned. |
| 2422 | // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned. |
| 2423 | // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to |
| 2424 | // be returned. |
| 2425 | // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be |
| 2426 | // returned. |
| 2427 | static void ConditionalizedRequestUpdatesCacheHelper( |
| 2428 | const Response& net_response_1, |
| 2429 | const Response& net_response_2, |
| 2430 | const Response& cached_response_2, |
| 2431 | const char* extra_request_headers) { |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2432 | MockHttpCache cache; |
| 2433 | |
| 2434 | // The URL we will be requesting. |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 2435 | const char kUrl[] = "http://foobar.com/main.css"; |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2436 | |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2437 | // Junk network response. |
| 2438 | static const Response kUnexpectedResponse = { |
| 2439 | "HTTP/1.1 500 Unexpected", |
| 2440 | "Server: unexpected_header", |
| 2441 | "unexpected body" |
| 2442 | }; |
| 2443 | |
| 2444 | // We will control the network layer's responses for |kUrl| using |
| 2445 | // |mock_network_response|. |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 2446 | MockTransaction mock_network_response = { 0 }; |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2447 | mock_network_response.url = kUrl; |
| 2448 | AddMockTransaction(&mock_network_response); |
| 2449 | |
| 2450 | // Request |kUrl| for the first time. It should hit the network and |
| 2451 | // receive |kNetResponse1|, which it saves into the HTTP cache. |
| 2452 | |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 2453 | MockTransaction request = { 0 }; |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2454 | request.url = kUrl; |
| 2455 | request.method = "GET"; |
| 2456 | request.request_headers = ""; |
| 2457 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2458 | net_response_1.AssignTo(&mock_network_response); // Network mock. |
| 2459 | net_response_1.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2460 | |
| 2461 | std::string response_headers; |
| 2462 | RunTransactionTestWithResponse( |
| 2463 | cache.http_cache(), request, &response_headers); |
| 2464 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2465 | EXPECT_EQ(net_response_1.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2466 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2467 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2468 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2469 | |
[email protected] | 6f40bf7 | 2009-07-23 17:52:37 | [diff] [blame] | 2470 | // Request |kUrl| a second time. Now |kNetResponse1| it is in the HTTP |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2471 | // cache, so we don't hit the network. |
| 2472 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2473 | request.load_flags = LOAD_ONLY_FROM_CACHE; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2474 | |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2475 | kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2476 | net_response_1.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2477 | |
| 2478 | RunTransactionTestWithResponse( |
| 2479 | cache.http_cache(), request, &response_headers); |
| 2480 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2481 | EXPECT_EQ(net_response_1.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2482 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2483 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2484 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2485 | |
| 2486 | // Request |kUrl| yet again, but this time give the request an |
| 2487 | // "If-Modified-Since" header. This will cause the request to re-hit the |
| 2488 | // network. However now the network response is going to be |
| 2489 | // different -- this simulates a change made to the CSS file. |
| 2490 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2491 | request.request_headers = extra_request_headers; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2492 | request.load_flags = LOAD_NORMAL; |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2493 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2494 | net_response_2.AssignTo(&mock_network_response); // Network mock. |
| 2495 | net_response_2.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2496 | |
| 2497 | RunTransactionTestWithResponse( |
| 2498 | cache.http_cache(), request, &response_headers); |
| 2499 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2500 | EXPECT_EQ(net_response_2.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2501 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2502 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2503 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2504 | |
| 2505 | // Finally, request |kUrl| again. This request should be serviced from |
| 2506 | // the cache. Moreover, the value in the cache should be |kNetResponse2| |
| 2507 | // and NOT |kNetResponse1|. The previous step should have replaced the |
| 2508 | // value in the cache with the modified response. |
| 2509 | |
| 2510 | request.request_headers = ""; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2511 | request.load_flags = LOAD_ONLY_FROM_CACHE; |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2512 | |
| 2513 | kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2514 | cached_response_2.AssignTo(&request); // Expected result. |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2515 | |
| 2516 | RunTransactionTestWithResponse( |
| 2517 | cache.http_cache(), request, &response_headers); |
| 2518 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2519 | EXPECT_EQ(cached_response_2.status_and_headers(), response_headers); |
[email protected] | bded84c | 2009-07-23 00:36:06 | [diff] [blame] | 2520 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2521 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 2522 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2523 | |
| 2524 | RemoveMockTransaction(&mock_network_response); |
| 2525 | } |
| 2526 | |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2527 | // Check that when an "if-modified-since" header is attached |
| 2528 | // to the request, the result still updates the cached entry. |
| 2529 | TEST(HttpCache, ConditionalizedRequestUpdatesCache1) { |
| 2530 | // First network response for |kUrl|. |
| 2531 | static const Response kNetResponse1 = { |
| 2532 | "HTTP/1.1 200 OK", |
| 2533 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2534 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2535 | "body1" |
| 2536 | }; |
| 2537 | |
| 2538 | // Second network response for |kUrl|. |
| 2539 | static const Response kNetResponse2 = { |
| 2540 | "HTTP/1.1 200 OK", |
| 2541 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2542 | "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2543 | "body2" |
| 2544 | }; |
| 2545 | |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 2546 | const char extra_headers[] = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2547 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2548 | |
| 2549 | ConditionalizedRequestUpdatesCacheHelper( |
| 2550 | kNetResponse1, kNetResponse2, kNetResponse2, extra_headers); |
| 2551 | } |
| 2552 | |
| 2553 | // Check that when an "if-none-match" header is attached |
| 2554 | // to the request, the result updates the cached entry. |
| 2555 | TEST(HttpCache, ConditionalizedRequestUpdatesCache2) { |
| 2556 | // First network response for |kUrl|. |
| 2557 | static const Response kNetResponse1 = { |
| 2558 | "HTTP/1.1 200 OK", |
| 2559 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2560 | "Etag: \"ETAG1\"\n" |
| 2561 | "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire. |
| 2562 | "body1" |
| 2563 | }; |
| 2564 | |
| 2565 | // Second network response for |kUrl|. |
| 2566 | static const Response kNetResponse2 = { |
| 2567 | "HTTP/1.1 200 OK", |
| 2568 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2569 | "Etag: \"ETAG2\"\n" |
| 2570 | "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire. |
| 2571 | "body2" |
| 2572 | }; |
| 2573 | |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 2574 | const char extra_headers[] = "If-None-Match: \"ETAG1\"\r\n"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2575 | |
| 2576 | ConditionalizedRequestUpdatesCacheHelper( |
| 2577 | kNetResponse1, kNetResponse2, kNetResponse2, extra_headers); |
| 2578 | } |
| 2579 | |
| 2580 | // Check that when an "if-modified-since" header is attached |
| 2581 | // to a request, the 304 (not modified result) result updates the cached |
| 2582 | // headers, and the 304 response is returned rather than the cached response. |
| 2583 | TEST(HttpCache, ConditionalizedRequestUpdatesCache3) { |
| 2584 | // First network response for |kUrl|. |
| 2585 | static const Response kNetResponse1 = { |
| 2586 | "HTTP/1.1 200 OK", |
| 2587 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2588 | "Server: server1\n" |
| 2589 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2590 | "body1" |
| 2591 | }; |
| 2592 | |
| 2593 | // Second network response for |kUrl|. |
| 2594 | static const Response kNetResponse2 = { |
| 2595 | "HTTP/1.1 304 Not Modified", |
| 2596 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2597 | "Server: server2\n" |
| 2598 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2599 | "" |
| 2600 | }; |
| 2601 | |
| 2602 | static const Response kCachedResponse2 = { |
| 2603 | "HTTP/1.1 200 OK", |
| 2604 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2605 | "Server: server2\n" |
| 2606 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2607 | "body1" |
| 2608 | }; |
| 2609 | |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 2610 | const char extra_headers[] = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2611 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2612 | |
| 2613 | ConditionalizedRequestUpdatesCacheHelper( |
| 2614 | kNetResponse1, kNetResponse2, kCachedResponse2, extra_headers); |
| 2615 | } |
| 2616 | |
| 2617 | // Test that when doing an externally conditionalized if-modified-since |
| 2618 | // and there is no corresponding cache entry, a new cache entry is NOT |
| 2619 | // created (304 response). |
| 2620 | TEST(HttpCache, ConditionalizedRequestUpdatesCache4) { |
| 2621 | MockHttpCache cache; |
| 2622 | |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 2623 | const char kUrl[] = "http://foobar.com/main.css"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2624 | |
| 2625 | static const Response kNetResponse = { |
| 2626 | "HTTP/1.1 304 Not Modified", |
| 2627 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2628 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2629 | "" |
| 2630 | }; |
| 2631 | |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 2632 | const char kExtraRequestHeaders[] = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2633 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2634 | |
| 2635 | // We will control the network layer's responses for |kUrl| using |
| 2636 | // |mock_network_response|. |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 2637 | MockTransaction mock_network_response = { 0 }; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2638 | mock_network_response.url = kUrl; |
| 2639 | AddMockTransaction(&mock_network_response); |
| 2640 | |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 2641 | MockTransaction request = { 0 }; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2642 | request.url = kUrl; |
| 2643 | request.method = "GET"; |
| 2644 | request.request_headers = kExtraRequestHeaders; |
| 2645 | |
| 2646 | kNetResponse.AssignTo(&mock_network_response); // Network mock. |
| 2647 | kNetResponse.AssignTo(&request); // Expected result. |
| 2648 | |
| 2649 | std::string response_headers; |
| 2650 | RunTransactionTestWithResponse( |
| 2651 | cache.http_cache(), request, &response_headers); |
| 2652 | |
| 2653 | EXPECT_EQ(kNetResponse.status_and_headers(), response_headers); |
| 2654 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2655 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2656 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2657 | |
| 2658 | RemoveMockTransaction(&mock_network_response); |
| 2659 | } |
| 2660 | |
| 2661 | // Test that when doing an externally conditionalized if-modified-since |
| 2662 | // and there is no corresponding cache entry, a new cache entry is NOT |
| 2663 | // created (200 response). |
| 2664 | TEST(HttpCache, ConditionalizedRequestUpdatesCache5) { |
| 2665 | MockHttpCache cache; |
| 2666 | |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 2667 | const char kUrl[] = "http://foobar.com/main.css"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2668 | |
| 2669 | static const Response kNetResponse = { |
| 2670 | "HTTP/1.1 200 OK", |
| 2671 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2672 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2673 | "foobar!!!" |
| 2674 | }; |
| 2675 | |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 2676 | const char kExtraRequestHeaders[] = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2677 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2678 | |
| 2679 | // We will control the network layer's responses for |kUrl| using |
| 2680 | // |mock_network_response|. |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 2681 | MockTransaction mock_network_response = { 0 }; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2682 | mock_network_response.url = kUrl; |
| 2683 | AddMockTransaction(&mock_network_response); |
| 2684 | |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 2685 | MockTransaction request = { 0 }; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2686 | request.url = kUrl; |
| 2687 | request.method = "GET"; |
| 2688 | request.request_headers = kExtraRequestHeaders; |
| 2689 | |
| 2690 | kNetResponse.AssignTo(&mock_network_response); // Network mock. |
| 2691 | kNetResponse.AssignTo(&request); // Expected result. |
| 2692 | |
| 2693 | std::string response_headers; |
| 2694 | RunTransactionTestWithResponse( |
| 2695 | cache.http_cache(), request, &response_headers); |
| 2696 | |
| 2697 | EXPECT_EQ(kNetResponse.status_and_headers(), response_headers); |
| 2698 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2699 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2700 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2701 | |
| 2702 | RemoveMockTransaction(&mock_network_response); |
| 2703 | } |
| 2704 | |
| 2705 | // Test that when doing an externally conditionalized if-modified-since |
| 2706 | // if the date does not match the cache entry's last-modified date, |
| 2707 | // then we do NOT use the response (304) to update the cache. |
| 2708 | // (the if-modified-since date is 2 days AFTER the cache's modification date). |
| 2709 | TEST(HttpCache, ConditionalizedRequestUpdatesCache6) { |
| 2710 | static const Response kNetResponse1 = { |
| 2711 | "HTTP/1.1 200 OK", |
| 2712 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2713 | "Server: server1\n" |
| 2714 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2715 | "body1" |
| 2716 | }; |
| 2717 | |
| 2718 | // Second network response for |kUrl|. |
| 2719 | static const Response kNetResponse2 = { |
| 2720 | "HTTP/1.1 304 Not Modified", |
| 2721 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2722 | "Server: server2\n" |
| 2723 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2724 | "" |
| 2725 | }; |
| 2726 | |
| 2727 | // This is two days in the future from the original response's last-modified |
| 2728 | // date! |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 2729 | const char kExtraRequestHeaders[] = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2730 | "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2731 | |
| 2732 | ConditionalizedRequestUpdatesCacheHelper( |
| 2733 | kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2734 | } |
| 2735 | |
| 2736 | // Test that when doing an externally conditionalized if-none-match |
| 2737 | // if the etag does not match the cache entry's etag, then we do not use the |
| 2738 | // response (304) to update the cache. |
| 2739 | TEST(HttpCache, ConditionalizedRequestUpdatesCache7) { |
| 2740 | static const Response kNetResponse1 = { |
| 2741 | "HTTP/1.1 200 OK", |
| 2742 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2743 | "Etag: \"Foo1\"\n" |
| 2744 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2745 | "body1" |
| 2746 | }; |
| 2747 | |
| 2748 | // Second network response for |kUrl|. |
| 2749 | static const Response kNetResponse2 = { |
| 2750 | "HTTP/1.1 304 Not Modified", |
| 2751 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2752 | "Etag: \"Foo2\"\n" |
| 2753 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2754 | "" |
| 2755 | }; |
| 2756 | |
| 2757 | // Different etag from original response. |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 2758 | const char kExtraRequestHeaders[] = "If-None-Match: \"Foo2\"\r\n"; |
[email protected] | 4de4fb1 | 2009-08-03 22:11:18 | [diff] [blame] | 2759 | |
| 2760 | ConditionalizedRequestUpdatesCacheHelper( |
| 2761 | kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2762 | } |
| 2763 | |
[email protected] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2764 | // Test that doing an externally conditionalized request with both if-none-match |
| 2765 | // and if-modified-since updates the cache. |
| 2766 | TEST(HttpCache, ConditionalizedRequestUpdatesCache8) { |
| 2767 | static const Response kNetResponse1 = { |
| 2768 | "HTTP/1.1 200 OK", |
| 2769 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2770 | "Etag: \"Foo1\"\n" |
| 2771 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2772 | "body1" |
| 2773 | }; |
| 2774 | |
| 2775 | // Second network response for |kUrl|. |
| 2776 | static const Response kNetResponse2 = { |
| 2777 | "HTTP/1.1 200 OK", |
| 2778 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2779 | "Etag: \"Foo2\"\n" |
| 2780 | "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2781 | "body2" |
| 2782 | }; |
| 2783 | |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 2784 | const char kExtraRequestHeaders[] = |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 2785 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n" |
| 2786 | "If-None-Match: \"Foo1\"\r\n"; |
[email protected] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2787 | |
| 2788 | ConditionalizedRequestUpdatesCacheHelper( |
| 2789 | kNetResponse1, kNetResponse2, kNetResponse2, kExtraRequestHeaders); |
| 2790 | } |
| 2791 | |
| 2792 | // Test that doing an externally conditionalized request with both if-none-match |
| 2793 | // and if-modified-since does not update the cache with only one match. |
| 2794 | TEST(HttpCache, ConditionalizedRequestUpdatesCache9) { |
| 2795 | static const Response kNetResponse1 = { |
| 2796 | "HTTP/1.1 200 OK", |
| 2797 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2798 | "Etag: \"Foo1\"\n" |
| 2799 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2800 | "body1" |
| 2801 | }; |
| 2802 | |
| 2803 | // Second network response for |kUrl|. |
| 2804 | static const Response kNetResponse2 = { |
| 2805 | "HTTP/1.1 200 OK", |
| 2806 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2807 | "Etag: \"Foo2\"\n" |
| 2808 | "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2809 | "body2" |
| 2810 | }; |
| 2811 | |
| 2812 | // The etag doesn't match what we have stored. |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 2813 | const char kExtraRequestHeaders[] = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2814 | "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n" |
| 2815 | "If-None-Match: \"Foo2\"\r\n"; |
[email protected] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2816 | |
| 2817 | ConditionalizedRequestUpdatesCacheHelper( |
| 2818 | kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2819 | } |
| 2820 | |
| 2821 | // Test that doing an externally conditionalized request with both if-none-match |
| 2822 | // and if-modified-since does not update the cache with only one match. |
| 2823 | TEST(HttpCache, ConditionalizedRequestUpdatesCache10) { |
| 2824 | static const Response kNetResponse1 = { |
| 2825 | "HTTP/1.1 200 OK", |
| 2826 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2827 | "Etag: \"Foo1\"\n" |
| 2828 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2829 | "body1" |
| 2830 | }; |
| 2831 | |
| 2832 | // Second network response for |kUrl|. |
| 2833 | static const Response kNetResponse2 = { |
| 2834 | "HTTP/1.1 200 OK", |
| 2835 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2836 | "Etag: \"Foo2\"\n" |
| 2837 | "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2838 | "body2" |
| 2839 | }; |
| 2840 | |
| 2841 | // The modification date doesn't match what we have stored. |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 2842 | const char kExtraRequestHeaders[] = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 2843 | "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n" |
| 2844 | "If-None-Match: \"Foo1\"\r\n"; |
[email protected] | f2ee745 | 2009-11-02 21:43:02 | [diff] [blame] | 2845 | |
| 2846 | ConditionalizedRequestUpdatesCacheHelper( |
| 2847 | kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2848 | } |
| 2849 | |
[email protected] | 6f40bf7 | 2009-07-23 17:52:37 | [diff] [blame] | 2850 | TEST(HttpCache, UrlContainingHash) { |
| 2851 | MockHttpCache cache; |
| 2852 | |
| 2853 | // Do a typical GET request -- should write an entry into our cache. |
| 2854 | MockTransaction trans(kTypicalGET_Transaction); |
| 2855 | RunTransactionTest(cache.http_cache(), trans); |
| 2856 | |
| 2857 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2858 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2859 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2860 | |
| 2861 | // Request the same URL, but this time with a reference section (hash). |
| 2862 | // Since the cache key strips the hash sections, this should be a cache hit. |
| 2863 | std::string url_with_hash = std::string(trans.url) + "#multiple#hashes"; |
| 2864 | trans.url = url_with_hash.c_str(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2865 | trans.load_flags = LOAD_ONLY_FROM_CACHE; |
[email protected] | 6f40bf7 | 2009-07-23 17:52:37 | [diff] [blame] | 2866 | |
| 2867 | RunTransactionTest(cache.http_cache(), trans); |
| 2868 | |
| 2869 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2870 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2871 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2872 | } |
| 2873 | |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2874 | // Tests that we skip the cache for POST requests that do not have an upload |
| 2875 | // identifier. |
| 2876 | TEST(HttpCache, SimplePOST_SkipsCache) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2877 | MockHttpCache cache; |
| 2878 | |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2879 | RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction); |
| 2880 | |
| 2881 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2882 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2883 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2884 | } |
| 2885 | |
rvargas | 8e4b4b6a | 2014-10-23 21:24:56 | [diff] [blame] | 2886 | // Tests POST handling with a disabled cache (no DCHECK). |
| 2887 | TEST(HttpCache, SimplePOST_DisabledCache) { |
| 2888 | MockHttpCache cache; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2889 | cache.http_cache()->set_mode(HttpCache::Mode::DISABLE); |
rvargas | 8e4b4b6a | 2014-10-23 21:24:56 | [diff] [blame] | 2890 | |
| 2891 | RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction); |
| 2892 | |
| 2893 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2894 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2895 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2896 | } |
| 2897 | |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2898 | TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) { |
| 2899 | MockHttpCache cache; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2900 | |
| 2901 | MockTransaction transaction(kSimplePOST_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2902 | transaction.load_flags |= LOAD_ONLY_FROM_CACHE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2903 | |
| 2904 | MockHttpRequest request(transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2905 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2906 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2907 | scoped_ptr<HttpTransaction> trans; |
| 2908 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 2909 | ASSERT_TRUE(trans.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2910 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2911 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 2912 | ASSERT_EQ(ERR_CACHE_MISS, callback.GetResult(rv)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2913 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 2914 | trans.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2915 | |
| 2916 | EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 2917 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2918 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2919 | } |
| 2920 | |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2921 | TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Hit) { |
| 2922 | MockHttpCache cache; |
| 2923 | |
| 2924 | // Test that we hit the cache for POST requests. |
| 2925 | |
| 2926 | MockTransaction transaction(kSimplePOST_Transaction); |
| 2927 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 2928 | const int64_t kUploadId = 1; // Just a dummy value. |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2929 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 2930 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
| 2931 | element_readers.push_back( |
| 2932 | make_scoped_ptr(new UploadBytesElementReader("hello", 5))); |
| 2933 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2934 | kUploadId); |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2935 | MockHttpRequest request(transaction); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2936 | request.upload_data_stream = &upload_data_stream; |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2937 | |
| 2938 | // Populate the cache. |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2939 | RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2940 | |
| 2941 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2942 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2943 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2944 | |
| 2945 | // Load from cache. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2946 | request.load_flags |= LOAD_ONLY_FROM_CACHE; |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 2947 | RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
[email protected] | 96bac98 | 2009-03-24 18:20:06 | [diff] [blame] | 2948 | |
| 2949 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2950 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2951 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2952 | } |
| 2953 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 2954 | // Test that we don't hit the cache for POST requests if there is a byte range. |
| 2955 | TEST(HttpCache, SimplePOST_WithRanges) { |
| 2956 | MockHttpCache cache; |
| 2957 | |
| 2958 | MockTransaction transaction(kSimplePOST_Transaction); |
| 2959 | transaction.request_headers = "Range: bytes = 0-4\r\n"; |
| 2960 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 2961 | const int64_t kUploadId = 1; // Just a dummy value. |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 2962 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 2963 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
| 2964 | element_readers.push_back( |
| 2965 | make_scoped_ptr(new UploadBytesElementReader("hello", 5))); |
| 2966 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2967 | kUploadId); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2968 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 2969 | MockHttpRequest request(transaction); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2970 | request.upload_data_stream = &upload_data_stream; |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 2971 | |
| 2972 | // Attempt to populate the cache. |
| 2973 | RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
| 2974 | |
| 2975 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2976 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2977 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2978 | } |
| 2979 | |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2980 | // Tests that a POST is cached separately from a previously cached GET. |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 2981 | TEST(HttpCache, SimplePOST_SeparateCache) { |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2982 | MockHttpCache cache; |
| 2983 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 2984 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
| 2985 | element_readers.push_back( |
| 2986 | make_scoped_ptr(new UploadBytesElementReader("hello", 5))); |
| 2987 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1); |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 2988 | |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 2989 | MockTransaction transaction(kSimplePOST_Transaction); |
| 2990 | MockHttpRequest req1(transaction); |
| 2991 | req1.upload_data_stream = &upload_data_stream; |
| 2992 | |
| 2993 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 2994 | |
| 2995 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2996 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2997 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2998 | |
| 2999 | transaction.method = "GET"; |
| 3000 | MockHttpRequest req2(transaction); |
| 3001 | |
| 3002 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3003 | |
| 3004 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3005 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3006 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3007 | } |
| 3008 | |
| 3009 | // Tests that a successful POST invalidates a previously cached GET. |
| 3010 | TEST(HttpCache, SimplePOST_Invalidate_205) { |
| 3011 | MockHttpCache cache; |
| 3012 | |
| 3013 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3014 | AddMockTransaction(&transaction); |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 3015 | MockHttpRequest req1(transaction); |
| 3016 | |
| 3017 | // Attempt to populate the cache. |
| 3018 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3019 | |
| 3020 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3021 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3022 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3023 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 3024 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
| 3025 | element_readers.push_back( |
| 3026 | make_scoped_ptr(new UploadBytesElementReader("hello", 5))); |
| 3027 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3028 | |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 3029 | transaction.method = "POST"; |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 3030 | transaction.status = "HTTP/1.1 205 No Content"; |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 3031 | MockHttpRequest req2(transaction); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3032 | req2.upload_data_stream = &upload_data_stream; |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 3033 | |
| 3034 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3035 | |
| 3036 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3037 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3038 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 3039 | |
| 3040 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3041 | |
| 3042 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3043 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3044 | EXPECT_EQ(3, cache.disk_cache()->create_count()); |
| 3045 | RemoveMockTransaction(&transaction); |
| 3046 | } |
| 3047 | |
[email protected] | cdead512 | 2013-09-12 22:50:49 | [diff] [blame] | 3048 | // Tests that a successful POST invalidates a previously cached GET, even when |
| 3049 | // there is no upload identifier. |
| 3050 | TEST(HttpCache, SimplePOST_NoUploadId_Invalidate_205) { |
| 3051 | MockHttpCache cache; |
| 3052 | |
| 3053 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3054 | AddMockTransaction(&transaction); |
| 3055 | MockHttpRequest req1(transaction); |
| 3056 | |
| 3057 | // Attempt to populate the cache. |
| 3058 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3059 | |
| 3060 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3061 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3062 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3063 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 3064 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
| 3065 | element_readers.push_back( |
| 3066 | make_scoped_ptr(new UploadBytesElementReader("hello", 5))); |
| 3067 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | cdead512 | 2013-09-12 22:50:49 | [diff] [blame] | 3068 | |
| 3069 | transaction.method = "POST"; |
| 3070 | transaction.status = "HTTP/1.1 205 No Content"; |
| 3071 | MockHttpRequest req2(transaction); |
| 3072 | req2.upload_data_stream = &upload_data_stream; |
| 3073 | |
| 3074 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3075 | |
| 3076 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3077 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3078 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3079 | |
| 3080 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3081 | |
| 3082 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3083 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3084 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3085 | RemoveMockTransaction(&transaction); |
| 3086 | } |
| 3087 | |
[email protected] | ed0dc6c | 2013-09-17 19:48:11 | [diff] [blame] | 3088 | // Tests that processing a POST before creating the backend doesn't crash. |
| 3089 | TEST(HttpCache, SimplePOST_NoUploadId_NoBackend) { |
| 3090 | // This will initialize a cache object with NULL backend. |
mmenke | bc31a2c | 2015-10-29 13:44:45 | [diff] [blame] | 3091 | scoped_ptr<MockBlockingBackendFactory> factory( |
| 3092 | new MockBlockingBackendFactory()); |
[email protected] | ed0dc6c | 2013-09-17 19:48:11 | [diff] [blame] | 3093 | factory->set_fail(true); |
| 3094 | factory->FinishCreation(); |
mmenke | bc31a2c | 2015-10-29 13:44:45 | [diff] [blame] | 3095 | MockHttpCache cache(factory.Pass()); |
[email protected] | ed0dc6c | 2013-09-17 19:48:11 | [diff] [blame] | 3096 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 3097 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
| 3098 | element_readers.push_back( |
| 3099 | make_scoped_ptr(new UploadBytesElementReader("hello", 5))); |
| 3100 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | ed0dc6c | 2013-09-17 19:48:11 | [diff] [blame] | 3101 | |
| 3102 | MockTransaction transaction(kSimplePOST_Transaction); |
| 3103 | AddMockTransaction(&transaction); |
| 3104 | MockHttpRequest req(transaction); |
| 3105 | req.upload_data_stream = &upload_data_stream; |
| 3106 | |
| 3107 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req, NULL); |
| 3108 | |
| 3109 | RemoveMockTransaction(&transaction); |
| 3110 | } |
| 3111 | |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 3112 | // Tests that we don't invalidate entries as a result of a failed POST. |
| 3113 | TEST(HttpCache, SimplePOST_DontInvalidate_100) { |
| 3114 | MockHttpCache cache; |
| 3115 | |
| 3116 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3117 | AddMockTransaction(&transaction); |
| 3118 | MockHttpRequest req1(transaction); |
| 3119 | |
| 3120 | // Attempt to populate the cache. |
| 3121 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3122 | |
| 3123 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3124 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3125 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3126 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 3127 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
| 3128 | element_readers.push_back( |
| 3129 | make_scoped_ptr(new UploadBytesElementReader("hello", 5))); |
| 3130 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1); |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 3131 | |
| 3132 | transaction.method = "POST"; |
| 3133 | transaction.status = "HTTP/1.1 100 Continue"; |
| 3134 | MockHttpRequest req2(transaction); |
| 3135 | req2.upload_data_stream = &upload_data_stream; |
| 3136 | |
| 3137 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3138 | |
| 3139 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3140 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3141 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3142 | |
| 3143 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3144 | |
| 3145 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3146 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3147 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3148 | RemoveMockTransaction(&transaction); |
[email protected] | aa5458fd | 2012-04-13 00:06:30 | [diff] [blame] | 3149 | } |
| 3150 | |
[email protected] | b53a412 | 2014-07-31 00:06:46 | [diff] [blame] | 3151 | // Tests that a HEAD request is not cached by itself. |
| 3152 | TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Miss) { |
| 3153 | MockHttpCache cache; |
| 3154 | MockTransaction transaction(kSimplePOST_Transaction); |
| 3155 | AddMockTransaction(&transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3156 | transaction.load_flags |= LOAD_ONLY_FROM_CACHE; |
[email protected] | b53a412 | 2014-07-31 00:06:46 | [diff] [blame] | 3157 | transaction.method = "HEAD"; |
| 3158 | |
| 3159 | MockHttpRequest request(transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3160 | TestCompletionCallback callback; |
[email protected] | b53a412 | 2014-07-31 00:06:46 | [diff] [blame] | 3161 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3162 | scoped_ptr<HttpTransaction> trans; |
| 3163 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
[email protected] | b53a412 | 2014-07-31 00:06:46 | [diff] [blame] | 3164 | ASSERT_TRUE(trans.get()); |
| 3165 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3166 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 3167 | ASSERT_EQ(ERR_CACHE_MISS, callback.GetResult(rv)); |
[email protected] | b53a412 | 2014-07-31 00:06:46 | [diff] [blame] | 3168 | |
| 3169 | trans.reset(); |
| 3170 | |
| 3171 | EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 3172 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3173 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3174 | RemoveMockTransaction(&transaction); |
| 3175 | } |
| 3176 | |
| 3177 | // Tests that a HEAD request is served from a cached GET. |
| 3178 | TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Hit) { |
| 3179 | MockHttpCache cache; |
| 3180 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3181 | AddMockTransaction(&transaction); |
| 3182 | |
| 3183 | // Populate the cache. |
| 3184 | RunTransactionTest(cache.http_cache(), transaction); |
| 3185 | |
| 3186 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3187 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3188 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3189 | |
| 3190 | // Load from cache. |
| 3191 | transaction.method = "HEAD"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3192 | transaction.load_flags |= LOAD_ONLY_FROM_CACHE; |
[email protected] | b53a412 | 2014-07-31 00:06:46 | [diff] [blame] | 3193 | transaction.data = ""; |
| 3194 | RunTransactionTest(cache.http_cache(), transaction); |
| 3195 | |
| 3196 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3197 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3198 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3199 | RemoveMockTransaction(&transaction); |
| 3200 | } |
| 3201 | |
| 3202 | // Tests that a read-only request served from the cache preserves CL. |
| 3203 | TEST(HttpCache, SimpleHEAD_ContentLengthOnHit_Read) { |
| 3204 | MockHttpCache cache; |
| 3205 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3206 | AddMockTransaction(&transaction); |
| 3207 | transaction.response_headers = "Content-Length: 42\n"; |
| 3208 | |
| 3209 | // Populate the cache. |
| 3210 | RunTransactionTest(cache.http_cache(), transaction); |
| 3211 | |
| 3212 | // Load from cache. |
| 3213 | transaction.method = "HEAD"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3214 | transaction.load_flags |= LOAD_ONLY_FROM_CACHE; |
[email protected] | b53a412 | 2014-07-31 00:06:46 | [diff] [blame] | 3215 | transaction.data = ""; |
| 3216 | std::string headers; |
| 3217 | |
| 3218 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3219 | |
| 3220 | EXPECT_EQ("HTTP/1.1 200 OK\nContent-Length: 42\n", headers); |
| 3221 | RemoveMockTransaction(&transaction); |
| 3222 | } |
| 3223 | |
| 3224 | // Tests that a read-write request served from the cache preserves CL. |
| 3225 | TEST(HttpCache, ETagHEAD_ContentLengthOnHit_ReadWrite) { |
| 3226 | MockHttpCache cache; |
| 3227 | MockTransaction transaction(kETagGET_Transaction); |
| 3228 | AddMockTransaction(&transaction); |
| 3229 | std::string server_headers(kETagGET_Transaction.response_headers); |
| 3230 | server_headers.append("Content-Length: 42\n"); |
| 3231 | transaction.response_headers = server_headers.data(); |
| 3232 | |
| 3233 | // Populate the cache. |
| 3234 | RunTransactionTest(cache.http_cache(), transaction); |
| 3235 | |
| 3236 | // Load from cache. |
| 3237 | transaction.method = "HEAD"; |
| 3238 | transaction.data = ""; |
| 3239 | std::string headers; |
| 3240 | |
| 3241 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3242 | |
| 3243 | EXPECT_NE(std::string::npos, headers.find("Content-Length: 42\n")); |
| 3244 | RemoveMockTransaction(&transaction); |
| 3245 | } |
| 3246 | |
| 3247 | // Tests that a HEAD request that includes byte ranges bypasses the cache. |
| 3248 | TEST(HttpCache, SimpleHEAD_WithRanges) { |
| 3249 | MockHttpCache cache; |
| 3250 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3251 | AddMockTransaction(&transaction); |
| 3252 | |
| 3253 | // Populate the cache. |
| 3254 | RunTransactionTest(cache.http_cache(), transaction); |
| 3255 | |
| 3256 | // Load from cache. |
| 3257 | transaction.method = "HEAD"; |
| 3258 | transaction.request_headers = "Range: bytes = 0-4\r\n"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3259 | transaction.load_flags |= LOAD_ONLY_FROM_CACHE; |
| 3260 | transaction.return_code = ERR_CACHE_MISS; |
[email protected] | b53a412 | 2014-07-31 00:06:46 | [diff] [blame] | 3261 | RunTransactionTest(cache.http_cache(), transaction); |
| 3262 | |
| 3263 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3264 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3265 | RemoveMockTransaction(&transaction); |
| 3266 | } |
| 3267 | |
| 3268 | // Tests that a HEAD request can be served from a partialy cached resource. |
| 3269 | TEST(HttpCache, SimpleHEAD_WithCachedRanges) { |
| 3270 | MockHttpCache cache; |
| 3271 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3272 | |
| 3273 | // Write to the cache (40-49). |
| 3274 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 3275 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3276 | |
| 3277 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3278 | |
| 3279 | transaction.url = kRangeGET_TransactionOK.url; |
| 3280 | transaction.method = "HEAD"; |
| 3281 | transaction.data = ""; |
| 3282 | AddMockTransaction(&transaction); |
| 3283 | std::string headers; |
| 3284 | |
| 3285 | // Load from cache. |
| 3286 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3287 | |
| 3288 | EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n")); |
rvargas | ee8204a | 2015-02-11 21:28:11 | [diff] [blame] | 3289 | EXPECT_NE(std::string::npos, headers.find("Content-Length: 80\n")); |
[email protected] | b53a412 | 2014-07-31 00:06:46 | [diff] [blame] | 3290 | EXPECT_EQ(std::string::npos, headers.find("Content-Range")); |
| 3291 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3292 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3293 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3294 | RemoveMockTransaction(&transaction); |
| 3295 | } |
| 3296 | |
| 3297 | // Tests that a HEAD request can be served from a truncated resource. |
| 3298 | TEST(HttpCache, SimpleHEAD_WithTruncatedEntry) { |
| 3299 | MockHttpCache cache; |
| 3300 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 3301 | |
| 3302 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 3303 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 3304 | "ETag: \"foo\"\n" |
| 3305 | "Accept-Ranges: bytes\n" |
| 3306 | "Content-Length: 80\n"); |
| 3307 | CreateTruncatedEntry(raw_headers, &cache); |
| 3308 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3309 | |
| 3310 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3311 | |
| 3312 | transaction.url = kRangeGET_TransactionOK.url; |
| 3313 | transaction.method = "HEAD"; |
| 3314 | transaction.data = ""; |
| 3315 | AddMockTransaction(&transaction); |
| 3316 | std::string headers; |
| 3317 | |
| 3318 | // Load from cache. |
| 3319 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3320 | |
| 3321 | EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n")); |
| 3322 | EXPECT_NE(std::string::npos, headers.find("Content-Length: 80\n")); |
| 3323 | EXPECT_EQ(std::string::npos, headers.find("Content-Range")); |
| 3324 | EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 3325 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3326 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3327 | RemoveMockTransaction(&transaction); |
| 3328 | } |
| 3329 | |
| 3330 | // Tests that a HEAD request updates the cached response. |
| 3331 | TEST(HttpCache, TypicalHEAD_UpdatesResponse) { |
| 3332 | MockHttpCache cache; |
| 3333 | MockTransaction transaction(kTypicalGET_Transaction); |
| 3334 | AddMockTransaction(&transaction); |
| 3335 | |
| 3336 | // Populate the cache. |
| 3337 | RunTransactionTest(cache.http_cache(), transaction); |
| 3338 | |
| 3339 | // Update the cache. |
| 3340 | transaction.method = "HEAD"; |
| 3341 | transaction.response_headers = "Foo: bar\n"; |
| 3342 | transaction.data = ""; |
| 3343 | transaction.status = "HTTP/1.1 304 Not Modified\n"; |
| 3344 | std::string headers; |
| 3345 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3346 | RemoveMockTransaction(&transaction); |
| 3347 | |
| 3348 | EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n")); |
| 3349 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3350 | |
| 3351 | MockTransaction transaction2(kTypicalGET_Transaction); |
| 3352 | AddMockTransaction(&transaction2); |
| 3353 | |
| 3354 | // Make sure we are done with the previous transaction. |
| 3355 | base::MessageLoop::current()->RunUntilIdle(); |
| 3356 | |
| 3357 | // Load from the cache. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3358 | transaction2.load_flags |= LOAD_ONLY_FROM_CACHE; |
[email protected] | b53a412 | 2014-07-31 00:06:46 | [diff] [blame] | 3359 | RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
| 3360 | |
| 3361 | EXPECT_NE(std::string::npos, headers.find("Foo: bar\n")); |
| 3362 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3363 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 3364 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3365 | RemoveMockTransaction(&transaction2); |
| 3366 | } |
| 3367 | |
| 3368 | // Tests that an externally conditionalized HEAD request updates the cache. |
| 3369 | TEST(HttpCache, TypicalHEAD_ConditionalizedRequestUpdatesResponse) { |
| 3370 | MockHttpCache cache; |
| 3371 | MockTransaction transaction(kTypicalGET_Transaction); |
| 3372 | AddMockTransaction(&transaction); |
| 3373 | |
| 3374 | // Populate the cache. |
| 3375 | RunTransactionTest(cache.http_cache(), transaction); |
| 3376 | |
| 3377 | // Update the cache. |
| 3378 | transaction.method = "HEAD"; |
| 3379 | transaction.request_headers = |
| 3380 | "If-Modified-Since: Wed, 28 Nov 2007 00:40:09 GMT\r\n"; |
| 3381 | transaction.response_headers = "Foo: bar\n"; |
| 3382 | transaction.data = ""; |
| 3383 | transaction.status = "HTTP/1.1 304 Not Modified\n"; |
| 3384 | std::string headers; |
| 3385 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3386 | RemoveMockTransaction(&transaction); |
| 3387 | |
| 3388 | EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 304 Not Modified\n")); |
| 3389 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3390 | |
| 3391 | MockTransaction transaction2(kTypicalGET_Transaction); |
| 3392 | AddMockTransaction(&transaction2); |
| 3393 | |
| 3394 | // Make sure we are done with the previous transaction. |
| 3395 | base::MessageLoop::current()->RunUntilIdle(); |
| 3396 | |
| 3397 | // Load from the cache. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3398 | transaction2.load_flags |= LOAD_ONLY_FROM_CACHE; |
[email protected] | b53a412 | 2014-07-31 00:06:46 | [diff] [blame] | 3399 | RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
| 3400 | |
| 3401 | EXPECT_NE(std::string::npos, headers.find("Foo: bar\n")); |
| 3402 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3403 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 3404 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3405 | RemoveMockTransaction(&transaction2); |
| 3406 | } |
| 3407 | |
| 3408 | // Tests that a HEAD request invalidates an old cached entry. |
| 3409 | TEST(HttpCache, SimpleHEAD_InvalidatesEntry) { |
| 3410 | MockHttpCache cache; |
| 3411 | MockTransaction transaction(kTypicalGET_Transaction); |
| 3412 | AddMockTransaction(&transaction); |
| 3413 | |
| 3414 | // Populate the cache. |
| 3415 | RunTransactionTest(cache.http_cache(), transaction); |
| 3416 | |
| 3417 | // Update the cache. |
| 3418 | transaction.method = "HEAD"; |
| 3419 | transaction.data = ""; |
| 3420 | RunTransactionTest(cache.http_cache(), transaction); |
| 3421 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3422 | |
| 3423 | // Load from the cache. |
| 3424 | transaction.method = "GET"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3425 | transaction.load_flags |= LOAD_ONLY_FROM_CACHE; |
| 3426 | transaction.return_code = ERR_CACHE_MISS; |
[email protected] | b53a412 | 2014-07-31 00:06:46 | [diff] [blame] | 3427 | RunTransactionTest(cache.http_cache(), transaction); |
| 3428 | |
| 3429 | RemoveMockTransaction(&transaction); |
| 3430 | } |
| 3431 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3432 | // Tests that we do not cache the response of a PUT. |
| 3433 | TEST(HttpCache, SimplePUT_Miss) { |
| 3434 | MockHttpCache cache; |
| 3435 | |
| 3436 | MockTransaction transaction(kSimplePOST_Transaction); |
| 3437 | transaction.method = "PUT"; |
| 3438 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 3439 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
| 3440 | element_readers.push_back( |
| 3441 | make_scoped_ptr(new UploadBytesElementReader("hello", 5))); |
| 3442 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3443 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3444 | MockHttpRequest request(transaction); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3445 | request.upload_data_stream = &upload_data_stream; |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3446 | |
| 3447 | // Attempt to populate the cache. |
| 3448 | RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
| 3449 | |
| 3450 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3451 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3452 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3453 | } |
| 3454 | |
| 3455 | // Tests that we invalidate entries as a result of a PUT. |
| 3456 | TEST(HttpCache, SimplePUT_Invalidate) { |
| 3457 | MockHttpCache cache; |
| 3458 | |
[email protected] | f5648e9 | 2012-08-02 17:13:04 | [diff] [blame] | 3459 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3460 | MockHttpRequest req1(transaction); |
| 3461 | |
| 3462 | // Attempt to populate the cache. |
| 3463 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3464 | |
| 3465 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3466 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3467 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3468 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 3469 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
| 3470 | element_readers.push_back( |
| 3471 | make_scoped_ptr(new UploadBytesElementReader("hello", 5))); |
| 3472 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3473 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3474 | transaction.method = "PUT"; |
| 3475 | MockHttpRequest req2(transaction); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3476 | req2.upload_data_stream = &upload_data_stream; |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3477 | |
| 3478 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3479 | |
| 3480 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3481 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3482 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3483 | |
| 3484 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3485 | |
| 3486 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3487 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3488 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3489 | } |
| 3490 | |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 3491 | // Tests that we invalidate entries as a result of a PUT. |
| 3492 | TEST(HttpCache, SimplePUT_Invalidate_305) { |
| 3493 | MockHttpCache cache; |
| 3494 | |
| 3495 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3496 | AddMockTransaction(&transaction); |
| 3497 | MockHttpRequest req1(transaction); |
| 3498 | |
| 3499 | // Attempt to populate the cache. |
| 3500 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3501 | |
| 3502 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3503 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3504 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3505 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 3506 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
| 3507 | element_readers.push_back( |
| 3508 | make_scoped_ptr(new UploadBytesElementReader("hello", 5))); |
| 3509 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 3510 | |
| 3511 | transaction.method = "PUT"; |
| 3512 | transaction.status = "HTTP/1.1 305 Use Proxy"; |
| 3513 | MockHttpRequest req2(transaction); |
| 3514 | req2.upload_data_stream = &upload_data_stream; |
| 3515 | |
| 3516 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3517 | |
| 3518 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3519 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3520 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3521 | |
| 3522 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3523 | |
| 3524 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3525 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3526 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3527 | RemoveMockTransaction(&transaction); |
| 3528 | } |
| 3529 | |
| 3530 | // Tests that we don't invalidate entries as a result of a failed PUT. |
| 3531 | TEST(HttpCache, SimplePUT_DontInvalidate_404) { |
| 3532 | MockHttpCache cache; |
| 3533 | |
| 3534 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3535 | AddMockTransaction(&transaction); |
| 3536 | MockHttpRequest req1(transaction); |
| 3537 | |
| 3538 | // Attempt to populate the cache. |
| 3539 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3540 | |
| 3541 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3542 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3543 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3544 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 3545 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
| 3546 | element_readers.push_back( |
| 3547 | make_scoped_ptr(new UploadBytesElementReader("hello", 5))); |
| 3548 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 3549 | |
| 3550 | transaction.method = "PUT"; |
| 3551 | transaction.status = "HTTP/1.1 404 Not Found"; |
| 3552 | MockHttpRequest req2(transaction); |
| 3553 | req2.upload_data_stream = &upload_data_stream; |
| 3554 | |
| 3555 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3556 | |
| 3557 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3558 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3559 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3560 | |
| 3561 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3562 | |
| 3563 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3564 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 3565 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3566 | RemoveMockTransaction(&transaction); |
| 3567 | } |
| 3568 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3569 | // Tests that we do not cache the response of a DELETE. |
| 3570 | TEST(HttpCache, SimpleDELETE_Miss) { |
| 3571 | MockHttpCache cache; |
| 3572 | |
| 3573 | MockTransaction transaction(kSimplePOST_Transaction); |
| 3574 | transaction.method = "DELETE"; |
| 3575 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 3576 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
| 3577 | element_readers.push_back( |
| 3578 | make_scoped_ptr(new UploadBytesElementReader("hello", 5))); |
| 3579 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3580 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3581 | MockHttpRequest request(transaction); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3582 | request.upload_data_stream = &upload_data_stream; |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3583 | |
| 3584 | // Attempt to populate the cache. |
| 3585 | RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
| 3586 | |
| 3587 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3588 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3589 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3590 | } |
| 3591 | |
| 3592 | // Tests that we invalidate entries as a result of a DELETE. |
| 3593 | TEST(HttpCache, SimpleDELETE_Invalidate) { |
| 3594 | MockHttpCache cache; |
| 3595 | |
[email protected] | f5648e9 | 2012-08-02 17:13:04 | [diff] [blame] | 3596 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3597 | MockHttpRequest req1(transaction); |
| 3598 | |
| 3599 | // Attempt to populate the cache. |
| 3600 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3601 | |
| 3602 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3603 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3604 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3605 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 3606 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
| 3607 | element_readers.push_back( |
| 3608 | make_scoped_ptr(new UploadBytesElementReader("hello", 5))); |
| 3609 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3610 | |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3611 | transaction.method = "DELETE"; |
| 3612 | MockHttpRequest req2(transaction); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3613 | req2.upload_data_stream = &upload_data_stream; |
[email protected] | 7940e16 | 2012-03-14 03:45:18 | [diff] [blame] | 3614 | |
| 3615 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3616 | |
| 3617 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3618 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3619 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3620 | |
| 3621 | RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3622 | |
| 3623 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3624 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3625 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3626 | } |
| 3627 | |
[email protected] | f42cac9 | 2012-12-21 22:59:05 | [diff] [blame] | 3628 | // Tests that we invalidate entries as a result of a DELETE. |
| 3629 | TEST(HttpCache, SimpleDELETE_Invalidate_301) { |
| 3630 | MockHttpCache cache; |
| 3631 | |
| 3632 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3633 | AddMockTransaction(&transaction); |
| 3634 | |
| 3635 | // Attempt to populate the cache. |
| 3636 | RunTransactionTest(cache.http_cache(), transaction); |
| 3637 | |
| 3638 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3639 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3640 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3641 | |
| 3642 | transaction.method = "DELETE"; |
| 3643 | transaction.status = "HTTP/1.1 301 Moved Permanently "; |
| 3644 | |
| 3645 | RunTransactionTest(cache.http_cache(), transaction); |
| 3646 | |
| 3647 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3648 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3649 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3650 | |
| 3651 | transaction.method = "GET"; |
| 3652 | RunTransactionTest(cache.http_cache(), transaction); |
| 3653 | |
| 3654 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3655 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3656 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3657 | RemoveMockTransaction(&transaction); |
| 3658 | } |
| 3659 | |
| 3660 | // Tests that we don't invalidate entries as a result of a failed DELETE. |
| 3661 | TEST(HttpCache, SimpleDELETE_DontInvalidate_416) { |
| 3662 | MockHttpCache cache; |
| 3663 | |
| 3664 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3665 | AddMockTransaction(&transaction); |
| 3666 | |
| 3667 | // Attempt to populate the cache. |
| 3668 | RunTransactionTest(cache.http_cache(), transaction); |
| 3669 | |
| 3670 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3671 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3672 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3673 | |
| 3674 | transaction.method = "DELETE"; |
| 3675 | transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable"; |
| 3676 | |
| 3677 | RunTransactionTest(cache.http_cache(), transaction); |
| 3678 | |
| 3679 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3680 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3681 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3682 | |
| 3683 | transaction.method = "GET"; |
| 3684 | transaction.status = "HTTP/1.1 200 OK"; |
| 3685 | RunTransactionTest(cache.http_cache(), transaction); |
| 3686 | |
| 3687 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3688 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 3689 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3690 | RemoveMockTransaction(&transaction); |
| 3691 | } |
| 3692 | |
[email protected] | 7d84e64 | 2013-04-11 00:04:07 | [diff] [blame] | 3693 | // Tests that we don't invalidate entries after a failed network transaction. |
| 3694 | TEST(HttpCache, SimpleGET_DontInvalidateOnFailure) { |
| 3695 | MockHttpCache cache; |
| 3696 | |
| 3697 | // Populate the cache. |
| 3698 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 3699 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3700 | |
| 3701 | // Fail the network request. |
| 3702 | MockTransaction transaction(kSimpleGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3703 | transaction.return_code = ERR_FAILED; |
| 3704 | transaction.load_flags |= LOAD_VALIDATE_CACHE; |
[email protected] | 7d84e64 | 2013-04-11 00:04:07 | [diff] [blame] | 3705 | |
| 3706 | AddMockTransaction(&transaction); |
| 3707 | RunTransactionTest(cache.http_cache(), transaction); |
| 3708 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3709 | RemoveMockTransaction(&transaction); |
| 3710 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3711 | transaction.load_flags = LOAD_ONLY_FROM_CACHE; |
| 3712 | transaction.return_code = OK; |
[email protected] | 7d84e64 | 2013-04-11 00:04:07 | [diff] [blame] | 3713 | AddMockTransaction(&transaction); |
| 3714 | RunTransactionTest(cache.http_cache(), transaction); |
| 3715 | |
| 3716 | // Make sure the transaction didn't reach the network. |
| 3717 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3718 | RemoveMockTransaction(&transaction); |
| 3719 | } |
| 3720 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3721 | TEST(HttpCache, RangeGET_SkipsCache) { |
| 3722 | MockHttpCache cache; |
| 3723 | |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 3724 | // Test that we skip the cache for range GET requests. Eventually, we will |
| 3725 | // want to cache these, but we'll still have cases where skipping the cache |
| 3726 | // makes sense, so we want to make sure that it works properly. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3727 | |
| 3728 | RunTransactionTest(cache.http_cache(), kRangeGET_Transaction); |
| 3729 | |
| 3730 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3731 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3732 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3733 | |
| 3734 | MockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 3735 | transaction.request_headers = "If-None-Match: foo\r\n"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3736 | RunTransactionTest(cache.http_cache(), transaction); |
| 3737 | |
| 3738 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3739 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3740 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3741 | |
[email protected] | 72d1e59 | 2009-03-10 17:39:46 | [diff] [blame] | 3742 | transaction.request_headers = |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 3743 | "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3744 | RunTransactionTest(cache.http_cache(), transaction); |
| 3745 | |
| 3746 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3747 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3748 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3749 | } |
| 3750 | |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 3751 | // Test that we skip the cache for range requests that include a validation |
| 3752 | // header. |
| 3753 | TEST(HttpCache, RangeGET_SkipsCache2) { |
| 3754 | MockHttpCache cache; |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 3755 | |
| 3756 | MockTransaction transaction(kRangeGET_Transaction); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3757 | transaction.request_headers = "If-None-Match: foo\r\n" |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3758 | EXTRA_HEADER |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 3759 | "Range: bytes = 40-49\r\n"; |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 3760 | RunTransactionTest(cache.http_cache(), transaction); |
| 3761 | |
| 3762 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3763 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3764 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3765 | |
| 3766 | transaction.request_headers = |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3767 | "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n" |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3768 | EXTRA_HEADER |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 3769 | "Range: bytes = 40-49\r\n"; |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 3770 | RunTransactionTest(cache.http_cache(), transaction); |
| 3771 | |
| 3772 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3773 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3774 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3775 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3776 | transaction.request_headers = "If-Range: bla\r\n" |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3777 | EXTRA_HEADER |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 3778 | "Range: bytes = 40-49\r\n"; |
[email protected] | 86291440d | 2009-08-28 18:46:35 | [diff] [blame] | 3779 | RunTransactionTest(cache.http_cache(), transaction); |
| 3780 | |
| 3781 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3782 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3783 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3784 | } |
| 3785 | |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 3786 | TEST(HttpCache, SimpleGET_DoesntLogHeaders) { |
| 3787 | MockHttpCache cache; |
| 3788 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3789 | BoundTestNetLog log; |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 3790 | RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, |
| 3791 | log.bound()); |
| 3792 | |
| 3793 | EXPECT_FALSE(LogContainsEventType( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3794 | log, NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS)); |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 3795 | } |
| 3796 | |
| 3797 | TEST(HttpCache, RangeGET_LogsHeaders) { |
| 3798 | MockHttpCache cache; |
| 3799 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3800 | BoundTestNetLog log; |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 3801 | RunTransactionTestWithLog(cache.http_cache(), kRangeGET_Transaction, |
| 3802 | log.bound()); |
| 3803 | |
| 3804 | EXPECT_TRUE(LogContainsEventType( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3805 | log, NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS)); |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | TEST(HttpCache, ExternalValidation_LogsHeaders) { |
| 3809 | MockHttpCache cache; |
| 3810 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3811 | BoundTestNetLog log; |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 3812 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3813 | transaction.request_headers = "If-None-Match: foo\r\n" EXTRA_HEADER; |
| 3814 | RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); |
| 3815 | |
| 3816 | EXPECT_TRUE(LogContainsEventType( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3817 | log, NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS)); |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | TEST(HttpCache, SpecialHeaders_LogsHeaders) { |
| 3821 | MockHttpCache cache; |
| 3822 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3823 | BoundTestNetLog log; |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 3824 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3825 | transaction.request_headers = "cache-control: no-cache\r\n" EXTRA_HEADER; |
| 3826 | RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); |
| 3827 | |
| 3828 | EXPECT_TRUE(LogContainsEventType( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3829 | log, NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS)); |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 3830 | } |
| 3831 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 3832 | // Tests that receiving 206 for a regular request is handled correctly. |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 3833 | TEST(HttpCache, GET_Crazy206) { |
| 3834 | MockHttpCache cache; |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 3835 | |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 3836 | // Write to the cache. |
| 3837 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3838 | AddMockTransaction(&transaction); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 3839 | transaction.request_headers = EXTRA_HEADER; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3840 | transaction.handler = NULL; |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 3841 | RunTransactionTest(cache.http_cache(), transaction); |
| 3842 | |
| 3843 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3844 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3845 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3846 | |
| 3847 | // This should read again from the net. |
| 3848 | RunTransactionTest(cache.http_cache(), transaction); |
| 3849 | |
| 3850 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 3851 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3852 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 3853 | RemoveMockTransaction(&transaction); |
[email protected] | 7ee4c407 | 2009-06-30 18:49:47 | [diff] [blame] | 3854 | } |
| 3855 | |
[email protected] | 6a2e83ea | 2014-01-02 20:47:56 | [diff] [blame] | 3856 | // Tests that receiving 416 for a regular request is handled correctly. |
| 3857 | TEST(HttpCache, GET_Crazy416) { |
| 3858 | MockHttpCache cache; |
| 3859 | |
| 3860 | // Write to the cache. |
| 3861 | MockTransaction transaction(kSimpleGET_Transaction); |
| 3862 | AddMockTransaction(&transaction); |
| 3863 | transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable"; |
| 3864 | RunTransactionTest(cache.http_cache(), transaction); |
| 3865 | |
| 3866 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3867 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3868 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3869 | |
| 3870 | RemoveMockTransaction(&transaction); |
| 3871 | } |
| 3872 | |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3873 | // Tests that we don't store partial responses that can't be validated. |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 3874 | TEST(HttpCache, RangeGET_NoStrongValidators) { |
| 3875 | MockHttpCache cache; |
| 3876 | std::string headers; |
| 3877 | |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3878 | // Attempt to write to the cache (40-49). |
| 3879 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
rvargas | 439a05cd | 2014-08-30 01:43:56 | [diff] [blame] | 3880 | transaction.response_headers = "Content-Length: 10\n" |
| 3881 | "Cache-Control: max-age=3600\n" |
| 3882 | "ETag: w/\"foo\"\n"; |
| 3883 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3884 | |
| 3885 | Verify206Response(headers, 40, 49); |
| 3886 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3887 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3888 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3889 | |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3890 | // Now verify that there's no cached data. |
rvargas | 439a05cd | 2014-08-30 01:43:56 | [diff] [blame] | 3891 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 3892 | &headers); |
| 3893 | |
| 3894 | Verify206Response(headers, 40, 49); |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3895 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3896 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3897 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
rvargas | 439a05cd | 2014-08-30 01:43:56 | [diff] [blame] | 3898 | } |
| 3899 | |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3900 | // Tests failures to conditionalize byte range requests. |
| 3901 | TEST(HttpCache, RangeGET_NoConditionalization) { |
rvargas | 439a05cd | 2014-08-30 01:43:56 | [diff] [blame] | 3902 | MockHttpCache cache; |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3903 | cache.FailConditionalizations(); |
rvargas | 439a05cd | 2014-08-30 01:43:56 | [diff] [blame] | 3904 | std::string headers; |
| 3905 | |
| 3906 | // Write to the cache (40-49). |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3907 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 3908 | transaction.response_headers = "Content-Length: 10\n" |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3909 | "ETag: \"foo\"\n"; |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 3910 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3911 | |
| 3912 | Verify206Response(headers, 40, 49); |
| 3913 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3914 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3915 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3916 | |
rvargas | 439a05cd | 2014-08-30 01:43:56 | [diff] [blame] | 3917 | // Now verify that the cached data is not used. |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 3918 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 3919 | &headers); |
| 3920 | |
| 3921 | Verify206Response(headers, 40, 49); |
| 3922 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
rvargas | 439a05cd | 2014-08-30 01:43:56 | [diff] [blame] | 3923 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 3924 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 3925 | } |
| 3926 | |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 3927 | // Tests that restarting a partial request when the cached data cannot be |
| 3928 | // revalidated logs an event. |
| 3929 | TEST(HttpCache, RangeGET_NoValidation_LogsRestart) { |
| 3930 | MockHttpCache cache; |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3931 | cache.FailConditionalizations(); |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 3932 | |
| 3933 | // Write to the cache (40-49). |
| 3934 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3935 | transaction.response_headers = "Content-Length: 10\n" |
| 3936 | "ETag: \"foo\"\n"; |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 3937 | RunTransactionTest(cache.http_cache(), transaction); |
| 3938 | |
| 3939 | // Now verify that the cached data is not used. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3940 | BoundTestNetLog log; |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 3941 | RunTransactionTestWithLog(cache.http_cache(), kRangeGET_TransactionOK, |
| 3942 | log.bound()); |
| 3943 | |
| 3944 | EXPECT_TRUE(LogContainsEventType( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3945 | log, NetLog::TYPE_HTTP_CACHE_RESTART_PARTIAL_REQUEST)); |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 3946 | } |
| 3947 | |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3948 | // Tests that a failure to conditionalize a regular request (no range) with a |
| 3949 | // sparse entry results in a full response. |
| 3950 | TEST(HttpCache, GET_NoConditionalization) { |
rvargas | b499156 | 2015-01-07 21:59:57 | [diff] [blame] | 3951 | MockHttpCache cache; |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3952 | cache.FailConditionalizations(); |
rvargas | b499156 | 2015-01-07 21:59:57 | [diff] [blame] | 3953 | std::string headers; |
| 3954 | |
| 3955 | // Write to the cache (40-49). |
| 3956 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3957 | transaction.response_headers = "Content-Length: 10\n" |
| 3958 | "ETag: \"foo\"\n"; |
rvargas | b499156 | 2015-01-07 21:59:57 | [diff] [blame] | 3959 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3960 | |
| 3961 | Verify206Response(headers, 40, 49); |
| 3962 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3963 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3964 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3965 | |
| 3966 | // Now verify that the cached data is not used. |
| 3967 | // Don't ask for a range. The cache will attempt to use the cached data but |
| 3968 | // should discard it as it cannot be validated. A regular request should go |
| 3969 | // to the server and a new entry should be created. |
| 3970 | transaction.request_headers = EXTRA_HEADER; |
| 3971 | transaction.data = "Not a range"; |
| 3972 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3973 | |
| 3974 | EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); |
| 3975 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3976 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3977 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3978 | |
| 3979 | // The last response was saved. |
| 3980 | RunTransactionTest(cache.http_cache(), transaction); |
| 3981 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3982 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 3983 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3984 | } |
| 3985 | |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3986 | // Verifies that conditionalization failures when asking for a range that would |
| 3987 | // require the cache to modify the range to ask, result in a network request |
| 3988 | // that matches the user's one. |
| 3989 | TEST(HttpCache, RangeGET_NoConditionalization2) { |
rvargas | b499156 | 2015-01-07 21:59:57 | [diff] [blame] | 3990 | MockHttpCache cache; |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3991 | cache.FailConditionalizations(); |
rvargas | b499156 | 2015-01-07 21:59:57 | [diff] [blame] | 3992 | std::string headers; |
| 3993 | |
| 3994 | // Write to the cache (40-49). |
| 3995 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 3996 | transaction.response_headers = "Content-Length: 10\n" |
| 3997 | "ETag: \"foo\"\n"; |
rvargas | b499156 | 2015-01-07 21:59:57 | [diff] [blame] | 3998 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3999 | |
| 4000 | Verify206Response(headers, 40, 49); |
| 4001 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4002 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4003 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4004 | |
| 4005 | // Now verify that the cached data is not used. |
| 4006 | // Ask for a range that extends before and after the cached data so that the |
| 4007 | // cache would normally mix data from three sources. After deleting the entry, |
| 4008 | // the response will come from a single network request. |
| 4009 | transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
| 4010 | transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
| 4011 | transaction.response_headers = kRangeGET_TransactionOK.response_headers; |
| 4012 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4013 | |
| 4014 | Verify206Response(headers, 20, 59); |
| 4015 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4016 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4017 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4018 | |
| 4019 | // The last response was saved. |
| 4020 | RunTransactionTest(cache.http_cache(), transaction); |
| 4021 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4022 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 4023 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4024 | } |
| 4025 | |
[email protected] | 9f10ec3 | 2013-11-01 00:51:53 | [diff] [blame] | 4026 | // Tests that we cache partial responses that lack content-length. |
| 4027 | TEST(HttpCache, RangeGET_NoContentLength) { |
| 4028 | MockHttpCache cache; |
| 4029 | std::string headers; |
| 4030 | |
| 4031 | // Attempt to write to the cache (40-49). |
| 4032 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4033 | AddMockTransaction(&transaction); |
| 4034 | transaction.response_headers = "ETag: \"foo\"\n" |
| 4035 | "Accept-Ranges: bytes\n" |
| 4036 | "Content-Range: bytes 40-49/80\n"; |
| 4037 | transaction.handler = NULL; |
| 4038 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4039 | |
| 4040 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4041 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4042 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4043 | |
| 4044 | // Now verify that there's no cached data. |
| 4045 | transaction.handler = &RangeTransactionServer::RangeHandler; |
| 4046 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 4047 | &headers); |
| 4048 | |
| 4049 | Verify206Response(headers, 40, 49); |
| 4050 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4051 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4052 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4053 | |
| 4054 | RemoveMockTransaction(&transaction); |
| 4055 | } |
| 4056 | |
tfarina | e04a95b | 2015-09-18 22:48:12 | [diff] [blame] | 4057 | // Fails only on bots. crbug.com/533640 |
| 4058 | #if defined(OS_ANDROID) |
| 4059 | #define MAYBE_RangeGET_OK DISABLED_RangeGET_OK |
| 4060 | #else |
| 4061 | #define MAYBE_RangeGET_OK RangeGET_OK |
| 4062 | #endif |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4063 | // Tests that we can cache range requests and fetch random blocks from the |
| 4064 | // cache and the network. |
tfarina | e04a95b | 2015-09-18 22:48:12 | [diff] [blame] | 4065 | TEST(HttpCache, MAYBE_RangeGET_OK) { |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 4066 | MockHttpCache cache; |
| 4067 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 4068 | std::string headers; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 4069 | |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 4070 | // Write to the cache (40-49). |
| 4071 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 4072 | &headers); |
| 4073 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4074 | Verify206Response(headers, 40, 49); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 4075 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4076 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4077 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4078 | |
| 4079 | // Read from the cache (40-49). |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 4080 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 4081 | &headers); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 4082 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4083 | Verify206Response(headers, 40, 49); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4084 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 4085 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4086 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4087 | |
| 4088 | // Make sure we are done with the previous transaction. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 4089 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 4090 | |
| 4091 | // Write to the cache (30-39). |
| 4092 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 4093 | transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 4094 | transaction.data = "rg: 30-39 "; |
[email protected] | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 4095 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 4096 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4097 | Verify206Response(headers, 30, 39); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4098 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 4099 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 4100 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4101 | |
| 4102 | // Make sure we are done with the previous transaction. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 4103 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 4104 | |
| 4105 | // Write and read from the cache (20-59). |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 4106 | transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 4107 | transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4108 | BoundTestNetLog log; |
| 4109 | LoadTimingInfo load_timing_info; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4110 | RunTransactionTestWithResponseAndGetTiming( |
| 4111 | cache.http_cache(), transaction, &headers, log.bound(), |
| 4112 | &load_timing_info); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 4113 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4114 | Verify206Response(headers, 20, 59); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4115 | EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 4116 | EXPECT_EQ(3, cache.disk_cache()->open_count()); |
| 4117 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4118 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 4119 | |
| 4120 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4121 | } |
| 4122 | |
tfarina | e04a95b | 2015-09-18 22:48:12 | [diff] [blame] | 4123 | // Fails only on bots. crbug.com/533640 |
| 4124 | #if defined(OS_ANDROID) |
| 4125 | #define MAYBE_RangeGET_SyncOK DISABLED_RangeGET_SyncOK |
| 4126 | #else |
| 4127 | #define MAYBE_RangeGET_SyncOK RangeGET_SyncOK |
| 4128 | #endif |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4129 | // Tests that we can cache range requests and fetch random blocks from the |
| 4130 | // cache and the network, with synchronous responses. |
tfarina | e04a95b | 2015-09-18 22:48:12 | [diff] [blame] | 4131 | TEST(HttpCache, MAYBE_RangeGET_SyncOK) { |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4132 | MockHttpCache cache; |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4133 | |
| 4134 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4135 | transaction.test_mode = TEST_MODE_SYNC_ALL; |
| 4136 | AddMockTransaction(&transaction); |
| 4137 | |
| 4138 | // Write to the cache (40-49). |
| 4139 | std::string headers; |
| 4140 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4141 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4142 | Verify206Response(headers, 40, 49); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4143 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4144 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4145 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4146 | |
| 4147 | // Read from the cache (40-49). |
| 4148 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4149 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4150 | Verify206Response(headers, 40, 49); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4151 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4152 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4153 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4154 | |
| 4155 | // Make sure we are done with the previous transaction. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 4156 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4157 | |
| 4158 | // Write to the cache (30-39). |
| 4159 | transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; |
| 4160 | transaction.data = "rg: 30-39 "; |
| 4161 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4162 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4163 | Verify206Response(headers, 30, 39); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4164 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4165 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4166 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4167 | |
| 4168 | // Make sure we are done with the previous transaction. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 4169 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4170 | |
| 4171 | // Write and read from the cache (20-59). |
| 4172 | transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
| 4173 | transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4174 | BoundTestNetLog log; |
| 4175 | LoadTimingInfo load_timing_info; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4176 | RunTransactionTestWithResponseAndGetTiming( |
| 4177 | cache.http_cache(), transaction, &headers, log.bound(), |
| 4178 | &load_timing_info); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4179 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4180 | Verify206Response(headers, 20, 59); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4181 | EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4182 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 4183 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4184 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 4185 | |
| 4186 | RemoveMockTransaction(&transaction); |
| 4187 | } |
| 4188 | |
rvargas | 9ba2445 | 2015-07-18 03:13:25 | [diff] [blame] | 4189 | // Tests that if the previous transaction is cancelled while busy (doing sparse |
| 4190 | // IO), a new transaction (that reuses that same ActiveEntry) waits until the |
| 4191 | // entry is ready again. |
| 4192 | TEST(HttpCache, Sparse_WaitForEntry) { |
| 4193 | MockHttpCache cache; |
| 4194 | |
| 4195 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 4196 | |
| 4197 | // Create a sparse entry. |
| 4198 | RunTransactionTest(cache.http_cache(), transaction); |
| 4199 | |
| 4200 | // Simulate a previous transaction being cancelled. |
| 4201 | disk_cache::Entry* entry; |
| 4202 | ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
| 4203 | entry->CancelSparseIO(); |
| 4204 | |
| 4205 | // Test with a range request. |
| 4206 | RunTransactionTest(cache.http_cache(), transaction); |
| 4207 | |
| 4208 | // Now test with a regular request. |
| 4209 | entry->CancelSparseIO(); |
| 4210 | transaction.request_headers = EXTRA_HEADER; |
| 4211 | transaction.data = kFullRangeData; |
| 4212 | RunTransactionTest(cache.http_cache(), transaction); |
| 4213 | |
| 4214 | entry->Close(); |
| 4215 | } |
| 4216 | |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4217 | // Tests that we don't revalidate an entry unless we are required to do so. |
| 4218 | TEST(HttpCache, RangeGET_Revalidate1) { |
| 4219 | MockHttpCache cache; |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4220 | std::string headers; |
| 4221 | |
| 4222 | // Write to the cache (40-49). |
| 4223 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4224 | transaction.response_headers = |
| 4225 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 4226 | "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n" // Should never expire. |
| 4227 | "ETag: \"foo\"\n" |
| 4228 | "Accept-Ranges: bytes\n" |
| 4229 | "Content-Length: 10\n"; |
| 4230 | AddMockTransaction(&transaction); |
| 4231 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4232 | |
| 4233 | Verify206Response(headers, 40, 49); |
| 4234 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4235 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4236 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4237 | |
| 4238 | // Read from the cache (40-49). |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4239 | BoundTestNetLog log; |
| 4240 | LoadTimingInfo load_timing_info; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4241 | RunTransactionTestWithResponseAndGetTiming( |
| 4242 | cache.http_cache(), transaction, &headers, log.bound(), |
| 4243 | &load_timing_info); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4244 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4245 | Verify206Response(headers, 40, 49); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4246 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4247 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4248 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4249 | TestLoadTimingCachedResponse(load_timing_info); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4250 | |
| 4251 | // Read again forcing the revalidation. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4252 | transaction.load_flags |= LOAD_VALIDATE_CACHE; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4253 | RunTransactionTestWithResponseAndGetTiming( |
| 4254 | cache.http_cache(), transaction, &headers, log.bound(), |
| 4255 | &load_timing_info); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4256 | |
| 4257 | Verify206Response(headers, 40, 49); |
| 4258 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4259 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4260 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4261 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4262 | |
| 4263 | RemoveMockTransaction(&transaction); |
| 4264 | } |
| 4265 | |
tfarina | e04a95b | 2015-09-18 22:48:12 | [diff] [blame] | 4266 | // Fails only on bots. crbug.com/533640 |
| 4267 | #if defined(OS_ANDROID) |
| 4268 | #define MAYBE_RangeGET_Revalidate2 DISABLED_RangeGET_Revalidate2 |
| 4269 | #else |
| 4270 | #define MAYBE_RangeGET_Revalidate2 RangeGET_Revalidate2 |
| 4271 | #endif |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4272 | // Checks that we revalidate an entry when the headers say so. |
tfarina | e04a95b | 2015-09-18 22:48:12 | [diff] [blame] | 4273 | TEST(HttpCache, MAYBE_RangeGET_Revalidate2) { |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4274 | MockHttpCache cache; |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4275 | std::string headers; |
| 4276 | |
| 4277 | // Write to the cache (40-49). |
| 4278 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4279 | transaction.response_headers = |
| 4280 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 4281 | "Expires: Sat, 18 Apr 2009 01:10:43 GMT\n" // Expired. |
| 4282 | "ETag: \"foo\"\n" |
| 4283 | "Accept-Ranges: bytes\n" |
| 4284 | "Content-Length: 10\n"; |
| 4285 | AddMockTransaction(&transaction); |
| 4286 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4287 | |
| 4288 | Verify206Response(headers, 40, 49); |
| 4289 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4290 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4291 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4292 | |
| 4293 | // Read from the cache (40-49). |
| 4294 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4295 | Verify206Response(headers, 40, 49); |
| 4296 | |
| 4297 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4298 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4299 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4300 | |
| 4301 | RemoveMockTransaction(&transaction); |
| 4302 | } |
| 4303 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4304 | // Tests that we deal with 304s for range requests. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 4305 | TEST(HttpCache, RangeGET_304) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4306 | MockHttpCache cache; |
| 4307 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4308 | std::string headers; |
| 4309 | |
| 4310 | // Write to the cache (40-49). |
| 4311 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 4312 | &headers); |
| 4313 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4314 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4315 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4316 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4317 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4318 | |
| 4319 | // Read from the cache (40-49). |
| 4320 | RangeTransactionServer handler; |
| 4321 | handler.set_not_modified(true); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4322 | MockTransaction transaction(kRangeGET_TransactionOK); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4323 | transaction.load_flags |= LOAD_VALIDATE_CACHE; |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4324 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4325 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4326 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4327 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4328 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4329 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4330 | |
| 4331 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4332 | } |
| 4333 | |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 4334 | // Tests that we deal with 206s when revalidating range requests. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 4335 | TEST(HttpCache, RangeGET_ModifiedResult) { |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 4336 | MockHttpCache cache; |
| 4337 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4338 | std::string headers; |
| 4339 | |
| 4340 | // Write to the cache (40-49). |
| 4341 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 4342 | &headers); |
| 4343 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4344 | Verify206Response(headers, 40, 49); |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 4345 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4346 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4347 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4348 | |
| 4349 | // Attempt to read from the cache (40-49). |
| 4350 | RangeTransactionServer handler; |
| 4351 | handler.set_modified(true); |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4352 | MockTransaction transaction(kRangeGET_TransactionOK); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4353 | transaction.load_flags |= LOAD_VALIDATE_CACHE; |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 4354 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 4355 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4356 | Verify206Response(headers, 40, 49); |
[email protected] | a7983789 | 2009-08-20 21:18:29 | [diff] [blame] | 4357 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4358 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4359 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4360 | |
| 4361 | // And the entry should be gone. |
| 4362 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 4363 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 4364 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4365 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4366 | |
| 4367 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4368 | } |
| 4369 | |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 4370 | // Tests that when a server returns 206 with a sub-range of the requested range, |
| 4371 | // and there is nothing stored in the cache, the returned response is passed to |
| 4372 | // the caller as is. In this context, a subrange means a response that starts |
| 4373 | // with the same byte that was requested, but that is not the whole range that |
| 4374 | // was requested. |
| 4375 | TEST(HttpCache, RangeGET_206ReturnsSubrangeRange_NoCachedContent) { |
| 4376 | MockHttpCache cache; |
| 4377 | std::string headers; |
| 4378 | |
| 4379 | // Request a large range (40-59). The server sends 40-49. |
| 4380 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 4381 | transaction.request_headers = "Range: bytes = 40-59\r\n" EXTRA_HEADER; |
| 4382 | transaction.response_headers = |
| 4383 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 4384 | "ETag: \"foo\"\n" |
| 4385 | "Accept-Ranges: bytes\n" |
| 4386 | "Content-Length: 10\n" |
| 4387 | "Content-Range: bytes 40-49/80\n"; |
| 4388 | transaction.handler = nullptr; |
| 4389 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4390 | |
| 4391 | Verify206Response(headers, 40, 49); |
| 4392 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4393 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4394 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4395 | } |
| 4396 | |
| 4397 | // Tests that when a server returns 206 with a sub-range of the requested range, |
| 4398 | // and there was an entry stored in the cache, the cache gets out of the way. |
| 4399 | TEST(HttpCache, RangeGET_206ReturnsSubrangeRange_CachedContent) { |
| 4400 | MockHttpCache cache; |
| 4401 | std::string headers; |
| 4402 | |
| 4403 | // Write to the cache (70-79). |
| 4404 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 4405 | transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER; |
| 4406 | transaction.data = "rg: 70-79 "; |
| 4407 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4408 | Verify206Response(headers, 70, 79); |
| 4409 | |
| 4410 | // Request a large range (40-79). The cache will ask the server for 40-59. |
| 4411 | // The server returns 40-49. The cache should consider the server confused and |
| 4412 | // abort caching, restarting the request without caching. |
| 4413 | transaction.request_headers = "Range: bytes = 40-79\r\n" EXTRA_HEADER; |
| 4414 | transaction.response_headers = |
| 4415 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 4416 | "ETag: \"foo\"\n" |
| 4417 | "Accept-Ranges: bytes\n" |
| 4418 | "Content-Length: 10\n" |
| 4419 | "Content-Range: bytes 40-49/80\n"; |
| 4420 | transaction.handler = nullptr; |
| 4421 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4422 | |
| 4423 | // Two new network requests were issued, one from the cache and another after |
| 4424 | // deleting the entry. |
| 4425 | Verify206Response(headers, 40, 49); |
| 4426 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 4427 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4428 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4429 | |
| 4430 | // The entry was deleted. |
| 4431 | RunTransactionTest(cache.http_cache(), transaction); |
| 4432 | EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
| 4433 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4434 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4435 | } |
| 4436 | |
| 4437 | // Tests that when a server returns 206 with a sub-range of the requested range, |
| 4438 | // and there was an entry stored in the cache, the cache gets out of the way, |
| 4439 | // when the caller is not using ranges. |
| 4440 | TEST(HttpCache, GET_206ReturnsSubrangeRange_CachedContent) { |
| 4441 | MockHttpCache cache; |
| 4442 | std::string headers; |
| 4443 | |
| 4444 | // Write to the cache (70-79). |
| 4445 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 4446 | transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER; |
| 4447 | transaction.data = "rg: 70-79 "; |
| 4448 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4449 | Verify206Response(headers, 70, 79); |
| 4450 | |
| 4451 | // Don't ask for a range. The cache will ask the server for 0-69. |
| 4452 | // The server returns 40-49. The cache should consider the server confused and |
| 4453 | // abort caching, restarting the request. |
| 4454 | // The second network request should not be a byte range request so the server |
| 4455 | // should return 200 + "Not a range" |
| 4456 | transaction.request_headers = "X-Return-Default-Range:\r\n" EXTRA_HEADER; |
| 4457 | transaction.data = "Not a range"; |
| 4458 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4459 | |
| 4460 | EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); |
| 4461 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 4462 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4463 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4464 | |
| 4465 | // The entry was deleted. |
| 4466 | RunTransactionTest(cache.http_cache(), transaction); |
| 4467 | EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
| 4468 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4469 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4470 | } |
| 4471 | |
| 4472 | // Tests that when a server returns 206 with a random range and there is |
| 4473 | // nothing stored in the cache, the returned response is passed to the caller |
| 4474 | // as is. In this context, a WrongRange means that the returned range may or may |
| 4475 | // not have any relationship with the requested range (may or may not be |
| 4476 | // contained). The important part is that the first byte doesn't match the first |
| 4477 | // requested byte. |
| 4478 | TEST(HttpCache, RangeGET_206ReturnsWrongRange_NoCachedContent) { |
| 4479 | MockHttpCache cache; |
| 4480 | std::string headers; |
| 4481 | |
| 4482 | // Request a large range (30-59). The server sends (40-49). |
| 4483 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 4484 | transaction.request_headers = "Range: bytes = 30-59\r\n" EXTRA_HEADER; |
| 4485 | transaction.response_headers = |
| 4486 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 4487 | "ETag: \"foo\"\n" |
| 4488 | "Accept-Ranges: bytes\n" |
| 4489 | "Content-Length: 10\n" |
| 4490 | "Content-Range: bytes 40-49/80\n"; |
| 4491 | transaction.handler = nullptr; |
| 4492 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4493 | |
| 4494 | Verify206Response(headers, 40, 49); |
| 4495 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4496 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4497 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4498 | |
| 4499 | // The entry was deleted. |
| 4500 | RunTransactionTest(cache.http_cache(), transaction); |
| 4501 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4502 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4503 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4504 | } |
| 4505 | |
| 4506 | // Tests that when a server returns 206 with a random range and there is |
| 4507 | // an entry stored in the cache, the cache gets out of the way. |
| 4508 | TEST(HttpCache, RangeGET_206ReturnsWrongRange_CachedContent) { |
| 4509 | MockHttpCache cache; |
| 4510 | std::string headers; |
| 4511 | |
| 4512 | // Write to the cache (70-79). |
| 4513 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 4514 | transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER; |
| 4515 | transaction.data = "rg: 70-79 "; |
| 4516 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4517 | Verify206Response(headers, 70, 79); |
| 4518 | |
| 4519 | // Request a large range (30-79). The cache will ask the server for 30-69. |
| 4520 | // The server returns 40-49. The cache should consider the server confused and |
| 4521 | // abort caching, returning the weird range to the caller. |
| 4522 | transaction.request_headers = "Range: bytes = 30-79\r\n" EXTRA_HEADER; |
| 4523 | transaction.response_headers = |
| 4524 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 4525 | "ETag: \"foo\"\n" |
| 4526 | "Accept-Ranges: bytes\n" |
| 4527 | "Content-Length: 10\n" |
| 4528 | "Content-Range: bytes 40-49/80\n"; |
| 4529 | transaction.handler = nullptr; |
| 4530 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4531 | |
| 4532 | Verify206Response(headers, 40, 49); |
| 4533 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 4534 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4535 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4536 | |
| 4537 | // The entry was deleted. |
| 4538 | RunTransactionTest(cache.http_cache(), transaction); |
| 4539 | EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
| 4540 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4541 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4542 | } |
| 4543 | |
| 4544 | // Tests that when a caller asks for a range beyond EOF, with an empty cache, |
| 4545 | // the response matches the one provided by the server. |
| 4546 | TEST(HttpCache, RangeGET_206ReturnsSmallerFile_NoCachedContent) { |
| 4547 | MockHttpCache cache; |
| 4548 | std::string headers; |
| 4549 | |
| 4550 | // Request a large range (70-99). The server sends 70-79. |
| 4551 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 4552 | transaction.request_headers = "Range: bytes = 70-99\r\n" EXTRA_HEADER; |
| 4553 | transaction.data = "rg: 70-79 "; |
| 4554 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4555 | |
| 4556 | Verify206Response(headers, 70, 79); |
| 4557 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4558 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4559 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4560 | |
| 4561 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 4562 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4563 | } |
| 4564 | |
| 4565 | // Tests that when a caller asks for a range beyond EOF, with a cached entry, |
| 4566 | // the cache automatically fixes the request. |
| 4567 | TEST(HttpCache, RangeGET_206ReturnsSmallerFile_CachedContent) { |
| 4568 | MockHttpCache cache; |
| 4569 | std::string headers; |
| 4570 | |
| 4571 | // Write to the cache (40-49). |
| 4572 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 4573 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4574 | |
| 4575 | // Request a large range (70-99). The server sends 70-79. |
| 4576 | transaction.request_headers = "Range: bytes = 70-99\r\n" EXTRA_HEADER; |
| 4577 | transaction.data = "rg: 70-79 "; |
| 4578 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4579 | |
| 4580 | Verify206Response(headers, 70, 79); |
| 4581 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4582 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4583 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4584 | |
| 4585 | // The entry was not deleted (the range was automatically fixed). |
| 4586 | RunTransactionTest(cache.http_cache(), transaction); |
| 4587 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4588 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 4589 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4590 | } |
| 4591 | |
| 4592 | // Tests that when a caller asks for a not-satisfiable range, the server's |
| 4593 | // response is forwarded to the caller. |
| 4594 | TEST(HttpCache, RangeGET_416_NoCachedContent) { |
| 4595 | MockHttpCache cache; |
| 4596 | std::string headers; |
| 4597 | |
| 4598 | // Request a range beyond EOF (80-99). |
| 4599 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 4600 | transaction.request_headers = "Range: bytes = 80-99\r\n" EXTRA_HEADER; |
| 4601 | transaction.data = ""; |
| 4602 | transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable"; |
| 4603 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4604 | |
| 4605 | EXPECT_EQ(0U, headers.find(transaction.status)); |
| 4606 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4607 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4608 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4609 | |
| 4610 | // The entry was deleted. |
| 4611 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 4612 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4613 | } |
| 4614 | |
[email protected] | 9f03cb7a | 2012-07-30 23:15:20 | [diff] [blame] | 4615 | // Tests that we cache 301s for range requests. |
| 4616 | TEST(HttpCache, RangeGET_301) { |
| 4617 | MockHttpCache cache; |
| 4618 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 4619 | transaction.status = "HTTP/1.1 301 Moved Permanently"; |
| 4620 | transaction.response_headers = "Location: http://www.bar.com/\n"; |
| 4621 | transaction.data = ""; |
| 4622 | transaction.handler = NULL; |
[email protected] | 9f03cb7a | 2012-07-30 23:15:20 | [diff] [blame] | 4623 | |
| 4624 | // Write to the cache. |
| 4625 | RunTransactionTest(cache.http_cache(), transaction); |
| 4626 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4627 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4628 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4629 | |
| 4630 | // Read from the cache. |
| 4631 | RunTransactionTest(cache.http_cache(), transaction); |
| 4632 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4633 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4634 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 9f03cb7a | 2012-07-30 23:15:20 | [diff] [blame] | 4635 | } |
| 4636 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4637 | // Tests that we can cache range requests when the start or end is unknown. |
| 4638 | // 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] | 4639 | TEST(HttpCache, UnknownRangeGET_1) { |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4640 | MockHttpCache cache; |
| 4641 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4642 | std::string headers; |
| 4643 | |
| 4644 | // Write to the cache (70-79). |
| 4645 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 4646 | transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4647 | transaction.data = "rg: 70-79 "; |
| 4648 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4649 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4650 | Verify206Response(headers, 70, 79); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4651 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4652 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4653 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4654 | |
| 4655 | // Make sure we are done with the previous transaction. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 4656 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4657 | |
| 4658 | // Write and read from the cache (60-79). |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 4659 | transaction.request_headers = "Range: bytes = 60-\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4660 | transaction.data = "rg: 60-69 rg: 70-79 "; |
| 4661 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4662 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4663 | Verify206Response(headers, 60, 79); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4664 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4665 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4666 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4667 | |
| 4668 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4669 | } |
| 4670 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4671 | // Tests that we can cache range requests when the start or end is unknown. |
| 4672 | // We start with one request from a given point, followed by a suffix request. |
| 4673 | // We'll also verify that synchronous cache responses work as intended. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 4674 | TEST(HttpCache, UnknownRangeGET_2) { |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4675 | MockHttpCache cache; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4676 | std::string headers; |
| 4677 | |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4678 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4679 | transaction.test_mode = TEST_MODE_SYNC_CACHE_START | |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 4680 | TEST_MODE_SYNC_CACHE_READ | |
| 4681 | TEST_MODE_SYNC_CACHE_WRITE; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4682 | AddMockTransaction(&transaction); |
| 4683 | |
| 4684 | // Write to the cache (70-79). |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 4685 | transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4686 | transaction.data = "rg: 70-79 "; |
| 4687 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4688 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4689 | Verify206Response(headers, 70, 79); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4690 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4691 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4692 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4693 | |
| 4694 | // Make sure we are done with the previous transaction. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 4695 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4696 | |
| 4697 | // Write and read from the cache (60-79). |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 4698 | transaction.request_headers = "Range: bytes = -20\r\n" EXTRA_HEADER; |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4699 | transaction.data = "rg: 60-69 rg: 70-79 "; |
| 4700 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4701 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4702 | Verify206Response(headers, 60, 79); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4703 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4704 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4705 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4706 | |
| 4707 | RemoveMockTransaction(&transaction); |
| 4708 | } |
| 4709 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4710 | // Tests that receiving Not Modified when asking for an open range doesn't mess |
| 4711 | // up things. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 4712 | TEST(HttpCache, UnknownRangeGET_304) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4713 | MockHttpCache cache; |
| 4714 | std::string headers; |
| 4715 | |
| 4716 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4717 | AddMockTransaction(&transaction); |
| 4718 | |
| 4719 | RangeTransactionServer handler; |
| 4720 | handler.set_not_modified(true); |
| 4721 | |
| 4722 | // Ask for the end of the file, without knowing the length. |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 4723 | transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER; |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 4724 | transaction.data = ""; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4725 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4726 | |
| 4727 | // We just bypass the cache. |
| 4728 | EXPECT_EQ(0U, headers.find("HTTP/1.1 304 Not Modified\n")); |
| 4729 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4730 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4731 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4732 | |
| 4733 | RunTransactionTest(cache.http_cache(), transaction); |
| 4734 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4735 | |
| 4736 | RemoveMockTransaction(&transaction); |
| 4737 | } |
| 4738 | |
| 4739 | // 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] | 4740 | TEST(HttpCache, GET_Previous206) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4741 | MockHttpCache cache; |
| 4742 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4743 | std::string headers; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4744 | BoundTestNetLog log; |
| 4745 | LoadTimingInfo load_timing_info; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4746 | |
| 4747 | // Write to the cache (40-49). |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4748 | RunTransactionTestWithResponseAndGetTiming( |
| 4749 | cache.http_cache(), kRangeGET_TransactionOK, &headers, log.bound(), |
| 4750 | &load_timing_info); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4751 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4752 | Verify206Response(headers, 40, 49); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4753 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4754 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4755 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4756 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4757 | |
| 4758 | // Write and read from the cache (0-79), when not asked for a range. |
| 4759 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 4760 | transaction.request_headers = EXTRA_HEADER; |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 4761 | transaction.data = kFullRangeData; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4762 | RunTransactionTestWithResponseAndGetTiming( |
| 4763 | cache.http_cache(), transaction, &headers, log.bound(), |
| 4764 | &load_timing_info); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4765 | |
| 4766 | EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4767 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 4768 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4769 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4770 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 4771 | |
| 4772 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4773 | } |
| 4774 | |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 4775 | // 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] | 4776 | // part of the object and the server replies with 304 (Not Modified). |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 4777 | TEST(HttpCache, GET_Previous206_NotModified) { |
| 4778 | MockHttpCache cache; |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 4779 | |
| 4780 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 4781 | AddMockTransaction(&transaction); |
| 4782 | std::string headers; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4783 | BoundTestNetLog log; |
| 4784 | LoadTimingInfo load_timing_info; |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 4785 | |
| 4786 | // Write to the cache (0-9). |
[email protected] | 06c351a | 2010-12-03 19:11:29 | [diff] [blame] | 4787 | transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; |
| 4788 | transaction.data = "rg: 00-09 "; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4789 | RunTransactionTestWithResponseAndGetTiming( |
| 4790 | cache.http_cache(), transaction, &headers, log.bound(), |
| 4791 | &load_timing_info); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4792 | Verify206Response(headers, 0, 9); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4793 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 06c351a | 2010-12-03 19:11:29 | [diff] [blame] | 4794 | |
| 4795 | // Write to the cache (70-79). |
| 4796 | transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER; |
| 4797 | transaction.data = "rg: 70-79 "; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4798 | RunTransactionTestWithResponseAndGetTiming( |
| 4799 | cache.http_cache(), transaction, &headers, log.bound(), |
| 4800 | &load_timing_info); |
[email protected] | 06c351a | 2010-12-03 19:11:29 | [diff] [blame] | 4801 | Verify206Response(headers, 70, 79); |
| 4802 | |
| 4803 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 4804 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 4805 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4806 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 4807 | |
[email protected] | 06c351a | 2010-12-03 19:11:29 | [diff] [blame] | 4808 | // Read from the cache (0-9), write and read from cache (10 - 79). |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4809 | transaction.load_flags |= LOAD_VALIDATE_CACHE; |
[email protected] | 06c351a | 2010-12-03 19:11:29 | [diff] [blame] | 4810 | transaction.request_headers = "Foo: bar\r\n" EXTRA_HEADER; |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 4811 | transaction.data = kFullRangeData; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4812 | RunTransactionTestWithResponseAndGetTiming( |
| 4813 | cache.http_cache(), transaction, &headers, log.bound(), |
| 4814 | &load_timing_info); |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 4815 | |
| 4816 | EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); |
[email protected] | 06c351a | 2010-12-03 19:11:29 | [diff] [blame] | 4817 | EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
| 4818 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 4819 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4820 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 4821 | |
| 4822 | RemoveMockTransaction(&transaction); |
| 4823 | } |
| 4824 | |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 4825 | // Tests that we can handle a regular request to a sparse entry, that results in |
| 4826 | // new content provided by the server (206). |
| 4827 | TEST(HttpCache, GET_Previous206_NewContent) { |
| 4828 | MockHttpCache cache; |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 4829 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4830 | std::string headers; |
| 4831 | |
| 4832 | // Write to the cache (0-9). |
| 4833 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 4834 | transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; |
| 4835 | transaction.data = "rg: 00-09 "; |
| 4836 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 4837 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4838 | Verify206Response(headers, 0, 9); |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 4839 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4840 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 4841 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4842 | |
| 4843 | // Now we'll issue a request without any range that should result first in a |
| 4844 | // 206 (when revalidating), and then in a weird standard answer: the test |
| 4845 | // server will not modify the response so we'll get the default range... a |
| 4846 | // real server will answer with 200. |
| 4847 | MockTransaction transaction2(kRangeGET_TransactionOK); |
| 4848 | transaction2.request_headers = EXTRA_HEADER; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4849 | transaction2.load_flags |= LOAD_VALIDATE_CACHE; |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 4850 | transaction2.data = "Not a range"; |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 4851 | RangeTransactionServer handler; |
| 4852 | handler.set_modified(true); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4853 | BoundTestNetLog log; |
| 4854 | LoadTimingInfo load_timing_info; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4855 | RunTransactionTestWithResponseAndGetTiming( |
| 4856 | cache.http_cache(), transaction2, &headers, log.bound(), |
| 4857 | &load_timing_info); |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 4858 | |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 4859 | EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 4860 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 4861 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4862 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4863 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | a189bce | 2009-12-01 01:59:12 | [diff] [blame] | 4864 | |
| 4865 | // Verify that the previous request deleted the entry. |
| 4866 | RunTransactionTest(cache.http_cache(), transaction); |
| 4867 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4868 | |
| 4869 | RemoveMockTransaction(&transaction); |
| 4870 | } |
| 4871 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4872 | // Tests that we can handle cached 206 responses that are not sparse. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 4873 | TEST(HttpCache, GET_Previous206_NotSparse) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4874 | MockHttpCache cache; |
| 4875 | |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4876 | // Create a disk cache entry that stores 206 headers while not being sparse. |
| 4877 | disk_cache::Entry* entry; |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 4878 | ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry, |
| 4879 | NULL)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4880 | |
| 4881 | std::string raw_headers(kRangeGET_TransactionOK.status); |
| 4882 | raw_headers.append("\n"); |
| 4883 | raw_headers.append(kRangeGET_TransactionOK.response_headers); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4884 | raw_headers = |
| 4885 | HttpUtil::AssembleRawHeaders(raw_headers.data(), raw_headers.size()); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4886 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4887 | HttpResponseInfo response; |
| 4888 | response.headers = new HttpResponseHeaders(raw_headers); |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4889 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4890 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4891 | scoped_refptr<IOBuffer> buf(new IOBuffer(500)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4892 | int len = static_cast<int>(base::strlcpy(buf->data(), |
| 4893 | kRangeGET_TransactionOK.data, 500)); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4894 | TestCompletionCallback cb; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 4895 | int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true); |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4896 | EXPECT_EQ(len, cb.GetResult(rv)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4897 | entry->Close(); |
| 4898 | |
| 4899 | // Now see that we don't use the stored entry. |
| 4900 | std::string headers; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4901 | BoundTestNetLog log; |
| 4902 | LoadTimingInfo load_timing_info; |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4903 | RunTransactionTestWithResponseAndGetTiming( |
| 4904 | cache.http_cache(), kSimpleGET_Transaction, &headers, log.bound(), |
| 4905 | &load_timing_info); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4906 | |
| 4907 | // We are expecting a 200. |
| 4908 | std::string expected_headers(kSimpleGET_Transaction.status); |
| 4909 | expected_headers.append("\n"); |
| 4910 | expected_headers.append(kSimpleGET_Transaction.response_headers); |
| 4911 | EXPECT_EQ(expected_headers, headers); |
| 4912 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4913 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4914 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 4915 | TestLoadTimingNetworkRequest(load_timing_info); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4916 | } |
| 4917 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 4918 | // Tests that we can handle cached 206 responses that are not sparse. This time |
| 4919 | // we issue a range request and expect to receive a range. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 4920 | TEST(HttpCache, RangeGET_Previous206_NotSparse_2) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4921 | MockHttpCache cache; |
| 4922 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 4923 | |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4924 | // Create a disk cache entry that stores 206 headers while not being sparse. |
| 4925 | disk_cache::Entry* entry; |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 4926 | ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry, |
| 4927 | NULL)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4928 | |
| 4929 | std::string raw_headers(kRangeGET_TransactionOK.status); |
| 4930 | raw_headers.append("\n"); |
| 4931 | raw_headers.append(kRangeGET_TransactionOK.response_headers); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4932 | raw_headers = |
| 4933 | HttpUtil::AssembleRawHeaders(raw_headers.data(), raw_headers.size()); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4934 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4935 | HttpResponseInfo response; |
| 4936 | response.headers = new HttpResponseHeaders(raw_headers); |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4937 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4938 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4939 | scoped_refptr<IOBuffer> buf(new IOBuffer(500)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4940 | int len = static_cast<int>(base::strlcpy(buf->data(), |
| 4941 | kRangeGET_TransactionOK.data, 500)); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4942 | TestCompletionCallback cb; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 4943 | int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true); |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 4944 | EXPECT_EQ(len, cb.GetResult(rv)); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4945 | entry->Close(); |
| 4946 | |
| 4947 | // Now see that we don't use the stored entry. |
| 4948 | std::string headers; |
| 4949 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 4950 | &headers); |
| 4951 | |
| 4952 | // We are expecting a 206. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4953 | Verify206Response(headers, 40, 49); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 4954 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 4955 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 4956 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 4957 | |
| 4958 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 4959 | } |
| 4960 | |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 4961 | // Tests that we can handle cached 206 responses that can't be validated. |
| 4962 | TEST(HttpCache, GET_Previous206_NotValidation) { |
| 4963 | MockHttpCache cache; |
| 4964 | |
| 4965 | // Create a disk cache entry that stores 206 headers. |
| 4966 | disk_cache::Entry* entry; |
| 4967 | ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry, |
| 4968 | NULL)); |
| 4969 | |
| 4970 | // Make sure that the headers cannot be validated with the server. |
| 4971 | std::string raw_headers(kRangeGET_TransactionOK.status); |
| 4972 | raw_headers.append("\n"); |
| 4973 | raw_headers.append("Content-Length: 80\n"); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4974 | raw_headers = |
| 4975 | HttpUtil::AssembleRawHeaders(raw_headers.data(), raw_headers.size()); |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 4976 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4977 | HttpResponseInfo response; |
| 4978 | response.headers = new HttpResponseHeaders(raw_headers); |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 4979 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); |
| 4980 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4981 | scoped_refptr<IOBuffer> buf(new IOBuffer(500)); |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 4982 | int len = static_cast<int>(base::strlcpy(buf->data(), |
| 4983 | kRangeGET_TransactionOK.data, 500)); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 4984 | TestCompletionCallback cb; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 4985 | int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true); |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 4986 | EXPECT_EQ(len, cb.GetResult(rv)); |
| 4987 | entry->Close(); |
| 4988 | |
| 4989 | // Now see that we don't use the stored entry. |
| 4990 | std::string headers; |
| 4991 | RunTransactionTestWithResponse(cache.http_cache(), kSimpleGET_Transaction, |
| 4992 | &headers); |
| 4993 | |
| 4994 | // We are expecting a 200. |
| 4995 | std::string expected_headers(kSimpleGET_Transaction.status); |
| 4996 | expected_headers.append("\n"); |
| 4997 | expected_headers.append(kSimpleGET_Transaction.response_headers); |
| 4998 | EXPECT_EQ(expected_headers, headers); |
| 4999 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5000 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5001 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5002 | } |
| 5003 | |
tfarina | e04a95b | 2015-09-18 22:48:12 | [diff] [blame] | 5004 | // Fails only on bots. crbug.com/533640 |
| 5005 | #if defined(OS_ANDROID) |
| 5006 | #define MAYBE_RangeGET_Previous200 DISABLED_RangeGET_Previous200 |
| 5007 | #else |
| 5008 | #define MAYBE_RangeGET_Previous200 RangeGET_Previous200 |
| 5009 | #endif |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5010 | // Tests that we can handle range requests with cached 200 responses. |
tfarina | e04a95b | 2015-09-18 22:48:12 | [diff] [blame] | 5011 | TEST(HttpCache, MAYBE_RangeGET_Previous200) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5012 | MockHttpCache cache; |
| 5013 | |
| 5014 | // Store the whole thing with status 200. |
| 5015 | MockTransaction transaction(kTypicalGET_Transaction); |
| 5016 | transaction.url = kRangeGET_TransactionOK.url; |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 5017 | transaction.data = kFullRangeData; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5018 | AddMockTransaction(&transaction); |
| 5019 | RunTransactionTest(cache.http_cache(), transaction); |
| 5020 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5021 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5022 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5023 | |
| 5024 | RemoveMockTransaction(&transaction); |
| 5025 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5026 | |
| 5027 | // Now see that we use the stored entry. |
| 5028 | std::string headers; |
| 5029 | MockTransaction transaction2(kRangeGET_TransactionOK); |
| 5030 | RangeTransactionServer handler; |
| 5031 | handler.set_not_modified(true); |
| 5032 | RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
| 5033 | |
| 5034 | // We are expecting a 206. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 5035 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5036 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5037 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5038 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5039 | |
[email protected] | 8f28d63 | 2009-10-01 22:09:21 | [diff] [blame] | 5040 | // The last transaction has finished so make sure the entry is deactivated. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 5041 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 8f28d63 | 2009-10-01 22:09:21 | [diff] [blame] | 5042 | |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 5043 | // Make a request for an invalid range. |
| 5044 | MockTransaction transaction3(kRangeGET_TransactionOK); |
| 5045 | transaction3.request_headers = "Range: bytes = 80-90\r\n" EXTRA_HEADER; |
[email protected] | 9f03cb7a | 2012-07-30 23:15:20 | [diff] [blame] | 5046 | transaction3.data = transaction.data; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5047 | transaction3.load_flags = LOAD_PREFERRING_CACHE; |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 5048 | RunTransactionTestWithResponse(cache.http_cache(), transaction3, &headers); |
| 5049 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
[email protected] | 9f03cb7a | 2012-07-30 23:15:20 | [diff] [blame] | 5050 | EXPECT_EQ(0U, headers.find("HTTP/1.1 200 ")); |
| 5051 | EXPECT_EQ(std::string::npos, headers.find("Content-Range:")); |
| 5052 | EXPECT_EQ(std::string::npos, headers.find("Content-Length: 80")); |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 5053 | |
| 5054 | // Make sure the entry is deactivated. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 5055 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 5056 | |
| 5057 | // Even though the request was invalid, we should have the entry. |
| 5058 | RunTransactionTest(cache.http_cache(), transaction2); |
| 5059 | EXPECT_EQ(3, cache.disk_cache()->open_count()); |
| 5060 | |
| 5061 | // Make sure the entry is deactivated. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 5062 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 5063 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5064 | // Now we should receive a range from the server and drop the stored entry. |
| 5065 | handler.set_not_modified(false); |
| 5066 | transaction2.request_headers = kRangeGET_TransactionOK.request_headers; |
| 5067 | RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 5068 | Verify206Response(headers, 40, 49); |
[email protected] | 9f03cb7a | 2012-07-30 23:15:20 | [diff] [blame] | 5069 | EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 5070 | EXPECT_EQ(4, cache.disk_cache()->open_count()); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5071 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5072 | |
| 5073 | RunTransactionTest(cache.http_cache(), transaction2); |
| 5074 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5075 | |
| 5076 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5077 | } |
| 5078 | |
| 5079 | // Tests that we can handle a 200 response when dealing with sparse entries. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 5080 | TEST(HttpCache, RangeRequestResultsIn200) { |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 5081 | MockHttpCache cache; |
| 5082 | AddMockTransaction(&kRangeGET_TransactionOK); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 5083 | std::string headers; |
| 5084 | |
| 5085 | // Write to the cache (70-79). |
| 5086 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 5087 | transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 5088 | transaction.data = "rg: 70-79 "; |
| 5089 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 5090 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 5091 | Verify206Response(headers, 70, 79); |
[email protected] | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 5092 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5093 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5094 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5095 | |
| 5096 | // Now we'll issue a request that results in a plain 200 response, but to |
| 5097 | // the to the same URL that we used to store sparse data, and making sure |
| 5098 | // that we ask for a range. |
| 5099 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5100 | MockTransaction transaction2(kSimpleGET_Transaction); |
| 5101 | transaction2.url = kRangeGET_TransactionOK.url; |
| 5102 | transaction2.request_headers = kRangeGET_TransactionOK.request_headers; |
| 5103 | AddMockTransaction(&transaction2); |
| 5104 | |
| 5105 | RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
| 5106 | |
| 5107 | std::string expected_headers(kSimpleGET_Transaction.status); |
| 5108 | expected_headers.append("\n"); |
| 5109 | expected_headers.append(kSimpleGET_Transaction.response_headers); |
| 5110 | EXPECT_EQ(expected_headers, headers); |
| 5111 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5112 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5113 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5114 | |
| 5115 | RemoveMockTransaction(&transaction2); |
| 5116 | } |
| 5117 | |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5118 | // Tests that a range request that falls outside of the size that we know about |
| 5119 | // only deletes the entry if the resource has indeed changed. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 5120 | TEST(HttpCache, RangeGET_MoreThanCurrentSize) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5121 | MockHttpCache cache; |
| 5122 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5123 | std::string headers; |
| 5124 | |
| 5125 | // Write to the cache (40-49). |
| 5126 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 5127 | &headers); |
| 5128 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 5129 | Verify206Response(headers, 40, 49); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5130 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5131 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5132 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5133 | |
| 5134 | // A weird request should not delete this entry. Ask for bytes 120-. |
| 5135 | MockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 5136 | transaction.request_headers = "Range: bytes = 120-\r\n" EXTRA_HEADER; |
[email protected] | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 5137 | transaction.data = ""; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5138 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 5139 | |
| 5140 | EXPECT_EQ(0U, headers.find("HTTP/1.1 416 ")); |
| 5141 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5142 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5143 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5144 | |
| 5145 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 5146 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 5147 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5148 | |
| 5149 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5150 | } |
| 5151 | |
tfarina | e04a95b | 2015-09-18 22:48:12 | [diff] [blame] | 5152 | // Fails only on bots. crbug.com/533640 |
| 5153 | #if defined(OS_ANDROID) |
| 5154 | #define MAYBE_RangeGET_Cancel DISABLED_RangeGET_Cancel |
| 5155 | #else |
| 5156 | #define MAYBE_RangeGET_Cancel RangeGET_Cancel |
| 5157 | #endif |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 5158 | // Tests that we don't delete a sparse entry when we cancel a request. |
tfarina | e04a95b | 2015-09-18 22:48:12 | [diff] [blame] | 5159 | TEST(HttpCache, MAYBE_RangeGET_Cancel) { |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 5160 | MockHttpCache cache; |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 5161 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5162 | |
| 5163 | MockHttpRequest request(kRangeGET_TransactionOK); |
| 5164 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 5165 | Context* c = new Context(); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 5166 | int rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5167 | ASSERT_EQ(OK, rv); |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 5168 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5169 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 5170 | if (rv == ERR_IO_PENDING) |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 5171 | rv = c->callback.WaitForResult(); |
| 5172 | |
| 5173 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5174 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5175 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5176 | |
| 5177 | // Make sure that the entry has some data stored. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5178 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 5179 | rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5180 | if (rv == ERR_IO_PENDING) |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 5181 | rv = c->callback.WaitForResult(); |
| 5182 | EXPECT_EQ(buf->size(), rv); |
| 5183 | |
| 5184 | // Destroy the transaction. |
| 5185 | delete c; |
| 5186 | |
| 5187 | // Verify that the entry has not been deleted. |
| 5188 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 5189 | ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 2c852853 | 2009-09-09 16:55:22 | [diff] [blame] | 5190 | entry->Close(); |
| 5191 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5192 | } |
| 5193 | |
tfarina | e04a95b | 2015-09-18 22:48:12 | [diff] [blame] | 5194 | // Fails only on bots. crbug.com/533640 |
| 5195 | #if defined(OS_ANDROID) |
| 5196 | #define MAYBE_RangeGET_Cancel2 DISABLED_RangeGET_Cancel2 |
| 5197 | #else |
| 5198 | #define MAYBE_RangeGET_Cancel2 RangeGET_Cancel2 |
| 5199 | #endif |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 5200 | // Tests that we don't delete a sparse entry when we start a new request after |
| 5201 | // cancelling the previous one. |
tfarina | e04a95b | 2015-09-18 22:48:12 | [diff] [blame] | 5202 | TEST(HttpCache, MAYBE_RangeGET_Cancel2) { |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 5203 | MockHttpCache cache; |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 5204 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5205 | |
| 5206 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 5207 | MockHttpRequest request(kRangeGET_TransactionOK); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5208 | request.load_flags |= LOAD_VALIDATE_CACHE; |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 5209 | |
| 5210 | Context* c = new Context(); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 5211 | int rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5212 | ASSERT_EQ(OK, rv); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 5213 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5214 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 5215 | if (rv == ERR_IO_PENDING) |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 5216 | rv = c->callback.WaitForResult(); |
| 5217 | |
| 5218 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5219 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5220 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5221 | |
| 5222 | // Make sure that we revalidate the entry and read from the cache (a single |
| 5223 | // read will return while waiting for the network). |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5224 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 5225 | rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 5226 | EXPECT_EQ(5, c->callback.GetResult(rv)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 5227 | rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5228 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 5229 | |
| 5230 | // Destroy the transaction before completing the read. |
| 5231 | delete c; |
| 5232 | |
| 5233 | // We have the read and the delete (OnProcessPendingQueue) waiting on the |
| 5234 | // message loop. This means that a new transaction will just reuse the same |
| 5235 | // active entry (no open or create). |
| 5236 | |
| 5237 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 5238 | |
[email protected] | 5beca81 | 2010-06-24 17:55:24 | [diff] [blame] | 5239 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | 06e62ba | 2009-10-08 23:07:39 | [diff] [blame] | 5240 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5241 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5242 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5243 | } |
| 5244 | |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 5245 | // A slight variation of the previous test, this time we cancel two requests in |
| 5246 | // a row, making sure that the second is waiting for the entry to be ready. |
| 5247 | TEST(HttpCache, RangeGET_Cancel3) { |
| 5248 | MockHttpCache cache; |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 5249 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5250 | |
| 5251 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 5252 | MockHttpRequest request(kRangeGET_TransactionOK); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5253 | request.load_flags |= LOAD_VALIDATE_CACHE; |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 5254 | |
| 5255 | Context* c = new Context(); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 5256 | int rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5257 | ASSERT_EQ(OK, rv); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 5258 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5259 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 5260 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 5261 | rv = c->callback.WaitForResult(); |
| 5262 | |
| 5263 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5264 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5265 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5266 | |
| 5267 | // Make sure that we revalidate the entry and read from the cache (a single |
| 5268 | // read will return while waiting for the network). |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5269 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 5270 | rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 5271 | EXPECT_EQ(5, c->callback.GetResult(rv)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 5272 | rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5273 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 5274 | |
| 5275 | // Destroy the transaction before completing the read. |
| 5276 | delete c; |
| 5277 | |
| 5278 | // We have the read and the delete (OnProcessPendingQueue) waiting on the |
| 5279 | // message loop. This means that a new transaction will just reuse the same |
| 5280 | // active entry (no open or create). |
| 5281 | |
| 5282 | c = new Context(); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 5283 | rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5284 | ASSERT_EQ(OK, rv); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 5285 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5286 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 5287 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 5288 | |
| 5289 | MockDiskEntry::IgnoreCallbacks(true); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 5290 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 5291 | MockDiskEntry::IgnoreCallbacks(false); |
| 5292 | |
| 5293 | // The new transaction is waiting for the query range callback. |
| 5294 | delete c; |
| 5295 | |
| 5296 | // And we should not crash when the callback is delivered. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 5297 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 24f4639 | 2009-11-19 18:45:23 | [diff] [blame] | 5298 | |
| 5299 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5300 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5301 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5302 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5303 | } |
| 5304 | |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5305 | // Tests that an invalid range response results in no cached entry. |
| 5306 | TEST(HttpCache, RangeGET_InvalidResponse1) { |
| 5307 | MockHttpCache cache; |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5308 | std::string headers; |
| 5309 | |
| 5310 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 5311 | transaction.handler = NULL; |
| 5312 | transaction.response_headers = "Content-Range: bytes 40-49/45\n" |
| 5313 | "Content-Length: 10\n"; |
| 5314 | AddMockTransaction(&transaction); |
| 5315 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 5316 | |
| 5317 | std::string expected(transaction.status); |
| 5318 | expected.append("\n"); |
| 5319 | expected.append(transaction.response_headers); |
| 5320 | EXPECT_EQ(expected, headers); |
| 5321 | |
| 5322 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5323 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5324 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5325 | |
| 5326 | // Verify that we don't have a cached entry. |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 5327 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 5328 | EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5329 | |
| 5330 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5331 | } |
| 5332 | |
| 5333 | // Tests that we reject a range that doesn't match the content-length. |
| 5334 | TEST(HttpCache, RangeGET_InvalidResponse2) { |
| 5335 | MockHttpCache cache; |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5336 | std::string headers; |
| 5337 | |
| 5338 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 5339 | transaction.handler = NULL; |
| 5340 | transaction.response_headers = "Content-Range: bytes 40-49/80\n" |
| 5341 | "Content-Length: 20\n"; |
| 5342 | AddMockTransaction(&transaction); |
| 5343 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 5344 | |
| 5345 | std::string expected(transaction.status); |
| 5346 | expected.append("\n"); |
| 5347 | expected.append(transaction.response_headers); |
| 5348 | EXPECT_EQ(expected, headers); |
| 5349 | |
| 5350 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5351 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5352 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5353 | |
| 5354 | // Verify that we don't have a cached entry. |
[email protected] | 7d7ad6e4 | 2010-01-14 01:30:53 | [diff] [blame] | 5355 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 5356 | EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5357 | |
| 5358 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5359 | } |
| 5360 | |
| 5361 | // Tests that if a server tells us conflicting information about a resource we |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 5362 | // drop the entry. |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5363 | TEST(HttpCache, RangeGET_InvalidResponse3) { |
| 5364 | MockHttpCache cache; |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5365 | std::string headers; |
| 5366 | |
| 5367 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 5368 | transaction.handler = NULL; |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 5369 | transaction.request_headers = "Range: bytes = 50-59\r\n" EXTRA_HEADER; |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5370 | std::string response_headers(transaction.response_headers); |
| 5371 | response_headers.append("Content-Range: bytes 50-59/160\n"); |
| 5372 | transaction.response_headers = response_headers.c_str(); |
| 5373 | AddMockTransaction(&transaction); |
| 5374 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 5375 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 5376 | Verify206Response(headers, 50, 59); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5377 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5378 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5379 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5380 | |
| 5381 | RemoveMockTransaction(&transaction); |
| 5382 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5383 | |
| 5384 | // This transaction will report a resource size of 80 bytes, and we think it's |
| 5385 | // 160 so we should ignore the response. |
| 5386 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 5387 | &headers); |
| 5388 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 5389 | Verify206Response(headers, 40, 49); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5390 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5391 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5392 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5393 | |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 5394 | // Verify that the entry is gone. |
| 5395 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 5396 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5397 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5398 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5399 | } |
| 5400 | |
| 5401 | // Tests that we handle large range values properly. |
| 5402 | TEST(HttpCache, RangeGET_LargeValues) { |
| 5403 | // We need a real sparse cache for this test. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5404 | MockHttpCache cache(HttpCache::DefaultBackend::InMemory(1024 * 1024)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5405 | std::string headers; |
| 5406 | |
| 5407 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 5408 | transaction.handler = NULL; |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 5409 | transaction.request_headers = "Range: bytes = 4294967288-4294967297\r\n" |
| 5410 | EXTRA_HEADER; |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5411 | transaction.response_headers = |
[email protected] | 8a30114 | 2011-04-13 18:33:40 | [diff] [blame] | 5412 | "ETag: \"foo\"\n" |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5413 | "Content-Range: bytes 4294967288-4294967297/4294967299\n" |
| 5414 | "Content-Length: 10\n"; |
| 5415 | AddMockTransaction(&transaction); |
| 5416 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 5417 | |
| 5418 | std::string expected(transaction.status); |
| 5419 | expected.append("\n"); |
| 5420 | expected.append(transaction.response_headers); |
| 5421 | EXPECT_EQ(expected, headers); |
| 5422 | |
| 5423 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5424 | |
| 5425 | // Verify that we have a cached entry. |
| 5426 | disk_cache::Entry* en; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 5427 | ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &en)); |
[email protected] | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 5428 | en->Close(); |
| 5429 | |
| 5430 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5431 | } |
| 5432 | |
[email protected] | 93e7844 | 2009-10-27 04:46:32 | [diff] [blame] | 5433 | // Tests that we don't crash with a range request if the disk cache was not |
| 5434 | // initialized properly. |
| 5435 | TEST(HttpCache, RangeGET_NoDiskCache) { |
mmenke | bc31a2c | 2015-10-29 13:44:45 | [diff] [blame] | 5436 | scoped_ptr<MockBlockingBackendFactory> factory( |
| 5437 | new MockBlockingBackendFactory()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 5438 | factory->set_fail(true); |
| 5439 | factory->FinishCreation(); // We'll complete synchronously. |
mmenke | bc31a2c | 2015-10-29 13:44:45 | [diff] [blame] | 5440 | MockHttpCache cache(factory.Pass()); |
[email protected] | f870252 | 2010-05-12 18:40:10 | [diff] [blame] | 5441 | |
[email protected] | 93e7844 | 2009-10-27 04:46:32 | [diff] [blame] | 5442 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5443 | |
| 5444 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 5445 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5446 | |
| 5447 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5448 | } |
| 5449 | |
[email protected] | 793618a | 2009-11-03 23:08:12 | [diff] [blame] | 5450 | // Tests that we handle byte range requests that skip the cache. |
| 5451 | TEST(HttpCache, RangeHEAD) { |
| 5452 | MockHttpCache cache; |
[email protected] | 793618a | 2009-11-03 23:08:12 | [diff] [blame] | 5453 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5454 | |
| 5455 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 5456 | transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; |
| 5457 | transaction.method = "HEAD"; |
| 5458 | transaction.data = "rg: 70-79 "; |
| 5459 | |
| 5460 | std::string headers; |
| 5461 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 5462 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 5463 | Verify206Response(headers, 70, 79); |
[email protected] | 793618a | 2009-11-03 23:08:12 | [diff] [blame] | 5464 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5465 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5466 | EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 5467 | |
| 5468 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5469 | } |
| 5470 | |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 5471 | // Tests that we don't crash when after reading from the cache we issue a |
| 5472 | // request for the next range and the server gives us a 200 synchronously. |
| 5473 | TEST(HttpCache, RangeGET_FastFlakyServer) { |
| 5474 | MockHttpCache cache; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 5475 | |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 5476 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 5477 | transaction.request_headers = "Range: bytes = 40-\r\n" EXTRA_HEADER; |
| 5478 | transaction.test_mode = TEST_MODE_SYNC_NET_START; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5479 | transaction.load_flags |= LOAD_VALIDATE_CACHE; |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 5480 | |
| 5481 | // Write to the cache. |
| 5482 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 5483 | |
| 5484 | // And now read from the cache and the network. |
| 5485 | RangeTransactionServer handler; |
| 5486 | handler.set_bad_200(true); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5487 | transaction.data = "Not a range"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5488 | BoundTestNetLog log; |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 5489 | RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 5490 | |
| 5491 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 5492 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5493 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
rvargas | 80059b3 | 2015-01-02 23:39:52 | [diff] [blame] | 5494 | EXPECT_TRUE(LogContainsEventType( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5495 | log, NetLog::TYPE_HTTP_CACHE_RE_SEND_PARTIAL_REQUEST)); |
[email protected] | fa59e6a | 2009-12-02 18:07:46 | [diff] [blame] | 5496 | } |
| 5497 | |
[email protected] | c14117b9 | 2010-01-21 19:22:57 | [diff] [blame] | 5498 | // Tests that when the server gives us less data than expected, we don't keep |
| 5499 | // asking for more data. |
| 5500 | TEST(HttpCache, RangeGET_FastFlakyServer2) { |
| 5501 | MockHttpCache cache; |
[email protected] | c14117b9 | 2010-01-21 19:22:57 | [diff] [blame] | 5502 | |
| 5503 | // First, check with an empty cache (WRITE mode). |
| 5504 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 5505 | transaction.request_headers = "Range: bytes = 40-49\r\n" EXTRA_HEADER; |
| 5506 | transaction.data = "rg: 40-"; // Less than expected. |
| 5507 | transaction.handler = NULL; |
| 5508 | std::string headers(transaction.response_headers); |
| 5509 | headers.append("Content-Range: bytes 40-49/80\n"); |
| 5510 | transaction.response_headers = headers.c_str(); |
| 5511 | |
| 5512 | AddMockTransaction(&transaction); |
| 5513 | |
| 5514 | // Write to the cache. |
| 5515 | RunTransactionTest(cache.http_cache(), transaction); |
| 5516 | |
| 5517 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5518 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5519 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5520 | |
| 5521 | // Now verify that even in READ_WRITE mode, we forward the bad response to |
| 5522 | // the caller. |
| 5523 | transaction.request_headers = "Range: bytes = 60-69\r\n" EXTRA_HEADER; |
| 5524 | transaction.data = "rg: 60-"; // Less than expected. |
| 5525 | headers = kRangeGET_TransactionOK.response_headers; |
| 5526 | headers.append("Content-Range: bytes 60-69/80\n"); |
| 5527 | transaction.response_headers = headers.c_str(); |
| 5528 | |
| 5529 | RunTransactionTest(cache.http_cache(), transaction); |
| 5530 | |
| 5531 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5532 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5533 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5534 | |
| 5535 | RemoveMockTransaction(&transaction); |
| 5536 | } |
| 5537 | |
[email protected] | 20960e07 | 2011-09-20 20:59:01 | [diff] [blame] | 5538 | #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5539 | // This test hits a NOTREACHED so it is a release mode only test. |
[email protected] | 21f659d | 2009-08-24 17:59:31 | [diff] [blame] | 5540 | TEST(HttpCache, RangeGET_OK_LoadOnlyFromCache) { |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5541 | MockHttpCache cache; |
| 5542 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5543 | |
| 5544 | // Write to the cache (40-49). |
| 5545 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 5546 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5547 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5548 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5549 | |
| 5550 | // Force this transaction to read from the cache. |
| 5551 | MockTransaction transaction(kRangeGET_TransactionOK); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5552 | transaction.load_flags |= LOAD_ONLY_FROM_CACHE; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5553 | |
| 5554 | MockHttpRequest request(transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5555 | TestCompletionCallback callback; |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5556 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5557 | scoped_ptr<HttpTransaction> trans; |
| 5558 | int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); |
| 5559 | EXPECT_EQ(OK, rv); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5560 | ASSERT_TRUE(trans.get()); |
| 5561 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5562 | rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 5563 | if (rv == ERR_IO_PENDING) |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5564 | rv = callback.WaitForResult(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5565 | ASSERT_EQ(ERR_CACHE_MISS, rv); |
[email protected] | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 5566 | |
| 5567 | trans.reset(); |
| 5568 | |
| 5569 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5570 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5571 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5572 | |
| 5573 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5574 | } |
| 5575 | #endif |
| 5576 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5577 | // Tests the handling of the "truncation" flag. |
| 5578 | TEST(HttpCache, WriteResponseInfo_Truncated) { |
| 5579 | MockHttpCache cache; |
| 5580 | disk_cache::Entry* entry; |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 5581 | ASSERT_TRUE(cache.CreateBackendEntry("http://www.google.com", &entry, |
| 5582 | NULL)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5583 | |
| 5584 | std::string headers("HTTP/1.1 200 OK"); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5585 | headers = HttpUtil::AssembleRawHeaders(headers.data(), headers.size()); |
| 5586 | HttpResponseInfo response; |
| 5587 | response.headers = new HttpResponseHeaders(headers); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5588 | |
| 5589 | // Set the last argument for this to be an incomplete request. |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 5590 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5591 | bool truncated = false; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 5592 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5593 | EXPECT_TRUE(truncated); |
| 5594 | |
| 5595 | // And now test the opposite case. |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 5596 | EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5597 | truncated = true; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 5598 | EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5599 | EXPECT_FALSE(truncated); |
| 5600 | entry->Close(); |
| 5601 | } |
| 5602 | |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 5603 | // Tests basic pickling/unpickling of HttpResponseInfo. |
| 5604 | TEST(HttpCache, PersistHttpResponseInfo) { |
| 5605 | // Set some fields (add more if needed.) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5606 | HttpResponseInfo response1; |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 5607 | response1.was_cached = false; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5608 | response1.socket_address = HostPortPair("1.2.3.4", 80); |
| 5609 | response1.headers = new HttpResponseHeaders("HTTP/1.1 200 OK"); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 5610 | |
| 5611 | // Pickle. |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 5612 | base::Pickle pickle; |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 5613 | response1.Persist(&pickle, false, false); |
| 5614 | |
| 5615 | // Unpickle. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5616 | HttpResponseInfo response2; |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 5617 | bool response_truncated; |
| 5618 | EXPECT_TRUE(response2.InitFromPickle(pickle, &response_truncated)); |
| 5619 | EXPECT_FALSE(response_truncated); |
| 5620 | |
| 5621 | // Verify fields. |
| 5622 | EXPECT_TRUE(response2.was_cached); // InitFromPickle sets this flag. |
| 5623 | EXPECT_EQ("1.2.3.4", response2.socket_address.host()); |
| 5624 | EXPECT_EQ(80, response2.socket_address.port()); |
| 5625 | EXPECT_EQ("HTTP/1.1 200 OK", response2.headers->GetStatusLine()); |
| 5626 | } |
| 5627 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5628 | // Tests that we delete an entry when the request is cancelled before starting |
| 5629 | // to read from the network. |
| 5630 | TEST(HttpCache, DoomOnDestruction) { |
| 5631 | MockHttpCache cache; |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5632 | |
| 5633 | MockHttpRequest request(kSimpleGET_Transaction); |
| 5634 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 5635 | Context* c = new Context(); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 5636 | int rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5637 | ASSERT_EQ(OK, rv); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5638 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5639 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 5640 | if (rv == ERR_IO_PENDING) |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5641 | c->result = c->callback.WaitForResult(); |
| 5642 | |
| 5643 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5644 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5645 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5646 | |
| 5647 | // Destroy the transaction. We only have the headers so we should delete this |
| 5648 | // entry. |
| 5649 | delete c; |
| 5650 | |
| 5651 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 5652 | |
| 5653 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5654 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5655 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5656 | } |
| 5657 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5658 | // Tests that we delete an entry when the request is cancelled if the response |
| 5659 | // does not have content-length and strong validators. |
| 5660 | TEST(HttpCache, DoomOnDestruction2) { |
| 5661 | MockHttpCache cache; |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5662 | |
| 5663 | MockHttpRequest request(kSimpleGET_Transaction); |
| 5664 | |
| 5665 | Context* c = new Context(); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 5666 | int rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5667 | ASSERT_EQ(OK, rv); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5668 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5669 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 5670 | if (rv == ERR_IO_PENDING) |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5671 | rv = c->callback.WaitForResult(); |
| 5672 | |
| 5673 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5674 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5675 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5676 | |
| 5677 | // Make sure that the entry has some data stored. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5678 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 5679 | rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5680 | if (rv == ERR_IO_PENDING) |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5681 | rv = c->callback.WaitForResult(); |
| 5682 | EXPECT_EQ(buf->size(), rv); |
| 5683 | |
| 5684 | // Destroy the transaction. |
| 5685 | delete c; |
| 5686 | |
| 5687 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 5688 | |
| 5689 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5690 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5691 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5692 | } |
| 5693 | |
| 5694 | // Tests that we delete an entry when the request is cancelled if the response |
| 5695 | // has an "Accept-Ranges: none" header. |
| 5696 | TEST(HttpCache, DoomOnDestruction3) { |
| 5697 | MockHttpCache cache; |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5698 | |
| 5699 | MockTransaction transaction(kSimpleGET_Transaction); |
| 5700 | transaction.response_headers = |
| 5701 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 5702 | "Content-Length: 22\n" |
| 5703 | "Accept-Ranges: none\n" |
[email protected] | 29cc1ce4 | 2012-07-22 18:39:35 | [diff] [blame] | 5704 | "Etag: \"foopy\"\n"; |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5705 | AddMockTransaction(&transaction); |
| 5706 | MockHttpRequest request(transaction); |
| 5707 | |
| 5708 | Context* c = new Context(); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 5709 | int rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5710 | ASSERT_EQ(OK, rv); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5711 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5712 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 5713 | if (rv == ERR_IO_PENDING) |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5714 | rv = c->callback.WaitForResult(); |
| 5715 | |
| 5716 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5717 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5718 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5719 | |
| 5720 | // Make sure that the entry has some data stored. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5721 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 5722 | rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5723 | if (rv == ERR_IO_PENDING) |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5724 | rv = c->callback.WaitForResult(); |
| 5725 | EXPECT_EQ(buf->size(), rv); |
| 5726 | |
| 5727 | // Destroy the transaction. |
| 5728 | delete c; |
| 5729 | |
| 5730 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 5731 | |
| 5732 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5733 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5734 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5735 | |
| 5736 | RemoveMockTransaction(&transaction); |
| 5737 | } |
| 5738 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5739 | // Tests that we mark an entry as incomplete when the request is cancelled. |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 5740 | TEST(HttpCache, SetTruncatedFlag) { |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5741 | MockHttpCache cache; |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5742 | |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 5743 | ScopedMockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5744 | transaction.response_headers = |
| 5745 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 5746 | "Content-Length: 22\n" |
[email protected] | 29cc1ce4 | 2012-07-22 18:39:35 | [diff] [blame] | 5747 | "Etag: \"foopy\"\n"; |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5748 | MockHttpRequest request(transaction); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5749 | |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 5750 | scoped_ptr<Context> c(new Context()); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 5751 | |
| 5752 | int rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5753 | ASSERT_EQ(OK, rv); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5754 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5755 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 5756 | if (rv == ERR_IO_PENDING) |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5757 | rv = c->callback.WaitForResult(); |
| 5758 | |
| 5759 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5760 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5761 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5762 | |
| 5763 | // Make sure that the entry has some data stored. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5764 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 5765 | rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5766 | if (rv == ERR_IO_PENDING) |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5767 | rv = c->callback.WaitForResult(); |
| 5768 | EXPECT_EQ(buf->size(), rv); |
| 5769 | |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 5770 | // We want to cancel the request when the transaction is busy. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 5771 | rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5772 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 5773 | EXPECT_FALSE(c->callback.have_result()); |
| 5774 | |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 5775 | MockHttpCache::SetTestMode(TEST_MODE_SYNC_ALL); |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 5776 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5777 | // Destroy the transaction. |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 5778 | c->trans.reset(); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 5779 | MockHttpCache::SetTestMode(0); |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 5780 | |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 5781 | |
[email protected] | 6df35cc | 2010-02-10 00:53:06 | [diff] [blame] | 5782 | // Make sure that we don't invoke the callback. We may have an issue if the |
| 5783 | // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we |
| 5784 | // could end up with the transaction being deleted twice if we send any |
| 5785 | // notification from the transaction destructor (see http://crbug.com/31723). |
| 5786 | EXPECT_FALSE(c->callback.have_result()); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5787 | |
| 5788 | // Verify that the entry is marked as incomplete. |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 5789 | VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5790 | } |
| 5791 | |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 5792 | // Tests that we don't mark an entry as truncated when we read everything. |
| 5793 | TEST(HttpCache, DontSetTruncatedFlag) { |
| 5794 | MockHttpCache cache; |
| 5795 | |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 5796 | ScopedMockTransaction transaction(kSimpleGET_Transaction); |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 5797 | transaction.response_headers = |
| 5798 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 5799 | "Content-Length: 22\n" |
[email protected] | 29cc1ce4 | 2012-07-22 18:39:35 | [diff] [blame] | 5800 | "Etag: \"foopy\"\n"; |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 5801 | MockHttpRequest request(transaction); |
| 5802 | |
| 5803 | scoped_ptr<Context> c(new Context()); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 5804 | int rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5805 | ASSERT_EQ(OK, rv); |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 5806 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5807 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 5808 | EXPECT_EQ(OK, c->callback.GetResult(rv)); |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 5809 | |
| 5810 | // Read everything. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5811 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(22)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 5812 | rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 5813 | EXPECT_EQ(buf->size(), c->callback.GetResult(rv)); |
| 5814 | |
| 5815 | // Destroy the transaction. |
| 5816 | c->trans.reset(); |
| 5817 | |
| 5818 | // Verify that the entry is not marked as truncated. |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 5819 | VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, false, 0); |
| 5820 | } |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 5821 | |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 5822 | // Tests that sparse entries don't set the truncate flag. |
| 5823 | TEST(HttpCache, RangeGET_DontTruncate) { |
| 5824 | MockHttpCache cache; |
| 5825 | |
| 5826 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 5827 | transaction.request_headers = "Range: bytes = 0-19\r\n" EXTRA_HEADER; |
| 5828 | |
| 5829 | scoped_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); |
| 5830 | scoped_ptr<HttpTransaction> trans; |
| 5831 | |
| 5832 | int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); |
| 5833 | EXPECT_EQ(OK, rv); |
| 5834 | |
| 5835 | TestCompletionCallback cb; |
| 5836 | rv = trans->Start(request.get(), cb.callback(), BoundNetLog()); |
| 5837 | EXPECT_EQ(0, cb.GetResult(rv)); |
| 5838 | |
| 5839 | scoped_refptr<IOBuffer> buf(new IOBuffer(10)); |
| 5840 | rv = trans->Read(buf.get(), 10, cb.callback()); |
| 5841 | EXPECT_EQ(10, cb.GetResult(rv)); |
| 5842 | |
| 5843 | // Should not trigger any DCHECK. |
| 5844 | trans.reset(); |
| 5845 | VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); |
| 5846 | } |
| 5847 | |
| 5848 | // Tests that sparse entries don't set the truncate flag (when the byte range |
| 5849 | // starts after 0). |
| 5850 | TEST(HttpCache, RangeGET_DontTruncate2) { |
| 5851 | MockHttpCache cache; |
| 5852 | |
| 5853 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 5854 | transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER; |
| 5855 | |
| 5856 | scoped_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); |
| 5857 | scoped_ptr<HttpTransaction> trans; |
| 5858 | |
| 5859 | int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); |
| 5860 | EXPECT_EQ(OK, rv); |
| 5861 | |
| 5862 | TestCompletionCallback cb; |
| 5863 | rv = trans->Start(request.get(), cb.callback(), BoundNetLog()); |
| 5864 | EXPECT_EQ(0, cb.GetResult(rv)); |
| 5865 | |
| 5866 | scoped_refptr<IOBuffer> buf(new IOBuffer(10)); |
| 5867 | rv = trans->Read(buf.get(), 10, cb.callback()); |
| 5868 | EXPECT_EQ(10, cb.GetResult(rv)); |
| 5869 | |
| 5870 | // Should not trigger any DCHECK. |
| 5871 | trans.reset(); |
| 5872 | VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); |
[email protected] | 2f9be75 | 2011-05-05 21:16:50 | [diff] [blame] | 5873 | } |
| 5874 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5875 | // Tests that we can continue with a request that was interrupted. |
| 5876 | TEST(HttpCache, GET_IncompleteResource) { |
| 5877 | MockHttpCache cache; |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 5878 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5879 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5880 | std::string raw_headers("HTTP/1.1 200 OK\n" |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 5881 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5882 | "ETag: \"foo\"\n" |
| 5883 | "Accept-Ranges: bytes\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5884 | "Content-Length: 80\n"); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 5885 | CreateTruncatedEntry(raw_headers, &cache); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5886 | |
| 5887 | // Now make a regular request. |
| 5888 | std::string headers; |
[email protected] | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 5889 | transaction.request_headers = EXTRA_HEADER; |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 5890 | transaction.data = kFullRangeData; |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5891 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 5892 | |
| 5893 | // We update the headers with the ones received while revalidating. |
| 5894 | std::string expected_headers( |
| 5895 | "HTTP/1.1 200 OK\n" |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 5896 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5897 | "Accept-Ranges: bytes\n" |
| 5898 | "ETag: \"foo\"\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 5899 | "Content-Length: 80\n"); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5900 | |
| 5901 | EXPECT_EQ(expected_headers, headers); |
| 5902 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5903 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5904 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5905 | |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5906 | // Verify that the disk entry was updated. |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 5907 | VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80); |
[email protected] | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 5908 | } |
| 5909 | |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 5910 | // Tests the handling of no-store when revalidating a truncated entry. |
| 5911 | TEST(HttpCache, GET_IncompleteResource_NoStore) { |
| 5912 | MockHttpCache cache; |
| 5913 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5914 | |
| 5915 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 5916 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 5917 | "ETag: \"foo\"\n" |
| 5918 | "Accept-Ranges: bytes\n" |
| 5919 | "Content-Length: 80\n"); |
| 5920 | CreateTruncatedEntry(raw_headers, &cache); |
| 5921 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5922 | |
| 5923 | // Now make a regular request. |
| 5924 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 5925 | transaction.request_headers = EXTRA_HEADER; |
| 5926 | std::string response_headers(transaction.response_headers); |
| 5927 | response_headers += ("Cache-Control: no-store\n"); |
| 5928 | transaction.response_headers = response_headers.c_str(); |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 5929 | transaction.data = kFullRangeData; |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 5930 | AddMockTransaction(&transaction); |
| 5931 | |
| 5932 | std::string headers; |
| 5933 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 5934 | |
| 5935 | // We update the headers with the ones received while revalidating. |
| 5936 | std::string expected_headers( |
| 5937 | "HTTP/1.1 200 OK\n" |
| 5938 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 5939 | "Accept-Ranges: bytes\n" |
| 5940 | "Cache-Control: no-store\n" |
| 5941 | "ETag: \"foo\"\n" |
| 5942 | "Content-Length: 80\n"); |
| 5943 | |
| 5944 | EXPECT_EQ(expected_headers, headers); |
| 5945 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5946 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5947 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5948 | |
| 5949 | // Verify that the disk entry was deleted. |
| 5950 | disk_cache::Entry* entry; |
| 5951 | EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
| 5952 | RemoveMockTransaction(&transaction); |
| 5953 | } |
| 5954 | |
| 5955 | // Tests cancelling a request after the server sent no-store. |
| 5956 | TEST(HttpCache, GET_IncompleteResource_Cancel) { |
| 5957 | MockHttpCache cache; |
| 5958 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 5959 | |
| 5960 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 5961 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 5962 | "ETag: \"foo\"\n" |
| 5963 | "Accept-Ranges: bytes\n" |
| 5964 | "Content-Length: 80\n"); |
| 5965 | CreateTruncatedEntry(raw_headers, &cache); |
| 5966 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5967 | |
| 5968 | // Now make a regular request. |
| 5969 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 5970 | transaction.request_headers = EXTRA_HEADER; |
| 5971 | std::string response_headers(transaction.response_headers); |
| 5972 | response_headers += ("Cache-Control: no-store\n"); |
| 5973 | transaction.response_headers = response_headers.c_str(); |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 5974 | transaction.data = kFullRangeData; |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 5975 | AddMockTransaction(&transaction); |
| 5976 | |
| 5977 | MockHttpRequest request(transaction); |
| 5978 | Context* c = new Context(); |
| 5979 | |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 5980 | int rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5981 | ASSERT_EQ(OK, rv); |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 5982 | |
[email protected] | 9f4633c6 | 2012-05-29 18:38:57 | [diff] [blame] | 5983 | // Queue another request to this transaction. We have to start this request |
| 5984 | // before the first one gets the response from the server and dooms the entry, |
| 5985 | // otherwise it will just create a new entry without being queued to the first |
| 5986 | // request. |
| 5987 | Context* pending = new Context(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5988 | ASSERT_EQ(OK, cache.CreateTransaction(&pending->trans)); |
[email protected] | 9f4633c6 | 2012-05-29 18:38:57 | [diff] [blame] | 5989 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5990 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 5991 | EXPECT_EQ(ERR_IO_PENDING, |
[email protected] | 9f4633c6 | 2012-05-29 18:38:57 | [diff] [blame] | 5992 | pending->trans->Start(&request, pending->callback.callback(), |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5993 | BoundNetLog())); |
| 5994 | EXPECT_EQ(OK, c->callback.GetResult(rv)); |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 5995 | |
| 5996 | // Make sure that the entry has some data stored. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 5997 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 5998 | rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 5999 | EXPECT_EQ(5, c->callback.GetResult(rv)); |
| 6000 | |
[email protected] | 9f4633c6 | 2012-05-29 18:38:57 | [diff] [blame] | 6001 | // Cancel the requests. |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 6002 | delete c; |
[email protected] | 9f4633c6 | 2012-05-29 18:38:57 | [diff] [blame] | 6003 | delete pending; |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 6004 | |
| 6005 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6006 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
[email protected] | 9f4633c6 | 2012-05-29 18:38:57 | [diff] [blame] | 6007 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 6008 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 6009 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 767c2c9d | 2012-05-09 23:44:27 | [diff] [blame] | 6010 | RemoveMockTransaction(&transaction); |
| 6011 | } |
| 6012 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 6013 | // Tests that we delete truncated entries if the server changes its mind midway. |
| 6014 | TEST(HttpCache, GET_IncompleteResource2) { |
| 6015 | MockHttpCache cache; |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 6016 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 6017 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 6018 | // Content-length will be intentionally bad. |
| 6019 | std::string raw_headers("HTTP/1.1 200 OK\n" |
[email protected] | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 6020 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 6021 | "ETag: \"foo\"\n" |
| 6022 | "Accept-Ranges: bytes\n" |
| 6023 | "Content-Length: 50\n"); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6024 | CreateTruncatedEntry(raw_headers, &cache); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 6025 | |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6026 | // Now make a regular request. We expect the code to fail the validation and |
| 6027 | // retry the request without using byte ranges. |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 6028 | std::string headers; |
| 6029 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 6030 | transaction.request_headers = EXTRA_HEADER; |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6031 | transaction.data = "Not a range"; |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 6032 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 6033 | |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6034 | // The server will return 200 instead of a byte range. |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 6035 | std::string expected_headers( |
| 6036 | "HTTP/1.1 200 OK\n" |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6037 | "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 6038 | |
| 6039 | EXPECT_EQ(expected_headers, headers); |
| 6040 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6041 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6042 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6043 | |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 6044 | // Verify that the disk entry was deleted. |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6045 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 6046 | ASSERT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6047 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 6048 | } |
| 6049 | |
| 6050 | // Tests that we always validate a truncated request. |
| 6051 | TEST(HttpCache, GET_IncompleteResource3) { |
| 6052 | MockHttpCache cache; |
| 6053 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 6054 | |
| 6055 | // This should not require validation for 10 hours. |
| 6056 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 6057 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 6058 | "ETag: \"foo\"\n" |
| 6059 | "Cache-Control: max-age= 36000\n" |
| 6060 | "Accept-Ranges: bytes\n" |
| 6061 | "Content-Length: 80\n"); |
| 6062 | CreateTruncatedEntry(raw_headers, &cache); |
| 6063 | |
| 6064 | // Now make a regular request. |
| 6065 | std::string headers; |
| 6066 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 6067 | transaction.request_headers = EXTRA_HEADER; |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 6068 | transaction.data = kFullRangeData; |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6069 | |
| 6070 | scoped_ptr<Context> c(new Context); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 6071 | int rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6072 | ASSERT_EQ(OK, rv); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6073 | |
| 6074 | MockHttpRequest request(transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6075 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 6076 | EXPECT_EQ(OK, c->callback.GetResult(rv)); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6077 | |
| 6078 | // We should have checked with the server before finishing Start(). |
| 6079 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6080 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6081 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6082 | |
| 6083 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 6084 | } |
| 6085 | |
[email protected] | d7358ba | 2014-01-04 01:39:49 | [diff] [blame] | 6086 | // Tests that we handle 401s for truncated resources. |
| 6087 | TEST(HttpCache, GET_IncompleteResourceWithAuth) { |
| 6088 | MockHttpCache cache; |
| 6089 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 6090 | |
| 6091 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 6092 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 6093 | "ETag: \"foo\"\n" |
| 6094 | "Accept-Ranges: bytes\n" |
| 6095 | "Content-Length: 80\n"); |
| 6096 | CreateTruncatedEntry(raw_headers, &cache); |
| 6097 | |
| 6098 | // Now make a regular request. |
| 6099 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 6100 | transaction.request_headers = "X-Require-Mock-Auth: dummy\r\n" |
| 6101 | EXTRA_HEADER; |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 6102 | transaction.data = kFullRangeData; |
[email protected] | d7358ba | 2014-01-04 01:39:49 | [diff] [blame] | 6103 | RangeTransactionServer handler; |
| 6104 | |
| 6105 | scoped_ptr<Context> c(new Context); |
| 6106 | int rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6107 | ASSERT_EQ(OK, rv); |
[email protected] | d7358ba | 2014-01-04 01:39:49 | [diff] [blame] | 6108 | |
| 6109 | MockHttpRequest request(transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6110 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 6111 | EXPECT_EQ(OK, c->callback.GetResult(rv)); |
[email protected] | d7358ba | 2014-01-04 01:39:49 | [diff] [blame] | 6112 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6113 | const HttpResponseInfo* response = c->trans->GetResponseInfo(); |
[email protected] | d7358ba | 2014-01-04 01:39:49 | [diff] [blame] | 6114 | ASSERT_TRUE(response); |
| 6115 | ASSERT_EQ(401, response->headers->response_code()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6116 | rv = c->trans->RestartWithAuth(AuthCredentials(), c->callback.callback()); |
| 6117 | EXPECT_EQ(OK, c->callback.GetResult(rv)); |
[email protected] | d7358ba | 2014-01-04 01:39:49 | [diff] [blame] | 6118 | response = c->trans->GetResponseInfo(); |
| 6119 | ASSERT_TRUE(response); |
| 6120 | ASSERT_EQ(200, response->headers->response_code()); |
| 6121 | |
| 6122 | ReadAndVerifyTransaction(c->trans.get(), transaction); |
| 6123 | c.reset(); // The destructor could delete the entry. |
| 6124 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6125 | |
| 6126 | // Verify that the entry was not deleted. |
| 6127 | disk_cache::Entry* entry; |
| 6128 | ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
| 6129 | entry->Close(); |
| 6130 | |
| 6131 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 6132 | } |
| 6133 | |
liberato | 1f59bb3f | 2015-05-29 14:19:10 | [diff] [blame] | 6134 | // Test that the transaction won't retry failed partial requests |
| 6135 | // after it starts reading data. http://crbug.com/474835 |
| 6136 | TEST(HttpCache, TransactionRetryLimit) { |
| 6137 | MockHttpCache cache; |
| 6138 | |
| 6139 | // Cache 0-9, so that we have data to read before failing. |
| 6140 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 6141 | transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; |
| 6142 | transaction.data = "rg: 00-09 "; |
| 6143 | |
| 6144 | // Write to the cache. |
| 6145 | RunTransactionTest(cache.http_cache(), transaction); |
| 6146 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6147 | |
| 6148 | // And now read from the cache and the network. 10-19 will get a |
| 6149 | // 401, but will have already returned 0-9. |
| 6150 | // We do not set X-Require-Mock-Auth because that causes the mock |
| 6151 | // network transaction to become IsReadyToRestartForAuth(). |
| 6152 | transaction.request_headers = |
| 6153 | "Range: bytes = 0-79\r\n" |
| 6154 | "X-Require-Mock-Auth-Alt: dummy\r\n" EXTRA_HEADER; |
| 6155 | |
| 6156 | scoped_ptr<Context> c(new Context); |
| 6157 | int rv = cache.CreateTransaction(&c->trans); |
| 6158 | ASSERT_EQ(OK, rv); |
| 6159 | |
| 6160 | MockHttpRequest request(transaction); |
| 6161 | |
| 6162 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 6163 | if (rv == ERR_IO_PENDING) |
| 6164 | rv = c->callback.WaitForResult(); |
| 6165 | std::string content; |
| 6166 | rv = ReadTransaction(c->trans.get(), &content); |
| 6167 | EXPECT_EQ(ERR_CACHE_AUTH_FAILURE_AFTER_READ, rv); |
| 6168 | } |
| 6169 | |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6170 | // Tests that we cache a 200 response to the validation request. |
| 6171 | TEST(HttpCache, GET_IncompleteResource4) { |
| 6172 | MockHttpCache cache; |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 6173 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6174 | |
| 6175 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 6176 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 6177 | "ETag: \"foo\"\n" |
| 6178 | "Accept-Ranges: bytes\n" |
| 6179 | "Content-Length: 80\n"); |
| 6180 | CreateTruncatedEntry(raw_headers, &cache); |
| 6181 | |
| 6182 | // Now make a regular request. |
| 6183 | std::string headers; |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6184 | transaction.request_headers = EXTRA_HEADER; |
| 6185 | transaction.data = "Not a range"; |
| 6186 | RangeTransactionServer handler; |
| 6187 | handler.set_bad_200(true); |
| 6188 | RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 6189 | |
| 6190 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6191 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6192 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6193 | |
| 6194 | // Verify that the disk entry was updated. |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 6195 | VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 11); |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 6196 | } |
| 6197 | |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 6198 | // Tests that when we cancel a request that was interrupted, we mark it again |
| 6199 | // as truncated. |
| 6200 | TEST(HttpCache, GET_CancelIncompleteResource) { |
| 6201 | MockHttpCache cache; |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 6202 | ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 6203 | |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 6204 | std::string raw_headers("HTTP/1.1 200 OK\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 6205 | "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 6206 | "ETag: \"foo\"\n" |
| 6207 | "Accept-Ranges: bytes\n" |
[email protected] | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 6208 | "Content-Length: 80\n"); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6209 | CreateTruncatedEntry(raw_headers, &cache); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 6210 | |
| 6211 | // Now make a regular request. |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 6212 | transaction.request_headers = EXTRA_HEADER; |
| 6213 | |
| 6214 | MockHttpRequest request(transaction); |
| 6215 | Context* c = new Context(); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 6216 | int rv = cache.CreateTransaction(&c->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6217 | ASSERT_EQ(OK, rv); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 6218 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6219 | rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 6220 | EXPECT_EQ(OK, c->callback.GetResult(rv)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 6221 | |
| 6222 | // Read 20 bytes from the cache, and 10 from the net. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6223 | scoped_refptr<IOBuffer> buf(new IOBuffer(100)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6224 | rv = c->trans->Read(buf.get(), 20, c->callback.callback()); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6225 | EXPECT_EQ(20, c->callback.GetResult(rv)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6226 | rv = c->trans->Read(buf.get(), 10, c->callback.callback()); |
[email protected] | 21e74320 | 2009-12-18 01:31:04 | [diff] [blame] | 6227 | EXPECT_EQ(10, c->callback.GetResult(rv)); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 6228 | |
| 6229 | // At this point, we are already reading so canceling the request should leave |
| 6230 | // a truncated one. |
| 6231 | delete c; |
| 6232 | |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 6233 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6234 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6235 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6236 | |
| 6237 | // Verify that the disk entry was updated: now we have 30 bytes. |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 6238 | VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, true, 30); |
[email protected] | 8a92555 | 2009-11-20 23:16:00 | [diff] [blame] | 6239 | } |
| 6240 | |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 6241 | // Tests that we can handle range requests when we have a truncated entry. |
| 6242 | TEST(HttpCache, RangeGET_IncompleteResource) { |
| 6243 | MockHttpCache cache; |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 6244 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 6245 | |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 6246 | // Content-length will be intentionally bogus. |
| 6247 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 6248 | "Last-Modified: something\n" |
| 6249 | "ETag: \"foo\"\n" |
| 6250 | "Accept-Ranges: bytes\n" |
| 6251 | "Content-Length: 10\n"); |
[email protected] | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 6252 | CreateTruncatedEntry(raw_headers, &cache); |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 6253 | |
| 6254 | // Now make a range request. |
| 6255 | std::string headers; |
| 6256 | RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 6257 | &headers); |
| 6258 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 6259 | Verify206Response(headers, 40, 49); |
[email protected] | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 6260 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6261 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6262 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 6263 | |
| 6264 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 6265 | } |
| 6266 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6267 | TEST(HttpCache, SyncRead) { |
| 6268 | MockHttpCache cache; |
| 6269 | |
| 6270 | // This test ensures that a read that completes synchronously does not cause |
| 6271 | // any problems. |
| 6272 | |
| 6273 | ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 6274 | transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START | |
[email protected] | 73cae57 | 2009-10-22 18:36:19 | [diff] [blame] | 6275 | TEST_MODE_SYNC_CACHE_READ | |
| 6276 | TEST_MODE_SYNC_CACHE_WRITE); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6277 | |
| 6278 | MockHttpRequest r1(transaction), |
[email protected] | 4677316 | 2010-05-07 22:31:20 | [diff] [blame] | 6279 | r2(transaction), |
| 6280 | r3(transaction); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6281 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6282 | TestTransactionConsumer c1(DEFAULT_PRIORITY, cache.http_cache()), |
| 6283 | c2(DEFAULT_PRIORITY, cache.http_cache()), |
| 6284 | c3(DEFAULT_PRIORITY, cache.http_cache()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6285 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6286 | c1.Start(&r1, BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6287 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6288 | r2.load_flags |= LOAD_ONLY_FROM_CACHE; |
| 6289 | c2.Start(&r2, BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6290 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6291 | r3.load_flags |= LOAD_ONLY_FROM_CACHE; |
| 6292 | c3.Start(&r3, BoundNetLog()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6293 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 6294 | base::MessageLoop::current()->Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6295 | |
| 6296 | EXPECT_TRUE(c1.is_done()); |
| 6297 | EXPECT_TRUE(c2.is_done()); |
| 6298 | EXPECT_TRUE(c3.is_done()); |
| 6299 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6300 | EXPECT_EQ(OK, c1.error()); |
| 6301 | EXPECT_EQ(OK, c2.error()); |
| 6302 | EXPECT_EQ(OK, c3.error()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6303 | } |
| 6304 | |
| 6305 | TEST(HttpCache, ValidationResultsIn200) { |
| 6306 | MockHttpCache cache; |
| 6307 | |
| 6308 | // This test ensures that a conditional request, which results in a 200 |
| 6309 | // instead of a 304, properly truncates the existing response data. |
| 6310 | |
| 6311 | // write to the cache |
| 6312 | RunTransactionTest(cache.http_cache(), kETagGET_Transaction); |
| 6313 | |
| 6314 | // force this transaction to validate the cache |
| 6315 | MockTransaction transaction(kETagGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6316 | transaction.load_flags |= LOAD_VALIDATE_CACHE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6317 | RunTransactionTest(cache.http_cache(), transaction); |
| 6318 | |
| 6319 | // read from the cache |
| 6320 | RunTransactionTest(cache.http_cache(), kETagGET_Transaction); |
| 6321 | } |
| 6322 | |
| 6323 | TEST(HttpCache, CachedRedirect) { |
| 6324 | MockHttpCache cache; |
| 6325 | |
| 6326 | ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); |
| 6327 | kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; |
| 6328 | kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; |
| 6329 | |
| 6330 | MockHttpRequest request(kTestTransaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6331 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6332 | |
[email protected] | 5b2bacc2 | 2013-09-19 17:58:39 | [diff] [blame] | 6333 | // Write to the cache. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6334 | { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6335 | scoped_ptr<HttpTransaction> trans; |
| 6336 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6337 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6338 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 6339 | if (rv == ERR_IO_PENDING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6340 | rv = callback.WaitForResult(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6341 | ASSERT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6342 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6343 | const HttpResponseInfo* info = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6344 | ASSERT_TRUE(info); |
| 6345 | |
| 6346 | EXPECT_EQ(info->headers->response_code(), 301); |
| 6347 | |
| 6348 | std::string location; |
| 6349 | info->headers->EnumerateHeader(NULL, "Location", &location); |
| 6350 | EXPECT_EQ(location, "http://www.bar.com/"); |
| 6351 | |
[email protected] | 5b2bacc2 | 2013-09-19 17:58:39 | [diff] [blame] | 6352 | // Mark the transaction as completed so it is cached. |
| 6353 | trans->DoneReading(); |
| 6354 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 6355 | // Destroy transaction when going out of scope. We have not actually |
| 6356 | // read the response body -- want to test that it is still getting cached. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6357 | } |
| 6358 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6359 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 6360 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6361 | |
[email protected] | 5b2bacc2 | 2013-09-19 17:58:39 | [diff] [blame] | 6362 | // Active entries in the cache are not retired synchronously. Make |
| 6363 | // sure the next run hits the MockHttpCache and open_count is |
| 6364 | // correct. |
| 6365 | base::MessageLoop::current()->RunUntilIdle(); |
| 6366 | |
| 6367 | // Read from the cache. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6368 | { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6369 | scoped_ptr<HttpTransaction> trans; |
| 6370 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6371 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6372 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 6373 | if (rv == ERR_IO_PENDING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6374 | rv = callback.WaitForResult(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6375 | ASSERT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6376 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6377 | const HttpResponseInfo* info = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6378 | ASSERT_TRUE(info); |
| 6379 | |
| 6380 | EXPECT_EQ(info->headers->response_code(), 301); |
| 6381 | |
| 6382 | std::string location; |
| 6383 | info->headers->EnumerateHeader(NULL, "Location", &location); |
| 6384 | EXPECT_EQ(location, "http://www.bar.com/"); |
| 6385 | |
[email protected] | 5b2bacc2 | 2013-09-19 17:58:39 | [diff] [blame] | 6386 | // Mark the transaction as completed so it is cached. |
| 6387 | trans->DoneReading(); |
| 6388 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 6389 | // Destroy transaction when going out of scope. We have not actually |
| 6390 | // read the response body -- want to test that it is still getting cached. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6391 | } |
| 6392 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6393 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6394 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6395 | } |
| 6396 | |
[email protected] | 2b337ef | 2013-08-23 20:07:05 | [diff] [blame] | 6397 | // Verify that no-cache resources are stored in cache, but are not fetched from |
| 6398 | // cache during normal loads. |
| 6399 | TEST(HttpCache, CacheControlNoCacheNormalLoad) { |
| 6400 | MockHttpCache cache; |
| 6401 | |
| 6402 | ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 6403 | transaction.response_headers = "cache-control: no-cache\n"; |
| 6404 | |
| 6405 | // Initial load. |
| 6406 | RunTransactionTest(cache.http_cache(), transaction); |
| 6407 | |
| 6408 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6409 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 6410 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6411 | |
| 6412 | // Try loading again; it should result in a network fetch. |
| 6413 | RunTransactionTest(cache.http_cache(), transaction); |
| 6414 | |
| 6415 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6416 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6417 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6418 | |
| 6419 | disk_cache::Entry* entry; |
| 6420 | EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry)); |
| 6421 | entry->Close(); |
| 6422 | } |
| 6423 | |
| 6424 | // Verify that no-cache resources are stored in cache and fetched from cache |
| 6425 | // when the LOAD_PREFERRING_CACHE flag is set. |
| 6426 | TEST(HttpCache, CacheControlNoCacheHistoryLoad) { |
| 6427 | MockHttpCache cache; |
| 6428 | |
| 6429 | ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 6430 | transaction.response_headers = "cache-control: no-cache\n"; |
| 6431 | |
| 6432 | // Initial load. |
| 6433 | RunTransactionTest(cache.http_cache(), transaction); |
| 6434 | |
| 6435 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6436 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 6437 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6438 | |
| 6439 | // Try loading again with LOAD_PREFERRING_CACHE. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6440 | transaction.load_flags = LOAD_PREFERRING_CACHE; |
[email protected] | 2b337ef | 2013-08-23 20:07:05 | [diff] [blame] | 6441 | RunTransactionTest(cache.http_cache(), transaction); |
| 6442 | |
| 6443 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6444 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6445 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6446 | |
| 6447 | disk_cache::Entry* entry; |
| 6448 | EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry)); |
| 6449 | entry->Close(); |
| 6450 | } |
| 6451 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6452 | TEST(HttpCache, CacheControlNoStore) { |
| 6453 | MockHttpCache cache; |
| 6454 | |
| 6455 | ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 6456 | transaction.response_headers = "cache-control: no-store\n"; |
| 6457 | |
| 6458 | // initial load |
| 6459 | RunTransactionTest(cache.http_cache(), transaction); |
| 6460 | |
| 6461 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6462 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 6463 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6464 | |
| 6465 | // try loading again; it should result in a network fetch |
| 6466 | RunTransactionTest(cache.http_cache(), transaction); |
| 6467 | |
| 6468 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6469 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 6470 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 6471 | |
| 6472 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 6473 | EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6474 | } |
| 6475 | |
| 6476 | TEST(HttpCache, CacheControlNoStore2) { |
| 6477 | // this test is similar to the above test, except that the initial response |
| 6478 | // is cachable, but when it is validated, no-store is received causing the |
| 6479 | // cached document to be deleted. |
| 6480 | MockHttpCache cache; |
| 6481 | |
| 6482 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 6483 | |
| 6484 | // initial load |
| 6485 | RunTransactionTest(cache.http_cache(), transaction); |
| 6486 | |
| 6487 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6488 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 6489 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6490 | |
| 6491 | // try loading again; it should result in a network fetch |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6492 | transaction.load_flags = LOAD_VALIDATE_CACHE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6493 | transaction.response_headers = "cache-control: no-store\n"; |
| 6494 | RunTransactionTest(cache.http_cache(), transaction); |
| 6495 | |
| 6496 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6497 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6498 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6499 | |
| 6500 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 6501 | EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6502 | } |
| 6503 | |
| 6504 | TEST(HttpCache, CacheControlNoStore3) { |
| 6505 | // this test is similar to the above test, except that the response is a 304 |
| 6506 | // instead of a 200. this should never happen in practice, but it seems like |
| 6507 | // a good thing to verify that we still destroy the cache entry. |
| 6508 | MockHttpCache cache; |
| 6509 | |
| 6510 | ScopedMockTransaction transaction(kETagGET_Transaction); |
| 6511 | |
| 6512 | // initial load |
| 6513 | RunTransactionTest(cache.http_cache(), transaction); |
| 6514 | |
| 6515 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6516 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 6517 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6518 | |
| 6519 | // try loading again; it should result in a network fetch |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6520 | transaction.load_flags = LOAD_VALIDATE_CACHE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6521 | transaction.response_headers = "cache-control: no-store\n"; |
| 6522 | transaction.status = "HTTP/1.1 304 Not Modified"; |
| 6523 | RunTransactionTest(cache.http_cache(), transaction); |
| 6524 | |
| 6525 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6526 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6527 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6528 | |
| 6529 | disk_cache::Entry* entry; |
[email protected] | 02e7a01 | 2010-05-10 23:06:33 | [diff] [blame] | 6530 | EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6531 | } |
| 6532 | |
| 6533 | // Ensure that we don't cache requests served over bad HTTPS. |
| 6534 | TEST(HttpCache, SimpleGET_SSLError) { |
| 6535 | MockHttpCache cache; |
| 6536 | |
| 6537 | MockTransaction transaction = kSimpleGET_Transaction; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6538 | transaction.cert_status = CERT_STATUS_REVOKED; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6539 | ScopedMockTransaction scoped_transaction(transaction); |
| 6540 | |
| 6541 | // write to the cache |
| 6542 | RunTransactionTest(cache.http_cache(), transaction); |
| 6543 | |
| 6544 | // Test that it was not cached. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6545 | transaction.load_flags |= LOAD_ONLY_FROM_CACHE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6546 | |
| 6547 | MockHttpRequest request(transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6548 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6549 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6550 | scoped_ptr<HttpTransaction> trans; |
| 6551 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6552 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6553 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 6554 | if (rv == ERR_IO_PENDING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6555 | rv = callback.WaitForResult(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6556 | ASSERT_EQ(ERR_CACHE_MISS, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6557 | } |
[email protected] | 3e2d38d | 2009-02-14 02:01:18 | [diff] [blame] | 6558 | |
| 6559 | // Ensure that we don't crash by if left-behind transactions. |
| 6560 | TEST(HttpCache, OutlivedTransactions) { |
| 6561 | MockHttpCache* cache = new MockHttpCache; |
| 6562 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6563 | scoped_ptr<HttpTransaction> trans; |
| 6564 | EXPECT_EQ(OK, cache->CreateTransaction(&trans)); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 6565 | |
[email protected] | b367d9a5 | 2009-02-27 01:02:51 | [diff] [blame] | 6566 | delete cache; |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 6567 | trans.reset(); |
[email protected] | 3e2d38d | 2009-02-14 02:01:18 | [diff] [blame] | 6568 | } |
[email protected] | 98179700 | 2009-06-05 07:14:15 | [diff] [blame] | 6569 | |
| 6570 | // Test that the disabled mode works. |
| 6571 | TEST(HttpCache, CacheDisabledMode) { |
| 6572 | MockHttpCache cache; |
| 6573 | |
| 6574 | // write to the cache |
| 6575 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 6576 | |
| 6577 | // go into disabled mode |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6578 | cache.http_cache()->set_mode(HttpCache::DISABLE); |
[email protected] | 98179700 | 2009-06-05 07:14:15 | [diff] [blame] | 6579 | |
| 6580 | // force this transaction to write to the cache again |
| 6581 | MockTransaction transaction(kSimpleGET_Transaction); |
| 6582 | |
| 6583 | RunTransactionTest(cache.http_cache(), transaction); |
| 6584 | |
| 6585 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6586 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 6587 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6588 | } |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 6589 | |
| 6590 | // Other tests check that the response headers of the cached response |
| 6591 | // get updated on 304. Here we specifically check that the |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 6592 | // HttpResponseHeaders::request_time and HttpResponseHeaders::response_time |
| 6593 | // fields also gets updated. |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 6594 | // http://crbug.com/20594. |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 6595 | TEST(HttpCache, UpdatesRequestResponseTimeOn304) { |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 6596 | MockHttpCache cache; |
| 6597 | |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 6598 | const char kUrl[] = "http://foobar"; |
| 6599 | const char kData[] = "body"; |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 6600 | |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 6601 | MockTransaction mock_network_response = { 0 }; |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 6602 | mock_network_response.url = kUrl; |
| 6603 | |
| 6604 | AddMockTransaction(&mock_network_response); |
| 6605 | |
| 6606 | // Request |kUrl|, causing |kNetResponse1| to be written to the cache. |
| 6607 | |
[email protected] | 4822ae0 | 2012-09-11 17:37:59 | [diff] [blame] | 6608 | MockTransaction request = { 0 }; |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 6609 | request.url = kUrl; |
| 6610 | request.method = "GET"; |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 6611 | request.request_headers = "\r\n"; |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 6612 | request.data = kData; |
| 6613 | |
| 6614 | static const Response kNetResponse1 = { |
| 6615 | "HTTP/1.1 200 OK", |
| 6616 | "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 6617 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 6618 | kData |
| 6619 | }; |
| 6620 | |
| 6621 | kNetResponse1.AssignTo(&mock_network_response); |
| 6622 | |
| 6623 | RunTransactionTest(cache.http_cache(), request); |
| 6624 | |
| 6625 | // Request |kUrl| again, this time validating the cache and getting |
| 6626 | // a 304 back. |
| 6627 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6628 | request.load_flags = LOAD_VALIDATE_CACHE; |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 6629 | |
| 6630 | static const Response kNetResponse2 = { |
| 6631 | "HTTP/1.1 304 Not Modified", |
| 6632 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n", |
| 6633 | "" |
| 6634 | }; |
| 6635 | |
| 6636 | kNetResponse2.AssignTo(&mock_network_response); |
| 6637 | |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 6638 | base::Time request_time = base::Time() + base::TimeDelta::FromHours(1234); |
| 6639 | base::Time response_time = base::Time() + base::TimeDelta::FromHours(1235); |
| 6640 | |
| 6641 | mock_network_response.request_time = request_time; |
| 6642 | mock_network_response.response_time = response_time; |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 6643 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6644 | HttpResponseInfo response; |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 6645 | RunTransactionTestWithResponseInfo(cache.http_cache(), request, &response); |
| 6646 | |
[email protected] | ca2f19e | 2009-09-04 22:53:16 | [diff] [blame] | 6647 | // The request and response times should have been updated. |
| 6648 | EXPECT_EQ(request_time.ToInternalValue(), |
| 6649 | response.request_time.ToInternalValue()); |
| 6650 | EXPECT_EQ(response_time.ToInternalValue(), |
[email protected] | 207d58c7 | 2009-09-04 18:59:29 | [diff] [blame] | 6651 | response.response_time.ToInternalValue()); |
| 6652 | |
| 6653 | std::string headers; |
| 6654 | response.headers->GetNormalizedHeaders(&headers); |
| 6655 | |
| 6656 | EXPECT_EQ("HTTP/1.1 200 OK\n" |
| 6657 | "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 6658 | "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 6659 | headers); |
| 6660 | |
| 6661 | RemoveMockTransaction(&mock_network_response); |
| 6662 | } |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6663 | |
| 6664 | // Tests that we can write metadata to an entry. |
| 6665 | TEST(HttpCache, WriteMetadata_OK) { |
| 6666 | MockHttpCache cache; |
| 6667 | |
| 6668 | // Write to the cache |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6669 | HttpResponseInfo response; |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6670 | RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 6671 | &response); |
| 6672 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 6673 | |
| 6674 | // Trivial call. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6675 | cache.http_cache()->WriteMetadata(GURL("foo"), DEFAULT_PRIORITY, Time::Now(), |
| 6676 | NULL, 0); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6677 | |
| 6678 | // Write meta data to the same entry. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6679 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50)); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6680 | memset(buf->data(), 0, buf->size()); |
| 6681 | base::strlcpy(buf->data(), "Hi there", buf->size()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6682 | cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url), |
| 6683 | DEFAULT_PRIORITY, response.response_time, |
| 6684 | buf.get(), buf->size()); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6685 | |
| 6686 | // Release the buffer before the operation takes place. |
| 6687 | buf = NULL; |
| 6688 | |
| 6689 | // Makes sure we finish pending operations. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 6690 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6691 | |
| 6692 | RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 6693 | &response); |
| 6694 | ASSERT_TRUE(response.metadata.get() != NULL); |
| 6695 | EXPECT_EQ(50, response.metadata->size()); |
| 6696 | EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there")); |
| 6697 | |
| 6698 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6699 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 6700 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6701 | } |
| 6702 | |
| 6703 | // Tests that we only write metadata to an entry if the time stamp matches. |
| 6704 | TEST(HttpCache, WriteMetadata_Fail) { |
| 6705 | MockHttpCache cache; |
| 6706 | |
| 6707 | // Write to the cache |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6708 | HttpResponseInfo response; |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6709 | RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 6710 | &response); |
| 6711 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 6712 | |
| 6713 | // Attempt to write meta data to the same entry. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6714 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50)); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6715 | memset(buf->data(), 0, buf->size()); |
| 6716 | base::strlcpy(buf->data(), "Hi there", buf->size()); |
| 6717 | base::Time expected_time = response.response_time - |
| 6718 | base::TimeDelta::FromMilliseconds(20); |
yangguo | 7fbf4c1 | 2015-02-20 22:16:57 | [diff] [blame] | 6719 | cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url), |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6720 | DEFAULT_PRIORITY, expected_time, buf.get(), |
| 6721 | buf->size()); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6722 | |
| 6723 | // Makes sure we finish pending operations. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 6724 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6725 | |
| 6726 | RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 6727 | &response); |
| 6728 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 6729 | |
| 6730 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6731 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 6732 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6733 | } |
| 6734 | |
| 6735 | // Tests that we can read metadata after validating the entry and with READ mode |
| 6736 | // transactions. |
| 6737 | TEST(HttpCache, ReadMetadata) { |
| 6738 | MockHttpCache cache; |
| 6739 | |
| 6740 | // Write to the cache |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6741 | HttpResponseInfo response; |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6742 | RunTransactionTestWithResponseInfo(cache.http_cache(), |
| 6743 | kTypicalGET_Transaction, &response); |
| 6744 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 6745 | |
| 6746 | // Write meta data to the same entry. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6747 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50)); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6748 | memset(buf->data(), 0, buf->size()); |
| 6749 | base::strlcpy(buf->data(), "Hi there", buf->size()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6750 | cache.http_cache()->WriteMetadata(GURL(kTypicalGET_Transaction.url), |
| 6751 | DEFAULT_PRIORITY, response.response_time, |
| 6752 | buf.get(), buf->size()); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6753 | |
| 6754 | // Makes sure we finish pending operations. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 6755 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6756 | |
| 6757 | // Start with a READ mode transaction. |
| 6758 | MockTransaction trans1(kTypicalGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6759 | trans1.load_flags = LOAD_ONLY_FROM_CACHE; |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6760 | |
| 6761 | RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response); |
| 6762 | ASSERT_TRUE(response.metadata.get() != NULL); |
| 6763 | EXPECT_EQ(50, response.metadata->size()); |
| 6764 | EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there")); |
| 6765 | |
| 6766 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6767 | EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 6768 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 6769 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6770 | |
| 6771 | // Now make sure that the entry is re-validated with the server. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6772 | trans1.load_flags = LOAD_VALIDATE_CACHE; |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6773 | trans1.status = "HTTP/1.1 304 Not Modified"; |
| 6774 | AddMockTransaction(&trans1); |
| 6775 | |
| 6776 | response.metadata = NULL; |
| 6777 | RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response); |
| 6778 | EXPECT_TRUE(response.metadata.get() != NULL); |
| 6779 | |
| 6780 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6781 | EXPECT_EQ(3, cache.disk_cache()->open_count()); |
| 6782 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 6783 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6784 | RemoveMockTransaction(&trans1); |
| 6785 | |
| 6786 | // Now return 200 when validating the entry so the metadata will be lost. |
| 6787 | MockTransaction trans2(kTypicalGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6788 | trans2.load_flags = LOAD_VALIDATE_CACHE; |
[email protected] | 47b9505 | 2010-03-02 19:10:07 | [diff] [blame] | 6789 | RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); |
| 6790 | EXPECT_TRUE(response.metadata.get() == NULL); |
| 6791 | |
| 6792 | EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 6793 | EXPECT_EQ(4, cache.disk_cache()->open_count()); |
| 6794 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6795 | } |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 6796 | |
| 6797 | // Tests that we don't mark entries as truncated when a filter detects the end |
| 6798 | // of the stream. |
| 6799 | TEST(HttpCache, FilterCompletion) { |
| 6800 | MockHttpCache cache; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6801 | TestCompletionCallback callback; |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 6802 | |
| 6803 | { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6804 | scoped_ptr<HttpTransaction> trans; |
| 6805 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 6806 | |
| 6807 | MockHttpRequest request(kSimpleGET_Transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6808 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 6809 | EXPECT_EQ(OK, callback.GetResult(rv)); |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 6810 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6811 | scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6812 | rv = trans->Read(buf.get(), 256, callback.callback()); |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 6813 | EXPECT_GT(callback.GetResult(rv), 0); |
| 6814 | |
| 6815 | // Now make sure that the entry is preserved. |
| 6816 | trans->DoneReading(); |
| 6817 | } |
| 6818 | |
[email protected] | c85316f | 2011-08-18 23:27:36 | [diff] [blame] | 6819 | // Make sure that the ActiveEntry is gone. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 6820 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 6821 | |
| 6822 | // Read from the cache. |
| 6823 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 6824 | |
| 6825 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6826 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6827 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6828 | } |
[email protected] | c85316f | 2011-08-18 23:27:36 | [diff] [blame] | 6829 | |
[email protected] | 5b2bacc2 | 2013-09-19 17:58:39 | [diff] [blame] | 6830 | // Tests that we don't mark entries as truncated and release the cache |
| 6831 | // entry when DoneReading() is called before any Read() calls, such as |
| 6832 | // for a redirect. |
| 6833 | TEST(HttpCache, DoneReading) { |
| 6834 | MockHttpCache cache; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6835 | TestCompletionCallback callback; |
[email protected] | 5b2bacc2 | 2013-09-19 17:58:39 | [diff] [blame] | 6836 | |
| 6837 | ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 6838 | transaction.data = ""; |
| 6839 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6840 | scoped_ptr<HttpTransaction> trans; |
| 6841 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
[email protected] | 5b2bacc2 | 2013-09-19 17:58:39 | [diff] [blame] | 6842 | |
| 6843 | MockHttpRequest request(transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6844 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 6845 | EXPECT_EQ(OK, callback.GetResult(rv)); |
[email protected] | 5b2bacc2 | 2013-09-19 17:58:39 | [diff] [blame] | 6846 | |
| 6847 | trans->DoneReading(); |
| 6848 | // Leave the transaction around. |
| 6849 | |
| 6850 | // Make sure that the ActiveEntry is gone. |
| 6851 | base::MessageLoop::current()->RunUntilIdle(); |
| 6852 | |
| 6853 | // Read from the cache. This should not deadlock. |
| 6854 | RunTransactionTest(cache.http_cache(), transaction); |
| 6855 | |
| 6856 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6857 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6858 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6859 | } |
| 6860 | |
[email protected] | 255c044 | 2013-09-10 23:59:04 | [diff] [blame] | 6861 | // Tests that we stop caching when told. |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6862 | TEST(HttpCache, StopCachingDeletesEntry) { |
| 6863 | MockHttpCache cache; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6864 | TestCompletionCallback callback; |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6865 | MockHttpRequest request(kSimpleGET_Transaction); |
| 6866 | |
| 6867 | { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6868 | scoped_ptr<HttpTransaction> trans; |
| 6869 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6870 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6871 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 6872 | EXPECT_EQ(OK, callback.GetResult(rv)); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6873 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6874 | scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6875 | rv = trans->Read(buf.get(), 10, callback.callback()); |
[email protected] | 921ce41 | 2013-09-20 23:03:48 | [diff] [blame] | 6876 | EXPECT_EQ(10, callback.GetResult(rv)); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6877 | |
| 6878 | trans->StopCaching(); |
| 6879 | |
| 6880 | // We should be able to keep reading. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6881 | rv = trans->Read(buf.get(), 256, callback.callback()); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6882 | EXPECT_GT(callback.GetResult(rv), 0); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6883 | rv = trans->Read(buf.get(), 256, callback.callback()); |
[email protected] | 921ce41 | 2013-09-20 23:03:48 | [diff] [blame] | 6884 | EXPECT_EQ(0, callback.GetResult(rv)); |
| 6885 | } |
| 6886 | |
| 6887 | // Make sure that the ActiveEntry is gone. |
| 6888 | base::MessageLoop::current()->RunUntilIdle(); |
| 6889 | |
| 6890 | // Verify that the entry is gone. |
| 6891 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 6892 | |
| 6893 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6894 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 6895 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 6896 | } |
| 6897 | |
| 6898 | // Tests that we stop caching when told, even if DoneReading is called |
| 6899 | // after StopCaching. |
| 6900 | TEST(HttpCache, StopCachingThenDoneReadingDeletesEntry) { |
| 6901 | MockHttpCache cache; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6902 | TestCompletionCallback callback; |
[email protected] | 921ce41 | 2013-09-20 23:03:48 | [diff] [blame] | 6903 | MockHttpRequest request(kSimpleGET_Transaction); |
| 6904 | |
| 6905 | { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6906 | scoped_ptr<HttpTransaction> trans; |
| 6907 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
[email protected] | 921ce41 | 2013-09-20 23:03:48 | [diff] [blame] | 6908 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6909 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 6910 | EXPECT_EQ(OK, callback.GetResult(rv)); |
[email protected] | 921ce41 | 2013-09-20 23:03:48 | [diff] [blame] | 6911 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6912 | scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
[email protected] | 921ce41 | 2013-09-20 23:03:48 | [diff] [blame] | 6913 | rv = trans->Read(buf.get(), 10, callback.callback()); |
| 6914 | EXPECT_EQ(10, callback.GetResult(rv)); |
| 6915 | |
| 6916 | trans->StopCaching(); |
| 6917 | |
| 6918 | // We should be able to keep reading. |
| 6919 | rv = trans->Read(buf.get(), 256, callback.callback()); |
| 6920 | EXPECT_GT(callback.GetResult(rv), 0); |
| 6921 | rv = trans->Read(buf.get(), 256, callback.callback()); |
| 6922 | EXPECT_EQ(0, callback.GetResult(rv)); |
| 6923 | |
| 6924 | // We should be able to call DoneReading. |
| 6925 | trans->DoneReading(); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6926 | } |
| 6927 | |
| 6928 | // Make sure that the ActiveEntry is gone. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 6929 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6930 | |
| 6931 | // Verify that the entry is gone. |
| 6932 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 6933 | |
| 6934 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6935 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 6936 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 6937 | } |
| 6938 | |
[email protected] | 255c044 | 2013-09-10 23:59:04 | [diff] [blame] | 6939 | // Tests that we stop caching when told, when using auth. |
| 6940 | TEST(HttpCache, StopCachingWithAuthDeletesEntry) { |
| 6941 | MockHttpCache cache; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6942 | TestCompletionCallback callback; |
[email protected] | 255c044 | 2013-09-10 23:59:04 | [diff] [blame] | 6943 | MockTransaction mock_transaction(kSimpleGET_Transaction); |
| 6944 | mock_transaction.status = "HTTP/1.1 401 Unauthorized"; |
| 6945 | AddMockTransaction(&mock_transaction); |
| 6946 | MockHttpRequest request(mock_transaction); |
| 6947 | |
| 6948 | { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6949 | scoped_ptr<HttpTransaction> trans; |
| 6950 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
[email protected] | 255c044 | 2013-09-10 23:59:04 | [diff] [blame] | 6951 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6952 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 6953 | EXPECT_EQ(OK, callback.GetResult(rv)); |
[email protected] | 255c044 | 2013-09-10 23:59:04 | [diff] [blame] | 6954 | |
| 6955 | trans->StopCaching(); |
| 6956 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6957 | scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
[email protected] | 255c044 | 2013-09-10 23:59:04 | [diff] [blame] | 6958 | rv = trans->Read(buf.get(), 10, callback.callback()); |
| 6959 | EXPECT_EQ(callback.GetResult(rv), 10); |
| 6960 | } |
| 6961 | RemoveMockTransaction(&mock_transaction); |
| 6962 | |
| 6963 | // Make sure that the ActiveEntry is gone. |
| 6964 | base::MessageLoop::current()->RunUntilIdle(); |
| 6965 | |
| 6966 | // Verify that the entry is gone. |
| 6967 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 6968 | |
| 6969 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6970 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 6971 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 6972 | } |
| 6973 | |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6974 | // Tests that when we are told to stop caching we don't throw away valid data. |
| 6975 | TEST(HttpCache, StopCachingSavesEntry) { |
| 6976 | MockHttpCache cache; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6977 | TestCompletionCallback callback; |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6978 | MockHttpRequest request(kSimpleGET_Transaction); |
| 6979 | |
| 6980 | { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6981 | scoped_ptr<HttpTransaction> trans; |
| 6982 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6983 | |
| 6984 | // Force a response that can be resumed. |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 6985 | ScopedMockTransaction mock_transaction(kSimpleGET_Transaction); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6986 | AddMockTransaction(&mock_transaction); |
| 6987 | mock_transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 6988 | "Content-Length: 42\n" |
[email protected] | 29cc1ce4 | 2012-07-22 18:39:35 | [diff] [blame] | 6989 | "Etag: \"foo\"\n"; |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6990 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6991 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 6992 | EXPECT_EQ(OK, callback.GetResult(rv)); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6993 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 6994 | scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6995 | rv = trans->Read(buf.get(), 10, callback.callback()); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 6996 | EXPECT_EQ(callback.GetResult(rv), 10); |
| 6997 | |
| 6998 | trans->StopCaching(); |
| 6999 | |
| 7000 | // We should be able to keep reading. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 7001 | rv = trans->Read(buf.get(), 256, callback.callback()); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 7002 | EXPECT_GT(callback.GetResult(rv), 0); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 7003 | rv = trans->Read(buf.get(), 256, callback.callback()); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 7004 | EXPECT_EQ(callback.GetResult(rv), 0); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 7005 | } |
| 7006 | |
| 7007 | // Verify that the entry is marked as incomplete. |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 7008 | VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); |
[email protected] | 60d94d7 | 2011-11-18 19:54:57 | [diff] [blame] | 7009 | } |
| 7010 | |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 7011 | // Tests that we handle truncated enries when StopCaching is called. |
| 7012 | TEST(HttpCache, StopCachingTruncatedEntry) { |
| 7013 | MockHttpCache cache; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7014 | TestCompletionCallback callback; |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 7015 | MockHttpRequest request(kRangeGET_TransactionOK); |
| 7016 | request.extra_headers.Clear(); |
[email protected] | 1dce442e | 2013-04-23 03:06:29 | [diff] [blame] | 7017 | request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 7018 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 7019 | |
| 7020 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 7021 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7022 | "ETag: \"foo\"\n" |
| 7023 | "Accept-Ranges: bytes\n" |
| 7024 | "Content-Length: 80\n"); |
| 7025 | CreateTruncatedEntry(raw_headers, &cache); |
| 7026 | |
| 7027 | { |
| 7028 | // Now make a regular request. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7029 | scoped_ptr<HttpTransaction> trans; |
| 7030 | ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 7031 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7032 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 7033 | EXPECT_EQ(OK, callback.GetResult(rv)); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 7034 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7035 | scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 7036 | rv = trans->Read(buf.get(), 10, callback.callback()); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 7037 | EXPECT_EQ(callback.GetResult(rv), 10); |
| 7038 | |
| 7039 | // This is actually going to do nothing. |
| 7040 | trans->StopCaching(); |
| 7041 | |
| 7042 | // We should be able to keep reading. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 7043 | rv = trans->Read(buf.get(), 256, callback.callback()); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 7044 | EXPECT_GT(callback.GetResult(rv), 0); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 7045 | rv = trans->Read(buf.get(), 256, callback.callback()); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 7046 | EXPECT_GT(callback.GetResult(rv), 0); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 7047 | rv = trans->Read(buf.get(), 256, callback.callback()); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 7048 | EXPECT_EQ(callback.GetResult(rv), 0); |
| 7049 | } |
| 7050 | |
| 7051 | // Verify that the disk entry was updated. |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 7052 | VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80); |
[email protected] | 49abb41 | 2011-11-22 18:36:15 | [diff] [blame] | 7053 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 7054 | } |
| 7055 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 7056 | // Tests that we detect truncated resources from the net when there is |
[email protected] | c85316f | 2011-08-18 23:27:36 | [diff] [blame] | 7057 | // a Content-Length header. |
| 7058 | TEST(HttpCache, TruncatedByContentLength) { |
| 7059 | MockHttpCache cache; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7060 | TestCompletionCallback callback; |
[email protected] | c85316f | 2011-08-18 23:27:36 | [diff] [blame] | 7061 | |
| 7062 | MockTransaction transaction(kSimpleGET_Transaction); |
| 7063 | AddMockTransaction(&transaction); |
| 7064 | transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 7065 | "Content-Length: 100\n"; |
| 7066 | RunTransactionTest(cache.http_cache(), transaction); |
| 7067 | RemoveMockTransaction(&transaction); |
| 7068 | |
| 7069 | // Read from the cache. |
| 7070 | RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 7071 | |
| 7072 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 7073 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 7074 | EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 7075 | } |
| 7076 | |
| 7077 | // Tests that we actually flag entries as truncated when we detect an error |
| 7078 | // from the net. |
| 7079 | TEST(HttpCache, TruncatedByContentLength2) { |
| 7080 | MockHttpCache cache; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7081 | TestCompletionCallback callback; |
[email protected] | c85316f | 2011-08-18 23:27:36 | [diff] [blame] | 7082 | |
| 7083 | MockTransaction transaction(kSimpleGET_Transaction); |
| 7084 | AddMockTransaction(&transaction); |
| 7085 | transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 7086 | "Content-Length: 100\n" |
[email protected] | 29cc1ce4 | 2012-07-22 18:39:35 | [diff] [blame] | 7087 | "Etag: \"foo\"\n"; |
[email protected] | c85316f | 2011-08-18 23:27:36 | [diff] [blame] | 7088 | RunTransactionTest(cache.http_cache(), transaction); |
| 7089 | RemoveMockTransaction(&transaction); |
| 7090 | |
| 7091 | // Verify that the entry is marked as incomplete. |
rvargas | 1c7570e | 2015-09-17 23:05:45 | [diff] [blame] | 7092 | VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); |
[email protected] | c85316f | 2011-08-18 23:27:36 | [diff] [blame] | 7093 | } |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 7094 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7095 | // Make sure that calling SetPriority on a cache transaction passes on |
| 7096 | // its priority updates to its underlying network transaction. |
| 7097 | TEST(HttpCache, SetPriority) { |
| 7098 | MockHttpCache cache; |
| 7099 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7100 | scoped_ptr<HttpTransaction> trans; |
| 7101 | ASSERT_EQ(OK, cache.http_cache()->CreateTransaction(IDLE, &trans)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7102 | |
| 7103 | // Shouldn't crash, but doesn't do anything either. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7104 | trans->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7105 | |
[email protected] | 8ee0492 | 2013-06-07 09:05:12 | [diff] [blame] | 7106 | EXPECT_FALSE(cache.network_layer()->last_transaction()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7107 | EXPECT_EQ(DEFAULT_PRIORITY, |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7108 | cache.network_layer()->last_create_transaction_priority()); |
| 7109 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7110 | HttpRequestInfo info; |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7111 | info.url = GURL(kSimpleGET_Transaction.url); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7112 | TestCompletionCallback callback; |
| 7113 | EXPECT_EQ(ERR_IO_PENDING, |
| 7114 | trans->Start(&info, callback.callback(), BoundNetLog())); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7115 | |
[email protected] | 7ffdea0 | 2013-06-26 19:00:38 | [diff] [blame] | 7116 | EXPECT_TRUE(cache.network_layer()->last_transaction()); |
| 7117 | if (cache.network_layer()->last_transaction()) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7118 | EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority()); |
| 7119 | EXPECT_EQ(LOW, cache.network_layer()->last_transaction()->priority()); |
[email protected] | 7ffdea0 | 2013-06-26 19:00:38 | [diff] [blame] | 7120 | } |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7121 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7122 | trans->SetPriority(HIGHEST); |
[email protected] | 7ffdea0 | 2013-06-26 19:00:38 | [diff] [blame] | 7123 | |
| 7124 | if (cache.network_layer()->last_transaction()) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7125 | EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority()); |
| 7126 | EXPECT_EQ(HIGHEST, cache.network_layer()->last_transaction()->priority()); |
[email protected] | 7ffdea0 | 2013-06-26 19:00:38 | [diff] [blame] | 7127 | } |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7128 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7129 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7130 | } |
| 7131 | |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 7132 | // Make sure that calling SetWebSocketHandshakeStreamCreateHelper on a cache |
| 7133 | // transaction passes on its argument to the underlying network transaction. |
| 7134 | TEST(HttpCache, SetWebSocketHandshakeStreamCreateHelper) { |
| 7135 | MockHttpCache cache; |
| 7136 | |
| 7137 | FakeWebSocketHandshakeStreamCreateHelper create_helper; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7138 | scoped_ptr<HttpTransaction> trans; |
| 7139 | ASSERT_EQ(OK, cache.http_cache()->CreateTransaction(IDLE, &trans)); |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 7140 | |
| 7141 | EXPECT_FALSE(cache.network_layer()->last_transaction()); |
| 7142 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7143 | HttpRequestInfo info; |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 7144 | info.url = GURL(kSimpleGET_Transaction.url); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7145 | TestCompletionCallback callback; |
| 7146 | EXPECT_EQ(ERR_IO_PENDING, |
| 7147 | trans->Start(&info, callback.callback(), BoundNetLog())); |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 7148 | |
| 7149 | ASSERT_TRUE(cache.network_layer()->last_transaction()); |
| 7150 | EXPECT_FALSE(cache.network_layer()->last_transaction()-> |
| 7151 | websocket_handshake_stream_create_helper()); |
| 7152 | trans->SetWebSocketHandshakeStreamCreateHelper(&create_helper); |
| 7153 | EXPECT_EQ(&create_helper, |
| 7154 | cache.network_layer()->last_transaction()-> |
| 7155 | websocket_handshake_stream_create_helper()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7156 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 7157 | } |
| 7158 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7159 | // Make sure that a cache transaction passes on its priority to |
| 7160 | // newly-created network transactions. |
| 7161 | TEST(HttpCache, SetPriorityNewTransaction) { |
| 7162 | MockHttpCache cache; |
| 7163 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 7164 | |
| 7165 | std::string raw_headers("HTTP/1.1 200 OK\n" |
| 7166 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7167 | "ETag: \"foo\"\n" |
| 7168 | "Accept-Ranges: bytes\n" |
| 7169 | "Content-Length: 80\n"); |
| 7170 | CreateTruncatedEntry(raw_headers, &cache); |
| 7171 | |
| 7172 | // Now make a regular request. |
| 7173 | std::string headers; |
| 7174 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 7175 | transaction.request_headers = EXTRA_HEADER; |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 7176 | transaction.data = kFullRangeData; |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7177 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7178 | scoped_ptr<HttpTransaction> trans; |
| 7179 | ASSERT_EQ(OK, cache.http_cache()->CreateTransaction(MEDIUM, &trans)); |
| 7180 | EXPECT_EQ(DEFAULT_PRIORITY, |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7181 | cache.network_layer()->last_create_transaction_priority()); |
| 7182 | |
| 7183 | MockHttpRequest info(transaction); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7184 | TestCompletionCallback callback; |
| 7185 | EXPECT_EQ(ERR_IO_PENDING, |
| 7186 | trans->Start(&info, callback.callback(), BoundNetLog())); |
| 7187 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7188 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7189 | EXPECT_EQ(MEDIUM, cache.network_layer()->last_create_transaction_priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7190 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7191 | trans->SetPriority(HIGHEST); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7192 | // Should trigger a new network transaction and pick up the new |
| 7193 | // priority. |
| 7194 | ReadAndVerifyTransaction(trans.get(), transaction); |
| 7195 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7196 | EXPECT_EQ(HIGHEST, cache.network_layer()->last_create_transaction_priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7197 | |
| 7198 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 7199 | } |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7200 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7201 | namespace { |
| 7202 | |
| 7203 | void RunTransactionAndGetNetworkBytes(MockHttpCache& cache, |
| 7204 | const MockTransaction& trans_info, |
| 7205 | int64_t* sent_bytes, |
| 7206 | int64_t* received_bytes) { |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 7207 | RunTransactionTestBase(cache.http_cache(), trans_info, |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7208 | MockHttpRequest(trans_info), nullptr, BoundNetLog(), |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 7209 | nullptr, sent_bytes, received_bytes, nullptr); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7210 | } |
| 7211 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7212 | } // namespace |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7213 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7214 | TEST(HttpCache, NetworkBytesCacheMissAndThenHit) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7215 | MockHttpCache cache; |
| 7216 | |
| 7217 | MockTransaction transaction(kSimpleGET_Transaction); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7218 | int64_t sent, received; |
| 7219 | RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
| 7220 | EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); |
| 7221 | EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7222 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7223 | RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
| 7224 | EXPECT_EQ(0, sent); |
| 7225 | EXPECT_EQ(0, received); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7226 | } |
| 7227 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7228 | TEST(HttpCache, NetworkBytesConditionalRequest304) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7229 | MockHttpCache cache; |
| 7230 | |
| 7231 | ScopedMockTransaction transaction(kETagGET_Transaction); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7232 | int64_t sent, received; |
| 7233 | RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
| 7234 | EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); |
| 7235 | EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7236 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7237 | transaction.load_flags = LOAD_VALIDATE_CACHE; |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7238 | transaction.handler = ETagGet_ConditionalRequest_Handler; |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7239 | RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
| 7240 | EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); |
| 7241 | EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7242 | } |
| 7243 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7244 | TEST(HttpCache, NetworkBytesConditionalRequest200) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7245 | MockHttpCache cache; |
| 7246 | |
| 7247 | MockTransaction transaction(kTypicalGET_Transaction); |
| 7248 | transaction.request_headers = "Foo: bar\r\n"; |
| 7249 | transaction.response_headers = |
| 7250 | "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
| 7251 | "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 7252 | "Etag: \"foopy\"\n" |
| 7253 | "Cache-Control: max-age=0\n" |
| 7254 | "Vary: Foo\n"; |
| 7255 | AddMockTransaction(&transaction); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7256 | int64_t sent, received; |
| 7257 | RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
| 7258 | EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); |
| 7259 | EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7260 | |
| 7261 | RevalidationServer server; |
| 7262 | transaction.handler = server.Handler; |
| 7263 | transaction.request_headers = "Foo: none\r\n"; |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7264 | RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
| 7265 | EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); |
| 7266 | EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7267 | |
| 7268 | RemoveMockTransaction(&transaction); |
| 7269 | } |
| 7270 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7271 | TEST(HttpCache, NetworkBytesRange) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7272 | MockHttpCache cache; |
| 7273 | AddMockTransaction(&kRangeGET_TransactionOK); |
| 7274 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 7275 | |
| 7276 | // Read bytes 40-49 from the network. |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7277 | int64_t sent, received; |
| 7278 | RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
| 7279 | EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); |
| 7280 | EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7281 | |
| 7282 | // Read bytes 40-49 from the cache. |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7283 | RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
| 7284 | EXPECT_EQ(0, sent); |
| 7285 | EXPECT_EQ(0, received); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7286 | base::MessageLoop::current()->RunUntilIdle(); |
| 7287 | |
| 7288 | // Read bytes 30-39 from the network. |
| 7289 | transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; |
| 7290 | transaction.data = "rg: 30-39 "; |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7291 | RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
| 7292 | EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); |
| 7293 | EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7294 | base::MessageLoop::current()->RunUntilIdle(); |
| 7295 | |
| 7296 | // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. |
| 7297 | transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
| 7298 | transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7299 | RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); |
| 7300 | EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes * 2, sent); |
| 7301 | EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes * 2, received); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 7302 | |
| 7303 | RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 7304 | } |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7305 | |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7306 | class HttpCachePrefetchValidationTest : public ::testing::Test { |
| 7307 | protected: |
| 7308 | static const int kMaxAgeSecs = 100; |
| 7309 | static const int kRequireValidationSecs = kMaxAgeSecs + 1; |
| 7310 | |
| 7311 | HttpCachePrefetchValidationTest() : transaction_(kSimpleGET_Transaction) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7312 | DCHECK_LT(kMaxAgeSecs, prefetch_reuse_mins() * kNumSecondsPerMinute); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7313 | |
| 7314 | clock_ = new base::SimpleTestClock(); |
| 7315 | cache_.http_cache()->SetClockForTesting(make_scoped_ptr(clock_)); |
| 7316 | cache_.network_layer()->SetClock(clock_); |
| 7317 | |
| 7318 | transaction_.response_headers = "Cache-Control: max-age=100\n"; |
| 7319 | } |
| 7320 | |
| 7321 | bool TransactionRequiredNetwork(int load_flags) { |
| 7322 | int pre_transaction_count = transaction_count(); |
| 7323 | transaction_.load_flags = load_flags; |
| 7324 | RunTransactionTest(cache_.http_cache(), transaction_); |
| 7325 | return pre_transaction_count != transaction_count(); |
| 7326 | } |
| 7327 | |
| 7328 | void AdvanceTime(int seconds) { |
| 7329 | clock_->Advance(base::TimeDelta::FromSeconds(seconds)); |
| 7330 | } |
| 7331 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7332 | int prefetch_reuse_mins() { return HttpCache::kPrefetchReuseMins; } |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7333 | |
| 7334 | // How many times this test has sent requests to the (fake) origin |
| 7335 | // server. Every test case needs to make at least one request to initialise |
| 7336 | // the cache. |
| 7337 | int transaction_count() { |
| 7338 | return cache_.network_layer()->transaction_count(); |
| 7339 | } |
| 7340 | |
| 7341 | MockHttpCache cache_; |
| 7342 | ScopedMockTransaction transaction_; |
| 7343 | std::string response_headers_; |
| 7344 | base::SimpleTestClock* clock_; |
| 7345 | }; |
| 7346 | |
| 7347 | TEST_F(HttpCachePrefetchValidationTest, SkipValidationShortlyAfterPrefetch) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7348 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7349 | AdvanceTime(kRequireValidationSecs); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7350 | EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7351 | } |
| 7352 | |
| 7353 | TEST_F(HttpCachePrefetchValidationTest, ValidateLongAfterPrefetch) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7354 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
| 7355 | AdvanceTime(prefetch_reuse_mins() * kNumSecondsPerMinute); |
| 7356 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7357 | } |
| 7358 | |
| 7359 | TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceOnly) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7360 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7361 | AdvanceTime(kRequireValidationSecs); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7362 | EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL)); |
| 7363 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7364 | } |
| 7365 | |
| 7366 | TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceReadOnly) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7367 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7368 | AdvanceTime(kRequireValidationSecs); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7369 | EXPECT_FALSE(TransactionRequiredNetwork(LOAD_ONLY_FROM_CACHE)); |
| 7370 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7371 | } |
| 7372 | |
| 7373 | TEST_F(HttpCachePrefetchValidationTest, BypassCacheOverwritesPrefetch) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7374 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7375 | AdvanceTime(kRequireValidationSecs); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7376 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_BYPASS_CACHE)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7377 | AdvanceTime(kRequireValidationSecs); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7378 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7379 | } |
| 7380 | |
| 7381 | TEST_F(HttpCachePrefetchValidationTest, |
| 7382 | SkipValidationOnExistingEntryThatNeedsValidation) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7383 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7384 | AdvanceTime(kRequireValidationSecs); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7385 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7386 | AdvanceTime(kRequireValidationSecs); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7387 | EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL)); |
| 7388 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7389 | } |
| 7390 | |
| 7391 | TEST_F(HttpCachePrefetchValidationTest, |
| 7392 | SkipValidationOnExistingEntryThatDoesNotNeedValidation) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7393 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL)); |
| 7394 | EXPECT_FALSE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7395 | AdvanceTime(kRequireValidationSecs); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7396 | EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL)); |
| 7397 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7398 | } |
| 7399 | |
| 7400 | TEST_F(HttpCachePrefetchValidationTest, PrefetchMultipleTimes) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7401 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
| 7402 | EXPECT_FALSE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7403 | AdvanceTime(kRequireValidationSecs); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7404 | EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7405 | } |
| 7406 | |
| 7407 | TEST_F(HttpCachePrefetchValidationTest, ValidateOnDelayedSecondPrefetch) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7408 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7409 | AdvanceTime(kRequireValidationSecs); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7410 | EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7411 | AdvanceTime(kRequireValidationSecs); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7412 | EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL)); |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 7413 | } |
| 7414 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7415 | static void CheckResourceFreshnessHeader(const HttpRequestInfo* request, |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7416 | std::string* response_status, |
| 7417 | std::string* response_headers, |
| 7418 | std::string* response_data) { |
| 7419 | std::string value; |
[email protected] | 7274704 | 2014-08-15 19:29:07 | [diff] [blame] | 7420 | EXPECT_TRUE(request->extra_headers.GetHeader("Resource-Freshness", &value)); |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7421 | EXPECT_EQ("max-age=3600,stale-while-revalidate=7200,age=10801", value); |
| 7422 | } |
| 7423 | |
[email protected] | 7274704 | 2014-08-15 19:29:07 | [diff] [blame] | 7424 | // Verify that the Resource-Freshness header is sent on a revalidation if the |
| 7425 | // stale-while-revalidate directive was on the response. |
ricea | b38b3f9 | 2015-06-19 13:24:07 | [diff] [blame] | 7426 | TEST(HttpCache, ResourceFreshnessHeaderSent) { |
| 7427 | MockHttpCache cache; |
| 7428 | |
| 7429 | ScopedMockTransaction stale_while_revalidate_transaction( |
| 7430 | kSimpleGET_Transaction); |
| 7431 | stale_while_revalidate_transaction.response_headers = |
| 7432 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7433 | "Age: 10801\n" |
| 7434 | "Cache-Control: max-age=3600,stale-while-revalidate=7200\n"; |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7435 | |
| 7436 | // Write to the cache. |
ricea | b38b3f9 | 2015-06-19 13:24:07 | [diff] [blame] | 7437 | RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7438 | |
ricea | b38b3f9 | 2015-06-19 13:24:07 | [diff] [blame] | 7439 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7440 | |
[email protected] | 7274704 | 2014-08-15 19:29:07 | [diff] [blame] | 7441 | // Send the request again and check that Resource-Freshness header is added. |
ricea | b38b3f9 | 2015-06-19 13:24:07 | [diff] [blame] | 7442 | stale_while_revalidate_transaction.handler = CheckResourceFreshnessHeader; |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7443 | |
ricea | b38b3f9 | 2015-06-19 13:24:07 | [diff] [blame] | 7444 | RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7445 | |
ricea | b38b3f9 | 2015-06-19 13:24:07 | [diff] [blame] | 7446 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7447 | } |
| 7448 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7449 | static void CheckResourceFreshnessAbsent(const HttpRequestInfo* request, |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7450 | std::string* response_status, |
| 7451 | std::string* response_headers, |
| 7452 | std::string* response_data) { |
[email protected] | 7274704 | 2014-08-15 19:29:07 | [diff] [blame] | 7453 | EXPECT_FALSE(request->extra_headers.HasHeader("Resource-Freshness")); |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7454 | } |
| 7455 | |
[email protected] | 7274704 | 2014-08-15 19:29:07 | [diff] [blame] | 7456 | // Verify that the Resource-Freshness header is not sent when |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7457 | // stale-while-revalidate is 0. |
ricea | b38b3f9 | 2015-06-19 13:24:07 | [diff] [blame] | 7458 | TEST(HttpCache, ResourceFreshnessHeaderNotSent) { |
| 7459 | MockHttpCache cache; |
| 7460 | |
| 7461 | ScopedMockTransaction stale_while_revalidate_transaction( |
| 7462 | kSimpleGET_Transaction); |
| 7463 | stale_while_revalidate_transaction.response_headers = |
| 7464 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7465 | "Age: 10801\n" |
| 7466 | "Cache-Control: max-age=3600,stale-while-revalidate=0\n"; |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7467 | |
| 7468 | // Write to the cache. |
ricea | b38b3f9 | 2015-06-19 13:24:07 | [diff] [blame] | 7469 | RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7470 | |
ricea | b38b3f9 | 2015-06-19 13:24:07 | [diff] [blame] | 7471 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7472 | |
[email protected] | 7274704 | 2014-08-15 19:29:07 | [diff] [blame] | 7473 | // Send the request again and check that Resource-Freshness header is absent. |
ricea | b38b3f9 | 2015-06-19 13:24:07 | [diff] [blame] | 7474 | stale_while_revalidate_transaction.handler = CheckResourceFreshnessAbsent; |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7475 | |
ricea | b38b3f9 | 2015-06-19 13:24:07 | [diff] [blame] | 7476 | RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7477 | |
ricea | b38b3f9 | 2015-06-19 13:24:07 | [diff] [blame] | 7478 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
[email protected] | f311c23 | 2014-07-30 11:33:56 | [diff] [blame] | 7479 | } |
dalecurtis | fa29c3e | 2014-08-26 23:40:59 | [diff] [blame] | 7480 | |
ricea | 8bad09d9 | 2015-08-24 09:01:49 | [diff] [blame] | 7481 | TEST(HttpCache, StaleContentNotUsedWhenLoadFlagNotSet) { |
| 7482 | MockHttpCache cache; |
| 7483 | |
| 7484 | ScopedMockTransaction stale_while_revalidate_transaction( |
| 7485 | kSimpleGET_Transaction); |
| 7486 | |
| 7487 | stale_while_revalidate_transaction.response_headers = |
| 7488 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7489 | "Age: 10801\n" |
| 7490 | "Cache-Control: max-age=0,stale-while-revalidate=86400\n"; |
| 7491 | |
| 7492 | // Write to the cache. |
| 7493 | RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); |
| 7494 | |
| 7495 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 7496 | |
| 7497 | // Send the request again and check that it is sent to the network again. |
| 7498 | HttpResponseInfo response_info; |
| 7499 | RunTransactionTestWithResponseInfo( |
| 7500 | cache.http_cache(), stale_while_revalidate_transaction, &response_info); |
| 7501 | |
| 7502 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 7503 | EXPECT_FALSE(response_info.async_revalidation_required); |
| 7504 | } |
| 7505 | |
| 7506 | TEST(HttpCache, StaleContentUsedWhenLoadFlagSetAndUsable) { |
| 7507 | MockHttpCache cache; |
| 7508 | |
| 7509 | ScopedMockTransaction stale_while_revalidate_transaction( |
| 7510 | kSimpleGET_Transaction); |
| 7511 | stale_while_revalidate_transaction.load_flags |= |
| 7512 | LOAD_SUPPORT_ASYNC_REVALIDATION; |
| 7513 | stale_while_revalidate_transaction.response_headers = |
| 7514 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7515 | "Age: 10801\n" |
| 7516 | "Cache-Control: max-age=0,stale-while-revalidate=86400\n"; |
| 7517 | |
| 7518 | // Write to the cache. |
| 7519 | RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); |
| 7520 | |
| 7521 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 7522 | |
| 7523 | // Send the request again and check that it is not sent to the network again. |
| 7524 | HttpResponseInfo response_info; |
| 7525 | RunTransactionTestWithResponseInfo( |
| 7526 | cache.http_cache(), stale_while_revalidate_transaction, &response_info); |
| 7527 | |
| 7528 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 7529 | EXPECT_TRUE(response_info.async_revalidation_required); |
| 7530 | } |
| 7531 | |
| 7532 | TEST(HttpCache, StaleContentNotUsedWhenUnusable) { |
| 7533 | MockHttpCache cache; |
| 7534 | |
| 7535 | ScopedMockTransaction stale_while_revalidate_transaction( |
| 7536 | kSimpleGET_Transaction); |
| 7537 | stale_while_revalidate_transaction.load_flags |= |
| 7538 | LOAD_SUPPORT_ASYNC_REVALIDATION; |
| 7539 | stale_while_revalidate_transaction.response_headers = |
| 7540 | "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7541 | "Age: 10801\n" |
| 7542 | "Cache-Control: max-age=0,stale-while-revalidate=1800\n"; |
| 7543 | |
| 7544 | // Write to the cache. |
| 7545 | RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); |
| 7546 | |
| 7547 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 7548 | |
| 7549 | // Send the request again and check that it is sent to the network again. |
| 7550 | HttpResponseInfo response_info; |
| 7551 | RunTransactionTestWithResponseInfo( |
| 7552 | cache.http_cache(), stale_while_revalidate_transaction, &response_info); |
| 7553 | |
| 7554 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 7555 | EXPECT_FALSE(response_info.async_revalidation_required); |
| 7556 | } |
| 7557 | |
dalecurtis | fa29c3e | 2014-08-26 23:40:59 | [diff] [blame] | 7558 | // Tests that we allow multiple simultaneous, non-overlapping transactions to |
| 7559 | // take place on a sparse entry. |
| 7560 | TEST(HttpCache, RangeGET_MultipleRequests) { |
| 7561 | MockHttpCache cache; |
| 7562 | |
| 7563 | // Create a transaction for bytes 0-9. |
| 7564 | MockHttpRequest request(kRangeGET_TransactionOK); |
| 7565 | MockTransaction transaction(kRangeGET_TransactionOK); |
| 7566 | transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; |
| 7567 | transaction.data = "rg: 00-09 "; |
| 7568 | AddMockTransaction(&transaction); |
| 7569 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7570 | TestCompletionCallback callback; |
| 7571 | scoped_ptr<HttpTransaction> trans; |
| 7572 | int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); |
| 7573 | EXPECT_EQ(OK, rv); |
dalecurtis | fa29c3e | 2014-08-26 23:40:59 | [diff] [blame] | 7574 | ASSERT_TRUE(trans.get()); |
| 7575 | |
| 7576 | // Start our transaction. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7577 | trans->Start(&request, callback.callback(), BoundNetLog()); |
dalecurtis | fa29c3e | 2014-08-26 23:40:59 | [diff] [blame] | 7578 | |
| 7579 | // A second transaction on a different part of the file (the default |
| 7580 | // kRangeGET_TransactionOK requests 40-49) should not be blocked by |
| 7581 | // the already pending transaction. |
| 7582 | RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 7583 | |
| 7584 | // Let the first transaction complete. |
| 7585 | callback.WaitForResult(); |
| 7586 | |
| 7587 | RemoveMockTransaction(&transaction); |
| 7588 | } |
yhirano | e7d9adc | 2014-10-14 19:42:20 | [diff] [blame] | 7589 | |
| 7590 | // Makes sure that a request stops using the cache when the response headers |
| 7591 | // with "Cache-Control: no-store" arrives. That means that another request for |
| 7592 | // the same URL can be processed before the response body of the original |
| 7593 | // request arrives. |
| 7594 | TEST(HttpCache, NoStoreResponseShouldNotBlockFollowingRequests) { |
| 7595 | MockHttpCache cache; |
| 7596 | ScopedMockTransaction mock_transaction(kSimpleGET_Transaction); |
| 7597 | mock_transaction.response_headers = "Cache-Control: no-store\n"; |
| 7598 | MockHttpRequest request(mock_transaction); |
| 7599 | |
| 7600 | scoped_ptr<Context> first(new Context); |
| 7601 | first->result = cache.CreateTransaction(&first->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7602 | ASSERT_EQ(OK, first->result); |
| 7603 | EXPECT_EQ(LOAD_STATE_IDLE, first->trans->GetLoadState()); |
| 7604 | first->result = |
| 7605 | first->trans->Start(&request, first->callback.callback(), BoundNetLog()); |
| 7606 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, first->trans->GetLoadState()); |
yhirano | e7d9adc | 2014-10-14 19:42:20 | [diff] [blame] | 7607 | |
| 7608 | base::MessageLoop::current()->RunUntilIdle(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7609 | EXPECT_EQ(LOAD_STATE_IDLE, first->trans->GetLoadState()); |
yhirano | e7d9adc | 2014-10-14 19:42:20 | [diff] [blame] | 7610 | ASSERT_TRUE(first->trans->GetResponseInfo()); |
| 7611 | EXPECT_TRUE(first->trans->GetResponseInfo()->headers->HasHeaderValue( |
| 7612 | "Cache-Control", "no-store")); |
| 7613 | // Here we have read the response header but not read the response body yet. |
| 7614 | |
| 7615 | // Let us create the second (read) transaction. |
| 7616 | scoped_ptr<Context> second(new Context); |
| 7617 | second->result = cache.CreateTransaction(&second->trans); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7618 | ASSERT_EQ(OK, second->result); |
| 7619 | EXPECT_EQ(LOAD_STATE_IDLE, second->trans->GetLoadState()); |
| 7620 | second->result = second->trans->Start(&request, second->callback.callback(), |
| 7621 | BoundNetLog()); |
yhirano | e7d9adc | 2014-10-14 19:42:20 | [diff] [blame] | 7622 | |
| 7623 | // Here the second transaction proceeds without reading the first body. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7624 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); |
yhirano | e7d9adc | 2014-10-14 19:42:20 | [diff] [blame] | 7625 | base::MessageLoop::current()->RunUntilIdle(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7626 | EXPECT_EQ(LOAD_STATE_IDLE, second->trans->GetLoadState()); |
yhirano | e7d9adc | 2014-10-14 19:42:20 | [diff] [blame] | 7627 | ASSERT_TRUE(second->trans->GetResponseInfo()); |
| 7628 | EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( |
| 7629 | "Cache-Control", "no-store")); |
| 7630 | ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); |
| 7631 | } |
davidben | 2ec0ed34 | 2015-06-08 21:17:28 | [diff] [blame] | 7632 | |
| 7633 | // Tests that serving a response entirely from cache replays the previous |
| 7634 | // SSLInfo. |
| 7635 | TEST(HttpCache, CachePreservesSSLInfo) { |
| 7636 | static const uint16_t kTLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0xc02f; |
| 7637 | int status = 0; |
| 7638 | SSLConnectionStatusSetCipherSuite(kTLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 7639 | &status); |
| 7640 | SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2, &status); |
| 7641 | |
| 7642 | scoped_refptr<X509Certificate> cert = |
| 7643 | ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem"); |
| 7644 | |
| 7645 | MockHttpCache cache; |
| 7646 | |
| 7647 | ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 7648 | transaction.cert = cert; |
| 7649 | transaction.ssl_connection_status = status; |
| 7650 | |
| 7651 | // Fetch the resource. |
| 7652 | HttpResponseInfo response_info; |
| 7653 | RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
| 7654 | &response_info); |
| 7655 | |
| 7656 | // The request should have hit the network and a cache entry created. |
| 7657 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 7658 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 7659 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 7660 | |
| 7661 | // The expected SSL state was reported. |
| 7662 | EXPECT_EQ(transaction.ssl_connection_status, |
| 7663 | response_info.ssl_info.connection_status); |
| 7664 | EXPECT_TRUE(cert->Equals(response_info.ssl_info.cert.get())); |
| 7665 | |
| 7666 | // Fetch the resource again. |
| 7667 | RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
| 7668 | &response_info); |
| 7669 | |
| 7670 | // The request should have been reused without hitting the network. |
| 7671 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 7672 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 7673 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 7674 | |
| 7675 | // The SSL state was preserved. |
| 7676 | EXPECT_EQ(status, response_info.ssl_info.connection_status); |
| 7677 | EXPECT_TRUE(cert->Equals(response_info.ssl_info.cert.get())); |
| 7678 | } |
| 7679 | |
| 7680 | // Tests that SSLInfo gets updated when revalidating a cached response. |
| 7681 | TEST(HttpCache, RevalidationUpdatesSSLInfo) { |
| 7682 | static const uint16_t kTLS_RSA_WITH_RC4_128_MD5 = 0x0004; |
| 7683 | static const uint16_t kTLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0xc02f; |
| 7684 | |
| 7685 | int status1 = 0; |
| 7686 | SSLConnectionStatusSetCipherSuite(kTLS_RSA_WITH_RC4_128_MD5, &status1); |
| 7687 | SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1, &status1); |
| 7688 | int status2 = 0; |
| 7689 | SSLConnectionStatusSetCipherSuite(kTLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 7690 | &status2); |
| 7691 | SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2, &status2); |
| 7692 | |
| 7693 | scoped_refptr<X509Certificate> cert1 = |
| 7694 | ImportCertFromFile(GetTestCertsDirectory(), "expired_cert.pem"); |
| 7695 | scoped_refptr<X509Certificate> cert2 = |
| 7696 | ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem"); |
| 7697 | |
| 7698 | MockHttpCache cache; |
| 7699 | |
| 7700 | ScopedMockTransaction transaction(kTypicalGET_Transaction); |
| 7701 | transaction.cert = cert1; |
| 7702 | transaction.ssl_connection_status = status1; |
| 7703 | |
| 7704 | // Fetch the resource. |
| 7705 | HttpResponseInfo response_info; |
| 7706 | RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
| 7707 | &response_info); |
| 7708 | |
| 7709 | // The request should have hit the network and a cache entry created. |
| 7710 | EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 7711 | EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 7712 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 7713 | EXPECT_FALSE(response_info.was_cached); |
| 7714 | |
| 7715 | // The expected SSL state was reported. |
| 7716 | EXPECT_EQ(status1, response_info.ssl_info.connection_status); |
| 7717 | EXPECT_TRUE(cert1->Equals(response_info.ssl_info.cert.get())); |
| 7718 | |
| 7719 | // The server deploys a more modern configuration but reports 304 on the |
| 7720 | // revalidation attempt. |
| 7721 | transaction.status = "HTTP/1.1 304 Not Modified"; |
| 7722 | transaction.cert = cert2; |
| 7723 | transaction.ssl_connection_status = status2; |
| 7724 | |
| 7725 | // Fetch the resource again, forcing a revalidation. |
| 7726 | transaction.request_headers = "Cache-Control: max-age=0\r\n"; |
| 7727 | RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
| 7728 | &response_info); |
| 7729 | |
| 7730 | // The request should have been successfully revalidated. |
| 7731 | EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 7732 | EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 7733 | EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 7734 | EXPECT_TRUE(response_info.was_cached); |
| 7735 | |
| 7736 | // The new SSL state is reported. |
| 7737 | EXPECT_EQ(status2, response_info.ssl_info.connection_status); |
| 7738 | EXPECT_TRUE(cert2->Equals(response_info.ssl_info.cert.get())); |
| 7739 | } |
| 7740 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7741 | } // namespace net |