1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/http/http_cache.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "net/base/cache_type.h"
14 #include "net/base/host_port_pair.h"
15 #include "net/base/load_flags.h"
16 #include "net/base/load_timing_info.h"
17 #include "net/base/load_timing_info_test_util.h"
18 #include "net/base/net_errors.h"
19 #include "net/base/net_log_unittest.h"
20 #include "net/base/upload_bytes_element_reader.h"
21 #include "net/base/upload_data_stream.h"
22 #include "net/cert/cert_status_flags.h"
23 #include "net/disk_cache/disk_cache.h"
24 #include "net/http/http_byte_range.h"
25 #include "net/http/http_request_headers.h"
26 #include "net/http/http_request_info.h"
27 #include "net/http/http_response_headers.h"
28 #include "net/http/http_response_info.h"
29 #include "net/http/http_transaction.h"
30 #include "net/http/http_transaction_delegate.h"
31 #include "net/http/http_transaction_unittest.h"
32 #include "net/http/http_util.h"
33 #include "net/http/mock_http_cache.h"
34 #include "net/ssl/ssl_cert_request_info.h"
35 #include "testing/gtest/include/gtest/gtest.h"
41 // Tests the load timing values of a request that goes through a
42 // MockNetworkTransaction.
43 void TestLoadTimingNetworkRequest(const net::LoadTimingInfo
& load_timing_info
) {
44 EXPECT_FALSE(load_timing_info
.socket_reused
);
45 EXPECT_NE(net::NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
47 EXPECT_TRUE(load_timing_info
.proxy_resolve_start
.is_null());
48 EXPECT_TRUE(load_timing_info
.proxy_resolve_end
.is_null());
50 net::ExpectConnectTimingHasTimes(load_timing_info
.connect_timing
,
51 net::CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY
);
52 EXPECT_LE(load_timing_info
.connect_timing
.connect_end
,
53 load_timing_info
.send_start
);
55 EXPECT_LE(load_timing_info
.send_start
, load_timing_info
.send_end
);
57 // Set by URLRequest / URLRequestHttpJob, at a higher level.
58 EXPECT_TRUE(load_timing_info
.request_start_time
.is_null());
59 EXPECT_TRUE(load_timing_info
.request_start
.is_null());
60 EXPECT_TRUE(load_timing_info
.receive_headers_end
.is_null());
63 // Tests the load timing values of a request that receives a cached response.
64 void TestLoadTimingCachedResponse(const net::LoadTimingInfo
& load_timing_info
) {
65 EXPECT_FALSE(load_timing_info
.socket_reused
);
66 EXPECT_EQ(net::NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
68 EXPECT_TRUE(load_timing_info
.proxy_resolve_start
.is_null());
69 EXPECT_TRUE(load_timing_info
.proxy_resolve_end
.is_null());
71 net::ExpectConnectTimingHasNoTimes(load_timing_info
.connect_timing
);
73 // Only the send start / end times should be sent, and they should have the
75 EXPECT_FALSE(load_timing_info
.send_start
.is_null());
76 EXPECT_EQ(load_timing_info
.send_start
, load_timing_info
.send_end
);
78 // Set by URLRequest / URLRequestHttpJob, at a higher level.
79 EXPECT_TRUE(load_timing_info
.request_start_time
.is_null());
80 EXPECT_TRUE(load_timing_info
.request_start
.is_null());
81 EXPECT_TRUE(load_timing_info
.receive_headers_end
.is_null());
84 class DeleteCacheCompletionCallback
: public net::TestCompletionCallbackBase
{
86 explicit DeleteCacheCompletionCallback(MockHttpCache
* cache
)
88 callback_(base::Bind(&DeleteCacheCompletionCallback::OnComplete
,
89 base::Unretained(this))) {
92 const net::CompletionCallback
& callback() const { return callback_
; }
95 void OnComplete(int result
) {
100 MockHttpCache
* cache_
;
101 net::CompletionCallback callback_
;
103 DISALLOW_COPY_AND_ASSIGN(DeleteCacheCompletionCallback
);
106 //-----------------------------------------------------------------------------
109 class TestHttpTransactionDelegate
: public net::HttpTransactionDelegate
{
111 TestHttpTransactionDelegate(int num_cache_actions_to_observe
,
112 int num_network_actions_to_observe
)
113 : num_callbacks_observed_(0),
114 num_remaining_cache_actions_to_observe_(num_cache_actions_to_observe
),
115 num_remaining_network_actions_to_observe_(
116 num_network_actions_to_observe
),
117 cache_action_in_progress_(false),
118 network_action_in_progress_(false) {
120 virtual ~TestHttpTransactionDelegate() {
121 EXPECT_EQ(0, num_remaining_cache_actions_to_observe_
);
122 EXPECT_EQ(0, num_remaining_network_actions_to_observe_
);
123 EXPECT_FALSE(cache_action_in_progress_
);
124 EXPECT_FALSE(network_action_in_progress_
);
126 virtual void OnCacheActionStart() OVERRIDE
{
127 num_callbacks_observed_
++;
128 EXPECT_FALSE(cache_action_in_progress_
);
129 EXPECT_FALSE(network_action_in_progress_
);
130 EXPECT_GT(num_remaining_cache_actions_to_observe_
, 0);
131 num_remaining_cache_actions_to_observe_
--;
132 cache_action_in_progress_
= true;
134 virtual void OnCacheActionFinish() OVERRIDE
{
135 num_callbacks_observed_
++;
136 EXPECT_TRUE(cache_action_in_progress_
);
137 cache_action_in_progress_
= false;
139 virtual void OnNetworkActionStart() OVERRIDE
{
140 num_callbacks_observed_
++;
141 EXPECT_FALSE(cache_action_in_progress_
);
142 EXPECT_FALSE(network_action_in_progress_
);
143 EXPECT_GT(num_remaining_network_actions_to_observe_
, 0);
144 num_remaining_network_actions_to_observe_
--;
145 network_action_in_progress_
= true;
147 virtual void OnNetworkActionFinish() OVERRIDE
{
148 num_callbacks_observed_
++;
149 EXPECT_TRUE(network_action_in_progress_
);
150 network_action_in_progress_
= false;
153 int num_callbacks_observed() { return num_callbacks_observed_
; }
156 int num_callbacks_observed_
;
157 int num_remaining_cache_actions_to_observe_
;
158 int num_remaining_network_actions_to_observe_
;
159 bool cache_action_in_progress_
;
160 bool network_action_in_progress_
;
163 void ReadAndVerifyTransaction(net::HttpTransaction
* trans
,
164 const MockTransaction
& trans_info
) {
166 int rv
= ReadTransaction(trans
, &content
);
168 EXPECT_EQ(net::OK
, rv
);
169 std::string
expected(trans_info
.data
);
170 EXPECT_EQ(expected
, content
);
173 const int kNoDelegateTransactionCheck
= -1;
175 void RunTransactionTestWithRequestAndDelegateAndGetTiming(
176 net::HttpCache
* cache
,
177 const MockTransaction
& trans_info
,
178 const MockHttpRequest
& request
,
179 net::HttpResponseInfo
* response_info
,
180 int num_cache_delegate_actions
,
181 int num_network_delegate_actions
,
182 const net::BoundNetLog
& net_log
,
183 net::LoadTimingInfo
* load_timing_info
) {
184 net::TestCompletionCallback callback
;
186 // write to the cache
188 scoped_ptr
<TestHttpTransactionDelegate
> delegate
;
189 if (num_cache_delegate_actions
!= kNoDelegateTransactionCheck
&&
190 num_network_delegate_actions
!= kNoDelegateTransactionCheck
) {
192 new TestHttpTransactionDelegate(num_cache_delegate_actions
,
193 num_network_delegate_actions
));
195 scoped_ptr
<net::HttpTransaction
> trans
;
196 int rv
= cache
->CreateTransaction(
197 net::DEFAULT_PRIORITY
, &trans
, delegate
.get());
198 EXPECT_EQ(net::OK
, rv
);
199 ASSERT_TRUE(trans
.get());
201 rv
= trans
->Start(&request
, callback
.callback(), net_log
);
202 if (rv
== net::ERR_IO_PENDING
)
203 rv
= callback
.WaitForResult();
204 ASSERT_EQ(trans_info
.return_code
, rv
);
209 const net::HttpResponseInfo
* response
= trans
->GetResponseInfo();
210 ASSERT_TRUE(response
);
213 *response_info
= *response
;
215 if (load_timing_info
) {
216 // If a fake network connection is used, need a NetLog to get a fake socket
218 EXPECT_TRUE(net_log
.net_log());
219 *load_timing_info
= net::LoadTimingInfo();
220 trans
->GetLoadTimingInfo(load_timing_info
);
223 ReadAndVerifyTransaction(trans
.get(), trans_info
);
226 void RunTransactionTestWithRequestAndDelegate(
227 net::HttpCache
* cache
,
228 const MockTransaction
& trans_info
,
229 const MockHttpRequest
& request
,
230 net::HttpResponseInfo
* response_info
,
231 int num_cache_delegate_actions
,
232 int num_network_delegate_actions
) {
233 RunTransactionTestWithRequestAndDelegateAndGetTiming(
234 cache
, trans_info
, request
, response_info
, num_cache_delegate_actions
,
235 num_network_delegate_actions
, net::BoundNetLog(), NULL
);
238 void RunTransactionTestWithRequest(net::HttpCache
* cache
,
239 const MockTransaction
& trans_info
,
240 const MockHttpRequest
& request
,
241 net::HttpResponseInfo
* response_info
) {
242 RunTransactionTestWithRequestAndDelegate(
243 cache
, trans_info
, request
, response_info
, kNoDelegateTransactionCheck
,
244 kNoDelegateTransactionCheck
);
247 void RunTransactionTestAndGetTiming(
248 net::HttpCache
* cache
,
249 const MockTransaction
& trans_info
,
250 const net::BoundNetLog
& log
,
251 net::LoadTimingInfo
* load_timing_info
) {
252 RunTransactionTestWithRequestAndDelegateAndGetTiming(
253 cache
, trans_info
, MockHttpRequest(trans_info
), NULL
,
254 kNoDelegateTransactionCheck
, kNoDelegateTransactionCheck
, log
,
258 void RunTransactionTestWithDelegate(net::HttpCache
* cache
,
259 const MockTransaction
& trans_info
,
260 int num_cache_delegate_actions
,
261 int num_network_delegate_actions
) {
262 RunTransactionTestWithRequestAndDelegate(
263 cache
, trans_info
, MockHttpRequest(trans_info
), NULL
,
264 num_cache_delegate_actions
, num_network_delegate_actions
);
267 void RunTransactionTest(net::HttpCache
* cache
,
268 const MockTransaction
& trans_info
) {
269 RunTransactionTestAndGetTiming(cache
, trans_info
, net::BoundNetLog(), NULL
);
272 void RunTransactionTestWithResponseInfo(net::HttpCache
* cache
,
273 const MockTransaction
& trans_info
,
274 net::HttpResponseInfo
* response
) {
275 RunTransactionTestWithRequest(
276 cache
, trans_info
, MockHttpRequest(trans_info
), response
);
279 void RunTransactionTestWithResponseInfoAndGetTiming(
280 net::HttpCache
* cache
,
281 const MockTransaction
& trans_info
,
282 net::HttpResponseInfo
* response
,
283 const net::BoundNetLog
& log
,
284 net::LoadTimingInfo
* load_timing_info
) {
285 RunTransactionTestWithRequestAndDelegateAndGetTiming(
286 cache
, trans_info
, MockHttpRequest(trans_info
), response
,
287 kNoDelegateTransactionCheck
, kNoDelegateTransactionCheck
, log
,
291 void RunTransactionTestWithResponse(net::HttpCache
* cache
,
292 const MockTransaction
& trans_info
,
293 std::string
* response_headers
) {
294 net::HttpResponseInfo response
;
295 RunTransactionTestWithResponseInfo(cache
, trans_info
, &response
);
296 response
.headers
->GetNormalizedHeaders(response_headers
);
299 void RunTransactionTestWithResponseAndGetTiming(
300 net::HttpCache
* cache
,
301 const MockTransaction
& trans_info
,
302 std::string
* response_headers
,
303 const net::BoundNetLog
& log
,
304 net::LoadTimingInfo
* load_timing_info
) {
305 net::HttpResponseInfo response
;
306 RunTransactionTestWithRequestAndDelegateAndGetTiming(
307 cache
, trans_info
, MockHttpRequest(trans_info
), &response
,
308 kNoDelegateTransactionCheck
, kNoDelegateTransactionCheck
,
309 log
, load_timing_info
);
310 response
.headers
->GetNormalizedHeaders(response_headers
);
313 // This class provides a handler for kFastNoStoreGET_Transaction so that the
314 // no-store header can be included on demand.
315 class FastTransactionServer
{
317 FastTransactionServer() {
320 ~FastTransactionServer() {}
322 void set_no_store(bool value
) { no_store
= value
; }
324 static void FastNoStoreHandler(const net::HttpRequestInfo
* request
,
325 std::string
* response_status
,
326 std::string
* response_headers
,
327 std::string
* response_data
) {
329 *response_headers
= "Cache-Control: no-store\n";
333 static bool no_store
;
334 DISALLOW_COPY_AND_ASSIGN(FastTransactionServer
);
336 bool FastTransactionServer::no_store
;
338 const MockTransaction kFastNoStoreGET_Transaction
= {
339 "http://www.google.com/nostore",
343 net::LOAD_VALIDATE_CACHE
,
345 "Cache-Control: max-age=10000\n",
347 "<html><body>Google Blah Blah</body></html>",
348 TEST_MODE_SYNC_NET_START
,
349 &FastTransactionServer::FastNoStoreHandler
,
354 // This class provides a handler for kRangeGET_TransactionOK so that the range
355 // request can be served on demand.
356 class RangeTransactionServer
{
358 RangeTransactionServer() {
359 not_modified_
= false;
363 ~RangeTransactionServer() {
364 not_modified_
= false;
369 // Returns only 416 or 304 when set.
370 void set_not_modified(bool value
) { not_modified_
= value
; }
372 // Returns 206 when revalidating a range (instead of 304).
373 void set_modified(bool value
) { modified_
= value
; }
375 // Returns 200 instead of 206 (a malformed response overall).
376 void set_bad_200(bool value
) { bad_200_
= value
; }
378 static void RangeHandler(const net::HttpRequestInfo
* request
,
379 std::string
* response_status
,
380 std::string
* response_headers
,
381 std::string
* response_data
);
384 static bool not_modified_
;
385 static bool modified_
;
386 static bool bad_200_
;
387 DISALLOW_COPY_AND_ASSIGN(RangeTransactionServer
);
389 bool RangeTransactionServer::not_modified_
= false;
390 bool RangeTransactionServer::modified_
= false;
391 bool RangeTransactionServer::bad_200_
= false;
393 // A dummy extra header that must be preserved on a given request.
395 // EXTRA_HEADER_LINE doesn't include a line terminator because it
396 // will be passed to AddHeaderFromString() which doesn't accept them.
397 #define EXTRA_HEADER_LINE "Extra: header"
399 // EXTRA_HEADER contains a line terminator, as expected by
400 // AddHeadersFromString() (_not_ AddHeaderFromString()).
401 #define EXTRA_HEADER EXTRA_HEADER_LINE "\r\n"
403 static const char kExtraHeaderKey
[] = "Extra";
406 void RangeTransactionServer::RangeHandler(const net::HttpRequestInfo
* request
,
407 std::string
* response_status
,
408 std::string
* response_headers
,
409 std::string
* response_data
) {
410 if (request
->extra_headers
.IsEmpty()) {
411 response_status
->assign("HTTP/1.1 416 Requested Range Not Satisfiable");
412 response_data
->clear();
416 // We want to make sure we don't delete extra headers.
417 EXPECT_TRUE(request
->extra_headers
.HasHeader(kExtraHeaderKey
));
420 response_status
->assign("HTTP/1.1 304 Not Modified");
421 response_data
->clear();
425 std::vector
<net::HttpByteRange
> ranges
;
426 std::string range_header
;
427 if (!request
->extra_headers
.GetHeader(
428 net::HttpRequestHeaders::kRange
, &range_header
) ||
429 !net::HttpUtil::ParseRangeHeader(range_header
, &ranges
) || bad_200_
||
430 ranges
.size() != 1) {
431 // This is not a byte range request. We return 200.
432 response_status
->assign("HTTP/1.1 200 OK");
433 response_headers
->assign("Date: Wed, 28 Nov 2007 09:40:09 GMT");
434 response_data
->assign("Not a range");
438 // We can handle this range request.
439 net::HttpByteRange byte_range
= ranges
[0];
440 if (byte_range
.first_byte_position() > 79) {
441 response_status
->assign("HTTP/1.1 416 Requested Range Not Satisfiable");
442 response_data
->clear();
446 EXPECT_TRUE(byte_range
.ComputeBounds(80));
447 int start
= static_cast<int>(byte_range
.first_byte_position());
448 int end
= static_cast<int>(byte_range
.last_byte_position());
452 std::string content_range
= base::StringPrintf(
453 "Content-Range: bytes %d-%d/80\n", start
, end
);
454 response_headers
->append(content_range
);
456 if (!request
->extra_headers
.HasHeader("If-None-Match") || modified_
) {
459 EXPECT_EQ(0, end
% 10);
462 EXPECT_EQ(9, (end
- start
) % 10);
463 for (int block_start
= start
; block_start
< end
; block_start
+= 10) {
464 base::StringAppendF(&data
, "rg: %02d-%02d ",
465 block_start
, block_start
+ 9);
468 *response_data
= data
;
470 if (end
- start
!= 9) {
471 // We also have to fix content-length.
472 int len
= end
- start
+ 1;
473 std::string content_length
= base::StringPrintf("Content-Length: %d\n",
475 response_headers
->replace(response_headers
->find("Content-Length:"),
476 content_length
.size(), content_length
);
479 response_status
->assign("HTTP/1.1 304 Not Modified");
480 response_data
->clear();
484 const MockTransaction kRangeGET_TransactionOK
= {
485 "http://www.google.com/range",
488 "Range: bytes = 40-49\r\n"
491 "HTTP/1.1 206 Partial Content",
492 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
494 "Accept-Ranges: bytes\n"
495 "Content-Length: 10\n",
499 &RangeTransactionServer::RangeHandler
,
504 // Verifies the response headers (|response|) match a partial content
505 // response for the range starting at |start| and ending at |end|.
506 void Verify206Response(std::string response
, int start
, int end
) {
507 std::string
raw_headers(net::HttpUtil::AssembleRawHeaders(response
.data(),
509 scoped_refptr
<net::HttpResponseHeaders
> headers(
510 new net::HttpResponseHeaders(raw_headers
));
512 ASSERT_EQ(206, headers
->response_code());
514 int64 range_start
, range_end
, object_size
;
516 headers
->GetContentRange(&range_start
, &range_end
, &object_size
));
517 int64 content_length
= headers
->GetContentLength();
519 int length
= end
- start
+ 1;
520 ASSERT_EQ(length
, content_length
);
521 ASSERT_EQ(start
, range_start
);
522 ASSERT_EQ(end
, range_end
);
525 // Creates a truncated entry that can be resumed using byte ranges.
526 void CreateTruncatedEntry(std::string raw_headers
, MockHttpCache
* cache
) {
527 // Create a disk cache entry that stores an incomplete resource.
528 disk_cache::Entry
* entry
;
529 ASSERT_TRUE(cache
->CreateBackendEntry(kRangeGET_TransactionOK
.url
, &entry
,
532 raw_headers
= net::HttpUtil::AssembleRawHeaders(raw_headers
.data(),
535 net::HttpResponseInfo response
;
536 response
.response_time
= base::Time::Now();
537 response
.request_time
= base::Time::Now();
538 response
.headers
= new net::HttpResponseHeaders(raw_headers
);
539 // Set the last argument for this to be an incomplete request.
540 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry
, &response
, true, true));
542 scoped_refptr
<net::IOBuffer
> buf(new net::IOBuffer(100));
543 int len
= static_cast<int>(base::strlcpy(buf
->data(),
544 "rg: 00-09 rg: 10-19 ", 100));
545 net::TestCompletionCallback cb
;
546 int rv
= entry
->WriteData(1, 0, buf
.get(), len
, cb
.callback(), true);
547 EXPECT_EQ(len
, cb
.GetResult(rv
));
551 // Helper to represent a network HTTP response.
553 // Set this response into |trans|.
554 void AssignTo(MockTransaction
* trans
) const {
555 trans
->status
= status
;
556 trans
->response_headers
= headers
;
560 std::string
status_and_headers() const {
561 return std::string(status
) + "\n" + std::string(headers
);
570 Context() : result(net::ERR_IO_PENDING
) {}
573 net::TestCompletionCallback callback
;
574 scoped_ptr
<net::HttpTransaction
> trans
;
580 //-----------------------------------------------------------------------------
583 TEST(HttpCache
, CreateThenDestroy
) {
586 scoped_ptr
<net::HttpTransaction
> trans
;
587 int rv
= cache
.http_cache()->CreateTransaction(
588 net::DEFAULT_PRIORITY
, &trans
, NULL
);
589 EXPECT_EQ(net::OK
, rv
);
590 ASSERT_TRUE(trans
.get());
593 TEST(HttpCache
, GetBackend
) {
594 MockHttpCache
cache(net::HttpCache::DefaultBackend::InMemory(0));
596 disk_cache::Backend
* backend
;
597 net::TestCompletionCallback cb
;
598 // This will lazily initialize the backend.
599 int rv
= cache
.http_cache()->GetBackend(&backend
, cb
.callback());
600 EXPECT_EQ(net::OK
, cb
.GetResult(rv
));
603 TEST(HttpCache
, SimpleGET
) {
605 net::CapturingBoundNetLog log
;
606 net::LoadTimingInfo load_timing_info
;
608 // Write to the cache.
609 RunTransactionTestAndGetTiming(cache
.http_cache(), kSimpleGET_Transaction
,
610 log
.bound(), &load_timing_info
);
612 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
613 EXPECT_EQ(0, cache
.disk_cache()->open_count());
614 EXPECT_EQ(1, cache
.disk_cache()->create_count());
615 TestLoadTimingNetworkRequest(load_timing_info
);
618 TEST(HttpCache
, SimpleGETNoDiskCache
) {
621 cache
.disk_cache()->set_fail_requests();
623 net::CapturingBoundNetLog log
;
624 log
.SetLogLevel(net::NetLog::LOG_BASIC
);
625 net::LoadTimingInfo load_timing_info
;
627 // Read from the network, and don't use the cache.
628 RunTransactionTestAndGetTiming(cache
.http_cache(), kSimpleGET_Transaction
,
629 log
.bound(), &load_timing_info
);
631 // Check that the NetLog was filled as expected.
632 // (We attempted to both Open and Create entries, but both failed).
633 net::CapturingNetLog::CapturedEntryList entries
;
634 log
.GetEntries(&entries
);
636 EXPECT_EQ(6u, entries
.size());
637 EXPECT_TRUE(net::LogContainsBeginEvent(
638 entries
, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND
));
639 EXPECT_TRUE(net::LogContainsEndEvent(
640 entries
, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND
));
641 EXPECT_TRUE(net::LogContainsBeginEvent(
642 entries
, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
));
643 EXPECT_TRUE(net::LogContainsEndEvent(
644 entries
, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
));
645 EXPECT_TRUE(net::LogContainsBeginEvent(
646 entries
, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
));
647 EXPECT_TRUE(net::LogContainsEndEvent(
648 entries
, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
));
650 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
651 EXPECT_EQ(0, cache
.disk_cache()->open_count());
652 EXPECT_EQ(0, cache
.disk_cache()->create_count());
653 TestLoadTimingNetworkRequest(load_timing_info
);
656 TEST(HttpCache
, SimpleGETNoDiskCache2
) {
657 // This will initialize a cache object with NULL backend.
658 MockBlockingBackendFactory
* factory
= new MockBlockingBackendFactory();
659 factory
->set_fail(true);
660 factory
->FinishCreation(); // We'll complete synchronously.
661 MockHttpCache
cache(factory
);
663 // Read from the network, and don't use the cache.
664 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
666 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
667 EXPECT_FALSE(cache
.http_cache()->GetCurrentBackend());
670 // Tests that IOBuffers are not referenced after IO completes.
671 TEST(HttpCache
, ReleaseBuffer
) {
674 // Write to the cache.
675 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
677 MockHttpRequest
request(kSimpleGET_Transaction
);
678 scoped_ptr
<net::HttpTransaction
> trans
;
679 int rv
= cache
.http_cache()->CreateTransaction(
680 net::DEFAULT_PRIORITY
, &trans
, NULL
);
681 ASSERT_EQ(net::OK
, rv
);
683 const int kBufferSize
= 10;
684 scoped_refptr
<net::IOBuffer
> buffer(new net::IOBuffer(kBufferSize
));
685 net::ReleaseBufferCompletionCallback
cb(buffer
.get());
687 rv
= trans
->Start(&request
, cb
.callback(), net::BoundNetLog());
688 EXPECT_EQ(net::OK
, cb
.GetResult(rv
));
690 rv
= trans
->Read(buffer
.get(), kBufferSize
, cb
.callback());
691 EXPECT_EQ(kBufferSize
, cb
.GetResult(rv
));
694 TEST(HttpCache
, SimpleGETWithDiskFailures
) {
697 cache
.disk_cache()->set_soft_failures(true);
699 // Read from the network, and fail to write to the cache.
700 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
702 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
703 EXPECT_EQ(0, cache
.disk_cache()->open_count());
704 EXPECT_EQ(1, cache
.disk_cache()->create_count());
706 // This one should see an empty cache again.
707 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
709 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
710 EXPECT_EQ(0, cache
.disk_cache()->open_count());
711 EXPECT_EQ(2, cache
.disk_cache()->create_count());
714 // Tests that disk failures after the transaction has started don't cause the
716 TEST(HttpCache
, SimpleGETWithDiskFailures2
) {
719 MockHttpRequest
request(kSimpleGET_Transaction
);
721 scoped_ptr
<Context
> c(new Context());
722 int rv
= cache
.http_cache()->CreateTransaction(
723 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
724 EXPECT_EQ(net::OK
, rv
);
726 rv
= c
->trans
->Start(&request
, c
->callback
.callback(), net::BoundNetLog());
727 EXPECT_EQ(net::ERR_IO_PENDING
, rv
);
728 rv
= c
->callback
.WaitForResult();
730 // Start failing request now.
731 cache
.disk_cache()->set_soft_failures(true);
733 // We have to open the entry again to propagate the failure flag.
734 disk_cache::Entry
* en
;
735 ASSERT_TRUE(cache
.OpenBackendEntry(kSimpleGET_Transaction
.url
, &en
));
738 ReadAndVerifyTransaction(c
->trans
.get(), kSimpleGET_Transaction
);
741 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
742 EXPECT_EQ(1, cache
.disk_cache()->open_count());
743 EXPECT_EQ(1, cache
.disk_cache()->create_count());
745 // This one should see an empty cache again.
746 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
748 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
749 EXPECT_EQ(1, cache
.disk_cache()->open_count());
750 EXPECT_EQ(2, cache
.disk_cache()->create_count());
753 // Tests that we handle failures to read from the cache.
754 TEST(HttpCache
, SimpleGETWithDiskFailures3
) {
757 // Read from the network, and write to the cache.
758 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
760 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
761 EXPECT_EQ(0, cache
.disk_cache()->open_count());
762 EXPECT_EQ(1, cache
.disk_cache()->create_count());
764 cache
.disk_cache()->set_soft_failures(true);
766 // Now fail to read from the cache.
767 scoped_ptr
<Context
> c(new Context());
768 int rv
= cache
.http_cache()->CreateTransaction(
769 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
770 EXPECT_EQ(net::OK
, rv
);
772 MockHttpRequest
request(kSimpleGET_Transaction
);
773 rv
= c
->trans
->Start(&request
, c
->callback
.callback(), net::BoundNetLog());
774 EXPECT_EQ(net::OK
, c
->callback
.GetResult(rv
));
776 // Now verify that the entry was removed from the cache.
777 cache
.disk_cache()->set_soft_failures(false);
779 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
780 EXPECT_EQ(1, cache
.disk_cache()->open_count());
781 EXPECT_EQ(2, cache
.disk_cache()->create_count());
783 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
785 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
786 EXPECT_EQ(1, cache
.disk_cache()->open_count());
787 EXPECT_EQ(3, cache
.disk_cache()->create_count());
790 TEST(HttpCache
, SimpleGET_LoadOnlyFromCache_Hit
) {
793 net::CapturingBoundNetLog log
;
795 // This prevents a number of write events from being logged.
796 log
.SetLogLevel(net::NetLog::LOG_BASIC
);
798 net::LoadTimingInfo load_timing_info
;
800 // Write to the cache.
801 RunTransactionTestAndGetTiming(cache
.http_cache(), kSimpleGET_Transaction
,
802 log
.bound(), &load_timing_info
);
804 // Check that the NetLog was filled as expected.
805 net::CapturingNetLog::CapturedEntryList entries
;
806 log
.GetEntries(&entries
);
808 EXPECT_EQ(8u, entries
.size());
809 EXPECT_TRUE(net::LogContainsBeginEvent(
810 entries
, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND
));
811 EXPECT_TRUE(net::LogContainsEndEvent(
812 entries
, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND
));
813 EXPECT_TRUE(net::LogContainsBeginEvent(
814 entries
, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
));
815 EXPECT_TRUE(net::LogContainsEndEvent(
816 entries
, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
));
817 EXPECT_TRUE(net::LogContainsBeginEvent(
818 entries
, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
));
819 EXPECT_TRUE(net::LogContainsEndEvent(
820 entries
, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
));
821 EXPECT_TRUE(net::LogContainsBeginEvent(
822 entries
, 6, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
));
823 EXPECT_TRUE(net::LogContainsEndEvent(
824 entries
, 7, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
));
826 TestLoadTimingNetworkRequest(load_timing_info
);
828 // Force this transaction to read from the cache.
829 MockTransaction
transaction(kSimpleGET_Transaction
);
830 transaction
.load_flags
|= net::LOAD_ONLY_FROM_CACHE
;
834 RunTransactionTestAndGetTiming(cache
.http_cache(), transaction
, log
.bound(),
837 // Check that the NetLog was filled as expected.
838 log
.GetEntries(&entries
);
840 EXPECT_EQ(8u, entries
.size());
841 EXPECT_TRUE(net::LogContainsBeginEvent(
842 entries
, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND
));
843 EXPECT_TRUE(net::LogContainsEndEvent(
844 entries
, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND
));
845 EXPECT_TRUE(net::LogContainsBeginEvent(
846 entries
, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
));
847 EXPECT_TRUE(net::LogContainsEndEvent(
848 entries
, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
));
849 EXPECT_TRUE(net::LogContainsBeginEvent(
850 entries
, 4, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
));
851 EXPECT_TRUE(net::LogContainsEndEvent(
852 entries
, 5, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
));
853 EXPECT_TRUE(net::LogContainsBeginEvent(
854 entries
, 6, net::NetLog::TYPE_HTTP_CACHE_READ_INFO
));
855 EXPECT_TRUE(net::LogContainsEndEvent(
856 entries
, 7, net::NetLog::TYPE_HTTP_CACHE_READ_INFO
));
858 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
859 EXPECT_EQ(1, cache
.disk_cache()->open_count());
860 EXPECT_EQ(1, cache
.disk_cache()->create_count());
861 TestLoadTimingCachedResponse(load_timing_info
);
864 TEST(HttpCache
, SimpleGET_LoadOnlyFromCache_Miss
) {
867 // force this transaction to read from the cache
868 MockTransaction
transaction(kSimpleGET_Transaction
);
869 transaction
.load_flags
|= net::LOAD_ONLY_FROM_CACHE
;
871 MockHttpRequest
request(transaction
);
872 net::TestCompletionCallback callback
;
874 scoped_ptr
<net::HttpTransaction
> trans
;
875 int rv
= cache
.http_cache()->CreateTransaction(
876 net::DEFAULT_PRIORITY
, &trans
, NULL
);
877 EXPECT_EQ(net::OK
, rv
);
878 ASSERT_TRUE(trans
.get());
880 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
881 if (rv
== net::ERR_IO_PENDING
)
882 rv
= callback
.WaitForResult();
883 ASSERT_EQ(net::ERR_CACHE_MISS
, rv
);
887 EXPECT_EQ(0, cache
.network_layer()->transaction_count());
888 EXPECT_EQ(0, cache
.disk_cache()->open_count());
889 EXPECT_EQ(0, cache
.disk_cache()->create_count());
892 TEST(HttpCache
, SimpleGET_LoadPreferringCache_Hit
) {
895 // write to the cache
896 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
898 // force this transaction to read from the cache if valid
899 MockTransaction
transaction(kSimpleGET_Transaction
);
900 transaction
.load_flags
|= net::LOAD_PREFERRING_CACHE
;
902 RunTransactionTest(cache
.http_cache(), transaction
);
904 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
905 EXPECT_EQ(1, cache
.disk_cache()->open_count());
906 EXPECT_EQ(1, cache
.disk_cache()->create_count());
909 TEST(HttpCache
, SimpleGET_LoadPreferringCache_Miss
) {
912 // force this transaction to read from the cache if valid
913 MockTransaction
transaction(kSimpleGET_Transaction
);
914 transaction
.load_flags
|= net::LOAD_PREFERRING_CACHE
;
916 RunTransactionTest(cache
.http_cache(), transaction
);
918 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
919 EXPECT_EQ(0, cache
.disk_cache()->open_count());
920 EXPECT_EQ(1, cache
.disk_cache()->create_count());
923 // Tests LOAD_PREFERRING_CACHE in the presence of vary headers.
924 TEST(HttpCache
, SimpleGET_LoadPreferringCache_VaryMatch
) {
927 // Write to the cache.
928 MockTransaction
transaction(kSimpleGET_Transaction
);
929 transaction
.request_headers
= "Foo: bar\r\n";
930 transaction
.response_headers
= "Cache-Control: max-age=10000\n"
932 AddMockTransaction(&transaction
);
933 RunTransactionTest(cache
.http_cache(), transaction
);
935 // Read from the cache.
936 transaction
.load_flags
|= net::LOAD_PREFERRING_CACHE
;
937 RunTransactionTest(cache
.http_cache(), transaction
);
939 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
940 EXPECT_EQ(1, cache
.disk_cache()->open_count());
941 EXPECT_EQ(1, cache
.disk_cache()->create_count());
942 RemoveMockTransaction(&transaction
);
945 // Tests LOAD_PREFERRING_CACHE in the presence of vary headers.
946 TEST(HttpCache
, SimpleGET_LoadPreferringCache_VaryMismatch
) {
949 // Write to the cache.
950 MockTransaction
transaction(kSimpleGET_Transaction
);
951 transaction
.request_headers
= "Foo: bar\r\n";
952 transaction
.response_headers
= "Cache-Control: max-age=10000\n"
954 AddMockTransaction(&transaction
);
955 RunTransactionTest(cache
.http_cache(), transaction
);
957 // Attempt to read from the cache... this is a vary mismatch that must reach
958 // the network again.
959 transaction
.load_flags
|= net::LOAD_PREFERRING_CACHE
;
960 transaction
.request_headers
= "Foo: none\r\n";
961 net::CapturingBoundNetLog log
;
962 net::LoadTimingInfo load_timing_info
;
963 RunTransactionTestAndGetTiming(cache
.http_cache(), transaction
, log
.bound(),
966 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
967 EXPECT_EQ(1, cache
.disk_cache()->open_count());
968 EXPECT_EQ(1, cache
.disk_cache()->create_count());
969 TestLoadTimingNetworkRequest(load_timing_info
);
970 RemoveMockTransaction(&transaction
);
973 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on
975 TEST(HttpCache
, SimpleGET_CacheOverride_Network
) {
979 MockTransaction
transaction(kSimpleGET_Transaction
);
980 transaction
.load_flags
|= net::LOAD_FROM_CACHE_IF_OFFLINE
;
981 transaction
.response_headers
= "Cache-Control: no-cache\n";
983 AddMockTransaction(&transaction
);
984 RunTransactionTest(cache
.http_cache(), transaction
);
985 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
986 EXPECT_EQ(1, cache
.disk_cache()->create_count());
987 RemoveMockTransaction(&transaction
);
989 // Re-run transaction; make sure the result came from the network,
991 transaction
.data
= "Changed data.";
992 AddMockTransaction(&transaction
);
993 net::HttpResponseInfo response_info
;
994 RunTransactionTestWithResponseInfo(cache
.http_cache(), transaction
,
997 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
998 EXPECT_FALSE(response_info
.server_data_unavailable
);
999 EXPECT_TRUE(response_info
.network_accessed
);
1001 RemoveMockTransaction(&transaction
);
1004 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on
1006 TEST(HttpCache
, SimpleGET_CacheOverride_Offline
) {
1007 MockHttpCache cache
;
1010 MockTransaction
transaction(kSimpleGET_Transaction
);
1011 transaction
.load_flags
|= net::LOAD_FROM_CACHE_IF_OFFLINE
;
1012 transaction
.response_headers
= "Cache-Control: no-cache\n";
1014 AddMockTransaction(&transaction
);
1015 RunTransactionTest(cache
.http_cache(), transaction
);
1016 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
1017 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1018 RemoveMockTransaction(&transaction
);
1020 // Network failure with offline error; should return cache entry above +
1021 // flag signalling stale data.
1022 transaction
.return_code
= net::ERR_NAME_NOT_RESOLVED
;
1023 AddMockTransaction(&transaction
);
1025 MockHttpRequest
request(transaction
);
1026 net::TestCompletionCallback callback
;
1027 scoped_ptr
<net::HttpTransaction
> trans
;
1028 int rv
= cache
.http_cache()->CreateTransaction(
1029 net::DEFAULT_PRIORITY
, &trans
, NULL
);
1030 EXPECT_EQ(net::OK
, rv
);
1031 ASSERT_TRUE(trans
.get());
1032 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
1033 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
1035 const net::HttpResponseInfo
* response_info
= trans
->GetResponseInfo();
1036 ASSERT_TRUE(response_info
);
1037 EXPECT_TRUE(response_info
->server_data_unavailable
);
1038 EXPECT_TRUE(response_info
->was_cached
);
1039 EXPECT_FALSE(response_info
->network_accessed
);
1040 ReadAndVerifyTransaction(trans
.get(), transaction
);
1041 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
1043 RemoveMockTransaction(&transaction
);
1046 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on
1047 // non-offline failure.
1048 TEST(HttpCache
, SimpleGET_CacheOverride_NonOffline
) {
1049 MockHttpCache cache
;
1052 MockTransaction
transaction(kSimpleGET_Transaction
);
1053 transaction
.load_flags
|= net::LOAD_FROM_CACHE_IF_OFFLINE
;
1054 transaction
.response_headers
= "Cache-Control: no-cache\n";
1056 AddMockTransaction(&transaction
);
1057 RunTransactionTest(cache
.http_cache(), transaction
);
1058 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
1059 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1060 RemoveMockTransaction(&transaction
);
1062 // Network failure with non-offline error; should fail with that error.
1063 transaction
.return_code
= net::ERR_PROXY_CONNECTION_FAILED
;
1064 AddMockTransaction(&transaction
);
1066 net::HttpResponseInfo response_info2
;
1067 RunTransactionTestWithResponseInfo(cache
.http_cache(), transaction
,
1070 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
1071 EXPECT_FALSE(response_info2
.server_data_unavailable
);
1073 RemoveMockTransaction(&transaction
);
1076 // Confirm if we have an empty cache, a read is marked as network verified.
1077 TEST(HttpCache
, SimpleGET_NetworkAccessed_Network
) {
1078 MockHttpCache cache
;
1080 // write to the cache
1081 net::HttpResponseInfo response_info
;
1082 RunTransactionTestWithResponseInfo(cache
.http_cache(), kSimpleGET_Transaction
,
1085 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
1086 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1087 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1088 EXPECT_TRUE(response_info
.network_accessed
);
1091 // Confirm if we have a fresh entry in cache, it isn't marked as
1092 // network verified.
1093 TEST(HttpCache
, SimpleGET_NetworkAccessed_Cache
) {
1094 MockHttpCache cache
;
1097 MockTransaction
transaction(kSimpleGET_Transaction
);
1099 RunTransactionTest(cache
.http_cache(), transaction
);
1100 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
1101 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1103 // Re-run transaction; make sure we don't mark the network as accessed.
1104 net::HttpResponseInfo response_info
;
1105 RunTransactionTestWithResponseInfo(cache
.http_cache(), transaction
,
1108 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
1109 EXPECT_FALSE(response_info
.server_data_unavailable
);
1110 EXPECT_FALSE(response_info
.network_accessed
);
1113 TEST(HttpCache
, SimpleGET_LoadBypassCache
) {
1114 MockHttpCache cache
;
1116 // Write to the cache.
1117 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
1119 // Force this transaction to write to the cache again.
1120 MockTransaction
transaction(kSimpleGET_Transaction
);
1121 transaction
.load_flags
|= net::LOAD_BYPASS_CACHE
;
1123 net::CapturingBoundNetLog log
;
1125 // This prevents a number of write events from being logged.
1126 log
.SetLogLevel(net::NetLog::LOG_BASIC
);
1127 net::LoadTimingInfo load_timing_info
;
1129 // Write to the cache.
1130 RunTransactionTestAndGetTiming(cache
.http_cache(), transaction
, log
.bound(),
1133 // Check that the NetLog was filled as expected.
1134 net::CapturingNetLog::CapturedEntryList entries
;
1135 log
.GetEntries(&entries
);
1137 EXPECT_EQ(8u, entries
.size());
1138 EXPECT_TRUE(net::LogContainsBeginEvent(
1139 entries
, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND
));
1140 EXPECT_TRUE(net::LogContainsEndEvent(
1141 entries
, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND
));
1142 EXPECT_TRUE(net::LogContainsBeginEvent(
1143 entries
, 2, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY
));
1144 EXPECT_TRUE(net::LogContainsEndEvent(
1145 entries
, 3, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY
));
1146 EXPECT_TRUE(net::LogContainsBeginEvent(
1147 entries
, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
));
1148 EXPECT_TRUE(net::LogContainsEndEvent(
1149 entries
, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
));
1150 EXPECT_TRUE(net::LogContainsBeginEvent(
1151 entries
, 6, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
));
1152 EXPECT_TRUE(net::LogContainsEndEvent(
1153 entries
, 7, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
));
1155 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
1156 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1157 EXPECT_EQ(2, cache
.disk_cache()->create_count());
1158 TestLoadTimingNetworkRequest(load_timing_info
);
1161 TEST(HttpCache
, SimpleGET_LoadBypassCache_Implicit
) {
1162 MockHttpCache cache
;
1164 // write to the cache
1165 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
1167 // force this transaction to write to the cache again
1168 MockTransaction
transaction(kSimpleGET_Transaction
);
1169 transaction
.request_headers
= "pragma: no-cache\r\n";
1171 RunTransactionTest(cache
.http_cache(), transaction
);
1173 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
1174 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1175 EXPECT_EQ(2, cache
.disk_cache()->create_count());
1178 TEST(HttpCache
, SimpleGET_LoadBypassCache_Implicit2
) {
1179 MockHttpCache cache
;
1181 // write to the cache
1182 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
1184 // force this transaction to write to the cache again
1185 MockTransaction
transaction(kSimpleGET_Transaction
);
1186 transaction
.request_headers
= "cache-control: no-cache\r\n";
1188 RunTransactionTest(cache
.http_cache(), transaction
);
1190 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
1191 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1192 EXPECT_EQ(2, cache
.disk_cache()->create_count());
1195 TEST(HttpCache
, SimpleGET_LoadValidateCache
) {
1196 MockHttpCache cache
;
1198 // Write to the cache.
1199 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
1201 // Read from the cache.
1202 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
1204 // Force this transaction to validate the cache.
1205 MockTransaction
transaction(kSimpleGET_Transaction
);
1206 transaction
.load_flags
|= net::LOAD_VALIDATE_CACHE
;
1208 net::HttpResponseInfo response_info
;
1209 net::CapturingBoundNetLog log
;
1210 net::LoadTimingInfo load_timing_info
;
1211 RunTransactionTestWithResponseInfoAndGetTiming(
1212 cache
.http_cache(), transaction
, &response_info
, log
.bound(),
1215 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
1216 EXPECT_EQ(1, cache
.disk_cache()->open_count());
1217 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1218 EXPECT_TRUE(response_info
.network_accessed
);
1219 TestLoadTimingNetworkRequest(load_timing_info
);
1222 TEST(HttpCache
, SimpleGET_LoadValidateCache_Implicit
) {
1223 MockHttpCache cache
;
1225 // write to the cache
1226 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
1228 // read from the cache
1229 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
1231 // force this transaction to validate the cache
1232 MockTransaction
transaction(kSimpleGET_Transaction
);
1233 transaction
.request_headers
= "cache-control: max-age=0\r\n";
1235 RunTransactionTest(cache
.http_cache(), transaction
);
1237 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
1238 EXPECT_EQ(1, cache
.disk_cache()->open_count());
1239 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1242 static void PreserveRequestHeaders_Handler(
1243 const net::HttpRequestInfo
* request
,
1244 std::string
* response_status
,
1245 std::string
* response_headers
,
1246 std::string
* response_data
) {
1247 EXPECT_TRUE(request
->extra_headers
.HasHeader(kExtraHeaderKey
));
1250 // Tests that we don't remove extra headers for simple requests.
1251 TEST(HttpCache
, SimpleGET_PreserveRequestHeaders
) {
1252 MockHttpCache cache
;
1254 MockTransaction
transaction(kSimpleGET_Transaction
);
1255 transaction
.handler
= PreserveRequestHeaders_Handler
;
1256 transaction
.request_headers
= EXTRA_HEADER
;
1257 transaction
.response_headers
= "Cache-Control: max-age=0\n";
1258 AddMockTransaction(&transaction
);
1260 // Write, then revalidate the entry.
1261 RunTransactionTest(cache
.http_cache(), transaction
);
1262 RunTransactionTest(cache
.http_cache(), transaction
);
1264 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
1265 EXPECT_EQ(1, cache
.disk_cache()->open_count());
1266 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1267 RemoveMockTransaction(&transaction
);
1270 // Tests that we don't remove extra headers for conditionalized requests.
1271 TEST(HttpCache
, ConditionalizedGET_PreserveRequestHeaders
) {
1272 MockHttpCache cache
;
1274 // Write to the cache.
1275 RunTransactionTest(cache
.http_cache(), kETagGET_Transaction
);
1277 MockTransaction
transaction(kETagGET_Transaction
);
1278 transaction
.handler
= PreserveRequestHeaders_Handler
;
1279 transaction
.request_headers
= "If-None-Match: \"foopy\"\r\n"
1281 AddMockTransaction(&transaction
);
1283 RunTransactionTest(cache
.http_cache(), transaction
);
1285 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
1286 EXPECT_EQ(1, cache
.disk_cache()->open_count());
1287 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1288 RemoveMockTransaction(&transaction
);
1291 TEST(HttpCache
, SimpleGET_ManyReaders
) {
1292 MockHttpCache cache
;
1294 MockHttpRequest
request(kSimpleGET_Transaction
);
1296 std::vector
<Context
*> context_list
;
1297 const int kNumTransactions
= 5;
1299 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1300 context_list
.push_back(new Context());
1301 Context
* c
= context_list
[i
];
1303 c
->result
= cache
.http_cache()->CreateTransaction(
1304 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
1305 EXPECT_EQ(net::OK
, c
->result
);
1306 EXPECT_EQ(net::LOAD_STATE_IDLE
, c
->trans
->GetLoadState());
1308 c
->result
= c
->trans
->Start(
1309 &request
, c
->callback
.callback(), net::BoundNetLog());
1312 // All requests are waiting for the active entry.
1313 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1314 Context
* c
= context_list
[i
];
1315 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE
, c
->trans
->GetLoadState());
1318 // Allow all requests to move from the Create queue to the active entry.
1319 base::MessageLoop::current()->RunUntilIdle();
1321 // The first request should be a writer at this point, and the subsequent
1322 // requests should be pending.
1324 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
1325 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1326 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1328 // All requests depend on the writer, and the writer is between Start and
1330 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1331 Context
* c
= context_list
[i
];
1332 EXPECT_EQ(net::LOAD_STATE_IDLE
, c
->trans
->GetLoadState());
1335 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1336 Context
* c
= context_list
[i
];
1337 if (c
->result
== net::ERR_IO_PENDING
)
1338 c
->result
= c
->callback
.WaitForResult();
1339 ReadAndVerifyTransaction(c
->trans
.get(), kSimpleGET_Transaction
);
1342 // We should not have had to re-open the disk entry
1344 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
1345 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1346 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1348 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1349 Context
* c
= context_list
[i
];
1354 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769.
1355 // If cancelling a request is racing with another request for the same resource
1356 // finishing, we have to make sure that we remove both transactions from the
1358 TEST(HttpCache
, SimpleGET_RacingReaders
) {
1359 MockHttpCache cache
;
1361 MockHttpRequest
request(kSimpleGET_Transaction
);
1362 MockHttpRequest
reader_request(kSimpleGET_Transaction
);
1363 reader_request
.load_flags
= net::LOAD_ONLY_FROM_CACHE
;
1365 std::vector
<Context
*> context_list
;
1366 const int kNumTransactions
= 5;
1368 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1369 context_list
.push_back(new Context());
1370 Context
* c
= context_list
[i
];
1372 c
->result
= cache
.http_cache()->CreateTransaction(
1373 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
1374 EXPECT_EQ(net::OK
, c
->result
);
1376 MockHttpRequest
* this_request
= &request
;
1377 if (i
== 1 || i
== 2)
1378 this_request
= &reader_request
;
1380 c
->result
= c
->trans
->Start(
1381 this_request
, c
->callback
.callback(), net::BoundNetLog());
1384 // Allow all requests to move from the Create queue to the active entry.
1385 base::MessageLoop::current()->RunUntilIdle();
1387 // The first request should be a writer at this point, and the subsequent
1388 // requests should be pending.
1390 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
1391 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1392 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1394 Context
* c
= context_list
[0];
1395 ASSERT_EQ(net::ERR_IO_PENDING
, c
->result
);
1396 c
->result
= c
->callback
.WaitForResult();
1397 ReadAndVerifyTransaction(c
->trans
.get(), kSimpleGET_Transaction
);
1399 // Now we have 2 active readers and two queued transactions.
1401 EXPECT_EQ(net::LOAD_STATE_IDLE
,
1402 context_list
[2]->trans
->GetLoadState());
1403 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE
,
1404 context_list
[3]->trans
->GetLoadState());
1406 c
= context_list
[1];
1407 ASSERT_EQ(net::ERR_IO_PENDING
, c
->result
);
1408 c
->result
= c
->callback
.WaitForResult();
1409 if (c
->result
== net::OK
)
1410 ReadAndVerifyTransaction(c
->trans
.get(), kSimpleGET_Transaction
);
1412 // At this point we have one reader, two pending transactions and a task on
1413 // the queue to move to the next transaction. Now we cancel the request that
1414 // is the current reader, and expect the queued task to be able to start the
1417 c
= context_list
[2];
1420 for (int i
= 3; i
< kNumTransactions
; ++i
) {
1421 Context
* c
= context_list
[i
];
1422 if (c
->result
== net::ERR_IO_PENDING
)
1423 c
->result
= c
->callback
.WaitForResult();
1424 if (c
->result
== net::OK
)
1425 ReadAndVerifyTransaction(c
->trans
.get(), kSimpleGET_Transaction
);
1428 // We should not have had to re-open the disk entry.
1430 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
1431 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1432 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1434 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1435 Context
* c
= context_list
[i
];
1440 // Tests that we can doom an entry with pending transactions and delete one of
1441 // the pending transactions before the first one completes.
1442 // See http://code.google.com/p/chromium/issues/detail?id=25588
1443 TEST(HttpCache
, SimpleGET_DoomWithPending
) {
1444 // We need simultaneous doomed / not_doomed entries so let's use a real cache.
1445 MockHttpCache
cache(net::HttpCache::DefaultBackend::InMemory(1024 * 1024));
1447 MockHttpRequest
request(kSimpleGET_Transaction
);
1448 MockHttpRequest
writer_request(kSimpleGET_Transaction
);
1449 writer_request
.load_flags
= net::LOAD_BYPASS_CACHE
;
1451 ScopedVector
<Context
> context_list
;
1452 const int kNumTransactions
= 4;
1454 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1455 context_list
.push_back(new Context());
1456 Context
* c
= context_list
[i
];
1458 c
->result
= cache
.http_cache()->CreateTransaction(
1459 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
1460 EXPECT_EQ(net::OK
, c
->result
);
1462 MockHttpRequest
* this_request
= &request
;
1464 this_request
= &writer_request
;
1466 c
->result
= c
->trans
->Start(
1467 this_request
, c
->callback
.callback(), net::BoundNetLog());
1470 // The first request should be a writer at this point, and the two subsequent
1471 // requests should be pending. The last request doomed the first entry.
1473 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
1475 // Cancel the first queued transaction.
1476 delete context_list
[1];
1477 context_list
.get()[1] = NULL
;
1479 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1482 Context
* c
= context_list
[i
];
1483 ASSERT_EQ(net::ERR_IO_PENDING
, c
->result
);
1484 c
->result
= c
->callback
.WaitForResult();
1485 ReadAndVerifyTransaction(c
->trans
.get(), kSimpleGET_Transaction
);
1489 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4731.
1490 // We may attempt to delete an entry synchronously with the act of adding a new
1491 // transaction to said entry.
1492 TEST(HttpCache
, FastNoStoreGET_DoneWithPending
) {
1493 MockHttpCache cache
;
1495 // The headers will be served right from the call to Start() the request.
1496 MockHttpRequest
request(kFastNoStoreGET_Transaction
);
1497 FastTransactionServer request_handler
;
1498 AddMockTransaction(&kFastNoStoreGET_Transaction
);
1500 std::vector
<Context
*> context_list
;
1501 const int kNumTransactions
= 3;
1503 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1504 context_list
.push_back(new Context());
1505 Context
* c
= context_list
[i
];
1507 c
->result
= cache
.http_cache()->CreateTransaction(
1508 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
1509 EXPECT_EQ(net::OK
, c
->result
);
1511 c
->result
= c
->trans
->Start(
1512 &request
, c
->callback
.callback(), net::BoundNetLog());
1515 // Allow all requests to move from the Create queue to the active entry.
1516 base::MessageLoop::current()->RunUntilIdle();
1518 // The first request should be a writer at this point, and the subsequent
1519 // requests should be pending.
1521 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
1522 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1523 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1525 // Now, make sure that the second request asks for the entry not to be stored.
1526 request_handler
.set_no_store(true);
1528 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1529 Context
* c
= context_list
[i
];
1530 if (c
->result
== net::ERR_IO_PENDING
)
1531 c
->result
= c
->callback
.WaitForResult();
1532 ReadAndVerifyTransaction(c
->trans
.get(), kFastNoStoreGET_Transaction
);
1536 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
1537 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1538 EXPECT_EQ(2, cache
.disk_cache()->create_count());
1540 RemoveMockTransaction(&kFastNoStoreGET_Transaction
);
1543 TEST(HttpCache
, SimpleGET_ManyWriters_CancelFirst
) {
1544 MockHttpCache cache
;
1546 MockHttpRequest
request(kSimpleGET_Transaction
);
1548 std::vector
<Context
*> context_list
;
1549 const int kNumTransactions
= 2;
1551 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1552 context_list
.push_back(new Context());
1553 Context
* c
= context_list
[i
];
1555 c
->result
= cache
.http_cache()->CreateTransaction(
1556 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
1557 EXPECT_EQ(net::OK
, c
->result
);
1559 c
->result
= c
->trans
->Start(
1560 &request
, c
->callback
.callback(), net::BoundNetLog());
1563 // Allow all requests to move from the Create queue to the active entry.
1564 base::MessageLoop::current()->RunUntilIdle();
1566 // The first request should be a writer at this point, and the subsequent
1567 // requests should be pending.
1569 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
1570 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1571 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1573 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1574 Context
* c
= context_list
[i
];
1575 if (c
->result
== net::ERR_IO_PENDING
)
1576 c
->result
= c
->callback
.WaitForResult();
1577 // Destroy only the first transaction.
1580 context_list
[i
] = NULL
;
1584 // Complete the rest of the transactions.
1585 for (int i
= 1; i
< kNumTransactions
; ++i
) {
1586 Context
* c
= context_list
[i
];
1587 ReadAndVerifyTransaction(c
->trans
.get(), kSimpleGET_Transaction
);
1590 // We should have had to re-open the disk entry.
1592 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
1593 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1594 EXPECT_EQ(2, cache
.disk_cache()->create_count());
1596 for (int i
= 1; i
< kNumTransactions
; ++i
) {
1597 Context
* c
= context_list
[i
];
1602 // Tests that we can cancel requests that are queued waiting to open the disk
1604 TEST(HttpCache
, SimpleGET_ManyWriters_CancelCreate
) {
1605 MockHttpCache cache
;
1607 MockHttpRequest
request(kSimpleGET_Transaction
);
1609 std::vector
<Context
*> context_list
;
1610 const int kNumTransactions
= 5;
1612 for (int i
= 0; i
< kNumTransactions
; i
++) {
1613 context_list
.push_back(new Context());
1614 Context
* c
= context_list
[i
];
1616 c
->result
= cache
.http_cache()->CreateTransaction(
1617 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
1618 EXPECT_EQ(net::OK
, c
->result
);
1620 c
->result
= c
->trans
->Start(
1621 &request
, c
->callback
.callback(), net::BoundNetLog());
1624 // The first request should be creating the disk cache entry and the others
1625 // should be pending.
1627 EXPECT_EQ(0, cache
.network_layer()->transaction_count());
1628 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1629 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1631 // Cancel a request from the pending queue.
1632 delete context_list
[3];
1633 context_list
[3] = NULL
;
1635 // Cancel the request that is creating the entry. This will force the pending
1636 // operations to restart.
1637 delete context_list
[0];
1638 context_list
[0] = NULL
;
1640 // Complete the rest of the transactions.
1641 for (int i
= 1; i
< kNumTransactions
; i
++) {
1642 Context
* c
= context_list
[i
];
1644 c
->result
= c
->callback
.GetResult(c
->result
);
1645 ReadAndVerifyTransaction(c
->trans
.get(), kSimpleGET_Transaction
);
1649 // We should have had to re-create the disk entry.
1651 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
1652 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1653 EXPECT_EQ(2, cache
.disk_cache()->create_count());
1655 for (int i
= 1; i
< kNumTransactions
; ++i
) {
1656 delete context_list
[i
];
1660 // Tests that we can cancel a single request to open a disk cache entry.
1661 TEST(HttpCache
, SimpleGET_CancelCreate
) {
1662 MockHttpCache cache
;
1664 MockHttpRequest
request(kSimpleGET_Transaction
);
1666 Context
* c
= new Context();
1668 c
->result
= cache
.http_cache()->CreateTransaction(
1669 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
1670 EXPECT_EQ(net::OK
, c
->result
);
1672 c
->result
= c
->trans
->Start(
1673 &request
, c
->callback
.callback(), net::BoundNetLog());
1674 EXPECT_EQ(net::ERR_IO_PENDING
, c
->result
);
1676 // Release the reference that the mock disk cache keeps for this entry, so
1677 // that we test that the http cache handles the cancellation correctly.
1678 cache
.disk_cache()->ReleaseAll();
1681 base::MessageLoop::current()->RunUntilIdle();
1682 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1685 // Tests that we delete/create entries even if multiple requests are queued.
1686 TEST(HttpCache
, SimpleGET_ManyWriters_BypassCache
) {
1687 MockHttpCache cache
;
1689 MockHttpRequest
request(kSimpleGET_Transaction
);
1690 request
.load_flags
= net::LOAD_BYPASS_CACHE
;
1692 std::vector
<Context
*> context_list
;
1693 const int kNumTransactions
= 5;
1695 for (int i
= 0; i
< kNumTransactions
; i
++) {
1696 context_list
.push_back(new Context());
1697 Context
* c
= context_list
[i
];
1699 c
->result
= cache
.http_cache()->CreateTransaction(
1700 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
1701 EXPECT_EQ(net::OK
, c
->result
);
1703 c
->result
= c
->trans
->Start(
1704 &request
, c
->callback
.callback(), net::BoundNetLog());
1707 // The first request should be deleting the disk cache entry and the others
1708 // should be pending.
1710 EXPECT_EQ(0, cache
.network_layer()->transaction_count());
1711 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1712 EXPECT_EQ(0, cache
.disk_cache()->create_count());
1714 // Complete the transactions.
1715 for (int i
= 0; i
< kNumTransactions
; i
++) {
1716 Context
* c
= context_list
[i
];
1717 c
->result
= c
->callback
.GetResult(c
->result
);
1718 ReadAndVerifyTransaction(c
->trans
.get(), kSimpleGET_Transaction
);
1721 // We should have had to re-create the disk entry multiple times.
1723 EXPECT_EQ(5, cache
.network_layer()->transaction_count());
1724 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1725 EXPECT_EQ(5, cache
.disk_cache()->create_count());
1727 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1728 delete context_list
[i
];
1732 TEST(HttpCache
, SimpleGET_AbandonedCacheRead
) {
1733 MockHttpCache cache
;
1735 // write to the cache
1736 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
1738 MockHttpRequest
request(kSimpleGET_Transaction
);
1739 net::TestCompletionCallback callback
;
1741 scoped_ptr
<net::HttpTransaction
> trans
;
1742 int rv
= cache
.http_cache()->CreateTransaction(
1743 net::DEFAULT_PRIORITY
, &trans
, NULL
);
1744 EXPECT_EQ(net::OK
, rv
);
1745 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
1746 if (rv
== net::ERR_IO_PENDING
)
1747 rv
= callback
.WaitForResult();
1748 ASSERT_EQ(net::OK
, rv
);
1750 scoped_refptr
<net::IOBuffer
> buf(new net::IOBuffer(256));
1751 rv
= trans
->Read(buf
.get(), 256, callback
.callback());
1752 EXPECT_EQ(net::ERR_IO_PENDING
, rv
);
1754 // Test that destroying the transaction while it is reading from the cache
1758 // Make sure we pump any pending events, which should include a call to
1759 // HttpCache::Transaction::OnCacheReadCompleted.
1760 base::MessageLoop::current()->RunUntilIdle();
1763 // Tests that we can delete the HttpCache and deal with queued transactions
1764 // ("waiting for the backend" as opposed to Active or Doomed entries).
1765 TEST(HttpCache
, SimpleGET_ManyWriters_DeleteCache
) {
1766 scoped_ptr
<MockHttpCache
> cache(new MockHttpCache(
1767 new MockBackendNoCbFactory()));
1769 MockHttpRequest
request(kSimpleGET_Transaction
);
1771 std::vector
<Context
*> context_list
;
1772 const int kNumTransactions
= 5;
1774 for (int i
= 0; i
< kNumTransactions
; i
++) {
1775 context_list
.push_back(new Context());
1776 Context
* c
= context_list
[i
];
1778 c
->result
= cache
->http_cache()->CreateTransaction(
1779 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
1780 EXPECT_EQ(net::OK
, c
->result
);
1782 c
->result
= c
->trans
->Start(
1783 &request
, c
->callback
.callback(), net::BoundNetLog());
1786 // The first request should be creating the disk cache entry and the others
1787 // should be pending.
1789 EXPECT_EQ(0, cache
->network_layer()->transaction_count());
1790 EXPECT_EQ(0, cache
->disk_cache()->open_count());
1791 EXPECT_EQ(0, cache
->disk_cache()->create_count());
1795 // There is not much to do with the transactions at this point... they are
1796 // waiting for a callback that will not fire.
1797 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1798 delete context_list
[i
];
1802 // Tests that we queue requests when initializing the backend.
1803 TEST(HttpCache
, SimpleGET_WaitForBackend
) {
1804 MockBlockingBackendFactory
* factory
= new MockBlockingBackendFactory();
1805 MockHttpCache
cache(factory
);
1807 MockHttpRequest
request0(kSimpleGET_Transaction
);
1808 MockHttpRequest
request1(kTypicalGET_Transaction
);
1809 MockHttpRequest
request2(kETagGET_Transaction
);
1811 std::vector
<Context
*> context_list
;
1812 const int kNumTransactions
= 3;
1814 for (int i
= 0; i
< kNumTransactions
; i
++) {
1815 context_list
.push_back(new Context());
1816 Context
* c
= context_list
[i
];
1818 c
->result
= cache
.http_cache()->CreateTransaction(
1819 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
1820 EXPECT_EQ(net::OK
, c
->result
);
1823 context_list
[0]->result
= context_list
[0]->trans
->Start(
1824 &request0
, context_list
[0]->callback
.callback(), net::BoundNetLog());
1825 context_list
[1]->result
= context_list
[1]->trans
->Start(
1826 &request1
, context_list
[1]->callback
.callback(), net::BoundNetLog());
1827 context_list
[2]->result
= context_list
[2]->trans
->Start(
1828 &request2
, context_list
[2]->callback
.callback(), net::BoundNetLog());
1830 // Just to make sure that everything is still pending.
1831 base::MessageLoop::current()->RunUntilIdle();
1833 // The first request should be creating the disk cache.
1834 EXPECT_FALSE(context_list
[0]->callback
.have_result());
1836 factory
->FinishCreation();
1838 base::MessageLoop::current()->RunUntilIdle();
1839 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
1840 EXPECT_EQ(3, cache
.disk_cache()->create_count());
1842 for (int i
= 0; i
< kNumTransactions
; ++i
) {
1843 EXPECT_TRUE(context_list
[i
]->callback
.have_result());
1844 delete context_list
[i
];
1848 // Tests that we can cancel requests that are queued waiting for the backend
1849 // to be initialized.
1850 TEST(HttpCache
, SimpleGET_WaitForBackend_CancelCreate
) {
1851 MockBlockingBackendFactory
* factory
= new MockBlockingBackendFactory();
1852 MockHttpCache
cache(factory
);
1854 MockHttpRequest
request0(kSimpleGET_Transaction
);
1855 MockHttpRequest
request1(kTypicalGET_Transaction
);
1856 MockHttpRequest
request2(kETagGET_Transaction
);
1858 std::vector
<Context
*> context_list
;
1859 const int kNumTransactions
= 3;
1861 for (int i
= 0; i
< kNumTransactions
; i
++) {
1862 context_list
.push_back(new Context());
1863 Context
* c
= context_list
[i
];
1865 c
->result
= cache
.http_cache()->CreateTransaction(
1866 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
1867 EXPECT_EQ(net::OK
, c
->result
);
1870 context_list
[0]->result
= context_list
[0]->trans
->Start(
1871 &request0
, context_list
[0]->callback
.callback(), net::BoundNetLog());
1872 context_list
[1]->result
= context_list
[1]->trans
->Start(
1873 &request1
, context_list
[1]->callback
.callback(), net::BoundNetLog());
1874 context_list
[2]->result
= context_list
[2]->trans
->Start(
1875 &request2
, context_list
[2]->callback
.callback(), net::BoundNetLog());
1877 // Just to make sure that everything is still pending.
1878 base::MessageLoop::current()->RunUntilIdle();
1880 // The first request should be creating the disk cache.
1881 EXPECT_FALSE(context_list
[0]->callback
.have_result());
1883 // Cancel a request from the pending queue.
1884 delete context_list
[1];
1885 context_list
[1] = NULL
;
1887 // Cancel the request that is creating the entry.
1888 delete context_list
[0];
1889 context_list
[0] = NULL
;
1891 // Complete the last transaction.
1892 factory
->FinishCreation();
1894 context_list
[2]->result
=
1895 context_list
[2]->callback
.GetResult(context_list
[2]->result
);
1896 ReadAndVerifyTransaction(context_list
[2]->trans
.get(), kETagGET_Transaction
);
1898 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
1899 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1901 delete context_list
[2];
1904 // Tests that we can delete the cache while creating the backend.
1905 TEST(HttpCache
, DeleteCacheWaitingForBackend
) {
1906 MockBlockingBackendFactory
* factory
= new MockBlockingBackendFactory();
1907 scoped_ptr
<MockHttpCache
> cache(new MockHttpCache(factory
));
1909 MockHttpRequest
request(kSimpleGET_Transaction
);
1911 scoped_ptr
<Context
> c(new Context());
1912 c
->result
= cache
->http_cache()->CreateTransaction(
1913 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
1914 EXPECT_EQ(net::OK
, c
->result
);
1916 c
->trans
->Start(&request
, c
->callback
.callback(), net::BoundNetLog());
1918 // Just to make sure that everything is still pending.
1919 base::MessageLoop::current()->RunUntilIdle();
1921 // The request should be creating the disk cache.
1922 EXPECT_FALSE(c
->callback
.have_result());
1924 // We cannot call FinishCreation because the factory itself will go away with
1925 // the cache, so grab the callback and attempt to use it.
1926 net::CompletionCallback callback
= factory
->callback();
1927 scoped_ptr
<disk_cache::Backend
>* backend
= factory
->backend();
1930 base::MessageLoop::current()->RunUntilIdle();
1933 callback
.Run(net::ERR_ABORTED
);
1936 // Tests that we can delete the cache while creating the backend, from within
1937 // one of the callbacks.
1938 TEST(HttpCache
, DeleteCacheWaitingForBackend2
) {
1939 MockBlockingBackendFactory
* factory
= new MockBlockingBackendFactory();
1940 MockHttpCache
* cache
= new MockHttpCache(factory
);
1942 DeleteCacheCompletionCallback
cb(cache
);
1943 disk_cache::Backend
* backend
;
1944 int rv
= cache
->http_cache()->GetBackend(&backend
, cb
.callback());
1945 EXPECT_EQ(net::ERR_IO_PENDING
, rv
);
1947 // Now let's queue a regular transaction
1948 MockHttpRequest
request(kSimpleGET_Transaction
);
1950 scoped_ptr
<Context
> c(new Context());
1951 c
->result
= cache
->http_cache()->CreateTransaction(
1952 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
1953 EXPECT_EQ(net::OK
, c
->result
);
1955 c
->trans
->Start(&request
, c
->callback
.callback(), net::BoundNetLog());
1957 // And another direct backend request.
1958 net::TestCompletionCallback cb2
;
1959 rv
= cache
->http_cache()->GetBackend(&backend
, cb2
.callback());
1960 EXPECT_EQ(net::ERR_IO_PENDING
, rv
);
1962 // Just to make sure that everything is still pending.
1963 base::MessageLoop::current()->RunUntilIdle();
1965 // The request should be queued.
1966 EXPECT_FALSE(c
->callback
.have_result());
1968 // Generate the callback.
1969 factory
->FinishCreation();
1970 rv
= cb
.WaitForResult();
1972 // The cache should be gone by now.
1973 base::MessageLoop::current()->RunUntilIdle();
1974 EXPECT_EQ(net::OK
, c
->callback
.GetResult(c
->result
));
1975 EXPECT_FALSE(cb2
.have_result());
1978 TEST(HttpCache
, TypicalGET_ConditionalRequest
) {
1979 MockHttpCache cache
;
1981 // write to the cache
1982 RunTransactionTest(cache
.http_cache(), kTypicalGET_Transaction
);
1984 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
1985 EXPECT_EQ(0, cache
.disk_cache()->open_count());
1986 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1988 // Get the same URL again, but this time we expect it to result
1989 // in a conditional request.
1990 net::CapturingBoundNetLog log
;
1991 net::LoadTimingInfo load_timing_info
;
1992 RunTransactionTestAndGetTiming(cache
.http_cache(), kTypicalGET_Transaction
,
1993 log
.bound(), &load_timing_info
);
1995 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
1996 EXPECT_EQ(1, cache
.disk_cache()->open_count());
1997 EXPECT_EQ(1, cache
.disk_cache()->create_count());
1998 TestLoadTimingNetworkRequest(load_timing_info
);
2001 static void ETagGet_ConditionalRequest_Handler(
2002 const net::HttpRequestInfo
* request
,
2003 std::string
* response_status
,
2004 std::string
* response_headers
,
2005 std::string
* response_data
) {
2007 request
->extra_headers
.HasHeader(net::HttpRequestHeaders::kIfNoneMatch
));
2008 response_status
->assign("HTTP/1.1 304 Not Modified");
2009 response_headers
->assign(kETagGET_Transaction
.response_headers
);
2010 response_data
->clear();
2013 TEST(HttpCache
, ETagGET_ConditionalRequest_304
) {
2014 MockHttpCache cache
;
2016 ScopedMockTransaction
transaction(kETagGET_Transaction
);
2018 // write to the cache
2019 RunTransactionTest(cache
.http_cache(), transaction
);
2021 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2022 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2023 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2025 // Get the same URL again, but this time we expect it to result
2026 // in a conditional request.
2027 transaction
.load_flags
= net::LOAD_VALIDATE_CACHE
;
2028 transaction
.handler
= ETagGet_ConditionalRequest_Handler
;
2029 net::CapturingBoundNetLog log
;
2030 net::LoadTimingInfo load_timing_info
;
2031 RunTransactionTestAndGetTiming(cache
.http_cache(), transaction
, log
.bound(),
2034 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
2035 EXPECT_EQ(1, cache
.disk_cache()->open_count());
2036 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2037 TestLoadTimingNetworkRequest(load_timing_info
);
2040 class RevalidationServer
{
2042 RevalidationServer() {
2043 s_etag_used_
= false;
2044 s_last_modified_used_
= false;
2047 bool EtagUsed() { return s_etag_used_
; }
2048 bool LastModifiedUsed() { return s_last_modified_used_
; }
2050 static void Handler(const net::HttpRequestInfo
* request
,
2051 std::string
* response_status
,
2052 std::string
* response_headers
,
2053 std::string
* response_data
);
2056 static bool s_etag_used_
;
2057 static bool s_last_modified_used_
;
2059 bool RevalidationServer::s_etag_used_
= false;
2060 bool RevalidationServer::s_last_modified_used_
= false;
2062 void RevalidationServer::Handler(const net::HttpRequestInfo
* request
,
2063 std::string
* response_status
,
2064 std::string
* response_headers
,
2065 std::string
* response_data
) {
2066 if (request
->extra_headers
.HasHeader(net::HttpRequestHeaders::kIfNoneMatch
))
2067 s_etag_used_
= true;
2069 if (request
->extra_headers
.HasHeader(
2070 net::HttpRequestHeaders::kIfModifiedSince
)) {
2071 s_last_modified_used_
= true;
2074 if (s_etag_used_
|| s_last_modified_used_
) {
2075 response_status
->assign("HTTP/1.1 304 Not Modified");
2076 response_headers
->assign(kTypicalGET_Transaction
.response_headers
);
2077 response_data
->clear();
2079 response_status
->assign(kTypicalGET_Transaction
.status
);
2080 response_headers
->assign(kTypicalGET_Transaction
.response_headers
);
2081 response_data
->assign(kTypicalGET_Transaction
.data
);
2085 // Tests revalidation after a vary match.
2086 TEST(HttpCache
, SimpleGET_LoadValidateCache_VaryMatch
) {
2087 MockHttpCache cache
;
2089 // Write to the cache.
2090 MockTransaction
transaction(kTypicalGET_Transaction
);
2091 transaction
.request_headers
= "Foo: bar\r\n";
2092 transaction
.response_headers
=
2093 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
2094 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
2096 "Cache-Control: max-age=0\n"
2098 AddMockTransaction(&transaction
);
2099 RunTransactionTest(cache
.http_cache(), transaction
);
2101 // Read from the cache.
2102 RevalidationServer server
;
2103 transaction
.handler
= server
.Handler
;
2104 net::CapturingBoundNetLog log
;
2105 net::LoadTimingInfo load_timing_info
;
2106 RunTransactionTestAndGetTiming(cache
.http_cache(), transaction
, log
.bound(),
2109 EXPECT_TRUE(server
.EtagUsed());
2110 EXPECT_TRUE(server
.LastModifiedUsed());
2111 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
2112 EXPECT_EQ(1, cache
.disk_cache()->open_count());
2113 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2114 TestLoadTimingNetworkRequest(load_timing_info
);
2115 RemoveMockTransaction(&transaction
);
2118 // Tests revalidation after a vary mismatch if etag is present.
2119 TEST(HttpCache
, SimpleGET_LoadValidateCache_VaryMismatch
) {
2120 MockHttpCache cache
;
2122 // Write to the cache.
2123 MockTransaction
transaction(kTypicalGET_Transaction
);
2124 transaction
.request_headers
= "Foo: bar\r\n";
2125 transaction
.response_headers
=
2126 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
2127 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
2129 "Cache-Control: max-age=0\n"
2131 AddMockTransaction(&transaction
);
2132 RunTransactionTest(cache
.http_cache(), transaction
);
2134 // Read from the cache and revalidate the entry.
2135 RevalidationServer server
;
2136 transaction
.handler
= server
.Handler
;
2137 transaction
.request_headers
= "Foo: none\r\n";
2138 net::CapturingBoundNetLog log
;
2139 net::LoadTimingInfo load_timing_info
;
2140 RunTransactionTestAndGetTiming(cache
.http_cache(), transaction
, log
.bound(),
2143 EXPECT_TRUE(server
.EtagUsed());
2144 EXPECT_FALSE(server
.LastModifiedUsed());
2145 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
2146 EXPECT_EQ(1, cache
.disk_cache()->open_count());
2147 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2148 TestLoadTimingNetworkRequest(load_timing_info
);
2149 RemoveMockTransaction(&transaction
);
2152 // Tests lack of revalidation after a vary mismatch and no etag.
2153 TEST(HttpCache
, SimpleGET_LoadDontValidateCache_VaryMismatch
) {
2154 MockHttpCache cache
;
2156 // Write to the cache.
2157 MockTransaction
transaction(kTypicalGET_Transaction
);
2158 transaction
.request_headers
= "Foo: bar\r\n";
2159 transaction
.response_headers
=
2160 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
2161 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
2162 "Cache-Control: max-age=0\n"
2164 AddMockTransaction(&transaction
);
2165 RunTransactionTest(cache
.http_cache(), transaction
);
2167 // Read from the cache and don't revalidate the entry.
2168 RevalidationServer server
;
2169 transaction
.handler
= server
.Handler
;
2170 transaction
.request_headers
= "Foo: none\r\n";
2171 net::CapturingBoundNetLog log
;
2172 net::LoadTimingInfo load_timing_info
;
2173 RunTransactionTestAndGetTiming(cache
.http_cache(), transaction
, log
.bound(),
2176 EXPECT_FALSE(server
.EtagUsed());
2177 EXPECT_FALSE(server
.LastModifiedUsed());
2178 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
2179 EXPECT_EQ(1, cache
.disk_cache()->open_count());
2180 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2181 TestLoadTimingNetworkRequest(load_timing_info
);
2182 RemoveMockTransaction(&transaction
);
2185 static void ETagGet_UnconditionalRequest_Handler(
2186 const net::HttpRequestInfo
* request
,
2187 std::string
* response_status
,
2188 std::string
* response_headers
,
2189 std::string
* response_data
) {
2191 request
->extra_headers
.HasHeader(net::HttpRequestHeaders::kIfNoneMatch
));
2194 TEST(HttpCache
, ETagGET_Http10
) {
2195 MockHttpCache cache
;
2197 ScopedMockTransaction
transaction(kETagGET_Transaction
);
2198 transaction
.status
= "HTTP/1.0 200 OK";
2200 // Write to the cache.
2201 RunTransactionTest(cache
.http_cache(), transaction
);
2203 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2204 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2205 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2207 // Get the same URL again, without generating a conditional request.
2208 transaction
.load_flags
= net::LOAD_VALIDATE_CACHE
;
2209 transaction
.handler
= ETagGet_UnconditionalRequest_Handler
;
2210 RunTransactionTest(cache
.http_cache(), transaction
);
2212 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
2213 EXPECT_EQ(1, cache
.disk_cache()->open_count());
2214 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2217 TEST(HttpCache
, ETagGET_Http10_Range
) {
2218 MockHttpCache cache
;
2220 ScopedMockTransaction
transaction(kETagGET_Transaction
);
2221 transaction
.status
= "HTTP/1.0 200 OK";
2223 // Write to the cache.
2224 RunTransactionTest(cache
.http_cache(), transaction
);
2226 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2227 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2228 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2230 // Get the same URL again, but use a byte range request.
2231 transaction
.load_flags
= net::LOAD_VALIDATE_CACHE
;
2232 transaction
.handler
= ETagGet_UnconditionalRequest_Handler
;
2233 transaction
.request_headers
= "Range: bytes = 5-\r\n";
2234 RunTransactionTest(cache
.http_cache(), transaction
);
2236 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
2237 EXPECT_EQ(1, cache
.disk_cache()->open_count());
2238 EXPECT_EQ(2, cache
.disk_cache()->create_count());
2241 static void ETagGet_ConditionalRequest_NoStore_Handler(
2242 const net::HttpRequestInfo
* request
,
2243 std::string
* response_status
,
2244 std::string
* response_headers
,
2245 std::string
* response_data
) {
2247 request
->extra_headers
.HasHeader(net::HttpRequestHeaders::kIfNoneMatch
));
2248 response_status
->assign("HTTP/1.1 304 Not Modified");
2249 response_headers
->assign("Cache-Control: no-store\n");
2250 response_data
->clear();
2253 TEST(HttpCache
, ETagGET_ConditionalRequest_304_NoStore
) {
2254 MockHttpCache cache
;
2256 ScopedMockTransaction
transaction(kETagGET_Transaction
);
2258 // Write to the cache.
2259 RunTransactionTest(cache
.http_cache(), transaction
);
2261 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2262 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2263 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2265 // Get the same URL again, but this time we expect it to result
2266 // in a conditional request.
2267 transaction
.load_flags
= net::LOAD_VALIDATE_CACHE
;
2268 transaction
.handler
= ETagGet_ConditionalRequest_NoStore_Handler
;
2269 RunTransactionTest(cache
.http_cache(), transaction
);
2271 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
2272 EXPECT_EQ(1, cache
.disk_cache()->open_count());
2273 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2275 ScopedMockTransaction
transaction2(kETagGET_Transaction
);
2277 // Write to the cache again. This should create a new entry.
2278 RunTransactionTest(cache
.http_cache(), transaction2
);
2280 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
2281 EXPECT_EQ(1, cache
.disk_cache()->open_count());
2282 EXPECT_EQ(2, cache
.disk_cache()->create_count());
2285 // Helper that does 4 requests using HttpCache:
2287 // (1) loads |kUrl| -- expects |net_response_1| to be returned.
2288 // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned.
2289 // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to
2291 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be
2293 static void ConditionalizedRequestUpdatesCacheHelper(
2294 const Response
& net_response_1
,
2295 const Response
& net_response_2
,
2296 const Response
& cached_response_2
,
2297 const char* extra_request_headers
) {
2298 MockHttpCache cache
;
2300 // The URL we will be requesting.
2301 const char* kUrl
= "http://foobar.com/main.css";
2303 // Junk network response.
2304 static const Response kUnexpectedResponse
= {
2305 "HTTP/1.1 500 Unexpected",
2306 "Server: unexpected_header",
2310 // We will control the network layer's responses for |kUrl| using
2311 // |mock_network_response|.
2312 MockTransaction mock_network_response
= { 0 };
2313 mock_network_response
.url
= kUrl
;
2314 AddMockTransaction(&mock_network_response
);
2316 // Request |kUrl| for the first time. It should hit the network and
2317 // receive |kNetResponse1|, which it saves into the HTTP cache.
2319 MockTransaction request
= { 0 };
2321 request
.method
= "GET";
2322 request
.request_headers
= "";
2324 net_response_1
.AssignTo(&mock_network_response
); // Network mock.
2325 net_response_1
.AssignTo(&request
); // Expected result.
2327 std::string response_headers
;
2328 RunTransactionTestWithResponse(
2329 cache
.http_cache(), request
, &response_headers
);
2331 EXPECT_EQ(net_response_1
.status_and_headers(), response_headers
);
2332 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2333 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2334 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2336 // Request |kUrl| a second time. Now |kNetResponse1| it is in the HTTP
2337 // cache, so we don't hit the network.
2339 request
.load_flags
= net::LOAD_ONLY_FROM_CACHE
;
2341 kUnexpectedResponse
.AssignTo(&mock_network_response
); // Network mock.
2342 net_response_1
.AssignTo(&request
); // Expected result.
2344 RunTransactionTestWithResponse(
2345 cache
.http_cache(), request
, &response_headers
);
2347 EXPECT_EQ(net_response_1
.status_and_headers(), response_headers
);
2348 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2349 EXPECT_EQ(1, cache
.disk_cache()->open_count());
2350 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2352 // Request |kUrl| yet again, but this time give the request an
2353 // "If-Modified-Since" header. This will cause the request to re-hit the
2354 // network. However now the network response is going to be
2355 // different -- this simulates a change made to the CSS file.
2357 request
.request_headers
= extra_request_headers
;
2358 request
.load_flags
= net::LOAD_NORMAL
;
2360 net_response_2
.AssignTo(&mock_network_response
); // Network mock.
2361 net_response_2
.AssignTo(&request
); // Expected result.
2363 RunTransactionTestWithResponse(
2364 cache
.http_cache(), request
, &response_headers
);
2366 EXPECT_EQ(net_response_2
.status_and_headers(), response_headers
);
2367 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
2368 EXPECT_EQ(1, cache
.disk_cache()->open_count());
2369 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2371 // Finally, request |kUrl| again. This request should be serviced from
2372 // the cache. Moreover, the value in the cache should be |kNetResponse2|
2373 // and NOT |kNetResponse1|. The previous step should have replaced the
2374 // value in the cache with the modified response.
2376 request
.request_headers
= "";
2377 request
.load_flags
= net::LOAD_ONLY_FROM_CACHE
;
2379 kUnexpectedResponse
.AssignTo(&mock_network_response
); // Network mock.
2380 cached_response_2
.AssignTo(&request
); // Expected result.
2382 RunTransactionTestWithResponse(
2383 cache
.http_cache(), request
, &response_headers
);
2385 EXPECT_EQ(cached_response_2
.status_and_headers(), response_headers
);
2386 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
2387 EXPECT_EQ(2, cache
.disk_cache()->open_count());
2388 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2390 RemoveMockTransaction(&mock_network_response
);
2393 // Check that when an "if-modified-since" header is attached
2394 // to the request, the result still updates the cached entry.
2395 TEST(HttpCache
, ConditionalizedRequestUpdatesCache1
) {
2396 // First network response for |kUrl|.
2397 static const Response kNetResponse1
= {
2399 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2400 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2404 // Second network response for |kUrl|.
2405 static const Response kNetResponse2
= {
2407 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2408 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
2412 const char* extra_headers
=
2413 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
2415 ConditionalizedRequestUpdatesCacheHelper(
2416 kNetResponse1
, kNetResponse2
, kNetResponse2
, extra_headers
);
2419 // Check that when an "if-none-match" header is attached
2420 // to the request, the result updates the cached entry.
2421 TEST(HttpCache
, ConditionalizedRequestUpdatesCache2
) {
2422 // First network response for |kUrl|.
2423 static const Response kNetResponse1
= {
2425 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2427 "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire.
2431 // Second network response for |kUrl|.
2432 static const Response kNetResponse2
= {
2434 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2436 "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire.
2440 const char* extra_headers
= "If-None-Match: \"ETAG1\"\r\n";
2442 ConditionalizedRequestUpdatesCacheHelper(
2443 kNetResponse1
, kNetResponse2
, kNetResponse2
, extra_headers
);
2446 // Check that when an "if-modified-since" header is attached
2447 // to a request, the 304 (not modified result) result updates the cached
2448 // headers, and the 304 response is returned rather than the cached response.
2449 TEST(HttpCache
, ConditionalizedRequestUpdatesCache3
) {
2450 // First network response for |kUrl|.
2451 static const Response kNetResponse1
= {
2453 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2455 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2459 // Second network response for |kUrl|.
2460 static const Response kNetResponse2
= {
2461 "HTTP/1.1 304 Not Modified",
2462 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2464 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2468 static const Response kCachedResponse2
= {
2470 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2472 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2476 const char* extra_headers
=
2477 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
2479 ConditionalizedRequestUpdatesCacheHelper(
2480 kNetResponse1
, kNetResponse2
, kCachedResponse2
, extra_headers
);
2483 // Test that when doing an externally conditionalized if-modified-since
2484 // and there is no corresponding cache entry, a new cache entry is NOT
2485 // created (304 response).
2486 TEST(HttpCache
, ConditionalizedRequestUpdatesCache4
) {
2487 MockHttpCache cache
;
2489 const char* kUrl
= "http://foobar.com/main.css";
2491 static const Response kNetResponse
= {
2492 "HTTP/1.1 304 Not Modified",
2493 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2494 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2498 const char* kExtraRequestHeaders
=
2499 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
2501 // We will control the network layer's responses for |kUrl| using
2502 // |mock_network_response|.
2503 MockTransaction mock_network_response
= { 0 };
2504 mock_network_response
.url
= kUrl
;
2505 AddMockTransaction(&mock_network_response
);
2507 MockTransaction request
= { 0 };
2509 request
.method
= "GET";
2510 request
.request_headers
= kExtraRequestHeaders
;
2512 kNetResponse
.AssignTo(&mock_network_response
); // Network mock.
2513 kNetResponse
.AssignTo(&request
); // Expected result.
2515 std::string response_headers
;
2516 RunTransactionTestWithResponse(
2517 cache
.http_cache(), request
, &response_headers
);
2519 EXPECT_EQ(kNetResponse
.status_and_headers(), response_headers
);
2520 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2521 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2522 EXPECT_EQ(0, cache
.disk_cache()->create_count());
2524 RemoveMockTransaction(&mock_network_response
);
2527 // Test that when doing an externally conditionalized if-modified-since
2528 // and there is no corresponding cache entry, a new cache entry is NOT
2529 // created (200 response).
2530 TEST(HttpCache
, ConditionalizedRequestUpdatesCache5
) {
2531 MockHttpCache cache
;
2533 const char* kUrl
= "http://foobar.com/main.css";
2535 static const Response kNetResponse
= {
2537 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2538 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2542 const char* kExtraRequestHeaders
=
2543 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
2545 // We will control the network layer's responses for |kUrl| using
2546 // |mock_network_response|.
2547 MockTransaction mock_network_response
= { 0 };
2548 mock_network_response
.url
= kUrl
;
2549 AddMockTransaction(&mock_network_response
);
2551 MockTransaction request
= { 0 };
2553 request
.method
= "GET";
2554 request
.request_headers
= kExtraRequestHeaders
;
2556 kNetResponse
.AssignTo(&mock_network_response
); // Network mock.
2557 kNetResponse
.AssignTo(&request
); // Expected result.
2559 std::string response_headers
;
2560 RunTransactionTestWithResponse(
2561 cache
.http_cache(), request
, &response_headers
);
2563 EXPECT_EQ(kNetResponse
.status_and_headers(), response_headers
);
2564 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2565 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2566 EXPECT_EQ(0, cache
.disk_cache()->create_count());
2568 RemoveMockTransaction(&mock_network_response
);
2571 // Test that when doing an externally conditionalized if-modified-since
2572 // if the date does not match the cache entry's last-modified date,
2573 // then we do NOT use the response (304) to update the cache.
2574 // (the if-modified-since date is 2 days AFTER the cache's modification date).
2575 TEST(HttpCache
, ConditionalizedRequestUpdatesCache6
) {
2576 static const Response kNetResponse1
= {
2578 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2580 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2584 // Second network response for |kUrl|.
2585 static const Response kNetResponse2
= {
2586 "HTTP/1.1 304 Not Modified",
2587 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2589 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2593 // This is two days in the future from the original response's last-modified
2595 const char* kExtraRequestHeaders
=
2596 "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n";
2598 ConditionalizedRequestUpdatesCacheHelper(
2599 kNetResponse1
, kNetResponse2
, kNetResponse1
, kExtraRequestHeaders
);
2602 // Test that when doing an externally conditionalized if-none-match
2603 // if the etag does not match the cache entry's etag, then we do not use the
2604 // response (304) to update the cache.
2605 TEST(HttpCache
, ConditionalizedRequestUpdatesCache7
) {
2606 static const Response kNetResponse1
= {
2608 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2610 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2614 // Second network response for |kUrl|.
2615 static const Response kNetResponse2
= {
2616 "HTTP/1.1 304 Not Modified",
2617 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2619 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2623 // Different etag from original response.
2624 const char* kExtraRequestHeaders
= "If-None-Match: \"Foo2\"\r\n";
2626 ConditionalizedRequestUpdatesCacheHelper(
2627 kNetResponse1
, kNetResponse2
, kNetResponse1
, kExtraRequestHeaders
);
2630 // Test that doing an externally conditionalized request with both if-none-match
2631 // and if-modified-since updates the cache.
2632 TEST(HttpCache
, ConditionalizedRequestUpdatesCache8
) {
2633 static const Response kNetResponse1
= {
2635 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2637 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2641 // Second network response for |kUrl|.
2642 static const Response kNetResponse2
= {
2644 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2646 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
2650 const char* kExtraRequestHeaders
=
2651 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"
2652 "If-None-Match: \"Foo1\"\r\n";
2654 ConditionalizedRequestUpdatesCacheHelper(
2655 kNetResponse1
, kNetResponse2
, kNetResponse2
, kExtraRequestHeaders
);
2658 // Test that doing an externally conditionalized request with both if-none-match
2659 // and if-modified-since does not update the cache with only one match.
2660 TEST(HttpCache
, ConditionalizedRequestUpdatesCache9
) {
2661 static const Response kNetResponse1
= {
2663 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2665 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2669 // Second network response for |kUrl|.
2670 static const Response kNetResponse2
= {
2672 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2674 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
2678 // The etag doesn't match what we have stored.
2679 const char* kExtraRequestHeaders
=
2680 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"
2681 "If-None-Match: \"Foo2\"\r\n";
2683 ConditionalizedRequestUpdatesCacheHelper(
2684 kNetResponse1
, kNetResponse2
, kNetResponse1
, kExtraRequestHeaders
);
2687 // Test that doing an externally conditionalized request with both if-none-match
2688 // and if-modified-since does not update the cache with only one match.
2689 TEST(HttpCache
, ConditionalizedRequestUpdatesCache10
) {
2690 static const Response kNetResponse1
= {
2692 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2694 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2698 // Second network response for |kUrl|.
2699 static const Response kNetResponse2
= {
2701 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2703 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
2707 // The modification date doesn't match what we have stored.
2708 const char* kExtraRequestHeaders
=
2709 "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n"
2710 "If-None-Match: \"Foo1\"\r\n";
2712 ConditionalizedRequestUpdatesCacheHelper(
2713 kNetResponse1
, kNetResponse2
, kNetResponse1
, kExtraRequestHeaders
);
2716 TEST(HttpCache
, UrlContainingHash
) {
2717 MockHttpCache cache
;
2719 // Do a typical GET request -- should write an entry into our cache.
2720 MockTransaction
trans(kTypicalGET_Transaction
);
2721 RunTransactionTest(cache
.http_cache(), trans
);
2723 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2724 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2725 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2727 // Request the same URL, but this time with a reference section (hash).
2728 // Since the cache key strips the hash sections, this should be a cache hit.
2729 std::string url_with_hash
= std::string(trans
.url
) + "#multiple#hashes";
2730 trans
.url
= url_with_hash
.c_str();
2731 trans
.load_flags
= net::LOAD_ONLY_FROM_CACHE
;
2733 RunTransactionTest(cache
.http_cache(), trans
);
2735 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2736 EXPECT_EQ(1, cache
.disk_cache()->open_count());
2737 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2740 // Tests that we skip the cache for POST requests that do not have an upload
2742 TEST(HttpCache
, SimplePOST_SkipsCache
) {
2743 MockHttpCache cache
;
2745 RunTransactionTest(cache
.http_cache(), kSimplePOST_Transaction
);
2747 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2748 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2749 EXPECT_EQ(0, cache
.disk_cache()->create_count());
2752 TEST(HttpCache
, SimplePOST_LoadOnlyFromCache_Miss
) {
2753 MockHttpCache cache
;
2755 MockTransaction
transaction(kSimplePOST_Transaction
);
2756 transaction
.load_flags
|= net::LOAD_ONLY_FROM_CACHE
;
2758 MockHttpRequest
request(transaction
);
2759 net::TestCompletionCallback callback
;
2761 scoped_ptr
<net::HttpTransaction
> trans
;
2762 int rv
= cache
.http_cache()->CreateTransaction(
2763 net::DEFAULT_PRIORITY
, &trans
, NULL
);
2764 EXPECT_EQ(net::OK
, rv
);
2765 ASSERT_TRUE(trans
.get());
2767 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
2768 ASSERT_EQ(net::ERR_CACHE_MISS
, callback
.GetResult(rv
));
2772 EXPECT_EQ(0, cache
.network_layer()->transaction_count());
2773 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2774 EXPECT_EQ(0, cache
.disk_cache()->create_count());
2777 TEST(HttpCache
, SimplePOST_LoadOnlyFromCache_Hit
) {
2778 MockHttpCache cache
;
2780 // Test that we hit the cache for POST requests.
2782 MockTransaction
transaction(kSimplePOST_Transaction
);
2784 const int64 kUploadId
= 1; // Just a dummy value.
2786 ScopedVector
<net::UploadElementReader
> element_readers
;
2787 element_readers
.push_back(new net::UploadBytesElementReader("hello", 5));
2788 net::UploadDataStream
upload_data_stream(element_readers
.Pass(), kUploadId
);
2789 MockHttpRequest
request(transaction
);
2790 request
.upload_data_stream
= &upload_data_stream
;
2792 // Populate the cache.
2793 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, request
, NULL
);
2795 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2796 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2797 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2800 request
.load_flags
|= net::LOAD_ONLY_FROM_CACHE
;
2801 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, request
, NULL
);
2803 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2804 EXPECT_EQ(1, cache
.disk_cache()->open_count());
2805 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2808 // Test that we don't hit the cache for POST requests if there is a byte range.
2809 TEST(HttpCache
, SimplePOST_WithRanges
) {
2810 MockHttpCache cache
;
2812 MockTransaction
transaction(kSimplePOST_Transaction
);
2813 transaction
.request_headers
= "Range: bytes = 0-4\r\n";
2815 const int64 kUploadId
= 1; // Just a dummy value.
2817 ScopedVector
<net::UploadElementReader
> element_readers
;
2818 element_readers
.push_back(new net::UploadBytesElementReader("hello", 5));
2819 net::UploadDataStream
upload_data_stream(element_readers
.Pass(), kUploadId
);
2821 MockHttpRequest
request(transaction
);
2822 request
.upload_data_stream
= &upload_data_stream
;
2824 // Attempt to populate the cache.
2825 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, request
, NULL
);
2827 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2828 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2829 EXPECT_EQ(0, cache
.disk_cache()->create_count());
2832 // Tests that a POST is cached separately from a previously cached GET.
2833 TEST(HttpCache
, SimplePOST_SeparateCache
) {
2834 MockHttpCache cache
;
2836 ScopedVector
<net::UploadElementReader
> element_readers
;
2837 element_readers
.push_back(new net::UploadBytesElementReader("hello", 5));
2838 net::UploadDataStream
upload_data_stream(element_readers
.Pass(), 1);
2840 MockTransaction
transaction(kSimplePOST_Transaction
);
2841 MockHttpRequest
req1(transaction
);
2842 req1
.upload_data_stream
= &upload_data_stream
;
2844 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
2846 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2847 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2848 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2850 transaction
.method
= "GET";
2851 MockHttpRequest
req2(transaction
);
2853 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req2
, NULL
);
2855 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
2856 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2857 EXPECT_EQ(2, cache
.disk_cache()->create_count());
2860 // Tests that a successful POST invalidates a previously cached GET.
2861 TEST(HttpCache
, SimplePOST_Invalidate_205
) {
2862 MockHttpCache cache
;
2864 MockTransaction
transaction(kSimpleGET_Transaction
);
2865 AddMockTransaction(&transaction
);
2866 MockHttpRequest
req1(transaction
);
2868 // Attempt to populate the cache.
2869 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
2871 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2872 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2873 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2875 ScopedVector
<net::UploadElementReader
> element_readers
;
2876 element_readers
.push_back(new net::UploadBytesElementReader("hello", 5));
2877 net::UploadDataStream
upload_data_stream(element_readers
.Pass(), 1);
2879 transaction
.method
= "POST";
2880 transaction
.status
= "HTTP/1.1 205 No Content";
2881 MockHttpRequest
req2(transaction
);
2882 req2
.upload_data_stream
= &upload_data_stream
;
2884 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req2
, NULL
);
2886 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
2887 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2888 EXPECT_EQ(2, cache
.disk_cache()->create_count());
2890 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
2892 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
2893 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2894 EXPECT_EQ(3, cache
.disk_cache()->create_count());
2895 RemoveMockTransaction(&transaction
);
2898 // Tests that a successful POST invalidates a previously cached GET, even when
2899 // there is no upload identifier.
2900 TEST(HttpCache
, SimplePOST_NoUploadId_Invalidate_205
) {
2901 MockHttpCache cache
;
2903 MockTransaction
transaction(kSimpleGET_Transaction
);
2904 AddMockTransaction(&transaction
);
2905 MockHttpRequest
req1(transaction
);
2907 // Attempt to populate the cache.
2908 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
2910 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2911 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2912 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2914 ScopedVector
<net::UploadElementReader
> element_readers
;
2915 element_readers
.push_back(new net::UploadBytesElementReader("hello", 5));
2916 net::UploadDataStream
upload_data_stream(element_readers
.Pass(), 0);
2918 transaction
.method
= "POST";
2919 transaction
.status
= "HTTP/1.1 205 No Content";
2920 MockHttpRequest
req2(transaction
);
2921 req2
.upload_data_stream
= &upload_data_stream
;
2923 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req2
, NULL
);
2925 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
2926 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2927 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2929 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
2931 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
2932 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2933 EXPECT_EQ(2, cache
.disk_cache()->create_count());
2934 RemoveMockTransaction(&transaction
);
2937 // Tests that processing a POST before creating the backend doesn't crash.
2938 TEST(HttpCache
, SimplePOST_NoUploadId_NoBackend
) {
2939 // This will initialize a cache object with NULL backend.
2940 MockBlockingBackendFactory
* factory
= new MockBlockingBackendFactory();
2941 factory
->set_fail(true);
2942 factory
->FinishCreation();
2943 MockHttpCache
cache(factory
);
2945 ScopedVector
<net::UploadElementReader
> element_readers
;
2946 element_readers
.push_back(new net::UploadBytesElementReader("hello", 5));
2947 net::UploadDataStream
upload_data_stream(element_readers
.Pass(), 0);
2949 MockTransaction
transaction(kSimplePOST_Transaction
);
2950 AddMockTransaction(&transaction
);
2951 MockHttpRequest
req(transaction
);
2952 req
.upload_data_stream
= &upload_data_stream
;
2954 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req
, NULL
);
2956 RemoveMockTransaction(&transaction
);
2959 // Tests that we don't invalidate entries as a result of a failed POST.
2960 TEST(HttpCache
, SimplePOST_DontInvalidate_100
) {
2961 MockHttpCache cache
;
2963 MockTransaction
transaction(kSimpleGET_Transaction
);
2964 AddMockTransaction(&transaction
);
2965 MockHttpRequest
req1(transaction
);
2967 // Attempt to populate the cache.
2968 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
2970 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
2971 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2972 EXPECT_EQ(1, cache
.disk_cache()->create_count());
2974 ScopedVector
<net::UploadElementReader
> element_readers
;
2975 element_readers
.push_back(new net::UploadBytesElementReader("hello", 5));
2976 net::UploadDataStream
upload_data_stream(element_readers
.Pass(), 1);
2978 transaction
.method
= "POST";
2979 transaction
.status
= "HTTP/1.1 100 Continue";
2980 MockHttpRequest
req2(transaction
);
2981 req2
.upload_data_stream
= &upload_data_stream
;
2983 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req2
, NULL
);
2985 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
2986 EXPECT_EQ(0, cache
.disk_cache()->open_count());
2987 EXPECT_EQ(2, cache
.disk_cache()->create_count());
2989 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
2991 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
2992 EXPECT_EQ(1, cache
.disk_cache()->open_count());
2993 EXPECT_EQ(2, cache
.disk_cache()->create_count());
2994 RemoveMockTransaction(&transaction
);
2997 // Tests that we do not cache the response of a PUT.
2998 TEST(HttpCache
, SimplePUT_Miss
) {
2999 MockHttpCache cache
;
3001 MockTransaction
transaction(kSimplePOST_Transaction
);
3002 transaction
.method
= "PUT";
3004 ScopedVector
<net::UploadElementReader
> element_readers
;
3005 element_readers
.push_back(new net::UploadBytesElementReader("hello", 5));
3006 net::UploadDataStream
upload_data_stream(element_readers
.Pass(), 0);
3008 MockHttpRequest
request(transaction
);
3009 request
.upload_data_stream
= &upload_data_stream
;
3011 // Attempt to populate the cache.
3012 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, request
, NULL
);
3014 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3015 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3016 EXPECT_EQ(0, cache
.disk_cache()->create_count());
3019 // Tests that we invalidate entries as a result of a PUT.
3020 TEST(HttpCache
, SimplePUT_Invalidate
) {
3021 MockHttpCache cache
;
3023 MockTransaction
transaction(kSimpleGET_Transaction
);
3024 MockHttpRequest
req1(transaction
);
3026 // Attempt to populate the cache.
3027 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
3029 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3030 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3031 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3033 ScopedVector
<net::UploadElementReader
> element_readers
;
3034 element_readers
.push_back(new net::UploadBytesElementReader("hello", 5));
3035 net::UploadDataStream
upload_data_stream(element_readers
.Pass(), 0);
3037 transaction
.method
= "PUT";
3038 MockHttpRequest
req2(transaction
);
3039 req2
.upload_data_stream
= &upload_data_stream
;
3041 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req2
, NULL
);
3043 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3044 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3045 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3047 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
3049 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
3050 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3051 EXPECT_EQ(2, cache
.disk_cache()->create_count());
3054 // Tests that we invalidate entries as a result of a PUT.
3055 TEST(HttpCache
, SimplePUT_Invalidate_305
) {
3056 MockHttpCache cache
;
3058 MockTransaction
transaction(kSimpleGET_Transaction
);
3059 AddMockTransaction(&transaction
);
3060 MockHttpRequest
req1(transaction
);
3062 // Attempt to populate the cache.
3063 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
3065 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3066 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3067 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3069 ScopedVector
<net::UploadElementReader
> element_readers
;
3070 element_readers
.push_back(new net::UploadBytesElementReader("hello", 5));
3071 net::UploadDataStream
upload_data_stream(element_readers
.Pass(), 0);
3073 transaction
.method
= "PUT";
3074 transaction
.status
= "HTTP/1.1 305 Use Proxy";
3075 MockHttpRequest
req2(transaction
);
3076 req2
.upload_data_stream
= &upload_data_stream
;
3078 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req2
, NULL
);
3080 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3081 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3082 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3084 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
3086 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
3087 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3088 EXPECT_EQ(2, cache
.disk_cache()->create_count());
3089 RemoveMockTransaction(&transaction
);
3092 // Tests that we don't invalidate entries as a result of a failed PUT.
3093 TEST(HttpCache
, SimplePUT_DontInvalidate_404
) {
3094 MockHttpCache cache
;
3096 MockTransaction
transaction(kSimpleGET_Transaction
);
3097 AddMockTransaction(&transaction
);
3098 MockHttpRequest
req1(transaction
);
3100 // Attempt to populate the cache.
3101 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
3103 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3104 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3105 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3107 ScopedVector
<net::UploadElementReader
> element_readers
;
3108 element_readers
.push_back(new net::UploadBytesElementReader("hello", 5));
3109 net::UploadDataStream
upload_data_stream(element_readers
.Pass(), 0);
3111 transaction
.method
= "PUT";
3112 transaction
.status
= "HTTP/1.1 404 Not Found";
3113 MockHttpRequest
req2(transaction
);
3114 req2
.upload_data_stream
= &upload_data_stream
;
3116 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req2
, NULL
);
3118 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3119 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3120 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3122 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
3124 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3125 EXPECT_EQ(2, cache
.disk_cache()->open_count());
3126 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3127 RemoveMockTransaction(&transaction
);
3130 // Tests that we do not cache the response of a DELETE.
3131 TEST(HttpCache
, SimpleDELETE_Miss
) {
3132 MockHttpCache cache
;
3134 MockTransaction
transaction(kSimplePOST_Transaction
);
3135 transaction
.method
= "DELETE";
3137 ScopedVector
<net::UploadElementReader
> element_readers
;
3138 element_readers
.push_back(new net::UploadBytesElementReader("hello", 5));
3139 net::UploadDataStream
upload_data_stream(element_readers
.Pass(), 0);
3141 MockHttpRequest
request(transaction
);
3142 request
.upload_data_stream
= &upload_data_stream
;
3144 // Attempt to populate the cache.
3145 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, request
, NULL
);
3147 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3148 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3149 EXPECT_EQ(0, cache
.disk_cache()->create_count());
3152 // Tests that we invalidate entries as a result of a DELETE.
3153 TEST(HttpCache
, SimpleDELETE_Invalidate
) {
3154 MockHttpCache cache
;
3156 MockTransaction
transaction(kSimpleGET_Transaction
);
3157 MockHttpRequest
req1(transaction
);
3159 // Attempt to populate the cache.
3160 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
3162 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3163 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3164 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3166 ScopedVector
<net::UploadElementReader
> element_readers
;
3167 element_readers
.push_back(new net::UploadBytesElementReader("hello", 5));
3168 net::UploadDataStream
upload_data_stream(element_readers
.Pass(), 0);
3170 transaction
.method
= "DELETE";
3171 MockHttpRequest
req2(transaction
);
3172 req2
.upload_data_stream
= &upload_data_stream
;
3174 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req2
, NULL
);
3176 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3177 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3178 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3180 RunTransactionTestWithRequest(cache
.http_cache(), transaction
, req1
, NULL
);
3182 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
3183 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3184 EXPECT_EQ(2, cache
.disk_cache()->create_count());
3187 // Tests that we invalidate entries as a result of a DELETE.
3188 TEST(HttpCache
, SimpleDELETE_Invalidate_301
) {
3189 MockHttpCache cache
;
3191 MockTransaction
transaction(kSimpleGET_Transaction
);
3192 AddMockTransaction(&transaction
);
3194 // Attempt to populate the cache.
3195 RunTransactionTest(cache
.http_cache(), transaction
);
3197 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3198 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3199 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3201 transaction
.method
= "DELETE";
3202 transaction
.status
= "HTTP/1.1 301 Moved Permanently ";
3204 RunTransactionTest(cache
.http_cache(), transaction
);
3206 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3207 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3208 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3210 transaction
.method
= "GET";
3211 RunTransactionTest(cache
.http_cache(), transaction
);
3213 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
3214 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3215 EXPECT_EQ(2, cache
.disk_cache()->create_count());
3216 RemoveMockTransaction(&transaction
);
3219 // Tests that we don't invalidate entries as a result of a failed DELETE.
3220 TEST(HttpCache
, SimpleDELETE_DontInvalidate_416
) {
3221 MockHttpCache cache
;
3223 MockTransaction
transaction(kSimpleGET_Transaction
);
3224 AddMockTransaction(&transaction
);
3226 // Attempt to populate the cache.
3227 RunTransactionTest(cache
.http_cache(), transaction
);
3229 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3230 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3231 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3233 transaction
.method
= "DELETE";
3234 transaction
.status
= "HTTP/1.1 416 Requested Range Not Satisfiable";
3236 RunTransactionTest(cache
.http_cache(), transaction
);
3238 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3239 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3240 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3242 transaction
.method
= "GET";
3243 transaction
.status
= "HTTP/1.1 200 OK";
3244 RunTransactionTest(cache
.http_cache(), transaction
);
3246 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3247 EXPECT_EQ(2, cache
.disk_cache()->open_count());
3248 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3249 RemoveMockTransaction(&transaction
);
3252 // Tests that we don't invalidate entries after a failed network transaction.
3253 TEST(HttpCache
, SimpleGET_DontInvalidateOnFailure
) {
3254 MockHttpCache cache
;
3256 // Populate the cache.
3257 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
3258 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3260 // Fail the network request.
3261 MockTransaction
transaction(kSimpleGET_Transaction
);
3262 transaction
.return_code
= net::ERR_FAILED
;
3263 transaction
.load_flags
|= net::LOAD_VALIDATE_CACHE
;
3265 AddMockTransaction(&transaction
);
3266 RunTransactionTest(cache
.http_cache(), transaction
);
3267 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3268 RemoveMockTransaction(&transaction
);
3270 transaction
.load_flags
= net::LOAD_ONLY_FROM_CACHE
;
3271 transaction
.return_code
= net::OK
;
3272 AddMockTransaction(&transaction
);
3273 RunTransactionTest(cache
.http_cache(), transaction
);
3275 // Make sure the transaction didn't reach the network.
3276 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3277 RemoveMockTransaction(&transaction
);
3280 TEST(HttpCache
, RangeGET_SkipsCache
) {
3281 MockHttpCache cache
;
3283 // Test that we skip the cache for range GET requests. Eventually, we will
3284 // want to cache these, but we'll still have cases where skipping the cache
3285 // makes sense, so we want to make sure that it works properly.
3287 RunTransactionTest(cache
.http_cache(), kRangeGET_Transaction
);
3289 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3290 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3291 EXPECT_EQ(0, cache
.disk_cache()->create_count());
3293 MockTransaction
transaction(kSimpleGET_Transaction
);
3294 transaction
.request_headers
= "If-None-Match: foo\r\n";
3295 RunTransactionTest(cache
.http_cache(), transaction
);
3297 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3298 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3299 EXPECT_EQ(0, cache
.disk_cache()->create_count());
3301 transaction
.request_headers
=
3302 "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n";
3303 RunTransactionTest(cache
.http_cache(), transaction
);
3305 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
3306 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3307 EXPECT_EQ(0, cache
.disk_cache()->create_count());
3310 // Test that we skip the cache for range requests that include a validation
3312 TEST(HttpCache
, RangeGET_SkipsCache2
) {
3313 MockHttpCache cache
;
3315 MockTransaction
transaction(kRangeGET_Transaction
);
3316 transaction
.request_headers
= "If-None-Match: foo\r\n"
3318 "Range: bytes = 40-49\r\n";
3319 RunTransactionTest(cache
.http_cache(), transaction
);
3321 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3322 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3323 EXPECT_EQ(0, cache
.disk_cache()->create_count());
3325 transaction
.request_headers
=
3326 "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n"
3328 "Range: bytes = 40-49\r\n";
3329 RunTransactionTest(cache
.http_cache(), transaction
);
3331 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3332 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3333 EXPECT_EQ(0, cache
.disk_cache()->create_count());
3335 transaction
.request_headers
= "If-Range: bla\r\n"
3337 "Range: bytes = 40-49\r\n";
3338 RunTransactionTest(cache
.http_cache(), transaction
);
3340 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
3341 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3342 EXPECT_EQ(0, cache
.disk_cache()->create_count());
3345 // Tests that receiving 206 for a regular request is handled correctly.
3346 TEST(HttpCache
, GET_Crazy206
) {
3347 MockHttpCache cache
;
3349 // Write to the cache.
3350 MockTransaction
transaction(kRangeGET_TransactionOK
);
3351 AddMockTransaction(&transaction
);
3352 transaction
.request_headers
= EXTRA_HEADER
;
3353 transaction
.handler
= NULL
;
3354 RunTransactionTest(cache
.http_cache(), transaction
);
3356 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3357 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3358 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3360 // This should read again from the net.
3361 RunTransactionTest(cache
.http_cache(), transaction
);
3363 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3364 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3365 EXPECT_EQ(2, cache
.disk_cache()->create_count());
3366 RemoveMockTransaction(&transaction
);
3369 // Tests that we don't cache partial responses that can't be validated.
3370 TEST(HttpCache
, RangeGET_NoStrongValidators
) {
3371 MockHttpCache cache
;
3372 std::string headers
;
3374 // Attempt to write to the cache (40-49).
3375 MockTransaction
transaction(kRangeGET_TransactionOK
);
3376 AddMockTransaction(&transaction
);
3377 transaction
.response_headers
= "Content-Length: 10\n"
3378 "ETag: w/\"foo\"\n";
3379 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3381 Verify206Response(headers
, 40, 49);
3382 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3383 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3384 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3386 // Now verify that there's no cached data.
3387 RunTransactionTestWithResponse(cache
.http_cache(), kRangeGET_TransactionOK
,
3390 Verify206Response(headers
, 40, 49);
3391 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3392 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3393 EXPECT_EQ(2, cache
.disk_cache()->create_count());
3395 RemoveMockTransaction(&transaction
);
3398 // Tests that we can cache range requests and fetch random blocks from the
3399 // cache and the network.
3400 TEST(HttpCache
, RangeGET_OK
) {
3401 MockHttpCache cache
;
3402 AddMockTransaction(&kRangeGET_TransactionOK
);
3403 std::string headers
;
3405 // Write to the cache (40-49).
3406 RunTransactionTestWithResponse(cache
.http_cache(), kRangeGET_TransactionOK
,
3409 Verify206Response(headers
, 40, 49);
3410 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3411 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3412 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3414 // Read from the cache (40-49).
3415 RunTransactionTestWithResponse(cache
.http_cache(), kRangeGET_TransactionOK
,
3418 Verify206Response(headers
, 40, 49);
3419 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3420 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3421 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3423 // Make sure we are done with the previous transaction.
3424 base::MessageLoop::current()->RunUntilIdle();
3426 // Write to the cache (30-39).
3427 MockTransaction
transaction(kRangeGET_TransactionOK
);
3428 transaction
.request_headers
= "Range: bytes = 30-39\r\n" EXTRA_HEADER
;
3429 transaction
.data
= "rg: 30-39 ";
3430 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3432 Verify206Response(headers
, 30, 39);
3433 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3434 EXPECT_EQ(2, cache
.disk_cache()->open_count());
3435 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3437 // Make sure we are done with the previous transaction.
3438 base::MessageLoop::current()->RunUntilIdle();
3440 // Write and read from the cache (20-59).
3441 transaction
.request_headers
= "Range: bytes = 20-59\r\n" EXTRA_HEADER
;
3442 transaction
.data
= "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 ";
3443 net::CapturingBoundNetLog log
;
3444 net::LoadTimingInfo load_timing_info
;
3445 RunTransactionTestWithResponseAndGetTiming(
3446 cache
.http_cache(), transaction
, &headers
, log
.bound(),
3449 Verify206Response(headers
, 20, 59);
3450 EXPECT_EQ(4, cache
.network_layer()->transaction_count());
3451 EXPECT_EQ(3, cache
.disk_cache()->open_count());
3452 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3453 TestLoadTimingNetworkRequest(load_timing_info
);
3455 RemoveMockTransaction(&kRangeGET_TransactionOK
);
3458 #if defined(OS_ANDROID)
3460 // Checks that with a cache backend having Sparse IO unimplementes the cache
3461 // entry would be doomed after a range request.
3462 // TODO(pasko): remove when the SimpleBackendImpl implements Sparse IO.
3463 TEST(HttpCache
, RangeGET_SparseNotImplemented
) {
3464 MockHttpCache cache
;
3465 cache
.disk_cache()->set_fail_sparse_requests();
3467 // Run a cacheable request to prime the cache.
3468 MockTransaction
transaction(kTypicalGET_Transaction
);
3469 transaction
.url
= kRangeGET_TransactionOK
.url
;
3470 AddMockTransaction(&transaction
);
3471 RunTransactionTest(cache
.http_cache(), transaction
);
3472 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3473 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3474 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3476 // Verify that we added the entry.
3477 disk_cache::Entry
* entry
;
3478 net::TestCompletionCallback cb
;
3479 int rv
= cache
.disk_cache()->OpenEntry(transaction
.url
,
3482 ASSERT_EQ(net::OK
, cb
.GetResult(rv
));
3483 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3485 RemoveMockTransaction(&transaction
);
3487 // Request the range with the backend that does not support it.
3488 MockTransaction
transaction2(kRangeGET_TransactionOK
);
3489 std::string headers
;
3490 AddMockTransaction(&transaction2
);
3491 RunTransactionTestWithResponse(cache
.http_cache(), transaction2
, &headers
);
3492 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3493 EXPECT_EQ(2, cache
.disk_cache()->open_count());
3494 EXPECT_EQ(2, cache
.disk_cache()->create_count());
3496 // Mock cache would return net::ERR_CACHE_OPEN_FAILURE on a doomed entry, even
3497 // if it was re-created later, so this effectively checks that the old data is
3499 disk_cache::Entry
* entry2
;
3500 rv
= cache
.disk_cache()->OpenEntry(transaction2
.url
,
3503 ASSERT_EQ(net::ERR_CACHE_OPEN_FAILURE
, cb
.GetResult(rv
));
3504 RemoveMockTransaction(&transaction2
);
3507 TEST(HttpCache
, RangeGET_SparseNotImplementedOnEmptyCache
) {
3508 MockHttpCache cache
;
3509 cache
.disk_cache()->set_fail_sparse_requests();
3511 // Request the range with the backend that does not support it.
3512 MockTransaction
transaction(kRangeGET_TransactionOK
);
3513 std::string headers
;
3514 AddMockTransaction(&transaction
);
3515 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3516 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3517 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3518 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3520 // Mock cache would return net::ERR_CACHE_OPEN_FAILURE on a doomed entry, even
3521 // if it was re-created later, so this effectively checks that the old data is
3522 // gone as a result of a failed range write.
3523 disk_cache::Entry
* entry
;
3524 net::TestCompletionCallback cb
;
3525 int rv
= cache
.disk_cache()->OpenEntry(transaction
.url
,
3528 ASSERT_EQ(net::ERR_CACHE_OPEN_FAILURE
, cb
.GetResult(rv
));
3529 RemoveMockTransaction(&transaction
);
3532 #endif // OS_ANDROID
3534 // Tests that we can cache range requests and fetch random blocks from the
3535 // cache and the network, with synchronous responses.
3536 TEST(HttpCache
, RangeGET_SyncOK
) {
3537 MockHttpCache cache
;
3539 MockTransaction
transaction(kRangeGET_TransactionOK
);
3540 transaction
.test_mode
= TEST_MODE_SYNC_ALL
;
3541 AddMockTransaction(&transaction
);
3543 // Write to the cache (40-49).
3544 std::string headers
;
3545 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3547 Verify206Response(headers
, 40, 49);
3548 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3549 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3550 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3552 // Read from the cache (40-49).
3553 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3555 Verify206Response(headers
, 40, 49);
3556 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3557 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3558 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3560 // Make sure we are done with the previous transaction.
3561 base::MessageLoop::current()->RunUntilIdle();
3563 // Write to the cache (30-39).
3564 transaction
.request_headers
= "Range: bytes = 30-39\r\n" EXTRA_HEADER
;
3565 transaction
.data
= "rg: 30-39 ";
3566 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3568 Verify206Response(headers
, 30, 39);
3569 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3570 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3571 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3573 // Make sure we are done with the previous transaction.
3574 base::MessageLoop::current()->RunUntilIdle();
3576 // Write and read from the cache (20-59).
3577 transaction
.request_headers
= "Range: bytes = 20-59\r\n" EXTRA_HEADER
;
3578 transaction
.data
= "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 ";
3579 net::CapturingBoundNetLog log
;
3580 net::LoadTimingInfo load_timing_info
;
3581 RunTransactionTestWithResponseAndGetTiming(
3582 cache
.http_cache(), transaction
, &headers
, log
.bound(),
3585 Verify206Response(headers
, 20, 59);
3586 EXPECT_EQ(4, cache
.network_layer()->transaction_count());
3587 EXPECT_EQ(2, cache
.disk_cache()->open_count());
3588 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3589 TestLoadTimingNetworkRequest(load_timing_info
);
3591 RemoveMockTransaction(&transaction
);
3594 // Tests that we don't revalidate an entry unless we are required to do so.
3595 TEST(HttpCache
, RangeGET_Revalidate1
) {
3596 MockHttpCache cache
;
3597 std::string headers
;
3599 // Write to the cache (40-49).
3600 MockTransaction
transaction(kRangeGET_TransactionOK
);
3601 transaction
.response_headers
=
3602 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
3603 "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n" // Should never expire.
3605 "Accept-Ranges: bytes\n"
3606 "Content-Length: 10\n";
3607 AddMockTransaction(&transaction
);
3608 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3610 Verify206Response(headers
, 40, 49);
3611 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3612 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3613 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3615 // Read from the cache (40-49).
3616 net::CapturingBoundNetLog log
;
3617 net::LoadTimingInfo load_timing_info
;
3618 RunTransactionTestWithResponseAndGetTiming(
3619 cache
.http_cache(), transaction
, &headers
, log
.bound(),
3622 Verify206Response(headers
, 40, 49);
3623 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3624 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3625 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3626 TestLoadTimingCachedResponse(load_timing_info
);
3628 // Read again forcing the revalidation.
3629 transaction
.load_flags
|= net::LOAD_VALIDATE_CACHE
;
3630 RunTransactionTestWithResponseAndGetTiming(
3631 cache
.http_cache(), transaction
, &headers
, log
.bound(),
3634 Verify206Response(headers
, 40, 49);
3635 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3636 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3637 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3638 TestLoadTimingNetworkRequest(load_timing_info
);
3640 RemoveMockTransaction(&transaction
);
3643 // Checks that we revalidate an entry when the headers say so.
3644 TEST(HttpCache
, RangeGET_Revalidate2
) {
3645 MockHttpCache cache
;
3646 std::string headers
;
3648 // Write to the cache (40-49).
3649 MockTransaction
transaction(kRangeGET_TransactionOK
);
3650 transaction
.response_headers
=
3651 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
3652 "Expires: Sat, 18 Apr 2009 01:10:43 GMT\n" // Expired.
3654 "Accept-Ranges: bytes\n"
3655 "Content-Length: 10\n";
3656 AddMockTransaction(&transaction
);
3657 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3659 Verify206Response(headers
, 40, 49);
3660 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3661 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3662 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3664 // Read from the cache (40-49).
3665 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3666 Verify206Response(headers
, 40, 49);
3668 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3669 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3670 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3672 RemoveMockTransaction(&transaction
);
3675 // Tests that we deal with 304s for range requests.
3676 TEST(HttpCache
, RangeGET_304
) {
3677 MockHttpCache cache
;
3678 AddMockTransaction(&kRangeGET_TransactionOK
);
3679 std::string headers
;
3681 // Write to the cache (40-49).
3682 RunTransactionTestWithResponse(cache
.http_cache(), kRangeGET_TransactionOK
,
3685 Verify206Response(headers
, 40, 49);
3686 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3687 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3688 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3690 // Read from the cache (40-49).
3691 RangeTransactionServer handler
;
3692 handler
.set_not_modified(true);
3693 MockTransaction
transaction(kRangeGET_TransactionOK
);
3694 transaction
.load_flags
|= net::LOAD_VALIDATE_CACHE
;
3695 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3697 Verify206Response(headers
, 40, 49);
3698 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3699 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3700 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3702 RemoveMockTransaction(&kRangeGET_TransactionOK
);
3705 // Tests that we deal with 206s when revalidating range requests.
3706 TEST(HttpCache
, RangeGET_ModifiedResult
) {
3707 MockHttpCache cache
;
3708 AddMockTransaction(&kRangeGET_TransactionOK
);
3709 std::string headers
;
3711 // Write to the cache (40-49).
3712 RunTransactionTestWithResponse(cache
.http_cache(), kRangeGET_TransactionOK
,
3715 Verify206Response(headers
, 40, 49);
3716 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3717 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3718 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3720 // Attempt to read from the cache (40-49).
3721 RangeTransactionServer handler
;
3722 handler
.set_modified(true);
3723 MockTransaction
transaction(kRangeGET_TransactionOK
);
3724 transaction
.load_flags
|= net::LOAD_VALIDATE_CACHE
;
3725 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3727 Verify206Response(headers
, 40, 49);
3728 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3729 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3730 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3732 // And the entry should be gone.
3733 RunTransactionTest(cache
.http_cache(), kRangeGET_TransactionOK
);
3734 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
3735 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3736 EXPECT_EQ(2, cache
.disk_cache()->create_count());
3738 RemoveMockTransaction(&kRangeGET_TransactionOK
);
3741 // Tests that we cache 301s for range requests.
3742 TEST(HttpCache
, RangeGET_301
) {
3743 MockHttpCache cache
;
3744 ScopedMockTransaction
transaction(kRangeGET_TransactionOK
);
3745 transaction
.status
= "HTTP/1.1 301 Moved Permanently";
3746 transaction
.response_headers
= "Location: http://www.bar.com/\n";
3747 transaction
.data
= "";
3748 transaction
.handler
= NULL
;
3749 AddMockTransaction(&transaction
);
3751 // Write to the cache.
3752 RunTransactionTest(cache
.http_cache(), transaction
);
3753 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3754 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3755 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3757 // Read from the cache.
3758 RunTransactionTest(cache
.http_cache(), transaction
);
3759 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3760 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3761 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3763 RemoveMockTransaction(&transaction
);
3766 // Tests that we can cache range requests when the start or end is unknown.
3767 // We start with one suffix request, followed by a request from a given point.
3768 TEST(HttpCache
, UnknownRangeGET_1
) {
3769 MockHttpCache cache
;
3770 AddMockTransaction(&kRangeGET_TransactionOK
);
3771 std::string headers
;
3773 // Write to the cache (70-79).
3774 MockTransaction
transaction(kRangeGET_TransactionOK
);
3775 transaction
.request_headers
= "Range: bytes = -10\r\n" EXTRA_HEADER
;
3776 transaction
.data
= "rg: 70-79 ";
3777 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3779 Verify206Response(headers
, 70, 79);
3780 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3781 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3782 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3784 // Make sure we are done with the previous transaction.
3785 base::MessageLoop::current()->RunUntilIdle();
3787 // Write and read from the cache (60-79).
3788 transaction
.request_headers
= "Range: bytes = 60-\r\n" EXTRA_HEADER
;
3789 transaction
.data
= "rg: 60-69 rg: 70-79 ";
3790 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3792 Verify206Response(headers
, 60, 79);
3793 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3794 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3795 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3797 RemoveMockTransaction(&kRangeGET_TransactionOK
);
3800 // Tests that we can cache range requests when the start or end is unknown.
3801 // We start with one request from a given point, followed by a suffix request.
3802 // We'll also verify that synchronous cache responses work as intended.
3803 TEST(HttpCache
, UnknownRangeGET_2
) {
3804 MockHttpCache cache
;
3805 std::string headers
;
3807 MockTransaction
transaction(kRangeGET_TransactionOK
);
3808 transaction
.test_mode
= TEST_MODE_SYNC_CACHE_START
|
3809 TEST_MODE_SYNC_CACHE_READ
|
3810 TEST_MODE_SYNC_CACHE_WRITE
;
3811 AddMockTransaction(&transaction
);
3813 // Write to the cache (70-79).
3814 transaction
.request_headers
= "Range: bytes = 70-\r\n" EXTRA_HEADER
;
3815 transaction
.data
= "rg: 70-79 ";
3816 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3818 Verify206Response(headers
, 70, 79);
3819 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3820 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3821 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3823 // Make sure we are done with the previous transaction.
3824 base::MessageLoop::current()->RunUntilIdle();
3826 // Write and read from the cache (60-79).
3827 transaction
.request_headers
= "Range: bytes = -20\r\n" EXTRA_HEADER
;
3828 transaction
.data
= "rg: 60-69 rg: 70-79 ";
3829 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3831 Verify206Response(headers
, 60, 79);
3832 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3833 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3834 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3836 RemoveMockTransaction(&transaction
);
3839 // Tests that receiving Not Modified when asking for an open range doesn't mess
3841 TEST(HttpCache
, UnknownRangeGET_304
) {
3842 MockHttpCache cache
;
3843 std::string headers
;
3845 MockTransaction
transaction(kRangeGET_TransactionOK
);
3846 AddMockTransaction(&transaction
);
3848 RangeTransactionServer handler
;
3849 handler
.set_not_modified(true);
3851 // Ask for the end of the file, without knowing the length.
3852 transaction
.request_headers
= "Range: bytes = 70-\r\n" EXTRA_HEADER
;
3853 transaction
.data
= "";
3854 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3856 // We just bypass the cache.
3857 EXPECT_EQ(0U, headers
.find("HTTP/1.1 304 Not Modified\n"));
3858 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3859 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3860 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3862 RunTransactionTest(cache
.http_cache(), transaction
);
3863 EXPECT_EQ(2, cache
.disk_cache()->create_count());
3865 RemoveMockTransaction(&transaction
);
3868 // Tests that we can handle non-range requests when we have cached a range.
3869 TEST(HttpCache
, GET_Previous206
) {
3870 MockHttpCache cache
;
3871 AddMockTransaction(&kRangeGET_TransactionOK
);
3872 std::string headers
;
3873 net::CapturingBoundNetLog log
;
3874 net::LoadTimingInfo load_timing_info
;
3876 // Write to the cache (40-49).
3877 RunTransactionTestWithResponseAndGetTiming(
3878 cache
.http_cache(), kRangeGET_TransactionOK
, &headers
, log
.bound(),
3881 Verify206Response(headers
, 40, 49);
3882 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3883 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3884 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3885 TestLoadTimingNetworkRequest(load_timing_info
);
3887 // Write and read from the cache (0-79), when not asked for a range.
3888 MockTransaction
transaction(kRangeGET_TransactionOK
);
3889 transaction
.request_headers
= EXTRA_HEADER
;
3890 transaction
.data
= "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
3891 "rg: 50-59 rg: 60-69 rg: 70-79 ";
3892 RunTransactionTestWithResponseAndGetTiming(
3893 cache
.http_cache(), transaction
, &headers
, log
.bound(),
3896 EXPECT_EQ(0U, headers
.find("HTTP/1.1 200 OK\n"));
3897 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
3898 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3899 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3900 TestLoadTimingNetworkRequest(load_timing_info
);
3902 RemoveMockTransaction(&kRangeGET_TransactionOK
);
3905 // Tests that we can handle non-range requests when we have cached the first
3906 // part of the object and the server replies with 304 (Not Modified).
3907 TEST(HttpCache
, GET_Previous206_NotModified
) {
3908 MockHttpCache cache
;
3910 MockTransaction
transaction(kRangeGET_TransactionOK
);
3911 AddMockTransaction(&transaction
);
3912 std::string headers
;
3913 net::CapturingBoundNetLog log
;
3914 net::LoadTimingInfo load_timing_info
;
3916 // Write to the cache (0-9).
3917 transaction
.request_headers
= "Range: bytes = 0-9\r\n" EXTRA_HEADER
;
3918 transaction
.data
= "rg: 00-09 ";
3919 RunTransactionTestWithResponseAndGetTiming(
3920 cache
.http_cache(), transaction
, &headers
, log
.bound(),
3922 Verify206Response(headers
, 0, 9);
3923 TestLoadTimingNetworkRequest(load_timing_info
);
3925 // Write to the cache (70-79).
3926 transaction
.request_headers
= "Range: bytes = 70-79\r\n" EXTRA_HEADER
;
3927 transaction
.data
= "rg: 70-79 ";
3928 RunTransactionTestWithResponseAndGetTiming(
3929 cache
.http_cache(), transaction
, &headers
, log
.bound(),
3931 Verify206Response(headers
, 70, 79);
3933 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
3934 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3935 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3936 TestLoadTimingNetworkRequest(load_timing_info
);
3938 // Read from the cache (0-9), write and read from cache (10 - 79).
3939 transaction
.load_flags
|= net::LOAD_VALIDATE_CACHE
;
3940 transaction
.request_headers
= "Foo: bar\r\n" EXTRA_HEADER
;
3941 transaction
.data
= "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
3942 "rg: 50-59 rg: 60-69 rg: 70-79 ";
3943 RunTransactionTestWithResponseAndGetTiming(
3944 cache
.http_cache(), transaction
, &headers
, log
.bound(),
3947 EXPECT_EQ(0U, headers
.find("HTTP/1.1 200 OK\n"));
3948 EXPECT_EQ(4, cache
.network_layer()->transaction_count());
3949 EXPECT_EQ(2, cache
.disk_cache()->open_count());
3950 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3951 TestLoadTimingNetworkRequest(load_timing_info
);
3953 RemoveMockTransaction(&transaction
);
3956 // Tests that we can handle a regular request to a sparse entry, that results in
3957 // new content provided by the server (206).
3958 TEST(HttpCache
, GET_Previous206_NewContent
) {
3959 MockHttpCache cache
;
3960 AddMockTransaction(&kRangeGET_TransactionOK
);
3961 std::string headers
;
3963 // Write to the cache (0-9).
3964 MockTransaction
transaction(kRangeGET_TransactionOK
);
3965 transaction
.request_headers
= "Range: bytes = 0-9\r\n" EXTRA_HEADER
;
3966 transaction
.data
= "rg: 00-09 ";
3967 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
3969 Verify206Response(headers
, 0, 9);
3970 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
3971 EXPECT_EQ(0, cache
.disk_cache()->open_count());
3972 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3974 // Now we'll issue a request without any range that should result first in a
3975 // 206 (when revalidating), and then in a weird standard answer: the test
3976 // server will not modify the response so we'll get the default range... a
3977 // real server will answer with 200.
3978 MockTransaction
transaction2(kRangeGET_TransactionOK
);
3979 transaction2
.request_headers
= EXTRA_HEADER
;
3980 transaction2
.load_flags
|= net::LOAD_VALIDATE_CACHE
;
3981 transaction2
.data
= "Not a range";
3982 RangeTransactionServer handler
;
3983 handler
.set_modified(true);
3984 net::CapturingBoundNetLog log
;
3985 net::LoadTimingInfo load_timing_info
;
3986 RunTransactionTestWithResponseAndGetTiming(
3987 cache
.http_cache(), transaction2
, &headers
, log
.bound(),
3990 EXPECT_EQ(0U, headers
.find("HTTP/1.1 200 OK\n"));
3991 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
3992 EXPECT_EQ(1, cache
.disk_cache()->open_count());
3993 EXPECT_EQ(1, cache
.disk_cache()->create_count());
3994 TestLoadTimingNetworkRequest(load_timing_info
);
3996 // Verify that the previous request deleted the entry.
3997 RunTransactionTest(cache
.http_cache(), transaction
);
3998 EXPECT_EQ(2, cache
.disk_cache()->create_count());
4000 RemoveMockTransaction(&transaction
);
4003 // Tests that we can handle cached 206 responses that are not sparse.
4004 TEST(HttpCache
, GET_Previous206_NotSparse
) {
4005 MockHttpCache cache
;
4007 // Create a disk cache entry that stores 206 headers while not being sparse.
4008 disk_cache::Entry
* entry
;
4009 ASSERT_TRUE(cache
.CreateBackendEntry(kSimpleGET_Transaction
.url
, &entry
,
4012 std::string
raw_headers(kRangeGET_TransactionOK
.status
);
4013 raw_headers
.append("\n");
4014 raw_headers
.append(kRangeGET_TransactionOK
.response_headers
);
4015 raw_headers
= net::HttpUtil::AssembleRawHeaders(raw_headers
.data(),
4016 raw_headers
.size());
4018 net::HttpResponseInfo response
;
4019 response
.headers
= new net::HttpResponseHeaders(raw_headers
);
4020 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry
, &response
, true, false));
4022 scoped_refptr
<net::IOBuffer
> buf(new net::IOBuffer(500));
4023 int len
= static_cast<int>(base::strlcpy(buf
->data(),
4024 kRangeGET_TransactionOK
.data
, 500));
4025 net::TestCompletionCallback cb
;
4026 int rv
= entry
->WriteData(1, 0, buf
.get(), len
, cb
.callback(), true);
4027 EXPECT_EQ(len
, cb
.GetResult(rv
));
4030 // Now see that we don't use the stored entry.
4031 std::string headers
;
4032 net::CapturingBoundNetLog log
;
4033 net::LoadTimingInfo load_timing_info
;
4034 RunTransactionTestWithResponseAndGetTiming(
4035 cache
.http_cache(), kSimpleGET_Transaction
, &headers
, log
.bound(),
4038 // We are expecting a 200.
4039 std::string
expected_headers(kSimpleGET_Transaction
.status
);
4040 expected_headers
.append("\n");
4041 expected_headers
.append(kSimpleGET_Transaction
.response_headers
);
4042 EXPECT_EQ(expected_headers
, headers
);
4043 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4044 EXPECT_EQ(1, cache
.disk_cache()->open_count());
4045 EXPECT_EQ(2, cache
.disk_cache()->create_count());
4046 TestLoadTimingNetworkRequest(load_timing_info
);
4049 // Tests that we can handle cached 206 responses that are not sparse. This time
4050 // we issue a range request and expect to receive a range.
4051 TEST(HttpCache
, RangeGET_Previous206_NotSparse_2
) {
4052 MockHttpCache cache
;
4053 AddMockTransaction(&kRangeGET_TransactionOK
);
4055 // Create a disk cache entry that stores 206 headers while not being sparse.
4056 disk_cache::Entry
* entry
;
4057 ASSERT_TRUE(cache
.CreateBackendEntry(kRangeGET_TransactionOK
.url
, &entry
,
4060 std::string
raw_headers(kRangeGET_TransactionOK
.status
);
4061 raw_headers
.append("\n");
4062 raw_headers
.append(kRangeGET_TransactionOK
.response_headers
);
4063 raw_headers
= net::HttpUtil::AssembleRawHeaders(raw_headers
.data(),
4064 raw_headers
.size());
4066 net::HttpResponseInfo response
;
4067 response
.headers
= new net::HttpResponseHeaders(raw_headers
);
4068 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry
, &response
, true, false));
4070 scoped_refptr
<net::IOBuffer
> buf(new net::IOBuffer(500));
4071 int len
= static_cast<int>(base::strlcpy(buf
->data(),
4072 kRangeGET_TransactionOK
.data
, 500));
4073 net::TestCompletionCallback cb
;
4074 int rv
= entry
->WriteData(1, 0, buf
.get(), len
, cb
.callback(), true);
4075 EXPECT_EQ(len
, cb
.GetResult(rv
));
4078 // Now see that we don't use the stored entry.
4079 std::string headers
;
4080 RunTransactionTestWithResponse(cache
.http_cache(), kRangeGET_TransactionOK
,
4083 // We are expecting a 206.
4084 Verify206Response(headers
, 40, 49);
4085 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4086 EXPECT_EQ(1, cache
.disk_cache()->open_count());
4087 EXPECT_EQ(2, cache
.disk_cache()->create_count());
4089 RemoveMockTransaction(&kRangeGET_TransactionOK
);
4092 // Tests that we can handle cached 206 responses that can't be validated.
4093 TEST(HttpCache
, GET_Previous206_NotValidation
) {
4094 MockHttpCache cache
;
4096 // Create a disk cache entry that stores 206 headers.
4097 disk_cache::Entry
* entry
;
4098 ASSERT_TRUE(cache
.CreateBackendEntry(kSimpleGET_Transaction
.url
, &entry
,
4101 // Make sure that the headers cannot be validated with the server.
4102 std::string
raw_headers(kRangeGET_TransactionOK
.status
);
4103 raw_headers
.append("\n");
4104 raw_headers
.append("Content-Length: 80\n");
4105 raw_headers
= net::HttpUtil::AssembleRawHeaders(raw_headers
.data(),
4106 raw_headers
.size());
4108 net::HttpResponseInfo response
;
4109 response
.headers
= new net::HttpResponseHeaders(raw_headers
);
4110 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry
, &response
, true, false));
4112 scoped_refptr
<net::IOBuffer
> buf(new net::IOBuffer(500));
4113 int len
= static_cast<int>(base::strlcpy(buf
->data(),
4114 kRangeGET_TransactionOK
.data
, 500));
4115 net::TestCompletionCallback cb
;
4116 int rv
= entry
->WriteData(1, 0, buf
.get(), len
, cb
.callback(), true);
4117 EXPECT_EQ(len
, cb
.GetResult(rv
));
4120 // Now see that we don't use the stored entry.
4121 std::string headers
;
4122 RunTransactionTestWithResponse(cache
.http_cache(), kSimpleGET_Transaction
,
4125 // We are expecting a 200.
4126 std::string
expected_headers(kSimpleGET_Transaction
.status
);
4127 expected_headers
.append("\n");
4128 expected_headers
.append(kSimpleGET_Transaction
.response_headers
);
4129 EXPECT_EQ(expected_headers
, headers
);
4130 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4131 EXPECT_EQ(1, cache
.disk_cache()->open_count());
4132 EXPECT_EQ(2, cache
.disk_cache()->create_count());
4135 // Tests that we can handle range requests with cached 200 responses.
4136 TEST(HttpCache
, RangeGET_Previous200
) {
4137 MockHttpCache cache
;
4139 // Store the whole thing with status 200.
4140 MockTransaction
transaction(kTypicalGET_Transaction
);
4141 transaction
.url
= kRangeGET_TransactionOK
.url
;
4142 transaction
.data
= "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
4143 "rg: 50-59 rg: 60-69 rg: 70-79 ";
4144 AddMockTransaction(&transaction
);
4145 RunTransactionTest(cache
.http_cache(), transaction
);
4146 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4147 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4148 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4150 RemoveMockTransaction(&transaction
);
4151 AddMockTransaction(&kRangeGET_TransactionOK
);
4153 // Now see that we use the stored entry.
4154 std::string headers
;
4155 MockTransaction
transaction2(kRangeGET_TransactionOK
);
4156 RangeTransactionServer handler
;
4157 handler
.set_not_modified(true);
4158 RunTransactionTestWithResponse(cache
.http_cache(), transaction2
, &headers
);
4160 // We are expecting a 206.
4161 Verify206Response(headers
, 40, 49);
4162 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
4163 EXPECT_EQ(1, cache
.disk_cache()->open_count());
4164 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4166 // The last transaction has finished so make sure the entry is deactivated.
4167 base::MessageLoop::current()->RunUntilIdle();
4169 // Make a request for an invalid range.
4170 MockTransaction
transaction3(kRangeGET_TransactionOK
);
4171 transaction3
.request_headers
= "Range: bytes = 80-90\r\n" EXTRA_HEADER
;
4172 transaction3
.data
= transaction
.data
;
4173 transaction3
.load_flags
= net::LOAD_PREFERRING_CACHE
;
4174 RunTransactionTestWithResponse(cache
.http_cache(), transaction3
, &headers
);
4175 EXPECT_EQ(2, cache
.disk_cache()->open_count());
4176 EXPECT_EQ(0U, headers
.find("HTTP/1.1 200 "));
4177 EXPECT_EQ(std::string::npos
, headers
.find("Content-Range:"));
4178 EXPECT_EQ(std::string::npos
, headers
.find("Content-Length: 80"));
4180 // Make sure the entry is deactivated.
4181 base::MessageLoop::current()->RunUntilIdle();
4183 // Even though the request was invalid, we should have the entry.
4184 RunTransactionTest(cache
.http_cache(), transaction2
);
4185 EXPECT_EQ(3, cache
.disk_cache()->open_count());
4187 // Make sure the entry is deactivated.
4188 base::MessageLoop::current()->RunUntilIdle();
4190 // Now we should receive a range from the server and drop the stored entry.
4191 handler
.set_not_modified(false);
4192 transaction2
.request_headers
= kRangeGET_TransactionOK
.request_headers
;
4193 RunTransactionTestWithResponse(cache
.http_cache(), transaction2
, &headers
);
4194 Verify206Response(headers
, 40, 49);
4195 EXPECT_EQ(4, cache
.network_layer()->transaction_count());
4196 EXPECT_EQ(4, cache
.disk_cache()->open_count());
4197 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4199 RunTransactionTest(cache
.http_cache(), transaction2
);
4200 EXPECT_EQ(2, cache
.disk_cache()->create_count());
4202 RemoveMockTransaction(&kRangeGET_TransactionOK
);
4205 // Tests that we can handle a 200 response when dealing with sparse entries.
4206 TEST(HttpCache
, RangeRequestResultsIn200
) {
4207 MockHttpCache cache
;
4208 AddMockTransaction(&kRangeGET_TransactionOK
);
4209 std::string headers
;
4211 // Write to the cache (70-79).
4212 MockTransaction
transaction(kRangeGET_TransactionOK
);
4213 transaction
.request_headers
= "Range: bytes = -10\r\n" EXTRA_HEADER
;
4214 transaction
.data
= "rg: 70-79 ";
4215 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
4217 Verify206Response(headers
, 70, 79);
4218 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4219 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4220 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4222 // Now we'll issue a request that results in a plain 200 response, but to
4223 // the to the same URL that we used to store sparse data, and making sure
4224 // that we ask for a range.
4225 RemoveMockTransaction(&kRangeGET_TransactionOK
);
4226 MockTransaction
transaction2(kSimpleGET_Transaction
);
4227 transaction2
.url
= kRangeGET_TransactionOK
.url
;
4228 transaction2
.request_headers
= kRangeGET_TransactionOK
.request_headers
;
4229 AddMockTransaction(&transaction2
);
4231 RunTransactionTestWithResponse(cache
.http_cache(), transaction2
, &headers
);
4233 std::string
expected_headers(kSimpleGET_Transaction
.status
);
4234 expected_headers
.append("\n");
4235 expected_headers
.append(kSimpleGET_Transaction
.response_headers
);
4236 EXPECT_EQ(expected_headers
, headers
);
4237 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
4238 EXPECT_EQ(1, cache
.disk_cache()->open_count());
4239 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4241 RemoveMockTransaction(&transaction2
);
4244 // Tests that a range request that falls outside of the size that we know about
4245 // only deletes the entry if the resource has indeed changed.
4246 TEST(HttpCache
, RangeGET_MoreThanCurrentSize
) {
4247 MockHttpCache cache
;
4248 AddMockTransaction(&kRangeGET_TransactionOK
);
4249 std::string headers
;
4251 // Write to the cache (40-49).
4252 RunTransactionTestWithResponse(cache
.http_cache(), kRangeGET_TransactionOK
,
4255 Verify206Response(headers
, 40, 49);
4256 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4257 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4258 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4260 // A weird request should not delete this entry. Ask for bytes 120-.
4261 MockTransaction
transaction(kRangeGET_TransactionOK
);
4262 transaction
.request_headers
= "Range: bytes = 120-\r\n" EXTRA_HEADER
;
4263 transaction
.data
= "";
4264 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
4266 EXPECT_EQ(0U, headers
.find("HTTP/1.1 416 "));
4267 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
4268 EXPECT_EQ(1, cache
.disk_cache()->open_count());
4269 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4271 RunTransactionTest(cache
.http_cache(), kRangeGET_TransactionOK
);
4272 EXPECT_EQ(2, cache
.disk_cache()->open_count());
4273 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4275 RemoveMockTransaction(&kRangeGET_TransactionOK
);
4278 // Tests that we don't delete a sparse entry when we cancel a request.
4279 TEST(HttpCache
, RangeGET_Cancel
) {
4280 MockHttpCache cache
;
4281 AddMockTransaction(&kRangeGET_TransactionOK
);
4283 MockHttpRequest
request(kRangeGET_TransactionOK
);
4285 Context
* c
= new Context();
4286 int rv
= cache
.http_cache()->CreateTransaction(
4287 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
4288 EXPECT_EQ(net::OK
, rv
);
4290 rv
= c
->trans
->Start(&request
, c
->callback
.callback(), net::BoundNetLog());
4291 if (rv
== net::ERR_IO_PENDING
)
4292 rv
= c
->callback
.WaitForResult();
4294 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4295 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4296 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4298 // Make sure that the entry has some data stored.
4299 scoped_refptr
<net::IOBufferWithSize
> buf(new net::IOBufferWithSize(10));
4300 rv
= c
->trans
->Read(buf
.get(), buf
->size(), c
->callback
.callback());
4301 if (rv
== net::ERR_IO_PENDING
)
4302 rv
= c
->callback
.WaitForResult();
4303 EXPECT_EQ(buf
->size(), rv
);
4305 // Destroy the transaction.
4308 // Verify that the entry has not been deleted.
4309 disk_cache::Entry
* entry
;
4310 ASSERT_TRUE(cache
.OpenBackendEntry(kRangeGET_TransactionOK
.url
, &entry
));
4312 RemoveMockTransaction(&kRangeGET_TransactionOK
);
4315 // Tests that we don't delete a sparse entry when we start a new request after
4316 // cancelling the previous one.
4317 TEST(HttpCache
, RangeGET_Cancel2
) {
4318 MockHttpCache cache
;
4319 AddMockTransaction(&kRangeGET_TransactionOK
);
4321 RunTransactionTest(cache
.http_cache(), kRangeGET_TransactionOK
);
4322 MockHttpRequest
request(kRangeGET_TransactionOK
);
4323 request
.load_flags
|= net::LOAD_VALIDATE_CACHE
;
4325 Context
* c
= new Context();
4326 int rv
= cache
.http_cache()->CreateTransaction(
4327 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
4328 EXPECT_EQ(net::OK
, rv
);
4330 rv
= c
->trans
->Start(&request
, c
->callback
.callback(), net::BoundNetLog());
4331 if (rv
== net::ERR_IO_PENDING
)
4332 rv
= c
->callback
.WaitForResult();
4334 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
4335 EXPECT_EQ(1, cache
.disk_cache()->open_count());
4336 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4338 // Make sure that we revalidate the entry and read from the cache (a single
4339 // read will return while waiting for the network).
4340 scoped_refptr
<net::IOBufferWithSize
> buf(new net::IOBufferWithSize(5));
4341 rv
= c
->trans
->Read(buf
.get(), buf
->size(), c
->callback
.callback());
4342 EXPECT_EQ(5, c
->callback
.GetResult(rv
));
4343 rv
= c
->trans
->Read(buf
.get(), buf
->size(), c
->callback
.callback());
4344 EXPECT_EQ(net::ERR_IO_PENDING
, rv
);
4346 // Destroy the transaction before completing the read.
4349 // We have the read and the delete (OnProcessPendingQueue) waiting on the
4350 // message loop. This means that a new transaction will just reuse the same
4351 // active entry (no open or create).
4353 RunTransactionTest(cache
.http_cache(), kRangeGET_TransactionOK
);
4355 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
4356 EXPECT_EQ(1, cache
.disk_cache()->open_count());
4357 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4358 RemoveMockTransaction(&kRangeGET_TransactionOK
);
4361 // A slight variation of the previous test, this time we cancel two requests in
4362 // a row, making sure that the second is waiting for the entry to be ready.
4363 TEST(HttpCache
, RangeGET_Cancel3
) {
4364 MockHttpCache cache
;
4365 AddMockTransaction(&kRangeGET_TransactionOK
);
4367 RunTransactionTest(cache
.http_cache(), kRangeGET_TransactionOK
);
4368 MockHttpRequest
request(kRangeGET_TransactionOK
);
4369 request
.load_flags
|= net::LOAD_VALIDATE_CACHE
;
4371 Context
* c
= new Context();
4372 int rv
= cache
.http_cache()->CreateTransaction(
4373 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
4374 EXPECT_EQ(net::OK
, rv
);
4376 rv
= c
->trans
->Start(&request
, c
->callback
.callback(), net::BoundNetLog());
4377 EXPECT_EQ(net::ERR_IO_PENDING
, rv
);
4378 rv
= c
->callback
.WaitForResult();
4380 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
4381 EXPECT_EQ(1, cache
.disk_cache()->open_count());
4382 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4384 // Make sure that we revalidate the entry and read from the cache (a single
4385 // read will return while waiting for the network).
4386 scoped_refptr
<net::IOBufferWithSize
> buf(new net::IOBufferWithSize(5));
4387 rv
= c
->trans
->Read(buf
.get(), buf
->size(), c
->callback
.callback());
4388 EXPECT_EQ(5, c
->callback
.GetResult(rv
));
4389 rv
= c
->trans
->Read(buf
.get(), buf
->size(), c
->callback
.callback());
4390 EXPECT_EQ(net::ERR_IO_PENDING
, rv
);
4392 // Destroy the transaction before completing the read.
4395 // We have the read and the delete (OnProcessPendingQueue) waiting on the
4396 // message loop. This means that a new transaction will just reuse the same
4397 // active entry (no open or create).
4400 rv
= cache
.http_cache()->CreateTransaction(
4401 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
4402 EXPECT_EQ(net::OK
, rv
);
4404 rv
= c
->trans
->Start(&request
, c
->callback
.callback(), net::BoundNetLog());
4405 EXPECT_EQ(net::ERR_IO_PENDING
, rv
);
4407 MockDiskEntry::IgnoreCallbacks(true);
4408 base::MessageLoop::current()->RunUntilIdle();
4409 MockDiskEntry::IgnoreCallbacks(false);
4411 // The new transaction is waiting for the query range callback.
4414 // And we should not crash when the callback is delivered.
4415 base::MessageLoop::current()->RunUntilIdle();
4417 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
4418 EXPECT_EQ(1, cache
.disk_cache()->open_count());
4419 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4420 RemoveMockTransaction(&kRangeGET_TransactionOK
);
4423 // Tests that an invalid range response results in no cached entry.
4424 TEST(HttpCache
, RangeGET_InvalidResponse1
) {
4425 MockHttpCache cache
;
4426 std::string headers
;
4428 MockTransaction
transaction(kRangeGET_TransactionOK
);
4429 transaction
.handler
= NULL
;
4430 transaction
.response_headers
= "Content-Range: bytes 40-49/45\n"
4431 "Content-Length: 10\n";
4432 AddMockTransaction(&transaction
);
4433 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
4435 std::string
expected(transaction
.status
);
4436 expected
.append("\n");
4437 expected
.append(transaction
.response_headers
);
4438 EXPECT_EQ(expected
, headers
);
4440 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4441 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4442 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4444 // Verify that we don't have a cached entry.
4445 disk_cache::Entry
* entry
;
4446 EXPECT_FALSE(cache
.OpenBackendEntry(kRangeGET_TransactionOK
.url
, &entry
));
4448 RemoveMockTransaction(&kRangeGET_TransactionOK
);
4451 // Tests that we reject a range that doesn't match the content-length.
4452 TEST(HttpCache
, RangeGET_InvalidResponse2
) {
4453 MockHttpCache cache
;
4454 std::string headers
;
4456 MockTransaction
transaction(kRangeGET_TransactionOK
);
4457 transaction
.handler
= NULL
;
4458 transaction
.response_headers
= "Content-Range: bytes 40-49/80\n"
4459 "Content-Length: 20\n";
4460 AddMockTransaction(&transaction
);
4461 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
4463 std::string
expected(transaction
.status
);
4464 expected
.append("\n");
4465 expected
.append(transaction
.response_headers
);
4466 EXPECT_EQ(expected
, headers
);
4468 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4469 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4470 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4472 // Verify that we don't have a cached entry.
4473 disk_cache::Entry
* entry
;
4474 EXPECT_FALSE(cache
.OpenBackendEntry(kRangeGET_TransactionOK
.url
, &entry
));
4476 RemoveMockTransaction(&kRangeGET_TransactionOK
);
4479 // Tests that if a server tells us conflicting information about a resource we
4480 // ignore the response.
4481 TEST(HttpCache
, RangeGET_InvalidResponse3
) {
4482 MockHttpCache cache
;
4483 std::string headers
;
4485 MockTransaction
transaction(kRangeGET_TransactionOK
);
4486 transaction
.handler
= NULL
;
4487 transaction
.request_headers
= "Range: bytes = 50-59\r\n" EXTRA_HEADER
;
4488 std::string
response_headers(transaction
.response_headers
);
4489 response_headers
.append("Content-Range: bytes 50-59/160\n");
4490 transaction
.response_headers
= response_headers
.c_str();
4491 AddMockTransaction(&transaction
);
4492 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
4494 Verify206Response(headers
, 50, 59);
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());
4499 RemoveMockTransaction(&transaction
);
4500 AddMockTransaction(&kRangeGET_TransactionOK
);
4502 // This transaction will report a resource size of 80 bytes, and we think it's
4503 // 160 so we should ignore the response.
4504 RunTransactionTestWithResponse(cache
.http_cache(), kRangeGET_TransactionOK
,
4507 Verify206Response(headers
, 40, 49);
4508 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
4509 EXPECT_EQ(1, cache
.disk_cache()->open_count());
4510 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4512 // Verify that we cached the first response but not the second one.
4513 disk_cache::Entry
* en
;
4514 ASSERT_TRUE(cache
.OpenBackendEntry(kRangeGET_TransactionOK
.url
, &en
));
4516 int64 cached_start
= 0;
4517 net::TestCompletionCallback cb
;
4518 int rv
= en
->GetAvailableRange(40, 20, &cached_start
, cb
.callback());
4519 EXPECT_EQ(10, cb
.GetResult(rv
));
4520 EXPECT_EQ(50, cached_start
);
4523 RemoveMockTransaction(&kRangeGET_TransactionOK
);
4526 // Tests that we handle large range values properly.
4527 TEST(HttpCache
, RangeGET_LargeValues
) {
4528 // We need a real sparse cache for this test.
4529 MockHttpCache
cache(net::HttpCache::DefaultBackend::InMemory(1024 * 1024));
4530 std::string headers
;
4532 MockTransaction
transaction(kRangeGET_TransactionOK
);
4533 transaction
.handler
= NULL
;
4534 transaction
.request_headers
= "Range: bytes = 4294967288-4294967297\r\n"
4536 transaction
.response_headers
=
4538 "Content-Range: bytes 4294967288-4294967297/4294967299\n"
4539 "Content-Length: 10\n";
4540 AddMockTransaction(&transaction
);
4541 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
4543 std::string
expected(transaction
.status
);
4544 expected
.append("\n");
4545 expected
.append(transaction
.response_headers
);
4546 EXPECT_EQ(expected
, headers
);
4548 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4550 // Verify that we have a cached entry.
4551 disk_cache::Entry
* en
;
4552 ASSERT_TRUE(cache
.OpenBackendEntry(kRangeGET_TransactionOK
.url
, &en
));
4555 RemoveMockTransaction(&kRangeGET_TransactionOK
);
4558 // Tests that we don't crash with a range request if the disk cache was not
4559 // initialized properly.
4560 TEST(HttpCache
, RangeGET_NoDiskCache
) {
4561 MockBlockingBackendFactory
* factory
= new MockBlockingBackendFactory();
4562 factory
->set_fail(true);
4563 factory
->FinishCreation(); // We'll complete synchronously.
4564 MockHttpCache
cache(factory
);
4566 AddMockTransaction(&kRangeGET_TransactionOK
);
4568 RunTransactionTest(cache
.http_cache(), kRangeGET_TransactionOK
);
4569 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4571 RemoveMockTransaction(&kRangeGET_TransactionOK
);
4574 // Tests that we handle byte range requests that skip the cache.
4575 TEST(HttpCache
, RangeHEAD
) {
4576 MockHttpCache cache
;
4577 AddMockTransaction(&kRangeGET_TransactionOK
);
4579 MockTransaction
transaction(kRangeGET_TransactionOK
);
4580 transaction
.request_headers
= "Range: bytes = -10\r\n" EXTRA_HEADER
;
4581 transaction
.method
= "HEAD";
4582 transaction
.data
= "rg: 70-79 ";
4584 std::string headers
;
4585 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
4587 Verify206Response(headers
, 70, 79);
4588 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4589 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4590 EXPECT_EQ(0, cache
.disk_cache()->create_count());
4592 RemoveMockTransaction(&kRangeGET_TransactionOK
);
4595 // Tests that we don't crash when after reading from the cache we issue a
4596 // request for the next range and the server gives us a 200 synchronously.
4597 TEST(HttpCache
, RangeGET_FastFlakyServer
) {
4598 MockHttpCache cache
;
4600 MockTransaction
transaction(kRangeGET_TransactionOK
);
4601 transaction
.request_headers
= "Range: bytes = 40-\r\n" EXTRA_HEADER
;
4602 transaction
.test_mode
= TEST_MODE_SYNC_NET_START
;
4603 transaction
.load_flags
|= net::LOAD_VALIDATE_CACHE
;
4604 AddMockTransaction(&transaction
);
4606 // Write to the cache.
4607 RunTransactionTest(cache
.http_cache(), kRangeGET_TransactionOK
);
4609 // And now read from the cache and the network.
4610 RangeTransactionServer handler
;
4611 handler
.set_bad_200(true);
4612 transaction
.data
= "Not a range";
4613 RunTransactionTest(cache
.http_cache(), transaction
);
4615 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
4616 EXPECT_EQ(1, cache
.disk_cache()->open_count());
4617 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4619 RemoveMockTransaction(&transaction
);
4622 // Tests that when the server gives us less data than expected, we don't keep
4623 // asking for more data.
4624 TEST(HttpCache
, RangeGET_FastFlakyServer2
) {
4625 MockHttpCache cache
;
4627 // First, check with an empty cache (WRITE mode).
4628 MockTransaction
transaction(kRangeGET_TransactionOK
);
4629 transaction
.request_headers
= "Range: bytes = 40-49\r\n" EXTRA_HEADER
;
4630 transaction
.data
= "rg: 40-"; // Less than expected.
4631 transaction
.handler
= NULL
;
4632 std::string
headers(transaction
.response_headers
);
4633 headers
.append("Content-Range: bytes 40-49/80\n");
4634 transaction
.response_headers
= headers
.c_str();
4636 AddMockTransaction(&transaction
);
4638 // Write to the cache.
4639 RunTransactionTest(cache
.http_cache(), transaction
);
4641 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4642 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4643 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4645 // Now verify that even in READ_WRITE mode, we forward the bad response to
4647 transaction
.request_headers
= "Range: bytes = 60-69\r\n" EXTRA_HEADER
;
4648 transaction
.data
= "rg: 60-"; // Less than expected.
4649 headers
= kRangeGET_TransactionOK
.response_headers
;
4650 headers
.append("Content-Range: bytes 60-69/80\n");
4651 transaction
.response_headers
= headers
.c_str();
4653 RunTransactionTest(cache
.http_cache(), transaction
);
4655 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
4656 EXPECT_EQ(1, cache
.disk_cache()->open_count());
4657 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4659 RemoveMockTransaction(&transaction
);
4662 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
4663 // This test hits a NOTREACHED so it is a release mode only test.
4664 TEST(HttpCache
, RangeGET_OK_LoadOnlyFromCache
) {
4665 MockHttpCache cache
;
4666 AddMockTransaction(&kRangeGET_TransactionOK
);
4668 // Write to the cache (40-49).
4669 RunTransactionTest(cache
.http_cache(), kRangeGET_TransactionOK
);
4670 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4671 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4672 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4674 // Force this transaction to read from the cache.
4675 MockTransaction
transaction(kRangeGET_TransactionOK
);
4676 transaction
.load_flags
|= net::LOAD_ONLY_FROM_CACHE
;
4678 MockHttpRequest
request(transaction
);
4679 net::TestCompletionCallback callback
;
4681 scoped_ptr
<net::HttpTransaction
> trans
;
4682 int rv
= cache
.http_cache()->CreateTransaction(
4683 net::DEFAULT_PRIORITY
, &trans
, NULL
);
4684 EXPECT_EQ(net::OK
, rv
);
4685 ASSERT_TRUE(trans
.get());
4687 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
4688 if (rv
== net::ERR_IO_PENDING
)
4689 rv
= callback
.WaitForResult();
4690 ASSERT_EQ(net::ERR_CACHE_MISS
, rv
);
4694 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4695 EXPECT_EQ(1, cache
.disk_cache()->open_count());
4696 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4698 RemoveMockTransaction(&kRangeGET_TransactionOK
);
4702 // Tests the handling of the "truncation" flag.
4703 TEST(HttpCache
, WriteResponseInfo_Truncated
) {
4704 MockHttpCache cache
;
4705 disk_cache::Entry
* entry
;
4706 ASSERT_TRUE(cache
.CreateBackendEntry("http://www.google.com", &entry
,
4709 std::string
headers("HTTP/1.1 200 OK");
4710 headers
= net::HttpUtil::AssembleRawHeaders(headers
.data(), headers
.size());
4711 net::HttpResponseInfo response
;
4712 response
.headers
= new net::HttpResponseHeaders(headers
);
4714 // Set the last argument for this to be an incomplete request.
4715 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry
, &response
, true, true));
4716 bool truncated
= false;
4717 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry
, &response
, &truncated
));
4718 EXPECT_TRUE(truncated
);
4720 // And now test the opposite case.
4721 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry
, &response
, true, false));
4723 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry
, &response
, &truncated
));
4724 EXPECT_FALSE(truncated
);
4728 // Tests basic pickling/unpickling of HttpResponseInfo.
4729 TEST(HttpCache
, PersistHttpResponseInfo
) {
4730 // Set some fields (add more if needed.)
4731 net::HttpResponseInfo response1
;
4732 response1
.was_cached
= false;
4733 response1
.socket_address
= net::HostPortPair("1.2.3.4", 80);
4734 response1
.headers
= new net::HttpResponseHeaders("HTTP/1.1 200 OK");
4738 response1
.Persist(&pickle
, false, false);
4741 net::HttpResponseInfo response2
;
4742 bool response_truncated
;
4743 EXPECT_TRUE(response2
.InitFromPickle(pickle
, &response_truncated
));
4744 EXPECT_FALSE(response_truncated
);
4747 EXPECT_TRUE(response2
.was_cached
); // InitFromPickle sets this flag.
4748 EXPECT_EQ("1.2.3.4", response2
.socket_address
.host());
4749 EXPECT_EQ(80, response2
.socket_address
.port());
4750 EXPECT_EQ("HTTP/1.1 200 OK", response2
.headers
->GetStatusLine());
4753 // Tests that we delete an entry when the request is cancelled before starting
4754 // to read from the network.
4755 TEST(HttpCache
, DoomOnDestruction
) {
4756 MockHttpCache cache
;
4758 MockHttpRequest
request(kSimpleGET_Transaction
);
4760 Context
* c
= new Context();
4761 int rv
= cache
.http_cache()->CreateTransaction(
4762 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
4763 EXPECT_EQ(net::OK
, rv
);
4765 rv
= c
->trans
->Start(&request
, c
->callback
.callback(), net::BoundNetLog());
4766 if (rv
== net::ERR_IO_PENDING
)
4767 c
->result
= c
->callback
.WaitForResult();
4769 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4770 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4771 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4773 // Destroy the transaction. We only have the headers so we should delete this
4777 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
4779 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
4780 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4781 EXPECT_EQ(2, cache
.disk_cache()->create_count());
4784 // Tests that we delete an entry when the request is cancelled if the response
4785 // does not have content-length and strong validators.
4786 TEST(HttpCache
, DoomOnDestruction2
) {
4787 MockHttpCache cache
;
4789 MockHttpRequest
request(kSimpleGET_Transaction
);
4791 Context
* c
= new Context();
4792 int rv
= cache
.http_cache()->CreateTransaction(
4793 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
4794 EXPECT_EQ(net::OK
, rv
);
4796 rv
= c
->trans
->Start(&request
, c
->callback
.callback(), net::BoundNetLog());
4797 if (rv
== net::ERR_IO_PENDING
)
4798 rv
= c
->callback
.WaitForResult();
4800 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4801 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4802 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4804 // Make sure that the entry has some data stored.
4805 scoped_refptr
<net::IOBufferWithSize
> buf(new net::IOBufferWithSize(10));
4806 rv
= c
->trans
->Read(buf
.get(), buf
->size(), c
->callback
.callback());
4807 if (rv
== net::ERR_IO_PENDING
)
4808 rv
= c
->callback
.WaitForResult();
4809 EXPECT_EQ(buf
->size(), rv
);
4811 // Destroy the transaction.
4814 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
4816 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
4817 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4818 EXPECT_EQ(2, cache
.disk_cache()->create_count());
4821 // Tests that we delete an entry when the request is cancelled if the response
4822 // has an "Accept-Ranges: none" header.
4823 TEST(HttpCache
, DoomOnDestruction3
) {
4824 MockHttpCache cache
;
4826 MockTransaction
transaction(kSimpleGET_Transaction
);
4827 transaction
.response_headers
=
4828 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
4829 "Content-Length: 22\n"
4830 "Accept-Ranges: none\n"
4831 "Etag: \"foopy\"\n";
4832 AddMockTransaction(&transaction
);
4833 MockHttpRequest
request(transaction
);
4835 Context
* c
= new Context();
4836 int rv
= cache
.http_cache()->CreateTransaction(
4837 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
4838 EXPECT_EQ(net::OK
, rv
);
4840 rv
= c
->trans
->Start(&request
, c
->callback
.callback(), net::BoundNetLog());
4841 if (rv
== net::ERR_IO_PENDING
)
4842 rv
= c
->callback
.WaitForResult();
4844 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4845 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4846 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4848 // Make sure that the entry has some data stored.
4849 scoped_refptr
<net::IOBufferWithSize
> buf(new net::IOBufferWithSize(10));
4850 rv
= c
->trans
->Read(buf
.get(), buf
->size(), c
->callback
.callback());
4851 if (rv
== net::ERR_IO_PENDING
)
4852 rv
= c
->callback
.WaitForResult();
4853 EXPECT_EQ(buf
->size(), rv
);
4855 // Destroy the transaction.
4858 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
4860 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
4861 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4862 EXPECT_EQ(2, cache
.disk_cache()->create_count());
4864 RemoveMockTransaction(&transaction
);
4867 // Tests that we mark an entry as incomplete when the request is cancelled.
4868 TEST(HttpCache
, SetTruncatedFlag
) {
4869 MockHttpCache cache
;
4871 MockTransaction
transaction(kSimpleGET_Transaction
);
4872 transaction
.response_headers
=
4873 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
4874 "Content-Length: 22\n"
4875 "Etag: \"foopy\"\n";
4876 AddMockTransaction(&transaction
);
4877 MockHttpRequest
request(transaction
);
4879 scoped_ptr
<Context
> c(new Context());
4880 // We use a test delegate to ensure that after initiating destruction
4881 // of the transaction, no further delegate callbacks happen.
4882 // We initialize the TestHttpTransactionDelegate with the correct number of
4883 // cache actions and network actions to be reported.
4884 scoped_ptr
<TestHttpTransactionDelegate
> delegate(
4885 new TestHttpTransactionDelegate(7, 3));
4886 int rv
= cache
.http_cache()->CreateTransaction(
4887 net::DEFAULT_PRIORITY
, &c
->trans
, delegate
.get());
4888 EXPECT_EQ(net::OK
, rv
);
4890 rv
= c
->trans
->Start(&request
, c
->callback
.callback(), net::BoundNetLog());
4891 if (rv
== net::ERR_IO_PENDING
)
4892 rv
= c
->callback
.WaitForResult();
4894 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
4895 EXPECT_EQ(0, cache
.disk_cache()->open_count());
4896 EXPECT_EQ(1, cache
.disk_cache()->create_count());
4898 // Make sure that the entry has some data stored.
4899 scoped_refptr
<net::IOBufferWithSize
> buf(new net::IOBufferWithSize(10));
4900 rv
= c
->trans
->Read(buf
.get(), buf
->size(), c
->callback
.callback());
4901 if (rv
== net::ERR_IO_PENDING
)
4902 rv
= c
->callback
.WaitForResult();
4903 EXPECT_EQ(buf
->size(), rv
);
4905 // We want to cancel the request when the transaction is busy.
4906 rv
= c
->trans
->Read(buf
.get(), buf
->size(), c
->callback
.callback());
4907 EXPECT_EQ(net::ERR_IO_PENDING
, rv
);
4908 EXPECT_FALSE(c
->callback
.have_result());
4910 MockHttpCache::SetTestMode(TEST_MODE_SYNC_ALL
);
4911 int num_delegate_callbacks_before_destruction
=
4912 delegate
->num_callbacks_observed();
4914 // Destroy the transaction.
4916 MockHttpCache::SetTestMode(0);
4918 // Ensure the delegate received no callbacks during destruction.
4919 EXPECT_EQ(num_delegate_callbacks_before_destruction
,
4920 delegate
->num_callbacks_observed());
4922 // Since the transaction was aborted in the middle of network I/O, we will
4923 // manually call the delegate so that its pending I/O operation will be
4924 // closed (which is what the test delegate is expecting).
4925 delegate
->OnNetworkActionFinish();
4927 // Make sure that we don't invoke the callback. We may have an issue if the
4928 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we
4929 // could end up with the transaction being deleted twice if we send any
4930 // notification from the transaction destructor (see http://crbug.com/31723).
4931 EXPECT_FALSE(c
->callback
.have_result());
4933 // Verify that the entry is marked as incomplete.
4934 disk_cache::Entry
* entry
;
4935 ASSERT_TRUE(cache
.OpenBackendEntry(kSimpleGET_Transaction
.url
, &entry
));
4936 net::HttpResponseInfo response
;
4937 bool truncated
= false;
4938 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry
, &response
, &truncated
));
4939 EXPECT_TRUE(truncated
);
4942 RemoveMockTransaction(&transaction
);
4945 // Tests that we don't mark an entry as truncated when we read everything.
4946 TEST(HttpCache
, DontSetTruncatedFlag
) {
4947 MockHttpCache cache
;
4949 MockTransaction
transaction(kSimpleGET_Transaction
);
4950 transaction
.response_headers
=
4951 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
4952 "Content-Length: 22\n"
4953 "Etag: \"foopy\"\n";
4954 AddMockTransaction(&transaction
);
4955 MockHttpRequest
request(transaction
);
4957 scoped_ptr
<Context
> c(new Context());
4958 int rv
= cache
.http_cache()->CreateTransaction(
4959 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
4960 EXPECT_EQ(net::OK
, rv
);
4962 rv
= c
->trans
->Start(&request
, c
->callback
.callback(), net::BoundNetLog());
4963 EXPECT_EQ(net::OK
, c
->callback
.GetResult(rv
));
4966 scoped_refptr
<net::IOBufferWithSize
> buf(new net::IOBufferWithSize(22));
4967 rv
= c
->trans
->Read(buf
.get(), buf
->size(), c
->callback
.callback());
4968 EXPECT_EQ(buf
->size(), c
->callback
.GetResult(rv
));
4970 // Destroy the transaction.
4973 // Verify that the entry is not marked as truncated.
4974 disk_cache::Entry
* entry
;
4975 ASSERT_TRUE(cache
.OpenBackendEntry(kSimpleGET_Transaction
.url
, &entry
));
4976 net::HttpResponseInfo response
;
4977 bool truncated
= true;
4978 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry
, &response
, &truncated
));
4979 EXPECT_FALSE(truncated
);
4982 RemoveMockTransaction(&transaction
);
4985 // Tests that we can continue with a request that was interrupted.
4986 TEST(HttpCache
, GET_IncompleteResource
) {
4987 MockHttpCache cache
;
4988 AddMockTransaction(&kRangeGET_TransactionOK
);
4990 std::string
raw_headers("HTTP/1.1 200 OK\n"
4991 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
4993 "Accept-Ranges: bytes\n"
4994 "Content-Length: 80\n");
4995 CreateTruncatedEntry(raw_headers
, &cache
);
4997 // Now make a regular request.
4998 std::string headers
;
4999 MockTransaction
transaction(kRangeGET_TransactionOK
);
5000 transaction
.request_headers
= EXTRA_HEADER
;
5001 transaction
.data
= "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
5002 "rg: 50-59 rg: 60-69 rg: 70-79 ";
5003 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
5005 // We update the headers with the ones received while revalidating.
5006 std::string
expected_headers(
5008 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
5009 "Accept-Ranges: bytes\n"
5011 "Content-Length: 80\n");
5013 EXPECT_EQ(expected_headers
, headers
);
5014 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
5015 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5016 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5018 // Verify that the disk entry was updated.
5019 disk_cache::Entry
* entry
;
5020 ASSERT_TRUE(cache
.OpenBackendEntry(kRangeGET_TransactionOK
.url
, &entry
));
5021 EXPECT_EQ(80, entry
->GetDataSize(1));
5022 bool truncated
= true;
5023 net::HttpResponseInfo response
;
5024 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry
, &response
, &truncated
));
5025 EXPECT_FALSE(truncated
);
5028 RemoveMockTransaction(&kRangeGET_TransactionOK
);
5031 // Tests the handling of no-store when revalidating a truncated entry.
5032 TEST(HttpCache
, GET_IncompleteResource_NoStore
) {
5033 MockHttpCache cache
;
5034 AddMockTransaction(&kRangeGET_TransactionOK
);
5036 std::string
raw_headers("HTTP/1.1 200 OK\n"
5037 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
5039 "Accept-Ranges: bytes\n"
5040 "Content-Length: 80\n");
5041 CreateTruncatedEntry(raw_headers
, &cache
);
5042 RemoveMockTransaction(&kRangeGET_TransactionOK
);
5044 // Now make a regular request.
5045 MockTransaction
transaction(kRangeGET_TransactionOK
);
5046 transaction
.request_headers
= EXTRA_HEADER
;
5047 std::string
response_headers(transaction
.response_headers
);
5048 response_headers
+= ("Cache-Control: no-store\n");
5049 transaction
.response_headers
= response_headers
.c_str();
5050 transaction
.data
= "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
5051 "rg: 50-59 rg: 60-69 rg: 70-79 ";
5052 AddMockTransaction(&transaction
);
5054 std::string headers
;
5055 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
5057 // We update the headers with the ones received while revalidating.
5058 std::string
expected_headers(
5060 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
5061 "Accept-Ranges: bytes\n"
5062 "Cache-Control: no-store\n"
5064 "Content-Length: 80\n");
5066 EXPECT_EQ(expected_headers
, headers
);
5067 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
5068 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5069 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5071 // Verify that the disk entry was deleted.
5072 disk_cache::Entry
* entry
;
5073 EXPECT_FALSE(cache
.OpenBackendEntry(kRangeGET_TransactionOK
.url
, &entry
));
5074 RemoveMockTransaction(&transaction
);
5077 // Tests cancelling a request after the server sent no-store.
5078 TEST(HttpCache
, GET_IncompleteResource_Cancel
) {
5079 MockHttpCache cache
;
5080 AddMockTransaction(&kRangeGET_TransactionOK
);
5082 std::string
raw_headers("HTTP/1.1 200 OK\n"
5083 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
5085 "Accept-Ranges: bytes\n"
5086 "Content-Length: 80\n");
5087 CreateTruncatedEntry(raw_headers
, &cache
);
5088 RemoveMockTransaction(&kRangeGET_TransactionOK
);
5090 // Now make a regular request.
5091 MockTransaction
transaction(kRangeGET_TransactionOK
);
5092 transaction
.request_headers
= EXTRA_HEADER
;
5093 std::string
response_headers(transaction
.response_headers
);
5094 response_headers
+= ("Cache-Control: no-store\n");
5095 transaction
.response_headers
= response_headers
.c_str();
5096 transaction
.data
= "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
5097 "rg: 50-59 rg: 60-69 rg: 70-79 ";
5098 AddMockTransaction(&transaction
);
5100 MockHttpRequest
request(transaction
);
5101 Context
* c
= new Context();
5103 int rv
= cache
.http_cache()->CreateTransaction(
5104 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
);
5105 EXPECT_EQ(net::OK
, rv
);
5107 // Queue another request to this transaction. We have to start this request
5108 // before the first one gets the response from the server and dooms the entry,
5109 // otherwise it will just create a new entry without being queued to the first
5111 Context
* pending
= new Context();
5113 cache
.http_cache()->CreateTransaction(
5114 net::DEFAULT_PRIORITY
, &pending
->trans
, NULL
));
5116 rv
= c
->trans
->Start(&request
, c
->callback
.callback(), net::BoundNetLog());
5117 EXPECT_EQ(net::ERR_IO_PENDING
,
5118 pending
->trans
->Start(&request
, pending
->callback
.callback(),
5119 net::BoundNetLog()));
5120 EXPECT_EQ(net::OK
, c
->callback
.GetResult(rv
));
5122 // Make sure that the entry has some data stored.
5123 scoped_refptr
<net::IOBufferWithSize
> buf(new net::IOBufferWithSize(5));
5124 rv
= c
->trans
->Read(buf
.get(), buf
->size(), c
->callback
.callback());
5125 EXPECT_EQ(5, c
->callback
.GetResult(rv
));
5127 // Cancel the requests.
5131 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5132 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5133 EXPECT_EQ(2, cache
.disk_cache()->create_count());
5135 base::MessageLoop::current()->RunUntilIdle();
5136 RemoveMockTransaction(&transaction
);
5139 // Tests that we delete truncated entries if the server changes its mind midway.
5140 TEST(HttpCache
, GET_IncompleteResource2
) {
5141 MockHttpCache cache
;
5142 AddMockTransaction(&kRangeGET_TransactionOK
);
5144 // Content-length will be intentionally bad.
5145 std::string
raw_headers("HTTP/1.1 200 OK\n"
5146 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
5148 "Accept-Ranges: bytes\n"
5149 "Content-Length: 50\n");
5150 CreateTruncatedEntry(raw_headers
, &cache
);
5152 // Now make a regular request. We expect the code to fail the validation and
5153 // retry the request without using byte ranges.
5154 std::string headers
;
5155 MockTransaction
transaction(kRangeGET_TransactionOK
);
5156 transaction
.request_headers
= EXTRA_HEADER
;
5157 transaction
.data
= "Not a range";
5158 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
5160 // The server will return 200 instead of a byte range.
5161 std::string
expected_headers(
5163 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n");
5165 EXPECT_EQ(expected_headers
, headers
);
5166 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
5167 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5168 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5170 // Verify that the disk entry was deleted.
5171 disk_cache::Entry
* entry
;
5172 ASSERT_FALSE(cache
.OpenBackendEntry(kRangeGET_TransactionOK
.url
, &entry
));
5173 RemoveMockTransaction(&kRangeGET_TransactionOK
);
5176 // Tests that we always validate a truncated request.
5177 TEST(HttpCache
, GET_IncompleteResource3
) {
5178 MockHttpCache cache
;
5179 AddMockTransaction(&kRangeGET_TransactionOK
);
5181 // This should not require validation for 10 hours.
5182 std::string
raw_headers("HTTP/1.1 200 OK\n"
5183 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
5185 "Cache-Control: max-age= 36000\n"
5186 "Accept-Ranges: bytes\n"
5187 "Content-Length: 80\n");
5188 CreateTruncatedEntry(raw_headers
, &cache
);
5190 // Now make a regular request.
5191 std::string headers
;
5192 MockTransaction
transaction(kRangeGET_TransactionOK
);
5193 transaction
.request_headers
= EXTRA_HEADER
;
5194 transaction
.data
= "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
5195 "rg: 50-59 rg: 60-69 rg: 70-79 ";
5197 scoped_ptr
<Context
> c(new Context
);
5198 EXPECT_EQ(net::OK
, cache
.http_cache()->CreateTransaction(
5199 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
));
5201 MockHttpRequest
request(transaction
);
5202 int rv
= c
->trans
->Start(
5203 &request
, c
->callback
.callback(), net::BoundNetLog());
5204 EXPECT_EQ(net::OK
, c
->callback
.GetResult(rv
));
5206 // We should have checked with the server before finishing Start().
5207 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5208 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5209 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5211 RemoveMockTransaction(&kRangeGET_TransactionOK
);
5214 // Tests that we cache a 200 response to the validation request.
5215 TEST(HttpCache
, GET_IncompleteResource4
) {
5216 MockHttpCache cache
;
5217 AddMockTransaction(&kRangeGET_TransactionOK
);
5219 std::string
raw_headers("HTTP/1.1 200 OK\n"
5220 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
5222 "Accept-Ranges: bytes\n"
5223 "Content-Length: 80\n");
5224 CreateTruncatedEntry(raw_headers
, &cache
);
5226 // Now make a regular request.
5227 std::string headers
;
5228 MockTransaction
transaction(kRangeGET_TransactionOK
);
5229 transaction
.request_headers
= EXTRA_HEADER
;
5230 transaction
.data
= "Not a range";
5231 RangeTransactionServer handler
;
5232 handler
.set_bad_200(true);
5233 RunTransactionTestWithResponse(cache
.http_cache(), transaction
, &headers
);
5235 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5236 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5237 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5239 // Verify that the disk entry was updated.
5240 disk_cache::Entry
* entry
;
5241 ASSERT_TRUE(cache
.OpenBackendEntry(kRangeGET_TransactionOK
.url
, &entry
));
5242 EXPECT_EQ(11, entry
->GetDataSize(1));
5243 bool truncated
= true;
5244 net::HttpResponseInfo response
;
5245 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry
, &response
, &truncated
));
5246 EXPECT_FALSE(truncated
);
5249 RemoveMockTransaction(&kRangeGET_TransactionOK
);
5252 // Tests that when we cancel a request that was interrupted, we mark it again
5254 TEST(HttpCache
, GET_CancelIncompleteResource
) {
5255 MockHttpCache cache
;
5256 AddMockTransaction(&kRangeGET_TransactionOK
);
5258 std::string
raw_headers("HTTP/1.1 200 OK\n"
5259 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
5261 "Accept-Ranges: bytes\n"
5262 "Content-Length: 80\n");
5263 CreateTruncatedEntry(raw_headers
, &cache
);
5265 // Now make a regular request.
5266 MockTransaction
transaction(kRangeGET_TransactionOK
);
5267 transaction
.request_headers
= EXTRA_HEADER
;
5269 MockHttpRequest
request(transaction
);
5270 Context
* c
= new Context();
5271 EXPECT_EQ(net::OK
, cache
.http_cache()->CreateTransaction(
5272 net::DEFAULT_PRIORITY
, &c
->trans
, NULL
));
5274 int rv
= c
->trans
->Start(
5275 &request
, c
->callback
.callback(), net::BoundNetLog());
5276 EXPECT_EQ(net::OK
, c
->callback
.GetResult(rv
));
5278 // Read 20 bytes from the cache, and 10 from the net.
5279 scoped_refptr
<net::IOBuffer
> buf(new net::IOBuffer(100));
5280 rv
= c
->trans
->Read(buf
.get(), 20, c
->callback
.callback());
5281 EXPECT_EQ(20, c
->callback
.GetResult(rv
));
5282 rv
= c
->trans
->Read(buf
.get(), 10, c
->callback
.callback());
5283 EXPECT_EQ(10, c
->callback
.GetResult(rv
));
5285 // At this point, we are already reading so canceling the request should leave
5289 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
5290 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5291 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5293 // Verify that the disk entry was updated: now we have 30 bytes.
5294 disk_cache::Entry
* entry
;
5295 ASSERT_TRUE(cache
.OpenBackendEntry(kRangeGET_TransactionOK
.url
, &entry
));
5296 EXPECT_EQ(30, entry
->GetDataSize(1));
5297 bool truncated
= false;
5298 net::HttpResponseInfo response
;
5299 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry
, &response
, &truncated
));
5300 EXPECT_TRUE(truncated
);
5302 RemoveMockTransaction(&kRangeGET_TransactionOK
);
5305 // Tests that we can handle range requests when we have a truncated entry.
5306 TEST(HttpCache
, RangeGET_IncompleteResource
) {
5307 MockHttpCache cache
;
5308 AddMockTransaction(&kRangeGET_TransactionOK
);
5310 // Content-length will be intentionally bogus.
5311 std::string
raw_headers("HTTP/1.1 200 OK\n"
5312 "Last-Modified: something\n"
5314 "Accept-Ranges: bytes\n"
5315 "Content-Length: 10\n");
5316 CreateTruncatedEntry(raw_headers
, &cache
);
5318 // Now make a range request.
5319 std::string headers
;
5320 RunTransactionTestWithResponse(cache
.http_cache(), kRangeGET_TransactionOK
,
5323 Verify206Response(headers
, 40, 49);
5324 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5325 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5326 EXPECT_EQ(2, cache
.disk_cache()->create_count());
5328 RemoveMockTransaction(&kRangeGET_TransactionOK
);
5331 TEST(HttpCache
, SyncRead
) {
5332 MockHttpCache cache
;
5334 // This test ensures that a read that completes synchronously does not cause
5337 ScopedMockTransaction
transaction(kSimpleGET_Transaction
);
5338 transaction
.test_mode
|= (TEST_MODE_SYNC_CACHE_START
|
5339 TEST_MODE_SYNC_CACHE_READ
|
5340 TEST_MODE_SYNC_CACHE_WRITE
);
5342 MockHttpRequest
r1(transaction
),
5346 TestTransactionConsumer
c1(net::DEFAULT_PRIORITY
, cache
.http_cache()),
5347 c2(net::DEFAULT_PRIORITY
, cache
.http_cache()),
5348 c3(net::DEFAULT_PRIORITY
, cache
.http_cache());
5350 c1
.Start(&r1
, net::BoundNetLog());
5352 r2
.load_flags
|= net::LOAD_ONLY_FROM_CACHE
;
5353 c2
.Start(&r2
, net::BoundNetLog());
5355 r3
.load_flags
|= net::LOAD_ONLY_FROM_CACHE
;
5356 c3
.Start(&r3
, net::BoundNetLog());
5358 base::MessageLoop::current()->Run();
5360 EXPECT_TRUE(c1
.is_done());
5361 EXPECT_TRUE(c2
.is_done());
5362 EXPECT_TRUE(c3
.is_done());
5364 EXPECT_EQ(net::OK
, c1
.error());
5365 EXPECT_EQ(net::OK
, c2
.error());
5366 EXPECT_EQ(net::OK
, c3
.error());
5369 TEST(HttpCache
, ValidationResultsIn200
) {
5370 MockHttpCache cache
;
5372 // This test ensures that a conditional request, which results in a 200
5373 // instead of a 304, properly truncates the existing response data.
5375 // write to the cache
5376 RunTransactionTest(cache
.http_cache(), kETagGET_Transaction
);
5378 // force this transaction to validate the cache
5379 MockTransaction
transaction(kETagGET_Transaction
);
5380 transaction
.load_flags
|= net::LOAD_VALIDATE_CACHE
;
5381 RunTransactionTest(cache
.http_cache(), transaction
);
5383 // read from the cache
5384 RunTransactionTest(cache
.http_cache(), kETagGET_Transaction
);
5387 TEST(HttpCache
, CachedRedirect
) {
5388 MockHttpCache cache
;
5390 ScopedMockTransaction
kTestTransaction(kSimpleGET_Transaction
);
5391 kTestTransaction
.status
= "HTTP/1.1 301 Moved Permanently";
5392 kTestTransaction
.response_headers
= "Location: http://www.bar.com/\n";
5394 MockHttpRequest
request(kTestTransaction
);
5395 net::TestCompletionCallback callback
;
5397 // Write to the cache.
5399 scoped_ptr
<net::HttpTransaction
> trans
;
5400 int rv
= cache
.http_cache()->CreateTransaction(
5401 net::DEFAULT_PRIORITY
, &trans
, NULL
);
5402 EXPECT_EQ(net::OK
, rv
);
5403 ASSERT_TRUE(trans
.get());
5405 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
5406 if (rv
== net::ERR_IO_PENDING
)
5407 rv
= callback
.WaitForResult();
5408 ASSERT_EQ(net::OK
, rv
);
5410 const net::HttpResponseInfo
* info
= trans
->GetResponseInfo();
5413 EXPECT_EQ(info
->headers
->response_code(), 301);
5415 std::string location
;
5416 info
->headers
->EnumerateHeader(NULL
, "Location", &location
);
5417 EXPECT_EQ(location
, "http://www.bar.com/");
5419 // Mark the transaction as completed so it is cached.
5420 trans
->DoneReading();
5422 // Destroy transaction when going out of scope. We have not actually
5423 // read the response body -- want to test that it is still getting cached.
5425 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5426 EXPECT_EQ(0, cache
.disk_cache()->open_count());
5427 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5429 // Active entries in the cache are not retired synchronously. Make
5430 // sure the next run hits the MockHttpCache and open_count is
5432 base::MessageLoop::current()->RunUntilIdle();
5434 // Read from the cache.
5436 scoped_ptr
<net::HttpTransaction
> trans
;
5437 int rv
= cache
.http_cache()->CreateTransaction(
5438 net::DEFAULT_PRIORITY
, &trans
, NULL
);
5439 EXPECT_EQ(net::OK
, rv
);
5440 ASSERT_TRUE(trans
.get());
5442 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
5443 if (rv
== net::ERR_IO_PENDING
)
5444 rv
= callback
.WaitForResult();
5445 ASSERT_EQ(net::OK
, rv
);
5447 const net::HttpResponseInfo
* info
= trans
->GetResponseInfo();
5450 EXPECT_EQ(info
->headers
->response_code(), 301);
5452 std::string location
;
5453 info
->headers
->EnumerateHeader(NULL
, "Location", &location
);
5454 EXPECT_EQ(location
, "http://www.bar.com/");
5456 // Mark the transaction as completed so it is cached.
5457 trans
->DoneReading();
5459 // Destroy transaction when going out of scope. We have not actually
5460 // read the response body -- want to test that it is still getting cached.
5462 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5463 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5464 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5467 // Verify that no-cache resources are stored in cache, but are not fetched from
5468 // cache during normal loads.
5469 TEST(HttpCache
, CacheControlNoCacheNormalLoad
) {
5470 MockHttpCache cache
;
5472 ScopedMockTransaction
transaction(kSimpleGET_Transaction
);
5473 transaction
.response_headers
= "cache-control: no-cache\n";
5476 RunTransactionTest(cache
.http_cache(), transaction
);
5478 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5479 EXPECT_EQ(0, cache
.disk_cache()->open_count());
5480 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5482 // Try loading again; it should result in a network fetch.
5483 RunTransactionTest(cache
.http_cache(), transaction
);
5485 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
5486 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5487 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5489 disk_cache::Entry
* entry
;
5490 EXPECT_TRUE(cache
.OpenBackendEntry(transaction
.url
, &entry
));
5494 // Verify that no-cache resources are stored in cache and fetched from cache
5495 // when the LOAD_PREFERRING_CACHE flag is set.
5496 TEST(HttpCache
, CacheControlNoCacheHistoryLoad
) {
5497 MockHttpCache cache
;
5499 ScopedMockTransaction
transaction(kSimpleGET_Transaction
);
5500 transaction
.response_headers
= "cache-control: no-cache\n";
5503 RunTransactionTest(cache
.http_cache(), transaction
);
5505 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5506 EXPECT_EQ(0, cache
.disk_cache()->open_count());
5507 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5509 // Try loading again with LOAD_PREFERRING_CACHE.
5510 transaction
.load_flags
= net::LOAD_PREFERRING_CACHE
;
5511 RunTransactionTest(cache
.http_cache(), transaction
);
5513 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5514 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5515 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5517 disk_cache::Entry
* entry
;
5518 EXPECT_TRUE(cache
.OpenBackendEntry(transaction
.url
, &entry
));
5522 TEST(HttpCache
, CacheControlNoStore
) {
5523 MockHttpCache cache
;
5525 ScopedMockTransaction
transaction(kSimpleGET_Transaction
);
5526 transaction
.response_headers
= "cache-control: no-store\n";
5529 RunTransactionTest(cache
.http_cache(), transaction
);
5531 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5532 EXPECT_EQ(0, cache
.disk_cache()->open_count());
5533 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5535 // try loading again; it should result in a network fetch
5536 RunTransactionTest(cache
.http_cache(), transaction
);
5538 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
5539 EXPECT_EQ(0, cache
.disk_cache()->open_count());
5540 EXPECT_EQ(2, cache
.disk_cache()->create_count());
5542 disk_cache::Entry
* entry
;
5543 EXPECT_FALSE(cache
.OpenBackendEntry(transaction
.url
, &entry
));
5546 TEST(HttpCache
, CacheControlNoStore2
) {
5547 // this test is similar to the above test, except that the initial response
5548 // is cachable, but when it is validated, no-store is received causing the
5549 // cached document to be deleted.
5550 MockHttpCache cache
;
5552 ScopedMockTransaction
transaction(kETagGET_Transaction
);
5555 RunTransactionTest(cache
.http_cache(), transaction
);
5557 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5558 EXPECT_EQ(0, cache
.disk_cache()->open_count());
5559 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5561 // try loading again; it should result in a network fetch
5562 transaction
.load_flags
= net::LOAD_VALIDATE_CACHE
;
5563 transaction
.response_headers
= "cache-control: no-store\n";
5564 RunTransactionTest(cache
.http_cache(), transaction
);
5566 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
5567 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5568 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5570 disk_cache::Entry
* entry
;
5571 EXPECT_FALSE(cache
.OpenBackendEntry(transaction
.url
, &entry
));
5574 TEST(HttpCache
, CacheControlNoStore3
) {
5575 // this test is similar to the above test, except that the response is a 304
5576 // instead of a 200. this should never happen in practice, but it seems like
5577 // a good thing to verify that we still destroy the cache entry.
5578 MockHttpCache cache
;
5580 ScopedMockTransaction
transaction(kETagGET_Transaction
);
5583 RunTransactionTest(cache
.http_cache(), transaction
);
5585 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5586 EXPECT_EQ(0, cache
.disk_cache()->open_count());
5587 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5589 // try loading again; it should result in a network fetch
5590 transaction
.load_flags
= net::LOAD_VALIDATE_CACHE
;
5591 transaction
.response_headers
= "cache-control: no-store\n";
5592 transaction
.status
= "HTTP/1.1 304 Not Modified";
5593 RunTransactionTest(cache
.http_cache(), transaction
);
5595 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
5596 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5597 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5599 disk_cache::Entry
* entry
;
5600 EXPECT_FALSE(cache
.OpenBackendEntry(transaction
.url
, &entry
));
5603 // Ensure that we don't cache requests served over bad HTTPS.
5604 TEST(HttpCache
, SimpleGET_SSLError
) {
5605 MockHttpCache cache
;
5607 MockTransaction transaction
= kSimpleGET_Transaction
;
5608 transaction
.cert_status
= net::CERT_STATUS_REVOKED
;
5609 ScopedMockTransaction
scoped_transaction(transaction
);
5611 // write to the cache
5612 RunTransactionTest(cache
.http_cache(), transaction
);
5614 // Test that it was not cached.
5615 transaction
.load_flags
|= net::LOAD_ONLY_FROM_CACHE
;
5617 MockHttpRequest
request(transaction
);
5618 net::TestCompletionCallback callback
;
5620 scoped_ptr
<net::HttpTransaction
> trans
;
5621 int rv
= cache
.http_cache()->CreateTransaction(
5622 net::DEFAULT_PRIORITY
, &trans
, NULL
);
5623 EXPECT_EQ(net::OK
, rv
);
5624 ASSERT_TRUE(trans
.get());
5626 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
5627 if (rv
== net::ERR_IO_PENDING
)
5628 rv
= callback
.WaitForResult();
5629 ASSERT_EQ(net::ERR_CACHE_MISS
, rv
);
5632 // Ensure that we don't crash by if left-behind transactions.
5633 TEST(HttpCache
, OutlivedTransactions
) {
5634 MockHttpCache
* cache
= new MockHttpCache
;
5636 scoped_ptr
<net::HttpTransaction
> trans
;
5637 int rv
= cache
->http_cache()->CreateTransaction(
5638 net::DEFAULT_PRIORITY
, &trans
, NULL
);
5639 EXPECT_EQ(net::OK
, rv
);
5645 // Test that the disabled mode works.
5646 TEST(HttpCache
, CacheDisabledMode
) {
5647 MockHttpCache cache
;
5649 // write to the cache
5650 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
5652 // go into disabled mode
5653 cache
.http_cache()->set_mode(net::HttpCache::DISABLE
);
5655 // force this transaction to write to the cache again
5656 MockTransaction
transaction(kSimpleGET_Transaction
);
5658 RunTransactionTest(cache
.http_cache(), transaction
);
5660 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
5661 EXPECT_EQ(0, cache
.disk_cache()->open_count());
5662 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5665 // Other tests check that the response headers of the cached response
5666 // get updated on 304. Here we specifically check that the
5667 // HttpResponseHeaders::request_time and HttpResponseHeaders::response_time
5668 // fields also gets updated.
5669 // http://crbug.com/20594.
5670 TEST(HttpCache
, UpdatesRequestResponseTimeOn304
) {
5671 MockHttpCache cache
;
5673 const char* kUrl
= "http://foobar";
5674 const char* kData
= "body";
5676 MockTransaction mock_network_response
= { 0 };
5677 mock_network_response
.url
= kUrl
;
5679 AddMockTransaction(&mock_network_response
);
5681 // Request |kUrl|, causing |kNetResponse1| to be written to the cache.
5683 MockTransaction request
= { 0 };
5685 request
.method
= "GET";
5686 request
.request_headers
= "\r\n";
5687 request
.data
= kData
;
5689 static const Response kNetResponse1
= {
5691 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
5692 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
5696 kNetResponse1
.AssignTo(&mock_network_response
);
5698 RunTransactionTest(cache
.http_cache(), request
);
5700 // Request |kUrl| again, this time validating the cache and getting
5703 request
.load_flags
= net::LOAD_VALIDATE_CACHE
;
5705 static const Response kNetResponse2
= {
5706 "HTTP/1.1 304 Not Modified",
5707 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n",
5711 kNetResponse2
.AssignTo(&mock_network_response
);
5713 base::Time request_time
= base::Time() + base::TimeDelta::FromHours(1234);
5714 base::Time response_time
= base::Time() + base::TimeDelta::FromHours(1235);
5716 mock_network_response
.request_time
= request_time
;
5717 mock_network_response
.response_time
= response_time
;
5719 net::HttpResponseInfo response
;
5720 RunTransactionTestWithResponseInfo(cache
.http_cache(), request
, &response
);
5722 // The request and response times should have been updated.
5723 EXPECT_EQ(request_time
.ToInternalValue(),
5724 response
.request_time
.ToInternalValue());
5725 EXPECT_EQ(response_time
.ToInternalValue(),
5726 response
.response_time
.ToInternalValue());
5728 std::string headers
;
5729 response
.headers
->GetNormalizedHeaders(&headers
);
5731 EXPECT_EQ("HTTP/1.1 200 OK\n"
5732 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
5733 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
5736 RemoveMockTransaction(&mock_network_response
);
5739 // Tests that we can write metadata to an entry.
5740 TEST(HttpCache
, WriteMetadata_OK
) {
5741 MockHttpCache cache
;
5743 // Write to the cache
5744 net::HttpResponseInfo response
;
5745 RunTransactionTestWithResponseInfo(cache
.http_cache(), kSimpleGET_Transaction
,
5747 EXPECT_TRUE(response
.metadata
.get() == NULL
);
5750 cache
.http_cache()->WriteMetadata(GURL("foo"), net::DEFAULT_PRIORITY
,
5751 Time::Now(), NULL
, 0);
5753 // Write meta data to the same entry.
5754 scoped_refptr
<net::IOBufferWithSize
> buf(new net::IOBufferWithSize(50));
5755 memset(buf
->data(), 0, buf
->size());
5756 base::strlcpy(buf
->data(), "Hi there", buf
->size());
5757 cache
.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction
.url
),
5758 net::DEFAULT_PRIORITY
,
5759 response
.response_time
,
5763 // Release the buffer before the operation takes place.
5766 // Makes sure we finish pending operations.
5767 base::MessageLoop::current()->RunUntilIdle();
5769 RunTransactionTestWithResponseInfo(cache
.http_cache(), kSimpleGET_Transaction
,
5771 ASSERT_TRUE(response
.metadata
.get() != NULL
);
5772 EXPECT_EQ(50, response
.metadata
->size());
5773 EXPECT_EQ(0, strcmp(response
.metadata
->data(), "Hi there"));
5775 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5776 EXPECT_EQ(2, cache
.disk_cache()->open_count());
5777 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5780 // Tests that we only write metadata to an entry if the time stamp matches.
5781 TEST(HttpCache
, WriteMetadata_Fail
) {
5782 MockHttpCache cache
;
5784 // Write to the cache
5785 net::HttpResponseInfo response
;
5786 RunTransactionTestWithResponseInfo(cache
.http_cache(), kSimpleGET_Transaction
,
5788 EXPECT_TRUE(response
.metadata
.get() == NULL
);
5790 // Attempt to write meta data to the same entry.
5791 scoped_refptr
<net::IOBufferWithSize
> buf(new net::IOBufferWithSize(50));
5792 memset(buf
->data(), 0, buf
->size());
5793 base::strlcpy(buf
->data(), "Hi there", buf
->size());
5794 base::Time expected_time
= response
.response_time
-
5795 base::TimeDelta::FromMilliseconds(20);
5796 cache
.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction
.url
),
5797 net::DEFAULT_PRIORITY
,
5802 // Makes sure we finish pending operations.
5803 base::MessageLoop::current()->RunUntilIdle();
5805 RunTransactionTestWithResponseInfo(cache
.http_cache(), kSimpleGET_Transaction
,
5807 EXPECT_TRUE(response
.metadata
.get() == NULL
);
5809 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5810 EXPECT_EQ(2, cache
.disk_cache()->open_count());
5811 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5814 // Tests that we can read metadata after validating the entry and with READ mode
5816 TEST(HttpCache
, ReadMetadata
) {
5817 MockHttpCache cache
;
5819 // Write to the cache
5820 net::HttpResponseInfo response
;
5821 RunTransactionTestWithResponseInfo(cache
.http_cache(),
5822 kTypicalGET_Transaction
, &response
);
5823 EXPECT_TRUE(response
.metadata
.get() == NULL
);
5825 // Write meta data to the same entry.
5826 scoped_refptr
<net::IOBufferWithSize
> buf(new net::IOBufferWithSize(50));
5827 memset(buf
->data(), 0, buf
->size());
5828 base::strlcpy(buf
->data(), "Hi there", buf
->size());
5829 cache
.http_cache()->WriteMetadata(GURL(kTypicalGET_Transaction
.url
),
5830 net::DEFAULT_PRIORITY
,
5831 response
.response_time
,
5835 // Makes sure we finish pending operations.
5836 base::MessageLoop::current()->RunUntilIdle();
5838 // Start with a READ mode transaction.
5839 MockTransaction
trans1(kTypicalGET_Transaction
);
5840 trans1
.load_flags
= net::LOAD_ONLY_FROM_CACHE
;
5842 RunTransactionTestWithResponseInfo(cache
.http_cache(), trans1
, &response
);
5843 ASSERT_TRUE(response
.metadata
.get() != NULL
);
5844 EXPECT_EQ(50, response
.metadata
->size());
5845 EXPECT_EQ(0, strcmp(response
.metadata
->data(), "Hi there"));
5847 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5848 EXPECT_EQ(2, cache
.disk_cache()->open_count());
5849 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5850 base::MessageLoop::current()->RunUntilIdle();
5852 // Now make sure that the entry is re-validated with the server.
5853 trans1
.load_flags
= net::LOAD_VALIDATE_CACHE
;
5854 trans1
.status
= "HTTP/1.1 304 Not Modified";
5855 AddMockTransaction(&trans1
);
5857 response
.metadata
= NULL
;
5858 RunTransactionTestWithResponseInfo(cache
.http_cache(), trans1
, &response
);
5859 EXPECT_TRUE(response
.metadata
.get() != NULL
);
5861 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
5862 EXPECT_EQ(3, cache
.disk_cache()->open_count());
5863 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5864 base::MessageLoop::current()->RunUntilIdle();
5865 RemoveMockTransaction(&trans1
);
5867 // Now return 200 when validating the entry so the metadata will be lost.
5868 MockTransaction
trans2(kTypicalGET_Transaction
);
5869 trans2
.load_flags
= net::LOAD_VALIDATE_CACHE
;
5870 RunTransactionTestWithResponseInfo(cache
.http_cache(), trans2
, &response
);
5871 EXPECT_TRUE(response
.metadata
.get() == NULL
);
5873 EXPECT_EQ(3, cache
.network_layer()->transaction_count());
5874 EXPECT_EQ(4, cache
.disk_cache()->open_count());
5875 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5878 // Tests that we don't mark entries as truncated when a filter detects the end
5880 TEST(HttpCache
, FilterCompletion
) {
5881 MockHttpCache cache
;
5882 net::TestCompletionCallback callback
;
5885 scoped_ptr
<net::HttpTransaction
> trans
;
5886 int rv
= cache
.http_cache()->CreateTransaction(
5887 net::DEFAULT_PRIORITY
, &trans
, NULL
);
5888 EXPECT_EQ(net::OK
, rv
);
5890 MockHttpRequest
request(kSimpleGET_Transaction
);
5891 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
5892 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
5894 scoped_refptr
<net::IOBuffer
> buf(new net::IOBuffer(256));
5895 rv
= trans
->Read(buf
.get(), 256, callback
.callback());
5896 EXPECT_GT(callback
.GetResult(rv
), 0);
5898 // Now make sure that the entry is preserved.
5899 trans
->DoneReading();
5902 // Make sure that the ActiveEntry is gone.
5903 base::MessageLoop::current()->RunUntilIdle();
5905 // Read from the cache.
5906 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
5908 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5909 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5910 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5913 // Tests that we don't mark entries as truncated and release the cache
5914 // entry when DoneReading() is called before any Read() calls, such as
5916 TEST(HttpCache
, DoneReading
) {
5917 MockHttpCache cache
;
5918 net::TestCompletionCallback callback
;
5920 ScopedMockTransaction
transaction(kSimpleGET_Transaction
);
5921 transaction
.data
= "";
5923 scoped_ptr
<net::HttpTransaction
> trans
;
5924 int rv
= cache
.http_cache()->CreateTransaction(
5925 net::DEFAULT_PRIORITY
, &trans
, NULL
);
5926 EXPECT_EQ(net::OK
, rv
);
5928 MockHttpRequest
request(transaction
);
5929 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
5930 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
5932 trans
->DoneReading();
5933 // Leave the transaction around.
5935 // Make sure that the ActiveEntry is gone.
5936 base::MessageLoop::current()->RunUntilIdle();
5938 // Read from the cache. This should not deadlock.
5939 RunTransactionTest(cache
.http_cache(), transaction
);
5941 EXPECT_EQ(1, cache
.network_layer()->transaction_count());
5942 EXPECT_EQ(1, cache
.disk_cache()->open_count());
5943 EXPECT_EQ(1, cache
.disk_cache()->create_count());
5946 // Tests that we stop caching when told.
5947 TEST(HttpCache
, StopCachingDeletesEntry
) {
5948 MockHttpCache cache
;
5949 net::TestCompletionCallback callback
;
5950 MockHttpRequest
request(kSimpleGET_Transaction
);
5953 scoped_ptr
<net::HttpTransaction
> trans
;
5954 int rv
= cache
.http_cache()->CreateTransaction(
5955 net::DEFAULT_PRIORITY
, &trans
, NULL
);
5956 EXPECT_EQ(net::OK
, rv
);
5958 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
5959 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
5961 scoped_refptr
<net::IOBuffer
> buf(new net::IOBuffer(256));
5962 rv
= trans
->Read(buf
.get(), 10, callback
.callback());
5963 EXPECT_EQ(10, callback
.GetResult(rv
));
5965 trans
->StopCaching();
5967 // We should be able to keep reading.
5968 rv
= trans
->Read(buf
.get(), 256, callback
.callback());
5969 EXPECT_GT(callback
.GetResult(rv
), 0);
5970 rv
= trans
->Read(buf
.get(), 256, callback
.callback());
5971 EXPECT_EQ(0, callback
.GetResult(rv
));
5974 // Make sure that the ActiveEntry is gone.
5975 base::MessageLoop::current()->RunUntilIdle();
5977 // Verify that the entry is gone.
5978 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
5980 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
5981 EXPECT_EQ(0, cache
.disk_cache()->open_count());
5982 EXPECT_EQ(2, cache
.disk_cache()->create_count());
5985 // Tests that we stop caching when told, even if DoneReading is called
5986 // after StopCaching.
5987 TEST(HttpCache
, StopCachingThenDoneReadingDeletesEntry
) {
5988 MockHttpCache cache
;
5989 net::TestCompletionCallback callback
;
5990 MockHttpRequest
request(kSimpleGET_Transaction
);
5993 scoped_ptr
<net::HttpTransaction
> trans
;
5994 int rv
= cache
.http_cache()->CreateTransaction(
5995 net::DEFAULT_PRIORITY
, &trans
, NULL
);
5996 EXPECT_EQ(net::OK
, rv
);
5998 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
5999 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
6001 scoped_refptr
<net::IOBuffer
> buf(new net::IOBuffer(256));
6002 rv
= trans
->Read(buf
.get(), 10, callback
.callback());
6003 EXPECT_EQ(10, callback
.GetResult(rv
));
6005 trans
->StopCaching();
6007 // We should be able to keep reading.
6008 rv
= trans
->Read(buf
.get(), 256, callback
.callback());
6009 EXPECT_GT(callback
.GetResult(rv
), 0);
6010 rv
= trans
->Read(buf
.get(), 256, callback
.callback());
6011 EXPECT_EQ(0, callback
.GetResult(rv
));
6013 // We should be able to call DoneReading.
6014 trans
->DoneReading();
6017 // Make sure that the ActiveEntry is gone.
6018 base::MessageLoop::current()->RunUntilIdle();
6020 // Verify that the entry is gone.
6021 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
6023 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
6024 EXPECT_EQ(0, cache
.disk_cache()->open_count());
6025 EXPECT_EQ(2, cache
.disk_cache()->create_count());
6028 // Tests that we stop caching when told, when using auth.
6029 TEST(HttpCache
, StopCachingWithAuthDeletesEntry
) {
6030 MockHttpCache cache
;
6031 net::TestCompletionCallback callback
;
6032 MockTransaction
mock_transaction(kSimpleGET_Transaction
);
6033 mock_transaction
.status
= "HTTP/1.1 401 Unauthorized";
6034 AddMockTransaction(&mock_transaction
);
6035 MockHttpRequest
request(mock_transaction
);
6038 scoped_ptr
<net::HttpTransaction
> trans
;
6039 int rv
= cache
.http_cache()->CreateTransaction(
6040 net::DEFAULT_PRIORITY
, &trans
, NULL
);
6041 EXPECT_EQ(net::OK
, rv
);
6043 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
6044 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
6046 trans
->StopCaching();
6048 scoped_refptr
<net::IOBuffer
> buf(new net::IOBuffer(256));
6049 rv
= trans
->Read(buf
.get(), 10, callback
.callback());
6050 EXPECT_EQ(callback
.GetResult(rv
), 10);
6052 RemoveMockTransaction(&mock_transaction
);
6054 // Make sure that the ActiveEntry is gone.
6055 base::MessageLoop::current()->RunUntilIdle();
6057 // Verify that the entry is gone.
6058 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
6060 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
6061 EXPECT_EQ(0, cache
.disk_cache()->open_count());
6062 EXPECT_EQ(2, cache
.disk_cache()->create_count());
6065 // Tests that when we are told to stop caching we don't throw away valid data.
6066 TEST(HttpCache
, StopCachingSavesEntry
) {
6067 MockHttpCache cache
;
6068 net::TestCompletionCallback callback
;
6069 MockHttpRequest
request(kSimpleGET_Transaction
);
6072 scoped_ptr
<net::HttpTransaction
> trans
;
6073 int rv
= cache
.http_cache()->CreateTransaction(
6074 net::DEFAULT_PRIORITY
, &trans
, NULL
);
6075 EXPECT_EQ(net::OK
, rv
);
6077 // Force a response that can be resumed.
6078 MockTransaction
mock_transaction(kSimpleGET_Transaction
);
6079 AddMockTransaction(&mock_transaction
);
6080 mock_transaction
.response_headers
= "Cache-Control: max-age=10000\n"
6081 "Content-Length: 42\n"
6084 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
6085 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
6087 scoped_refptr
<net::IOBuffer
> buf(new net::IOBuffer(256));
6088 rv
= trans
->Read(buf
.get(), 10, callback
.callback());
6089 EXPECT_EQ(callback
.GetResult(rv
), 10);
6091 trans
->StopCaching();
6093 // We should be able to keep reading.
6094 rv
= trans
->Read(buf
.get(), 256, callback
.callback());
6095 EXPECT_GT(callback
.GetResult(rv
), 0);
6096 rv
= trans
->Read(buf
.get(), 256, callback
.callback());
6097 EXPECT_EQ(callback
.GetResult(rv
), 0);
6099 RemoveMockTransaction(&mock_transaction
);
6102 // Verify that the entry is marked as incomplete.
6103 disk_cache::Entry
* entry
;
6104 ASSERT_TRUE(cache
.OpenBackendEntry(kSimpleGET_Transaction
.url
, &entry
));
6105 net::HttpResponseInfo response
;
6106 bool truncated
= false;
6107 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry
, &response
, &truncated
));
6108 EXPECT_TRUE(truncated
);
6112 // Tests that we handle truncated enries when StopCaching is called.
6113 TEST(HttpCache
, StopCachingTruncatedEntry
) {
6114 MockHttpCache cache
;
6115 net::TestCompletionCallback callback
;
6116 MockHttpRequest
request(kRangeGET_TransactionOK
);
6117 request
.extra_headers
.Clear();
6118 request
.extra_headers
.AddHeaderFromString(EXTRA_HEADER_LINE
);
6119 AddMockTransaction(&kRangeGET_TransactionOK
);
6121 std::string
raw_headers("HTTP/1.1 200 OK\n"
6122 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
6124 "Accept-Ranges: bytes\n"
6125 "Content-Length: 80\n");
6126 CreateTruncatedEntry(raw_headers
, &cache
);
6129 // Now make a regular request.
6130 scoped_ptr
<net::HttpTransaction
> trans
;
6131 int rv
= cache
.http_cache()->CreateTransaction(
6132 net::DEFAULT_PRIORITY
, &trans
, NULL
);
6133 EXPECT_EQ(net::OK
, rv
);
6135 rv
= trans
->Start(&request
, callback
.callback(), net::BoundNetLog());
6136 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
6138 scoped_refptr
<net::IOBuffer
> buf(new net::IOBuffer(256));
6139 rv
= trans
->Read(buf
.get(), 10, callback
.callback());
6140 EXPECT_EQ(callback
.GetResult(rv
), 10);
6142 // This is actually going to do nothing.
6143 trans
->StopCaching();
6145 // We should be able to keep reading.
6146 rv
= trans
->Read(buf
.get(), 256, callback
.callback());
6147 EXPECT_GT(callback
.GetResult(rv
), 0);
6148 rv
= trans
->Read(buf
.get(), 256, callback
.callback());
6149 EXPECT_GT(callback
.GetResult(rv
), 0);
6150 rv
= trans
->Read(buf
.get(), 256, callback
.callback());
6151 EXPECT_EQ(callback
.GetResult(rv
), 0);
6154 // Verify that the disk entry was updated.
6155 disk_cache::Entry
* entry
;
6156 ASSERT_TRUE(cache
.OpenBackendEntry(kRangeGET_TransactionOK
.url
, &entry
));
6157 EXPECT_EQ(80, entry
->GetDataSize(1));
6158 bool truncated
= true;
6159 net::HttpResponseInfo response
;
6160 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry
, &response
, &truncated
));
6161 EXPECT_FALSE(truncated
);
6164 RemoveMockTransaction(&kRangeGET_TransactionOK
);
6167 // Tests that we detect truncated resources from the net when there is
6168 // a Content-Length header.
6169 TEST(HttpCache
, TruncatedByContentLength
) {
6170 MockHttpCache cache
;
6171 net::TestCompletionCallback callback
;
6173 MockTransaction
transaction(kSimpleGET_Transaction
);
6174 AddMockTransaction(&transaction
);
6175 transaction
.response_headers
= "Cache-Control: max-age=10000\n"
6176 "Content-Length: 100\n";
6177 RunTransactionTest(cache
.http_cache(), transaction
);
6178 RemoveMockTransaction(&transaction
);
6180 // Read from the cache.
6181 RunTransactionTest(cache
.http_cache(), kSimpleGET_Transaction
);
6183 EXPECT_EQ(2, cache
.network_layer()->transaction_count());
6184 EXPECT_EQ(0, cache
.disk_cache()->open_count());
6185 EXPECT_EQ(2, cache
.disk_cache()->create_count());
6188 // Tests that we actually flag entries as truncated when we detect an error
6190 TEST(HttpCache
, TruncatedByContentLength2
) {
6191 MockHttpCache cache
;
6192 net::TestCompletionCallback callback
;
6194 MockTransaction
transaction(kSimpleGET_Transaction
);
6195 AddMockTransaction(&transaction
);
6196 transaction
.response_headers
= "Cache-Control: max-age=10000\n"
6197 "Content-Length: 100\n"
6199 RunTransactionTest(cache
.http_cache(), transaction
);
6200 RemoveMockTransaction(&transaction
);
6202 // Verify that the entry is marked as incomplete.
6203 disk_cache::Entry
* entry
;
6204 ASSERT_TRUE(cache
.OpenBackendEntry(kSimpleGET_Transaction
.url
, &entry
));
6205 net::HttpResponseInfo response
;
6206 bool truncated
= false;
6207 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry
, &response
, &truncated
));
6208 EXPECT_TRUE(truncated
);
6212 TEST(HttpCache
, SimpleGET_LoadOnlyFromCache_Hit_TransactionDelegate
) {
6213 MockHttpCache cache
;
6215 // Write to the cache.
6216 RunTransactionTestWithDelegate(cache
.http_cache(),
6217 kSimpleGET_Transaction
,
6221 // Force this transaction to read from the cache.
6222 MockTransaction
transaction(kSimpleGET_Transaction
);
6223 transaction
.load_flags
|= net::LOAD_ONLY_FROM_CACHE
;
6225 RunTransactionTestWithDelegate(cache
.http_cache(),
6226 kSimpleGET_Transaction
,
6231 // Make sure that calling SetPriority on a cache transaction passes on
6232 // its priority updates to its underlying network transaction.
6233 TEST(HttpCache
, SetPriority
) {
6234 MockHttpCache cache
;
6236 scoped_ptr
<net::HttpTransaction
> trans
;
6237 EXPECT_EQ(net::OK
, cache
.http_cache()->CreateTransaction(
6238 net::IDLE
, &trans
, NULL
));
6239 ASSERT_TRUE(trans
.get());
6241 // Shouldn't crash, but doesn't do anything either.
6242 trans
->SetPriority(net::LOW
);
6244 EXPECT_FALSE(cache
.network_layer()->last_transaction());
6245 EXPECT_EQ(net::DEFAULT_PRIORITY
,
6246 cache
.network_layer()->last_create_transaction_priority());
6248 net::HttpRequestInfo info
;
6249 info
.url
= GURL(kSimpleGET_Transaction
.url
);
6250 net::TestCompletionCallback callback
;
6251 EXPECT_EQ(net::ERR_IO_PENDING
,
6252 trans
->Start(&info
, callback
.callback(), net::BoundNetLog()));
6254 EXPECT_TRUE(cache
.network_layer()->last_transaction());
6255 if (cache
.network_layer()->last_transaction()) {
6257 cache
.network_layer()->last_create_transaction_priority());
6259 cache
.network_layer()->last_transaction()->priority());
6262 trans
->SetPriority(net::HIGHEST
);
6264 if (cache
.network_layer()->last_transaction()) {
6266 cache
.network_layer()->last_create_transaction_priority());
6267 EXPECT_EQ(net::HIGHEST
,
6268 cache
.network_layer()->last_transaction()->priority());
6271 EXPECT_EQ(net::OK
, callback
.WaitForResult());
6274 // Make sure that a cache transaction passes on its priority to
6275 // newly-created network transactions.
6276 TEST(HttpCache
, SetPriorityNewTransaction
) {
6277 MockHttpCache cache
;
6278 AddMockTransaction(&kRangeGET_TransactionOK
);
6280 std::string
raw_headers("HTTP/1.1 200 OK\n"
6281 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
6283 "Accept-Ranges: bytes\n"
6284 "Content-Length: 80\n");
6285 CreateTruncatedEntry(raw_headers
, &cache
);
6287 // Now make a regular request.
6288 std::string headers
;
6289 MockTransaction
transaction(kRangeGET_TransactionOK
);
6290 transaction
.request_headers
= EXTRA_HEADER
;
6291 transaction
.data
= "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
6292 "rg: 50-59 rg: 60-69 rg: 70-79 ";
6294 scoped_ptr
<net::HttpTransaction
> trans
;
6295 EXPECT_EQ(net::OK
, cache
.http_cache()->CreateTransaction(
6296 net::MEDIUM
, &trans
, NULL
));
6297 ASSERT_TRUE(trans
.get());
6298 EXPECT_EQ(net::DEFAULT_PRIORITY
,
6299 cache
.network_layer()->last_create_transaction_priority());
6301 MockHttpRequest
info(transaction
);
6302 net::TestCompletionCallback callback
;
6303 EXPECT_EQ(net::ERR_IO_PENDING
,
6304 trans
->Start(&info
, callback
.callback(), net::BoundNetLog()));
6305 EXPECT_EQ(net::OK
, callback
.WaitForResult());
6307 EXPECT_EQ(net::MEDIUM
,
6308 cache
.network_layer()->last_create_transaction_priority());
6310 trans
->SetPriority(net::HIGHEST
);
6311 // Should trigger a new network transaction and pick up the new
6313 ReadAndVerifyTransaction(trans
.get(), transaction
);
6315 EXPECT_EQ(net::HIGHEST
,
6316 cache
.network_layer()->last_create_transaction_priority());
6318 RemoveMockTransaction(&kRangeGET_TransactionOK
);