blob: 4d0b81d318fcd30d74c0ef2219892de7f69f15a8 [file] [log] [blame]
[email protected]93fe75162012-02-09 21:51:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/http/http_cache.h"
6
sclittlefb249892015-09-10 21:33:227#include <stdint.h>
8
[email protected]c47c0372014-03-12 23:07:029#include <algorithm>
olli.raula6df48b2a2015-11-26 07:40:2210#include <vector>
[email protected]c47c0372014-03-12 23:07:0211
[email protected]f98ead62011-10-20 01:24:2012#include "base/bind.h"
[email protected]2a65aceb82011-12-19 20:59:2713#include "base/bind_helpers.h"
Avi Drissman13fc8932015-12-20 04:40:4614#include "base/macros.h"
olli.raula6df48b2a2015-11-26 07:40:2215#include "base/memory/scoped_ptr.h"
[email protected]18b577412013-07-18 04:19:1516#include "base/message_loop/message_loop.h"
ricea64c07d792014-10-08 03:37:0017#include "base/run_loop.h"
[email protected]125ef482013-06-11 18:32:4718#include "base/strings/string_util.h"
19#include "base/strings/stringprintf.h"
jkarlinfb1d5172015-01-12 14:10:2920#include "base/test/simple_test_clock.h"
[email protected]cfc076ec2009-11-07 02:27:2321#include "net/base/cache_type.h"
mmenkecbc2b712014-10-09 20:29:0722#include "net/base/elements_upload_data_stream.h"
[email protected]6d81b482011-02-22 19:47:1923#include "net/base/host_port_pair.h"
ttuttled9dbc652015-09-29 20:00:5924#include "net/base/ip_endpoint.h"
initial.commit586acc5fe2008-07-26 22:42:5225#include "net/base/load_flags.h"
[email protected]3b23a222013-05-15 21:33:2526#include "net/base/load_timing_info.h"
27#include "net/base/load_timing_info_test_util.h"
[email protected]d8eb84242010-09-25 02:25:0628#include "net/base/net_errors.h"
davidben2ec0ed342015-06-08 21:17:2829#include "net/base/test_data_directory.h"
[email protected]b2d26cfd2012-12-11 10:36:0630#include "net/base/upload_bytes_element_reader.h"
[email protected]6e7845ae2013-03-29 21:48:1131#include "net/cert/cert_status_flags.h"
davidben2ec0ed342015-06-08 21:17:2832#include "net/cert/x509_certificate.h"
initial.commit586acc5fe2008-07-26 22:42:5233#include "net/disk_cache/disk_cache.h"
[email protected]8bf26f49a2009-06-12 17:35:5034#include "net/http/http_byte_range.h"
jkarlinfb1d5172015-01-12 14:10:2935#include "net/http/http_cache_transaction.h"
[email protected]8c76ae22010-04-20 22:15:4336#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5237#include "net/http/http_request_info.h"
[email protected]95792eb12009-06-22 21:30:4038#include "net/http/http_response_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5239#include "net/http/http_response_info.h"
40#include "net/http/http_transaction.h"
[email protected]c41737d2014-05-14 07:47:1941#include "net/http/http_transaction_test_util.h"
[email protected]8bf26f49a2009-06-12 17:35:5042#include "net/http/http_util.h"
[email protected]f40156002011-11-22 21:19:0843#include "net/http/mock_http_cache.h"
mmenke16a7cbdd2015-04-24 23:00:5644#include "net/log/test_net_log.h"
mmenke43758e62015-05-04 21:09:4645#include "net/log/test_net_log_entry.h"
46#include "net/log/test_net_log_util.h"
[email protected]7e841a52013-11-22 09:04:2147#include "net/socket/client_socket_handle.h"
[email protected]536fd0b2013-03-14 17:41:5748#include "net/ssl/ssl_cert_request_info.h"
davidben2ec0ed342015-06-08 21:17:2849#include "net/ssl/ssl_connection_status_flags.h"
50#include "net/test/cert_test_util.h"
[email protected]831e4a32013-11-14 02:14:4451#include "net/websockets/websocket_handshake_stream_base.h"
initial.commit586acc5fe2008-07-26 22:42:5252#include "testing/gtest/include/gtest/gtest.h"
53
[email protected]e1acf6f2008-10-27 20:43:3354using base::Time;
55
ttuttle859dc7a2015-04-23 19:42:2956namespace net {
57
initial.commit586acc5fe2008-07-26 22:42:5258namespace {
59
[email protected]3b23a222013-05-15 21:33:2560// Tests the load timing values of a request that goes through a
61// MockNetworkTransaction.
ttuttle859dc7a2015-04-23 19:42:2962void TestLoadTimingNetworkRequest(const LoadTimingInfo& load_timing_info) {
[email protected]3b23a222013-05-15 21:33:2563 EXPECT_FALSE(load_timing_info.socket_reused);
ttuttle859dc7a2015-04-23 19:42:2964 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]3b23a222013-05-15 21:33:2565
66 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
67 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
68
ttuttle859dc7a2015-04-23 19:42:2969 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
70 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY);
[email protected]3b23a222013-05-15 21:33:2571 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.
ttuttle859dc7a2015-04-23 19:42:2983void TestLoadTimingCachedResponse(const LoadTimingInfo& load_timing_info) {
[email protected]3b23a222013-05-15 21:33:2584 EXPECT_FALSE(load_timing_info.socket_reused);
ttuttle859dc7a2015-04-23 19:42:2985 EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]3b23a222013-05-15 21:33:2586
87 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
88 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
89
ttuttle859dc7a2015-04-23 19:42:2990 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
[email protected]3b23a222013-05-15 21:33:2591
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
ttuttle859dc7a2015-04-23 19:42:29103class DeleteCacheCompletionCallback : public TestCompletionCallbackBase {
[email protected]6a9c55c2010-08-21 02:04:08104 public:
[email protected]2a65aceb82011-12-19 20:59:27105 explicit DeleteCacheCompletionCallback(MockHttpCache* cache)
106 : cache_(cache),
[email protected]aa249b52013-04-30 01:04:32107 callback_(base::Bind(&DeleteCacheCompletionCallback::OnComplete,
108 base::Unretained(this))) {
[email protected]2c5c9d52011-12-07 14:15:36109 }
[email protected]8ebf9b132011-12-06 19:11:51110
ttuttle859dc7a2015-04-23 19:42:29111 const CompletionCallback& callback() const { return callback_; }
[email protected]2a65aceb82011-12-19 20:59:27112
[email protected]6a9c55c2010-08-21 02:04:08113 private:
[email protected]2a65aceb82011-12-19 20:59:27114 void OnComplete(int result) {
115 delete cache_;
116 SetResult(result);
117 }
118
[email protected]6a9c55c2010-08-21 02:04:08119 MockHttpCache* cache_;
ttuttle859dc7a2015-04-23 19:42:29120 CompletionCallback callback_;
[email protected]2a65aceb82011-12-19 20:59:27121
122 DISALLOW_COPY_AND_ASSIGN(DeleteCacheCompletionCallback);
[email protected]6a9c55c2010-08-21 02:04:08123};
124
initial.commit586acc5fe2008-07-26 22:42:52125//-----------------------------------------------------------------------------
126// helpers
127
ttuttle859dc7a2015-04-23 19:42:29128void ReadAndVerifyTransaction(HttpTransaction* trans,
initial.commit586acc5fe2008-07-26 22:42:52129 const MockTransaction& trans_info) {
130 std::string content;
131 int rv = ReadTransaction(trans, &content);
132
ttuttle859dc7a2015-04-23 19:42:29133 EXPECT_EQ(OK, rv);
[email protected]bded84c2009-07-23 00:36:06134 std::string expected(trans_info.data);
135 EXPECT_EQ(expected, content);
initial.commit586acc5fe2008-07-26 22:42:52136}
137
ttuttle859dc7a2015-04-23 19:42:29138void RunTransactionTestBase(HttpCache* cache,
[email protected]027bd85a2013-12-27 22:39:10139 const MockTransaction& trans_info,
140 const MockHttpRequest& request,
ttuttle859dc7a2015-04-23 19:42:29141 HttpResponseInfo* response_info,
142 const BoundNetLog& net_log,
143 LoadTimingInfo* load_timing_info,
sclittlefb249892015-09-10 21:33:22144 int64_t* sent_bytes,
ttuttled9dbc652015-09-29 20:00:59145 int64_t* received_bytes,
146 IPEndPoint* remote_endpoint) {
ttuttle859dc7a2015-04-23 19:42:29147 TestCompletionCallback callback;
initial.commit586acc5fe2008-07-26 22:42:52148
149 // write to the cache
150
ttuttle859dc7a2015-04-23 19:42:29151 scoped_ptr<HttpTransaction> trans;
152 int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &trans);
153 EXPECT_EQ(OK, rv);
[email protected]af4876d2008-10-21 23:10:57154 ASSERT_TRUE(trans.get());
initial.commit586acc5fe2008-07-26 22:42:52155
[email protected]49639fa2011-12-20 23:22:41156 rv = trans->Start(&request, callback.callback(), net_log);
ttuttle859dc7a2015-04-23 19:42:29157 if (rv == ERR_IO_PENDING)
initial.commit586acc5fe2008-07-26 22:42:52158 rv = callback.WaitForResult();
[email protected]2ef5d00e2013-03-23 16:17:27159 ASSERT_EQ(trans_info.return_code, rv);
160
ttuttle859dc7a2015-04-23 19:42:29161 if (OK != rv)
[email protected]2ef5d00e2013-03-23 16:17:27162 return;
initial.commit586acc5fe2008-07-26 22:42:52163
ttuttle859dc7a2015-04-23 19:42:29164 const HttpResponseInfo* response = trans->GetResponseInfo();
initial.commit586acc5fe2008-07-26 22:42:52165 ASSERT_TRUE(response);
166
[email protected]207d58c72009-09-04 18:59:29167 if (response_info)
168 *response_info = *response;
[email protected]95792eb12009-06-22 21:30:40169
[email protected]3b23a222013-05-15 21:33:25170 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());
ttuttle859dc7a2015-04-23 19:42:29174 *load_timing_info = LoadTimingInfo();
[email protected]3b23a222013-05-15 21:33:25175 trans->GetLoadTimingInfo(load_timing_info);
176 }
177
ttuttled9dbc652015-09-29 20:00:59178 if (remote_endpoint)
179 ASSERT_TRUE(trans->GetRemoteEndpoint(remote_endpoint));
180
[email protected]af4876d2008-10-21 23:10:57181 ReadAndVerifyTransaction(trans.get(), trans_info);
[email protected]b8015c42013-12-24 15:18:19182
sclittlefb249892015-09-10 21:33:22183 if (sent_bytes)
184 *sent_bytes = trans->GetTotalSentBytes();
[email protected]b8015c42013-12-24 15:18:19185 if (received_bytes)
186 *received_bytes = trans->GetTotalReceivedBytes();
initial.commit586acc5fe2008-07-26 22:42:52187}
188
ttuttle859dc7a2015-04-23 19:42:29189void RunTransactionTestWithRequest(HttpCache* cache,
[email protected]baff44a2009-09-06 00:48:10190 const MockTransaction& trans_info,
191 const MockHttpRequest& request,
ttuttle859dc7a2015-04-23 19:42:29192 HttpResponseInfo* response_info) {
[email protected]027bd85a2013-12-27 22:39:10193 RunTransactionTestBase(cache, trans_info, request, response_info,
ttuttled9dbc652015-09-29 20:00:59194 BoundNetLog(), nullptr, nullptr, nullptr, nullptr);
[email protected]baff44a2009-09-06 00:48:10195}
196
ttuttle859dc7a2015-04-23 19:42:29197void RunTransactionTestAndGetTiming(HttpCache* cache,
[email protected]5a07c192012-07-30 20:18:22198 const MockTransaction& trans_info,
ttuttle859dc7a2015-04-23 19:42:29199 const BoundNetLog& log,
200 LoadTimingInfo* load_timing_info) {
[email protected]027bd85a2013-12-27 22:39:10201 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info),
ttuttled9dbc652015-09-29 20:00:59202 nullptr, log, load_timing_info, nullptr, nullptr,
203 nullptr);
204}
205
206void 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]baff44a2009-09-06 00:48:10215}
216
ttuttle859dc7a2015-04-23 19:42:29217void RunTransactionTest(HttpCache* cache, const MockTransaction& trans_info) {
sclittlefb249892015-09-10 21:33:22218 RunTransactionTestAndGetTiming(cache, trans_info, BoundNetLog(), nullptr);
[email protected]95792eb12009-06-22 21:30:40219}
220
ttuttle859dc7a2015-04-23 19:42:29221void RunTransactionTestWithLog(HttpCache* cache,
rvargas80059b32015-01-02 23:39:52222 const MockTransaction& trans_info,
ttuttle859dc7a2015-04-23 19:42:29223 const BoundNetLog& log) {
sclittlefb249892015-09-10 21:33:22224 RunTransactionTestAndGetTiming(cache, trans_info, log, nullptr);
rvargas80059b32015-01-02 23:39:52225}
226
ttuttle859dc7a2015-04-23 19:42:29227void RunTransactionTestWithResponseInfo(HttpCache* cache,
[email protected]207d58c72009-09-04 18:59:29228 const MockTransaction& trans_info,
ttuttle859dc7a2015-04-23 19:42:29229 HttpResponseInfo* response) {
[email protected]027bd85a2013-12-27 22:39:10230 RunTransactionTestWithRequest(cache, trans_info, MockHttpRequest(trans_info),
231 response);
[email protected]207d58c72009-09-04 18:59:29232}
233
[email protected]3b23a222013-05-15 21:33:25234void RunTransactionTestWithResponseInfoAndGetTiming(
ttuttle859dc7a2015-04-23 19:42:29235 HttpCache* cache,
[email protected]3b23a222013-05-15 21:33:25236 const MockTransaction& trans_info,
ttuttle859dc7a2015-04-23 19:42:29237 HttpResponseInfo* response,
238 const BoundNetLog& log,
239 LoadTimingInfo* load_timing_info) {
[email protected]027bd85a2013-12-27 22:39:10240 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info),
ttuttled9dbc652015-09-29 20:00:59241 response, log, load_timing_info, nullptr, nullptr,
242 nullptr);
[email protected]3b23a222013-05-15 21:33:25243}
244
ttuttle859dc7a2015-04-23 19:42:29245void RunTransactionTestWithResponse(HttpCache* cache,
[email protected]95792eb12009-06-22 21:30:40246 const MockTransaction& trans_info,
247 std::string* response_headers) {
ttuttle859dc7a2015-04-23 19:42:29248 HttpResponseInfo response;
[email protected]207d58c72009-09-04 18:59:29249 RunTransactionTestWithResponseInfo(cache, trans_info, &response);
250 response.headers->GetNormalizedHeaders(response_headers);
[email protected]96bac982009-03-24 18:20:06251}
252
[email protected]3b23a222013-05-15 21:33:25253void RunTransactionTestWithResponseAndGetTiming(
ttuttle859dc7a2015-04-23 19:42:29254 HttpCache* cache,
[email protected]3b23a222013-05-15 21:33:25255 const MockTransaction& trans_info,
256 std::string* response_headers,
ttuttle859dc7a2015-04-23 19:42:29257 const BoundNetLog& log,
258 LoadTimingInfo* load_timing_info) {
259 HttpResponseInfo response;
[email protected]027bd85a2013-12-27 22:39:10260 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info),
ttuttled9dbc652015-09-29 20:00:59261 &response, log, load_timing_info, nullptr, nullptr,
262 nullptr);
[email protected]3b23a222013-05-15 21:33:25263 response.headers->GetNormalizedHeaders(response_headers);
264}
265
[email protected]b367d9a52009-02-27 01:02:51266// This class provides a handler for kFastNoStoreGET_Transaction so that the
267// no-store header can be included on demand.
268class FastTransactionServer {
269 public:
270 FastTransactionServer() {
271 no_store = false;
272 }
273 ~FastTransactionServer() {}
274
275 void set_no_store(bool value) { no_store = value; }
276
ttuttle859dc7a2015-04-23 19:42:29277 static void FastNoStoreHandler(const HttpRequestInfo* request,
[email protected]b367d9a52009-02-27 01:02:51278 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};
289bool FastTransactionServer::no_store;
290
291const MockTransaction kFastNoStoreGET_Transaction = {
ttuttle859dc7a2015-04-23 19:42:29292 "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,
davidben2ec0ed342015-06-08 21:17:28303 nullptr,
304 0,
ttuttle859dc7a2015-04-23 19:42:29305 0,
306 OK};
[email protected]b367d9a52009-02-27 01:02:51307
[email protected]8bf26f49a2009-06-12 17:35:50308// This class provides a handler for kRangeGET_TransactionOK so that the range
309// request can be served on demand.
310class RangeTransactionServer {
311 public:
312 RangeTransactionServer() {
[email protected]e5dad132009-08-18 00:53:41313 not_modified_ = false;
[email protected]a79837892009-08-20 21:18:29314 modified_ = false;
[email protected]fa59e6a2009-12-02 18:07:46315 bad_200_ = false;
[email protected]8bf26f49a2009-06-12 17:35:50316 }
[email protected]e5dad132009-08-18 00:53:41317 ~RangeTransactionServer() {
318 not_modified_ = false;
[email protected]a79837892009-08-20 21:18:29319 modified_ = false;
[email protected]fa59e6a2009-12-02 18:07:46320 bad_200_ = false;
[email protected]e5dad132009-08-18 00:53:41321 }
[email protected]8bf26f49a2009-06-12 17:35:50322
[email protected]a79837892009-08-20 21:18:29323 // Returns only 416 or 304 when set.
[email protected]e5dad132009-08-18 00:53:41324 void set_not_modified(bool value) { not_modified_ = value; }
[email protected]8bf26f49a2009-06-12 17:35:50325
[email protected]a79837892009-08-20 21:18:29326 // Returns 206 when revalidating a range (instead of 304).
327 void set_modified(bool value) { modified_ = value; }
328
[email protected]fa59e6a2009-12-02 18:07:46329 // Returns 200 instead of 206 (a malformed response overall).
330 void set_bad_200(bool value) { bad_200_ = value; }
331
rvargas3b57e37a2015-01-06 00:56:34332 // 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.
liberato1f59bb3f2015-05-29 14:19:10335 // X-Require-Mock-Auth-Alt -> return 401.
rvargas3b57e37a2015-01-06 00:56:34336 // X-Return-Default-Range -> assume 40-49 was requested.
liberato1f59bb3f2015-05-29 14:19:10337 // The -Alt variant doesn't cause the MockNetworkTransaction to
338 // report that it IsReadyToRestartForAuth().
ttuttle859dc7a2015-04-23 19:42:29339 static void RangeHandler(const HttpRequestInfo* request,
[email protected]8bf26f49a2009-06-12 17:35:50340 std::string* response_status,
341 std::string* response_headers,
342 std::string* response_data);
343
344 private:
[email protected]e5dad132009-08-18 00:53:41345 static bool not_modified_;
[email protected]a79837892009-08-20 21:18:29346 static bool modified_;
[email protected]fa59e6a2009-12-02 18:07:46347 static bool bad_200_;
[email protected]8bf26f49a2009-06-12 17:35:50348 DISALLOW_COPY_AND_ASSIGN(RangeTransactionServer);
349};
[email protected]e5dad132009-08-18 00:53:41350bool RangeTransactionServer::not_modified_ = false;
[email protected]a79837892009-08-20 21:18:29351bool RangeTransactionServer::modified_ = false;
[email protected]fa59e6a2009-12-02 18:07:46352bool RangeTransactionServer::bad_200_ = false;
[email protected]8bf26f49a2009-06-12 17:35:50353
[email protected]e75e8af2009-11-03 00:04:20354// A dummy extra header that must be preserved on a given request.
[email protected]1dce442e2013-04-23 03:06:29355
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]8c76ae22010-04-20 22:15:43364static const char kExtraHeaderKey[] = "Extra";
[email protected]e75e8af2009-11-03 00:04:20365
[email protected]8bf26f49a2009-06-12 17:35:50366// Static.
ttuttle859dc7a2015-04-23 19:42:29367void RangeTransactionServer::RangeHandler(const HttpRequestInfo* request,
[email protected]8bf26f49a2009-06-12 17:35:50368 std::string* response_status,
369 std::string* response_headers,
370 std::string* response_data) {
[email protected]8c76ae22010-04-20 22:15:43371 if (request->extra_headers.IsEmpty()) {
[email protected]44f873a62009-08-12 00:14:48372 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable");
[email protected]a5c9d982010-10-12 20:48:02373 response_data->clear();
[email protected]8bf26f49a2009-06-12 17:35:50374 return;
[email protected]44f873a62009-08-12 00:14:48375 }
[email protected]8bf26f49a2009-06-12 17:35:50376
[email protected]e75e8af2009-11-03 00:04:20377 // We want to make sure we don't delete extra headers.
[email protected]8c76ae22010-04-20 22:15:43378 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey));
[email protected]e75e8af2009-11-03 00:04:20379
liberato1f59bb3f2015-05-29 14:19:10380 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]d7358ba2014-01-04 01:39:49385 response_status->assign("HTTP/1.1 401 Unauthorized");
386 response_data->assign("WWW-Authenticate: Foo\n");
387 return;
388 }
389
[email protected]e5dad132009-08-18 00:53:41390 if (not_modified_) {
391 response_status->assign("HTTP/1.1 304 Not Modified");
[email protected]a5c9d982010-10-12 20:48:02392 response_data->clear();
[email protected]e5dad132009-08-18 00:53:41393 return;
394 }
395
ttuttle859dc7a2015-04-23 19:42:29396 std::vector<HttpByteRange> ranges;
[email protected]8c76ae22010-04-20 22:15:43397 std::string range_header;
ttuttle859dc7a2015-04-23 19:42:29398 if (!request->extra_headers.GetHeader(HttpRequestHeaders::kRange,
399 &range_header) ||
400 !HttpUtil::ParseRangeHeader(range_header, &ranges) || bad_200_ ||
[email protected]634739b2011-03-02 18:08:25401 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]8bf26f49a2009-06-12 17:35:50406 return;
[email protected]634739b2011-03-02 18:08:25407 }
408
[email protected]8bf26f49a2009-06-12 17:35:50409 // We can handle this range request.
ttuttle859dc7a2015-04-23 19:42:29410 HttpByteRange byte_range = ranges[0];
rvargas3b57e37a2015-01-06 00:56:34411
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]e5dad132009-08-18 00:53:41417 if (byte_range.first_byte_position() > 79) {
418 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable");
[email protected]a5c9d982010-10-12 20:48:02419 response_data->clear();
[email protected]e5dad132009-08-18 00:53:41420 return;
421 }
422
[email protected]8bf26f49a2009-06-12 17:35:50423 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]d8eb84242010-09-25 02:25:06429 std::string content_range = base::StringPrintf(
430 "Content-Range: bytes %d-%d/80\n", start, end);
[email protected]8bf26f49a2009-06-12 17:35:50431 response_headers->append(content_range);
432
[email protected]8c76ae22010-04-20 22:15:43433 if (!request->extra_headers.HasHeader("If-None-Match") || modified_) {
[email protected]44f873a62009-08-12 00:14:48434 std::string data;
[email protected]634739b2011-03-02 18:08:25435 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]a77fa2dc2010-11-15 12:11:11444 }
[email protected]8bf26f49a2009-06-12 17:35:50445 *response_data = data;
[email protected]44f873a62009-08-12 00:14:48446
447 if (end - start != 9) {
448 // We also have to fix content-length.
449 int len = end - start + 1;
[email protected]d8eb84242010-09-25 02:25:06450 std::string content_length = base::StringPrintf("Content-Length: %d\n",
451 len);
[email protected]44f873a62009-08-12 00:14:48452 response_headers->replace(response_headers->find("Content-Length:"),
453 content_length.size(), content_length);
454 }
[email protected]8bf26f49a2009-06-12 17:35:50455 } else {
456 response_status->assign("HTTP/1.1 304 Not Modified");
457 response_data->clear();
458 }
459}
460
461const MockTransaction kRangeGET_TransactionOK = {
ttuttle859dc7a2015-04-23 19:42:29462 "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,
davidben2ec0ed342015-06-08 21:17:28476 nullptr,
477 0,
ttuttle859dc7a2015-04-23 19:42:29478 0,
479 OK};
[email protected]8bf26f49a2009-06-12 17:35:50480
rvargas3b57e37a2015-01-06 00:56:34481const 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]8c76ae22010-04-20 22:15:43485// Verifies the response headers (|response|) match a partial content
[email protected]95792eb12009-06-22 21:30:40486// response for the range starting at |start| and ending at |end|.
ki.stfu144dce12015-09-22 01:07:57487void Verify206Response(const std::string& response, int start, int end) {
ttuttle859dc7a2015-04-23 19:42:29488 std::string raw_headers(
489 HttpUtil::AssembleRawHeaders(response.data(), response.size()));
490 scoped_refptr<HttpResponseHeaders> headers(
491 new HttpResponseHeaders(raw_headers));
[email protected]95792eb12009-06-22 21:30:40492
[email protected]8c76ae22010-04-20 22:15:43493 ASSERT_EQ(206, headers->response_code());
[email protected]95792eb12009-06-22 21:30:40494
Avi Drissman13fc8932015-12-20 04:40:46495 int64_t range_start, range_end, object_size;
[email protected]8c76ae22010-04-20 22:15:43496 ASSERT_TRUE(
497 headers->GetContentRange(&range_start, &range_end, &object_size));
Avi Drissman13fc8932015-12-20 04:40:46498 int64_t content_length = headers->GetContentLength();
[email protected]95792eb12009-06-22 21:30:40499
500 int length = end - start + 1;
[email protected]8c76ae22010-04-20 22:15:43501 ASSERT_EQ(length, content_length);
502 ASSERT_EQ(start, range_start);
503 ASSERT_EQ(end, range_end);
[email protected]95792eb12009-06-22 21:30:40504}
505
[email protected]634739b2011-03-02 18:08:25506// Creates a truncated entry that can be resumed using byte ranges.
507void 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
ttuttle859dc7a2015-04-23 19:42:29513 raw_headers =
514 HttpUtil::AssembleRawHeaders(raw_headers.data(), raw_headers.size());
[email protected]634739b2011-03-02 18:08:25515
ttuttle859dc7a2015-04-23 19:42:29516 HttpResponseInfo response;
[email protected]634739b2011-03-02 18:08:25517 response.response_time = base::Time::Now();
518 response.request_time = base::Time::Now();
ttuttle859dc7a2015-04-23 19:42:29519 response.headers = new HttpResponseHeaders(raw_headers);
[email protected]634739b2011-03-02 18:08:25520 // Set the last argument for this to be an incomplete request.
521 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true));
522
ttuttle859dc7a2015-04-23 19:42:29523 scoped_refptr<IOBuffer> buf(new IOBuffer(100));
[email protected]634739b2011-03-02 18:08:25524 int len = static_cast<int>(base::strlcpy(buf->data(),
525 "rg: 00-09 rg: 10-19 ", 100));
ttuttle859dc7a2015-04-23 19:42:29526 TestCompletionCallback cb;
[email protected]90499482013-06-01 00:39:50527 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true);
[email protected]634739b2011-03-02 18:08:25528 EXPECT_EQ(len, cb.GetResult(rv));
529 entry->Close();
530}
531
rvargas1c7570e2015-09-17 23:05:45532// 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).
534void 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]bded84c2009-07-23 00:36:06550// Helper to represent a network HTTP response.
551struct 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]73cae572009-10-22 18:36:19568struct Context {
ttuttle859dc7a2015-04-23 19:42:29569 Context() : result(ERR_IO_PENDING) {}
[email protected]73cae572009-10-22 18:36:19570
571 int result;
ttuttle859dc7a2015-04-23 19:42:29572 TestCompletionCallback callback;
573 scoped_ptr<HttpTransaction> trans;
[email protected]73cae572009-10-22 18:36:19574};
575
[email protected]831e4a32013-11-14 02:14:44576class FakeWebSocketHandshakeStreamCreateHelper
ttuttle859dc7a2015-04-23 19:42:29577 : public WebSocketHandshakeStreamBase::CreateHelper {
[email protected]831e4a32013-11-14 02:14:44578 public:
dchengb03027d2014-10-21 12:00:20579 ~FakeWebSocketHandshakeStreamCreateHelper() override {}
ttuttle859dc7a2015-04-23 19:42:29580 WebSocketHandshakeStreamBase* CreateBasicStream(
581 scoped_ptr<ClientSocketHandle> connect,
dchengb03027d2014-10-21 12:00:20582 bool using_proxy) override {
[email protected]831e4a32013-11-14 02:14:44583 return NULL;
584 }
ttuttle859dc7a2015-04-23 19:42:29585 WebSocketHandshakeStreamBase* CreateSpdyStream(
586 const base::WeakPtr<SpdySession>& session,
mostynbba063d6032014-10-09 11:01:13587 bool use_relative_url) override {
[email protected]831e4a32013-11-14 02:14:44588 return NULL;
589 }
590};
591
[email protected]c47c0372014-03-12 23:07:02592// 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.
mmenke43758e62015-05-04 21:09:46595bool ShouldIgnoreLogEntry(const TestNetLogEntry& entry) {
[email protected]c47c0372014-03-12 23:07:02596 switch (entry.type) {
ttuttle859dc7a2015-04-23 19:42:29597 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]c47c0372014-03-12 23:07:02603 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.
mmenke43758e62015-05-04 21:09:46611void FilterLogEntries(TestNetLogEntry::List* entries) {
[email protected]c47c0372014-03-12 23:07:02612 entries->erase(std::remove_if(entries->begin(), entries->end(),
613 &ShouldIgnoreLogEntry),
614 entries->end());
615}
616
ttuttle859dc7a2015-04-23 19:42:29617bool LogContainsEventType(const BoundTestNetLog& log,
618 NetLog::EventType expected) {
mmenke43758e62015-05-04 21:09:46619 TestNetLogEntry::List entries;
rvargas80059b32015-01-02 23:39:52620 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.commit586acc5fe2008-07-26 22:42:52628} // namespace
629
630
631//-----------------------------------------------------------------------------
[email protected]f40156002011-11-22 21:19:08632// Tests.
initial.commit586acc5fe2008-07-26 22:42:52633
initial.commit586acc5fe2008-07-26 22:42:52634TEST(HttpCache, CreateThenDestroy) {
635 MockHttpCache cache;
636
ttuttle859dc7a2015-04-23 19:42:29637 scoped_ptr<HttpTransaction> trans;
638 EXPECT_EQ(OK, cache.CreateTransaction(&trans));
[email protected]af4876d2008-10-21 23:10:57639 ASSERT_TRUE(trans.get());
initial.commit586acc5fe2008-07-26 22:42:52640}
641
[email protected]cfc076ec2009-11-07 02:27:23642TEST(HttpCache, GetBackend) {
ttuttle859dc7a2015-04-23 19:42:29643 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(0));
[email protected]cfc076ec2009-11-07 02:27:23644
[email protected]6a989032010-06-14 19:05:33645 disk_cache::Backend* backend;
ttuttle859dc7a2015-04-23 19:42:29646 TestCompletionCallback cb;
[email protected]cfc076ec2009-11-07 02:27:23647 // This will lazily initialize the backend.
[email protected]2a65aceb82011-12-19 20:59:27648 int rv = cache.http_cache()->GetBackend(&backend, cb.callback());
ttuttle859dc7a2015-04-23 19:42:29649 EXPECT_EQ(OK, cb.GetResult(rv));
[email protected]cfc076ec2009-11-07 02:27:23650}
651
initial.commit586acc5fe2008-07-26 22:42:52652TEST(HttpCache, SimpleGET) {
653 MockHttpCache cache;
ttuttle859dc7a2015-04-23 19:42:29654 BoundTestNetLog log;
655 LoadTimingInfo load_timing_info;
initial.commit586acc5fe2008-07-26 22:42:52656
[email protected]3b23a222013-05-15 21:33:25657 // Write to the cache.
658 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction,
659 log.bound(), &load_timing_info);
initial.commit586acc5fe2008-07-26 22:42:52660
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]3b23a222013-05-15 21:33:25664 TestLoadTimingNetworkRequest(load_timing_info);
initial.commit586acc5fe2008-07-26 22:42:52665}
666
667TEST(HttpCache, SimpleGETNoDiskCache) {
668 MockHttpCache cache;
669
670 cache.disk_cache()->set_fail_requests();
671
ttuttle859dc7a2015-04-23 19:42:29672 BoundTestNetLog log;
673 LoadTimingInfo load_timing_info;
[email protected]baff44a2009-09-06 00:48:10674
initial.commit586acc5fe2008-07-26 22:42:52675 // Read from the network, and don't use the cache.
[email protected]3b23a222013-05-15 21:33:25676 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction,
677 log.bound(), &load_timing_info);
[email protected]baff44a2009-09-06 00:48:10678
[email protected]9e743cd2010-03-16 07:03:53679 // Check that the NetLog was filled as expected.
[email protected]baff44a2009-09-06 00:48:10680 // (We attempted to both Open and Create entries, but both failed).
mmenke43758e62015-05-04 21:09:46681 TestNetLogEntry::List entries;
[email protected]b2fcd0e2010-12-01 15:19:40682 log.GetEntries(&entries);
[email protected]c47c0372014-03-12 23:07:02683 FilterLogEntries(&entries);
[email protected]b2fcd0e2010-12-01 15:19:40684
685 EXPECT_EQ(6u, entries.size());
ttuttle859dc7a2015-04-23 19:42:29686 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.commit586acc5fe2008-07-26 22:42:52698
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]3b23a222013-05-15 21:33:25702 TestLoadTimingNetworkRequest(load_timing_info);
initial.commit586acc5fe2008-07-26 22:42:52703}
704
[email protected]46773162010-05-07 22:31:20705TEST(HttpCache, SimpleGETNoDiskCache2) {
706 // This will initialize a cache object with NULL backend.
mmenkebc31a2c2015-10-29 13:44:45707 scoped_ptr<MockBlockingBackendFactory> factory(
708 new MockBlockingBackendFactory());
[email protected]f8702522010-05-12 18:40:10709 factory->set_fail(true);
710 factory->FinishCreation(); // We'll complete synchronously.
mmenkebc31a2c2015-10-29 13:44:45711 MockHttpCache cache(factory.Pass());
[email protected]46773162010-05-07 22:31:20712
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]6a989032010-06-14 19:05:33717 EXPECT_FALSE(cache.http_cache()->GetCurrentBackend());
[email protected]46773162010-05-07 22:31:20718}
719
[email protected]f6c9d562013-01-15 19:28:13720// Tests that IOBuffers are not referenced after IO completes.
721TEST(HttpCache, ReleaseBuffer) {
722 MockHttpCache cache;
723
724 // Write to the cache.
725 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
726
727 MockHttpRequest request(kSimpleGET_Transaction);
ttuttle859dc7a2015-04-23 19:42:29728 scoped_ptr<HttpTransaction> trans;
729 ASSERT_EQ(OK, cache.CreateTransaction(&trans));
[email protected]f6c9d562013-01-15 19:28:13730
731 const int kBufferSize = 10;
ttuttle859dc7a2015-04-23 19:42:29732 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize));
733 ReleaseBufferCompletionCallback cb(buffer.get());
[email protected]f6c9d562013-01-15 19:28:13734
ttuttle859dc7a2015-04-23 19:42:29735 int rv = trans->Start(&request, cb.callback(), BoundNetLog());
736 EXPECT_EQ(OK, cb.GetResult(rv));
[email protected]f6c9d562013-01-15 19:28:13737
[email protected]90499482013-06-01 00:39:50738 rv = trans->Read(buffer.get(), kBufferSize, cb.callback());
[email protected]f6c9d562013-01-15 19:28:13739 EXPECT_EQ(kBufferSize, cb.GetResult(rv));
740}
741
[email protected]37095fe2009-08-07 00:13:12742TEST(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]73cae572009-10-22 18:36:19762// Tests that disk failures after the transaction has started don't cause the
763// request to fail.
764TEST(HttpCache, SimpleGETWithDiskFailures2) {
765 MockHttpCache cache;
766
767 MockHttpRequest request(kSimpleGET_Transaction);
768
769 scoped_ptr<Context> c(new Context());
[email protected]027bd85a2013-12-27 22:39:10770 int rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:29771 ASSERT_EQ(OK, rv);
[email protected]73cae572009-10-22 18:36:19772
ttuttle859dc7a2015-04-23 19:42:29773 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
774 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]73cae572009-10-22 18:36:19775 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]02e7a012010-05-10 23:06:33782 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &en));
[email protected]73cae572009-10-22 18:36:19783 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]93fe75162012-02-09 21:51:31800// Tests that we handle failures to read from the cache.
[email protected]4a244532011-04-04 02:10:33801TEST(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]027bd85a2013-12-27 22:39:10815 int rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:29816 ASSERT_EQ(OK, rv);
[email protected]4a244532011-04-04 02:10:33817
818 MockHttpRequest request(kSimpleGET_Transaction);
ttuttle859dc7a2015-04-23 19:42:29819 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
820 EXPECT_EQ(OK, c->callback.GetResult(rv));
[email protected]93fe75162012-02-09 21:51:31821
822 // Now verify that the entry was removed from the cache.
823 cache.disk_cache()->set_soft_failures(false);
824
[email protected]93fe75162012-02-09 21:51:31825 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]40caa4c2012-03-20 20:42:58828
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]4a244532011-04-04 02:10:33834}
835
initial.commit586acc5fe2008-07-26 22:42:52836TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) {
837 MockHttpCache cache;
838
ttuttle859dc7a2015-04-23 19:42:29839 BoundTestNetLog log;
840 LoadTimingInfo load_timing_info;
[email protected]3b23a222013-05-15 21:33:25841
842 // Write to the cache.
843 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction,
844 log.bound(), &load_timing_info);
[email protected]baff44a2009-09-06 00:48:10845
[email protected]9e743cd2010-03-16 07:03:53846 // Check that the NetLog was filled as expected.
mmenke43758e62015-05-04 21:09:46847 TestNetLogEntry::List entries;
[email protected]b2fcd0e2010-12-01 15:19:40848 log.GetEntries(&entries);
[email protected]c47c0372014-03-12 23:07:02849 FilterLogEntries(&entries);
[email protected]b2fcd0e2010-12-01 15:19:40850
851 EXPECT_EQ(8u, entries.size());
ttuttle859dc7a2015-04-23 19:42:29852 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.commit586acc5fe2008-07-26 22:42:52868
[email protected]3b23a222013-05-15 21:33:25869 TestLoadTimingNetworkRequest(load_timing_info);
870
871 // Force this transaction to read from the cache.
initial.commit586acc5fe2008-07-26 22:42:52872 MockTransaction transaction(kSimpleGET_Transaction);
ttuttle859dc7a2015-04-23 19:42:29873 transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
initial.commit586acc5fe2008-07-26 22:42:52874
[email protected]9e743cd2010-03-16 07:03:53875 log.Clear();
[email protected]baff44a2009-09-06 00:48:10876
[email protected]3b23a222013-05-15 21:33:25877 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(),
878 &load_timing_info);
[email protected]baff44a2009-09-06 00:48:10879
[email protected]9e743cd2010-03-16 07:03:53880 // Check that the NetLog was filled as expected.
[email protected]b2fcd0e2010-12-01 15:19:40881 log.GetEntries(&entries);
[email protected]c47c0372014-03-12 23:07:02882 FilterLogEntries(&entries);
[email protected]b2fcd0e2010-12-01 15:19:40883
884 EXPECT_EQ(8u, entries.size());
ttuttle859dc7a2015-04-23 19:42:29885 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.commit586acc5fe2008-07-26 22:42:52901
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]3b23a222013-05-15 21:33:25905 TestLoadTimingCachedResponse(load_timing_info);
initial.commit586acc5fe2008-07-26 22:42:52906}
907
908TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) {
909 MockHttpCache cache;
910
911 // force this transaction to read from the cache
912 MockTransaction transaction(kSimpleGET_Transaction);
ttuttle859dc7a2015-04-23 19:42:29913 transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
initial.commit586acc5fe2008-07-26 22:42:52914
915 MockHttpRequest request(transaction);
ttuttle859dc7a2015-04-23 19:42:29916 TestCompletionCallback callback;
initial.commit586acc5fe2008-07-26 22:42:52917
ttuttle859dc7a2015-04-23 19:42:29918 scoped_ptr<HttpTransaction> trans;
919 ASSERT_EQ(OK, cache.CreateTransaction(&trans));
initial.commit586acc5fe2008-07-26 22:42:52920
ttuttle859dc7a2015-04-23 19:42:29921 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
922 if (rv == ERR_IO_PENDING)
initial.commit586acc5fe2008-07-26 22:42:52923 rv = callback.WaitForResult();
ttuttle859dc7a2015-04-23 19:42:29924 ASSERT_EQ(ERR_CACHE_MISS, rv);
initial.commit586acc5fe2008-07-26 22:42:52925
[email protected]af4876d2008-10-21 23:10:57926 trans.reset();
initial.commit586acc5fe2008-07-26 22:42:52927
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
933TEST(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);
ttuttle859dc7a2015-04-23 19:42:29941 transaction.load_flags |= LOAD_PREFERRING_CACHE;
initial.commit586acc5fe2008-07-26 22:42:52942
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
950TEST(HttpCache, SimpleGET_LoadPreferringCache_Miss) {
951 MockHttpCache cache;
952
953 // force this transaction to read from the cache if valid
954 MockTransaction transaction(kSimpleGET_Transaction);
ttuttle859dc7a2015-04-23 19:42:29955 transaction.load_flags |= LOAD_PREFERRING_CACHE;
initial.commit586acc5fe2008-07-26 22:42:52956
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]528e7782012-11-16 22:36:17964// Tests LOAD_PREFERRING_CACHE in the presence of vary headers.
965TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMatch) {
966 MockHttpCache cache;
967
968 // Write to the cache.
969 MockTransaction transaction(kSimpleGET_Transaction);
[email protected]1dce442e2013-04-23 03:06:29970 transaction.request_headers = "Foo: bar\r\n";
[email protected]528e7782012-11-16 22:36:17971 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.
ttuttle859dc7a2015-04-23 19:42:29977 transaction.load_flags |= LOAD_PREFERRING_CACHE;
[email protected]528e7782012-11-16 22:36:17978 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.
987TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMismatch) {
988 MockHttpCache cache;
989
990 // Write to the cache.
991 MockTransaction transaction(kSimpleGET_Transaction);
[email protected]1dce442e2013-04-23 03:06:29992 transaction.request_headers = "Foo: bar\r\n";
[email protected]528e7782012-11-16 22:36:17993 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.
ttuttle859dc7a2015-04-23 19:42:291000 transaction.load_flags |= LOAD_PREFERRING_CACHE;
[email protected]1dce442e2013-04-23 03:06:291001 transaction.request_headers = "Foo: none\r\n";
ttuttle859dc7a2015-04-23 19:42:291002 BoundTestNetLog log;
1003 LoadTimingInfo load_timing_info;
[email protected]3b23a222013-05-15 21:33:251004 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(),
1005 &load_timing_info);
[email protected]528e7782012-11-16 22:36:171006
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]3b23a222013-05-15 21:33:251010 TestLoadTimingNetworkRequest(load_timing_info);
[email protected]528e7782012-11-16 22:36:171011 RemoveMockTransaction(&transaction);
1012}
1013
[email protected]419704c2014-01-14 11:18:061014// Tests that was_cached was set properly on a failure, even if the cached
1015// response wasn't returned.
1016TEST(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.
ttuttle859dc7a2015-04-23 19:42:291030 transaction.return_code = ERR_FAILED;
[email protected]419704c2014-01-14 11:18:061031 AddMockTransaction(&transaction);
1032
1033 MockHttpRequest request(transaction);
ttuttle859dc7a2015-04-23 19:42:291034 TestCompletionCallback callback;
1035 scoped_ptr<HttpTransaction> trans;
1036 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
1037 EXPECT_EQ(OK, rv);
[email protected]419704c2014-01-14 11:18:061038 ASSERT_TRUE(trans.get());
ttuttle859dc7a2015-04-23 19:42:291039 rv = trans->Start(&request, callback.callback(), BoundNetLog());
1040 EXPECT_EQ(ERR_FAILED, callback.GetResult(rv));
[email protected]419704c2014-01-14 11:18:061041
ttuttle859dc7a2015-04-23 19:42:291042 const HttpResponseInfo* response_info = trans->GetResponseInfo();
[email protected]419704c2014-01-14 11:18:061043 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]7cf7ccb2013-04-20 02:53:081050// Confirm if we have an empty cache, a read is marked as network verified.
1051TEST(HttpCache, SimpleGET_NetworkAccessed_Network) {
1052 MockHttpCache cache;
1053
1054 // write to the cache
ttuttle859dc7a2015-04-23 19:42:291055 HttpResponseInfo response_info;
[email protected]7cf7ccb2013-04-20 02:53:081056 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.
1067TEST(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.
ttuttle859dc7a2015-04-23 19:42:291078 HttpResponseInfo response_info;
[email protected]7cf7ccb2013-04-20 02:53:081079 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.commit586acc5fe2008-07-26 22:42:521087TEST(HttpCache, SimpleGET_LoadBypassCache) {
1088 MockHttpCache cache;
1089
[email protected]9393b7172010-02-11 00:12:151090 // Write to the cache.
initial.commit586acc5fe2008-07-26 22:42:521091 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1092
[email protected]9393b7172010-02-11 00:12:151093 // Force this transaction to write to the cache again.
initial.commit586acc5fe2008-07-26 22:42:521094 MockTransaction transaction(kSimpleGET_Transaction);
ttuttle859dc7a2015-04-23 19:42:291095 transaction.load_flags |= LOAD_BYPASS_CACHE;
initial.commit586acc5fe2008-07-26 22:42:521096
ttuttle859dc7a2015-04-23 19:42:291097 BoundTestNetLog log;
1098 LoadTimingInfo load_timing_info;
[email protected]f6f1bebc2011-01-07 03:04:541099
[email protected]3b23a222013-05-15 21:33:251100 // Write to the cache.
1101 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(),
1102 &load_timing_info);
[email protected]9393b7172010-02-11 00:12:151103
[email protected]9e743cd2010-03-16 07:03:531104 // Check that the NetLog was filled as expected.
mmenke43758e62015-05-04 21:09:461105 TestNetLogEntry::List entries;
[email protected]b2fcd0e2010-12-01 15:19:401106 log.GetEntries(&entries);
[email protected]c47c0372014-03-12 23:07:021107 FilterLogEntries(&entries);
[email protected]b2fcd0e2010-12-01 15:19:401108
1109 EXPECT_EQ(8u, entries.size());
ttuttle859dc7a2015-04-23 19:42:291110 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.commit586acc5fe2008-07-26 22:42:521126
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]3b23a222013-05-15 21:33:251130 TestLoadTimingNetworkRequest(load_timing_info);
initial.commit586acc5fe2008-07-26 22:42:521131}
1132
1133TEST(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]1dce442e2013-04-23 03:06:291141 transaction.request_headers = "pragma: no-cache\r\n";
initial.commit586acc5fe2008-07-26 22:42:521142
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
1150TEST(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]1dce442e2013-04-23 03:06:291158 transaction.request_headers = "cache-control: no-cache\r\n";
initial.commit586acc5fe2008-07-26 22:42:521159
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
1167TEST(HttpCache, SimpleGET_LoadValidateCache) {
1168 MockHttpCache cache;
1169
[email protected]3b23a222013-05-15 21:33:251170 // Write to the cache.
initial.commit586acc5fe2008-07-26 22:42:521171 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1172
[email protected]3b23a222013-05-15 21:33:251173 // Read from the cache.
initial.commit586acc5fe2008-07-26 22:42:521174 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1175
[email protected]3b23a222013-05-15 21:33:251176 // Force this transaction to validate the cache.
initial.commit586acc5fe2008-07-26 22:42:521177 MockTransaction transaction(kSimpleGET_Transaction);
ttuttle859dc7a2015-04-23 19:42:291178 transaction.load_flags |= LOAD_VALIDATE_CACHE;
initial.commit586acc5fe2008-07-26 22:42:521179
ttuttle859dc7a2015-04-23 19:42:291180 HttpResponseInfo response_info;
1181 BoundTestNetLog log;
1182 LoadTimingInfo load_timing_info;
[email protected]3b23a222013-05-15 21:33:251183 RunTransactionTestWithResponseInfoAndGetTiming(
1184 cache.http_cache(), transaction, &response_info, log.bound(),
1185 &load_timing_info);
initial.commit586acc5fe2008-07-26 22:42:521186
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]7cf7ccb2013-04-20 02:53:081190 EXPECT_TRUE(response_info.network_accessed);
[email protected]3b23a222013-05-15 21:33:251191 TestLoadTimingNetworkRequest(load_timing_info);
initial.commit586acc5fe2008-07-26 22:42:521192}
1193
1194TEST(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]1dce442e2013-04-23 03:06:291205 transaction.request_headers = "cache-control: max-age=0\r\n";
initial.commit586acc5fe2008-07-26 22:42:521206
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
ttuttle859dc7a2015-04-23 19:42:291214static void PreserveRequestHeaders_Handler(const HttpRequestInfo* request,
1215 std::string* response_status,
1216 std::string* response_headers,
1217 std::string* response_data) {
[email protected]8c76ae22010-04-20 22:15:431218 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey));
[email protected]a3eee212009-11-05 18:08:581219}
1220
1221// Tests that we don't remove extra headers for simple requests.
1222TEST(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.
1242TEST(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]8c76ae22010-04-20 22:15:431250 transaction.request_headers = "If-None-Match: \"foopy\"\r\n"
[email protected]a3eee212009-11-05 18:08:581251 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.commit586acc5fe2008-07-26 22:42:521262TEST(HttpCache, SimpleGET_ManyReaders) {
1263 MockHttpCache cache;
1264
1265 MockHttpRequest request(kSimpleGET_Transaction);
1266
initial.commit586acc5fe2008-07-26 22:42:521267 std::vector<Context*> context_list;
1268 const int kNumTransactions = 5;
1269
1270 for (int i = 0; i < kNumTransactions; ++i) {
[email protected]1638d602009-09-24 03:49:171271 context_list.push_back(new Context());
initial.commit586acc5fe2008-07-26 22:42:521272 Context* c = context_list[i];
[email protected]1638d602009-09-24 03:49:171273
[email protected]027bd85a2013-12-27 22:39:101274 c->result = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:291275 ASSERT_EQ(OK, c->result);
1276 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
[email protected]1638d602009-09-24 03:49:171277
ttuttle859dc7a2015-04-23 19:42:291278 c->result =
1279 c->trans->Start(&request, c->callback.callback(), BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:521280 }
1281
[email protected]fbf50472010-07-15 22:53:531282 // All requests are waiting for the active entry.
1283 for (int i = 0; i < kNumTransactions; ++i) {
1284 Context* c = context_list[i];
ttuttle859dc7a2015-04-23 19:42:291285 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
[email protected]fbf50472010-07-15 22:53:531286 }
1287
[email protected]7d7ad6e42010-01-14 01:30:531288 // Allow all requests to move from the Create queue to the active entry.
[email protected]2da659e2013-05-23 20:51:341289 base::MessageLoop::current()->RunUntilIdle();
[email protected]7d7ad6e42010-01-14 01:30:531290
1291 // The first request should be a writer at this point, and the subsequent
initial.commit586acc5fe2008-07-26 22:42:521292 // 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]fbf50472010-07-15 22:53:531298 // 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];
ttuttle859dc7a2015-04-23 19:42:291302 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
[email protected]fbf50472010-07-15 22:53:531303 }
1304
initial.commit586acc5fe2008-07-26 22:42:521305 for (int i = 0; i < kNumTransactions; ++i) {
1306 Context* c = context_list[i];
ttuttle859dc7a2015-04-23 19:42:291307 if (c->result == ERR_IO_PENDING)
initial.commit586acc5fe2008-07-26 22:42:521308 c->result = c->callback.WaitForResult();
[email protected]af4876d2008-10-21 23:10:571309 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
initial.commit586acc5fe2008-07-26 22:42:521310 }
1311
[email protected]7d7ad6e42010-01-14 01:30:531312 // We should not have had to re-open the disk entry
initial.commit586acc5fe2008-07-26 22:42:521313
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.commit586acc5fe2008-07-26 22:42:521320 delete c;
1321 }
1322}
1323
[email protected]e1891642009-01-07 18:30:571324// 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.
1328TEST(HttpCache, SimpleGET_RacingReaders) {
1329 MockHttpCache cache;
1330
1331 MockHttpRequest request(kSimpleGET_Transaction);
1332 MockHttpRequest reader_request(kSimpleGET_Transaction);
ttuttle859dc7a2015-04-23 19:42:291333 reader_request.load_flags = LOAD_ONLY_FROM_CACHE;
[email protected]e1891642009-01-07 18:30:571334
1335 std::vector<Context*> context_list;
1336 const int kNumTransactions = 5;
1337
1338 for (int i = 0; i < kNumTransactions; ++i) {
[email protected]1638d602009-09-24 03:49:171339 context_list.push_back(new Context());
[email protected]e1891642009-01-07 18:30:571340 Context* c = context_list[i];
[email protected]1638d602009-09-24 03:49:171341
[email protected]027bd85a2013-12-27 22:39:101342 c->result = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:291343 ASSERT_EQ(OK, c->result);
[email protected]1638d602009-09-24 03:49:171344
[email protected]e1891642009-01-07 18:30:571345 MockHttpRequest* this_request = &request;
1346 if (i == 1 || i == 2)
1347 this_request = &reader_request;
1348
ttuttle859dc7a2015-04-23 19:42:291349 c->result =
1350 c->trans->Start(this_request, c->callback.callback(), BoundNetLog());
[email protected]e1891642009-01-07 18:30:571351 }
1352
[email protected]7d7ad6e42010-01-14 01:30:531353 // Allow all requests to move from the Create queue to the active entry.
[email protected]2da659e2013-05-23 20:51:341354 base::MessageLoop::current()->RunUntilIdle();
[email protected]7d7ad6e42010-01-14 01:30:531355
[email protected]e1891642009-01-07 18:30:571356 // 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];
ttuttle859dc7a2015-04-23 19:42:291364 ASSERT_EQ(ERR_IO_PENDING, c->result);
[email protected]e1891642009-01-07 18:30:571365 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
ttuttle859dc7a2015-04-23 19:42:291370 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState());
1371 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE,
[email protected]fbf50472010-07-15 22:53:531372 context_list[3]->trans->GetLoadState());
1373
[email protected]e1891642009-01-07 18:30:571374 c = context_list[1];
ttuttle859dc7a2015-04-23 19:42:291375 ASSERT_EQ(ERR_IO_PENDING, c->result);
[email protected]e1891642009-01-07 18:30:571376 c->result = c->callback.WaitForResult();
ttuttle859dc7a2015-04-23 19:42:291377 if (c->result == OK)
[email protected]37095fe2009-08-07 00:13:121378 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
[email protected]e1891642009-01-07 18:30:571379
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];
ttuttle859dc7a2015-04-23 19:42:291390 if (c->result == ERR_IO_PENDING)
[email protected]e1891642009-01-07 18:30:571391 c->result = c->callback.WaitForResult();
ttuttle859dc7a2015-04-23 19:42:291392 if (c->result == OK)
[email protected]37095fe2009-08-07 00:13:121393 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
[email protected]e1891642009-01-07 18:30:571394 }
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]d5b94c72009-10-26 16:51:101408// 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
1411TEST(HttpCache, SimpleGET_DoomWithPending) {
1412 // We need simultaneous doomed / not_doomed entries so let's use a real cache.
ttuttle859dc7a2015-04-23 19:42:291413 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(1024 * 1024));
[email protected]d5b94c72009-10-26 16:51:101414
1415 MockHttpRequest request(kSimpleGET_Transaction);
1416 MockHttpRequest writer_request(kSimpleGET_Transaction);
ttuttle859dc7a2015-04-23 19:42:291417 writer_request.load_flags = LOAD_BYPASS_CACHE;
[email protected]d5b94c72009-10-26 16:51:101418
olli.raula6df48b2a2015-11-26 07:40:221419 std::vector<scoped_ptr<Context>> context_list;
[email protected]d5b94c72009-10-26 16:51:101420 const int kNumTransactions = 4;
1421
1422 for (int i = 0; i < kNumTransactions; ++i) {
olli.raula6df48b2a2015-11-26 07:40:221423 context_list.push_back(make_scoped_ptr(new Context()));
1424 Context* c = context_list[i].get();
[email protected]d5b94c72009-10-26 16:51:101425
[email protected]027bd85a2013-12-27 22:39:101426 c->result = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:291427 ASSERT_EQ(OK, c->result);
[email protected]d5b94c72009-10-26 16:51:101428
1429 MockHttpRequest* this_request = &request;
1430 if (i == 3)
1431 this_request = &writer_request;
1432
ttuttle859dc7a2015-04-23 19:42:291433 c->result =
1434 c->trans->Start(this_request, c->callback.callback(), BoundNetLog());
[email protected]d5b94c72009-10-26 16:51:101435 }
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.raula6df48b2a2015-11-26 07:40:221443 context_list[1].reset();
[email protected]d5b94c72009-10-26 16:51:101444
1445 for (int i = 0; i < kNumTransactions; ++i) {
1446 if (i == 1)
1447 continue;
olli.raula6df48b2a2015-11-26 07:40:221448 Context* c = context_list[i].get();
ttuttle859dc7a2015-04-23 19:42:291449 ASSERT_EQ(ERR_IO_PENDING, c->result);
[email protected]d5b94c72009-10-26 16:51:101450 c->result = c->callback.WaitForResult();
1451 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1452 }
1453}
1454
[email protected]b367d9a52009-02-27 01:02:511455// 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.
1458TEST(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]1638d602009-09-24 03:49:171470 context_list.push_back(new Context());
[email protected]b367d9a52009-02-27 01:02:511471 Context* c = context_list[i];
[email protected]1638d602009-09-24 03:49:171472
[email protected]027bd85a2013-12-27 22:39:101473 c->result = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:291474 ASSERT_EQ(OK, c->result);
[email protected]1638d602009-09-24 03:49:171475
ttuttle859dc7a2015-04-23 19:42:291476 c->result =
1477 c->trans->Start(&request, c->callback.callback(), BoundNetLog());
[email protected]b367d9a52009-02-27 01:02:511478 }
1479
[email protected]7d7ad6e42010-01-14 01:30:531480 // Allow all requests to move from the Create queue to the active entry.
[email protected]2da659e2013-05-23 20:51:341481 base::MessageLoop::current()->RunUntilIdle();
[email protected]7d7ad6e42010-01-14 01:30:531482
[email protected]b367d9a52009-02-27 01:02:511483 // 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];
ttuttle859dc7a2015-04-23 19:42:291495 if (c->result == ERR_IO_PENDING)
[email protected]b367d9a52009-02-27 01:02:511496 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.commit586acc5fe2008-07-26 22:42:521508TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) {
1509 MockHttpCache cache;
1510
1511 MockHttpRequest request(kSimpleGET_Transaction);
1512
initial.commit586acc5fe2008-07-26 22:42:521513 std::vector<Context*> context_list;
1514 const int kNumTransactions = 2;
1515
1516 for (int i = 0; i < kNumTransactions; ++i) {
[email protected]1638d602009-09-24 03:49:171517 context_list.push_back(new Context());
initial.commit586acc5fe2008-07-26 22:42:521518 Context* c = context_list[i];
[email protected]1638d602009-09-24 03:49:171519
[email protected]027bd85a2013-12-27 22:39:101520 c->result = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:291521 ASSERT_EQ(OK, c->result);
[email protected]1638d602009-09-24 03:49:171522
ttuttle859dc7a2015-04-23 19:42:291523 c->result =
1524 c->trans->Start(&request, c->callback.callback(), BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:521525 }
1526
[email protected]7d7ad6e42010-01-14 01:30:531527 // Allow all requests to move from the Create queue to the active entry.
[email protected]2da659e2013-05-23 20:51:341528 base::MessageLoop::current()->RunUntilIdle();
[email protected]7d7ad6e42010-01-14 01:30:531529
1530 // The first request should be a writer at this point, and the subsequent
initial.commit586acc5fe2008-07-26 22:42:521531 // 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];
ttuttle859dc7a2015-04-23 19:42:291539 if (c->result == ERR_IO_PENDING)
initial.commit586acc5fe2008-07-26 22:42:521540 c->result = c->callback.WaitForResult();
[email protected]7d7ad6e42010-01-14 01:30:531541 // Destroy only the first transaction.
initial.commit586acc5fe2008-07-26 22:42:521542 if (i == 0) {
initial.commit586acc5fe2008-07-26 22:42:521543 delete c;
1544 context_list[i] = NULL;
1545 }
1546 }
1547
[email protected]7d7ad6e42010-01-14 01:30:531548 // Complete the rest of the transactions.
initial.commit586acc5fe2008-07-26 22:42:521549 for (int i = 1; i < kNumTransactions; ++i) {
1550 Context* c = context_list[i];
[email protected]af4876d2008-10-21 23:10:571551 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
initial.commit586acc5fe2008-07-26 22:42:521552 }
1553
[email protected]7d7ad6e42010-01-14 01:30:531554 // We should have had to re-open the disk entry.
initial.commit586acc5fe2008-07-26 22:42:521555
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.commit586acc5fe2008-07-26 22:42:521562 delete c;
1563 }
1564}
1565
[email protected]7d7ad6e42010-01-14 01:30:531566// Tests that we can cancel requests that are queued waiting to open the disk
1567// cache entry.
1568TEST(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]027bd85a2013-12-27 22:39:101580 c->result = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:291581 ASSERT_EQ(OK, c->result);
[email protected]7d7ad6e42010-01-14 01:30:531582
ttuttle859dc7a2015-04-23 19:42:291583 c->result =
1584 c->trans->Start(&request, c->callback.callback(), BoundNetLog());
[email protected]7d7ad6e42010-01-14 01:30:531585 }
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]fb2622f2010-07-13 18:00:561623// Tests that we can cancel a single request to open a disk cache entry.
1624TEST(HttpCache, SimpleGET_CancelCreate) {
1625 MockHttpCache cache;
1626
1627 MockHttpRequest request(kSimpleGET_Transaction);
1628
1629 Context* c = new Context();
1630
[email protected]027bd85a2013-12-27 22:39:101631 c->result = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:291632 ASSERT_EQ(OK, c->result);
[email protected]fb2622f2010-07-13 18:00:561633
ttuttle859dc7a2015-04-23 19:42:291634 c->result = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
1635 EXPECT_EQ(ERR_IO_PENDING, c->result);
[email protected]fb2622f2010-07-13 18:00:561636
1637 // Release the reference that the mock disk cache keeps for this entry, so
[email protected]49639fa2011-12-20 23:22:411638 // that we test that the http cache handles the cancellation correctly.
[email protected]fb2622f2010-07-13 18:00:561639 cache.disk_cache()->ReleaseAll();
1640 delete c;
1641
[email protected]2da659e2013-05-23 20:51:341642 base::MessageLoop::current()->RunUntilIdle();
[email protected]fb2622f2010-07-13 18:00:561643 EXPECT_EQ(1, cache.disk_cache()->create_count());
1644}
1645
[email protected]7d7ad6e42010-01-14 01:30:531646// Tests that we delete/create entries even if multiple requests are queued.
1647TEST(HttpCache, SimpleGET_ManyWriters_BypassCache) {
1648 MockHttpCache cache;
1649
1650 MockHttpRequest request(kSimpleGET_Transaction);
ttuttle859dc7a2015-04-23 19:42:291651 request.load_flags = LOAD_BYPASS_CACHE;
[email protected]7d7ad6e42010-01-14 01:30:531652
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]027bd85a2013-12-27 22:39:101660 c->result = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:291661 ASSERT_EQ(OK, c->result);
[email protected]7d7ad6e42010-01-14 01:30:531662
ttuttle859dc7a2015-04-23 19:42:291663 c->result =
1664 c->trans->Start(&request, c->callback.callback(), BoundNetLog());
[email protected]7d7ad6e42010-01-14 01:30:531665 }
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]8aacaf382014-06-24 05:33:411692// Tests that a (simulated) timeout allows transactions waiting on the cache
1693// lock to continue.
1694TEST(HttpCache, SimpleGET_WriterTimeout) {
1695 MockHttpCache cache;
1696 cache.BypassCacheLock();
1697
1698 MockHttpRequest request(kSimpleGET_Transaction);
1699 Context c1, c2;
ttuttle859dc7a2015-04-23 19:42:291700 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]8aacaf382014-06-24 05:33:411706
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.commit586acc5fe2008-07-26 22:42:521717TEST(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);
ttuttle859dc7a2015-04-23 19:42:291724 TestCompletionCallback callback;
initial.commit586acc5fe2008-07-26 22:42:521725
ttuttle859dc7a2015-04-23 19:42:291726 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.commit586acc5fe2008-07-26 22:42:521730 rv = callback.WaitForResult();
ttuttle859dc7a2015-04-23 19:42:291731 ASSERT_EQ(OK, rv);
initial.commit586acc5fe2008-07-26 22:42:521732
ttuttle859dc7a2015-04-23 19:42:291733 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
[email protected]90499482013-06-01 00:39:501734 rv = trans->Read(buf.get(), 256, callback.callback());
ttuttle859dc7a2015-04-23 19:42:291735 EXPECT_EQ(ERR_IO_PENDING, rv);
initial.commit586acc5fe2008-07-26 22:42:521736
1737 // Test that destroying the transaction while it is reading from the cache
1738 // works properly.
[email protected]af4876d2008-10-21 23:10:571739 trans.reset();
initial.commit586acc5fe2008-07-26 22:42:521740
1741 // Make sure we pump any pending events, which should include a call to
1742 // HttpCache::Transaction::OnCacheReadCompleted.
[email protected]2da659e2013-05-23 20:51:341743 base::MessageLoop::current()->RunUntilIdle();
initial.commit586acc5fe2008-07-26 22:42:521744}
1745
[email protected]46773162010-05-07 22:31:201746// Tests that we can delete the HttpCache and deal with queued transactions
1747// ("waiting for the backend" as opposed to Active or Doomed entries).
1748TEST(HttpCache, SimpleGET_ManyWriters_DeleteCache) {
mmenkebc31a2c2015-10-29 13:44:451749 scoped_ptr<MockHttpCache> cache(
1750 new MockHttpCache(make_scoped_ptr(new MockBackendNoCbFactory())));
[email protected]46773162010-05-07 22:31:201751
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]027bd85a2013-12-27 22:39:101761 c->result = cache->CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:291762 ASSERT_EQ(OK, c->result);
[email protected]46773162010-05-07 22:31:201763
ttuttle859dc7a2015-04-23 19:42:291764 c->result =
1765 c->trans->Start(&request, c->callback.callback(), BoundNetLog());
[email protected]46773162010-05-07 22:31:201766 }
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]f8702522010-05-12 18:40:101784// Tests that we queue requests when initializing the backend.
1785TEST(HttpCache, SimpleGET_WaitForBackend) {
1786 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
mmenkebc31a2c2015-10-29 13:44:451787 MockHttpCache cache(make_scoped_ptr(factory));
[email protected]f8702522010-05-12 18:40:101788
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]027bd85a2013-12-27 22:39:101800 c->result = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:291801 ASSERT_EQ(OK, c->result);
[email protected]f8702522010-05-12 18:40:101802 }
1803
1804 context_list[0]->result = context_list[0]->trans->Start(
ttuttle859dc7a2015-04-23 19:42:291805 &request0, context_list[0]->callback.callback(), BoundNetLog());
[email protected]f8702522010-05-12 18:40:101806 context_list[1]->result = context_list[1]->trans->Start(
ttuttle859dc7a2015-04-23 19:42:291807 &request1, context_list[1]->callback.callback(), BoundNetLog());
[email protected]f8702522010-05-12 18:40:101808 context_list[2]->result = context_list[2]->trans->Start(
ttuttle859dc7a2015-04-23 19:42:291809 &request2, context_list[2]->callback.callback(), BoundNetLog());
[email protected]f8702522010-05-12 18:40:101810
1811 // Just to make sure that everything is still pending.
[email protected]2da659e2013-05-23 20:51:341812 base::MessageLoop::current()->RunUntilIdle();
[email protected]f8702522010-05-12 18:40:101813
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]2da659e2013-05-23 20:51:341819 base::MessageLoop::current()->RunUntilIdle();
[email protected]f8702522010-05-12 18:40:101820 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.
1831TEST(HttpCache, SimpleGET_WaitForBackend_CancelCreate) {
1832 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
mmenkebc31a2c2015-10-29 13:44:451833 MockHttpCache cache(make_scoped_ptr(factory));
[email protected]f8702522010-05-12 18:40:101834
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]027bd85a2013-12-27 22:39:101846 c->result = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:291847 ASSERT_EQ(OK, c->result);
[email protected]f8702522010-05-12 18:40:101848 }
1849
1850 context_list[0]->result = context_list[0]->trans->Start(
ttuttle859dc7a2015-04-23 19:42:291851 &request0, context_list[0]->callback.callback(), BoundNetLog());
[email protected]f8702522010-05-12 18:40:101852 context_list[1]->result = context_list[1]->trans->Start(
ttuttle859dc7a2015-04-23 19:42:291853 &request1, context_list[1]->callback.callback(), BoundNetLog());
[email protected]f8702522010-05-12 18:40:101854 context_list[2]->result = context_list[2]->trans->Start(
ttuttle859dc7a2015-04-23 19:42:291855 &request2, context_list[2]->callback.callback(), BoundNetLog());
[email protected]f8702522010-05-12 18:40:101856
1857 // Just to make sure that everything is still pending.
[email protected]2da659e2013-05-23 20:51:341858 base::MessageLoop::current()->RunUntilIdle();
[email protected]f8702522010-05-12 18:40:101859
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]e86e79d32010-07-17 00:29:251884// Tests that we can delete the cache while creating the backend.
1885TEST(HttpCache, DeleteCacheWaitingForBackend) {
1886 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
mmenkebc31a2c2015-10-29 13:44:451887 scoped_ptr<MockHttpCache> cache(new MockHttpCache(make_scoped_ptr(factory)));
[email protected]e86e79d32010-07-17 00:29:251888
1889 MockHttpRequest request(kSimpleGET_Transaction);
1890
1891 scoped_ptr<Context> c(new Context());
[email protected]027bd85a2013-12-27 22:39:101892 c->result = cache->CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:291893 ASSERT_EQ(OK, c->result);
[email protected]e86e79d32010-07-17 00:29:251894
ttuttle859dc7a2015-04-23 19:42:291895 c->trans->Start(&request, c->callback.callback(), BoundNetLog());
[email protected]e86e79d32010-07-17 00:29:251896
1897 // Just to make sure that everything is still pending.
[email protected]2da659e2013-05-23 20:51:341898 base::MessageLoop::current()->RunUntilIdle();
[email protected]e86e79d32010-07-17 00:29:251899
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.
ttuttle859dc7a2015-04-23 19:42:291905 CompletionCallback callback = factory->callback();
[email protected]8c3f5a32013-08-01 11:57:531906 scoped_ptr<disk_cache::Backend>* backend = factory->backend();
[email protected]e86e79d32010-07-17 00:29:251907
1908 cache.reset();
[email protected]2da659e2013-05-23 20:51:341909 base::MessageLoop::current()->RunUntilIdle();
[email protected]e86e79d32010-07-17 00:29:251910
[email protected]8c3f5a32013-08-01 11:57:531911 backend->reset();
ttuttle859dc7a2015-04-23 19:42:291912 callback.Run(ERR_ABORTED);
[email protected]e86e79d32010-07-17 00:29:251913}
1914
[email protected]ccf175c2010-08-21 01:41:591915// Tests that we can delete the cache while creating the backend, from within
1916// one of the callbacks.
1917TEST(HttpCache, DeleteCacheWaitingForBackend2) {
1918 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
mmenkebc31a2c2015-10-29 13:44:451919 MockHttpCache* cache = new MockHttpCache(make_scoped_ptr(factory));
[email protected]ccf175c2010-08-21 01:41:591920
[email protected]2a65aceb82011-12-19 20:59:271921 DeleteCacheCompletionCallback cb(cache);
[email protected]ccf175c2010-08-21 01:41:591922 disk_cache::Backend* backend;
[email protected]2a65aceb82011-12-19 20:59:271923 int rv = cache->http_cache()->GetBackend(&backend, cb.callback());
ttuttle859dc7a2015-04-23 19:42:291924 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]ccf175c2010-08-21 01:41:591925
1926 // Now let's queue a regular transaction
1927 MockHttpRequest request(kSimpleGET_Transaction);
1928
1929 scoped_ptr<Context> c(new Context());
[email protected]027bd85a2013-12-27 22:39:101930 c->result = cache->CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:291931 ASSERT_EQ(OK, c->result);
[email protected]ccf175c2010-08-21 01:41:591932
ttuttle859dc7a2015-04-23 19:42:291933 c->trans->Start(&request, c->callback.callback(), BoundNetLog());
[email protected]ccf175c2010-08-21 01:41:591934
1935 // And another direct backend request.
ttuttle859dc7a2015-04-23 19:42:291936 TestCompletionCallback cb2;
[email protected]2a65aceb82011-12-19 20:59:271937 rv = cache->http_cache()->GetBackend(&backend, cb2.callback());
ttuttle859dc7a2015-04-23 19:42:291938 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]ccf175c2010-08-21 01:41:591939
1940 // Just to make sure that everything is still pending.
[email protected]2da659e2013-05-23 20:51:341941 base::MessageLoop::current()->RunUntilIdle();
[email protected]ccf175c2010-08-21 01:41:591942
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]2da659e2013-05-23 20:51:341951 base::MessageLoop::current()->RunUntilIdle();
ttuttle859dc7a2015-04-23 19:42:291952 EXPECT_EQ(OK, c->callback.GetResult(c->result));
[email protected]ccf175c2010-08-21 01:41:591953 EXPECT_FALSE(cb2.have_result());
1954}
1955
tfarinae04a95b2015-09-18 22:48:121956// 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
1963TEST(HttpCache, MAYBE_TypicalGET_ConditionalRequest) {
initial.commit586acc5fe2008-07-26 22:42:521964 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]3b23a222013-05-15 21:33:251973 // Get the same URL again, but this time we expect it to result
initial.commit586acc5fe2008-07-26 22:42:521974 // in a conditional request.
ttuttle859dc7a2015-04-23 19:42:291975 BoundTestNetLog log;
1976 LoadTimingInfo load_timing_info;
[email protected]3b23a222013-05-15 21:33:251977 RunTransactionTestAndGetTiming(cache.http_cache(), kTypicalGET_Transaction,
1978 log.bound(), &load_timing_info);
initial.commit586acc5fe2008-07-26 22:42:521979
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]3b23a222013-05-15 21:33:251983 TestLoadTimingNetworkRequest(load_timing_info);
initial.commit586acc5fe2008-07-26 22:42:521984}
1985
ttuttle859dc7a2015-04-23 19:42:291986static void ETagGet_ConditionalRequest_Handler(const HttpRequestInfo* request,
1987 std::string* response_status,
1988 std::string* response_headers,
1989 std::string* response_data) {
[email protected]8c76ae22010-04-20 22:15:431990 EXPECT_TRUE(
ttuttle859dc7a2015-04-23 19:42:291991 request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch));
initial.commit586acc5fe2008-07-26 22:42:521992 response_status->assign("HTTP/1.1 304 Not Modified");
1993 response_headers->assign(kETagGET_Transaction.response_headers);
1994 response_data->clear();
1995}
1996
1997TEST(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]3b23a222013-05-15 21:33:252009 // Get the same URL again, but this time we expect it to result
initial.commit586acc5fe2008-07-26 22:42:522010 // in a conditional request.
ttuttle859dc7a2015-04-23 19:42:292011 transaction.load_flags = LOAD_VALIDATE_CACHE;
initial.commit586acc5fe2008-07-26 22:42:522012 transaction.handler = ETagGet_ConditionalRequest_Handler;
ttuttle859dc7a2015-04-23 19:42:292013 BoundTestNetLog log;
2014 LoadTimingInfo load_timing_info;
ttuttled9dbc652015-09-29 20:00:592015 IPEndPoint remote_endpoint;
2016 RunTransactionTestAndGetTimingAndConnectedSocketAddress(
2017 cache.http_cache(), transaction, log.bound(), &load_timing_info,
2018 &remote_endpoint);
initial.commit586acc5fe2008-07-26 22:42:522019
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]3b23a222013-05-15 21:33:252023 TestLoadTimingNetworkRequest(load_timing_info);
ttuttled9dbc652015-09-29 20:00:592024
2025 EXPECT_FALSE(remote_endpoint.address().empty());
initial.commit586acc5fe2008-07-26 22:42:522026}
2027
[email protected]9bb9f992013-01-11 01:43:162028class 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
ttuttle859dc7a2015-04-23 19:42:292038 static void Handler(const HttpRequestInfo* request,
[email protected]9bb9f992013-01-11 01:43:162039 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};
2047bool RevalidationServer::s_etag_used_ = false;
2048bool RevalidationServer::s_last_modified_used_ = false;
2049
ttuttle859dc7a2015-04-23 19:42:292050void RevalidationServer::Handler(const HttpRequestInfo* request,
[email protected]9bb9f992013-01-11 01:43:162051 std::string* response_status,
2052 std::string* response_headers,
2053 std::string* response_data) {
ttuttle859dc7a2015-04-23 19:42:292054 if (request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch))
[email protected]9bb9f992013-01-11 01:43:162055 s_etag_used_ = true;
2056
ttuttle859dc7a2015-04-23 19:42:292057 if (request->extra_headers.HasHeader(HttpRequestHeaders::kIfModifiedSince)) {
[email protected]9bb9f992013-01-11 01:43:162058 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.
rvargas28904d862015-03-09 19:21:092073TEST(HttpCache, GET_ValidateCache_VaryMatch) {
[email protected]9bb9f992013-01-11 01:43:162074 MockHttpCache cache;
2075
2076 // Write to the cache.
2077 MockTransaction transaction(kTypicalGET_Transaction);
[email protected]1dce442e2013-04-23 03:06:292078 transaction.request_headers = "Foo: bar\r\n";
[email protected]9bb9f992013-01-11 01:43:162079 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;
ttuttle859dc7a2015-04-23 19:42:292091 BoundTestNetLog log;
2092 LoadTimingInfo load_timing_info;
[email protected]3b23a222013-05-15 21:33:252093 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(),
2094 &load_timing_info);
[email protected]9bb9f992013-01-11 01:43:162095
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]3b23a222013-05-15 21:33:252101 TestLoadTimingNetworkRequest(load_timing_info);
[email protected]9bb9f992013-01-11 01:43:162102 RemoveMockTransaction(&transaction);
2103}
2104
2105// Tests revalidation after a vary mismatch if etag is present.
rvargas28904d862015-03-09 19:21:092106TEST(HttpCache, GET_ValidateCache_VaryMismatch) {
[email protected]9bb9f992013-01-11 01:43:162107 MockHttpCache cache;
2108
2109 // Write to the cache.
2110 MockTransaction transaction(kTypicalGET_Transaction);
[email protected]1dce442e2013-04-23 03:06:292111 transaction.request_headers = "Foo: bar\r\n";
[email protected]9bb9f992013-01-11 01:43:162112 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]1dce442e2013-04-23 03:06:292124 transaction.request_headers = "Foo: none\r\n";
ttuttle859dc7a2015-04-23 19:42:292125 BoundTestNetLog log;
2126 LoadTimingInfo load_timing_info;
[email protected]3b23a222013-05-15 21:33:252127 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(),
2128 &load_timing_info);
[email protected]9bb9f992013-01-11 01:43:162129
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]3b23a222013-05-15 21:33:252135 TestLoadTimingNetworkRequest(load_timing_info);
[email protected]9bb9f992013-01-11 01:43:162136 RemoveMockTransaction(&transaction);
2137}
2138
2139// Tests lack of revalidation after a vary mismatch and no etag.
rvargas28904d862015-03-09 19:21:092140TEST(HttpCache, GET_DontValidateCache_VaryMismatch) {
[email protected]9bb9f992013-01-11 01:43:162141 MockHttpCache cache;
2142
2143 // Write to the cache.
2144 MockTransaction transaction(kTypicalGET_Transaction);
[email protected]1dce442e2013-04-23 03:06:292145 transaction.request_headers = "Foo: bar\r\n";
[email protected]9bb9f992013-01-11 01:43:162146 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]1dce442e2013-04-23 03:06:292157 transaction.request_headers = "Foo: none\r\n";
ttuttle859dc7a2015-04-23 19:42:292158 BoundTestNetLog log;
2159 LoadTimingInfo load_timing_info;
[email protected]3b23a222013-05-15 21:33:252160 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(),
2161 &load_timing_info);
[email protected]9bb9f992013-01-11 01:43:162162
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]3b23a222013-05-15 21:33:252168 TestLoadTimingNetworkRequest(load_timing_info);
[email protected]9bb9f992013-01-11 01:43:162169 RemoveMockTransaction(&transaction);
2170}
2171
rvargas28904d862015-03-09 19:21:092172// Tests that a new vary header provided when revalidating an entry is saved.
2173TEST(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.
2212TEST(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.
2247TEST(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.
2284TEST(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
ttuttle859dc7a2015-04-23 19:42:292320static void ETagGet_UnconditionalRequest_Handler(const HttpRequestInfo* request,
2321 std::string* response_status,
2322 std::string* response_headers,
2323 std::string* response_data) {
[email protected]bd069d72011-05-19 01:11:112324 EXPECT_FALSE(
ttuttle859dc7a2015-04-23 19:42:292325 request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch));
[email protected]bd069d72011-05-19 01:11:112326}
2327
2328TEST(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.
ttuttle859dc7a2015-04-23 19:42:292342 transaction.load_flags = LOAD_VALIDATE_CACHE;
[email protected]bd069d72011-05-19 01:11:112343 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
2351TEST(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.
ttuttle859dc7a2015-04-23 19:42:292365 transaction.load_flags = LOAD_VALIDATE_CACHE;
[email protected]bd069d72011-05-19 01:11:112366 transaction.handler = ETagGet_UnconditionalRequest_Handler;
[email protected]1dce442e2013-04-23 03:06:292367 transaction.request_headers = "Range: bytes = 5-\r\n";
[email protected]bd069d72011-05-19 01:11:112368 RunTransactionTest(cache.http_cache(), transaction);
2369
[email protected]4a620712011-07-22 17:41:092370 EXPECT_EQ(2, cache.network_layer()->transaction_count());
[email protected]bd069d72011-05-19 01:11:112371 EXPECT_EQ(1, cache.disk_cache()->open_count());
2372 EXPECT_EQ(2, cache.disk_cache()->create_count());
2373}
2374
[email protected]b7d05ab2008-12-09 19:18:412375static void ETagGet_ConditionalRequest_NoStore_Handler(
ttuttle859dc7a2015-04-23 19:42:292376 const HttpRequestInfo* request,
[email protected]b7d05ab2008-12-09 19:18:412377 std::string* response_status,
2378 std::string* response_headers,
2379 std::string* response_data) {
[email protected]8c76ae22010-04-20 22:15:432380 EXPECT_TRUE(
ttuttle859dc7a2015-04-23 19:42:292381 request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch));
[email protected]b7d05ab2008-12-09 19:18:412382 response_status->assign("HTTP/1.1 304 Not Modified");
2383 response_headers->assign("Cache-Control: no-store\n");
2384 response_data->clear();
2385}
2386
2387TEST(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.
ttuttle859dc7a2015-04-23 19:42:292401 transaction.load_flags = LOAD_VALIDATE_CACHE;
[email protected]b7d05ab2008-12-09 19:18:412402 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]4de4fb12009-08-03 22:11:182419// 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.
2427static 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]bded84c2009-07-23 00:36:062432 MockHttpCache cache;
2433
2434 // The URL we will be requesting.
thestig9d3bb0c2015-01-24 00:49:512435 const char kUrl[] = "http://foobar.com/main.css";
[email protected]bded84c2009-07-23 00:36:062436
[email protected]bded84c2009-07-23 00:36:062437 // 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]4822ae02012-09-11 17:37:592446 MockTransaction mock_network_response = { 0 };
[email protected]bded84c2009-07-23 00:36:062447 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]4822ae02012-09-11 17:37:592453 MockTransaction request = { 0 };
[email protected]bded84c2009-07-23 00:36:062454 request.url = kUrl;
2455 request.method = "GET";
2456 request.request_headers = "";
2457
[email protected]4de4fb12009-08-03 22:11:182458 net_response_1.AssignTo(&mock_network_response); // Network mock.
2459 net_response_1.AssignTo(&request); // Expected result.
[email protected]bded84c2009-07-23 00:36:062460
2461 std::string response_headers;
2462 RunTransactionTestWithResponse(
2463 cache.http_cache(), request, &response_headers);
2464
[email protected]4de4fb12009-08-03 22:11:182465 EXPECT_EQ(net_response_1.status_and_headers(), response_headers);
[email protected]bded84c2009-07-23 00:36:062466 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]6f40bf72009-07-23 17:52:372470 // Request |kUrl| a second time. Now |kNetResponse1| it is in the HTTP
[email protected]bded84c2009-07-23 00:36:062471 // cache, so we don't hit the network.
2472
ttuttle859dc7a2015-04-23 19:42:292473 request.load_flags = LOAD_ONLY_FROM_CACHE;
[email protected]4de4fb12009-08-03 22:11:182474
[email protected]bded84c2009-07-23 00:36:062475 kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock.
[email protected]4de4fb12009-08-03 22:11:182476 net_response_1.AssignTo(&request); // Expected result.
[email protected]bded84c2009-07-23 00:36:062477
2478 RunTransactionTestWithResponse(
2479 cache.http_cache(), request, &response_headers);
2480
[email protected]4de4fb12009-08-03 22:11:182481 EXPECT_EQ(net_response_1.status_and_headers(), response_headers);
[email protected]bded84c2009-07-23 00:36:062482 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]4de4fb12009-08-03 22:11:182491 request.request_headers = extra_request_headers;
ttuttle859dc7a2015-04-23 19:42:292492 request.load_flags = LOAD_NORMAL;
[email protected]bded84c2009-07-23 00:36:062493
[email protected]4de4fb12009-08-03 22:11:182494 net_response_2.AssignTo(&mock_network_response); // Network mock.
2495 net_response_2.AssignTo(&request); // Expected result.
[email protected]bded84c2009-07-23 00:36:062496
2497 RunTransactionTestWithResponse(
2498 cache.http_cache(), request, &response_headers);
2499
[email protected]4de4fb12009-08-03 22:11:182500 EXPECT_EQ(net_response_2.status_and_headers(), response_headers);
[email protected]bded84c2009-07-23 00:36:062501 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 = "";
ttuttle859dc7a2015-04-23 19:42:292511 request.load_flags = LOAD_ONLY_FROM_CACHE;
[email protected]bded84c2009-07-23 00:36:062512
2513 kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock.
[email protected]4de4fb12009-08-03 22:11:182514 cached_response_2.AssignTo(&request); // Expected result.
[email protected]bded84c2009-07-23 00:36:062515
2516 RunTransactionTestWithResponse(
2517 cache.http_cache(), request, &response_headers);
2518
[email protected]4de4fb12009-08-03 22:11:182519 EXPECT_EQ(cached_response_2.status_and_headers(), response_headers);
[email protected]bded84c2009-07-23 00:36:062520 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]4de4fb12009-08-03 22:11:182527// Check that when an "if-modified-since" header is attached
2528// to the request, the result still updates the cached entry.
2529TEST(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
thestig9d3bb0c2015-01-24 00:49:512546 const char extra_headers[] =
[email protected]1dce442e2013-04-23 03:06:292547 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
[email protected]4de4fb12009-08-03 22:11:182548
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.
2555TEST(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
thestig9d3bb0c2015-01-24 00:49:512574 const char extra_headers[] = "If-None-Match: \"ETAG1\"\r\n";
[email protected]4de4fb12009-08-03 22:11:182575
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.
2583TEST(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
thestig9d3bb0c2015-01-24 00:49:512610 const char extra_headers[] =
[email protected]1dce442e2013-04-23 03:06:292611 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
[email protected]4de4fb12009-08-03 22:11:182612
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).
2620TEST(HttpCache, ConditionalizedRequestUpdatesCache4) {
2621 MockHttpCache cache;
2622
thestig9d3bb0c2015-01-24 00:49:512623 const char kUrl[] = "http://foobar.com/main.css";
[email protected]4de4fb12009-08-03 22:11:182624
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
thestig9d3bb0c2015-01-24 00:49:512632 const char kExtraRequestHeaders[] =
[email protected]1dce442e2013-04-23 03:06:292633 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
[email protected]4de4fb12009-08-03 22:11:182634
2635 // We will control the network layer's responses for |kUrl| using
2636 // |mock_network_response|.
[email protected]4822ae02012-09-11 17:37:592637 MockTransaction mock_network_response = { 0 };
[email protected]4de4fb12009-08-03 22:11:182638 mock_network_response.url = kUrl;
2639 AddMockTransaction(&mock_network_response);
2640
[email protected]4822ae02012-09-11 17:37:592641 MockTransaction request = { 0 };
[email protected]4de4fb12009-08-03 22:11:182642 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).
2664TEST(HttpCache, ConditionalizedRequestUpdatesCache5) {
2665 MockHttpCache cache;
2666
thestig9d3bb0c2015-01-24 00:49:512667 const char kUrl[] = "http://foobar.com/main.css";
[email protected]4de4fb12009-08-03 22:11:182668
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
thestig9d3bb0c2015-01-24 00:49:512676 const char kExtraRequestHeaders[] =
[email protected]1dce442e2013-04-23 03:06:292677 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
[email protected]4de4fb12009-08-03 22:11:182678
2679 // We will control the network layer's responses for |kUrl| using
2680 // |mock_network_response|.
[email protected]4822ae02012-09-11 17:37:592681 MockTransaction mock_network_response = { 0 };
[email protected]4de4fb12009-08-03 22:11:182682 mock_network_response.url = kUrl;
2683 AddMockTransaction(&mock_network_response);
2684
[email protected]4822ae02012-09-11 17:37:592685 MockTransaction request = { 0 };
[email protected]4de4fb12009-08-03 22:11:182686 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).
2709TEST(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!
thestig9d3bb0c2015-01-24 00:49:512729 const char kExtraRequestHeaders[] =
[email protected]1dce442e2013-04-23 03:06:292730 "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n";
[email protected]4de4fb12009-08-03 22:11:182731
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.
2739TEST(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.
thestig9d3bb0c2015-01-24 00:49:512758 const char kExtraRequestHeaders[] = "If-None-Match: \"Foo2\"\r\n";
[email protected]4de4fb12009-08-03 22:11:182759
2760 ConditionalizedRequestUpdatesCacheHelper(
2761 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
2762}
2763
[email protected]f2ee7452009-11-02 21:43:022764// Test that doing an externally conditionalized request with both if-none-match
2765// and if-modified-since updates the cache.
2766TEST(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
thestig9d3bb0c2015-01-24 00:49:512784 const char kExtraRequestHeaders[] =
[email protected]2227c692010-05-04 15:36:112785 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"
2786 "If-None-Match: \"Foo1\"\r\n";
[email protected]f2ee7452009-11-02 21:43:022787
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.
2794TEST(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.
thestig9d3bb0c2015-01-24 00:49:512813 const char kExtraRequestHeaders[] =
[email protected]1dce442e2013-04-23 03:06:292814 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"
2815 "If-None-Match: \"Foo2\"\r\n";
[email protected]f2ee7452009-11-02 21:43:022816
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.
2823TEST(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.
thestig9d3bb0c2015-01-24 00:49:512842 const char kExtraRequestHeaders[] =
[email protected]1dce442e2013-04-23 03:06:292843 "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n"
2844 "If-None-Match: \"Foo1\"\r\n";
[email protected]f2ee7452009-11-02 21:43:022845
2846 ConditionalizedRequestUpdatesCacheHelper(
2847 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
2848}
2849
[email protected]6f40bf72009-07-23 17:52:372850TEST(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();
ttuttle859dc7a2015-04-23 19:42:292865 trans.load_flags = LOAD_ONLY_FROM_CACHE;
[email protected]6f40bf72009-07-23 17:52:372866
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]aa5458fd2012-04-13 00:06:302874// Tests that we skip the cache for POST requests that do not have an upload
2875// identifier.
2876TEST(HttpCache, SimplePOST_SkipsCache) {
initial.commit586acc5fe2008-07-26 22:42:522877 MockHttpCache cache;
2878
[email protected]aa5458fd2012-04-13 00:06:302879 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
rvargas8e4b4b6a2014-10-23 21:24:562886// Tests POST handling with a disabled cache (no DCHECK).
2887TEST(HttpCache, SimplePOST_DisabledCache) {
2888 MockHttpCache cache;
ttuttle859dc7a2015-04-23 19:42:292889 cache.http_cache()->set_mode(HttpCache::Mode::DISABLE);
rvargas8e4b4b6a2014-10-23 21:24:562890
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]aa5458fd2012-04-13 00:06:302898TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) {
2899 MockHttpCache cache;
initial.commit586acc5fe2008-07-26 22:42:522900
2901 MockTransaction transaction(kSimplePOST_Transaction);
ttuttle859dc7a2015-04-23 19:42:292902 transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
initial.commit586acc5fe2008-07-26 22:42:522903
2904 MockHttpRequest request(transaction);
ttuttle859dc7a2015-04-23 19:42:292905 TestCompletionCallback callback;
initial.commit586acc5fe2008-07-26 22:42:522906
ttuttle859dc7a2015-04-23 19:42:292907 scoped_ptr<HttpTransaction> trans;
2908 ASSERT_EQ(OK, cache.CreateTransaction(&trans));
[email protected]af4876d2008-10-21 23:10:572909 ASSERT_TRUE(trans.get());
initial.commit586acc5fe2008-07-26 22:42:522910
ttuttle859dc7a2015-04-23 19:42:292911 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
2912 ASSERT_EQ(ERR_CACHE_MISS, callback.GetResult(rv));
initial.commit586acc5fe2008-07-26 22:42:522913
[email protected]af4876d2008-10-21 23:10:572914 trans.reset();
initial.commit586acc5fe2008-07-26 22:42:522915
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]96bac982009-03-24 18:20:062921TEST(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 Drissman13fc8932015-12-20 04:40:462928 const int64_t kUploadId = 1; // Just a dummy value.
[email protected]96bac982009-03-24 18:20:062929
olli.raula6df48b2a2015-11-26 07:40:222930 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),
ttuttle859dc7a2015-04-23 19:42:292934 kUploadId);
[email protected]96bac982009-03-24 18:20:062935 MockHttpRequest request(transaction);
[email protected]329b68b2012-11-14 17:54:272936 request.upload_data_stream = &upload_data_stream;
[email protected]96bac982009-03-24 18:20:062937
2938 // Populate the cache.
[email protected]95792eb12009-06-22 21:30:402939 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL);
[email protected]96bac982009-03-24 18:20:062940
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.
ttuttle859dc7a2015-04-23 19:42:292946 request.load_flags |= LOAD_ONLY_FROM_CACHE;
[email protected]95792eb12009-06-22 21:30:402947 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL);
[email protected]96bac982009-03-24 18:20:062948
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]7940e162012-03-14 03:45:182954// Test that we don't hit the cache for POST requests if there is a byte range.
2955TEST(HttpCache, SimplePOST_WithRanges) {
2956 MockHttpCache cache;
2957
2958 MockTransaction transaction(kSimplePOST_Transaction);
2959 transaction.request_headers = "Range: bytes = 0-4\r\n";
2960
Avi Drissman13fc8932015-12-20 04:40:462961 const int64_t kUploadId = 1; // Just a dummy value.
[email protected]7940e162012-03-14 03:45:182962
olli.raula6df48b2a2015-11-26 07:40:222963 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),
ttuttle859dc7a2015-04-23 19:42:292967 kUploadId);
[email protected]329b68b2012-11-14 17:54:272968
[email protected]7940e162012-03-14 03:45:182969 MockHttpRequest request(transaction);
[email protected]329b68b2012-11-14 17:54:272970 request.upload_data_stream = &upload_data_stream;
[email protected]7940e162012-03-14 03:45:182971
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]aa5458fd2012-04-13 00:06:302980// Tests that a POST is cached separately from a previously cached GET.
[email protected]f42cac92012-12-21 22:59:052981TEST(HttpCache, SimplePOST_SeparateCache) {
[email protected]aa5458fd2012-04-13 00:06:302982 MockHttpCache cache;
2983
olli.raula6df48b2a2015-11-26 07:40:222984 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]aa5458fd2012-04-13 00:06:302988
[email protected]f42cac92012-12-21 22:59:052989 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.
3010TEST(HttpCache, SimplePOST_Invalidate_205) {
3011 MockHttpCache cache;
3012
3013 MockTransaction transaction(kSimpleGET_Transaction);
3014 AddMockTransaction(&transaction);
[email protected]aa5458fd2012-04-13 00:06:303015 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.raula6df48b2a2015-11-26 07:40:223024 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]329b68b2012-11-14 17:54:273028
[email protected]aa5458fd2012-04-13 00:06:303029 transaction.method = "POST";
[email protected]f42cac92012-12-21 22:59:053030 transaction.status = "HTTP/1.1 205 No Content";
[email protected]aa5458fd2012-04-13 00:06:303031 MockHttpRequest req2(transaction);
[email protected]329b68b2012-11-14 17:54:273032 req2.upload_data_stream = &upload_data_stream;
[email protected]aa5458fd2012-04-13 00:06:303033
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]f42cac92012-12-21 22:59:053039
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]cdead5122013-09-12 22:50:493048// Tests that a successful POST invalidates a previously cached GET, even when
3049// there is no upload identifier.
3050TEST(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.raula6df48b2a2015-11-26 07:40:223064 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]cdead5122013-09-12 22:50:493068
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]ed0dc6c2013-09-17 19:48:113088// Tests that processing a POST before creating the backend doesn't crash.
3089TEST(HttpCache, SimplePOST_NoUploadId_NoBackend) {
3090 // This will initialize a cache object with NULL backend.
mmenkebc31a2c2015-10-29 13:44:453091 scoped_ptr<MockBlockingBackendFactory> factory(
3092 new MockBlockingBackendFactory());
[email protected]ed0dc6c2013-09-17 19:48:113093 factory->set_fail(true);
3094 factory->FinishCreation();
mmenkebc31a2c2015-10-29 13:44:453095 MockHttpCache cache(factory.Pass());
[email protected]ed0dc6c2013-09-17 19:48:113096
olli.raula6df48b2a2015-11-26 07:40:223097 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]ed0dc6c2013-09-17 19:48:113101
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]f42cac92012-12-21 22:59:053112// Tests that we don't invalidate entries as a result of a failed POST.
3113TEST(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.raula6df48b2a2015-11-26 07:40:223127 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]f42cac92012-12-21 22:59:053131
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]aa5458fd2012-04-13 00:06:303149}
3150
[email protected]b53a4122014-07-31 00:06:463151// Tests that a HEAD request is not cached by itself.
3152TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Miss) {
3153 MockHttpCache cache;
3154 MockTransaction transaction(kSimplePOST_Transaction);
3155 AddMockTransaction(&transaction);
ttuttle859dc7a2015-04-23 19:42:293156 transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
[email protected]b53a4122014-07-31 00:06:463157 transaction.method = "HEAD";
3158
3159 MockHttpRequest request(transaction);
ttuttle859dc7a2015-04-23 19:42:293160 TestCompletionCallback callback;
[email protected]b53a4122014-07-31 00:06:463161
ttuttle859dc7a2015-04-23 19:42:293162 scoped_ptr<HttpTransaction> trans;
3163 ASSERT_EQ(OK, cache.CreateTransaction(&trans));
[email protected]b53a4122014-07-31 00:06:463164 ASSERT_TRUE(trans.get());
3165
ttuttle859dc7a2015-04-23 19:42:293166 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
3167 ASSERT_EQ(ERR_CACHE_MISS, callback.GetResult(rv));
[email protected]b53a4122014-07-31 00:06:463168
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.
3178TEST(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";
ttuttle859dc7a2015-04-23 19:42:293192 transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
[email protected]b53a4122014-07-31 00:06:463193 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.
3203TEST(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";
ttuttle859dc7a2015-04-23 19:42:293214 transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
[email protected]b53a4122014-07-31 00:06:463215 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.
3225TEST(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.
3248TEST(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";
ttuttle859dc7a2015-04-23 19:42:293259 transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
3260 transaction.return_code = ERR_CACHE_MISS;
[email protected]b53a4122014-07-31 00:06:463261 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.
3269TEST(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"));
rvargasee8204a2015-02-11 21:28:113289 EXPECT_NE(std::string::npos, headers.find("Content-Length: 80\n"));
[email protected]b53a4122014-07-31 00:06:463290 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.
3298TEST(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.
3331TEST(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.
ttuttle859dc7a2015-04-23 19:42:293358 transaction2.load_flags |= LOAD_ONLY_FROM_CACHE;
[email protected]b53a4122014-07-31 00:06:463359 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.
3369TEST(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.
ttuttle859dc7a2015-04-23 19:42:293398 transaction2.load_flags |= LOAD_ONLY_FROM_CACHE;
[email protected]b53a4122014-07-31 00:06:463399 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.
3409TEST(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";
ttuttle859dc7a2015-04-23 19:42:293425 transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
3426 transaction.return_code = ERR_CACHE_MISS;
[email protected]b53a4122014-07-31 00:06:463427 RunTransactionTest(cache.http_cache(), transaction);
3428
3429 RemoveMockTransaction(&transaction);
3430}
3431
[email protected]7940e162012-03-14 03:45:183432// Tests that we do not cache the response of a PUT.
3433TEST(HttpCache, SimplePUT_Miss) {
3434 MockHttpCache cache;
3435
3436 MockTransaction transaction(kSimplePOST_Transaction);
3437 transaction.method = "PUT";
3438
olli.raula6df48b2a2015-11-26 07:40:223439 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]329b68b2012-11-14 17:54:273443
[email protected]7940e162012-03-14 03:45:183444 MockHttpRequest request(transaction);
[email protected]329b68b2012-11-14 17:54:273445 request.upload_data_stream = &upload_data_stream;
[email protected]7940e162012-03-14 03:45:183446
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.
3456TEST(HttpCache, SimplePUT_Invalidate) {
3457 MockHttpCache cache;
3458
[email protected]f5648e92012-08-02 17:13:043459 MockTransaction transaction(kSimpleGET_Transaction);
[email protected]7940e162012-03-14 03:45:183460 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.raula6df48b2a2015-11-26 07:40:223469 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]329b68b2012-11-14 17:54:273473
[email protected]7940e162012-03-14 03:45:183474 transaction.method = "PUT";
3475 MockHttpRequest req2(transaction);
[email protected]329b68b2012-11-14 17:54:273476 req2.upload_data_stream = &upload_data_stream;
[email protected]7940e162012-03-14 03:45:183477
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]f42cac92012-12-21 22:59:053491// Tests that we invalidate entries as a result of a PUT.
3492TEST(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.raula6df48b2a2015-11-26 07:40:223506 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]f42cac92012-12-21 22:59:053510
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.
3531TEST(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.raula6df48b2a2015-11-26 07:40:223545 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]f42cac92012-12-21 22:59:053549
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]7940e162012-03-14 03:45:183569// Tests that we do not cache the response of a DELETE.
3570TEST(HttpCache, SimpleDELETE_Miss) {
3571 MockHttpCache cache;
3572
3573 MockTransaction transaction(kSimplePOST_Transaction);
3574 transaction.method = "DELETE";
3575
olli.raula6df48b2a2015-11-26 07:40:223576 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]329b68b2012-11-14 17:54:273580
[email protected]7940e162012-03-14 03:45:183581 MockHttpRequest request(transaction);
[email protected]329b68b2012-11-14 17:54:273582 request.upload_data_stream = &upload_data_stream;
[email protected]7940e162012-03-14 03:45:183583
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.
3593TEST(HttpCache, SimpleDELETE_Invalidate) {
3594 MockHttpCache cache;
3595
[email protected]f5648e92012-08-02 17:13:043596 MockTransaction transaction(kSimpleGET_Transaction);
[email protected]7940e162012-03-14 03:45:183597 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.raula6df48b2a2015-11-26 07:40:223606 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]329b68b2012-11-14 17:54:273610
[email protected]7940e162012-03-14 03:45:183611 transaction.method = "DELETE";
3612 MockHttpRequest req2(transaction);
[email protected]329b68b2012-11-14 17:54:273613 req2.upload_data_stream = &upload_data_stream;
[email protected]7940e162012-03-14 03:45:183614
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]f42cac92012-12-21 22:59:053628// Tests that we invalidate entries as a result of a DELETE.
3629TEST(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.
3661TEST(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]7d84e642013-04-11 00:04:073693// Tests that we don't invalidate entries after a failed network transaction.
3694TEST(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);
ttuttle859dc7a2015-04-23 19:42:293703 transaction.return_code = ERR_FAILED;
3704 transaction.load_flags |= LOAD_VALIDATE_CACHE;
[email protected]7d84e642013-04-11 00:04:073705
3706 AddMockTransaction(&transaction);
3707 RunTransactionTest(cache.http_cache(), transaction);
3708 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3709 RemoveMockTransaction(&transaction);
3710
ttuttle859dc7a2015-04-23 19:42:293711 transaction.load_flags = LOAD_ONLY_FROM_CACHE;
3712 transaction.return_code = OK;
[email protected]7d84e642013-04-11 00:04:073713 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.commit586acc5fe2008-07-26 22:42:523721TEST(HttpCache, RangeGET_SkipsCache) {
3722 MockHttpCache cache;
3723
[email protected]8bf26f49a2009-06-12 17:35:503724 // 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.commit586acc5fe2008-07-26 22:42:523727
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]1dce442e2013-04-23 03:06:293735 transaction.request_headers = "If-None-Match: foo\r\n";
initial.commit586acc5fe2008-07-26 22:42:523736 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]72d1e592009-03-10 17:39:463742 transaction.request_headers =
[email protected]1dce442e2013-04-23 03:06:293743 "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n";
initial.commit586acc5fe2008-07-26 22:42:523744 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]86291440d2009-08-28 18:46:353751// Test that we skip the cache for range requests that include a validation
3752// header.
3753TEST(HttpCache, RangeGET_SkipsCache2) {
3754 MockHttpCache cache;
[email protected]86291440d2009-08-28 18:46:353755
3756 MockTransaction transaction(kRangeGET_Transaction);
[email protected]8c76ae22010-04-20 22:15:433757 transaction.request_headers = "If-None-Match: foo\r\n"
[email protected]e75e8af2009-11-03 00:04:203758 EXTRA_HEADER
[email protected]1dce442e2013-04-23 03:06:293759 "Range: bytes = 40-49\r\n";
[email protected]86291440d2009-08-28 18:46:353760 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]8c76ae22010-04-20 22:15:433767 "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n"
[email protected]e75e8af2009-11-03 00:04:203768 EXTRA_HEADER
[email protected]1dce442e2013-04-23 03:06:293769 "Range: bytes = 40-49\r\n";
[email protected]86291440d2009-08-28 18:46:353770 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]8c76ae22010-04-20 22:15:433776 transaction.request_headers = "If-Range: bla\r\n"
[email protected]e75e8af2009-11-03 00:04:203777 EXTRA_HEADER
[email protected]1dce442e2013-04-23 03:06:293778 "Range: bytes = 40-49\r\n";
[email protected]86291440d2009-08-28 18:46:353779 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
rvargas80059b32015-01-02 23:39:523786TEST(HttpCache, SimpleGET_DoesntLogHeaders) {
3787 MockHttpCache cache;
3788
ttuttle859dc7a2015-04-23 19:42:293789 BoundTestNetLog log;
rvargas80059b32015-01-02 23:39:523790 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction,
3791 log.bound());
3792
3793 EXPECT_FALSE(LogContainsEventType(
ttuttle859dc7a2015-04-23 19:42:293794 log, NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS));
rvargas80059b32015-01-02 23:39:523795}
3796
3797TEST(HttpCache, RangeGET_LogsHeaders) {
3798 MockHttpCache cache;
3799
ttuttle859dc7a2015-04-23 19:42:293800 BoundTestNetLog log;
rvargas80059b32015-01-02 23:39:523801 RunTransactionTestWithLog(cache.http_cache(), kRangeGET_Transaction,
3802 log.bound());
3803
3804 EXPECT_TRUE(LogContainsEventType(
ttuttle859dc7a2015-04-23 19:42:293805 log, NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS));
rvargas80059b32015-01-02 23:39:523806}
3807
3808TEST(HttpCache, ExternalValidation_LogsHeaders) {
3809 MockHttpCache cache;
3810
ttuttle859dc7a2015-04-23 19:42:293811 BoundTestNetLog log;
rvargas80059b32015-01-02 23:39:523812 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(
ttuttle859dc7a2015-04-23 19:42:293817 log, NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS));
rvargas80059b32015-01-02 23:39:523818}
3819
3820TEST(HttpCache, SpecialHeaders_LogsHeaders) {
3821 MockHttpCache cache;
3822
ttuttle859dc7a2015-04-23 19:42:293823 BoundTestNetLog log;
rvargas80059b32015-01-02 23:39:523824 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(
ttuttle859dc7a2015-04-23 19:42:293829 log, NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS));
rvargas80059b32015-01-02 23:39:523830}
3831
[email protected]e5dad132009-08-18 00:53:413832// Tests that receiving 206 for a regular request is handled correctly.
[email protected]7ee4c4072009-06-30 18:49:473833TEST(HttpCache, GET_Crazy206) {
3834 MockHttpCache cache;
[email protected]7ee4c4072009-06-30 18:49:473835
[email protected]7ee4c4072009-06-30 18:49:473836 // Write to the cache.
3837 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]44f873a62009-08-12 00:14:483838 AddMockTransaction(&transaction);
[email protected]e75e8af2009-11-03 00:04:203839 transaction.request_headers = EXTRA_HEADER;
[email protected]44f873a62009-08-12 00:14:483840 transaction.handler = NULL;
[email protected]7ee4c4072009-06-30 18:49:473841 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]21f659d2009-08-24 17:59:313851 EXPECT_EQ(0, cache.disk_cache()->open_count());
3852 EXPECT_EQ(2, cache.disk_cache()->create_count());
[email protected]44f873a62009-08-12 00:14:483853 RemoveMockTransaction(&transaction);
[email protected]7ee4c4072009-06-30 18:49:473854}
3855
[email protected]6a2e83ea2014-01-02 20:47:563856// Tests that receiving 416 for a regular request is handled correctly.
3857TEST(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
rvargas43dc8fd2015-01-07 23:03:253873// Tests that we don't store partial responses that can't be validated.
[email protected]8a301142011-04-13 18:33:403874TEST(HttpCache, RangeGET_NoStrongValidators) {
3875 MockHttpCache cache;
3876 std::string headers;
3877
rvargas43dc8fd2015-01-07 23:03:253878 // Attempt to write to the cache (40-49).
3879 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
rvargas439a05cd2014-08-30 01:43:563880 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
rvargas43dc8fd2015-01-07 23:03:253890 // Now verify that there's no cached data.
rvargas439a05cd2014-08-30 01:43:563891 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
3892 &headers);
3893
3894 Verify206Response(headers, 40, 49);
rvargas43dc8fd2015-01-07 23:03:253895 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());
rvargas439a05cd2014-08-30 01:43:563898}
3899
rvargas43dc8fd2015-01-07 23:03:253900// Tests failures to conditionalize byte range requests.
3901TEST(HttpCache, RangeGET_NoConditionalization) {
rvargas439a05cd2014-08-30 01:43:563902 MockHttpCache cache;
rvargas43dc8fd2015-01-07 23:03:253903 cache.FailConditionalizations();
rvargas439a05cd2014-08-30 01:43:563904 std::string headers;
3905
3906 // Write to the cache (40-49).
rvargas43dc8fd2015-01-07 23:03:253907 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
[email protected]8a301142011-04-13 18:33:403908 transaction.response_headers = "Content-Length: 10\n"
rvargas43dc8fd2015-01-07 23:03:253909 "ETag: \"foo\"\n";
[email protected]8a301142011-04-13 18:33:403910 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
rvargas439a05cd2014-08-30 01:43:563917 // Now verify that the cached data is not used.
[email protected]8a301142011-04-13 18:33:403918 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
3919 &headers);
3920
3921 Verify206Response(headers, 40, 49);
3922 EXPECT_EQ(2, cache.network_layer()->transaction_count());
rvargas439a05cd2014-08-30 01:43:563923 EXPECT_EQ(1, cache.disk_cache()->open_count());
[email protected]8a301142011-04-13 18:33:403924 EXPECT_EQ(2, cache.disk_cache()->create_count());
[email protected]8a301142011-04-13 18:33:403925}
3926
rvargas80059b32015-01-02 23:39:523927// Tests that restarting a partial request when the cached data cannot be
3928// revalidated logs an event.
3929TEST(HttpCache, RangeGET_NoValidation_LogsRestart) {
3930 MockHttpCache cache;
rvargas43dc8fd2015-01-07 23:03:253931 cache.FailConditionalizations();
rvargas80059b32015-01-02 23:39:523932
3933 // Write to the cache (40-49).
3934 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
rvargas43dc8fd2015-01-07 23:03:253935 transaction.response_headers = "Content-Length: 10\n"
3936 "ETag: \"foo\"\n";
rvargas80059b32015-01-02 23:39:523937 RunTransactionTest(cache.http_cache(), transaction);
3938
3939 // Now verify that the cached data is not used.
ttuttle859dc7a2015-04-23 19:42:293940 BoundTestNetLog log;
rvargas80059b32015-01-02 23:39:523941 RunTransactionTestWithLog(cache.http_cache(), kRangeGET_TransactionOK,
3942 log.bound());
3943
3944 EXPECT_TRUE(LogContainsEventType(
ttuttle859dc7a2015-04-23 19:42:293945 log, NetLog::TYPE_HTTP_CACHE_RESTART_PARTIAL_REQUEST));
rvargas80059b32015-01-02 23:39:523946}
3947
rvargas43dc8fd2015-01-07 23:03:253948// Tests that a failure to conditionalize a regular request (no range) with a
3949// sparse entry results in a full response.
3950TEST(HttpCache, GET_NoConditionalization) {
rvargasb4991562015-01-07 21:59:573951 MockHttpCache cache;
rvargas43dc8fd2015-01-07 23:03:253952 cache.FailConditionalizations();
rvargasb4991562015-01-07 21:59:573953 std::string headers;
3954
3955 // Write to the cache (40-49).
3956 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
rvargas43dc8fd2015-01-07 23:03:253957 transaction.response_headers = "Content-Length: 10\n"
3958 "ETag: \"foo\"\n";
rvargasb4991562015-01-07 21:59:573959 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
rvargas43dc8fd2015-01-07 23:03:253986// 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.
3989TEST(HttpCache, RangeGET_NoConditionalization2) {
rvargasb4991562015-01-07 21:59:573990 MockHttpCache cache;
rvargas43dc8fd2015-01-07 23:03:253991 cache.FailConditionalizations();
rvargasb4991562015-01-07 21:59:573992 std::string headers;
3993
3994 // Write to the cache (40-49).
3995 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
rvargas43dc8fd2015-01-07 23:03:253996 transaction.response_headers = "Content-Length: 10\n"
3997 "ETag: \"foo\"\n";
rvargasb4991562015-01-07 21:59:573998 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]9f10ec32013-11-01 00:51:534026// Tests that we cache partial responses that lack content-length.
4027TEST(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
tfarinae04a95b2015-09-18 22:48:124057// 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]e5dad132009-08-18 00:53:414063// Tests that we can cache range requests and fetch random blocks from the
4064// cache and the network.
tfarinae04a95b2015-09-18 22:48:124065TEST(HttpCache, MAYBE_RangeGET_OK) {
[email protected]8bf26f49a2009-06-12 17:35:504066 MockHttpCache cache;
4067 AddMockTransaction(&kRangeGET_TransactionOK);
[email protected]95792eb12009-06-22 21:30:404068 std::string headers;
[email protected]8bf26f49a2009-06-12 17:35:504069
[email protected]95792eb12009-06-22 21:30:404070 // Write to the cache (40-49).
4071 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
4072 &headers);
4073
[email protected]8c76ae22010-04-20 22:15:434074 Verify206Response(headers, 40, 49);
[email protected]8bf26f49a2009-06-12 17:35:504075 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]95792eb12009-06-22 21:30:404080 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
4081 &headers);
[email protected]8bf26f49a2009-06-12 17:35:504082
[email protected]8c76ae22010-04-20 22:15:434083 Verify206Response(headers, 40, 49);
[email protected]5beca812010-06-24 17:55:244084 EXPECT_EQ(1, cache.network_layer()->transaction_count());
[email protected]8bf26f49a2009-06-12 17:35:504085 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]2da659e2013-05-23 20:51:344089 base::MessageLoop::current()->RunUntilIdle();
[email protected]8bf26f49a2009-06-12 17:35:504090
4091 // Write to the cache (30-39).
4092 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]e75e8af2009-11-03 00:04:204093 transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER;
[email protected]8bf26f49a2009-06-12 17:35:504094 transaction.data = "rg: 30-39 ";
[email protected]95792eb12009-06-22 21:30:404095 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
[email protected]8bf26f49a2009-06-12 17:35:504096
[email protected]8c76ae22010-04-20 22:15:434097 Verify206Response(headers, 30, 39);
[email protected]5beca812010-06-24 17:55:244098 EXPECT_EQ(2, cache.network_layer()->transaction_count());
[email protected]8bf26f49a2009-06-12 17:35:504099 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]2da659e2013-05-23 20:51:344103 base::MessageLoop::current()->RunUntilIdle();
[email protected]8bf26f49a2009-06-12 17:35:504104
4105 // Write and read from the cache (20-59).
[email protected]e75e8af2009-11-03 00:04:204106 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER;
[email protected]8bf26f49a2009-06-12 17:35:504107 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 ";
ttuttle859dc7a2015-04-23 19:42:294108 BoundTestNetLog log;
4109 LoadTimingInfo load_timing_info;
[email protected]3b23a222013-05-15 21:33:254110 RunTransactionTestWithResponseAndGetTiming(
4111 cache.http_cache(), transaction, &headers, log.bound(),
4112 &load_timing_info);
[email protected]8bf26f49a2009-06-12 17:35:504113
[email protected]8c76ae22010-04-20 22:15:434114 Verify206Response(headers, 20, 59);
[email protected]5beca812010-06-24 17:55:244115 EXPECT_EQ(4, cache.network_layer()->transaction_count());
[email protected]8bf26f49a2009-06-12 17:35:504116 EXPECT_EQ(3, cache.disk_cache()->open_count());
4117 EXPECT_EQ(1, cache.disk_cache()->create_count());
[email protected]3b23a222013-05-15 21:33:254118 TestLoadTimingNetworkRequest(load_timing_info);
[email protected]8bf26f49a2009-06-12 17:35:504119
4120 RemoveMockTransaction(&kRangeGET_TransactionOK);
4121}
4122
tfarinae04a95b2015-09-18 22:48:124123// 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]21e743202009-12-18 01:31:044129// Tests that we can cache range requests and fetch random blocks from the
4130// cache and the network, with synchronous responses.
tfarinae04a95b2015-09-18 22:48:124131TEST(HttpCache, MAYBE_RangeGET_SyncOK) {
[email protected]21e743202009-12-18 01:31:044132 MockHttpCache cache;
[email protected]21e743202009-12-18 01:31:044133
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]8c76ae22010-04-20 22:15:434142 Verify206Response(headers, 40, 49);
[email protected]21e743202009-12-18 01:31:044143 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]8c76ae22010-04-20 22:15:434150 Verify206Response(headers, 40, 49);
[email protected]5beca812010-06-24 17:55:244151 EXPECT_EQ(1, cache.network_layer()->transaction_count());
[email protected]21e743202009-12-18 01:31:044152 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]2da659e2013-05-23 20:51:344156 base::MessageLoop::current()->RunUntilIdle();
[email protected]21e743202009-12-18 01:31:044157
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]8c76ae22010-04-20 22:15:434163 Verify206Response(headers, 30, 39);
[email protected]5beca812010-06-24 17:55:244164 EXPECT_EQ(2, cache.network_layer()->transaction_count());
[email protected]21e743202009-12-18 01:31:044165 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]2da659e2013-05-23 20:51:344169 base::MessageLoop::current()->RunUntilIdle();
[email protected]21e743202009-12-18 01:31:044170
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 ";
ttuttle859dc7a2015-04-23 19:42:294174 BoundTestNetLog log;
4175 LoadTimingInfo load_timing_info;
[email protected]3b23a222013-05-15 21:33:254176 RunTransactionTestWithResponseAndGetTiming(
4177 cache.http_cache(), transaction, &headers, log.bound(),
4178 &load_timing_info);
[email protected]21e743202009-12-18 01:31:044179
[email protected]8c76ae22010-04-20 22:15:434180 Verify206Response(headers, 20, 59);
[email protected]5beca812010-06-24 17:55:244181 EXPECT_EQ(4, cache.network_layer()->transaction_count());
[email protected]21e743202009-12-18 01:31:044182 EXPECT_EQ(2, cache.disk_cache()->open_count());
4183 EXPECT_EQ(1, cache.disk_cache()->create_count());
[email protected]3b23a222013-05-15 21:33:254184 TestLoadTimingNetworkRequest(load_timing_info);
[email protected]21e743202009-12-18 01:31:044185
4186 RemoveMockTransaction(&transaction);
4187}
4188
rvargas9ba24452015-07-18 03:13:254189// 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.
4192TEST(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]5beca812010-06-24 17:55:244217// Tests that we don't revalidate an entry unless we are required to do so.
4218TEST(HttpCache, RangeGET_Revalidate1) {
4219 MockHttpCache cache;
[email protected]5beca812010-06-24 17:55:244220 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).
ttuttle859dc7a2015-04-23 19:42:294239 BoundTestNetLog log;
4240 LoadTimingInfo load_timing_info;
[email protected]3b23a222013-05-15 21:33:254241 RunTransactionTestWithResponseAndGetTiming(
4242 cache.http_cache(), transaction, &headers, log.bound(),
4243 &load_timing_info);
[email protected]5beca812010-06-24 17:55:244244
[email protected]3b23a222013-05-15 21:33:254245 Verify206Response(headers, 40, 49);
[email protected]5beca812010-06-24 17:55:244246 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]3b23a222013-05-15 21:33:254249 TestLoadTimingCachedResponse(load_timing_info);
[email protected]5beca812010-06-24 17:55:244250
4251 // Read again forcing the revalidation.
ttuttle859dc7a2015-04-23 19:42:294252 transaction.load_flags |= LOAD_VALIDATE_CACHE;
[email protected]3b23a222013-05-15 21:33:254253 RunTransactionTestWithResponseAndGetTiming(
4254 cache.http_cache(), transaction, &headers, log.bound(),
4255 &load_timing_info);
[email protected]5beca812010-06-24 17:55:244256
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]3b23a222013-05-15 21:33:254261 TestLoadTimingNetworkRequest(load_timing_info);
[email protected]5beca812010-06-24 17:55:244262
4263 RemoveMockTransaction(&transaction);
4264}
4265
tfarinae04a95b2015-09-18 22:48:124266// 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]5beca812010-06-24 17:55:244272// Checks that we revalidate an entry when the headers say so.
tfarinae04a95b2015-09-18 22:48:124273TEST(HttpCache, MAYBE_RangeGET_Revalidate2) {
[email protected]5beca812010-06-24 17:55:244274 MockHttpCache cache;
[email protected]5beca812010-06-24 17:55:244275 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]e5dad132009-08-18 00:53:414304// Tests that we deal with 304s for range requests.
[email protected]21f659d2009-08-24 17:59:314305TEST(HttpCache, RangeGET_304) {
[email protected]e5dad132009-08-18 00:53:414306 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]8c76ae22010-04-20 22:15:434314 Verify206Response(headers, 40, 49);
[email protected]e5dad132009-08-18 00:53:414315 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]5beca812010-06-24 17:55:244322 MockTransaction transaction(kRangeGET_TransactionOK);
ttuttle859dc7a2015-04-23 19:42:294323 transaction.load_flags |= LOAD_VALIDATE_CACHE;
[email protected]5beca812010-06-24 17:55:244324 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
[email protected]e5dad132009-08-18 00:53:414325
[email protected]8c76ae22010-04-20 22:15:434326 Verify206Response(headers, 40, 49);
[email protected]e5dad132009-08-18 00:53:414327 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]a79837892009-08-20 21:18:294334// Tests that we deal with 206s when revalidating range requests.
[email protected]21f659d2009-08-24 17:59:314335TEST(HttpCache, RangeGET_ModifiedResult) {
[email protected]a79837892009-08-20 21:18:294336 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]8c76ae22010-04-20 22:15:434344 Verify206Response(headers, 40, 49);
[email protected]a79837892009-08-20 21:18:294345 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]5beca812010-06-24 17:55:244352 MockTransaction transaction(kRangeGET_TransactionOK);
ttuttle859dc7a2015-04-23 19:42:294353 transaction.load_flags |= LOAD_VALIDATE_CACHE;
[email protected]5beca812010-06-24 17:55:244354 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
[email protected]a79837892009-08-20 21:18:294355
[email protected]8c76ae22010-04-20 22:15:434356 Verify206Response(headers, 40, 49);
[email protected]a79837892009-08-20 21:18:294357 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
rvargas3b57e37a2015-01-06 00:56:344370// 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.
4375TEST(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.
4399TEST(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.
4440TEST(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.
4478TEST(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.
4508TEST(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.
4546TEST(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.
4567TEST(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.
4594TEST(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]9f03cb7a2012-07-30 23:15:204615// Tests that we cache 301s for range requests.
4616TEST(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]9f03cb7a2012-07-30 23:15:204623
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]9f03cb7a2012-07-30 23:15:204635}
4636
[email protected]e5dad132009-08-18 00:53:414637// 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]21f659d2009-08-24 17:59:314639TEST(HttpCache, UnknownRangeGET_1) {
[email protected]67fe45c2009-06-24 17:44:574640 MockHttpCache cache;
4641 AddMockTransaction(&kRangeGET_TransactionOK);
[email protected]67fe45c2009-06-24 17:44:574642 std::string headers;
4643
4644 // Write to the cache (70-79).
4645 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]e75e8af2009-11-03 00:04:204646 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
[email protected]67fe45c2009-06-24 17:44:574647 transaction.data = "rg: 70-79 ";
4648 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4649
[email protected]8c76ae22010-04-20 22:15:434650 Verify206Response(headers, 70, 79);
[email protected]67fe45c2009-06-24 17:44:574651 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]2da659e2013-05-23 20:51:344656 base::MessageLoop::current()->RunUntilIdle();
[email protected]67fe45c2009-06-24 17:44:574657
4658 // Write and read from the cache (60-79).
[email protected]e75e8af2009-11-03 00:04:204659 transaction.request_headers = "Range: bytes = 60-\r\n" EXTRA_HEADER;
[email protected]67fe45c2009-06-24 17:44:574660 transaction.data = "rg: 60-69 rg: 70-79 ";
4661 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4662
[email protected]8c76ae22010-04-20 22:15:434663 Verify206Response(headers, 60, 79);
[email protected]44f873a62009-08-12 00:14:484664 EXPECT_EQ(2, cache.network_layer()->transaction_count());
[email protected]67fe45c2009-06-24 17:44:574665 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]e5dad132009-08-18 00:53:414671// 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]21f659d2009-08-24 17:59:314674TEST(HttpCache, UnknownRangeGET_2) {
[email protected]67fe45c2009-06-24 17:44:574675 MockHttpCache cache;
[email protected]67fe45c2009-06-24 17:44:574676 std::string headers;
4677
[email protected]67fe45c2009-06-24 17:44:574678 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]44f873a62009-08-12 00:14:484679 transaction.test_mode = TEST_MODE_SYNC_CACHE_START |
[email protected]73cae572009-10-22 18:36:194680 TEST_MODE_SYNC_CACHE_READ |
4681 TEST_MODE_SYNC_CACHE_WRITE;
[email protected]44f873a62009-08-12 00:14:484682 AddMockTransaction(&transaction);
4683
4684 // Write to the cache (70-79).
[email protected]e75e8af2009-11-03 00:04:204685 transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER;
[email protected]67fe45c2009-06-24 17:44:574686 transaction.data = "rg: 70-79 ";
4687 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4688
[email protected]8c76ae22010-04-20 22:15:434689 Verify206Response(headers, 70, 79);
[email protected]67fe45c2009-06-24 17:44:574690 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]2da659e2013-05-23 20:51:344695 base::MessageLoop::current()->RunUntilIdle();
[email protected]67fe45c2009-06-24 17:44:574696
4697 // Write and read from the cache (60-79).
[email protected]e75e8af2009-11-03 00:04:204698 transaction.request_headers = "Range: bytes = -20\r\n" EXTRA_HEADER;
[email protected]67fe45c2009-06-24 17:44:574699 transaction.data = "rg: 60-69 rg: 70-79 ";
4700 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4701
[email protected]8c76ae22010-04-20 22:15:434702 Verify206Response(headers, 60, 79);
[email protected]44f873a62009-08-12 00:14:484703 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]e5dad132009-08-18 00:53:414710// Tests that receiving Not Modified when asking for an open range doesn't mess
4711// up things.
[email protected]21f659d2009-08-24 17:59:314712TEST(HttpCache, UnknownRangeGET_304) {
[email protected]e5dad132009-08-18 00:53:414713 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]e75e8af2009-11-03 00:04:204723 transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER;
[email protected]a5c9d982010-10-12 20:48:024724 transaction.data = "";
[email protected]e5dad132009-08-18 00:53:414725 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]21f659d2009-08-24 17:59:314740TEST(HttpCache, GET_Previous206) {
[email protected]44f873a62009-08-12 00:14:484741 MockHttpCache cache;
4742 AddMockTransaction(&kRangeGET_TransactionOK);
[email protected]44f873a62009-08-12 00:14:484743 std::string headers;
ttuttle859dc7a2015-04-23 19:42:294744 BoundTestNetLog log;
4745 LoadTimingInfo load_timing_info;
[email protected]44f873a62009-08-12 00:14:484746
4747 // Write to the cache (40-49).
[email protected]3b23a222013-05-15 21:33:254748 RunTransactionTestWithResponseAndGetTiming(
4749 cache.http_cache(), kRangeGET_TransactionOK, &headers, log.bound(),
4750 &load_timing_info);
[email protected]44f873a62009-08-12 00:14:484751
[email protected]8c76ae22010-04-20 22:15:434752 Verify206Response(headers, 40, 49);
[email protected]44f873a62009-08-12 00:14:484753 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]3b23a222013-05-15 21:33:254756 TestLoadTimingNetworkRequest(load_timing_info);
[email protected]44f873a62009-08-12 00:14:484757
4758 // Write and read from the cache (0-79), when not asked for a range.
4759 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]e75e8af2009-11-03 00:04:204760 transaction.request_headers = EXTRA_HEADER;
rvargas3b57e37a2015-01-06 00:56:344761 transaction.data = kFullRangeData;
[email protected]3b23a222013-05-15 21:33:254762 RunTransactionTestWithResponseAndGetTiming(
4763 cache.http_cache(), transaction, &headers, log.bound(),
4764 &load_timing_info);
[email protected]44f873a62009-08-12 00:14:484765
4766 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
[email protected]67fe45c2009-06-24 17:44:574767 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]3b23a222013-05-15 21:33:254770 TestLoadTimingNetworkRequest(load_timing_info);
[email protected]67fe45c2009-06-24 17:44:574771
4772 RemoveMockTransaction(&kRangeGET_TransactionOK);
4773}
4774
[email protected]d9adff2c2009-09-05 01:15:454775// Tests that we can handle non-range requests when we have cached the first
[email protected]06c351a2010-12-03 19:11:294776// part of the object and the server replies with 304 (Not Modified).
[email protected]d9adff2c2009-09-05 01:15:454777TEST(HttpCache, GET_Previous206_NotModified) {
4778 MockHttpCache cache;
[email protected]d9adff2c2009-09-05 01:15:454779
4780 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]d9adff2c2009-09-05 01:15:454781 AddMockTransaction(&transaction);
4782 std::string headers;
ttuttle859dc7a2015-04-23 19:42:294783 BoundTestNetLog log;
4784 LoadTimingInfo load_timing_info;
[email protected]d9adff2c2009-09-05 01:15:454785
4786 // Write to the cache (0-9).
[email protected]06c351a2010-12-03 19:11:294787 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
4788 transaction.data = "rg: 00-09 ";
[email protected]3b23a222013-05-15 21:33:254789 RunTransactionTestWithResponseAndGetTiming(
4790 cache.http_cache(), transaction, &headers, log.bound(),
4791 &load_timing_info);
[email protected]8c76ae22010-04-20 22:15:434792 Verify206Response(headers, 0, 9);
[email protected]3b23a222013-05-15 21:33:254793 TestLoadTimingNetworkRequest(load_timing_info);
[email protected]06c351a2010-12-03 19:11:294794
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]3b23a222013-05-15 21:33:254798 RunTransactionTestWithResponseAndGetTiming(
4799 cache.http_cache(), transaction, &headers, log.bound(),
4800 &load_timing_info);
[email protected]06c351a2010-12-03 19:11:294801 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]d9adff2c2009-09-05 01:15:454805 EXPECT_EQ(1, cache.disk_cache()->create_count());
[email protected]3b23a222013-05-15 21:33:254806 TestLoadTimingNetworkRequest(load_timing_info);
[email protected]d9adff2c2009-09-05 01:15:454807
[email protected]06c351a2010-12-03 19:11:294808 // Read from the cache (0-9), write and read from cache (10 - 79).
ttuttle859dc7a2015-04-23 19:42:294809 transaction.load_flags |= LOAD_VALIDATE_CACHE;
[email protected]06c351a2010-12-03 19:11:294810 transaction.request_headers = "Foo: bar\r\n" EXTRA_HEADER;
rvargas3b57e37a2015-01-06 00:56:344811 transaction.data = kFullRangeData;
[email protected]3b23a222013-05-15 21:33:254812 RunTransactionTestWithResponseAndGetTiming(
4813 cache.http_cache(), transaction, &headers, log.bound(),
4814 &load_timing_info);
[email protected]d9adff2c2009-09-05 01:15:454815
4816 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
[email protected]06c351a2010-12-03 19:11:294817 EXPECT_EQ(4, cache.network_layer()->transaction_count());
4818 EXPECT_EQ(2, cache.disk_cache()->open_count());
[email protected]d9adff2c2009-09-05 01:15:454819 EXPECT_EQ(1, cache.disk_cache()->create_count());
[email protected]3b23a222013-05-15 21:33:254820 TestLoadTimingNetworkRequest(load_timing_info);
[email protected]d9adff2c2009-09-05 01:15:454821
4822 RemoveMockTransaction(&transaction);
4823}
4824
[email protected]a189bce2009-12-01 01:59:124825// Tests that we can handle a regular request to a sparse entry, that results in
4826// new content provided by the server (206).
4827TEST(HttpCache, GET_Previous206_NewContent) {
4828 MockHttpCache cache;
[email protected]a189bce2009-12-01 01:59:124829 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]8c76ae22010-04-20 22:15:434838 Verify206Response(headers, 0, 9);
[email protected]a189bce2009-12-01 01:59:124839 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;
ttuttle859dc7a2015-04-23 19:42:294849 transaction2.load_flags |= LOAD_VALIDATE_CACHE;
[email protected]634739b2011-03-02 18:08:254850 transaction2.data = "Not a range";
[email protected]a189bce2009-12-01 01:59:124851 RangeTransactionServer handler;
4852 handler.set_modified(true);
ttuttle859dc7a2015-04-23 19:42:294853 BoundTestNetLog log;
4854 LoadTimingInfo load_timing_info;
[email protected]3b23a222013-05-15 21:33:254855 RunTransactionTestWithResponseAndGetTiming(
4856 cache.http_cache(), transaction2, &headers, log.bound(),
4857 &load_timing_info);
[email protected]a189bce2009-12-01 01:59:124858
[email protected]634739b2011-03-02 18:08:254859 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
[email protected]a189bce2009-12-01 01:59:124860 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]3b23a222013-05-15 21:33:254863 TestLoadTimingNetworkRequest(load_timing_info);
[email protected]a189bce2009-12-01 01:59:124864
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]e5dad132009-08-18 00:53:414872// Tests that we can handle cached 206 responses that are not sparse.
[email protected]21f659d2009-08-24 17:59:314873TEST(HttpCache, GET_Previous206_NotSparse) {
[email protected]44f873a62009-08-12 00:14:484874 MockHttpCache cache;
4875
[email protected]44f873a62009-08-12 00:14:484876 // Create a disk cache entry that stores 206 headers while not being sparse.
4877 disk_cache::Entry* entry;
[email protected]f6f1bebc2011-01-07 03:04:544878 ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry,
4879 NULL));
[email protected]44f873a62009-08-12 00:14:484880
4881 std::string raw_headers(kRangeGET_TransactionOK.status);
4882 raw_headers.append("\n");
4883 raw_headers.append(kRangeGET_TransactionOK.response_headers);
ttuttle859dc7a2015-04-23 19:42:294884 raw_headers =
4885 HttpUtil::AssembleRawHeaders(raw_headers.data(), raw_headers.size());
[email protected]44f873a62009-08-12 00:14:484886
ttuttle859dc7a2015-04-23 19:42:294887 HttpResponseInfo response;
4888 response.headers = new HttpResponseHeaders(raw_headers);
[email protected]02e7a012010-05-10 23:06:334889 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
[email protected]44f873a62009-08-12 00:14:484890
ttuttle859dc7a2015-04-23 19:42:294891 scoped_refptr<IOBuffer> buf(new IOBuffer(500));
[email protected]44f873a62009-08-12 00:14:484892 int len = static_cast<int>(base::strlcpy(buf->data(),
4893 kRangeGET_TransactionOK.data, 500));
ttuttle859dc7a2015-04-23 19:42:294894 TestCompletionCallback cb;
[email protected]90499482013-06-01 00:39:504895 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true);
[email protected]02e7a012010-05-10 23:06:334896 EXPECT_EQ(len, cb.GetResult(rv));
[email protected]44f873a62009-08-12 00:14:484897 entry->Close();
4898
4899 // Now see that we don't use the stored entry.
4900 std::string headers;
ttuttle859dc7a2015-04-23 19:42:294901 BoundTestNetLog log;
4902 LoadTimingInfo load_timing_info;
[email protected]3b23a222013-05-15 21:33:254903 RunTransactionTestWithResponseAndGetTiming(
4904 cache.http_cache(), kSimpleGET_Transaction, &headers, log.bound(),
4905 &load_timing_info);
[email protected]44f873a62009-08-12 00:14:484906
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]3b23a222013-05-15 21:33:254915 TestLoadTimingNetworkRequest(load_timing_info);
[email protected]44f873a62009-08-12 00:14:484916}
4917
[email protected]e5dad132009-08-18 00:53:414918// 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]21f659d2009-08-24 17:59:314920TEST(HttpCache, RangeGET_Previous206_NotSparse_2) {
[email protected]44f873a62009-08-12 00:14:484921 MockHttpCache cache;
4922 AddMockTransaction(&kRangeGET_TransactionOK);
4923
[email protected]44f873a62009-08-12 00:14:484924 // Create a disk cache entry that stores 206 headers while not being sparse.
4925 disk_cache::Entry* entry;
[email protected]f6f1bebc2011-01-07 03:04:544926 ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry,
4927 NULL));
[email protected]44f873a62009-08-12 00:14:484928
4929 std::string raw_headers(kRangeGET_TransactionOK.status);
4930 raw_headers.append("\n");
4931 raw_headers.append(kRangeGET_TransactionOK.response_headers);
ttuttle859dc7a2015-04-23 19:42:294932 raw_headers =
4933 HttpUtil::AssembleRawHeaders(raw_headers.data(), raw_headers.size());
[email protected]44f873a62009-08-12 00:14:484934
ttuttle859dc7a2015-04-23 19:42:294935 HttpResponseInfo response;
4936 response.headers = new HttpResponseHeaders(raw_headers);
[email protected]02e7a012010-05-10 23:06:334937 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
[email protected]44f873a62009-08-12 00:14:484938
ttuttle859dc7a2015-04-23 19:42:294939 scoped_refptr<IOBuffer> buf(new IOBuffer(500));
[email protected]44f873a62009-08-12 00:14:484940 int len = static_cast<int>(base::strlcpy(buf->data(),
4941 kRangeGET_TransactionOK.data, 500));
ttuttle859dc7a2015-04-23 19:42:294942 TestCompletionCallback cb;
[email protected]90499482013-06-01 00:39:504943 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true);
[email protected]02e7a012010-05-10 23:06:334944 EXPECT_EQ(len, cb.GetResult(rv));
[email protected]44f873a62009-08-12 00:14:484945 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]8c76ae22010-04-20 22:15:434953 Verify206Response(headers, 40, 49);
[email protected]44f873a62009-08-12 00:14:484954 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]8a301142011-04-13 18:33:404961// Tests that we can handle cached 206 responses that can't be validated.
4962TEST(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");
ttuttle859dc7a2015-04-23 19:42:294974 raw_headers =
4975 HttpUtil::AssembleRawHeaders(raw_headers.data(), raw_headers.size());
[email protected]8a301142011-04-13 18:33:404976
ttuttle859dc7a2015-04-23 19:42:294977 HttpResponseInfo response;
4978 response.headers = new HttpResponseHeaders(raw_headers);
[email protected]8a301142011-04-13 18:33:404979 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
4980
ttuttle859dc7a2015-04-23 19:42:294981 scoped_refptr<IOBuffer> buf(new IOBuffer(500));
[email protected]8a301142011-04-13 18:33:404982 int len = static_cast<int>(base::strlcpy(buf->data(),
4983 kRangeGET_TransactionOK.data, 500));
ttuttle859dc7a2015-04-23 19:42:294984 TestCompletionCallback cb;
[email protected]90499482013-06-01 00:39:504985 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true);
[email protected]8a301142011-04-13 18:33:404986 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
tfarinae04a95b2015-09-18 22:48:125004// 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]e5dad132009-08-18 00:53:415010// Tests that we can handle range requests with cached 200 responses.
tfarinae04a95b2015-09-18 22:48:125011TEST(HttpCache, MAYBE_RangeGET_Previous200) {
[email protected]e5dad132009-08-18 00:53:415012 MockHttpCache cache;
5013
5014 // Store the whole thing with status 200.
5015 MockTransaction transaction(kTypicalGET_Transaction);
5016 transaction.url = kRangeGET_TransactionOK.url;
rvargas3b57e37a2015-01-06 00:56:345017 transaction.data = kFullRangeData;
[email protected]e5dad132009-08-18 00:53:415018 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]8c76ae22010-04-20 22:15:435035 Verify206Response(headers, 40, 49);
[email protected]e5dad132009-08-18 00:53:415036 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]8f28d632009-10-01 22:09:215040 // The last transaction has finished so make sure the entry is deactivated.
[email protected]2da659e2013-05-23 20:51:345041 base::MessageLoop::current()->RunUntilIdle();
[email protected]8f28d632009-10-01 22:09:215042
[email protected]a5c9d982010-10-12 20:48:025043 // 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]9f03cb7a2012-07-30 23:15:205046 transaction3.data = transaction.data;
ttuttle859dc7a2015-04-23 19:42:295047 transaction3.load_flags = LOAD_PREFERRING_CACHE;
[email protected]a5c9d982010-10-12 20:48:025048 RunTransactionTestWithResponse(cache.http_cache(), transaction3, &headers);
5049 EXPECT_EQ(2, cache.disk_cache()->open_count());
[email protected]9f03cb7a2012-07-30 23:15:205050 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]a5c9d982010-10-12 20:48:025053
5054 // Make sure the entry is deactivated.
[email protected]2da659e2013-05-23 20:51:345055 base::MessageLoop::current()->RunUntilIdle();
[email protected]a5c9d982010-10-12 20:48:025056
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]2da659e2013-05-23 20:51:345062 base::MessageLoop::current()->RunUntilIdle();
[email protected]a5c9d982010-10-12 20:48:025063
[email protected]e5dad132009-08-18 00:53:415064 // 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]8c76ae22010-04-20 22:15:435068 Verify206Response(headers, 40, 49);
[email protected]9f03cb7a2012-07-30 23:15:205069 EXPECT_EQ(4, cache.network_layer()->transaction_count());
[email protected]a5c9d982010-10-12 20:48:025070 EXPECT_EQ(4, cache.disk_cache()->open_count());
[email protected]e5dad132009-08-18 00:53:415071 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]21f659d2009-08-24 17:59:315080TEST(HttpCache, RangeRequestResultsIn200) {
[email protected]44f873a62009-08-12 00:14:485081 MockHttpCache cache;
5082 AddMockTransaction(&kRangeGET_TransactionOK);
[email protected]44f873a62009-08-12 00:14:485083 std::string headers;
5084
5085 // Write to the cache (70-79).
5086 MockTransaction transaction(kRangeGET_TransactionOK);
[email protected]e75e8af2009-11-03 00:04:205087 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
[email protected]44f873a62009-08-12 00:14:485088 transaction.data = "rg: 70-79 ";
5089 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5090
[email protected]8c76ae22010-04-20 22:15:435091 Verify206Response(headers, 70, 79);
[email protected]44f873a62009-08-12 00:14:485092 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]e5dad132009-08-18 00:53:415118// 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]21f659d2009-08-24 17:59:315120TEST(HttpCache, RangeGET_MoreThanCurrentSize) {
[email protected]e5dad132009-08-18 00:53:415121 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]8c76ae22010-04-20 22:15:435129 Verify206Response(headers, 40, 49);
[email protected]e5dad132009-08-18 00:53:415130 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]e75e8af2009-11-03 00:04:205136 transaction.request_headers = "Range: bytes = 120-\r\n" EXTRA_HEADER;
[email protected]a5c9d982010-10-12 20:48:025137 transaction.data = "";
[email protected]e5dad132009-08-18 00:53:415138 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
tfarinae04a95b2015-09-18 22:48:125152// 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]2c8528532009-09-09 16:55:225158// Tests that we don't delete a sparse entry when we cancel a request.
tfarinae04a95b2015-09-18 22:48:125159TEST(HttpCache, MAYBE_RangeGET_Cancel) {
[email protected]2c8528532009-09-09 16:55:225160 MockHttpCache cache;
[email protected]2c8528532009-09-09 16:55:225161 AddMockTransaction(&kRangeGET_TransactionOK);
5162
5163 MockHttpRequest request(kRangeGET_TransactionOK);
5164
[email protected]1638d602009-09-24 03:49:175165 Context* c = new Context();
[email protected]027bd85a2013-12-27 22:39:105166 int rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:295167 ASSERT_EQ(OK, rv);
[email protected]2c8528532009-09-09 16:55:225168
ttuttle859dc7a2015-04-23 19:42:295169 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
5170 if (rv == ERR_IO_PENDING)
[email protected]2c8528532009-09-09 16:55:225171 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.
ttuttle859dc7a2015-04-23 19:42:295178 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10));
[email protected]90499482013-06-01 00:39:505179 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
ttuttle859dc7a2015-04-23 19:42:295180 if (rv == ERR_IO_PENDING)
[email protected]2c8528532009-09-09 16:55:225181 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]02e7a012010-05-10 23:06:335189 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
[email protected]2c8528532009-09-09 16:55:225190 entry->Close();
5191 RemoveMockTransaction(&kRangeGET_TransactionOK);
5192}
5193
tfarinae04a95b2015-09-18 22:48:125194// 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]06e62ba2009-10-08 23:07:395200// Tests that we don't delete a sparse entry when we start a new request after
5201// cancelling the previous one.
tfarinae04a95b2015-09-18 22:48:125202TEST(HttpCache, MAYBE_RangeGET_Cancel2) {
[email protected]06e62ba2009-10-08 23:07:395203 MockHttpCache cache;
[email protected]06e62ba2009-10-08 23:07:395204 AddMockTransaction(&kRangeGET_TransactionOK);
5205
5206 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5207 MockHttpRequest request(kRangeGET_TransactionOK);
ttuttle859dc7a2015-04-23 19:42:295208 request.load_flags |= LOAD_VALIDATE_CACHE;
[email protected]06e62ba2009-10-08 23:07:395209
5210 Context* c = new Context();
[email protected]027bd85a2013-12-27 22:39:105211 int rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:295212 ASSERT_EQ(OK, rv);
[email protected]06e62ba2009-10-08 23:07:395213
ttuttle859dc7a2015-04-23 19:42:295214 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
5215 if (rv == ERR_IO_PENDING)
[email protected]06e62ba2009-10-08 23:07:395216 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).
ttuttle859dc7a2015-04-23 19:42:295224 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5));
[email protected]90499482013-06-01 00:39:505225 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
[email protected]21e743202009-12-18 01:31:045226 EXPECT_EQ(5, c->callback.GetResult(rv));
[email protected]90499482013-06-01 00:39:505227 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
ttuttle859dc7a2015-04-23 19:42:295228 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]06e62ba2009-10-08 23:07:395229
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]5beca812010-06-24 17:55:245239 EXPECT_EQ(2, cache.network_layer()->transaction_count());
[email protected]06e62ba2009-10-08 23:07:395240 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]24f46392009-11-19 18:45:235245// 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.
5247TEST(HttpCache, RangeGET_Cancel3) {
5248 MockHttpCache cache;
[email protected]24f46392009-11-19 18:45:235249 AddMockTransaction(&kRangeGET_TransactionOK);
5250
5251 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5252 MockHttpRequest request(kRangeGET_TransactionOK);
ttuttle859dc7a2015-04-23 19:42:295253 request.load_flags |= LOAD_VALIDATE_CACHE;
[email protected]24f46392009-11-19 18:45:235254
5255 Context* c = new Context();
[email protected]027bd85a2013-12-27 22:39:105256 int rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:295257 ASSERT_EQ(OK, rv);
[email protected]24f46392009-11-19 18:45:235258
ttuttle859dc7a2015-04-23 19:42:295259 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
5260 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]24f46392009-11-19 18:45:235261 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).
ttuttle859dc7a2015-04-23 19:42:295269 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5));
[email protected]90499482013-06-01 00:39:505270 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
[email protected]21e743202009-12-18 01:31:045271 EXPECT_EQ(5, c->callback.GetResult(rv));
[email protected]90499482013-06-01 00:39:505272 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
ttuttle859dc7a2015-04-23 19:42:295273 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]24f46392009-11-19 18:45:235274
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]027bd85a2013-12-27 22:39:105283 rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:295284 ASSERT_EQ(OK, rv);
[email protected]24f46392009-11-19 18:45:235285
ttuttle859dc7a2015-04-23 19:42:295286 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
5287 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]24f46392009-11-19 18:45:235288
5289 MockDiskEntry::IgnoreCallbacks(true);
[email protected]2da659e2013-05-23 20:51:345290 base::MessageLoop::current()->RunUntilIdle();
[email protected]24f46392009-11-19 18:45:235291 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]2da659e2013-05-23 20:51:345297 base::MessageLoop::current()->RunUntilIdle();
[email protected]24f46392009-11-19 18:45:235298
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]7eab0d2262009-10-14 22:05:545305// Tests that an invalid range response results in no cached entry.
5306TEST(HttpCache, RangeGET_InvalidResponse1) {
5307 MockHttpCache cache;
[email protected]7eab0d2262009-10-14 22:05:545308 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]7d7ad6e42010-01-14 01:30:535327 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:335328 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
[email protected]7eab0d2262009-10-14 22:05:545329
5330 RemoveMockTransaction(&kRangeGET_TransactionOK);
5331}
5332
5333// Tests that we reject a range that doesn't match the content-length.
5334TEST(HttpCache, RangeGET_InvalidResponse2) {
5335 MockHttpCache cache;
[email protected]7eab0d2262009-10-14 22:05:545336 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]7d7ad6e42010-01-14 01:30:535355 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:335356 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
[email protected]7eab0d2262009-10-14 22:05:545357
5358 RemoveMockTransaction(&kRangeGET_TransactionOK);
5359}
5360
5361// Tests that if a server tells us conflicting information about a resource we
rvargas3b57e37a2015-01-06 00:56:345362// drop the entry.
[email protected]7eab0d2262009-10-14 22:05:545363TEST(HttpCache, RangeGET_InvalidResponse3) {
5364 MockHttpCache cache;
[email protected]7eab0d2262009-10-14 22:05:545365 std::string headers;
5366
5367 MockTransaction transaction(kRangeGET_TransactionOK);
5368 transaction.handler = NULL;
[email protected]e75e8af2009-11-03 00:04:205369 transaction.request_headers = "Range: bytes = 50-59\r\n" EXTRA_HEADER;
[email protected]7eab0d2262009-10-14 22:05:545370 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]8c76ae22010-04-20 22:15:435376 Verify206Response(headers, 50, 59);
[email protected]7eab0d2262009-10-14 22:05:545377 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]8c76ae22010-04-20 22:15:435389 Verify206Response(headers, 40, 49);
[email protected]7eab0d2262009-10-14 22:05:545390 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
rvargas3b57e37a2015-01-06 00:56:345394 // 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]7eab0d2262009-10-14 22:05:545398 RemoveMockTransaction(&kRangeGET_TransactionOK);
5399}
5400
5401// Tests that we handle large range values properly.
5402TEST(HttpCache, RangeGET_LargeValues) {
5403 // We need a real sparse cache for this test.
ttuttle859dc7a2015-04-23 19:42:295404 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(1024 * 1024));
[email protected]7eab0d2262009-10-14 22:05:545405 std::string headers;
5406
5407 MockTransaction transaction(kRangeGET_TransactionOK);
5408 transaction.handler = NULL;
[email protected]e75e8af2009-11-03 00:04:205409 transaction.request_headers = "Range: bytes = 4294967288-4294967297\r\n"
5410 EXTRA_HEADER;
[email protected]7eab0d2262009-10-14 22:05:545411 transaction.response_headers =
[email protected]8a301142011-04-13 18:33:405412 "ETag: \"foo\"\n"
[email protected]7eab0d2262009-10-14 22:05:545413 "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]02e7a012010-05-10 23:06:335427 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &en));
[email protected]7eab0d2262009-10-14 22:05:545428 en->Close();
5429
5430 RemoveMockTransaction(&kRangeGET_TransactionOK);
5431}
5432
[email protected]93e78442009-10-27 04:46:325433// Tests that we don't crash with a range request if the disk cache was not
5434// initialized properly.
5435TEST(HttpCache, RangeGET_NoDiskCache) {
mmenkebc31a2c2015-10-29 13:44:455436 scoped_ptr<MockBlockingBackendFactory> factory(
5437 new MockBlockingBackendFactory());
[email protected]f8702522010-05-12 18:40:105438 factory->set_fail(true);
5439 factory->FinishCreation(); // We'll complete synchronously.
mmenkebc31a2c2015-10-29 13:44:455440 MockHttpCache cache(factory.Pass());
[email protected]f8702522010-05-12 18:40:105441
[email protected]93e78442009-10-27 04:46:325442 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]793618a2009-11-03 23:08:125450// Tests that we handle byte range requests that skip the cache.
5451TEST(HttpCache, RangeHEAD) {
5452 MockHttpCache cache;
[email protected]793618a2009-11-03 23:08:125453 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]8c76ae22010-04-20 22:15:435463 Verify206Response(headers, 70, 79);
[email protected]793618a2009-11-03 23:08:125464 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]fa59e6a2009-12-02 18:07:465471// 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.
5473TEST(HttpCache, RangeGET_FastFlakyServer) {
5474 MockHttpCache cache;
[email protected]fa59e6a2009-12-02 18:07:465475
rvargas80059b32015-01-02 23:39:525476 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
[email protected]fa59e6a2009-12-02 18:07:465477 transaction.request_headers = "Range: bytes = 40-\r\n" EXTRA_HEADER;
5478 transaction.test_mode = TEST_MODE_SYNC_NET_START;
ttuttle859dc7a2015-04-23 19:42:295479 transaction.load_flags |= LOAD_VALIDATE_CACHE;
[email protected]fa59e6a2009-12-02 18:07:465480
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]634739b2011-03-02 18:08:255487 transaction.data = "Not a range";
ttuttle859dc7a2015-04-23 19:42:295488 BoundTestNetLog log;
rvargas80059b32015-01-02 23:39:525489 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound());
[email protected]fa59e6a2009-12-02 18:07:465490
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());
rvargas80059b32015-01-02 23:39:525494 EXPECT_TRUE(LogContainsEventType(
ttuttle859dc7a2015-04-23 19:42:295495 log, NetLog::TYPE_HTTP_CACHE_RE_SEND_PARTIAL_REQUEST));
[email protected]fa59e6a2009-12-02 18:07:465496}
5497
[email protected]c14117b92010-01-21 19:22:575498// Tests that when the server gives us less data than expected, we don't keep
5499// asking for more data.
5500TEST(HttpCache, RangeGET_FastFlakyServer2) {
5501 MockHttpCache cache;
[email protected]c14117b92010-01-21 19:22:575502
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]20960e072011-09-20 20:59:015538#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
[email protected]e5dad132009-08-18 00:53:415539// This test hits a NOTREACHED so it is a release mode only test.
[email protected]21f659d2009-08-24 17:59:315540TEST(HttpCache, RangeGET_OK_LoadOnlyFromCache) {
[email protected]e5dad132009-08-18 00:53:415541 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);
ttuttle859dc7a2015-04-23 19:42:295552 transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
[email protected]e5dad132009-08-18 00:53:415553
5554 MockHttpRequest request(transaction);
ttuttle859dc7a2015-04-23 19:42:295555 TestCompletionCallback callback;
[email protected]e5dad132009-08-18 00:53:415556
ttuttle859dc7a2015-04-23 19:42:295557 scoped_ptr<HttpTransaction> trans;
5558 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
5559 EXPECT_EQ(OK, rv);
[email protected]e5dad132009-08-18 00:53:415560 ASSERT_TRUE(trans.get());
5561
ttuttle859dc7a2015-04-23 19:42:295562 rv = trans->Start(&request, callback.callback(), BoundNetLog());
5563 if (rv == ERR_IO_PENDING)
[email protected]e5dad132009-08-18 00:53:415564 rv = callback.WaitForResult();
ttuttle859dc7a2015-04-23 19:42:295565 ASSERT_EQ(ERR_CACHE_MISS, rv);
[email protected]e5dad132009-08-18 00:53:415566
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]28accfe2009-09-04 23:36:335577// Tests the handling of the "truncation" flag.
5578TEST(HttpCache, WriteResponseInfo_Truncated) {
5579 MockHttpCache cache;
5580 disk_cache::Entry* entry;
[email protected]f6f1bebc2011-01-07 03:04:545581 ASSERT_TRUE(cache.CreateBackendEntry("http://www.google.com", &entry,
5582 NULL));
[email protected]28accfe2009-09-04 23:36:335583
5584 std::string headers("HTTP/1.1 200 OK");
ttuttle859dc7a2015-04-23 19:42:295585 headers = HttpUtil::AssembleRawHeaders(headers.data(), headers.size());
5586 HttpResponseInfo response;
5587 response.headers = new HttpResponseHeaders(headers);
[email protected]28accfe2009-09-04 23:36:335588
5589 // Set the last argument for this to be an incomplete request.
[email protected]02e7a012010-05-10 23:06:335590 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true));
[email protected]28accfe2009-09-04 23:36:335591 bool truncated = false;
[email protected]02e7a012010-05-10 23:06:335592 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
[email protected]28accfe2009-09-04 23:36:335593 EXPECT_TRUE(truncated);
5594
5595 // And now test the opposite case.
[email protected]02e7a012010-05-10 23:06:335596 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
[email protected]28accfe2009-09-04 23:36:335597 truncated = true;
[email protected]02e7a012010-05-10 23:06:335598 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
[email protected]28accfe2009-09-04 23:36:335599 EXPECT_FALSE(truncated);
5600 entry->Close();
5601}
5602
[email protected]6d81b482011-02-22 19:47:195603// Tests basic pickling/unpickling of HttpResponseInfo.
5604TEST(HttpCache, PersistHttpResponseInfo) {
5605 // Set some fields (add more if needed.)
ttuttle859dc7a2015-04-23 19:42:295606 HttpResponseInfo response1;
[email protected]6d81b482011-02-22 19:47:195607 response1.was_cached = false;
ttuttle859dc7a2015-04-23 19:42:295608 response1.socket_address = HostPortPair("1.2.3.4", 80);
5609 response1.headers = new HttpResponseHeaders("HTTP/1.1 200 OK");
[email protected]6d81b482011-02-22 19:47:195610
5611 // Pickle.
brettwbd4d7112015-06-03 04:29:255612 base::Pickle pickle;
[email protected]6d81b482011-02-22 19:47:195613 response1.Persist(&pickle, false, false);
5614
5615 // Unpickle.
ttuttle859dc7a2015-04-23 19:42:295616 HttpResponseInfo response2;
[email protected]6d81b482011-02-22 19:47:195617 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]28accfe2009-09-04 23:36:335628// Tests that we delete an entry when the request is cancelled before starting
5629// to read from the network.
5630TEST(HttpCache, DoomOnDestruction) {
5631 MockHttpCache cache;
[email protected]28accfe2009-09-04 23:36:335632
5633 MockHttpRequest request(kSimpleGET_Transaction);
5634
[email protected]1638d602009-09-24 03:49:175635 Context* c = new Context();
[email protected]027bd85a2013-12-27 22:39:105636 int rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:295637 ASSERT_EQ(OK, rv);
[email protected]28accfe2009-09-04 23:36:335638
ttuttle859dc7a2015-04-23 19:42:295639 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
5640 if (rv == ERR_IO_PENDING)
[email protected]28accfe2009-09-04 23:36:335641 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]dbd39fb2010-01-08 01:13:365658// Tests that we delete an entry when the request is cancelled if the response
5659// does not have content-length and strong validators.
5660TEST(HttpCache, DoomOnDestruction2) {
5661 MockHttpCache cache;
[email protected]dbd39fb2010-01-08 01:13:365662
5663 MockHttpRequest request(kSimpleGET_Transaction);
5664
5665 Context* c = new Context();
[email protected]027bd85a2013-12-27 22:39:105666 int rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:295667 ASSERT_EQ(OK, rv);
[email protected]dbd39fb2010-01-08 01:13:365668
ttuttle859dc7a2015-04-23 19:42:295669 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
5670 if (rv == ERR_IO_PENDING)
[email protected]dbd39fb2010-01-08 01:13:365671 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.
ttuttle859dc7a2015-04-23 19:42:295678 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10));
[email protected]90499482013-06-01 00:39:505679 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
ttuttle859dc7a2015-04-23 19:42:295680 if (rv == ERR_IO_PENDING)
[email protected]dbd39fb2010-01-08 01:13:365681 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.
5696TEST(HttpCache, DoomOnDestruction3) {
5697 MockHttpCache cache;
[email protected]dbd39fb2010-01-08 01:13:365698
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]29cc1ce42012-07-22 18:39:355704 "Etag: \"foopy\"\n";
[email protected]dbd39fb2010-01-08 01:13:365705 AddMockTransaction(&transaction);
5706 MockHttpRequest request(transaction);
5707
5708 Context* c = new Context();
[email protected]027bd85a2013-12-27 22:39:105709 int rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:295710 ASSERT_EQ(OK, rv);
[email protected]dbd39fb2010-01-08 01:13:365711
ttuttle859dc7a2015-04-23 19:42:295712 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
5713 if (rv == ERR_IO_PENDING)
[email protected]dbd39fb2010-01-08 01:13:365714 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.
ttuttle859dc7a2015-04-23 19:42:295721 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10));
[email protected]90499482013-06-01 00:39:505722 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
ttuttle859dc7a2015-04-23 19:42:295723 if (rv == ERR_IO_PENDING)
[email protected]dbd39fb2010-01-08 01:13:365724 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]28accfe2009-09-04 23:36:335739// Tests that we mark an entry as incomplete when the request is cancelled.
[email protected]2f9be752011-05-05 21:16:505740TEST(HttpCache, SetTruncatedFlag) {
[email protected]28accfe2009-09-04 23:36:335741 MockHttpCache cache;
[email protected]28accfe2009-09-04 23:36:335742
rvargas1c7570e2015-09-17 23:05:455743 ScopedMockTransaction transaction(kSimpleGET_Transaction);
[email protected]dbd39fb2010-01-08 01:13:365744 transaction.response_headers =
5745 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
5746 "Content-Length: 22\n"
[email protected]29cc1ce42012-07-22 18:39:355747 "Etag: \"foopy\"\n";
[email protected]dbd39fb2010-01-08 01:13:365748 MockHttpRequest request(transaction);
[email protected]28accfe2009-09-04 23:36:335749
[email protected]6df35cc2010-02-10 00:53:065750 scoped_ptr<Context> c(new Context());
[email protected]027bd85a2013-12-27 22:39:105751
5752 int rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:295753 ASSERT_EQ(OK, rv);
[email protected]28accfe2009-09-04 23:36:335754
ttuttle859dc7a2015-04-23 19:42:295755 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
5756 if (rv == ERR_IO_PENDING)
[email protected]28accfe2009-09-04 23:36:335757 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.
ttuttle859dc7a2015-04-23 19:42:295764 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10));
[email protected]90499482013-06-01 00:39:505765 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
ttuttle859dc7a2015-04-23 19:42:295766 if (rv == ERR_IO_PENDING)
[email protected]28accfe2009-09-04 23:36:335767 rv = c->callback.WaitForResult();
5768 EXPECT_EQ(buf->size(), rv);
5769
[email protected]6df35cc2010-02-10 00:53:065770 // We want to cancel the request when the transaction is busy.
[email protected]90499482013-06-01 00:39:505771 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
ttuttle859dc7a2015-04-23 19:42:295772 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]6df35cc2010-02-10 00:53:065773 EXPECT_FALSE(c->callback.have_result());
5774
[email protected]f40156002011-11-22 21:19:085775 MockHttpCache::SetTestMode(TEST_MODE_SYNC_ALL);
[email protected]6df35cc2010-02-10 00:53:065776
[email protected]28accfe2009-09-04 23:36:335777 // Destroy the transaction.
[email protected]6df35cc2010-02-10 00:53:065778 c->trans.reset();
[email protected]f40156002011-11-22 21:19:085779 MockHttpCache::SetTestMode(0);
[email protected]6df35cc2010-02-10 00:53:065780
[email protected]a9e0d1412012-08-20 22:13:015781
[email protected]6df35cc2010-02-10 00:53:065782 // 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]28accfe2009-09-04 23:36:335787
5788 // Verify that the entry is marked as incomplete.
rvargas1c7570e2015-09-17 23:05:455789 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0);
[email protected]28accfe2009-09-04 23:36:335790}
5791
[email protected]2f9be752011-05-05 21:16:505792// Tests that we don't mark an entry as truncated when we read everything.
5793TEST(HttpCache, DontSetTruncatedFlag) {
5794 MockHttpCache cache;
5795
rvargas1c7570e2015-09-17 23:05:455796 ScopedMockTransaction transaction(kSimpleGET_Transaction);
[email protected]2f9be752011-05-05 21:16:505797 transaction.response_headers =
5798 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
5799 "Content-Length: 22\n"
[email protected]29cc1ce42012-07-22 18:39:355800 "Etag: \"foopy\"\n";
[email protected]2f9be752011-05-05 21:16:505801 MockHttpRequest request(transaction);
5802
5803 scoped_ptr<Context> c(new Context());
[email protected]027bd85a2013-12-27 22:39:105804 int rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:295805 ASSERT_EQ(OK, rv);
[email protected]2f9be752011-05-05 21:16:505806
ttuttle859dc7a2015-04-23 19:42:295807 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
5808 EXPECT_EQ(OK, c->callback.GetResult(rv));
[email protected]2f9be752011-05-05 21:16:505809
5810 // Read everything.
ttuttle859dc7a2015-04-23 19:42:295811 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(22));
[email protected]90499482013-06-01 00:39:505812 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
[email protected]2f9be752011-05-05 21:16:505813 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.
rvargas1c7570e2015-09-17 23:05:455819 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, false, 0);
5820}
[email protected]2f9be752011-05-05 21:16:505821
rvargas1c7570e2015-09-17 23:05:455822// Tests that sparse entries don't set the truncate flag.
5823TEST(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).
5850TEST(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]2f9be752011-05-05 21:16:505873}
5874
[email protected]28accfe2009-09-04 23:36:335875// Tests that we can continue with a request that was interrupted.
5876TEST(HttpCache, GET_IncompleteResource) {
5877 MockHttpCache cache;
rvargas1c7570e2015-09-17 23:05:455878 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
[email protected]28accfe2009-09-04 23:36:335879
[email protected]28accfe2009-09-04 23:36:335880 std::string raw_headers("HTTP/1.1 200 OK\n"
[email protected]bd069d72011-05-19 01:11:115881 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
[email protected]28accfe2009-09-04 23:36:335882 "ETag: \"foo\"\n"
5883 "Accept-Ranges: bytes\n"
[email protected]dbd39fb2010-01-08 01:13:365884 "Content-Length: 80\n");
[email protected]634739b2011-03-02 18:08:255885 CreateTruncatedEntry(raw_headers, &cache);
[email protected]28accfe2009-09-04 23:36:335886
5887 // Now make a regular request.
5888 std::string headers;
[email protected]e75e8af2009-11-03 00:04:205889 transaction.request_headers = EXTRA_HEADER;
rvargas3b57e37a2015-01-06 00:56:345890 transaction.data = kFullRangeData;
[email protected]28accfe2009-09-04 23:36:335891 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]bd069d72011-05-19 01:11:115896 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
[email protected]28accfe2009-09-04 23:36:335897 "Accept-Ranges: bytes\n"
5898 "ETag: \"foo\"\n"
[email protected]dbd39fb2010-01-08 01:13:365899 "Content-Length: 80\n");
[email protected]28accfe2009-09-04 23:36:335900
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]28accfe2009-09-04 23:36:335906 // Verify that the disk entry was updated.
rvargas1c7570e2015-09-17 23:05:455907 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80);
[email protected]28accfe2009-09-04 23:36:335908}
5909
[email protected]767c2c9d2012-05-09 23:44:275910// Tests the handling of no-store when revalidating a truncated entry.
5911TEST(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();
rvargas3b57e37a2015-01-06 00:56:345929 transaction.data = kFullRangeData;
[email protected]767c2c9d2012-05-09 23:44:275930 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.
5956TEST(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();
rvargas3b57e37a2015-01-06 00:56:345974 transaction.data = kFullRangeData;
[email protected]767c2c9d2012-05-09 23:44:275975 AddMockTransaction(&transaction);
5976
5977 MockHttpRequest request(transaction);
5978 Context* c = new Context();
5979
[email protected]027bd85a2013-12-27 22:39:105980 int rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:295981 ASSERT_EQ(OK, rv);
[email protected]767c2c9d2012-05-09 23:44:275982
[email protected]9f4633c62012-05-29 18:38:575983 // 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();
ttuttle859dc7a2015-04-23 19:42:295988 ASSERT_EQ(OK, cache.CreateTransaction(&pending->trans));
[email protected]9f4633c62012-05-29 18:38:575989
ttuttle859dc7a2015-04-23 19:42:295990 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
5991 EXPECT_EQ(ERR_IO_PENDING,
[email protected]9f4633c62012-05-29 18:38:575992 pending->trans->Start(&request, pending->callback.callback(),
ttuttle859dc7a2015-04-23 19:42:295993 BoundNetLog()));
5994 EXPECT_EQ(OK, c->callback.GetResult(rv));
[email protected]767c2c9d2012-05-09 23:44:275995
5996 // Make sure that the entry has some data stored.
ttuttle859dc7a2015-04-23 19:42:295997 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5));
[email protected]90499482013-06-01 00:39:505998 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
[email protected]767c2c9d2012-05-09 23:44:275999 EXPECT_EQ(5, c->callback.GetResult(rv));
6000
[email protected]9f4633c62012-05-29 18:38:576001 // Cancel the requests.
[email protected]767c2c9d2012-05-09 23:44:276002 delete c;
[email protected]9f4633c62012-05-29 18:38:576003 delete pending;
[email protected]767c2c9d2012-05-09 23:44:276004
6005 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6006 EXPECT_EQ(1, cache.disk_cache()->open_count());
[email protected]9f4633c62012-05-29 18:38:576007 EXPECT_EQ(2, cache.disk_cache()->create_count());
[email protected]767c2c9d2012-05-09 23:44:276008
[email protected]2da659e2013-05-23 20:51:346009 base::MessageLoop::current()->RunUntilIdle();
[email protected]767c2c9d2012-05-09 23:44:276010 RemoveMockTransaction(&transaction);
6011}
6012
[email protected]dbd39fb2010-01-08 01:13:366013// Tests that we delete truncated entries if the server changes its mind midway.
6014TEST(HttpCache, GET_IncompleteResource2) {
6015 MockHttpCache cache;
[email protected]dbd39fb2010-01-08 01:13:366016 AddMockTransaction(&kRangeGET_TransactionOK);
6017
[email protected]dbd39fb2010-01-08 01:13:366018 // Content-length will be intentionally bad.
6019 std::string raw_headers("HTTP/1.1 200 OK\n"
[email protected]bd069d72011-05-19 01:11:116020 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
[email protected]dbd39fb2010-01-08 01:13:366021 "ETag: \"foo\"\n"
6022 "Accept-Ranges: bytes\n"
6023 "Content-Length: 50\n");
[email protected]634739b2011-03-02 18:08:256024 CreateTruncatedEntry(raw_headers, &cache);
[email protected]dbd39fb2010-01-08 01:13:366025
[email protected]634739b2011-03-02 18:08:256026 // Now make a regular request. We expect the code to fail the validation and
6027 // retry the request without using byte ranges.
[email protected]dbd39fb2010-01-08 01:13:366028 std::string headers;
6029 MockTransaction transaction(kRangeGET_TransactionOK);
6030 transaction.request_headers = EXTRA_HEADER;
[email protected]634739b2011-03-02 18:08:256031 transaction.data = "Not a range";
[email protected]dbd39fb2010-01-08 01:13:366032 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
6033
[email protected]634739b2011-03-02 18:08:256034 // The server will return 200 instead of a byte range.
[email protected]dbd39fb2010-01-08 01:13:366035 std::string expected_headers(
6036 "HTTP/1.1 200 OK\n"
[email protected]634739b2011-03-02 18:08:256037 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n");
[email protected]dbd39fb2010-01-08 01:13:366038
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]dbd39fb2010-01-08 01:13:366044 // Verify that the disk entry was deleted.
[email protected]634739b2011-03-02 18:08:256045 disk_cache::Entry* entry;
[email protected]02e7a012010-05-10 23:06:336046 ASSERT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
[email protected]634739b2011-03-02 18:08:256047 RemoveMockTransaction(&kRangeGET_TransactionOK);
6048}
6049
6050// Tests that we always validate a truncated request.
6051TEST(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;
rvargas3b57e37a2015-01-06 00:56:346068 transaction.data = kFullRangeData;
[email protected]634739b2011-03-02 18:08:256069
6070 scoped_ptr<Context> c(new Context);
[email protected]027bd85a2013-12-27 22:39:106071 int rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:296072 ASSERT_EQ(OK, rv);
[email protected]634739b2011-03-02 18:08:256073
6074 MockHttpRequest request(transaction);
ttuttle859dc7a2015-04-23 19:42:296075 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
6076 EXPECT_EQ(OK, c->callback.GetResult(rv));
[email protected]634739b2011-03-02 18:08:256077
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]d7358ba2014-01-04 01:39:496086// Tests that we handle 401s for truncated resources.
6087TEST(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;
rvargas3b57e37a2015-01-06 00:56:346102 transaction.data = kFullRangeData;
[email protected]d7358ba2014-01-04 01:39:496103 RangeTransactionServer handler;
6104
6105 scoped_ptr<Context> c(new Context);
6106 int rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:296107 ASSERT_EQ(OK, rv);
[email protected]d7358ba2014-01-04 01:39:496108
6109 MockHttpRequest request(transaction);
ttuttle859dc7a2015-04-23 19:42:296110 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
6111 EXPECT_EQ(OK, c->callback.GetResult(rv));
[email protected]d7358ba2014-01-04 01:39:496112
ttuttle859dc7a2015-04-23 19:42:296113 const HttpResponseInfo* response = c->trans->GetResponseInfo();
[email protected]d7358ba2014-01-04 01:39:496114 ASSERT_TRUE(response);
6115 ASSERT_EQ(401, response->headers->response_code());
ttuttle859dc7a2015-04-23 19:42:296116 rv = c->trans->RestartWithAuth(AuthCredentials(), c->callback.callback());
6117 EXPECT_EQ(OK, c->callback.GetResult(rv));
[email protected]d7358ba2014-01-04 01:39:496118 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
liberato1f59bb3f2015-05-29 14:19:106134// Test that the transaction won't retry failed partial requests
6135// after it starts reading data. http://crbug.com/474835
6136TEST(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]634739b2011-03-02 18:08:256170// Tests that we cache a 200 response to the validation request.
6171TEST(HttpCache, GET_IncompleteResource4) {
6172 MockHttpCache cache;
rvargas1c7570e2015-09-17 23:05:456173 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
[email protected]634739b2011-03-02 18:08:256174
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]634739b2011-03-02 18:08:256184 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.
rvargas1c7570e2015-09-17 23:05:456195 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 11);
[email protected]dbd39fb2010-01-08 01:13:366196}
6197
[email protected]8a925552009-11-20 23:16:006198// Tests that when we cancel a request that was interrupted, we mark it again
6199// as truncated.
6200TEST(HttpCache, GET_CancelIncompleteResource) {
6201 MockHttpCache cache;
rvargas1c7570e2015-09-17 23:05:456202 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
[email protected]8a925552009-11-20 23:16:006203
[email protected]8a925552009-11-20 23:16:006204 std::string raw_headers("HTTP/1.1 200 OK\n"
[email protected]dbd39fb2010-01-08 01:13:366205 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
[email protected]8a925552009-11-20 23:16:006206 "ETag: \"foo\"\n"
6207 "Accept-Ranges: bytes\n"
[email protected]dbd39fb2010-01-08 01:13:366208 "Content-Length: 80\n");
[email protected]634739b2011-03-02 18:08:256209 CreateTruncatedEntry(raw_headers, &cache);
[email protected]8a925552009-11-20 23:16:006210
6211 // Now make a regular request.
[email protected]8a925552009-11-20 23:16:006212 transaction.request_headers = EXTRA_HEADER;
6213
6214 MockHttpRequest request(transaction);
6215 Context* c = new Context();
[email protected]027bd85a2013-12-27 22:39:106216 int rv = cache.CreateTransaction(&c->trans);
ttuttle859dc7a2015-04-23 19:42:296217 ASSERT_EQ(OK, rv);
[email protected]8a925552009-11-20 23:16:006218
ttuttle859dc7a2015-04-23 19:42:296219 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog());
6220 EXPECT_EQ(OK, c->callback.GetResult(rv));
[email protected]8a925552009-11-20 23:16:006221
6222 // Read 20 bytes from the cache, and 10 from the net.
ttuttle859dc7a2015-04-23 19:42:296223 scoped_refptr<IOBuffer> buf(new IOBuffer(100));
[email protected]90499482013-06-01 00:39:506224 rv = c->trans->Read(buf.get(), 20, c->callback.callback());
[email protected]634739b2011-03-02 18:08:256225 EXPECT_EQ(20, c->callback.GetResult(rv));
[email protected]90499482013-06-01 00:39:506226 rv = c->trans->Read(buf.get(), 10, c->callback.callback());
[email protected]21e743202009-12-18 01:31:046227 EXPECT_EQ(10, c->callback.GetResult(rv));
[email protected]8a925552009-11-20 23:16:006228
6229 // At this point, we are already reading so canceling the request should leave
6230 // a truncated one.
6231 delete c;
6232
[email protected]8a925552009-11-20 23:16:006233 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.
rvargas1c7570e2015-09-17 23:05:456238 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, true, 30);
[email protected]8a925552009-11-20 23:16:006239}
6240
[email protected]ecd8becb2009-10-02 17:57:456241// Tests that we can handle range requests when we have a truncated entry.
6242TEST(HttpCache, RangeGET_IncompleteResource) {
6243 MockHttpCache cache;
[email protected]ecd8becb2009-10-02 17:57:456244 AddMockTransaction(&kRangeGET_TransactionOK);
6245
[email protected]ecd8becb2009-10-02 17:57:456246 // 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]634739b2011-03-02 18:08:256252 CreateTruncatedEntry(raw_headers, &cache);
[email protected]ecd8becb2009-10-02 17:57:456253
6254 // Now make a range request.
6255 std::string headers;
6256 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
6257 &headers);
6258
[email protected]8c76ae22010-04-20 22:15:436259 Verify206Response(headers, 40, 49);
[email protected]ecd8becb2009-10-02 17:57:456260 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.commit586acc5fe2008-07-26 22:42:526267TEST(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]73cae572009-10-22 18:36:196275 TEST_MODE_SYNC_CACHE_READ |
6276 TEST_MODE_SYNC_CACHE_WRITE);
initial.commit586acc5fe2008-07-26 22:42:526277
6278 MockHttpRequest r1(transaction),
[email protected]46773162010-05-07 22:31:206279 r2(transaction),
6280 r3(transaction);
initial.commit586acc5fe2008-07-26 22:42:526281
ttuttle859dc7a2015-04-23 19:42:296282 TestTransactionConsumer c1(DEFAULT_PRIORITY, cache.http_cache()),
6283 c2(DEFAULT_PRIORITY, cache.http_cache()),
6284 c3(DEFAULT_PRIORITY, cache.http_cache());
initial.commit586acc5fe2008-07-26 22:42:526285
ttuttle859dc7a2015-04-23 19:42:296286 c1.Start(&r1, BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:526287
ttuttle859dc7a2015-04-23 19:42:296288 r2.load_flags |= LOAD_ONLY_FROM_CACHE;
6289 c2.Start(&r2, BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:526290
ttuttle859dc7a2015-04-23 19:42:296291 r3.load_flags |= LOAD_ONLY_FROM_CACHE;
6292 c3.Start(&r3, BoundNetLog());
initial.commit586acc5fe2008-07-26 22:42:526293
[email protected]2da659e2013-05-23 20:51:346294 base::MessageLoop::current()->Run();
initial.commit586acc5fe2008-07-26 22:42:526295
6296 EXPECT_TRUE(c1.is_done());
6297 EXPECT_TRUE(c2.is_done());
6298 EXPECT_TRUE(c3.is_done());
6299
ttuttle859dc7a2015-04-23 19:42:296300 EXPECT_EQ(OK, c1.error());
6301 EXPECT_EQ(OK, c2.error());
6302 EXPECT_EQ(OK, c3.error());
initial.commit586acc5fe2008-07-26 22:42:526303}
6304
6305TEST(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);
ttuttle859dc7a2015-04-23 19:42:296316 transaction.load_flags |= LOAD_VALIDATE_CACHE;
initial.commit586acc5fe2008-07-26 22:42:526317 RunTransactionTest(cache.http_cache(), transaction);
6318
6319 // read from the cache
6320 RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
6321}
6322
6323TEST(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);
ttuttle859dc7a2015-04-23 19:42:296331 TestCompletionCallback callback;
initial.commit586acc5fe2008-07-26 22:42:526332
[email protected]5b2bacc22013-09-19 17:58:396333 // Write to the cache.
initial.commit586acc5fe2008-07-26 22:42:526334 {
ttuttle859dc7a2015-04-23 19:42:296335 scoped_ptr<HttpTransaction> trans;
6336 ASSERT_EQ(OK, cache.CreateTransaction(&trans));
initial.commit586acc5fe2008-07-26 22:42:526337
ttuttle859dc7a2015-04-23 19:42:296338 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
6339 if (rv == ERR_IO_PENDING)
initial.commit586acc5fe2008-07-26 22:42:526340 rv = callback.WaitForResult();
ttuttle859dc7a2015-04-23 19:42:296341 ASSERT_EQ(OK, rv);
initial.commit586acc5fe2008-07-26 22:42:526342
ttuttle859dc7a2015-04-23 19:42:296343 const HttpResponseInfo* info = trans->GetResponseInfo();
initial.commit586acc5fe2008-07-26 22:42:526344 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]5b2bacc22013-09-19 17:58:396352 // Mark the transaction as completed so it is cached.
6353 trans->DoneReading();
6354
[email protected]af4876d2008-10-21 23:10:576355 // 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.commit586acc5fe2008-07-26 22:42:526357 }
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]5b2bacc22013-09-19 17:58:396362 // 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.commit586acc5fe2008-07-26 22:42:526368 {
ttuttle859dc7a2015-04-23 19:42:296369 scoped_ptr<HttpTransaction> trans;
6370 ASSERT_EQ(OK, cache.CreateTransaction(&trans));
initial.commit586acc5fe2008-07-26 22:42:526371
ttuttle859dc7a2015-04-23 19:42:296372 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
6373 if (rv == ERR_IO_PENDING)
initial.commit586acc5fe2008-07-26 22:42:526374 rv = callback.WaitForResult();
ttuttle859dc7a2015-04-23 19:42:296375 ASSERT_EQ(OK, rv);
initial.commit586acc5fe2008-07-26 22:42:526376
ttuttle859dc7a2015-04-23 19:42:296377 const HttpResponseInfo* info = trans->GetResponseInfo();
initial.commit586acc5fe2008-07-26 22:42:526378 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]5b2bacc22013-09-19 17:58:396386 // Mark the transaction as completed so it is cached.
6387 trans->DoneReading();
6388
[email protected]af4876d2008-10-21 23:10:576389 // 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.commit586acc5fe2008-07-26 22:42:526391 }
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]2b337ef2013-08-23 20:07:056397// Verify that no-cache resources are stored in cache, but are not fetched from
6398// cache during normal loads.
6399TEST(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.
6426TEST(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.
ttuttle859dc7a2015-04-23 19:42:296440 transaction.load_flags = LOAD_PREFERRING_CACHE;
[email protected]2b337ef2013-08-23 20:07:056441 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.commit586acc5fe2008-07-26 22:42:526452TEST(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]02e7a012010-05-10 23:06:336473 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry));
initial.commit586acc5fe2008-07-26 22:42:526474}
6475
6476TEST(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
ttuttle859dc7a2015-04-23 19:42:296492 transaction.load_flags = LOAD_VALIDATE_CACHE;
initial.commit586acc5fe2008-07-26 22:42:526493 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]02e7a012010-05-10 23:06:336501 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry));
initial.commit586acc5fe2008-07-26 22:42:526502}
6503
6504TEST(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
ttuttle859dc7a2015-04-23 19:42:296520 transaction.load_flags = LOAD_VALIDATE_CACHE;
initial.commit586acc5fe2008-07-26 22:42:526521 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]02e7a012010-05-10 23:06:336530 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry));
initial.commit586acc5fe2008-07-26 22:42:526531}
6532
6533// Ensure that we don't cache requests served over bad HTTPS.
6534TEST(HttpCache, SimpleGET_SSLError) {
6535 MockHttpCache cache;
6536
6537 MockTransaction transaction = kSimpleGET_Transaction;
ttuttle859dc7a2015-04-23 19:42:296538 transaction.cert_status = CERT_STATUS_REVOKED;
initial.commit586acc5fe2008-07-26 22:42:526539 ScopedMockTransaction scoped_transaction(transaction);
6540
6541 // write to the cache
6542 RunTransactionTest(cache.http_cache(), transaction);
6543
6544 // Test that it was not cached.
ttuttle859dc7a2015-04-23 19:42:296545 transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
initial.commit586acc5fe2008-07-26 22:42:526546
6547 MockHttpRequest request(transaction);
ttuttle859dc7a2015-04-23 19:42:296548 TestCompletionCallback callback;
initial.commit586acc5fe2008-07-26 22:42:526549
ttuttle859dc7a2015-04-23 19:42:296550 scoped_ptr<HttpTransaction> trans;
6551 ASSERT_EQ(OK, cache.CreateTransaction(&trans));
initial.commit586acc5fe2008-07-26 22:42:526552
ttuttle859dc7a2015-04-23 19:42:296553 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
6554 if (rv == ERR_IO_PENDING)
initial.commit586acc5fe2008-07-26 22:42:526555 rv = callback.WaitForResult();
ttuttle859dc7a2015-04-23 19:42:296556 ASSERT_EQ(ERR_CACHE_MISS, rv);
initial.commit586acc5fe2008-07-26 22:42:526557}
[email protected]3e2d38d2009-02-14 02:01:186558
6559// Ensure that we don't crash by if left-behind transactions.
6560TEST(HttpCache, OutlivedTransactions) {
6561 MockHttpCache* cache = new MockHttpCache;
6562
ttuttle859dc7a2015-04-23 19:42:296563 scoped_ptr<HttpTransaction> trans;
6564 EXPECT_EQ(OK, cache->CreateTransaction(&trans));
[email protected]1638d602009-09-24 03:49:176565
[email protected]b367d9a52009-02-27 01:02:516566 delete cache;
[email protected]1638d602009-09-24 03:49:176567 trans.reset();
[email protected]3e2d38d2009-02-14 02:01:186568}
[email protected]981797002009-06-05 07:14:156569
6570// Test that the disabled mode works.
6571TEST(HttpCache, CacheDisabledMode) {
6572 MockHttpCache cache;
6573
6574 // write to the cache
6575 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
6576
6577 // go into disabled mode
ttuttle859dc7a2015-04-23 19:42:296578 cache.http_cache()->set_mode(HttpCache::DISABLE);
[email protected]981797002009-06-05 07:14:156579
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]207d58c72009-09-04 18:59:296589
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]ca2f19e2009-09-04 22:53:166592// HttpResponseHeaders::request_time and HttpResponseHeaders::response_time
6593// fields also gets updated.
[email protected]207d58c72009-09-04 18:59:296594// http://crbug.com/20594.
[email protected]ca2f19e2009-09-04 22:53:166595TEST(HttpCache, UpdatesRequestResponseTimeOn304) {
[email protected]207d58c72009-09-04 18:59:296596 MockHttpCache cache;
6597
thestig9d3bb0c2015-01-24 00:49:516598 const char kUrl[] = "http://foobar";
6599 const char kData[] = "body";
[email protected]207d58c72009-09-04 18:59:296600
[email protected]4822ae02012-09-11 17:37:596601 MockTransaction mock_network_response = { 0 };
[email protected]207d58c72009-09-04 18:59:296602 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]4822ae02012-09-11 17:37:596608 MockTransaction request = { 0 };
[email protected]207d58c72009-09-04 18:59:296609 request.url = kUrl;
6610 request.method = "GET";
[email protected]1dce442e2013-04-23 03:06:296611 request.request_headers = "\r\n";
[email protected]207d58c72009-09-04 18:59:296612 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
ttuttle859dc7a2015-04-23 19:42:296628 request.load_flags = LOAD_VALIDATE_CACHE;
[email protected]207d58c72009-09-04 18:59:296629
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]ca2f19e2009-09-04 22:53:166638 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]207d58c72009-09-04 18:59:296643
ttuttle859dc7a2015-04-23 19:42:296644 HttpResponseInfo response;
[email protected]207d58c72009-09-04 18:59:296645 RunTransactionTestWithResponseInfo(cache.http_cache(), request, &response);
6646
[email protected]ca2f19e2009-09-04 22:53:166647 // 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]207d58c72009-09-04 18:59:296651 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]47b95052010-03-02 19:10:076663
6664// Tests that we can write metadata to an entry.
6665TEST(HttpCache, WriteMetadata_OK) {
6666 MockHttpCache cache;
6667
6668 // Write to the cache
ttuttle859dc7a2015-04-23 19:42:296669 HttpResponseInfo response;
[email protected]47b95052010-03-02 19:10:076670 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
6671 &response);
6672 EXPECT_TRUE(response.metadata.get() == NULL);
6673
6674 // Trivial call.
ttuttle859dc7a2015-04-23 19:42:296675 cache.http_cache()->WriteMetadata(GURL("foo"), DEFAULT_PRIORITY, Time::Now(),
6676 NULL, 0);
[email protected]47b95052010-03-02 19:10:076677
6678 // Write meta data to the same entry.
ttuttle859dc7a2015-04-23 19:42:296679 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50));
[email protected]47b95052010-03-02 19:10:076680 memset(buf->data(), 0, buf->size());
6681 base::strlcpy(buf->data(), "Hi there", buf->size());
ttuttle859dc7a2015-04-23 19:42:296682 cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url),
6683 DEFAULT_PRIORITY, response.response_time,
6684 buf.get(), buf->size());
[email protected]47b95052010-03-02 19:10:076685
6686 // Release the buffer before the operation takes place.
6687 buf = NULL;
6688
6689 // Makes sure we finish pending operations.
[email protected]2da659e2013-05-23 20:51:346690 base::MessageLoop::current()->RunUntilIdle();
[email protected]47b95052010-03-02 19:10:076691
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.
6704TEST(HttpCache, WriteMetadata_Fail) {
6705 MockHttpCache cache;
6706
6707 // Write to the cache
ttuttle859dc7a2015-04-23 19:42:296708 HttpResponseInfo response;
[email protected]47b95052010-03-02 19:10:076709 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.
ttuttle859dc7a2015-04-23 19:42:296714 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50));
[email protected]47b95052010-03-02 19:10:076715 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);
yangguo7fbf4c12015-02-20 22:16:576719 cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url),
ttuttle859dc7a2015-04-23 19:42:296720 DEFAULT_PRIORITY, expected_time, buf.get(),
6721 buf->size());
[email protected]47b95052010-03-02 19:10:076722
6723 // Makes sure we finish pending operations.
[email protected]2da659e2013-05-23 20:51:346724 base::MessageLoop::current()->RunUntilIdle();
[email protected]47b95052010-03-02 19:10:076725
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.
6737TEST(HttpCache, ReadMetadata) {
6738 MockHttpCache cache;
6739
6740 // Write to the cache
ttuttle859dc7a2015-04-23 19:42:296741 HttpResponseInfo response;
[email protected]47b95052010-03-02 19:10:076742 RunTransactionTestWithResponseInfo(cache.http_cache(),
6743 kTypicalGET_Transaction, &response);
6744 EXPECT_TRUE(response.metadata.get() == NULL);
6745
6746 // Write meta data to the same entry.
ttuttle859dc7a2015-04-23 19:42:296747 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50));
[email protected]47b95052010-03-02 19:10:076748 memset(buf->data(), 0, buf->size());
6749 base::strlcpy(buf->data(), "Hi there", buf->size());
ttuttle859dc7a2015-04-23 19:42:296750 cache.http_cache()->WriteMetadata(GURL(kTypicalGET_Transaction.url),
6751 DEFAULT_PRIORITY, response.response_time,
6752 buf.get(), buf->size());
[email protected]47b95052010-03-02 19:10:076753
6754 // Makes sure we finish pending operations.
[email protected]2da659e2013-05-23 20:51:346755 base::MessageLoop::current()->RunUntilIdle();
[email protected]47b95052010-03-02 19:10:076756
6757 // Start with a READ mode transaction.
6758 MockTransaction trans1(kTypicalGET_Transaction);
ttuttle859dc7a2015-04-23 19:42:296759 trans1.load_flags = LOAD_ONLY_FROM_CACHE;
[email protected]47b95052010-03-02 19:10:076760
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]2da659e2013-05-23 20:51:346769 base::MessageLoop::current()->RunUntilIdle();
[email protected]47b95052010-03-02 19:10:076770
6771 // Now make sure that the entry is re-validated with the server.
ttuttle859dc7a2015-04-23 19:42:296772 trans1.load_flags = LOAD_VALIDATE_CACHE;
[email protected]47b95052010-03-02 19:10:076773 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]2da659e2013-05-23 20:51:346783 base::MessageLoop::current()->RunUntilIdle();
[email protected]47b95052010-03-02 19:10:076784 RemoveMockTransaction(&trans1);
6785
6786 // Now return 200 when validating the entry so the metadata will be lost.
6787 MockTransaction trans2(kTypicalGET_Transaction);
ttuttle859dc7a2015-04-23 19:42:296788 trans2.load_flags = LOAD_VALIDATE_CACHE;
[email protected]47b95052010-03-02 19:10:076789 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]5c04f722011-08-12 17:52:476796
6797// Tests that we don't mark entries as truncated when a filter detects the end
6798// of the stream.
6799TEST(HttpCache, FilterCompletion) {
6800 MockHttpCache cache;
ttuttle859dc7a2015-04-23 19:42:296801 TestCompletionCallback callback;
[email protected]5c04f722011-08-12 17:52:476802
6803 {
ttuttle859dc7a2015-04-23 19:42:296804 scoped_ptr<HttpTransaction> trans;
6805 ASSERT_EQ(OK, cache.CreateTransaction(&trans));
[email protected]5c04f722011-08-12 17:52:476806
6807 MockHttpRequest request(kSimpleGET_Transaction);
ttuttle859dc7a2015-04-23 19:42:296808 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
6809 EXPECT_EQ(OK, callback.GetResult(rv));
[email protected]5c04f722011-08-12 17:52:476810
ttuttle859dc7a2015-04-23 19:42:296811 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
[email protected]90499482013-06-01 00:39:506812 rv = trans->Read(buf.get(), 256, callback.callback());
[email protected]5c04f722011-08-12 17:52:476813 EXPECT_GT(callback.GetResult(rv), 0);
6814
6815 // Now make sure that the entry is preserved.
6816 trans->DoneReading();
6817 }
6818
[email protected]c85316f2011-08-18 23:27:366819 // Make sure that the ActiveEntry is gone.
[email protected]2da659e2013-05-23 20:51:346820 base::MessageLoop::current()->RunUntilIdle();
[email protected]5c04f722011-08-12 17:52:476821
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]c85316f2011-08-18 23:27:366829
[email protected]5b2bacc22013-09-19 17:58:396830// 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.
6833TEST(HttpCache, DoneReading) {
6834 MockHttpCache cache;
ttuttle859dc7a2015-04-23 19:42:296835 TestCompletionCallback callback;
[email protected]5b2bacc22013-09-19 17:58:396836
6837 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6838 transaction.data = "";
6839
ttuttle859dc7a2015-04-23 19:42:296840 scoped_ptr<HttpTransaction> trans;
6841 ASSERT_EQ(OK, cache.CreateTransaction(&trans));
[email protected]5b2bacc22013-09-19 17:58:396842
6843 MockHttpRequest request(transaction);
ttuttle859dc7a2015-04-23 19:42:296844 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
6845 EXPECT_EQ(OK, callback.GetResult(rv));
[email protected]5b2bacc22013-09-19 17:58:396846
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]255c0442013-09-10 23:59:046861// Tests that we stop caching when told.
[email protected]60d94d72011-11-18 19:54:576862TEST(HttpCache, StopCachingDeletesEntry) {
6863 MockHttpCache cache;
ttuttle859dc7a2015-04-23 19:42:296864 TestCompletionCallback callback;
[email protected]60d94d72011-11-18 19:54:576865 MockHttpRequest request(kSimpleGET_Transaction);
6866
6867 {
ttuttle859dc7a2015-04-23 19:42:296868 scoped_ptr<HttpTransaction> trans;
6869 ASSERT_EQ(OK, cache.CreateTransaction(&trans));
[email protected]60d94d72011-11-18 19:54:576870
ttuttle859dc7a2015-04-23 19:42:296871 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
6872 EXPECT_EQ(OK, callback.GetResult(rv));
[email protected]60d94d72011-11-18 19:54:576873
ttuttle859dc7a2015-04-23 19:42:296874 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
[email protected]90499482013-06-01 00:39:506875 rv = trans->Read(buf.get(), 10, callback.callback());
[email protected]921ce412013-09-20 23:03:486876 EXPECT_EQ(10, callback.GetResult(rv));
[email protected]60d94d72011-11-18 19:54:576877
6878 trans->StopCaching();
6879
6880 // We should be able to keep reading.
[email protected]90499482013-06-01 00:39:506881 rv = trans->Read(buf.get(), 256, callback.callback());
[email protected]60d94d72011-11-18 19:54:576882 EXPECT_GT(callback.GetResult(rv), 0);
[email protected]90499482013-06-01 00:39:506883 rv = trans->Read(buf.get(), 256, callback.callback());
[email protected]921ce412013-09-20 23:03:486884 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.
6900TEST(HttpCache, StopCachingThenDoneReadingDeletesEntry) {
6901 MockHttpCache cache;
ttuttle859dc7a2015-04-23 19:42:296902 TestCompletionCallback callback;
[email protected]921ce412013-09-20 23:03:486903 MockHttpRequest request(kSimpleGET_Transaction);
6904
6905 {
ttuttle859dc7a2015-04-23 19:42:296906 scoped_ptr<HttpTransaction> trans;
6907 ASSERT_EQ(OK, cache.CreateTransaction(&trans));
[email protected]921ce412013-09-20 23:03:486908
ttuttle859dc7a2015-04-23 19:42:296909 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
6910 EXPECT_EQ(OK, callback.GetResult(rv));
[email protected]921ce412013-09-20 23:03:486911
ttuttle859dc7a2015-04-23 19:42:296912 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
[email protected]921ce412013-09-20 23:03:486913 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]60d94d72011-11-18 19:54:576926 }
6927
6928 // Make sure that the ActiveEntry is gone.
[email protected]2da659e2013-05-23 20:51:346929 base::MessageLoop::current()->RunUntilIdle();
[email protected]60d94d72011-11-18 19:54:576930
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]255c0442013-09-10 23:59:046939// Tests that we stop caching when told, when using auth.
6940TEST(HttpCache, StopCachingWithAuthDeletesEntry) {
6941 MockHttpCache cache;
ttuttle859dc7a2015-04-23 19:42:296942 TestCompletionCallback callback;
[email protected]255c0442013-09-10 23:59:046943 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 {
ttuttle859dc7a2015-04-23 19:42:296949 scoped_ptr<HttpTransaction> trans;
6950 ASSERT_EQ(OK, cache.CreateTransaction(&trans));
[email protected]255c0442013-09-10 23:59:046951
ttuttle859dc7a2015-04-23 19:42:296952 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
6953 EXPECT_EQ(OK, callback.GetResult(rv));
[email protected]255c0442013-09-10 23:59:046954
6955 trans->StopCaching();
6956
ttuttle859dc7a2015-04-23 19:42:296957 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
[email protected]255c0442013-09-10 23:59:046958 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]60d94d72011-11-18 19:54:576974// Tests that when we are told to stop caching we don't throw away valid data.
6975TEST(HttpCache, StopCachingSavesEntry) {
6976 MockHttpCache cache;
ttuttle859dc7a2015-04-23 19:42:296977 TestCompletionCallback callback;
[email protected]60d94d72011-11-18 19:54:576978 MockHttpRequest request(kSimpleGET_Transaction);
6979
6980 {
ttuttle859dc7a2015-04-23 19:42:296981 scoped_ptr<HttpTransaction> trans;
6982 ASSERT_EQ(OK, cache.CreateTransaction(&trans));
[email protected]60d94d72011-11-18 19:54:576983
6984 // Force a response that can be resumed.
rvargas1c7570e2015-09-17 23:05:456985 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction);
[email protected]60d94d72011-11-18 19:54:576986 AddMockTransaction(&mock_transaction);
6987 mock_transaction.response_headers = "Cache-Control: max-age=10000\n"
6988 "Content-Length: 42\n"
[email protected]29cc1ce42012-07-22 18:39:356989 "Etag: \"foo\"\n";
[email protected]60d94d72011-11-18 19:54:576990
ttuttle859dc7a2015-04-23 19:42:296991 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
6992 EXPECT_EQ(OK, callback.GetResult(rv));
[email protected]60d94d72011-11-18 19:54:576993
ttuttle859dc7a2015-04-23 19:42:296994 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
[email protected]90499482013-06-01 00:39:506995 rv = trans->Read(buf.get(), 10, callback.callback());
[email protected]60d94d72011-11-18 19:54:576996 EXPECT_EQ(callback.GetResult(rv), 10);
6997
6998 trans->StopCaching();
6999
7000 // We should be able to keep reading.
[email protected]90499482013-06-01 00:39:507001 rv = trans->Read(buf.get(), 256, callback.callback());
[email protected]60d94d72011-11-18 19:54:577002 EXPECT_GT(callback.GetResult(rv), 0);
[email protected]90499482013-06-01 00:39:507003 rv = trans->Read(buf.get(), 256, callback.callback());
[email protected]60d94d72011-11-18 19:54:577004 EXPECT_EQ(callback.GetResult(rv), 0);
[email protected]60d94d72011-11-18 19:54:577005 }
7006
7007 // Verify that the entry is marked as incomplete.
rvargas1c7570e2015-09-17 23:05:457008 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0);
[email protected]60d94d72011-11-18 19:54:577009}
7010
[email protected]49abb412011-11-22 18:36:157011// Tests that we handle truncated enries when StopCaching is called.
7012TEST(HttpCache, StopCachingTruncatedEntry) {
7013 MockHttpCache cache;
ttuttle859dc7a2015-04-23 19:42:297014 TestCompletionCallback callback;
[email protected]49abb412011-11-22 18:36:157015 MockHttpRequest request(kRangeGET_TransactionOK);
7016 request.extra_headers.Clear();
[email protected]1dce442e2013-04-23 03:06:297017 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE);
[email protected]49abb412011-11-22 18:36:157018 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.
ttuttle859dc7a2015-04-23 19:42:297029 scoped_ptr<HttpTransaction> trans;
7030 ASSERT_EQ(OK, cache.CreateTransaction(&trans));
[email protected]49abb412011-11-22 18:36:157031
ttuttle859dc7a2015-04-23 19:42:297032 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
7033 EXPECT_EQ(OK, callback.GetResult(rv));
[email protected]49abb412011-11-22 18:36:157034
ttuttle859dc7a2015-04-23 19:42:297035 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
[email protected]90499482013-06-01 00:39:507036 rv = trans->Read(buf.get(), 10, callback.callback());
[email protected]49abb412011-11-22 18:36:157037 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]90499482013-06-01 00:39:507043 rv = trans->Read(buf.get(), 256, callback.callback());
[email protected]49abb412011-11-22 18:36:157044 EXPECT_GT(callback.GetResult(rv), 0);
[email protected]90499482013-06-01 00:39:507045 rv = trans->Read(buf.get(), 256, callback.callback());
[email protected]49abb412011-11-22 18:36:157046 EXPECT_GT(callback.GetResult(rv), 0);
[email protected]90499482013-06-01 00:39:507047 rv = trans->Read(buf.get(), 256, callback.callback());
[email protected]49abb412011-11-22 18:36:157048 EXPECT_EQ(callback.GetResult(rv), 0);
7049 }
7050
7051 // Verify that the disk entry was updated.
rvargas1c7570e2015-09-17 23:05:457052 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80);
[email protected]49abb412011-11-22 18:36:157053 RemoveMockTransaction(&kRangeGET_TransactionOK);
7054}
7055
[email protected]2a65aceb82011-12-19 20:59:277056// Tests that we detect truncated resources from the net when there is
[email protected]c85316f2011-08-18 23:27:367057// a Content-Length header.
7058TEST(HttpCache, TruncatedByContentLength) {
7059 MockHttpCache cache;
ttuttle859dc7a2015-04-23 19:42:297060 TestCompletionCallback callback;
[email protected]c85316f2011-08-18 23:27:367061
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.
7079TEST(HttpCache, TruncatedByContentLength2) {
7080 MockHttpCache cache;
ttuttle859dc7a2015-04-23 19:42:297081 TestCompletionCallback callback;
[email protected]c85316f2011-08-18 23:27:367082
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]29cc1ce42012-07-22 18:39:357087 "Etag: \"foo\"\n";
[email protected]c85316f2011-08-18 23:27:367088 RunTransactionTest(cache.http_cache(), transaction);
7089 RemoveMockTransaction(&transaction);
7090
7091 // Verify that the entry is marked as incomplete.
rvargas1c7570e2015-09-17 23:05:457092 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0);
[email protected]c85316f2011-08-18 23:27:367093}
[email protected]5a07c192012-07-30 20:18:227094
[email protected]5033ab82013-03-22 20:17:467095// Make sure that calling SetPriority on a cache transaction passes on
7096// its priority updates to its underlying network transaction.
7097TEST(HttpCache, SetPriority) {
7098 MockHttpCache cache;
7099
ttuttle859dc7a2015-04-23 19:42:297100 scoped_ptr<HttpTransaction> trans;
7101 ASSERT_EQ(OK, cache.http_cache()->CreateTransaction(IDLE, &trans));
[email protected]5033ab82013-03-22 20:17:467102
7103 // Shouldn't crash, but doesn't do anything either.
ttuttle859dc7a2015-04-23 19:42:297104 trans->SetPriority(LOW);
[email protected]5033ab82013-03-22 20:17:467105
[email protected]8ee04922013-06-07 09:05:127106 EXPECT_FALSE(cache.network_layer()->last_transaction());
ttuttle859dc7a2015-04-23 19:42:297107 EXPECT_EQ(DEFAULT_PRIORITY,
[email protected]5033ab82013-03-22 20:17:467108 cache.network_layer()->last_create_transaction_priority());
7109
ttuttle859dc7a2015-04-23 19:42:297110 HttpRequestInfo info;
[email protected]5033ab82013-03-22 20:17:467111 info.url = GURL(kSimpleGET_Transaction.url);
ttuttle859dc7a2015-04-23 19:42:297112 TestCompletionCallback callback;
7113 EXPECT_EQ(ERR_IO_PENDING,
7114 trans->Start(&info, callback.callback(), BoundNetLog()));
[email protected]5033ab82013-03-22 20:17:467115
[email protected]7ffdea02013-06-26 19:00:387116 EXPECT_TRUE(cache.network_layer()->last_transaction());
7117 if (cache.network_layer()->last_transaction()) {
ttuttle859dc7a2015-04-23 19:42:297118 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority());
7119 EXPECT_EQ(LOW, cache.network_layer()->last_transaction()->priority());
[email protected]7ffdea02013-06-26 19:00:387120 }
[email protected]5033ab82013-03-22 20:17:467121
ttuttle859dc7a2015-04-23 19:42:297122 trans->SetPriority(HIGHEST);
[email protected]7ffdea02013-06-26 19:00:387123
7124 if (cache.network_layer()->last_transaction()) {
ttuttle859dc7a2015-04-23 19:42:297125 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority());
7126 EXPECT_EQ(HIGHEST, cache.network_layer()->last_transaction()->priority());
[email protected]7ffdea02013-06-26 19:00:387127 }
[email protected]5033ab82013-03-22 20:17:467128
ttuttle859dc7a2015-04-23 19:42:297129 EXPECT_EQ(OK, callback.WaitForResult());
[email protected]5033ab82013-03-22 20:17:467130}
7131
[email protected]831e4a32013-11-14 02:14:447132// Make sure that calling SetWebSocketHandshakeStreamCreateHelper on a cache
7133// transaction passes on its argument to the underlying network transaction.
7134TEST(HttpCache, SetWebSocketHandshakeStreamCreateHelper) {
7135 MockHttpCache cache;
7136
7137 FakeWebSocketHandshakeStreamCreateHelper create_helper;
ttuttle859dc7a2015-04-23 19:42:297138 scoped_ptr<HttpTransaction> trans;
7139 ASSERT_EQ(OK, cache.http_cache()->CreateTransaction(IDLE, &trans));
[email protected]831e4a32013-11-14 02:14:447140
7141 EXPECT_FALSE(cache.network_layer()->last_transaction());
7142
ttuttle859dc7a2015-04-23 19:42:297143 HttpRequestInfo info;
[email protected]831e4a32013-11-14 02:14:447144 info.url = GURL(kSimpleGET_Transaction.url);
ttuttle859dc7a2015-04-23 19:42:297145 TestCompletionCallback callback;
7146 EXPECT_EQ(ERR_IO_PENDING,
7147 trans->Start(&info, callback.callback(), BoundNetLog()));
[email protected]831e4a32013-11-14 02:14:447148
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());
ttuttle859dc7a2015-04-23 19:42:297156 EXPECT_EQ(OK, callback.WaitForResult());
[email protected]831e4a32013-11-14 02:14:447157}
7158
[email protected]5033ab82013-03-22 20:17:467159// Make sure that a cache transaction passes on its priority to
7160// newly-created network transactions.
7161TEST(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;
rvargas3b57e37a2015-01-06 00:56:347176 transaction.data = kFullRangeData;
[email protected]5033ab82013-03-22 20:17:467177
ttuttle859dc7a2015-04-23 19:42:297178 scoped_ptr<HttpTransaction> trans;
7179 ASSERT_EQ(OK, cache.http_cache()->CreateTransaction(MEDIUM, &trans));
7180 EXPECT_EQ(DEFAULT_PRIORITY,
[email protected]5033ab82013-03-22 20:17:467181 cache.network_layer()->last_create_transaction_priority());
7182
7183 MockHttpRequest info(transaction);
ttuttle859dc7a2015-04-23 19:42:297184 TestCompletionCallback callback;
7185 EXPECT_EQ(ERR_IO_PENDING,
7186 trans->Start(&info, callback.callback(), BoundNetLog()));
7187 EXPECT_EQ(OK, callback.WaitForResult());
[email protected]5033ab82013-03-22 20:17:467188
ttuttle859dc7a2015-04-23 19:42:297189 EXPECT_EQ(MEDIUM, cache.network_layer()->last_create_transaction_priority());
[email protected]5033ab82013-03-22 20:17:467190
ttuttle859dc7a2015-04-23 19:42:297191 trans->SetPriority(HIGHEST);
[email protected]5033ab82013-03-22 20:17:467192 // Should trigger a new network transaction and pick up the new
7193 // priority.
7194 ReadAndVerifyTransaction(trans.get(), transaction);
7195
ttuttle859dc7a2015-04-23 19:42:297196 EXPECT_EQ(HIGHEST, cache.network_layer()->last_create_transaction_priority());
[email protected]5033ab82013-03-22 20:17:467197
7198 RemoveMockTransaction(&kRangeGET_TransactionOK);
7199}
[email protected]b8015c42013-12-24 15:18:197200
sclittlefb249892015-09-10 21:33:227201namespace {
7202
7203void RunTransactionAndGetNetworkBytes(MockHttpCache& cache,
7204 const MockTransaction& trans_info,
7205 int64_t* sent_bytes,
7206 int64_t* received_bytes) {
[email protected]027bd85a2013-12-27 22:39:107207 RunTransactionTestBase(cache.http_cache(), trans_info,
sclittlefb249892015-09-10 21:33:227208 MockHttpRequest(trans_info), nullptr, BoundNetLog(),
ttuttled9dbc652015-09-29 20:00:597209 nullptr, sent_bytes, received_bytes, nullptr);
[email protected]b8015c42013-12-24 15:18:197210}
7211
sclittlefb249892015-09-10 21:33:227212} // namespace
[email protected]b8015c42013-12-24 15:18:197213
sclittlefb249892015-09-10 21:33:227214TEST(HttpCache, NetworkBytesCacheMissAndThenHit) {
[email protected]b8015c42013-12-24 15:18:197215 MockHttpCache cache;
7216
7217 MockTransaction transaction(kSimpleGET_Transaction);
sclittlefb249892015-09-10 21:33:227218 int64_t sent, received;
7219 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7220 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
7221 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
[email protected]b8015c42013-12-24 15:18:197222
sclittlefb249892015-09-10 21:33:227223 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7224 EXPECT_EQ(0, sent);
7225 EXPECT_EQ(0, received);
[email protected]b8015c42013-12-24 15:18:197226}
7227
sclittlefb249892015-09-10 21:33:227228TEST(HttpCache, NetworkBytesConditionalRequest304) {
[email protected]b8015c42013-12-24 15:18:197229 MockHttpCache cache;
7230
7231 ScopedMockTransaction transaction(kETagGET_Transaction);
sclittlefb249892015-09-10 21:33:227232 int64_t sent, received;
7233 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7234 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
7235 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
[email protected]b8015c42013-12-24 15:18:197236
ttuttle859dc7a2015-04-23 19:42:297237 transaction.load_flags = LOAD_VALIDATE_CACHE;
[email protected]b8015c42013-12-24 15:18:197238 transaction.handler = ETagGet_ConditionalRequest_Handler;
sclittlefb249892015-09-10 21:33:227239 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7240 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
7241 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
[email protected]b8015c42013-12-24 15:18:197242}
7243
sclittlefb249892015-09-10 21:33:227244TEST(HttpCache, NetworkBytesConditionalRequest200) {
[email protected]b8015c42013-12-24 15:18:197245 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);
sclittlefb249892015-09-10 21:33:227256 int64_t sent, received;
7257 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7258 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
7259 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
[email protected]b8015c42013-12-24 15:18:197260
7261 RevalidationServer server;
7262 transaction.handler = server.Handler;
7263 transaction.request_headers = "Foo: none\r\n";
sclittlefb249892015-09-10 21:33:227264 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7265 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
7266 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
[email protected]b8015c42013-12-24 15:18:197267
7268 RemoveMockTransaction(&transaction);
7269}
7270
sclittlefb249892015-09-10 21:33:227271TEST(HttpCache, NetworkBytesRange) {
[email protected]b8015c42013-12-24 15:18:197272 MockHttpCache cache;
7273 AddMockTransaction(&kRangeGET_TransactionOK);
7274 MockTransaction transaction(kRangeGET_TransactionOK);
7275
7276 // Read bytes 40-49 from the network.
sclittlefb249892015-09-10 21:33:227277 int64_t sent, received;
7278 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7279 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
7280 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
[email protected]b8015c42013-12-24 15:18:197281
7282 // Read bytes 40-49 from the cache.
sclittlefb249892015-09-10 21:33:227283 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7284 EXPECT_EQ(0, sent);
7285 EXPECT_EQ(0, received);
[email protected]b8015c42013-12-24 15:18:197286 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 ";
sclittlefb249892015-09-10 21:33:227291 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7292 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
7293 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
[email protected]b8015c42013-12-24 15:18:197294 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 ";
sclittlefb249892015-09-10 21:33:227299 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7300 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes * 2, sent);
7301 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes * 2, received);
[email protected]b8015c42013-12-24 15:18:197302
7303 RemoveMockTransaction(&kRangeGET_TransactionOK);
7304}
[email protected]f311c232014-07-30 11:33:567305
jkarlinfb1d5172015-01-12 14:10:297306class 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) {
ttuttle859dc7a2015-04-23 19:42:297312 DCHECK_LT(kMaxAgeSecs, prefetch_reuse_mins() * kNumSecondsPerMinute);
jkarlinfb1d5172015-01-12 14:10:297313
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
ttuttle859dc7a2015-04-23 19:42:297332 int prefetch_reuse_mins() { return HttpCache::kPrefetchReuseMins; }
jkarlinfb1d5172015-01-12 14:10:297333
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
7347TEST_F(HttpCachePrefetchValidationTest, SkipValidationShortlyAfterPrefetch) {
ttuttle859dc7a2015-04-23 19:42:297348 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
jkarlinfb1d5172015-01-12 14:10:297349 AdvanceTime(kRequireValidationSecs);
ttuttle859dc7a2015-04-23 19:42:297350 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL));
jkarlinfb1d5172015-01-12 14:10:297351}
7352
7353TEST_F(HttpCachePrefetchValidationTest, ValidateLongAfterPrefetch) {
ttuttle859dc7a2015-04-23 19:42:297354 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
7355 AdvanceTime(prefetch_reuse_mins() * kNumSecondsPerMinute);
7356 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
jkarlinfb1d5172015-01-12 14:10:297357}
7358
7359TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceOnly) {
ttuttle859dc7a2015-04-23 19:42:297360 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
jkarlinfb1d5172015-01-12 14:10:297361 AdvanceTime(kRequireValidationSecs);
ttuttle859dc7a2015-04-23 19:42:297362 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL));
7363 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
jkarlinfb1d5172015-01-12 14:10:297364}
7365
7366TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceReadOnly) {
ttuttle859dc7a2015-04-23 19:42:297367 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
jkarlinfb1d5172015-01-12 14:10:297368 AdvanceTime(kRequireValidationSecs);
ttuttle859dc7a2015-04-23 19:42:297369 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_ONLY_FROM_CACHE));
7370 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
jkarlinfb1d5172015-01-12 14:10:297371}
7372
7373TEST_F(HttpCachePrefetchValidationTest, BypassCacheOverwritesPrefetch) {
ttuttle859dc7a2015-04-23 19:42:297374 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
jkarlinfb1d5172015-01-12 14:10:297375 AdvanceTime(kRequireValidationSecs);
ttuttle859dc7a2015-04-23 19:42:297376 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_BYPASS_CACHE));
jkarlinfb1d5172015-01-12 14:10:297377 AdvanceTime(kRequireValidationSecs);
ttuttle859dc7a2015-04-23 19:42:297378 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
jkarlinfb1d5172015-01-12 14:10:297379}
7380
7381TEST_F(HttpCachePrefetchValidationTest,
7382 SkipValidationOnExistingEntryThatNeedsValidation) {
ttuttle859dc7a2015-04-23 19:42:297383 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
jkarlinfb1d5172015-01-12 14:10:297384 AdvanceTime(kRequireValidationSecs);
ttuttle859dc7a2015-04-23 19:42:297385 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
jkarlinfb1d5172015-01-12 14:10:297386 AdvanceTime(kRequireValidationSecs);
ttuttle859dc7a2015-04-23 19:42:297387 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL));
7388 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
jkarlinfb1d5172015-01-12 14:10:297389}
7390
7391TEST_F(HttpCachePrefetchValidationTest,
7392 SkipValidationOnExistingEntryThatDoesNotNeedValidation) {
ttuttle859dc7a2015-04-23 19:42:297393 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
7394 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_PREFETCH));
jkarlinfb1d5172015-01-12 14:10:297395 AdvanceTime(kRequireValidationSecs);
ttuttle859dc7a2015-04-23 19:42:297396 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL));
7397 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
jkarlinfb1d5172015-01-12 14:10:297398}
7399
7400TEST_F(HttpCachePrefetchValidationTest, PrefetchMultipleTimes) {
ttuttle859dc7a2015-04-23 19:42:297401 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
7402 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_PREFETCH));
jkarlinfb1d5172015-01-12 14:10:297403 AdvanceTime(kRequireValidationSecs);
ttuttle859dc7a2015-04-23 19:42:297404 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL));
jkarlinfb1d5172015-01-12 14:10:297405}
7406
7407TEST_F(HttpCachePrefetchValidationTest, ValidateOnDelayedSecondPrefetch) {
ttuttle859dc7a2015-04-23 19:42:297408 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
jkarlinfb1d5172015-01-12 14:10:297409 AdvanceTime(kRequireValidationSecs);
ttuttle859dc7a2015-04-23 19:42:297410 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
jkarlinfb1d5172015-01-12 14:10:297411 AdvanceTime(kRequireValidationSecs);
ttuttle859dc7a2015-04-23 19:42:297412 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL));
jkarlinfb1d5172015-01-12 14:10:297413}
7414
ttuttle859dc7a2015-04-23 19:42:297415static void CheckResourceFreshnessHeader(const HttpRequestInfo* request,
[email protected]f311c232014-07-30 11:33:567416 std::string* response_status,
7417 std::string* response_headers,
7418 std::string* response_data) {
7419 std::string value;
[email protected]72747042014-08-15 19:29:077420 EXPECT_TRUE(request->extra_headers.GetHeader("Resource-Freshness", &value));
[email protected]f311c232014-07-30 11:33:567421 EXPECT_EQ("max-age=3600,stale-while-revalidate=7200,age=10801", value);
7422}
7423
[email protected]72747042014-08-15 19:29:077424// Verify that the Resource-Freshness header is sent on a revalidation if the
7425// stale-while-revalidate directive was on the response.
riceab38b3f92015-06-19 13:24:077426TEST(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]f311c232014-07-30 11:33:567435
7436 // Write to the cache.
riceab38b3f92015-06-19 13:24:077437 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
[email protected]f311c232014-07-30 11:33:567438
riceab38b3f92015-06-19 13:24:077439 EXPECT_EQ(1, cache.network_layer()->transaction_count());
[email protected]f311c232014-07-30 11:33:567440
[email protected]72747042014-08-15 19:29:077441 // Send the request again and check that Resource-Freshness header is added.
riceab38b3f92015-06-19 13:24:077442 stale_while_revalidate_transaction.handler = CheckResourceFreshnessHeader;
[email protected]f311c232014-07-30 11:33:567443
riceab38b3f92015-06-19 13:24:077444 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
[email protected]f311c232014-07-30 11:33:567445
riceab38b3f92015-06-19 13:24:077446 EXPECT_EQ(2, cache.network_layer()->transaction_count());
[email protected]f311c232014-07-30 11:33:567447}
7448
ttuttle859dc7a2015-04-23 19:42:297449static void CheckResourceFreshnessAbsent(const HttpRequestInfo* request,
[email protected]f311c232014-07-30 11:33:567450 std::string* response_status,
7451 std::string* response_headers,
7452 std::string* response_data) {
[email protected]72747042014-08-15 19:29:077453 EXPECT_FALSE(request->extra_headers.HasHeader("Resource-Freshness"));
[email protected]f311c232014-07-30 11:33:567454}
7455
[email protected]72747042014-08-15 19:29:077456// Verify that the Resource-Freshness header is not sent when
[email protected]f311c232014-07-30 11:33:567457// stale-while-revalidate is 0.
riceab38b3f92015-06-19 13:24:077458TEST(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]f311c232014-07-30 11:33:567467
7468 // Write to the cache.
riceab38b3f92015-06-19 13:24:077469 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
[email protected]f311c232014-07-30 11:33:567470
riceab38b3f92015-06-19 13:24:077471 EXPECT_EQ(1, cache.network_layer()->transaction_count());
[email protected]f311c232014-07-30 11:33:567472
[email protected]72747042014-08-15 19:29:077473 // Send the request again and check that Resource-Freshness header is absent.
riceab38b3f92015-06-19 13:24:077474 stale_while_revalidate_transaction.handler = CheckResourceFreshnessAbsent;
[email protected]f311c232014-07-30 11:33:567475
riceab38b3f92015-06-19 13:24:077476 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
[email protected]f311c232014-07-30 11:33:567477
riceab38b3f92015-06-19 13:24:077478 EXPECT_EQ(2, cache.network_layer()->transaction_count());
[email protected]f311c232014-07-30 11:33:567479}
dalecurtisfa29c3e2014-08-26 23:40:597480
ricea8bad09d92015-08-24 09:01:497481TEST(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
7506TEST(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
7532TEST(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
dalecurtisfa29c3e2014-08-26 23:40:597558// Tests that we allow multiple simultaneous, non-overlapping transactions to
7559// take place on a sparse entry.
7560TEST(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
ttuttle859dc7a2015-04-23 19:42:297570 TestCompletionCallback callback;
7571 scoped_ptr<HttpTransaction> trans;
7572 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
7573 EXPECT_EQ(OK, rv);
dalecurtisfa29c3e2014-08-26 23:40:597574 ASSERT_TRUE(trans.get());
7575
7576 // Start our transaction.
ttuttle859dc7a2015-04-23 19:42:297577 trans->Start(&request, callback.callback(), BoundNetLog());
dalecurtisfa29c3e2014-08-26 23:40:597578
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}
yhiranoe7d9adc2014-10-14 19:42:207589
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.
7594TEST(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);
ttuttle859dc7a2015-04-23 19:42:297602 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());
yhiranoe7d9adc2014-10-14 19:42:207607
7608 base::MessageLoop::current()->RunUntilIdle();
ttuttle859dc7a2015-04-23 19:42:297609 EXPECT_EQ(LOAD_STATE_IDLE, first->trans->GetLoadState());
yhiranoe7d9adc2014-10-14 19:42:207610 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);
ttuttle859dc7a2015-04-23 19:42:297618 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());
yhiranoe7d9adc2014-10-14 19:42:207622
7623 // Here the second transaction proceeds without reading the first body.
ttuttle859dc7a2015-04-23 19:42:297624 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState());
yhiranoe7d9adc2014-10-14 19:42:207625 base::MessageLoop::current()->RunUntilIdle();
ttuttle859dc7a2015-04-23 19:42:297626 EXPECT_EQ(LOAD_STATE_IDLE, second->trans->GetLoadState());
yhiranoe7d9adc2014-10-14 19:42:207627 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}
davidben2ec0ed342015-06-08 21:17:287632
7633// Tests that serving a response entirely from cache replays the previous
7634// SSLInfo.
7635TEST(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.
7681TEST(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
ttuttle859dc7a2015-04-23 19:42:297741} // namespace net