Update app list background color.
[chromium-blink-merge.git] / net / http / http_cache_unittest.cc
blob3458402376cdfe19663cb59e177cc4720a5aa3c9
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"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/test/simple_test_clock.h"
17 #include "net/base/cache_type.h"
18 #include "net/base/elements_upload_data_stream.h"
19 #include "net/base/host_port_pair.h"
20 #include "net/base/load_flags.h"
21 #include "net/base/load_timing_info.h"
22 #include "net/base/load_timing_info_test_util.h"
23 #include "net/base/net_errors.h"
24 #include "net/base/net_log_unittest.h"
25 #include "net/base/upload_bytes_element_reader.h"
26 #include "net/cert/cert_status_flags.h"
27 #include "net/disk_cache/disk_cache.h"
28 #include "net/http/http_byte_range.h"
29 #include "net/http/http_cache_transaction.h"
30 #include "net/http/http_request_headers.h"
31 #include "net/http/http_request_info.h"
32 #include "net/http/http_response_headers.h"
33 #include "net/http/http_response_info.h"
34 #include "net/http/http_transaction.h"
35 #include "net/http/http_transaction_test_util.h"
36 #include "net/http/http_util.h"
37 #include "net/http/mock_http_cache.h"
38 #include "net/socket/client_socket_handle.h"
39 #include "net/ssl/ssl_cert_request_info.h"
40 #include "net/websockets/websocket_handshake_stream_base.h"
41 #include "testing/gtest/include/gtest/gtest.h"
43 using base::Time;
45 namespace {
47 // Tests the load timing values of a request that goes through a
48 // MockNetworkTransaction.
49 void TestLoadTimingNetworkRequest(const net::LoadTimingInfo& load_timing_info) {
50 EXPECT_FALSE(load_timing_info.socket_reused);
51 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
53 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
54 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
56 net::ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
57 net::CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY);
58 EXPECT_LE(load_timing_info.connect_timing.connect_end,
59 load_timing_info.send_start);
61 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
63 // Set by URLRequest / URLRequestHttpJob, at a higher level.
64 EXPECT_TRUE(load_timing_info.request_start_time.is_null());
65 EXPECT_TRUE(load_timing_info.request_start.is_null());
66 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null());
69 // Tests the load timing values of a request that receives a cached response.
70 void TestLoadTimingCachedResponse(const net::LoadTimingInfo& load_timing_info) {
71 EXPECT_FALSE(load_timing_info.socket_reused);
72 EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
74 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
75 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
77 net::ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
79 // Only the send start / end times should be sent, and they should have the
80 // same value.
81 EXPECT_FALSE(load_timing_info.send_start.is_null());
82 EXPECT_EQ(load_timing_info.send_start, load_timing_info.send_end);
84 // Set by URLRequest / URLRequestHttpJob, at a higher level.
85 EXPECT_TRUE(load_timing_info.request_start_time.is_null());
86 EXPECT_TRUE(load_timing_info.request_start.is_null());
87 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null());
90 class DeleteCacheCompletionCallback : public net::TestCompletionCallbackBase {
91 public:
92 explicit DeleteCacheCompletionCallback(MockHttpCache* cache)
93 : cache_(cache),
94 callback_(base::Bind(&DeleteCacheCompletionCallback::OnComplete,
95 base::Unretained(this))) {
98 const net::CompletionCallback& callback() const { return callback_; }
100 private:
101 void OnComplete(int result) {
102 delete cache_;
103 SetResult(result);
106 MockHttpCache* cache_;
107 net::CompletionCallback callback_;
109 DISALLOW_COPY_AND_ASSIGN(DeleteCacheCompletionCallback);
112 //-----------------------------------------------------------------------------
113 // helpers
115 void ReadAndVerifyTransaction(net::HttpTransaction* trans,
116 const MockTransaction& trans_info) {
117 std::string content;
118 int rv = ReadTransaction(trans, &content);
120 EXPECT_EQ(net::OK, rv);
121 std::string expected(trans_info.data);
122 EXPECT_EQ(expected, content);
125 void RunTransactionTestBase(net::HttpCache* cache,
126 const MockTransaction& trans_info,
127 const MockHttpRequest& request,
128 net::HttpResponseInfo* response_info,
129 const net::BoundNetLog& net_log,
130 net::LoadTimingInfo* load_timing_info,
131 int64* received_bytes) {
132 net::TestCompletionCallback callback;
134 // write to the cache
136 scoped_ptr<net::HttpTransaction> trans;
137 int rv = cache->CreateTransaction(net::DEFAULT_PRIORITY, &trans);
138 EXPECT_EQ(net::OK, rv);
139 ASSERT_TRUE(trans.get());
141 rv = trans->Start(&request, callback.callback(), net_log);
142 if (rv == net::ERR_IO_PENDING)
143 rv = callback.WaitForResult();
144 ASSERT_EQ(trans_info.return_code, rv);
146 if (net::OK != rv)
147 return;
149 const net::HttpResponseInfo* response = trans->GetResponseInfo();
150 ASSERT_TRUE(response);
152 if (response_info)
153 *response_info = *response;
155 if (load_timing_info) {
156 // If a fake network connection is used, need a NetLog to get a fake socket
157 // ID.
158 EXPECT_TRUE(net_log.net_log());
159 *load_timing_info = net::LoadTimingInfo();
160 trans->GetLoadTimingInfo(load_timing_info);
163 ReadAndVerifyTransaction(trans.get(), trans_info);
165 if (received_bytes)
166 *received_bytes = trans->GetTotalReceivedBytes();
169 void RunTransactionTestWithRequest(net::HttpCache* cache,
170 const MockTransaction& trans_info,
171 const MockHttpRequest& request,
172 net::HttpResponseInfo* response_info) {
173 RunTransactionTestBase(cache, trans_info, request, response_info,
174 net::BoundNetLog(), NULL, NULL);
177 void RunTransactionTestAndGetTiming(net::HttpCache* cache,
178 const MockTransaction& trans_info,
179 const net::BoundNetLog& log,
180 net::LoadTimingInfo* load_timing_info) {
181 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info),
182 NULL, log, load_timing_info, NULL);
185 void RunTransactionTest(net::HttpCache* cache,
186 const MockTransaction& trans_info) {
187 RunTransactionTestAndGetTiming(cache, trans_info, net::BoundNetLog(), NULL);
190 void RunTransactionTestWithLog(net::HttpCache* cache,
191 const MockTransaction& trans_info,
192 const net::BoundNetLog& log) {
193 RunTransactionTestAndGetTiming(cache, trans_info, log, NULL);
196 void RunTransactionTestWithResponseInfo(net::HttpCache* cache,
197 const MockTransaction& trans_info,
198 net::HttpResponseInfo* response) {
199 RunTransactionTestWithRequest(cache, trans_info, MockHttpRequest(trans_info),
200 response);
203 void RunTransactionTestWithResponseInfoAndGetTiming(
204 net::HttpCache* cache,
205 const MockTransaction& trans_info,
206 net::HttpResponseInfo* response,
207 const net::BoundNetLog& log,
208 net::LoadTimingInfo* load_timing_info) {
209 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info),
210 response, log, load_timing_info, NULL);
213 void RunTransactionTestWithResponse(net::HttpCache* cache,
214 const MockTransaction& trans_info,
215 std::string* response_headers) {
216 net::HttpResponseInfo response;
217 RunTransactionTestWithResponseInfo(cache, trans_info, &response);
218 response.headers->GetNormalizedHeaders(response_headers);
221 void RunTransactionTestWithResponseAndGetTiming(
222 net::HttpCache* cache,
223 const MockTransaction& trans_info,
224 std::string* response_headers,
225 const net::BoundNetLog& log,
226 net::LoadTimingInfo* load_timing_info) {
227 net::HttpResponseInfo response;
228 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info),
229 &response, log, load_timing_info, NULL);
230 response.headers->GetNormalizedHeaders(response_headers);
233 // This class provides a handler for kFastNoStoreGET_Transaction so that the
234 // no-store header can be included on demand.
235 class FastTransactionServer {
236 public:
237 FastTransactionServer() {
238 no_store = false;
240 ~FastTransactionServer() {}
242 void set_no_store(bool value) { no_store = value; }
244 static void FastNoStoreHandler(const net::HttpRequestInfo* request,
245 std::string* response_status,
246 std::string* response_headers,
247 std::string* response_data) {
248 if (no_store)
249 *response_headers = "Cache-Control: no-store\n";
252 private:
253 static bool no_store;
254 DISALLOW_COPY_AND_ASSIGN(FastTransactionServer);
256 bool FastTransactionServer::no_store;
258 const MockTransaction kFastNoStoreGET_Transaction = {
259 "http://www.google.com/nostore",
260 "GET",
261 base::Time(),
263 net::LOAD_VALIDATE_CACHE,
264 "HTTP/1.1 200 OK",
265 "Cache-Control: max-age=10000\n",
266 base::Time(),
267 "<html><body>Google Blah Blah</body></html>",
268 TEST_MODE_SYNC_NET_START,
269 &FastTransactionServer::FastNoStoreHandler,
271 net::OK
274 // This class provides a handler for kRangeGET_TransactionOK so that the range
275 // request can be served on demand.
276 class RangeTransactionServer {
277 public:
278 RangeTransactionServer() {
279 not_modified_ = false;
280 modified_ = false;
281 bad_200_ = false;
283 ~RangeTransactionServer() {
284 not_modified_ = false;
285 modified_ = false;
286 bad_200_ = false;
289 // Returns only 416 or 304 when set.
290 void set_not_modified(bool value) { not_modified_ = value; }
292 // Returns 206 when revalidating a range (instead of 304).
293 void set_modified(bool value) { modified_ = value; }
295 // Returns 200 instead of 206 (a malformed response overall).
296 void set_bad_200(bool value) { bad_200_ = value; }
298 // Other than regular range related behavior (and the flags mentioned above),
299 // the server reacts to requests headers like so:
300 // X-Require-Mock-Auth -> return 401.
301 // X-Return-Default-Range -> assume 40-49 was requested.
302 static void RangeHandler(const net::HttpRequestInfo* request,
303 std::string* response_status,
304 std::string* response_headers,
305 std::string* response_data);
307 private:
308 static bool not_modified_;
309 static bool modified_;
310 static bool bad_200_;
311 DISALLOW_COPY_AND_ASSIGN(RangeTransactionServer);
313 bool RangeTransactionServer::not_modified_ = false;
314 bool RangeTransactionServer::modified_ = false;
315 bool RangeTransactionServer::bad_200_ = false;
317 // A dummy extra header that must be preserved on a given request.
319 // EXTRA_HEADER_LINE doesn't include a line terminator because it
320 // will be passed to AddHeaderFromString() which doesn't accept them.
321 #define EXTRA_HEADER_LINE "Extra: header"
323 // EXTRA_HEADER contains a line terminator, as expected by
324 // AddHeadersFromString() (_not_ AddHeaderFromString()).
325 #define EXTRA_HEADER EXTRA_HEADER_LINE "\r\n"
327 static const char kExtraHeaderKey[] = "Extra";
329 // Static.
330 void RangeTransactionServer::RangeHandler(const net::HttpRequestInfo* request,
331 std::string* response_status,
332 std::string* response_headers,
333 std::string* response_data) {
334 if (request->extra_headers.IsEmpty()) {
335 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable");
336 response_data->clear();
337 return;
340 // We want to make sure we don't delete extra headers.
341 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey));
343 if (request->extra_headers.HasHeader("X-Require-Mock-Auth") &&
344 !request->extra_headers.HasHeader("Authorization")) {
345 response_status->assign("HTTP/1.1 401 Unauthorized");
346 response_data->assign("WWW-Authenticate: Foo\n");
347 return;
350 if (not_modified_) {
351 response_status->assign("HTTP/1.1 304 Not Modified");
352 response_data->clear();
353 return;
356 std::vector<net::HttpByteRange> ranges;
357 std::string range_header;
358 if (!request->extra_headers.GetHeader(
359 net::HttpRequestHeaders::kRange, &range_header) ||
360 !net::HttpUtil::ParseRangeHeader(range_header, &ranges) || bad_200_ ||
361 ranges.size() != 1) {
362 // This is not a byte range request. We return 200.
363 response_status->assign("HTTP/1.1 200 OK");
364 response_headers->assign("Date: Wed, 28 Nov 2007 09:40:09 GMT");
365 response_data->assign("Not a range");
366 return;
369 // We can handle this range request.
370 net::HttpByteRange byte_range = ranges[0];
372 if (request->extra_headers.HasHeader("X-Return-Default-Range")) {
373 byte_range.set_first_byte_position(40);
374 byte_range.set_last_byte_position(49);
377 if (byte_range.first_byte_position() > 79) {
378 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable");
379 response_data->clear();
380 return;
383 EXPECT_TRUE(byte_range.ComputeBounds(80));
384 int start = static_cast<int>(byte_range.first_byte_position());
385 int end = static_cast<int>(byte_range.last_byte_position());
387 EXPECT_LT(end, 80);
389 std::string content_range = base::StringPrintf(
390 "Content-Range: bytes %d-%d/80\n", start, end);
391 response_headers->append(content_range);
393 if (!request->extra_headers.HasHeader("If-None-Match") || modified_) {
394 std::string data;
395 if (end == start) {
396 EXPECT_EQ(0, end % 10);
397 data = "r";
398 } else {
399 EXPECT_EQ(9, (end - start) % 10);
400 for (int block_start = start; block_start < end; block_start += 10) {
401 base::StringAppendF(&data, "rg: %02d-%02d ",
402 block_start, block_start + 9);
405 *response_data = data;
407 if (end - start != 9) {
408 // We also have to fix content-length.
409 int len = end - start + 1;
410 std::string content_length = base::StringPrintf("Content-Length: %d\n",
411 len);
412 response_headers->replace(response_headers->find("Content-Length:"),
413 content_length.size(), content_length);
415 } else {
416 response_status->assign("HTTP/1.1 304 Not Modified");
417 response_data->clear();
421 const MockTransaction kRangeGET_TransactionOK = {
422 "http://www.google.com/range",
423 "GET",
424 base::Time(),
425 "Range: bytes = 40-49\r\n"
426 EXTRA_HEADER,
427 net::LOAD_NORMAL,
428 "HTTP/1.1 206 Partial Content",
429 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
430 "ETag: \"foo\"\n"
431 "Accept-Ranges: bytes\n"
432 "Content-Length: 10\n",
433 base::Time(),
434 "rg: 40-49 ",
435 TEST_MODE_NORMAL,
436 &RangeTransactionServer::RangeHandler,
438 net::OK
441 const char kFullRangeData[] =
442 "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 "
443 "rg: 40-49 rg: 50-59 rg: 60-69 rg: 70-79 ";
445 // Verifies the response headers (|response|) match a partial content
446 // response for the range starting at |start| and ending at |end|.
447 void Verify206Response(std::string response, int start, int end) {
448 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(),
449 response.size()));
450 scoped_refptr<net::HttpResponseHeaders> headers(
451 new net::HttpResponseHeaders(raw_headers));
453 ASSERT_EQ(206, headers->response_code());
455 int64 range_start, range_end, object_size;
456 ASSERT_TRUE(
457 headers->GetContentRange(&range_start, &range_end, &object_size));
458 int64 content_length = headers->GetContentLength();
460 int length = end - start + 1;
461 ASSERT_EQ(length, content_length);
462 ASSERT_EQ(start, range_start);
463 ASSERT_EQ(end, range_end);
466 // Creates a truncated entry that can be resumed using byte ranges.
467 void CreateTruncatedEntry(std::string raw_headers, MockHttpCache* cache) {
468 // Create a disk cache entry that stores an incomplete resource.
469 disk_cache::Entry* entry;
470 ASSERT_TRUE(cache->CreateBackendEntry(kRangeGET_TransactionOK.url, &entry,
471 NULL));
473 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
474 raw_headers.size());
476 net::HttpResponseInfo response;
477 response.response_time = base::Time::Now();
478 response.request_time = base::Time::Now();
479 response.headers = new net::HttpResponseHeaders(raw_headers);
480 // Set the last argument for this to be an incomplete request.
481 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true));
483 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100));
484 int len = static_cast<int>(base::strlcpy(buf->data(),
485 "rg: 00-09 rg: 10-19 ", 100));
486 net::TestCompletionCallback cb;
487 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true);
488 EXPECT_EQ(len, cb.GetResult(rv));
489 entry->Close();
492 // Helper to represent a network HTTP response.
493 struct Response {
494 // Set this response into |trans|.
495 void AssignTo(MockTransaction* trans) const {
496 trans->status = status;
497 trans->response_headers = headers;
498 trans->data = body;
501 std::string status_and_headers() const {
502 return std::string(status) + "\n" + std::string(headers);
505 const char* status;
506 const char* headers;
507 const char* body;
510 struct Context {
511 Context() : result(net::ERR_IO_PENDING) {}
513 int result;
514 net::TestCompletionCallback callback;
515 scoped_ptr<net::HttpTransaction> trans;
518 class FakeWebSocketHandshakeStreamCreateHelper
519 : public net::WebSocketHandshakeStreamBase::CreateHelper {
520 public:
521 ~FakeWebSocketHandshakeStreamCreateHelper() override {}
522 net::WebSocketHandshakeStreamBase* CreateBasicStream(
523 scoped_ptr<net::ClientSocketHandle> connect,
524 bool using_proxy) override {
525 return NULL;
527 net::WebSocketHandshakeStreamBase* CreateSpdyStream(
528 const base::WeakPtr<net::SpdySession>& session,
529 bool use_relative_url) override {
530 return NULL;
534 // Returns true if |entry| is not one of the log types paid attention to in this
535 // test. Note that TYPE_HTTP_CACHE_WRITE_INFO and TYPE_HTTP_CACHE_*_DATA are
536 // ignored.
537 bool ShouldIgnoreLogEntry(const net::CapturingNetLog::CapturedEntry& entry) {
538 switch (entry.type) {
539 case net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND:
540 case net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY:
541 case net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY:
542 case net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY:
543 case net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY:
544 case net::NetLog::TYPE_HTTP_CACHE_READ_INFO:
545 return false;
546 default:
547 return true;
551 // Modifies |entries| to only include log entries created by the cache layer and
552 // asserted on in these tests.
553 void FilterLogEntries(net::CapturingNetLog::CapturedEntryList* entries) {
554 entries->erase(std::remove_if(entries->begin(), entries->end(),
555 &ShouldIgnoreLogEntry),
556 entries->end());
559 bool LogContainsEventType(const net::CapturingBoundNetLog& log,
560 net::NetLog::EventType expected) {
561 net::CapturingNetLog::CapturedEntryList entries;
562 log.GetEntries(&entries);
563 for (size_t i = 0; i < entries.size(); i++) {
564 if (entries[i].type == expected)
565 return true;
567 return false;
570 } // namespace
573 //-----------------------------------------------------------------------------
574 // Tests.
576 TEST(HttpCache, CreateThenDestroy) {
577 MockHttpCache cache;
579 scoped_ptr<net::HttpTransaction> trans;
580 EXPECT_EQ(net::OK, cache.CreateTransaction(&trans));
581 ASSERT_TRUE(trans.get());
584 TEST(HttpCache, GetBackend) {
585 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(0));
587 disk_cache::Backend* backend;
588 net::TestCompletionCallback cb;
589 // This will lazily initialize the backend.
590 int rv = cache.http_cache()->GetBackend(&backend, cb.callback());
591 EXPECT_EQ(net::OK, cb.GetResult(rv));
594 TEST(HttpCache, SimpleGET) {
595 MockHttpCache cache;
596 net::CapturingBoundNetLog log;
597 net::LoadTimingInfo load_timing_info;
599 // Write to the cache.
600 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction,
601 log.bound(), &load_timing_info);
603 EXPECT_EQ(1, cache.network_layer()->transaction_count());
604 EXPECT_EQ(0, cache.disk_cache()->open_count());
605 EXPECT_EQ(1, cache.disk_cache()->create_count());
606 TestLoadTimingNetworkRequest(load_timing_info);
609 TEST(HttpCache, SimpleGETNoDiskCache) {
610 MockHttpCache cache;
612 cache.disk_cache()->set_fail_requests();
614 net::CapturingBoundNetLog log;
615 net::LoadTimingInfo load_timing_info;
617 // Read from the network, and don't use the cache.
618 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction,
619 log.bound(), &load_timing_info);
621 // Check that the NetLog was filled as expected.
622 // (We attempted to both Open and Create entries, but both failed).
623 net::CapturingNetLog::CapturedEntryList entries;
624 log.GetEntries(&entries);
625 FilterLogEntries(&entries);
627 EXPECT_EQ(6u, entries.size());
628 EXPECT_TRUE(net::LogContainsBeginEvent(
629 entries, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND));
630 EXPECT_TRUE(net::LogContainsEndEvent(
631 entries, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND));
632 EXPECT_TRUE(net::LogContainsBeginEvent(
633 entries, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY));
634 EXPECT_TRUE(net::LogContainsEndEvent(
635 entries, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY));
636 EXPECT_TRUE(net::LogContainsBeginEvent(
637 entries, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY));
638 EXPECT_TRUE(net::LogContainsEndEvent(
639 entries, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY));
641 EXPECT_EQ(1, cache.network_layer()->transaction_count());
642 EXPECT_EQ(0, cache.disk_cache()->open_count());
643 EXPECT_EQ(0, cache.disk_cache()->create_count());
644 TestLoadTimingNetworkRequest(load_timing_info);
647 TEST(HttpCache, SimpleGETNoDiskCache2) {
648 // This will initialize a cache object with NULL backend.
649 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
650 factory->set_fail(true);
651 factory->FinishCreation(); // We'll complete synchronously.
652 MockHttpCache cache(factory);
654 // Read from the network, and don't use the cache.
655 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
657 EXPECT_EQ(1, cache.network_layer()->transaction_count());
658 EXPECT_FALSE(cache.http_cache()->GetCurrentBackend());
661 // Tests that IOBuffers are not referenced after IO completes.
662 TEST(HttpCache, ReleaseBuffer) {
663 MockHttpCache cache;
665 // Write to the cache.
666 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
668 MockHttpRequest request(kSimpleGET_Transaction);
669 scoped_ptr<net::HttpTransaction> trans;
670 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
672 const int kBufferSize = 10;
673 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize));
674 net::ReleaseBufferCompletionCallback cb(buffer.get());
676 int rv = trans->Start(&request, cb.callback(), net::BoundNetLog());
677 EXPECT_EQ(net::OK, cb.GetResult(rv));
679 rv = trans->Read(buffer.get(), kBufferSize, cb.callback());
680 EXPECT_EQ(kBufferSize, cb.GetResult(rv));
683 TEST(HttpCache, SimpleGETWithDiskFailures) {
684 MockHttpCache cache;
686 cache.disk_cache()->set_soft_failures(true);
688 // Read from the network, and fail to write to the cache.
689 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
691 EXPECT_EQ(1, cache.network_layer()->transaction_count());
692 EXPECT_EQ(0, cache.disk_cache()->open_count());
693 EXPECT_EQ(1, cache.disk_cache()->create_count());
695 // This one should see an empty cache again.
696 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
698 EXPECT_EQ(2, cache.network_layer()->transaction_count());
699 EXPECT_EQ(0, cache.disk_cache()->open_count());
700 EXPECT_EQ(2, cache.disk_cache()->create_count());
703 // Tests that disk failures after the transaction has started don't cause the
704 // request to fail.
705 TEST(HttpCache, SimpleGETWithDiskFailures2) {
706 MockHttpCache cache;
708 MockHttpRequest request(kSimpleGET_Transaction);
710 scoped_ptr<Context> c(new Context());
711 int rv = cache.CreateTransaction(&c->trans);
712 ASSERT_EQ(net::OK, rv);
714 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
715 EXPECT_EQ(net::ERR_IO_PENDING, rv);
716 rv = c->callback.WaitForResult();
718 // Start failing request now.
719 cache.disk_cache()->set_soft_failures(true);
721 // We have to open the entry again to propagate the failure flag.
722 disk_cache::Entry* en;
723 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &en));
724 en->Close();
726 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
727 c.reset();
729 EXPECT_EQ(1, cache.network_layer()->transaction_count());
730 EXPECT_EQ(1, cache.disk_cache()->open_count());
731 EXPECT_EQ(1, cache.disk_cache()->create_count());
733 // This one should see an empty cache again.
734 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
736 EXPECT_EQ(2, cache.network_layer()->transaction_count());
737 EXPECT_EQ(1, cache.disk_cache()->open_count());
738 EXPECT_EQ(2, cache.disk_cache()->create_count());
741 // Tests that we handle failures to read from the cache.
742 TEST(HttpCache, SimpleGETWithDiskFailures3) {
743 MockHttpCache cache;
745 // Read from the network, and write to the cache.
746 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
748 EXPECT_EQ(1, cache.network_layer()->transaction_count());
749 EXPECT_EQ(0, cache.disk_cache()->open_count());
750 EXPECT_EQ(1, cache.disk_cache()->create_count());
752 cache.disk_cache()->set_soft_failures(true);
754 // Now fail to read from the cache.
755 scoped_ptr<Context> c(new Context());
756 int rv = cache.CreateTransaction(&c->trans);
757 ASSERT_EQ(net::OK, rv);
759 MockHttpRequest request(kSimpleGET_Transaction);
760 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
761 EXPECT_EQ(net::OK, c->callback.GetResult(rv));
763 // Now verify that the entry was removed from the cache.
764 cache.disk_cache()->set_soft_failures(false);
766 EXPECT_EQ(2, cache.network_layer()->transaction_count());
767 EXPECT_EQ(1, cache.disk_cache()->open_count());
768 EXPECT_EQ(2, cache.disk_cache()->create_count());
770 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
772 EXPECT_EQ(3, cache.network_layer()->transaction_count());
773 EXPECT_EQ(1, cache.disk_cache()->open_count());
774 EXPECT_EQ(3, cache.disk_cache()->create_count());
777 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) {
778 MockHttpCache cache;
780 net::CapturingBoundNetLog log;
781 net::LoadTimingInfo load_timing_info;
783 // Write to the cache.
784 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction,
785 log.bound(), &load_timing_info);
787 // Check that the NetLog was filled as expected.
788 net::CapturingNetLog::CapturedEntryList entries;
789 log.GetEntries(&entries);
790 FilterLogEntries(&entries);
792 EXPECT_EQ(8u, entries.size());
793 EXPECT_TRUE(net::LogContainsBeginEvent(
794 entries, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND));
795 EXPECT_TRUE(net::LogContainsEndEvent(
796 entries, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND));
797 EXPECT_TRUE(net::LogContainsBeginEvent(
798 entries, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY));
799 EXPECT_TRUE(net::LogContainsEndEvent(
800 entries, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY));
801 EXPECT_TRUE(net::LogContainsBeginEvent(
802 entries, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY));
803 EXPECT_TRUE(net::LogContainsEndEvent(
804 entries, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY));
805 EXPECT_TRUE(net::LogContainsBeginEvent(
806 entries, 6, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY));
807 EXPECT_TRUE(net::LogContainsEndEvent(
808 entries, 7, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY));
810 TestLoadTimingNetworkRequest(load_timing_info);
812 // Force this transaction to read from the cache.
813 MockTransaction transaction(kSimpleGET_Transaction);
814 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
816 log.Clear();
818 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(),
819 &load_timing_info);
821 // Check that the NetLog was filled as expected.
822 log.GetEntries(&entries);
823 FilterLogEntries(&entries);
825 EXPECT_EQ(8u, entries.size());
826 EXPECT_TRUE(net::LogContainsBeginEvent(
827 entries, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND));
828 EXPECT_TRUE(net::LogContainsEndEvent(
829 entries, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND));
830 EXPECT_TRUE(net::LogContainsBeginEvent(
831 entries, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY));
832 EXPECT_TRUE(net::LogContainsEndEvent(
833 entries, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY));
834 EXPECT_TRUE(net::LogContainsBeginEvent(
835 entries, 4, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY));
836 EXPECT_TRUE(net::LogContainsEndEvent(
837 entries, 5, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY));
838 EXPECT_TRUE(net::LogContainsBeginEvent(
839 entries, 6, net::NetLog::TYPE_HTTP_CACHE_READ_INFO));
840 EXPECT_TRUE(net::LogContainsEndEvent(
841 entries, 7, net::NetLog::TYPE_HTTP_CACHE_READ_INFO));
843 EXPECT_EQ(1, cache.network_layer()->transaction_count());
844 EXPECT_EQ(1, cache.disk_cache()->open_count());
845 EXPECT_EQ(1, cache.disk_cache()->create_count());
846 TestLoadTimingCachedResponse(load_timing_info);
849 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) {
850 MockHttpCache cache;
852 // force this transaction to read from the cache
853 MockTransaction transaction(kSimpleGET_Transaction);
854 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
856 MockHttpRequest request(transaction);
857 net::TestCompletionCallback callback;
859 scoped_ptr<net::HttpTransaction> trans;
860 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
862 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
863 if (rv == net::ERR_IO_PENDING)
864 rv = callback.WaitForResult();
865 ASSERT_EQ(net::ERR_CACHE_MISS, rv);
867 trans.reset();
869 EXPECT_EQ(0, cache.network_layer()->transaction_count());
870 EXPECT_EQ(0, cache.disk_cache()->open_count());
871 EXPECT_EQ(0, cache.disk_cache()->create_count());
874 TEST(HttpCache, SimpleGET_LoadPreferringCache_Hit) {
875 MockHttpCache cache;
877 // write to the cache
878 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
880 // force this transaction to read from the cache if valid
881 MockTransaction transaction(kSimpleGET_Transaction);
882 transaction.load_flags |= net::LOAD_PREFERRING_CACHE;
884 RunTransactionTest(cache.http_cache(), transaction);
886 EXPECT_EQ(1, cache.network_layer()->transaction_count());
887 EXPECT_EQ(1, cache.disk_cache()->open_count());
888 EXPECT_EQ(1, cache.disk_cache()->create_count());
891 TEST(HttpCache, SimpleGET_LoadPreferringCache_Miss) {
892 MockHttpCache cache;
894 // force this transaction to read from the cache if valid
895 MockTransaction transaction(kSimpleGET_Transaction);
896 transaction.load_flags |= net::LOAD_PREFERRING_CACHE;
898 RunTransactionTest(cache.http_cache(), transaction);
900 EXPECT_EQ(1, cache.network_layer()->transaction_count());
901 EXPECT_EQ(0, cache.disk_cache()->open_count());
902 EXPECT_EQ(1, cache.disk_cache()->create_count());
905 // Tests LOAD_PREFERRING_CACHE in the presence of vary headers.
906 TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMatch) {
907 MockHttpCache cache;
909 // Write to the cache.
910 MockTransaction transaction(kSimpleGET_Transaction);
911 transaction.request_headers = "Foo: bar\r\n";
912 transaction.response_headers = "Cache-Control: max-age=10000\n"
913 "Vary: Foo\n";
914 AddMockTransaction(&transaction);
915 RunTransactionTest(cache.http_cache(), transaction);
917 // Read from the cache.
918 transaction.load_flags |= net::LOAD_PREFERRING_CACHE;
919 RunTransactionTest(cache.http_cache(), transaction);
921 EXPECT_EQ(1, cache.network_layer()->transaction_count());
922 EXPECT_EQ(1, cache.disk_cache()->open_count());
923 EXPECT_EQ(1, cache.disk_cache()->create_count());
924 RemoveMockTransaction(&transaction);
927 // Tests LOAD_PREFERRING_CACHE in the presence of vary headers.
928 TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMismatch) {
929 MockHttpCache cache;
931 // Write to the cache.
932 MockTransaction transaction(kSimpleGET_Transaction);
933 transaction.request_headers = "Foo: bar\r\n";
934 transaction.response_headers = "Cache-Control: max-age=10000\n"
935 "Vary: Foo\n";
936 AddMockTransaction(&transaction);
937 RunTransactionTest(cache.http_cache(), transaction);
939 // Attempt to read from the cache... this is a vary mismatch that must reach
940 // the network again.
941 transaction.load_flags |= net::LOAD_PREFERRING_CACHE;
942 transaction.request_headers = "Foo: none\r\n";
943 net::CapturingBoundNetLog log;
944 net::LoadTimingInfo load_timing_info;
945 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(),
946 &load_timing_info);
948 EXPECT_EQ(2, cache.network_layer()->transaction_count());
949 EXPECT_EQ(1, cache.disk_cache()->open_count());
950 EXPECT_EQ(1, cache.disk_cache()->create_count());
951 TestLoadTimingNetworkRequest(load_timing_info);
952 RemoveMockTransaction(&transaction);
955 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on
956 // network success
957 TEST(HttpCache, SimpleGET_CacheOverride_Network) {
958 MockHttpCache cache;
960 // Prime cache.
961 MockTransaction transaction(kSimpleGET_Transaction);
962 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE;
963 transaction.response_headers = "Cache-Control: no-cache\n";
965 AddMockTransaction(&transaction);
966 RunTransactionTest(cache.http_cache(), transaction);
967 EXPECT_EQ(1, cache.network_layer()->transaction_count());
968 EXPECT_EQ(1, cache.disk_cache()->create_count());
969 RemoveMockTransaction(&transaction);
971 // Re-run transaction; make sure the result came from the network,
972 // not the cache.
973 transaction.data = "Changed data.";
974 AddMockTransaction(&transaction);
975 net::HttpResponseInfo response_info;
976 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
977 &response_info);
979 EXPECT_EQ(2, cache.network_layer()->transaction_count());
980 EXPECT_FALSE(response_info.server_data_unavailable);
981 EXPECT_TRUE(response_info.network_accessed);
983 RemoveMockTransaction(&transaction);
986 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on
987 // offline failure
988 TEST(HttpCache, SimpleGET_CacheOverride_Offline) {
989 MockHttpCache cache;
991 // Prime cache.
992 MockTransaction transaction(kSimpleGET_Transaction);
993 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE;
994 transaction.response_headers = "Cache-Control: no-cache\n";
996 AddMockTransaction(&transaction);
997 RunTransactionTest(cache.http_cache(), transaction);
998 EXPECT_EQ(1, cache.network_layer()->transaction_count());
999 EXPECT_EQ(1, cache.disk_cache()->create_count());
1000 RemoveMockTransaction(&transaction);
1002 // Network failure with offline error; should return cache entry above +
1003 // flag signalling stale data.
1004 transaction.return_code = net::ERR_NAME_NOT_RESOLVED;
1005 AddMockTransaction(&transaction);
1007 MockHttpRequest request(transaction);
1008 net::TestCompletionCallback callback;
1009 scoped_ptr<net::HttpTransaction> trans;
1010 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
1011 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
1012 EXPECT_EQ(net::OK, callback.GetResult(rv));
1014 const net::HttpResponseInfo* response_info = trans->GetResponseInfo();
1015 ASSERT_TRUE(response_info);
1016 EXPECT_TRUE(response_info->server_data_unavailable);
1017 EXPECT_TRUE(response_info->was_cached);
1018 EXPECT_FALSE(response_info->network_accessed);
1019 ReadAndVerifyTransaction(trans.get(), transaction);
1020 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1022 RemoveMockTransaction(&transaction);
1025 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on
1026 // non-offline failure.
1027 TEST(HttpCache, SimpleGET_CacheOverride_NonOffline) {
1028 MockHttpCache cache;
1030 // Prime cache.
1031 MockTransaction transaction(kSimpleGET_Transaction);
1032 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE;
1033 transaction.response_headers = "Cache-Control: no-cache\n";
1035 AddMockTransaction(&transaction);
1036 RunTransactionTest(cache.http_cache(), transaction);
1037 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1038 EXPECT_EQ(1, cache.disk_cache()->create_count());
1039 RemoveMockTransaction(&transaction);
1041 // Network failure with non-offline error; should fail with that error.
1042 transaction.return_code = net::ERR_PROXY_CONNECTION_FAILED;
1043 AddMockTransaction(&transaction);
1045 net::HttpResponseInfo response_info2;
1046 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
1047 &response_info2);
1049 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1050 EXPECT_FALSE(response_info2.server_data_unavailable);
1052 RemoveMockTransaction(&transaction);
1055 // Tests that was_cached was set properly on a failure, even if the cached
1056 // response wasn't returned.
1057 TEST(HttpCache, SimpleGET_CacheSignal_Failure) {
1058 MockHttpCache cache;
1060 // Prime cache.
1061 MockTransaction transaction(kSimpleGET_Transaction);
1062 transaction.response_headers = "Cache-Control: no-cache\n";
1064 AddMockTransaction(&transaction);
1065 RunTransactionTest(cache.http_cache(), transaction);
1066 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1067 EXPECT_EQ(1, cache.disk_cache()->create_count());
1068 RemoveMockTransaction(&transaction);
1070 // Network failure with error; should fail but have was_cached set.
1071 transaction.return_code = net::ERR_FAILED;
1072 AddMockTransaction(&transaction);
1074 MockHttpRequest request(transaction);
1075 net::TestCompletionCallback callback;
1076 scoped_ptr<net::HttpTransaction> trans;
1077 int rv = cache.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, &trans);
1078 EXPECT_EQ(net::OK, rv);
1079 ASSERT_TRUE(trans.get());
1080 rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
1081 EXPECT_EQ(net::ERR_FAILED, callback.GetResult(rv));
1083 const net::HttpResponseInfo* response_info = trans->GetResponseInfo();
1084 ASSERT_TRUE(response_info);
1085 EXPECT_TRUE(response_info->was_cached);
1086 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1088 RemoveMockTransaction(&transaction);
1091 // Confirm if we have an empty cache, a read is marked as network verified.
1092 TEST(HttpCache, SimpleGET_NetworkAccessed_Network) {
1093 MockHttpCache cache;
1095 // write to the cache
1096 net::HttpResponseInfo response_info;
1097 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
1098 &response_info);
1100 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1101 EXPECT_EQ(0, cache.disk_cache()->open_count());
1102 EXPECT_EQ(1, cache.disk_cache()->create_count());
1103 EXPECT_TRUE(response_info.network_accessed);
1106 // Confirm if we have a fresh entry in cache, it isn't marked as
1107 // network verified.
1108 TEST(HttpCache, SimpleGET_NetworkAccessed_Cache) {
1109 MockHttpCache cache;
1111 // Prime cache.
1112 MockTransaction transaction(kSimpleGET_Transaction);
1114 RunTransactionTest(cache.http_cache(), transaction);
1115 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1116 EXPECT_EQ(1, cache.disk_cache()->create_count());
1118 // Re-run transaction; make sure we don't mark the network as accessed.
1119 net::HttpResponseInfo response_info;
1120 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
1121 &response_info);
1123 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1124 EXPECT_FALSE(response_info.server_data_unavailable);
1125 EXPECT_FALSE(response_info.network_accessed);
1128 TEST(HttpCache, SimpleGET_LoadBypassCache) {
1129 MockHttpCache cache;
1131 // Write to the cache.
1132 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1134 // Force this transaction to write to the cache again.
1135 MockTransaction transaction(kSimpleGET_Transaction);
1136 transaction.load_flags |= net::LOAD_BYPASS_CACHE;
1138 net::CapturingBoundNetLog log;
1139 net::LoadTimingInfo load_timing_info;
1141 // Write to the cache.
1142 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(),
1143 &load_timing_info);
1145 // Check that the NetLog was filled as expected.
1146 net::CapturingNetLog::CapturedEntryList entries;
1147 log.GetEntries(&entries);
1148 FilterLogEntries(&entries);
1150 EXPECT_EQ(8u, entries.size());
1151 EXPECT_TRUE(net::LogContainsBeginEvent(
1152 entries, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND));
1153 EXPECT_TRUE(net::LogContainsEndEvent(
1154 entries, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND));
1155 EXPECT_TRUE(net::LogContainsBeginEvent(
1156 entries, 2, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY));
1157 EXPECT_TRUE(net::LogContainsEndEvent(
1158 entries, 3, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY));
1159 EXPECT_TRUE(net::LogContainsBeginEvent(
1160 entries, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY));
1161 EXPECT_TRUE(net::LogContainsEndEvent(
1162 entries, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY));
1163 EXPECT_TRUE(net::LogContainsBeginEvent(
1164 entries, 6, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY));
1165 EXPECT_TRUE(net::LogContainsEndEvent(
1166 entries, 7, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY));
1168 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1169 EXPECT_EQ(0, cache.disk_cache()->open_count());
1170 EXPECT_EQ(2, cache.disk_cache()->create_count());
1171 TestLoadTimingNetworkRequest(load_timing_info);
1174 TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit) {
1175 MockHttpCache cache;
1177 // write to the cache
1178 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1180 // force this transaction to write to the cache again
1181 MockTransaction transaction(kSimpleGET_Transaction);
1182 transaction.request_headers = "pragma: no-cache\r\n";
1184 RunTransactionTest(cache.http_cache(), transaction);
1186 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1187 EXPECT_EQ(0, cache.disk_cache()->open_count());
1188 EXPECT_EQ(2, cache.disk_cache()->create_count());
1191 TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit2) {
1192 MockHttpCache cache;
1194 // write to the cache
1195 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1197 // force this transaction to write to the cache again
1198 MockTransaction transaction(kSimpleGET_Transaction);
1199 transaction.request_headers = "cache-control: no-cache\r\n";
1201 RunTransactionTest(cache.http_cache(), transaction);
1203 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1204 EXPECT_EQ(0, cache.disk_cache()->open_count());
1205 EXPECT_EQ(2, cache.disk_cache()->create_count());
1208 TEST(HttpCache, SimpleGET_LoadValidateCache) {
1209 MockHttpCache cache;
1211 // Write to the cache.
1212 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1214 // Read from the cache.
1215 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1217 // Force this transaction to validate the cache.
1218 MockTransaction transaction(kSimpleGET_Transaction);
1219 transaction.load_flags |= net::LOAD_VALIDATE_CACHE;
1221 net::HttpResponseInfo response_info;
1222 net::CapturingBoundNetLog log;
1223 net::LoadTimingInfo load_timing_info;
1224 RunTransactionTestWithResponseInfoAndGetTiming(
1225 cache.http_cache(), transaction, &response_info, log.bound(),
1226 &load_timing_info);
1228 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1229 EXPECT_EQ(1, cache.disk_cache()->open_count());
1230 EXPECT_EQ(1, cache.disk_cache()->create_count());
1231 EXPECT_TRUE(response_info.network_accessed);
1232 TestLoadTimingNetworkRequest(load_timing_info);
1235 TEST(HttpCache, SimpleGET_LoadValidateCache_Implicit) {
1236 MockHttpCache cache;
1238 // write to the cache
1239 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1241 // read from the cache
1242 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1244 // force this transaction to validate the cache
1245 MockTransaction transaction(kSimpleGET_Transaction);
1246 transaction.request_headers = "cache-control: max-age=0\r\n";
1248 RunTransactionTest(cache.http_cache(), transaction);
1250 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1251 EXPECT_EQ(1, cache.disk_cache()->open_count());
1252 EXPECT_EQ(1, cache.disk_cache()->create_count());
1255 static void PreserveRequestHeaders_Handler(
1256 const net::HttpRequestInfo* request,
1257 std::string* response_status,
1258 std::string* response_headers,
1259 std::string* response_data) {
1260 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey));
1263 // Tests that we don't remove extra headers for simple requests.
1264 TEST(HttpCache, SimpleGET_PreserveRequestHeaders) {
1265 MockHttpCache cache;
1267 MockTransaction transaction(kSimpleGET_Transaction);
1268 transaction.handler = PreserveRequestHeaders_Handler;
1269 transaction.request_headers = EXTRA_HEADER;
1270 transaction.response_headers = "Cache-Control: max-age=0\n";
1271 AddMockTransaction(&transaction);
1273 // Write, then revalidate the entry.
1274 RunTransactionTest(cache.http_cache(), transaction);
1275 RunTransactionTest(cache.http_cache(), transaction);
1277 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1278 EXPECT_EQ(1, cache.disk_cache()->open_count());
1279 EXPECT_EQ(1, cache.disk_cache()->create_count());
1280 RemoveMockTransaction(&transaction);
1283 // Tests that we don't remove extra headers for conditionalized requests.
1284 TEST(HttpCache, ConditionalizedGET_PreserveRequestHeaders) {
1285 MockHttpCache cache;
1287 // Write to the cache.
1288 RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
1290 MockTransaction transaction(kETagGET_Transaction);
1291 transaction.handler = PreserveRequestHeaders_Handler;
1292 transaction.request_headers = "If-None-Match: \"foopy\"\r\n"
1293 EXTRA_HEADER;
1294 AddMockTransaction(&transaction);
1296 RunTransactionTest(cache.http_cache(), transaction);
1298 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1299 EXPECT_EQ(1, cache.disk_cache()->open_count());
1300 EXPECT_EQ(1, cache.disk_cache()->create_count());
1301 RemoveMockTransaction(&transaction);
1304 TEST(HttpCache, SimpleGET_ManyReaders) {
1305 MockHttpCache cache;
1307 MockHttpRequest request(kSimpleGET_Transaction);
1309 std::vector<Context*> context_list;
1310 const int kNumTransactions = 5;
1312 for (int i = 0; i < kNumTransactions; ++i) {
1313 context_list.push_back(new Context());
1314 Context* c = context_list[i];
1316 c->result = cache.CreateTransaction(&c->trans);
1317 ASSERT_EQ(net::OK, c->result);
1318 EXPECT_EQ(net::LOAD_STATE_IDLE, c->trans->GetLoadState());
1320 c->result = c->trans->Start(
1321 &request, c->callback.callback(), net::BoundNetLog());
1324 // All requests are waiting for the active entry.
1325 for (int i = 0; i < kNumTransactions; ++i) {
1326 Context* c = context_list[i];
1327 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
1330 // Allow all requests to move from the Create queue to the active entry.
1331 base::MessageLoop::current()->RunUntilIdle();
1333 // The first request should be a writer at this point, and the subsequent
1334 // requests should be pending.
1336 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1337 EXPECT_EQ(0, cache.disk_cache()->open_count());
1338 EXPECT_EQ(1, cache.disk_cache()->create_count());
1340 // All requests depend on the writer, and the writer is between Start and
1341 // Read, i.e. idle.
1342 for (int i = 0; i < kNumTransactions; ++i) {
1343 Context* c = context_list[i];
1344 EXPECT_EQ(net::LOAD_STATE_IDLE, c->trans->GetLoadState());
1347 for (int i = 0; i < kNumTransactions; ++i) {
1348 Context* c = context_list[i];
1349 if (c->result == net::ERR_IO_PENDING)
1350 c->result = c->callback.WaitForResult();
1351 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1354 // We should not have had to re-open the disk entry
1356 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1357 EXPECT_EQ(0, cache.disk_cache()->open_count());
1358 EXPECT_EQ(1, cache.disk_cache()->create_count());
1360 for (int i = 0; i < kNumTransactions; ++i) {
1361 Context* c = context_list[i];
1362 delete c;
1366 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769.
1367 // If cancelling a request is racing with another request for the same resource
1368 // finishing, we have to make sure that we remove both transactions from the
1369 // entry.
1370 TEST(HttpCache, SimpleGET_RacingReaders) {
1371 MockHttpCache cache;
1373 MockHttpRequest request(kSimpleGET_Transaction);
1374 MockHttpRequest reader_request(kSimpleGET_Transaction);
1375 reader_request.load_flags = net::LOAD_ONLY_FROM_CACHE;
1377 std::vector<Context*> context_list;
1378 const int kNumTransactions = 5;
1380 for (int i = 0; i < kNumTransactions; ++i) {
1381 context_list.push_back(new Context());
1382 Context* c = context_list[i];
1384 c->result = cache.CreateTransaction(&c->trans);
1385 ASSERT_EQ(net::OK, c->result);
1387 MockHttpRequest* this_request = &request;
1388 if (i == 1 || i == 2)
1389 this_request = &reader_request;
1391 c->result = c->trans->Start(
1392 this_request, c->callback.callback(), net::BoundNetLog());
1395 // Allow all requests to move from the Create queue to the active entry.
1396 base::MessageLoop::current()->RunUntilIdle();
1398 // The first request should be a writer at this point, and the subsequent
1399 // requests should be pending.
1401 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1402 EXPECT_EQ(0, cache.disk_cache()->open_count());
1403 EXPECT_EQ(1, cache.disk_cache()->create_count());
1405 Context* c = context_list[0];
1406 ASSERT_EQ(net::ERR_IO_PENDING, c->result);
1407 c->result = c->callback.WaitForResult();
1408 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1410 // Now we have 2 active readers and two queued transactions.
1412 EXPECT_EQ(net::LOAD_STATE_IDLE,
1413 context_list[2]->trans->GetLoadState());
1414 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE,
1415 context_list[3]->trans->GetLoadState());
1417 c = context_list[1];
1418 ASSERT_EQ(net::ERR_IO_PENDING, c->result);
1419 c->result = c->callback.WaitForResult();
1420 if (c->result == net::OK)
1421 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1423 // At this point we have one reader, two pending transactions and a task on
1424 // the queue to move to the next transaction. Now we cancel the request that
1425 // is the current reader, and expect the queued task to be able to start the
1426 // next request.
1428 c = context_list[2];
1429 c->trans.reset();
1431 for (int i = 3; i < kNumTransactions; ++i) {
1432 Context* c = context_list[i];
1433 if (c->result == net::ERR_IO_PENDING)
1434 c->result = c->callback.WaitForResult();
1435 if (c->result == net::OK)
1436 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1439 // We should not have had to re-open the disk entry.
1441 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1442 EXPECT_EQ(0, cache.disk_cache()->open_count());
1443 EXPECT_EQ(1, cache.disk_cache()->create_count());
1445 for (int i = 0; i < kNumTransactions; ++i) {
1446 Context* c = context_list[i];
1447 delete c;
1451 // Tests that we can doom an entry with pending transactions and delete one of
1452 // the pending transactions before the first one completes.
1453 // See http://code.google.com/p/chromium/issues/detail?id=25588
1454 TEST(HttpCache, SimpleGET_DoomWithPending) {
1455 // We need simultaneous doomed / not_doomed entries so let's use a real cache.
1456 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(1024 * 1024));
1458 MockHttpRequest request(kSimpleGET_Transaction);
1459 MockHttpRequest writer_request(kSimpleGET_Transaction);
1460 writer_request.load_flags = net::LOAD_BYPASS_CACHE;
1462 ScopedVector<Context> context_list;
1463 const int kNumTransactions = 4;
1465 for (int i = 0; i < kNumTransactions; ++i) {
1466 context_list.push_back(new Context());
1467 Context* c = context_list[i];
1469 c->result = cache.CreateTransaction(&c->trans);
1470 ASSERT_EQ(net::OK, c->result);
1472 MockHttpRequest* this_request = &request;
1473 if (i == 3)
1474 this_request = &writer_request;
1476 c->result = c->trans->Start(
1477 this_request, c->callback.callback(), net::BoundNetLog());
1480 // The first request should be a writer at this point, and the two subsequent
1481 // requests should be pending. The last request doomed the first entry.
1483 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1485 // Cancel the first queued transaction.
1486 delete context_list[1];
1487 context_list.get()[1] = NULL;
1489 for (int i = 0; i < kNumTransactions; ++i) {
1490 if (i == 1)
1491 continue;
1492 Context* c = context_list[i];
1493 ASSERT_EQ(net::ERR_IO_PENDING, c->result);
1494 c->result = c->callback.WaitForResult();
1495 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1499 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4731.
1500 // We may attempt to delete an entry synchronously with the act of adding a new
1501 // transaction to said entry.
1502 TEST(HttpCache, FastNoStoreGET_DoneWithPending) {
1503 MockHttpCache cache;
1505 // The headers will be served right from the call to Start() the request.
1506 MockHttpRequest request(kFastNoStoreGET_Transaction);
1507 FastTransactionServer request_handler;
1508 AddMockTransaction(&kFastNoStoreGET_Transaction);
1510 std::vector<Context*> context_list;
1511 const int kNumTransactions = 3;
1513 for (int i = 0; i < kNumTransactions; ++i) {
1514 context_list.push_back(new Context());
1515 Context* c = context_list[i];
1517 c->result = cache.CreateTransaction(&c->trans);
1518 ASSERT_EQ(net::OK, c->result);
1520 c->result = c->trans->Start(
1521 &request, c->callback.callback(), net::BoundNetLog());
1524 // Allow all requests to move from the Create queue to the active entry.
1525 base::MessageLoop::current()->RunUntilIdle();
1527 // The first request should be a writer at this point, and the subsequent
1528 // requests should be pending.
1530 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1531 EXPECT_EQ(0, cache.disk_cache()->open_count());
1532 EXPECT_EQ(1, cache.disk_cache()->create_count());
1534 // Now, make sure that the second request asks for the entry not to be stored.
1535 request_handler.set_no_store(true);
1537 for (int i = 0; i < kNumTransactions; ++i) {
1538 Context* c = context_list[i];
1539 if (c->result == net::ERR_IO_PENDING)
1540 c->result = c->callback.WaitForResult();
1541 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction);
1542 delete c;
1545 EXPECT_EQ(3, cache.network_layer()->transaction_count());
1546 EXPECT_EQ(0, cache.disk_cache()->open_count());
1547 EXPECT_EQ(2, cache.disk_cache()->create_count());
1549 RemoveMockTransaction(&kFastNoStoreGET_Transaction);
1552 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) {
1553 MockHttpCache cache;
1555 MockHttpRequest request(kSimpleGET_Transaction);
1557 std::vector<Context*> context_list;
1558 const int kNumTransactions = 2;
1560 for (int i = 0; i < kNumTransactions; ++i) {
1561 context_list.push_back(new Context());
1562 Context* c = context_list[i];
1564 c->result = cache.CreateTransaction(&c->trans);
1565 ASSERT_EQ(net::OK, c->result);
1567 c->result = c->trans->Start(
1568 &request, c->callback.callback(), net::BoundNetLog());
1571 // Allow all requests to move from the Create queue to the active entry.
1572 base::MessageLoop::current()->RunUntilIdle();
1574 // The first request should be a writer at this point, and the subsequent
1575 // requests should be pending.
1577 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1578 EXPECT_EQ(0, cache.disk_cache()->open_count());
1579 EXPECT_EQ(1, cache.disk_cache()->create_count());
1581 for (int i = 0; i < kNumTransactions; ++i) {
1582 Context* c = context_list[i];
1583 if (c->result == net::ERR_IO_PENDING)
1584 c->result = c->callback.WaitForResult();
1585 // Destroy only the first transaction.
1586 if (i == 0) {
1587 delete c;
1588 context_list[i] = NULL;
1592 // Complete the rest of the transactions.
1593 for (int i = 1; i < kNumTransactions; ++i) {
1594 Context* c = context_list[i];
1595 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1598 // We should have had to re-open the disk entry.
1600 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1601 EXPECT_EQ(0, cache.disk_cache()->open_count());
1602 EXPECT_EQ(2, cache.disk_cache()->create_count());
1604 for (int i = 1; i < kNumTransactions; ++i) {
1605 Context* c = context_list[i];
1606 delete c;
1610 // Tests that we can cancel requests that are queued waiting to open the disk
1611 // cache entry.
1612 TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) {
1613 MockHttpCache cache;
1615 MockHttpRequest request(kSimpleGET_Transaction);
1617 std::vector<Context*> context_list;
1618 const int kNumTransactions = 5;
1620 for (int i = 0; i < kNumTransactions; i++) {
1621 context_list.push_back(new Context());
1622 Context* c = context_list[i];
1624 c->result = cache.CreateTransaction(&c->trans);
1625 ASSERT_EQ(net::OK, c->result);
1627 c->result = c->trans->Start(
1628 &request, c->callback.callback(), net::BoundNetLog());
1631 // The first request should be creating the disk cache entry and the others
1632 // should be pending.
1634 EXPECT_EQ(0, cache.network_layer()->transaction_count());
1635 EXPECT_EQ(0, cache.disk_cache()->open_count());
1636 EXPECT_EQ(1, cache.disk_cache()->create_count());
1638 // Cancel a request from the pending queue.
1639 delete context_list[3];
1640 context_list[3] = NULL;
1642 // Cancel the request that is creating the entry. This will force the pending
1643 // operations to restart.
1644 delete context_list[0];
1645 context_list[0] = NULL;
1647 // Complete the rest of the transactions.
1648 for (int i = 1; i < kNumTransactions; i++) {
1649 Context* c = context_list[i];
1650 if (c) {
1651 c->result = c->callback.GetResult(c->result);
1652 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1656 // We should have had to re-create the disk entry.
1658 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1659 EXPECT_EQ(0, cache.disk_cache()->open_count());
1660 EXPECT_EQ(2, cache.disk_cache()->create_count());
1662 for (int i = 1; i < kNumTransactions; ++i) {
1663 delete context_list[i];
1667 // Tests that we can cancel a single request to open a disk cache entry.
1668 TEST(HttpCache, SimpleGET_CancelCreate) {
1669 MockHttpCache cache;
1671 MockHttpRequest request(kSimpleGET_Transaction);
1673 Context* c = new Context();
1675 c->result = cache.CreateTransaction(&c->trans);
1676 ASSERT_EQ(net::OK, c->result);
1678 c->result = c->trans->Start(
1679 &request, c->callback.callback(), net::BoundNetLog());
1680 EXPECT_EQ(net::ERR_IO_PENDING, c->result);
1682 // Release the reference that the mock disk cache keeps for this entry, so
1683 // that we test that the http cache handles the cancellation correctly.
1684 cache.disk_cache()->ReleaseAll();
1685 delete c;
1687 base::MessageLoop::current()->RunUntilIdle();
1688 EXPECT_EQ(1, cache.disk_cache()->create_count());
1691 // Tests that we delete/create entries even if multiple requests are queued.
1692 TEST(HttpCache, SimpleGET_ManyWriters_BypassCache) {
1693 MockHttpCache cache;
1695 MockHttpRequest request(kSimpleGET_Transaction);
1696 request.load_flags = net::LOAD_BYPASS_CACHE;
1698 std::vector<Context*> context_list;
1699 const int kNumTransactions = 5;
1701 for (int i = 0; i < kNumTransactions; i++) {
1702 context_list.push_back(new Context());
1703 Context* c = context_list[i];
1705 c->result = cache.CreateTransaction(&c->trans);
1706 ASSERT_EQ(net::OK, c->result);
1708 c->result = c->trans->Start(
1709 &request, c->callback.callback(), net::BoundNetLog());
1712 // The first request should be deleting the disk cache entry and the others
1713 // should be pending.
1715 EXPECT_EQ(0, cache.network_layer()->transaction_count());
1716 EXPECT_EQ(0, cache.disk_cache()->open_count());
1717 EXPECT_EQ(0, cache.disk_cache()->create_count());
1719 // Complete the transactions.
1720 for (int i = 0; i < kNumTransactions; i++) {
1721 Context* c = context_list[i];
1722 c->result = c->callback.GetResult(c->result);
1723 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1726 // We should have had to re-create the disk entry multiple times.
1728 EXPECT_EQ(5, cache.network_layer()->transaction_count());
1729 EXPECT_EQ(0, cache.disk_cache()->open_count());
1730 EXPECT_EQ(5, cache.disk_cache()->create_count());
1732 for (int i = 0; i < kNumTransactions; ++i) {
1733 delete context_list[i];
1737 // Tests that a (simulated) timeout allows transactions waiting on the cache
1738 // lock to continue.
1739 TEST(HttpCache, SimpleGET_WriterTimeout) {
1740 MockHttpCache cache;
1741 cache.BypassCacheLock();
1743 MockHttpRequest request(kSimpleGET_Transaction);
1744 Context c1, c2;
1745 ASSERT_EQ(net::OK, cache.CreateTransaction(&c1.trans));
1746 ASSERT_EQ(net::ERR_IO_PENDING,
1747 c1.trans->Start(&request, c1.callback.callback(),
1748 net::BoundNetLog()));
1749 ASSERT_EQ(net::OK, cache.CreateTransaction(&c2.trans));
1750 ASSERT_EQ(net::ERR_IO_PENDING,
1751 c2.trans->Start(&request, c2.callback.callback(),
1752 net::BoundNetLog()));
1754 // The second request is queued after the first one.
1756 c2.callback.WaitForResult();
1757 ReadAndVerifyTransaction(c2.trans.get(), kSimpleGET_Transaction);
1759 // Complete the first transaction.
1760 c1.callback.WaitForResult();
1761 ReadAndVerifyTransaction(c1.trans.get(), kSimpleGET_Transaction);
1764 TEST(HttpCache, SimpleGET_AbandonedCacheRead) {
1765 MockHttpCache cache;
1767 // write to the cache
1768 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1770 MockHttpRequest request(kSimpleGET_Transaction);
1771 net::TestCompletionCallback callback;
1773 scoped_ptr<net::HttpTransaction> trans;
1774 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
1775 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
1776 if (rv == net::ERR_IO_PENDING)
1777 rv = callback.WaitForResult();
1778 ASSERT_EQ(net::OK, rv);
1780 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256));
1781 rv = trans->Read(buf.get(), 256, callback.callback());
1782 EXPECT_EQ(net::ERR_IO_PENDING, rv);
1784 // Test that destroying the transaction while it is reading from the cache
1785 // works properly.
1786 trans.reset();
1788 // Make sure we pump any pending events, which should include a call to
1789 // HttpCache::Transaction::OnCacheReadCompleted.
1790 base::MessageLoop::current()->RunUntilIdle();
1793 // Tests that we can delete the HttpCache and deal with queued transactions
1794 // ("waiting for the backend" as opposed to Active or Doomed entries).
1795 TEST(HttpCache, SimpleGET_ManyWriters_DeleteCache) {
1796 scoped_ptr<MockHttpCache> cache(new MockHttpCache(
1797 new MockBackendNoCbFactory()));
1799 MockHttpRequest request(kSimpleGET_Transaction);
1801 std::vector<Context*> context_list;
1802 const int kNumTransactions = 5;
1804 for (int i = 0; i < kNumTransactions; i++) {
1805 context_list.push_back(new Context());
1806 Context* c = context_list[i];
1808 c->result = cache->CreateTransaction(&c->trans);
1809 ASSERT_EQ(net::OK, c->result);
1811 c->result = c->trans->Start(
1812 &request, c->callback.callback(), net::BoundNetLog());
1815 // The first request should be creating the disk cache entry and the others
1816 // should be pending.
1818 EXPECT_EQ(0, cache->network_layer()->transaction_count());
1819 EXPECT_EQ(0, cache->disk_cache()->open_count());
1820 EXPECT_EQ(0, cache->disk_cache()->create_count());
1822 cache.reset();
1824 // There is not much to do with the transactions at this point... they are
1825 // waiting for a callback that will not fire.
1826 for (int i = 0; i < kNumTransactions; ++i) {
1827 delete context_list[i];
1831 // Tests that we queue requests when initializing the backend.
1832 TEST(HttpCache, SimpleGET_WaitForBackend) {
1833 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
1834 MockHttpCache cache(factory);
1836 MockHttpRequest request0(kSimpleGET_Transaction);
1837 MockHttpRequest request1(kTypicalGET_Transaction);
1838 MockHttpRequest request2(kETagGET_Transaction);
1840 std::vector<Context*> context_list;
1841 const int kNumTransactions = 3;
1843 for (int i = 0; i < kNumTransactions; i++) {
1844 context_list.push_back(new Context());
1845 Context* c = context_list[i];
1847 c->result = cache.CreateTransaction(&c->trans);
1848 ASSERT_EQ(net::OK, c->result);
1851 context_list[0]->result = context_list[0]->trans->Start(
1852 &request0, context_list[0]->callback.callback(), net::BoundNetLog());
1853 context_list[1]->result = context_list[1]->trans->Start(
1854 &request1, context_list[1]->callback.callback(), net::BoundNetLog());
1855 context_list[2]->result = context_list[2]->trans->Start(
1856 &request2, context_list[2]->callback.callback(), net::BoundNetLog());
1858 // Just to make sure that everything is still pending.
1859 base::MessageLoop::current()->RunUntilIdle();
1861 // The first request should be creating the disk cache.
1862 EXPECT_FALSE(context_list[0]->callback.have_result());
1864 factory->FinishCreation();
1866 base::MessageLoop::current()->RunUntilIdle();
1867 EXPECT_EQ(3, cache.network_layer()->transaction_count());
1868 EXPECT_EQ(3, cache.disk_cache()->create_count());
1870 for (int i = 0; i < kNumTransactions; ++i) {
1871 EXPECT_TRUE(context_list[i]->callback.have_result());
1872 delete context_list[i];
1876 // Tests that we can cancel requests that are queued waiting for the backend
1877 // to be initialized.
1878 TEST(HttpCache, SimpleGET_WaitForBackend_CancelCreate) {
1879 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
1880 MockHttpCache cache(factory);
1882 MockHttpRequest request0(kSimpleGET_Transaction);
1883 MockHttpRequest request1(kTypicalGET_Transaction);
1884 MockHttpRequest request2(kETagGET_Transaction);
1886 std::vector<Context*> context_list;
1887 const int kNumTransactions = 3;
1889 for (int i = 0; i < kNumTransactions; i++) {
1890 context_list.push_back(new Context());
1891 Context* c = context_list[i];
1893 c->result = cache.CreateTransaction(&c->trans);
1894 ASSERT_EQ(net::OK, c->result);
1897 context_list[0]->result = context_list[0]->trans->Start(
1898 &request0, context_list[0]->callback.callback(), net::BoundNetLog());
1899 context_list[1]->result = context_list[1]->trans->Start(
1900 &request1, context_list[1]->callback.callback(), net::BoundNetLog());
1901 context_list[2]->result = context_list[2]->trans->Start(
1902 &request2, context_list[2]->callback.callback(), net::BoundNetLog());
1904 // Just to make sure that everything is still pending.
1905 base::MessageLoop::current()->RunUntilIdle();
1907 // The first request should be creating the disk cache.
1908 EXPECT_FALSE(context_list[0]->callback.have_result());
1910 // Cancel a request from the pending queue.
1911 delete context_list[1];
1912 context_list[1] = NULL;
1914 // Cancel the request that is creating the entry.
1915 delete context_list[0];
1916 context_list[0] = NULL;
1918 // Complete the last transaction.
1919 factory->FinishCreation();
1921 context_list[2]->result =
1922 context_list[2]->callback.GetResult(context_list[2]->result);
1923 ReadAndVerifyTransaction(context_list[2]->trans.get(), kETagGET_Transaction);
1925 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1926 EXPECT_EQ(1, cache.disk_cache()->create_count());
1928 delete context_list[2];
1931 // Tests that we can delete the cache while creating the backend.
1932 TEST(HttpCache, DeleteCacheWaitingForBackend) {
1933 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
1934 scoped_ptr<MockHttpCache> cache(new MockHttpCache(factory));
1936 MockHttpRequest request(kSimpleGET_Transaction);
1938 scoped_ptr<Context> c(new Context());
1939 c->result = cache->CreateTransaction(&c->trans);
1940 ASSERT_EQ(net::OK, c->result);
1942 c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
1944 // Just to make sure that everything is still pending.
1945 base::MessageLoop::current()->RunUntilIdle();
1947 // The request should be creating the disk cache.
1948 EXPECT_FALSE(c->callback.have_result());
1950 // We cannot call FinishCreation because the factory itself will go away with
1951 // the cache, so grab the callback and attempt to use it.
1952 net::CompletionCallback callback = factory->callback();
1953 scoped_ptr<disk_cache::Backend>* backend = factory->backend();
1955 cache.reset();
1956 base::MessageLoop::current()->RunUntilIdle();
1958 backend->reset();
1959 callback.Run(net::ERR_ABORTED);
1962 // Tests that we can delete the cache while creating the backend, from within
1963 // one of the callbacks.
1964 TEST(HttpCache, DeleteCacheWaitingForBackend2) {
1965 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
1966 MockHttpCache* cache = new MockHttpCache(factory);
1968 DeleteCacheCompletionCallback cb(cache);
1969 disk_cache::Backend* backend;
1970 int rv = cache->http_cache()->GetBackend(&backend, cb.callback());
1971 EXPECT_EQ(net::ERR_IO_PENDING, rv);
1973 // Now let's queue a regular transaction
1974 MockHttpRequest request(kSimpleGET_Transaction);
1976 scoped_ptr<Context> c(new Context());
1977 c->result = cache->CreateTransaction(&c->trans);
1978 ASSERT_EQ(net::OK, c->result);
1980 c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
1982 // And another direct backend request.
1983 net::TestCompletionCallback cb2;
1984 rv = cache->http_cache()->GetBackend(&backend, cb2.callback());
1985 EXPECT_EQ(net::ERR_IO_PENDING, rv);
1987 // Just to make sure that everything is still pending.
1988 base::MessageLoop::current()->RunUntilIdle();
1990 // The request should be queued.
1991 EXPECT_FALSE(c->callback.have_result());
1993 // Generate the callback.
1994 factory->FinishCreation();
1995 rv = cb.WaitForResult();
1997 // The cache should be gone by now.
1998 base::MessageLoop::current()->RunUntilIdle();
1999 EXPECT_EQ(net::OK, c->callback.GetResult(c->result));
2000 EXPECT_FALSE(cb2.have_result());
2003 TEST(HttpCache, TypicalGET_ConditionalRequest) {
2004 MockHttpCache cache;
2006 // write to the cache
2007 RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction);
2009 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2010 EXPECT_EQ(0, cache.disk_cache()->open_count());
2011 EXPECT_EQ(1, cache.disk_cache()->create_count());
2013 // Get the same URL again, but this time we expect it to result
2014 // in a conditional request.
2015 net::CapturingBoundNetLog log;
2016 net::LoadTimingInfo load_timing_info;
2017 RunTransactionTestAndGetTiming(cache.http_cache(), kTypicalGET_Transaction,
2018 log.bound(), &load_timing_info);
2020 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2021 EXPECT_EQ(1, cache.disk_cache()->open_count());
2022 EXPECT_EQ(1, cache.disk_cache()->create_count());
2023 TestLoadTimingNetworkRequest(load_timing_info);
2026 static void ETagGet_ConditionalRequest_Handler(
2027 const net::HttpRequestInfo* request,
2028 std::string* response_status,
2029 std::string* response_headers,
2030 std::string* response_data) {
2031 EXPECT_TRUE(
2032 request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch));
2033 response_status->assign("HTTP/1.1 304 Not Modified");
2034 response_headers->assign(kETagGET_Transaction.response_headers);
2035 response_data->clear();
2038 TEST(HttpCache, ETagGET_ConditionalRequest_304) {
2039 MockHttpCache cache;
2041 ScopedMockTransaction transaction(kETagGET_Transaction);
2043 // write to the cache
2044 RunTransactionTest(cache.http_cache(), transaction);
2046 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2047 EXPECT_EQ(0, cache.disk_cache()->open_count());
2048 EXPECT_EQ(1, cache.disk_cache()->create_count());
2050 // Get the same URL again, but this time we expect it to result
2051 // in a conditional request.
2052 transaction.load_flags = net::LOAD_VALIDATE_CACHE;
2053 transaction.handler = ETagGet_ConditionalRequest_Handler;
2054 net::CapturingBoundNetLog log;
2055 net::LoadTimingInfo load_timing_info;
2056 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(),
2057 &load_timing_info);
2059 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2060 EXPECT_EQ(1, cache.disk_cache()->open_count());
2061 EXPECT_EQ(1, cache.disk_cache()->create_count());
2062 TestLoadTimingNetworkRequest(load_timing_info);
2065 class RevalidationServer {
2066 public:
2067 RevalidationServer() {
2068 s_etag_used_ = false;
2069 s_last_modified_used_ = false;
2072 bool EtagUsed() { return s_etag_used_; }
2073 bool LastModifiedUsed() { return s_last_modified_used_; }
2075 static void Handler(const net::HttpRequestInfo* request,
2076 std::string* response_status,
2077 std::string* response_headers,
2078 std::string* response_data);
2080 private:
2081 static bool s_etag_used_;
2082 static bool s_last_modified_used_;
2084 bool RevalidationServer::s_etag_used_ = false;
2085 bool RevalidationServer::s_last_modified_used_ = false;
2087 void RevalidationServer::Handler(const net::HttpRequestInfo* request,
2088 std::string* response_status,
2089 std::string* response_headers,
2090 std::string* response_data) {
2091 if (request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch))
2092 s_etag_used_ = true;
2094 if (request->extra_headers.HasHeader(
2095 net::HttpRequestHeaders::kIfModifiedSince)) {
2096 s_last_modified_used_ = true;
2099 if (s_etag_used_ || s_last_modified_used_) {
2100 response_status->assign("HTTP/1.1 304 Not Modified");
2101 response_headers->assign(kTypicalGET_Transaction.response_headers);
2102 response_data->clear();
2103 } else {
2104 response_status->assign(kTypicalGET_Transaction.status);
2105 response_headers->assign(kTypicalGET_Transaction.response_headers);
2106 response_data->assign(kTypicalGET_Transaction.data);
2110 // Tests revalidation after a vary match.
2111 TEST(HttpCache, SimpleGET_LoadValidateCache_VaryMatch) {
2112 MockHttpCache cache;
2114 // Write to the cache.
2115 MockTransaction transaction(kTypicalGET_Transaction);
2116 transaction.request_headers = "Foo: bar\r\n";
2117 transaction.response_headers =
2118 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
2119 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
2120 "Etag: \"foopy\"\n"
2121 "Cache-Control: max-age=0\n"
2122 "Vary: Foo\n";
2123 AddMockTransaction(&transaction);
2124 RunTransactionTest(cache.http_cache(), transaction);
2126 // Read from the cache.
2127 RevalidationServer server;
2128 transaction.handler = server.Handler;
2129 net::CapturingBoundNetLog log;
2130 net::LoadTimingInfo load_timing_info;
2131 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(),
2132 &load_timing_info);
2134 EXPECT_TRUE(server.EtagUsed());
2135 EXPECT_TRUE(server.LastModifiedUsed());
2136 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2137 EXPECT_EQ(1, cache.disk_cache()->open_count());
2138 EXPECT_EQ(1, cache.disk_cache()->create_count());
2139 TestLoadTimingNetworkRequest(load_timing_info);
2140 RemoveMockTransaction(&transaction);
2143 // Tests revalidation after a vary mismatch if etag is present.
2144 TEST(HttpCache, SimpleGET_LoadValidateCache_VaryMismatch) {
2145 MockHttpCache cache;
2147 // Write to the cache.
2148 MockTransaction transaction(kTypicalGET_Transaction);
2149 transaction.request_headers = "Foo: bar\r\n";
2150 transaction.response_headers =
2151 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
2152 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
2153 "Etag: \"foopy\"\n"
2154 "Cache-Control: max-age=0\n"
2155 "Vary: Foo\n";
2156 AddMockTransaction(&transaction);
2157 RunTransactionTest(cache.http_cache(), transaction);
2159 // Read from the cache and revalidate the entry.
2160 RevalidationServer server;
2161 transaction.handler = server.Handler;
2162 transaction.request_headers = "Foo: none\r\n";
2163 net::CapturingBoundNetLog log;
2164 net::LoadTimingInfo load_timing_info;
2165 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(),
2166 &load_timing_info);
2168 EXPECT_TRUE(server.EtagUsed());
2169 EXPECT_FALSE(server.LastModifiedUsed());
2170 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2171 EXPECT_EQ(1, cache.disk_cache()->open_count());
2172 EXPECT_EQ(1, cache.disk_cache()->create_count());
2173 TestLoadTimingNetworkRequest(load_timing_info);
2174 RemoveMockTransaction(&transaction);
2177 // Tests lack of revalidation after a vary mismatch and no etag.
2178 TEST(HttpCache, SimpleGET_LoadDontValidateCache_VaryMismatch) {
2179 MockHttpCache cache;
2181 // Write to the cache.
2182 MockTransaction transaction(kTypicalGET_Transaction);
2183 transaction.request_headers = "Foo: bar\r\n";
2184 transaction.response_headers =
2185 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
2186 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
2187 "Cache-Control: max-age=0\n"
2188 "Vary: Foo\n";
2189 AddMockTransaction(&transaction);
2190 RunTransactionTest(cache.http_cache(), transaction);
2192 // Read from the cache and don't revalidate the entry.
2193 RevalidationServer server;
2194 transaction.handler = server.Handler;
2195 transaction.request_headers = "Foo: none\r\n";
2196 net::CapturingBoundNetLog log;
2197 net::LoadTimingInfo load_timing_info;
2198 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(),
2199 &load_timing_info);
2201 EXPECT_FALSE(server.EtagUsed());
2202 EXPECT_FALSE(server.LastModifiedUsed());
2203 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2204 EXPECT_EQ(1, cache.disk_cache()->open_count());
2205 EXPECT_EQ(1, cache.disk_cache()->create_count());
2206 TestLoadTimingNetworkRequest(load_timing_info);
2207 RemoveMockTransaction(&transaction);
2210 static void ETagGet_UnconditionalRequest_Handler(
2211 const net::HttpRequestInfo* request,
2212 std::string* response_status,
2213 std::string* response_headers,
2214 std::string* response_data) {
2215 EXPECT_FALSE(
2216 request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch));
2219 TEST(HttpCache, ETagGET_Http10) {
2220 MockHttpCache cache;
2222 ScopedMockTransaction transaction(kETagGET_Transaction);
2223 transaction.status = "HTTP/1.0 200 OK";
2225 // Write to the cache.
2226 RunTransactionTest(cache.http_cache(), transaction);
2228 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2229 EXPECT_EQ(0, cache.disk_cache()->open_count());
2230 EXPECT_EQ(1, cache.disk_cache()->create_count());
2232 // Get the same URL again, without generating a conditional request.
2233 transaction.load_flags = net::LOAD_VALIDATE_CACHE;
2234 transaction.handler = ETagGet_UnconditionalRequest_Handler;
2235 RunTransactionTest(cache.http_cache(), transaction);
2237 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2238 EXPECT_EQ(1, cache.disk_cache()->open_count());
2239 EXPECT_EQ(1, cache.disk_cache()->create_count());
2242 TEST(HttpCache, ETagGET_Http10_Range) {
2243 MockHttpCache cache;
2245 ScopedMockTransaction transaction(kETagGET_Transaction);
2246 transaction.status = "HTTP/1.0 200 OK";
2248 // Write to the cache.
2249 RunTransactionTest(cache.http_cache(), transaction);
2251 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2252 EXPECT_EQ(0, cache.disk_cache()->open_count());
2253 EXPECT_EQ(1, cache.disk_cache()->create_count());
2255 // Get the same URL again, but use a byte range request.
2256 transaction.load_flags = net::LOAD_VALIDATE_CACHE;
2257 transaction.handler = ETagGet_UnconditionalRequest_Handler;
2258 transaction.request_headers = "Range: bytes = 5-\r\n";
2259 RunTransactionTest(cache.http_cache(), transaction);
2261 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2262 EXPECT_EQ(1, cache.disk_cache()->open_count());
2263 EXPECT_EQ(2, cache.disk_cache()->create_count());
2266 static void ETagGet_ConditionalRequest_NoStore_Handler(
2267 const net::HttpRequestInfo* request,
2268 std::string* response_status,
2269 std::string* response_headers,
2270 std::string* response_data) {
2271 EXPECT_TRUE(
2272 request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch));
2273 response_status->assign("HTTP/1.1 304 Not Modified");
2274 response_headers->assign("Cache-Control: no-store\n");
2275 response_data->clear();
2278 TEST(HttpCache, ETagGET_ConditionalRequest_304_NoStore) {
2279 MockHttpCache cache;
2281 ScopedMockTransaction transaction(kETagGET_Transaction);
2283 // Write to the cache.
2284 RunTransactionTest(cache.http_cache(), transaction);
2286 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2287 EXPECT_EQ(0, cache.disk_cache()->open_count());
2288 EXPECT_EQ(1, cache.disk_cache()->create_count());
2290 // Get the same URL again, but this time we expect it to result
2291 // in a conditional request.
2292 transaction.load_flags = net::LOAD_VALIDATE_CACHE;
2293 transaction.handler = ETagGet_ConditionalRequest_NoStore_Handler;
2294 RunTransactionTest(cache.http_cache(), transaction);
2296 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2297 EXPECT_EQ(1, cache.disk_cache()->open_count());
2298 EXPECT_EQ(1, cache.disk_cache()->create_count());
2300 ScopedMockTransaction transaction2(kETagGET_Transaction);
2302 // Write to the cache again. This should create a new entry.
2303 RunTransactionTest(cache.http_cache(), transaction2);
2305 EXPECT_EQ(3, cache.network_layer()->transaction_count());
2306 EXPECT_EQ(1, cache.disk_cache()->open_count());
2307 EXPECT_EQ(2, cache.disk_cache()->create_count());
2310 // Helper that does 4 requests using HttpCache:
2312 // (1) loads |kUrl| -- expects |net_response_1| to be returned.
2313 // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned.
2314 // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to
2315 // be returned.
2316 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be
2317 // returned.
2318 static void ConditionalizedRequestUpdatesCacheHelper(
2319 const Response& net_response_1,
2320 const Response& net_response_2,
2321 const Response& cached_response_2,
2322 const char* extra_request_headers) {
2323 MockHttpCache cache;
2325 // The URL we will be requesting.
2326 const char kUrl[] = "http://foobar.com/main.css";
2328 // Junk network response.
2329 static const Response kUnexpectedResponse = {
2330 "HTTP/1.1 500 Unexpected",
2331 "Server: unexpected_header",
2332 "unexpected body"
2335 // We will control the network layer's responses for |kUrl| using
2336 // |mock_network_response|.
2337 MockTransaction mock_network_response = { 0 };
2338 mock_network_response.url = kUrl;
2339 AddMockTransaction(&mock_network_response);
2341 // Request |kUrl| for the first time. It should hit the network and
2342 // receive |kNetResponse1|, which it saves into the HTTP cache.
2344 MockTransaction request = { 0 };
2345 request.url = kUrl;
2346 request.method = "GET";
2347 request.request_headers = "";
2349 net_response_1.AssignTo(&mock_network_response); // Network mock.
2350 net_response_1.AssignTo(&request); // Expected result.
2352 std::string response_headers;
2353 RunTransactionTestWithResponse(
2354 cache.http_cache(), request, &response_headers);
2356 EXPECT_EQ(net_response_1.status_and_headers(), response_headers);
2357 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2358 EXPECT_EQ(0, cache.disk_cache()->open_count());
2359 EXPECT_EQ(1, cache.disk_cache()->create_count());
2361 // Request |kUrl| a second time. Now |kNetResponse1| it is in the HTTP
2362 // cache, so we don't hit the network.
2364 request.load_flags = net::LOAD_ONLY_FROM_CACHE;
2366 kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock.
2367 net_response_1.AssignTo(&request); // Expected result.
2369 RunTransactionTestWithResponse(
2370 cache.http_cache(), request, &response_headers);
2372 EXPECT_EQ(net_response_1.status_and_headers(), response_headers);
2373 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2374 EXPECT_EQ(1, cache.disk_cache()->open_count());
2375 EXPECT_EQ(1, cache.disk_cache()->create_count());
2377 // Request |kUrl| yet again, but this time give the request an
2378 // "If-Modified-Since" header. This will cause the request to re-hit the
2379 // network. However now the network response is going to be
2380 // different -- this simulates a change made to the CSS file.
2382 request.request_headers = extra_request_headers;
2383 request.load_flags = net::LOAD_NORMAL;
2385 net_response_2.AssignTo(&mock_network_response); // Network mock.
2386 net_response_2.AssignTo(&request); // Expected result.
2388 RunTransactionTestWithResponse(
2389 cache.http_cache(), request, &response_headers);
2391 EXPECT_EQ(net_response_2.status_and_headers(), response_headers);
2392 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2393 EXPECT_EQ(1, cache.disk_cache()->open_count());
2394 EXPECT_EQ(1, cache.disk_cache()->create_count());
2396 // Finally, request |kUrl| again. This request should be serviced from
2397 // the cache. Moreover, the value in the cache should be |kNetResponse2|
2398 // and NOT |kNetResponse1|. The previous step should have replaced the
2399 // value in the cache with the modified response.
2401 request.request_headers = "";
2402 request.load_flags = net::LOAD_ONLY_FROM_CACHE;
2404 kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock.
2405 cached_response_2.AssignTo(&request); // Expected result.
2407 RunTransactionTestWithResponse(
2408 cache.http_cache(), request, &response_headers);
2410 EXPECT_EQ(cached_response_2.status_and_headers(), response_headers);
2411 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2412 EXPECT_EQ(2, cache.disk_cache()->open_count());
2413 EXPECT_EQ(1, cache.disk_cache()->create_count());
2415 RemoveMockTransaction(&mock_network_response);
2418 // Check that when an "if-modified-since" header is attached
2419 // to the request, the result still updates the cached entry.
2420 TEST(HttpCache, ConditionalizedRequestUpdatesCache1) {
2421 // First network response for |kUrl|.
2422 static const Response kNetResponse1 = {
2423 "HTTP/1.1 200 OK",
2424 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2425 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2426 "body1"
2429 // Second network response for |kUrl|.
2430 static const Response kNetResponse2 = {
2431 "HTTP/1.1 200 OK",
2432 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2433 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
2434 "body2"
2437 const char extra_headers[] =
2438 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
2440 ConditionalizedRequestUpdatesCacheHelper(
2441 kNetResponse1, kNetResponse2, kNetResponse2, extra_headers);
2444 // Check that when an "if-none-match" header is attached
2445 // to the request, the result updates the cached entry.
2446 TEST(HttpCache, ConditionalizedRequestUpdatesCache2) {
2447 // First network response for |kUrl|.
2448 static const Response kNetResponse1 = {
2449 "HTTP/1.1 200 OK",
2450 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2451 "Etag: \"ETAG1\"\n"
2452 "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire.
2453 "body1"
2456 // Second network response for |kUrl|.
2457 static const Response kNetResponse2 = {
2458 "HTTP/1.1 200 OK",
2459 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2460 "Etag: \"ETAG2\"\n"
2461 "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire.
2462 "body2"
2465 const char extra_headers[] = "If-None-Match: \"ETAG1\"\r\n";
2467 ConditionalizedRequestUpdatesCacheHelper(
2468 kNetResponse1, kNetResponse2, kNetResponse2, extra_headers);
2471 // Check that when an "if-modified-since" header is attached
2472 // to a request, the 304 (not modified result) result updates the cached
2473 // headers, and the 304 response is returned rather than the cached response.
2474 TEST(HttpCache, ConditionalizedRequestUpdatesCache3) {
2475 // First network response for |kUrl|.
2476 static const Response kNetResponse1 = {
2477 "HTTP/1.1 200 OK",
2478 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2479 "Server: server1\n"
2480 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2481 "body1"
2484 // Second network response for |kUrl|.
2485 static const Response kNetResponse2 = {
2486 "HTTP/1.1 304 Not Modified",
2487 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2488 "Server: server2\n"
2489 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2493 static const Response kCachedResponse2 = {
2494 "HTTP/1.1 200 OK",
2495 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2496 "Server: server2\n"
2497 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2498 "body1"
2501 const char extra_headers[] =
2502 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
2504 ConditionalizedRequestUpdatesCacheHelper(
2505 kNetResponse1, kNetResponse2, kCachedResponse2, extra_headers);
2508 // Test that when doing an externally conditionalized if-modified-since
2509 // and there is no corresponding cache entry, a new cache entry is NOT
2510 // created (304 response).
2511 TEST(HttpCache, ConditionalizedRequestUpdatesCache4) {
2512 MockHttpCache cache;
2514 const char kUrl[] = "http://foobar.com/main.css";
2516 static const Response kNetResponse = {
2517 "HTTP/1.1 304 Not Modified",
2518 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2519 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2523 const char kExtraRequestHeaders[] =
2524 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
2526 // We will control the network layer's responses for |kUrl| using
2527 // |mock_network_response|.
2528 MockTransaction mock_network_response = { 0 };
2529 mock_network_response.url = kUrl;
2530 AddMockTransaction(&mock_network_response);
2532 MockTransaction request = { 0 };
2533 request.url = kUrl;
2534 request.method = "GET";
2535 request.request_headers = kExtraRequestHeaders;
2537 kNetResponse.AssignTo(&mock_network_response); // Network mock.
2538 kNetResponse.AssignTo(&request); // Expected result.
2540 std::string response_headers;
2541 RunTransactionTestWithResponse(
2542 cache.http_cache(), request, &response_headers);
2544 EXPECT_EQ(kNetResponse.status_and_headers(), response_headers);
2545 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2546 EXPECT_EQ(0, cache.disk_cache()->open_count());
2547 EXPECT_EQ(0, cache.disk_cache()->create_count());
2549 RemoveMockTransaction(&mock_network_response);
2552 // Test that when doing an externally conditionalized if-modified-since
2553 // and there is no corresponding cache entry, a new cache entry is NOT
2554 // created (200 response).
2555 TEST(HttpCache, ConditionalizedRequestUpdatesCache5) {
2556 MockHttpCache cache;
2558 const char kUrl[] = "http://foobar.com/main.css";
2560 static const Response kNetResponse = {
2561 "HTTP/1.1 200 OK",
2562 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2563 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2564 "foobar!!!"
2567 const char kExtraRequestHeaders[] =
2568 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
2570 // We will control the network layer's responses for |kUrl| using
2571 // |mock_network_response|.
2572 MockTransaction mock_network_response = { 0 };
2573 mock_network_response.url = kUrl;
2574 AddMockTransaction(&mock_network_response);
2576 MockTransaction request = { 0 };
2577 request.url = kUrl;
2578 request.method = "GET";
2579 request.request_headers = kExtraRequestHeaders;
2581 kNetResponse.AssignTo(&mock_network_response); // Network mock.
2582 kNetResponse.AssignTo(&request); // Expected result.
2584 std::string response_headers;
2585 RunTransactionTestWithResponse(
2586 cache.http_cache(), request, &response_headers);
2588 EXPECT_EQ(kNetResponse.status_and_headers(), response_headers);
2589 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2590 EXPECT_EQ(0, cache.disk_cache()->open_count());
2591 EXPECT_EQ(0, cache.disk_cache()->create_count());
2593 RemoveMockTransaction(&mock_network_response);
2596 // Test that when doing an externally conditionalized if-modified-since
2597 // if the date does not match the cache entry's last-modified date,
2598 // then we do NOT use the response (304) to update the cache.
2599 // (the if-modified-since date is 2 days AFTER the cache's modification date).
2600 TEST(HttpCache, ConditionalizedRequestUpdatesCache6) {
2601 static const Response kNetResponse1 = {
2602 "HTTP/1.1 200 OK",
2603 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2604 "Server: server1\n"
2605 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2606 "body1"
2609 // Second network response for |kUrl|.
2610 static const Response kNetResponse2 = {
2611 "HTTP/1.1 304 Not Modified",
2612 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2613 "Server: server2\n"
2614 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2618 // This is two days in the future from the original response's last-modified
2619 // date!
2620 const char kExtraRequestHeaders[] =
2621 "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n";
2623 ConditionalizedRequestUpdatesCacheHelper(
2624 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
2627 // Test that when doing an externally conditionalized if-none-match
2628 // if the etag does not match the cache entry's etag, then we do not use the
2629 // response (304) to update the cache.
2630 TEST(HttpCache, ConditionalizedRequestUpdatesCache7) {
2631 static const Response kNetResponse1 = {
2632 "HTTP/1.1 200 OK",
2633 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2634 "Etag: \"Foo1\"\n"
2635 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2636 "body1"
2639 // Second network response for |kUrl|.
2640 static const Response kNetResponse2 = {
2641 "HTTP/1.1 304 Not Modified",
2642 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2643 "Etag: \"Foo2\"\n"
2644 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2648 // Different etag from original response.
2649 const char kExtraRequestHeaders[] = "If-None-Match: \"Foo2\"\r\n";
2651 ConditionalizedRequestUpdatesCacheHelper(
2652 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
2655 // Test that doing an externally conditionalized request with both if-none-match
2656 // and if-modified-since updates the cache.
2657 TEST(HttpCache, ConditionalizedRequestUpdatesCache8) {
2658 static const Response kNetResponse1 = {
2659 "HTTP/1.1 200 OK",
2660 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2661 "Etag: \"Foo1\"\n"
2662 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2663 "body1"
2666 // Second network response for |kUrl|.
2667 static const Response kNetResponse2 = {
2668 "HTTP/1.1 200 OK",
2669 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2670 "Etag: \"Foo2\"\n"
2671 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
2672 "body2"
2675 const char kExtraRequestHeaders[] =
2676 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"
2677 "If-None-Match: \"Foo1\"\r\n";
2679 ConditionalizedRequestUpdatesCacheHelper(
2680 kNetResponse1, kNetResponse2, kNetResponse2, kExtraRequestHeaders);
2683 // Test that doing an externally conditionalized request with both if-none-match
2684 // and if-modified-since does not update the cache with only one match.
2685 TEST(HttpCache, ConditionalizedRequestUpdatesCache9) {
2686 static const Response kNetResponse1 = {
2687 "HTTP/1.1 200 OK",
2688 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2689 "Etag: \"Foo1\"\n"
2690 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2691 "body1"
2694 // Second network response for |kUrl|.
2695 static const Response kNetResponse2 = {
2696 "HTTP/1.1 200 OK",
2697 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2698 "Etag: \"Foo2\"\n"
2699 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
2700 "body2"
2703 // The etag doesn't match what we have stored.
2704 const char kExtraRequestHeaders[] =
2705 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"
2706 "If-None-Match: \"Foo2\"\r\n";
2708 ConditionalizedRequestUpdatesCacheHelper(
2709 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
2712 // Test that doing an externally conditionalized request with both if-none-match
2713 // and if-modified-since does not update the cache with only one match.
2714 TEST(HttpCache, ConditionalizedRequestUpdatesCache10) {
2715 static const Response kNetResponse1 = {
2716 "HTTP/1.1 200 OK",
2717 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
2718 "Etag: \"Foo1\"\n"
2719 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2720 "body1"
2723 // Second network response for |kUrl|.
2724 static const Response kNetResponse2 = {
2725 "HTTP/1.1 200 OK",
2726 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2727 "Etag: \"Foo2\"\n"
2728 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
2729 "body2"
2732 // The modification date doesn't match what we have stored.
2733 const char kExtraRequestHeaders[] =
2734 "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n"
2735 "If-None-Match: \"Foo1\"\r\n";
2737 ConditionalizedRequestUpdatesCacheHelper(
2738 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
2741 TEST(HttpCache, UrlContainingHash) {
2742 MockHttpCache cache;
2744 // Do a typical GET request -- should write an entry into our cache.
2745 MockTransaction trans(kTypicalGET_Transaction);
2746 RunTransactionTest(cache.http_cache(), trans);
2748 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2749 EXPECT_EQ(0, cache.disk_cache()->open_count());
2750 EXPECT_EQ(1, cache.disk_cache()->create_count());
2752 // Request the same URL, but this time with a reference section (hash).
2753 // Since the cache key strips the hash sections, this should be a cache hit.
2754 std::string url_with_hash = std::string(trans.url) + "#multiple#hashes";
2755 trans.url = url_with_hash.c_str();
2756 trans.load_flags = net::LOAD_ONLY_FROM_CACHE;
2758 RunTransactionTest(cache.http_cache(), trans);
2760 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2761 EXPECT_EQ(1, cache.disk_cache()->open_count());
2762 EXPECT_EQ(1, cache.disk_cache()->create_count());
2765 // Tests that we skip the cache for POST requests that do not have an upload
2766 // identifier.
2767 TEST(HttpCache, SimplePOST_SkipsCache) {
2768 MockHttpCache cache;
2770 RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction);
2772 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2773 EXPECT_EQ(0, cache.disk_cache()->open_count());
2774 EXPECT_EQ(0, cache.disk_cache()->create_count());
2777 // Tests POST handling with a disabled cache (no DCHECK).
2778 TEST(HttpCache, SimplePOST_DisabledCache) {
2779 MockHttpCache cache;
2780 cache.http_cache()->set_mode(net::HttpCache::Mode::DISABLE);
2782 RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction);
2784 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2785 EXPECT_EQ(0, cache.disk_cache()->open_count());
2786 EXPECT_EQ(0, cache.disk_cache()->create_count());
2789 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) {
2790 MockHttpCache cache;
2792 MockTransaction transaction(kSimplePOST_Transaction);
2793 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
2795 MockHttpRequest request(transaction);
2796 net::TestCompletionCallback callback;
2798 scoped_ptr<net::HttpTransaction> trans;
2799 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
2800 ASSERT_TRUE(trans.get());
2802 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
2803 ASSERT_EQ(net::ERR_CACHE_MISS, callback.GetResult(rv));
2805 trans.reset();
2807 EXPECT_EQ(0, cache.network_layer()->transaction_count());
2808 EXPECT_EQ(0, cache.disk_cache()->open_count());
2809 EXPECT_EQ(0, cache.disk_cache()->create_count());
2812 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Hit) {
2813 MockHttpCache cache;
2815 // Test that we hit the cache for POST requests.
2817 MockTransaction transaction(kSimplePOST_Transaction);
2819 const int64 kUploadId = 1; // Just a dummy value.
2821 ScopedVector<net::UploadElementReader> element_readers;
2822 element_readers.push_back(new net::UploadBytesElementReader("hello", 5));
2823 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(),
2824 kUploadId);
2825 MockHttpRequest request(transaction);
2826 request.upload_data_stream = &upload_data_stream;
2828 // Populate the cache.
2829 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL);
2831 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2832 EXPECT_EQ(0, cache.disk_cache()->open_count());
2833 EXPECT_EQ(1, cache.disk_cache()->create_count());
2835 // Load from cache.
2836 request.load_flags |= net::LOAD_ONLY_FROM_CACHE;
2837 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL);
2839 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2840 EXPECT_EQ(1, cache.disk_cache()->open_count());
2841 EXPECT_EQ(1, cache.disk_cache()->create_count());
2844 // Test that we don't hit the cache for POST requests if there is a byte range.
2845 TEST(HttpCache, SimplePOST_WithRanges) {
2846 MockHttpCache cache;
2848 MockTransaction transaction(kSimplePOST_Transaction);
2849 transaction.request_headers = "Range: bytes = 0-4\r\n";
2851 const int64 kUploadId = 1; // Just a dummy value.
2853 ScopedVector<net::UploadElementReader> element_readers;
2854 element_readers.push_back(new net::UploadBytesElementReader("hello", 5));
2855 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(),
2856 kUploadId);
2858 MockHttpRequest request(transaction);
2859 request.upload_data_stream = &upload_data_stream;
2861 // Attempt to populate the cache.
2862 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL);
2864 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2865 EXPECT_EQ(0, cache.disk_cache()->open_count());
2866 EXPECT_EQ(0, cache.disk_cache()->create_count());
2869 // Tests that a POST is cached separately from a previously cached GET.
2870 TEST(HttpCache, SimplePOST_SeparateCache) {
2871 MockHttpCache cache;
2873 ScopedVector<net::UploadElementReader> element_readers;
2874 element_readers.push_back(new net::UploadBytesElementReader("hello", 5));
2875 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 1);
2877 MockTransaction transaction(kSimplePOST_Transaction);
2878 MockHttpRequest req1(transaction);
2879 req1.upload_data_stream = &upload_data_stream;
2881 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
2883 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2884 EXPECT_EQ(0, cache.disk_cache()->open_count());
2885 EXPECT_EQ(1, cache.disk_cache()->create_count());
2887 transaction.method = "GET";
2888 MockHttpRequest req2(transaction);
2890 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL);
2892 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2893 EXPECT_EQ(0, cache.disk_cache()->open_count());
2894 EXPECT_EQ(2, cache.disk_cache()->create_count());
2897 // Tests that a successful POST invalidates a previously cached GET.
2898 TEST(HttpCache, SimplePOST_Invalidate_205) {
2899 MockHttpCache cache;
2901 MockTransaction transaction(kSimpleGET_Transaction);
2902 AddMockTransaction(&transaction);
2903 MockHttpRequest req1(transaction);
2905 // Attempt to populate the cache.
2906 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
2908 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2909 EXPECT_EQ(0, cache.disk_cache()->open_count());
2910 EXPECT_EQ(1, cache.disk_cache()->create_count());
2912 ScopedVector<net::UploadElementReader> element_readers;
2913 element_readers.push_back(new net::UploadBytesElementReader("hello", 5));
2914 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 1);
2916 transaction.method = "POST";
2917 transaction.status = "HTTP/1.1 205 No Content";
2918 MockHttpRequest req2(transaction);
2919 req2.upload_data_stream = &upload_data_stream;
2921 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL);
2923 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2924 EXPECT_EQ(0, cache.disk_cache()->open_count());
2925 EXPECT_EQ(2, cache.disk_cache()->create_count());
2927 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
2929 EXPECT_EQ(3, cache.network_layer()->transaction_count());
2930 EXPECT_EQ(0, cache.disk_cache()->open_count());
2931 EXPECT_EQ(3, cache.disk_cache()->create_count());
2932 RemoveMockTransaction(&transaction);
2935 // Tests that a successful POST invalidates a previously cached GET, even when
2936 // there is no upload identifier.
2937 TEST(HttpCache, SimplePOST_NoUploadId_Invalidate_205) {
2938 MockHttpCache cache;
2940 MockTransaction transaction(kSimpleGET_Transaction);
2941 AddMockTransaction(&transaction);
2942 MockHttpRequest req1(transaction);
2944 // Attempt to populate the cache.
2945 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
2947 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2948 EXPECT_EQ(0, cache.disk_cache()->open_count());
2949 EXPECT_EQ(1, cache.disk_cache()->create_count());
2951 ScopedVector<net::UploadElementReader> element_readers;
2952 element_readers.push_back(new net::UploadBytesElementReader("hello", 5));
2953 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0);
2955 transaction.method = "POST";
2956 transaction.status = "HTTP/1.1 205 No Content";
2957 MockHttpRequest req2(transaction);
2958 req2.upload_data_stream = &upload_data_stream;
2960 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL);
2962 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2963 EXPECT_EQ(0, cache.disk_cache()->open_count());
2964 EXPECT_EQ(1, cache.disk_cache()->create_count());
2966 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
2968 EXPECT_EQ(3, cache.network_layer()->transaction_count());
2969 EXPECT_EQ(0, cache.disk_cache()->open_count());
2970 EXPECT_EQ(2, cache.disk_cache()->create_count());
2971 RemoveMockTransaction(&transaction);
2974 // Tests that processing a POST before creating the backend doesn't crash.
2975 TEST(HttpCache, SimplePOST_NoUploadId_NoBackend) {
2976 // This will initialize a cache object with NULL backend.
2977 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
2978 factory->set_fail(true);
2979 factory->FinishCreation();
2980 MockHttpCache cache(factory);
2982 ScopedVector<net::UploadElementReader> element_readers;
2983 element_readers.push_back(new net::UploadBytesElementReader("hello", 5));
2984 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0);
2986 MockTransaction transaction(kSimplePOST_Transaction);
2987 AddMockTransaction(&transaction);
2988 MockHttpRequest req(transaction);
2989 req.upload_data_stream = &upload_data_stream;
2991 RunTransactionTestWithRequest(cache.http_cache(), transaction, req, NULL);
2993 RemoveMockTransaction(&transaction);
2996 // Tests that we don't invalidate entries as a result of a failed POST.
2997 TEST(HttpCache, SimplePOST_DontInvalidate_100) {
2998 MockHttpCache cache;
3000 MockTransaction transaction(kSimpleGET_Transaction);
3001 AddMockTransaction(&transaction);
3002 MockHttpRequest req1(transaction);
3004 // Attempt to populate the cache.
3005 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3007 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3008 EXPECT_EQ(0, cache.disk_cache()->open_count());
3009 EXPECT_EQ(1, cache.disk_cache()->create_count());
3011 ScopedVector<net::UploadElementReader> element_readers;
3012 element_readers.push_back(new net::UploadBytesElementReader("hello", 5));
3013 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 1);
3015 transaction.method = "POST";
3016 transaction.status = "HTTP/1.1 100 Continue";
3017 MockHttpRequest req2(transaction);
3018 req2.upload_data_stream = &upload_data_stream;
3020 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL);
3022 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3023 EXPECT_EQ(0, cache.disk_cache()->open_count());
3024 EXPECT_EQ(2, cache.disk_cache()->create_count());
3026 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3028 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3029 EXPECT_EQ(1, cache.disk_cache()->open_count());
3030 EXPECT_EQ(2, cache.disk_cache()->create_count());
3031 RemoveMockTransaction(&transaction);
3034 // Tests that a HEAD request is not cached by itself.
3035 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Miss) {
3036 MockHttpCache cache;
3037 MockTransaction transaction(kSimplePOST_Transaction);
3038 AddMockTransaction(&transaction);
3039 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
3040 transaction.method = "HEAD";
3042 MockHttpRequest request(transaction);
3043 net::TestCompletionCallback callback;
3045 scoped_ptr<net::HttpTransaction> trans;
3046 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
3047 ASSERT_TRUE(trans.get());
3049 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
3050 ASSERT_EQ(net::ERR_CACHE_MISS, callback.GetResult(rv));
3052 trans.reset();
3054 EXPECT_EQ(0, cache.network_layer()->transaction_count());
3055 EXPECT_EQ(0, cache.disk_cache()->open_count());
3056 EXPECT_EQ(0, cache.disk_cache()->create_count());
3057 RemoveMockTransaction(&transaction);
3060 // Tests that a HEAD request is served from a cached GET.
3061 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Hit) {
3062 MockHttpCache cache;
3063 MockTransaction transaction(kSimpleGET_Transaction);
3064 AddMockTransaction(&transaction);
3066 // Populate the cache.
3067 RunTransactionTest(cache.http_cache(), transaction);
3069 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3070 EXPECT_EQ(0, cache.disk_cache()->open_count());
3071 EXPECT_EQ(1, cache.disk_cache()->create_count());
3073 // Load from cache.
3074 transaction.method = "HEAD";
3075 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
3076 transaction.data = "";
3077 RunTransactionTest(cache.http_cache(), transaction);
3079 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3080 EXPECT_EQ(1, cache.disk_cache()->open_count());
3081 EXPECT_EQ(1, cache.disk_cache()->create_count());
3082 RemoveMockTransaction(&transaction);
3085 // Tests that a read-only request served from the cache preserves CL.
3086 TEST(HttpCache, SimpleHEAD_ContentLengthOnHit_Read) {
3087 MockHttpCache cache;
3088 MockTransaction transaction(kSimpleGET_Transaction);
3089 AddMockTransaction(&transaction);
3090 transaction.response_headers = "Content-Length: 42\n";
3092 // Populate the cache.
3093 RunTransactionTest(cache.http_cache(), transaction);
3095 // Load from cache.
3096 transaction.method = "HEAD";
3097 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
3098 transaction.data = "";
3099 std::string headers;
3101 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3103 EXPECT_EQ("HTTP/1.1 200 OK\nContent-Length: 42\n", headers);
3104 RemoveMockTransaction(&transaction);
3107 // Tests that a read-write request served from the cache preserves CL.
3108 TEST(HttpCache, ETagHEAD_ContentLengthOnHit_ReadWrite) {
3109 MockHttpCache cache;
3110 MockTransaction transaction(kETagGET_Transaction);
3111 AddMockTransaction(&transaction);
3112 std::string server_headers(kETagGET_Transaction.response_headers);
3113 server_headers.append("Content-Length: 42\n");
3114 transaction.response_headers = server_headers.data();
3116 // Populate the cache.
3117 RunTransactionTest(cache.http_cache(), transaction);
3119 // Load from cache.
3120 transaction.method = "HEAD";
3121 transaction.data = "";
3122 std::string headers;
3124 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3126 EXPECT_NE(std::string::npos, headers.find("Content-Length: 42\n"));
3127 RemoveMockTransaction(&transaction);
3130 // Tests that a HEAD request that includes byte ranges bypasses the cache.
3131 TEST(HttpCache, SimpleHEAD_WithRanges) {
3132 MockHttpCache cache;
3133 MockTransaction transaction(kSimpleGET_Transaction);
3134 AddMockTransaction(&transaction);
3136 // Populate the cache.
3137 RunTransactionTest(cache.http_cache(), transaction);
3139 // Load from cache.
3140 transaction.method = "HEAD";
3141 transaction.request_headers = "Range: bytes = 0-4\r\n";
3142 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
3143 transaction.return_code = net::ERR_CACHE_MISS;
3144 RunTransactionTest(cache.http_cache(), transaction);
3146 EXPECT_EQ(0, cache.disk_cache()->open_count());
3147 EXPECT_EQ(1, cache.disk_cache()->create_count());
3148 RemoveMockTransaction(&transaction);
3151 // Tests that a HEAD request can be served from a partialy cached resource.
3152 TEST(HttpCache, SimpleHEAD_WithCachedRanges) {
3153 MockHttpCache cache;
3154 AddMockTransaction(&kRangeGET_TransactionOK);
3156 // Write to the cache (40-49).
3157 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
3158 RemoveMockTransaction(&kRangeGET_TransactionOK);
3160 MockTransaction transaction(kSimpleGET_Transaction);
3162 transaction.url = kRangeGET_TransactionOK.url;
3163 transaction.method = "HEAD";
3164 transaction.data = "";
3165 AddMockTransaction(&transaction);
3166 std::string headers;
3168 // Load from cache.
3169 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3171 EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n"));
3172 EXPECT_EQ(std::string::npos, headers.find("Content-Length"));
3173 EXPECT_EQ(std::string::npos, headers.find("Content-Range"));
3174 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3175 EXPECT_EQ(1, cache.disk_cache()->open_count());
3176 EXPECT_EQ(1, cache.disk_cache()->create_count());
3177 RemoveMockTransaction(&transaction);
3180 // Tests that a HEAD request can be served from a truncated resource.
3181 TEST(HttpCache, SimpleHEAD_WithTruncatedEntry) {
3182 MockHttpCache cache;
3183 AddMockTransaction(&kRangeGET_TransactionOK);
3185 std::string raw_headers("HTTP/1.1 200 OK\n"
3186 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
3187 "ETag: \"foo\"\n"
3188 "Accept-Ranges: bytes\n"
3189 "Content-Length: 80\n");
3190 CreateTruncatedEntry(raw_headers, &cache);
3191 RemoveMockTransaction(&kRangeGET_TransactionOK);
3193 MockTransaction transaction(kSimpleGET_Transaction);
3195 transaction.url = kRangeGET_TransactionOK.url;
3196 transaction.method = "HEAD";
3197 transaction.data = "";
3198 AddMockTransaction(&transaction);
3199 std::string headers;
3201 // Load from cache.
3202 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3204 EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n"));
3205 EXPECT_NE(std::string::npos, headers.find("Content-Length: 80\n"));
3206 EXPECT_EQ(std::string::npos, headers.find("Content-Range"));
3207 EXPECT_EQ(0, cache.network_layer()->transaction_count());
3208 EXPECT_EQ(1, cache.disk_cache()->open_count());
3209 EXPECT_EQ(1, cache.disk_cache()->create_count());
3210 RemoveMockTransaction(&transaction);
3213 // Tests that a HEAD request updates the cached response.
3214 TEST(HttpCache, TypicalHEAD_UpdatesResponse) {
3215 MockHttpCache cache;
3216 MockTransaction transaction(kTypicalGET_Transaction);
3217 AddMockTransaction(&transaction);
3219 // Populate the cache.
3220 RunTransactionTest(cache.http_cache(), transaction);
3222 // Update the cache.
3223 transaction.method = "HEAD";
3224 transaction.response_headers = "Foo: bar\n";
3225 transaction.data = "";
3226 transaction.status = "HTTP/1.1 304 Not Modified\n";
3227 std::string headers;
3228 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3229 RemoveMockTransaction(&transaction);
3231 EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n"));
3232 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3234 MockTransaction transaction2(kTypicalGET_Transaction);
3235 AddMockTransaction(&transaction2);
3237 // Make sure we are done with the previous transaction.
3238 base::MessageLoop::current()->RunUntilIdle();
3240 // Load from the cache.
3241 transaction2.load_flags |= net::LOAD_ONLY_FROM_CACHE;
3242 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
3244 EXPECT_NE(std::string::npos, headers.find("Foo: bar\n"));
3245 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3246 EXPECT_EQ(2, cache.disk_cache()->open_count());
3247 EXPECT_EQ(1, cache.disk_cache()->create_count());
3248 RemoveMockTransaction(&transaction2);
3251 // Tests that an externally conditionalized HEAD request updates the cache.
3252 TEST(HttpCache, TypicalHEAD_ConditionalizedRequestUpdatesResponse) {
3253 MockHttpCache cache;
3254 MockTransaction transaction(kTypicalGET_Transaction);
3255 AddMockTransaction(&transaction);
3257 // Populate the cache.
3258 RunTransactionTest(cache.http_cache(), transaction);
3260 // Update the cache.
3261 transaction.method = "HEAD";
3262 transaction.request_headers =
3263 "If-Modified-Since: Wed, 28 Nov 2007 00:40:09 GMT\r\n";
3264 transaction.response_headers = "Foo: bar\n";
3265 transaction.data = "";
3266 transaction.status = "HTTP/1.1 304 Not Modified\n";
3267 std::string headers;
3268 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3269 RemoveMockTransaction(&transaction);
3271 EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 304 Not Modified\n"));
3272 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3274 MockTransaction transaction2(kTypicalGET_Transaction);
3275 AddMockTransaction(&transaction2);
3277 // Make sure we are done with the previous transaction.
3278 base::MessageLoop::current()->RunUntilIdle();
3280 // Load from the cache.
3281 transaction2.load_flags |= net::LOAD_ONLY_FROM_CACHE;
3282 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
3284 EXPECT_NE(std::string::npos, headers.find("Foo: bar\n"));
3285 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3286 EXPECT_EQ(2, cache.disk_cache()->open_count());
3287 EXPECT_EQ(1, cache.disk_cache()->create_count());
3288 RemoveMockTransaction(&transaction2);
3291 // Tests that a HEAD request invalidates an old cached entry.
3292 TEST(HttpCache, SimpleHEAD_InvalidatesEntry) {
3293 MockHttpCache cache;
3294 MockTransaction transaction(kTypicalGET_Transaction);
3295 AddMockTransaction(&transaction);
3297 // Populate the cache.
3298 RunTransactionTest(cache.http_cache(), transaction);
3300 // Update the cache.
3301 transaction.method = "HEAD";
3302 transaction.data = "";
3303 RunTransactionTest(cache.http_cache(), transaction);
3304 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3306 // Load from the cache.
3307 transaction.method = "GET";
3308 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
3309 transaction.return_code = net::ERR_CACHE_MISS;
3310 RunTransactionTest(cache.http_cache(), transaction);
3312 RemoveMockTransaction(&transaction);
3315 // Tests that we do not cache the response of a PUT.
3316 TEST(HttpCache, SimplePUT_Miss) {
3317 MockHttpCache cache;
3319 MockTransaction transaction(kSimplePOST_Transaction);
3320 transaction.method = "PUT";
3322 ScopedVector<net::UploadElementReader> element_readers;
3323 element_readers.push_back(new net::UploadBytesElementReader("hello", 5));
3324 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0);
3326 MockHttpRequest request(transaction);
3327 request.upload_data_stream = &upload_data_stream;
3329 // Attempt to populate the cache.
3330 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL);
3332 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3333 EXPECT_EQ(0, cache.disk_cache()->open_count());
3334 EXPECT_EQ(0, cache.disk_cache()->create_count());
3337 // Tests that we invalidate entries as a result of a PUT.
3338 TEST(HttpCache, SimplePUT_Invalidate) {
3339 MockHttpCache cache;
3341 MockTransaction transaction(kSimpleGET_Transaction);
3342 MockHttpRequest req1(transaction);
3344 // Attempt to populate the cache.
3345 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3347 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3348 EXPECT_EQ(0, cache.disk_cache()->open_count());
3349 EXPECT_EQ(1, cache.disk_cache()->create_count());
3351 ScopedVector<net::UploadElementReader> element_readers;
3352 element_readers.push_back(new net::UploadBytesElementReader("hello", 5));
3353 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0);
3355 transaction.method = "PUT";
3356 MockHttpRequest req2(transaction);
3357 req2.upload_data_stream = &upload_data_stream;
3359 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL);
3361 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3362 EXPECT_EQ(1, cache.disk_cache()->open_count());
3363 EXPECT_EQ(1, cache.disk_cache()->create_count());
3365 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3367 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3368 EXPECT_EQ(1, cache.disk_cache()->open_count());
3369 EXPECT_EQ(2, cache.disk_cache()->create_count());
3372 // Tests that we invalidate entries as a result of a PUT.
3373 TEST(HttpCache, SimplePUT_Invalidate_305) {
3374 MockHttpCache cache;
3376 MockTransaction transaction(kSimpleGET_Transaction);
3377 AddMockTransaction(&transaction);
3378 MockHttpRequest req1(transaction);
3380 // Attempt to populate the cache.
3381 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3383 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3384 EXPECT_EQ(0, cache.disk_cache()->open_count());
3385 EXPECT_EQ(1, cache.disk_cache()->create_count());
3387 ScopedVector<net::UploadElementReader> element_readers;
3388 element_readers.push_back(new net::UploadBytesElementReader("hello", 5));
3389 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0);
3391 transaction.method = "PUT";
3392 transaction.status = "HTTP/1.1 305 Use Proxy";
3393 MockHttpRequest req2(transaction);
3394 req2.upload_data_stream = &upload_data_stream;
3396 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL);
3398 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3399 EXPECT_EQ(1, cache.disk_cache()->open_count());
3400 EXPECT_EQ(1, cache.disk_cache()->create_count());
3402 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3404 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3405 EXPECT_EQ(1, cache.disk_cache()->open_count());
3406 EXPECT_EQ(2, cache.disk_cache()->create_count());
3407 RemoveMockTransaction(&transaction);
3410 // Tests that we don't invalidate entries as a result of a failed PUT.
3411 TEST(HttpCache, SimplePUT_DontInvalidate_404) {
3412 MockHttpCache cache;
3414 MockTransaction transaction(kSimpleGET_Transaction);
3415 AddMockTransaction(&transaction);
3416 MockHttpRequest req1(transaction);
3418 // Attempt to populate the cache.
3419 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3421 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3422 EXPECT_EQ(0, cache.disk_cache()->open_count());
3423 EXPECT_EQ(1, cache.disk_cache()->create_count());
3425 ScopedVector<net::UploadElementReader> element_readers;
3426 element_readers.push_back(new net::UploadBytesElementReader("hello", 5));
3427 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0);
3429 transaction.method = "PUT";
3430 transaction.status = "HTTP/1.1 404 Not Found";
3431 MockHttpRequest req2(transaction);
3432 req2.upload_data_stream = &upload_data_stream;
3434 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL);
3436 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3437 EXPECT_EQ(1, cache.disk_cache()->open_count());
3438 EXPECT_EQ(1, cache.disk_cache()->create_count());
3440 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3442 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3443 EXPECT_EQ(2, cache.disk_cache()->open_count());
3444 EXPECT_EQ(1, cache.disk_cache()->create_count());
3445 RemoveMockTransaction(&transaction);
3448 // Tests that we do not cache the response of a DELETE.
3449 TEST(HttpCache, SimpleDELETE_Miss) {
3450 MockHttpCache cache;
3452 MockTransaction transaction(kSimplePOST_Transaction);
3453 transaction.method = "DELETE";
3455 ScopedVector<net::UploadElementReader> element_readers;
3456 element_readers.push_back(new net::UploadBytesElementReader("hello", 5));
3457 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0);
3459 MockHttpRequest request(transaction);
3460 request.upload_data_stream = &upload_data_stream;
3462 // Attempt to populate the cache.
3463 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL);
3465 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3466 EXPECT_EQ(0, cache.disk_cache()->open_count());
3467 EXPECT_EQ(0, cache.disk_cache()->create_count());
3470 // Tests that we invalidate entries as a result of a DELETE.
3471 TEST(HttpCache, SimpleDELETE_Invalidate) {
3472 MockHttpCache cache;
3474 MockTransaction transaction(kSimpleGET_Transaction);
3475 MockHttpRequest req1(transaction);
3477 // Attempt to populate the cache.
3478 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3480 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3481 EXPECT_EQ(0, cache.disk_cache()->open_count());
3482 EXPECT_EQ(1, cache.disk_cache()->create_count());
3484 ScopedVector<net::UploadElementReader> element_readers;
3485 element_readers.push_back(new net::UploadBytesElementReader("hello", 5));
3486 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0);
3488 transaction.method = "DELETE";
3489 MockHttpRequest req2(transaction);
3490 req2.upload_data_stream = &upload_data_stream;
3492 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL);
3494 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3495 EXPECT_EQ(1, cache.disk_cache()->open_count());
3496 EXPECT_EQ(1, cache.disk_cache()->create_count());
3498 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3500 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3501 EXPECT_EQ(1, cache.disk_cache()->open_count());
3502 EXPECT_EQ(2, cache.disk_cache()->create_count());
3505 // Tests that we invalidate entries as a result of a DELETE.
3506 TEST(HttpCache, SimpleDELETE_Invalidate_301) {
3507 MockHttpCache cache;
3509 MockTransaction transaction(kSimpleGET_Transaction);
3510 AddMockTransaction(&transaction);
3512 // Attempt to populate the cache.
3513 RunTransactionTest(cache.http_cache(), transaction);
3515 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3516 EXPECT_EQ(0, cache.disk_cache()->open_count());
3517 EXPECT_EQ(1, cache.disk_cache()->create_count());
3519 transaction.method = "DELETE";
3520 transaction.status = "HTTP/1.1 301 Moved Permanently ";
3522 RunTransactionTest(cache.http_cache(), transaction);
3524 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3525 EXPECT_EQ(1, cache.disk_cache()->open_count());
3526 EXPECT_EQ(1, cache.disk_cache()->create_count());
3528 transaction.method = "GET";
3529 RunTransactionTest(cache.http_cache(), transaction);
3531 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3532 EXPECT_EQ(1, cache.disk_cache()->open_count());
3533 EXPECT_EQ(2, cache.disk_cache()->create_count());
3534 RemoveMockTransaction(&transaction);
3537 // Tests that we don't invalidate entries as a result of a failed DELETE.
3538 TEST(HttpCache, SimpleDELETE_DontInvalidate_416) {
3539 MockHttpCache cache;
3541 MockTransaction transaction(kSimpleGET_Transaction);
3542 AddMockTransaction(&transaction);
3544 // Attempt to populate the cache.
3545 RunTransactionTest(cache.http_cache(), transaction);
3547 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3548 EXPECT_EQ(0, cache.disk_cache()->open_count());
3549 EXPECT_EQ(1, cache.disk_cache()->create_count());
3551 transaction.method = "DELETE";
3552 transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable";
3554 RunTransactionTest(cache.http_cache(), transaction);
3556 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3557 EXPECT_EQ(1, cache.disk_cache()->open_count());
3558 EXPECT_EQ(1, cache.disk_cache()->create_count());
3560 transaction.method = "GET";
3561 transaction.status = "HTTP/1.1 200 OK";
3562 RunTransactionTest(cache.http_cache(), transaction);
3564 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3565 EXPECT_EQ(2, cache.disk_cache()->open_count());
3566 EXPECT_EQ(1, cache.disk_cache()->create_count());
3567 RemoveMockTransaction(&transaction);
3570 // Tests that we don't invalidate entries after a failed network transaction.
3571 TEST(HttpCache, SimpleGET_DontInvalidateOnFailure) {
3572 MockHttpCache cache;
3574 // Populate the cache.
3575 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
3576 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3578 // Fail the network request.
3579 MockTransaction transaction(kSimpleGET_Transaction);
3580 transaction.return_code = net::ERR_FAILED;
3581 transaction.load_flags |= net::LOAD_VALIDATE_CACHE;
3583 AddMockTransaction(&transaction);
3584 RunTransactionTest(cache.http_cache(), transaction);
3585 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3586 RemoveMockTransaction(&transaction);
3588 transaction.load_flags = net::LOAD_ONLY_FROM_CACHE;
3589 transaction.return_code = net::OK;
3590 AddMockTransaction(&transaction);
3591 RunTransactionTest(cache.http_cache(), transaction);
3593 // Make sure the transaction didn't reach the network.
3594 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3595 RemoveMockTransaction(&transaction);
3598 TEST(HttpCache, RangeGET_SkipsCache) {
3599 MockHttpCache cache;
3601 // Test that we skip the cache for range GET requests. Eventually, we will
3602 // want to cache these, but we'll still have cases where skipping the cache
3603 // makes sense, so we want to make sure that it works properly.
3605 RunTransactionTest(cache.http_cache(), kRangeGET_Transaction);
3607 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3608 EXPECT_EQ(0, cache.disk_cache()->open_count());
3609 EXPECT_EQ(0, cache.disk_cache()->create_count());
3611 MockTransaction transaction(kSimpleGET_Transaction);
3612 transaction.request_headers = "If-None-Match: foo\r\n";
3613 RunTransactionTest(cache.http_cache(), transaction);
3615 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3616 EXPECT_EQ(0, cache.disk_cache()->open_count());
3617 EXPECT_EQ(0, cache.disk_cache()->create_count());
3619 transaction.request_headers =
3620 "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n";
3621 RunTransactionTest(cache.http_cache(), transaction);
3623 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3624 EXPECT_EQ(0, cache.disk_cache()->open_count());
3625 EXPECT_EQ(0, cache.disk_cache()->create_count());
3628 // Test that we skip the cache for range requests that include a validation
3629 // header.
3630 TEST(HttpCache, RangeGET_SkipsCache2) {
3631 MockHttpCache cache;
3633 MockTransaction transaction(kRangeGET_Transaction);
3634 transaction.request_headers = "If-None-Match: foo\r\n"
3635 EXTRA_HEADER
3636 "Range: bytes = 40-49\r\n";
3637 RunTransactionTest(cache.http_cache(), transaction);
3639 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3640 EXPECT_EQ(0, cache.disk_cache()->open_count());
3641 EXPECT_EQ(0, cache.disk_cache()->create_count());
3643 transaction.request_headers =
3644 "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n"
3645 EXTRA_HEADER
3646 "Range: bytes = 40-49\r\n";
3647 RunTransactionTest(cache.http_cache(), transaction);
3649 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3650 EXPECT_EQ(0, cache.disk_cache()->open_count());
3651 EXPECT_EQ(0, cache.disk_cache()->create_count());
3653 transaction.request_headers = "If-Range: bla\r\n"
3654 EXTRA_HEADER
3655 "Range: bytes = 40-49\r\n";
3656 RunTransactionTest(cache.http_cache(), transaction);
3658 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3659 EXPECT_EQ(0, cache.disk_cache()->open_count());
3660 EXPECT_EQ(0, cache.disk_cache()->create_count());
3663 TEST(HttpCache, SimpleGET_DoesntLogHeaders) {
3664 MockHttpCache cache;
3666 net::CapturingBoundNetLog log;
3667 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction,
3668 log.bound());
3670 EXPECT_FALSE(LogContainsEventType(
3671 log, net::NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS));
3674 TEST(HttpCache, RangeGET_LogsHeaders) {
3675 MockHttpCache cache;
3677 net::CapturingBoundNetLog log;
3678 RunTransactionTestWithLog(cache.http_cache(), kRangeGET_Transaction,
3679 log.bound());
3681 EXPECT_TRUE(LogContainsEventType(
3682 log, net::NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS));
3685 TEST(HttpCache, ExternalValidation_LogsHeaders) {
3686 MockHttpCache cache;
3688 net::CapturingBoundNetLog log;
3689 MockTransaction transaction(kSimpleGET_Transaction);
3690 transaction.request_headers = "If-None-Match: foo\r\n" EXTRA_HEADER;
3691 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound());
3693 EXPECT_TRUE(LogContainsEventType(
3694 log, net::NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS));
3697 TEST(HttpCache, SpecialHeaders_LogsHeaders) {
3698 MockHttpCache cache;
3700 net::CapturingBoundNetLog log;
3701 MockTransaction transaction(kSimpleGET_Transaction);
3702 transaction.request_headers = "cache-control: no-cache\r\n" EXTRA_HEADER;
3703 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound());
3705 EXPECT_TRUE(LogContainsEventType(
3706 log, net::NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS));
3709 // Tests that receiving 206 for a regular request is handled correctly.
3710 TEST(HttpCache, GET_Crazy206) {
3711 MockHttpCache cache;
3713 // Write to the cache.
3714 MockTransaction transaction(kRangeGET_TransactionOK);
3715 AddMockTransaction(&transaction);
3716 transaction.request_headers = EXTRA_HEADER;
3717 transaction.handler = NULL;
3718 RunTransactionTest(cache.http_cache(), transaction);
3720 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3721 EXPECT_EQ(0, cache.disk_cache()->open_count());
3722 EXPECT_EQ(1, cache.disk_cache()->create_count());
3724 // This should read again from the net.
3725 RunTransactionTest(cache.http_cache(), transaction);
3727 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3728 EXPECT_EQ(0, cache.disk_cache()->open_count());
3729 EXPECT_EQ(2, cache.disk_cache()->create_count());
3730 RemoveMockTransaction(&transaction);
3733 // Tests that receiving 416 for a regular request is handled correctly.
3734 TEST(HttpCache, GET_Crazy416) {
3735 MockHttpCache cache;
3737 // Write to the cache.
3738 MockTransaction transaction(kSimpleGET_Transaction);
3739 AddMockTransaction(&transaction);
3740 transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable";
3741 RunTransactionTest(cache.http_cache(), transaction);
3743 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3744 EXPECT_EQ(0, cache.disk_cache()->open_count());
3745 EXPECT_EQ(1, cache.disk_cache()->create_count());
3747 RemoveMockTransaction(&transaction);
3750 // Tests that we don't store partial responses that can't be validated.
3751 TEST(HttpCache, RangeGET_NoStrongValidators) {
3752 MockHttpCache cache;
3753 std::string headers;
3755 // Attempt to write to the cache (40-49).
3756 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
3757 transaction.response_headers = "Content-Length: 10\n"
3758 "Cache-Control: max-age=3600\n"
3759 "ETag: w/\"foo\"\n";
3760 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3762 Verify206Response(headers, 40, 49);
3763 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3764 EXPECT_EQ(0, cache.disk_cache()->open_count());
3765 EXPECT_EQ(1, cache.disk_cache()->create_count());
3767 // Now verify that there's no cached data.
3768 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
3769 &headers);
3771 Verify206Response(headers, 40, 49);
3772 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3773 EXPECT_EQ(0, cache.disk_cache()->open_count());
3774 EXPECT_EQ(2, cache.disk_cache()->create_count());
3777 // Tests failures to conditionalize byte range requests.
3778 TEST(HttpCache, RangeGET_NoConditionalization) {
3779 MockHttpCache cache;
3780 cache.FailConditionalizations();
3781 std::string headers;
3783 // Write to the cache (40-49).
3784 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
3785 transaction.response_headers = "Content-Length: 10\n"
3786 "ETag: \"foo\"\n";
3787 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3789 Verify206Response(headers, 40, 49);
3790 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3791 EXPECT_EQ(0, cache.disk_cache()->open_count());
3792 EXPECT_EQ(1, cache.disk_cache()->create_count());
3794 // Now verify that the cached data is not used.
3795 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
3796 &headers);
3798 Verify206Response(headers, 40, 49);
3799 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3800 EXPECT_EQ(1, cache.disk_cache()->open_count());
3801 EXPECT_EQ(2, cache.disk_cache()->create_count());
3804 // Tests that restarting a partial request when the cached data cannot be
3805 // revalidated logs an event.
3806 TEST(HttpCache, RangeGET_NoValidation_LogsRestart) {
3807 MockHttpCache cache;
3808 cache.FailConditionalizations();
3810 // Write to the cache (40-49).
3811 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
3812 transaction.response_headers = "Content-Length: 10\n"
3813 "ETag: \"foo\"\n";
3814 RunTransactionTest(cache.http_cache(), transaction);
3816 // Now verify that the cached data is not used.
3817 net::CapturingBoundNetLog log;
3818 RunTransactionTestWithLog(cache.http_cache(), kRangeGET_TransactionOK,
3819 log.bound());
3821 EXPECT_TRUE(LogContainsEventType(
3822 log, net::NetLog::TYPE_HTTP_CACHE_RESTART_PARTIAL_REQUEST));
3825 // Tests that a failure to conditionalize a regular request (no range) with a
3826 // sparse entry results in a full response.
3827 TEST(HttpCache, GET_NoConditionalization) {
3828 MockHttpCache cache;
3829 cache.FailConditionalizations();
3830 std::string headers;
3832 // Write to the cache (40-49).
3833 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
3834 transaction.response_headers = "Content-Length: 10\n"
3835 "ETag: \"foo\"\n";
3836 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3838 Verify206Response(headers, 40, 49);
3839 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3840 EXPECT_EQ(0, cache.disk_cache()->open_count());
3841 EXPECT_EQ(1, cache.disk_cache()->create_count());
3843 // Now verify that the cached data is not used.
3844 // Don't ask for a range. The cache will attempt to use the cached data but
3845 // should discard it as it cannot be validated. A regular request should go
3846 // to the server and a new entry should be created.
3847 transaction.request_headers = EXTRA_HEADER;
3848 transaction.data = "Not a range";
3849 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3851 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
3852 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3853 EXPECT_EQ(1, cache.disk_cache()->open_count());
3854 EXPECT_EQ(2, cache.disk_cache()->create_count());
3856 // The last response was saved.
3857 RunTransactionTest(cache.http_cache(), transaction);
3858 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3859 EXPECT_EQ(2, cache.disk_cache()->open_count());
3860 EXPECT_EQ(2, cache.disk_cache()->create_count());
3863 // Verifies that conditionalization failures when asking for a range that would
3864 // require the cache to modify the range to ask, result in a network request
3865 // that matches the user's one.
3866 TEST(HttpCache, RangeGET_NoConditionalization2) {
3867 MockHttpCache cache;
3868 cache.FailConditionalizations();
3869 std::string headers;
3871 // Write to the cache (40-49).
3872 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
3873 transaction.response_headers = "Content-Length: 10\n"
3874 "ETag: \"foo\"\n";
3875 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3877 Verify206Response(headers, 40, 49);
3878 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3879 EXPECT_EQ(0, cache.disk_cache()->open_count());
3880 EXPECT_EQ(1, cache.disk_cache()->create_count());
3882 // Now verify that the cached data is not used.
3883 // Ask for a range that extends before and after the cached data so that the
3884 // cache would normally mix data from three sources. After deleting the entry,
3885 // the response will come from a single network request.
3886 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER;
3887 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 ";
3888 transaction.response_headers = kRangeGET_TransactionOK.response_headers;
3889 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3891 Verify206Response(headers, 20, 59);
3892 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3893 EXPECT_EQ(1, cache.disk_cache()->open_count());
3894 EXPECT_EQ(2, cache.disk_cache()->create_count());
3896 // The last response was saved.
3897 RunTransactionTest(cache.http_cache(), transaction);
3898 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3899 EXPECT_EQ(2, cache.disk_cache()->open_count());
3900 EXPECT_EQ(2, cache.disk_cache()->create_count());
3903 // Tests that we cache partial responses that lack content-length.
3904 TEST(HttpCache, RangeGET_NoContentLength) {
3905 MockHttpCache cache;
3906 std::string headers;
3908 // Attempt to write to the cache (40-49).
3909 MockTransaction transaction(kRangeGET_TransactionOK);
3910 AddMockTransaction(&transaction);
3911 transaction.response_headers = "ETag: \"foo\"\n"
3912 "Accept-Ranges: bytes\n"
3913 "Content-Range: bytes 40-49/80\n";
3914 transaction.handler = NULL;
3915 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3917 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3918 EXPECT_EQ(0, cache.disk_cache()->open_count());
3919 EXPECT_EQ(1, cache.disk_cache()->create_count());
3921 // Now verify that there's no cached data.
3922 transaction.handler = &RangeTransactionServer::RangeHandler;
3923 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
3924 &headers);
3926 Verify206Response(headers, 40, 49);
3927 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3928 EXPECT_EQ(1, cache.disk_cache()->open_count());
3929 EXPECT_EQ(1, cache.disk_cache()->create_count());
3931 RemoveMockTransaction(&transaction);
3934 // Tests that we can cache range requests and fetch random blocks from the
3935 // cache and the network.
3936 TEST(HttpCache, RangeGET_OK) {
3937 MockHttpCache cache;
3938 AddMockTransaction(&kRangeGET_TransactionOK);
3939 std::string headers;
3941 // Write to the cache (40-49).
3942 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
3943 &headers);
3945 Verify206Response(headers, 40, 49);
3946 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3947 EXPECT_EQ(0, cache.disk_cache()->open_count());
3948 EXPECT_EQ(1, cache.disk_cache()->create_count());
3950 // Read from the cache (40-49).
3951 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
3952 &headers);
3954 Verify206Response(headers, 40, 49);
3955 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3956 EXPECT_EQ(1, cache.disk_cache()->open_count());
3957 EXPECT_EQ(1, cache.disk_cache()->create_count());
3959 // Make sure we are done with the previous transaction.
3960 base::MessageLoop::current()->RunUntilIdle();
3962 // Write to the cache (30-39).
3963 MockTransaction transaction(kRangeGET_TransactionOK);
3964 transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER;
3965 transaction.data = "rg: 30-39 ";
3966 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3968 Verify206Response(headers, 30, 39);
3969 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3970 EXPECT_EQ(2, cache.disk_cache()->open_count());
3971 EXPECT_EQ(1, cache.disk_cache()->create_count());
3973 // Make sure we are done with the previous transaction.
3974 base::MessageLoop::current()->RunUntilIdle();
3976 // Write and read from the cache (20-59).
3977 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER;
3978 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 ";
3979 net::CapturingBoundNetLog log;
3980 net::LoadTimingInfo load_timing_info;
3981 RunTransactionTestWithResponseAndGetTiming(
3982 cache.http_cache(), transaction, &headers, log.bound(),
3983 &load_timing_info);
3985 Verify206Response(headers, 20, 59);
3986 EXPECT_EQ(4, cache.network_layer()->transaction_count());
3987 EXPECT_EQ(3, cache.disk_cache()->open_count());
3988 EXPECT_EQ(1, cache.disk_cache()->create_count());
3989 TestLoadTimingNetworkRequest(load_timing_info);
3991 RemoveMockTransaction(&kRangeGET_TransactionOK);
3994 // Tests that we can cache range requests and fetch random blocks from the
3995 // cache and the network, with synchronous responses.
3996 TEST(HttpCache, RangeGET_SyncOK) {
3997 MockHttpCache cache;
3999 MockTransaction transaction(kRangeGET_TransactionOK);
4000 transaction.test_mode = TEST_MODE_SYNC_ALL;
4001 AddMockTransaction(&transaction);
4003 // Write to the cache (40-49).
4004 std::string headers;
4005 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4007 Verify206Response(headers, 40, 49);
4008 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4009 EXPECT_EQ(0, cache.disk_cache()->open_count());
4010 EXPECT_EQ(1, cache.disk_cache()->create_count());
4012 // Read from the cache (40-49).
4013 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4015 Verify206Response(headers, 40, 49);
4016 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4017 EXPECT_EQ(0, cache.disk_cache()->open_count());
4018 EXPECT_EQ(1, cache.disk_cache()->create_count());
4020 // Make sure we are done with the previous transaction.
4021 base::MessageLoop::current()->RunUntilIdle();
4023 // Write to the cache (30-39).
4024 transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER;
4025 transaction.data = "rg: 30-39 ";
4026 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4028 Verify206Response(headers, 30, 39);
4029 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4030 EXPECT_EQ(1, cache.disk_cache()->open_count());
4031 EXPECT_EQ(1, cache.disk_cache()->create_count());
4033 // Make sure we are done with the previous transaction.
4034 base::MessageLoop::current()->RunUntilIdle();
4036 // Write and read from the cache (20-59).
4037 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER;
4038 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 ";
4039 net::CapturingBoundNetLog log;
4040 net::LoadTimingInfo load_timing_info;
4041 RunTransactionTestWithResponseAndGetTiming(
4042 cache.http_cache(), transaction, &headers, log.bound(),
4043 &load_timing_info);
4045 Verify206Response(headers, 20, 59);
4046 EXPECT_EQ(4, cache.network_layer()->transaction_count());
4047 EXPECT_EQ(2, cache.disk_cache()->open_count());
4048 EXPECT_EQ(1, cache.disk_cache()->create_count());
4049 TestLoadTimingNetworkRequest(load_timing_info);
4051 RemoveMockTransaction(&transaction);
4054 // Tests that we don't revalidate an entry unless we are required to do so.
4055 TEST(HttpCache, RangeGET_Revalidate1) {
4056 MockHttpCache cache;
4057 std::string headers;
4059 // Write to the cache (40-49).
4060 MockTransaction transaction(kRangeGET_TransactionOK);
4061 transaction.response_headers =
4062 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
4063 "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n" // Should never expire.
4064 "ETag: \"foo\"\n"
4065 "Accept-Ranges: bytes\n"
4066 "Content-Length: 10\n";
4067 AddMockTransaction(&transaction);
4068 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4070 Verify206Response(headers, 40, 49);
4071 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4072 EXPECT_EQ(0, cache.disk_cache()->open_count());
4073 EXPECT_EQ(1, cache.disk_cache()->create_count());
4075 // Read from the cache (40-49).
4076 net::CapturingBoundNetLog log;
4077 net::LoadTimingInfo load_timing_info;
4078 RunTransactionTestWithResponseAndGetTiming(
4079 cache.http_cache(), transaction, &headers, log.bound(),
4080 &load_timing_info);
4082 Verify206Response(headers, 40, 49);
4083 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4084 EXPECT_EQ(1, cache.disk_cache()->open_count());
4085 EXPECT_EQ(1, cache.disk_cache()->create_count());
4086 TestLoadTimingCachedResponse(load_timing_info);
4088 // Read again forcing the revalidation.
4089 transaction.load_flags |= net::LOAD_VALIDATE_CACHE;
4090 RunTransactionTestWithResponseAndGetTiming(
4091 cache.http_cache(), transaction, &headers, log.bound(),
4092 &load_timing_info);
4094 Verify206Response(headers, 40, 49);
4095 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4096 EXPECT_EQ(1, cache.disk_cache()->open_count());
4097 EXPECT_EQ(1, cache.disk_cache()->create_count());
4098 TestLoadTimingNetworkRequest(load_timing_info);
4100 RemoveMockTransaction(&transaction);
4103 // Checks that we revalidate an entry when the headers say so.
4104 TEST(HttpCache, RangeGET_Revalidate2) {
4105 MockHttpCache cache;
4106 std::string headers;
4108 // Write to the cache (40-49).
4109 MockTransaction transaction(kRangeGET_TransactionOK);
4110 transaction.response_headers =
4111 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
4112 "Expires: Sat, 18 Apr 2009 01:10:43 GMT\n" // Expired.
4113 "ETag: \"foo\"\n"
4114 "Accept-Ranges: bytes\n"
4115 "Content-Length: 10\n";
4116 AddMockTransaction(&transaction);
4117 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4119 Verify206Response(headers, 40, 49);
4120 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4121 EXPECT_EQ(0, cache.disk_cache()->open_count());
4122 EXPECT_EQ(1, cache.disk_cache()->create_count());
4124 // Read from the cache (40-49).
4125 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4126 Verify206Response(headers, 40, 49);
4128 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4129 EXPECT_EQ(1, cache.disk_cache()->open_count());
4130 EXPECT_EQ(1, cache.disk_cache()->create_count());
4132 RemoveMockTransaction(&transaction);
4135 // Tests that we deal with 304s for range requests.
4136 TEST(HttpCache, RangeGET_304) {
4137 MockHttpCache cache;
4138 AddMockTransaction(&kRangeGET_TransactionOK);
4139 std::string headers;
4141 // Write to the cache (40-49).
4142 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
4143 &headers);
4145 Verify206Response(headers, 40, 49);
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 // Read from the cache (40-49).
4151 RangeTransactionServer handler;
4152 handler.set_not_modified(true);
4153 MockTransaction transaction(kRangeGET_TransactionOK);
4154 transaction.load_flags |= net::LOAD_VALIDATE_CACHE;
4155 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4157 Verify206Response(headers, 40, 49);
4158 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4159 EXPECT_EQ(1, cache.disk_cache()->open_count());
4160 EXPECT_EQ(1, cache.disk_cache()->create_count());
4162 RemoveMockTransaction(&kRangeGET_TransactionOK);
4165 // Tests that we deal with 206s when revalidating range requests.
4166 TEST(HttpCache, RangeGET_ModifiedResult) {
4167 MockHttpCache cache;
4168 AddMockTransaction(&kRangeGET_TransactionOK);
4169 std::string headers;
4171 // Write to the cache (40-49).
4172 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
4173 &headers);
4175 Verify206Response(headers, 40, 49);
4176 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4177 EXPECT_EQ(0, cache.disk_cache()->open_count());
4178 EXPECT_EQ(1, cache.disk_cache()->create_count());
4180 // Attempt to read from the cache (40-49).
4181 RangeTransactionServer handler;
4182 handler.set_modified(true);
4183 MockTransaction transaction(kRangeGET_TransactionOK);
4184 transaction.load_flags |= net::LOAD_VALIDATE_CACHE;
4185 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4187 Verify206Response(headers, 40, 49);
4188 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4189 EXPECT_EQ(1, cache.disk_cache()->open_count());
4190 EXPECT_EQ(1, cache.disk_cache()->create_count());
4192 // And the entry should be gone.
4193 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
4194 EXPECT_EQ(3, cache.network_layer()->transaction_count());
4195 EXPECT_EQ(1, cache.disk_cache()->open_count());
4196 EXPECT_EQ(2, cache.disk_cache()->create_count());
4198 RemoveMockTransaction(&kRangeGET_TransactionOK);
4201 // Tests that when a server returns 206 with a sub-range of the requested range,
4202 // and there is nothing stored in the cache, the returned response is passed to
4203 // the caller as is. In this context, a subrange means a response that starts
4204 // with the same byte that was requested, but that is not the whole range that
4205 // was requested.
4206 TEST(HttpCache, RangeGET_206ReturnsSubrangeRange_NoCachedContent) {
4207 MockHttpCache cache;
4208 std::string headers;
4210 // Request a large range (40-59). The server sends 40-49.
4211 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4212 transaction.request_headers = "Range: bytes = 40-59\r\n" EXTRA_HEADER;
4213 transaction.response_headers =
4214 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
4215 "ETag: \"foo\"\n"
4216 "Accept-Ranges: bytes\n"
4217 "Content-Length: 10\n"
4218 "Content-Range: bytes 40-49/80\n";
4219 transaction.handler = nullptr;
4220 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4222 Verify206Response(headers, 40, 49);
4223 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4224 EXPECT_EQ(0, cache.disk_cache()->open_count());
4225 EXPECT_EQ(1, cache.disk_cache()->create_count());
4228 // Tests that when a server returns 206 with a sub-range of the requested range,
4229 // and there was an entry stored in the cache, the cache gets out of the way.
4230 TEST(HttpCache, RangeGET_206ReturnsSubrangeRange_CachedContent) {
4231 MockHttpCache cache;
4232 std::string headers;
4234 // Write to the cache (70-79).
4235 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4236 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER;
4237 transaction.data = "rg: 70-79 ";
4238 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4239 Verify206Response(headers, 70, 79);
4241 // Request a large range (40-79). The cache will ask the server for 40-59.
4242 // The server returns 40-49. The cache should consider the server confused and
4243 // abort caching, restarting the request without caching.
4244 transaction.request_headers = "Range: bytes = 40-79\r\n" EXTRA_HEADER;
4245 transaction.response_headers =
4246 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
4247 "ETag: \"foo\"\n"
4248 "Accept-Ranges: bytes\n"
4249 "Content-Length: 10\n"
4250 "Content-Range: bytes 40-49/80\n";
4251 transaction.handler = nullptr;
4252 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4254 // Two new network requests were issued, one from the cache and another after
4255 // deleting the entry.
4256 Verify206Response(headers, 40, 49);
4257 EXPECT_EQ(3, cache.network_layer()->transaction_count());
4258 EXPECT_EQ(1, cache.disk_cache()->open_count());
4259 EXPECT_EQ(1, cache.disk_cache()->create_count());
4261 // The entry was deleted.
4262 RunTransactionTest(cache.http_cache(), transaction);
4263 EXPECT_EQ(4, cache.network_layer()->transaction_count());
4264 EXPECT_EQ(1, cache.disk_cache()->open_count());
4265 EXPECT_EQ(2, cache.disk_cache()->create_count());
4268 // Tests that when a server returns 206 with a sub-range of the requested range,
4269 // and there was an entry stored in the cache, the cache gets out of the way,
4270 // when the caller is not using ranges.
4271 TEST(HttpCache, GET_206ReturnsSubrangeRange_CachedContent) {
4272 MockHttpCache cache;
4273 std::string headers;
4275 // Write to the cache (70-79).
4276 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4277 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER;
4278 transaction.data = "rg: 70-79 ";
4279 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4280 Verify206Response(headers, 70, 79);
4282 // Don't ask for a range. The cache will ask the server for 0-69.
4283 // The server returns 40-49. The cache should consider the server confused and
4284 // abort caching, restarting the request.
4285 // The second network request should not be a byte range request so the server
4286 // should return 200 + "Not a range"
4287 transaction.request_headers = "X-Return-Default-Range:\r\n" EXTRA_HEADER;
4288 transaction.data = "Not a range";
4289 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4291 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
4292 EXPECT_EQ(3, cache.network_layer()->transaction_count());
4293 EXPECT_EQ(1, cache.disk_cache()->open_count());
4294 EXPECT_EQ(1, cache.disk_cache()->create_count());
4296 // The entry was deleted.
4297 RunTransactionTest(cache.http_cache(), transaction);
4298 EXPECT_EQ(4, cache.network_layer()->transaction_count());
4299 EXPECT_EQ(1, cache.disk_cache()->open_count());
4300 EXPECT_EQ(2, cache.disk_cache()->create_count());
4303 // Tests that when a server returns 206 with a random range and there is
4304 // nothing stored in the cache, the returned response is passed to the caller
4305 // as is. In this context, a WrongRange means that the returned range may or may
4306 // not have any relationship with the requested range (may or may not be
4307 // contained). The important part is that the first byte doesn't match the first
4308 // requested byte.
4309 TEST(HttpCache, RangeGET_206ReturnsWrongRange_NoCachedContent) {
4310 MockHttpCache cache;
4311 std::string headers;
4313 // Request a large range (30-59). The server sends (40-49).
4314 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4315 transaction.request_headers = "Range: bytes = 30-59\r\n" EXTRA_HEADER;
4316 transaction.response_headers =
4317 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
4318 "ETag: \"foo\"\n"
4319 "Accept-Ranges: bytes\n"
4320 "Content-Length: 10\n"
4321 "Content-Range: bytes 40-49/80\n";
4322 transaction.handler = nullptr;
4323 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4325 Verify206Response(headers, 40, 49);
4326 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4327 EXPECT_EQ(0, cache.disk_cache()->open_count());
4328 EXPECT_EQ(1, cache.disk_cache()->create_count());
4330 // The entry was deleted.
4331 RunTransactionTest(cache.http_cache(), transaction);
4332 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4333 EXPECT_EQ(0, cache.disk_cache()->open_count());
4334 EXPECT_EQ(2, cache.disk_cache()->create_count());
4337 // Tests that when a server returns 206 with a random range and there is
4338 // an entry stored in the cache, the cache gets out of the way.
4339 TEST(HttpCache, RangeGET_206ReturnsWrongRange_CachedContent) {
4340 MockHttpCache cache;
4341 std::string headers;
4343 // Write to the cache (70-79).
4344 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4345 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER;
4346 transaction.data = "rg: 70-79 ";
4347 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4348 Verify206Response(headers, 70, 79);
4350 // Request a large range (30-79). The cache will ask the server for 30-69.
4351 // The server returns 40-49. The cache should consider the server confused and
4352 // abort caching, returning the weird range to the caller.
4353 transaction.request_headers = "Range: bytes = 30-79\r\n" EXTRA_HEADER;
4354 transaction.response_headers =
4355 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
4356 "ETag: \"foo\"\n"
4357 "Accept-Ranges: bytes\n"
4358 "Content-Length: 10\n"
4359 "Content-Range: bytes 40-49/80\n";
4360 transaction.handler = nullptr;
4361 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4363 Verify206Response(headers, 40, 49);
4364 EXPECT_EQ(3, cache.network_layer()->transaction_count());
4365 EXPECT_EQ(1, cache.disk_cache()->open_count());
4366 EXPECT_EQ(1, cache.disk_cache()->create_count());
4368 // The entry was deleted.
4369 RunTransactionTest(cache.http_cache(), transaction);
4370 EXPECT_EQ(4, cache.network_layer()->transaction_count());
4371 EXPECT_EQ(1, cache.disk_cache()->open_count());
4372 EXPECT_EQ(2, cache.disk_cache()->create_count());
4375 // Tests that when a caller asks for a range beyond EOF, with an empty cache,
4376 // the response matches the one provided by the server.
4377 TEST(HttpCache, RangeGET_206ReturnsSmallerFile_NoCachedContent) {
4378 MockHttpCache cache;
4379 std::string headers;
4381 // Request a large range (70-99). The server sends 70-79.
4382 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4383 transaction.request_headers = "Range: bytes = 70-99\r\n" EXTRA_HEADER;
4384 transaction.data = "rg: 70-79 ";
4385 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4387 Verify206Response(headers, 70, 79);
4388 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4389 EXPECT_EQ(0, cache.disk_cache()->open_count());
4390 EXPECT_EQ(1, cache.disk_cache()->create_count());
4392 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
4393 EXPECT_EQ(1, cache.disk_cache()->open_count());
4396 // Tests that when a caller asks for a range beyond EOF, with a cached entry,
4397 // the cache automatically fixes the request.
4398 TEST(HttpCache, RangeGET_206ReturnsSmallerFile_CachedContent) {
4399 MockHttpCache cache;
4400 std::string headers;
4402 // Write to the cache (40-49).
4403 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4404 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4406 // Request a large range (70-99). The server sends 70-79.
4407 transaction.request_headers = "Range: bytes = 70-99\r\n" EXTRA_HEADER;
4408 transaction.data = "rg: 70-79 ";
4409 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4411 Verify206Response(headers, 70, 79);
4412 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4413 EXPECT_EQ(1, cache.disk_cache()->open_count());
4414 EXPECT_EQ(1, cache.disk_cache()->create_count());
4416 // The entry was not deleted (the range was automatically fixed).
4417 RunTransactionTest(cache.http_cache(), transaction);
4418 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4419 EXPECT_EQ(2, cache.disk_cache()->open_count());
4420 EXPECT_EQ(1, cache.disk_cache()->create_count());
4423 // Tests that when a caller asks for a not-satisfiable range, the server's
4424 // response is forwarded to the caller.
4425 TEST(HttpCache, RangeGET_416_NoCachedContent) {
4426 MockHttpCache cache;
4427 std::string headers;
4429 // Request a range beyond EOF (80-99).
4430 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4431 transaction.request_headers = "Range: bytes = 80-99\r\n" EXTRA_HEADER;
4432 transaction.data = "";
4433 transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable";
4434 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4436 EXPECT_EQ(0U, headers.find(transaction.status));
4437 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4438 EXPECT_EQ(0, cache.disk_cache()->open_count());
4439 EXPECT_EQ(1, cache.disk_cache()->create_count());
4441 // The entry was deleted.
4442 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
4443 EXPECT_EQ(2, cache.disk_cache()->create_count());
4446 // Tests that we cache 301s for range requests.
4447 TEST(HttpCache, RangeGET_301) {
4448 MockHttpCache cache;
4449 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4450 transaction.status = "HTTP/1.1 301 Moved Permanently";
4451 transaction.response_headers = "Location: http://www.bar.com/\n";
4452 transaction.data = "";
4453 transaction.handler = NULL;
4455 // Write to the cache.
4456 RunTransactionTest(cache.http_cache(), transaction);
4457 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4458 EXPECT_EQ(0, cache.disk_cache()->open_count());
4459 EXPECT_EQ(1, cache.disk_cache()->create_count());
4461 // Read from the cache.
4462 RunTransactionTest(cache.http_cache(), transaction);
4463 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4464 EXPECT_EQ(1, cache.disk_cache()->open_count());
4465 EXPECT_EQ(1, cache.disk_cache()->create_count());
4468 // Tests that we can cache range requests when the start or end is unknown.
4469 // We start with one suffix request, followed by a request from a given point.
4470 TEST(HttpCache, UnknownRangeGET_1) {
4471 MockHttpCache cache;
4472 AddMockTransaction(&kRangeGET_TransactionOK);
4473 std::string headers;
4475 // Write to the cache (70-79).
4476 MockTransaction transaction(kRangeGET_TransactionOK);
4477 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
4478 transaction.data = "rg: 70-79 ";
4479 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4481 Verify206Response(headers, 70, 79);
4482 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4483 EXPECT_EQ(0, cache.disk_cache()->open_count());
4484 EXPECT_EQ(1, cache.disk_cache()->create_count());
4486 // Make sure we are done with the previous transaction.
4487 base::MessageLoop::current()->RunUntilIdle();
4489 // Write and read from the cache (60-79).
4490 transaction.request_headers = "Range: bytes = 60-\r\n" EXTRA_HEADER;
4491 transaction.data = "rg: 60-69 rg: 70-79 ";
4492 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4494 Verify206Response(headers, 60, 79);
4495 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4496 EXPECT_EQ(1, cache.disk_cache()->open_count());
4497 EXPECT_EQ(1, cache.disk_cache()->create_count());
4499 RemoveMockTransaction(&kRangeGET_TransactionOK);
4502 // Tests that we can cache range requests when the start or end is unknown.
4503 // We start with one request from a given point, followed by a suffix request.
4504 // We'll also verify that synchronous cache responses work as intended.
4505 TEST(HttpCache, UnknownRangeGET_2) {
4506 MockHttpCache cache;
4507 std::string headers;
4509 MockTransaction transaction(kRangeGET_TransactionOK);
4510 transaction.test_mode = TEST_MODE_SYNC_CACHE_START |
4511 TEST_MODE_SYNC_CACHE_READ |
4512 TEST_MODE_SYNC_CACHE_WRITE;
4513 AddMockTransaction(&transaction);
4515 // Write to the cache (70-79).
4516 transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER;
4517 transaction.data = "rg: 70-79 ";
4518 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4520 Verify206Response(headers, 70, 79);
4521 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4522 EXPECT_EQ(0, cache.disk_cache()->open_count());
4523 EXPECT_EQ(1, cache.disk_cache()->create_count());
4525 // Make sure we are done with the previous transaction.
4526 base::MessageLoop::current()->RunUntilIdle();
4528 // Write and read from the cache (60-79).
4529 transaction.request_headers = "Range: bytes = -20\r\n" EXTRA_HEADER;
4530 transaction.data = "rg: 60-69 rg: 70-79 ";
4531 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4533 Verify206Response(headers, 60, 79);
4534 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4535 EXPECT_EQ(1, cache.disk_cache()->open_count());
4536 EXPECT_EQ(1, cache.disk_cache()->create_count());
4538 RemoveMockTransaction(&transaction);
4541 // Tests that receiving Not Modified when asking for an open range doesn't mess
4542 // up things.
4543 TEST(HttpCache, UnknownRangeGET_304) {
4544 MockHttpCache cache;
4545 std::string headers;
4547 MockTransaction transaction(kRangeGET_TransactionOK);
4548 AddMockTransaction(&transaction);
4550 RangeTransactionServer handler;
4551 handler.set_not_modified(true);
4553 // Ask for the end of the file, without knowing the length.
4554 transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER;
4555 transaction.data = "";
4556 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4558 // We just bypass the cache.
4559 EXPECT_EQ(0U, headers.find("HTTP/1.1 304 Not Modified\n"));
4560 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4561 EXPECT_EQ(0, cache.disk_cache()->open_count());
4562 EXPECT_EQ(1, cache.disk_cache()->create_count());
4564 RunTransactionTest(cache.http_cache(), transaction);
4565 EXPECT_EQ(2, cache.disk_cache()->create_count());
4567 RemoveMockTransaction(&transaction);
4570 // Tests that we can handle non-range requests when we have cached a range.
4571 TEST(HttpCache, GET_Previous206) {
4572 MockHttpCache cache;
4573 AddMockTransaction(&kRangeGET_TransactionOK);
4574 std::string headers;
4575 net::CapturingBoundNetLog log;
4576 net::LoadTimingInfo load_timing_info;
4578 // Write to the cache (40-49).
4579 RunTransactionTestWithResponseAndGetTiming(
4580 cache.http_cache(), kRangeGET_TransactionOK, &headers, log.bound(),
4581 &load_timing_info);
4583 Verify206Response(headers, 40, 49);
4584 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4585 EXPECT_EQ(0, cache.disk_cache()->open_count());
4586 EXPECT_EQ(1, cache.disk_cache()->create_count());
4587 TestLoadTimingNetworkRequest(load_timing_info);
4589 // Write and read from the cache (0-79), when not asked for a range.
4590 MockTransaction transaction(kRangeGET_TransactionOK);
4591 transaction.request_headers = EXTRA_HEADER;
4592 transaction.data = kFullRangeData;
4593 RunTransactionTestWithResponseAndGetTiming(
4594 cache.http_cache(), transaction, &headers, log.bound(),
4595 &load_timing_info);
4597 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
4598 EXPECT_EQ(3, cache.network_layer()->transaction_count());
4599 EXPECT_EQ(1, cache.disk_cache()->open_count());
4600 EXPECT_EQ(1, cache.disk_cache()->create_count());
4601 TestLoadTimingNetworkRequest(load_timing_info);
4603 RemoveMockTransaction(&kRangeGET_TransactionOK);
4606 // Tests that we can handle non-range requests when we have cached the first
4607 // part of the object and the server replies with 304 (Not Modified).
4608 TEST(HttpCache, GET_Previous206_NotModified) {
4609 MockHttpCache cache;
4611 MockTransaction transaction(kRangeGET_TransactionOK);
4612 AddMockTransaction(&transaction);
4613 std::string headers;
4614 net::CapturingBoundNetLog log;
4615 net::LoadTimingInfo load_timing_info;
4617 // Write to the cache (0-9).
4618 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
4619 transaction.data = "rg: 00-09 ";
4620 RunTransactionTestWithResponseAndGetTiming(
4621 cache.http_cache(), transaction, &headers, log.bound(),
4622 &load_timing_info);
4623 Verify206Response(headers, 0, 9);
4624 TestLoadTimingNetworkRequest(load_timing_info);
4626 // Write to the cache (70-79).
4627 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER;
4628 transaction.data = "rg: 70-79 ";
4629 RunTransactionTestWithResponseAndGetTiming(
4630 cache.http_cache(), transaction, &headers, log.bound(),
4631 &load_timing_info);
4632 Verify206Response(headers, 70, 79);
4634 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4635 EXPECT_EQ(1, cache.disk_cache()->open_count());
4636 EXPECT_EQ(1, cache.disk_cache()->create_count());
4637 TestLoadTimingNetworkRequest(load_timing_info);
4639 // Read from the cache (0-9), write and read from cache (10 - 79).
4640 transaction.load_flags |= net::LOAD_VALIDATE_CACHE;
4641 transaction.request_headers = "Foo: bar\r\n" EXTRA_HEADER;
4642 transaction.data = kFullRangeData;
4643 RunTransactionTestWithResponseAndGetTiming(
4644 cache.http_cache(), transaction, &headers, log.bound(),
4645 &load_timing_info);
4647 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
4648 EXPECT_EQ(4, cache.network_layer()->transaction_count());
4649 EXPECT_EQ(2, cache.disk_cache()->open_count());
4650 EXPECT_EQ(1, cache.disk_cache()->create_count());
4651 TestLoadTimingNetworkRequest(load_timing_info);
4653 RemoveMockTransaction(&transaction);
4656 // Tests that we can handle a regular request to a sparse entry, that results in
4657 // new content provided by the server (206).
4658 TEST(HttpCache, GET_Previous206_NewContent) {
4659 MockHttpCache cache;
4660 AddMockTransaction(&kRangeGET_TransactionOK);
4661 std::string headers;
4663 // Write to the cache (0-9).
4664 MockTransaction transaction(kRangeGET_TransactionOK);
4665 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
4666 transaction.data = "rg: 00-09 ";
4667 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4669 Verify206Response(headers, 0, 9);
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 // Now we'll issue a request without any range that should result first in a
4675 // 206 (when revalidating), and then in a weird standard answer: the test
4676 // server will not modify the response so we'll get the default range... a
4677 // real server will answer with 200.
4678 MockTransaction transaction2(kRangeGET_TransactionOK);
4679 transaction2.request_headers = EXTRA_HEADER;
4680 transaction2.load_flags |= net::LOAD_VALIDATE_CACHE;
4681 transaction2.data = "Not a range";
4682 RangeTransactionServer handler;
4683 handler.set_modified(true);
4684 net::CapturingBoundNetLog log;
4685 net::LoadTimingInfo load_timing_info;
4686 RunTransactionTestWithResponseAndGetTiming(
4687 cache.http_cache(), transaction2, &headers, log.bound(),
4688 &load_timing_info);
4690 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
4691 EXPECT_EQ(3, cache.network_layer()->transaction_count());
4692 EXPECT_EQ(1, cache.disk_cache()->open_count());
4693 EXPECT_EQ(1, cache.disk_cache()->create_count());
4694 TestLoadTimingNetworkRequest(load_timing_info);
4696 // Verify that the previous request deleted the entry.
4697 RunTransactionTest(cache.http_cache(), transaction);
4698 EXPECT_EQ(2, cache.disk_cache()->create_count());
4700 RemoveMockTransaction(&transaction);
4703 // Tests that we can handle cached 206 responses that are not sparse.
4704 TEST(HttpCache, GET_Previous206_NotSparse) {
4705 MockHttpCache cache;
4707 // Create a disk cache entry that stores 206 headers while not being sparse.
4708 disk_cache::Entry* entry;
4709 ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry,
4710 NULL));
4712 std::string raw_headers(kRangeGET_TransactionOK.status);
4713 raw_headers.append("\n");
4714 raw_headers.append(kRangeGET_TransactionOK.response_headers);
4715 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
4716 raw_headers.size());
4718 net::HttpResponseInfo response;
4719 response.headers = new net::HttpResponseHeaders(raw_headers);
4720 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
4722 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500));
4723 int len = static_cast<int>(base::strlcpy(buf->data(),
4724 kRangeGET_TransactionOK.data, 500));
4725 net::TestCompletionCallback cb;
4726 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true);
4727 EXPECT_EQ(len, cb.GetResult(rv));
4728 entry->Close();
4730 // Now see that we don't use the stored entry.
4731 std::string headers;
4732 net::CapturingBoundNetLog log;
4733 net::LoadTimingInfo load_timing_info;
4734 RunTransactionTestWithResponseAndGetTiming(
4735 cache.http_cache(), kSimpleGET_Transaction, &headers, log.bound(),
4736 &load_timing_info);
4738 // We are expecting a 200.
4739 std::string expected_headers(kSimpleGET_Transaction.status);
4740 expected_headers.append("\n");
4741 expected_headers.append(kSimpleGET_Transaction.response_headers);
4742 EXPECT_EQ(expected_headers, headers);
4743 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4744 EXPECT_EQ(1, cache.disk_cache()->open_count());
4745 EXPECT_EQ(2, cache.disk_cache()->create_count());
4746 TestLoadTimingNetworkRequest(load_timing_info);
4749 // Tests that we can handle cached 206 responses that are not sparse. This time
4750 // we issue a range request and expect to receive a range.
4751 TEST(HttpCache, RangeGET_Previous206_NotSparse_2) {
4752 MockHttpCache cache;
4753 AddMockTransaction(&kRangeGET_TransactionOK);
4755 // Create a disk cache entry that stores 206 headers while not being sparse.
4756 disk_cache::Entry* entry;
4757 ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry,
4758 NULL));
4760 std::string raw_headers(kRangeGET_TransactionOK.status);
4761 raw_headers.append("\n");
4762 raw_headers.append(kRangeGET_TransactionOK.response_headers);
4763 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
4764 raw_headers.size());
4766 net::HttpResponseInfo response;
4767 response.headers = new net::HttpResponseHeaders(raw_headers);
4768 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
4770 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500));
4771 int len = static_cast<int>(base::strlcpy(buf->data(),
4772 kRangeGET_TransactionOK.data, 500));
4773 net::TestCompletionCallback cb;
4774 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true);
4775 EXPECT_EQ(len, cb.GetResult(rv));
4776 entry->Close();
4778 // Now see that we don't use the stored entry.
4779 std::string headers;
4780 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
4781 &headers);
4783 // We are expecting a 206.
4784 Verify206Response(headers, 40, 49);
4785 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4786 EXPECT_EQ(1, cache.disk_cache()->open_count());
4787 EXPECT_EQ(2, cache.disk_cache()->create_count());
4789 RemoveMockTransaction(&kRangeGET_TransactionOK);
4792 // Tests that we can handle cached 206 responses that can't be validated.
4793 TEST(HttpCache, GET_Previous206_NotValidation) {
4794 MockHttpCache cache;
4796 // Create a disk cache entry that stores 206 headers.
4797 disk_cache::Entry* entry;
4798 ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry,
4799 NULL));
4801 // Make sure that the headers cannot be validated with the server.
4802 std::string raw_headers(kRangeGET_TransactionOK.status);
4803 raw_headers.append("\n");
4804 raw_headers.append("Content-Length: 80\n");
4805 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
4806 raw_headers.size());
4808 net::HttpResponseInfo response;
4809 response.headers = new net::HttpResponseHeaders(raw_headers);
4810 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
4812 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500));
4813 int len = static_cast<int>(base::strlcpy(buf->data(),
4814 kRangeGET_TransactionOK.data, 500));
4815 net::TestCompletionCallback cb;
4816 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true);
4817 EXPECT_EQ(len, cb.GetResult(rv));
4818 entry->Close();
4820 // Now see that we don't use the stored entry.
4821 std::string headers;
4822 RunTransactionTestWithResponse(cache.http_cache(), kSimpleGET_Transaction,
4823 &headers);
4825 // We are expecting a 200.
4826 std::string expected_headers(kSimpleGET_Transaction.status);
4827 expected_headers.append("\n");
4828 expected_headers.append(kSimpleGET_Transaction.response_headers);
4829 EXPECT_EQ(expected_headers, headers);
4830 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4831 EXPECT_EQ(1, cache.disk_cache()->open_count());
4832 EXPECT_EQ(2, cache.disk_cache()->create_count());
4835 // Tests that we can handle range requests with cached 200 responses.
4836 TEST(HttpCache, RangeGET_Previous200) {
4837 MockHttpCache cache;
4839 // Store the whole thing with status 200.
4840 MockTransaction transaction(kTypicalGET_Transaction);
4841 transaction.url = kRangeGET_TransactionOK.url;
4842 transaction.data = kFullRangeData;
4843 AddMockTransaction(&transaction);
4844 RunTransactionTest(cache.http_cache(), transaction);
4845 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4846 EXPECT_EQ(0, cache.disk_cache()->open_count());
4847 EXPECT_EQ(1, cache.disk_cache()->create_count());
4849 RemoveMockTransaction(&transaction);
4850 AddMockTransaction(&kRangeGET_TransactionOK);
4852 // Now see that we use the stored entry.
4853 std::string headers;
4854 MockTransaction transaction2(kRangeGET_TransactionOK);
4855 RangeTransactionServer handler;
4856 handler.set_not_modified(true);
4857 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
4859 // We are expecting a 206.
4860 Verify206Response(headers, 40, 49);
4861 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4862 EXPECT_EQ(1, cache.disk_cache()->open_count());
4863 EXPECT_EQ(1, cache.disk_cache()->create_count());
4865 // The last transaction has finished so make sure the entry is deactivated.
4866 base::MessageLoop::current()->RunUntilIdle();
4868 // Make a request for an invalid range.
4869 MockTransaction transaction3(kRangeGET_TransactionOK);
4870 transaction3.request_headers = "Range: bytes = 80-90\r\n" EXTRA_HEADER;
4871 transaction3.data = transaction.data;
4872 transaction3.load_flags = net::LOAD_PREFERRING_CACHE;
4873 RunTransactionTestWithResponse(cache.http_cache(), transaction3, &headers);
4874 EXPECT_EQ(2, cache.disk_cache()->open_count());
4875 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 "));
4876 EXPECT_EQ(std::string::npos, headers.find("Content-Range:"));
4877 EXPECT_EQ(std::string::npos, headers.find("Content-Length: 80"));
4879 // Make sure the entry is deactivated.
4880 base::MessageLoop::current()->RunUntilIdle();
4882 // Even though the request was invalid, we should have the entry.
4883 RunTransactionTest(cache.http_cache(), transaction2);
4884 EXPECT_EQ(3, cache.disk_cache()->open_count());
4886 // Make sure the entry is deactivated.
4887 base::MessageLoop::current()->RunUntilIdle();
4889 // Now we should receive a range from the server and drop the stored entry.
4890 handler.set_not_modified(false);
4891 transaction2.request_headers = kRangeGET_TransactionOK.request_headers;
4892 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
4893 Verify206Response(headers, 40, 49);
4894 EXPECT_EQ(4, cache.network_layer()->transaction_count());
4895 EXPECT_EQ(4, cache.disk_cache()->open_count());
4896 EXPECT_EQ(1, cache.disk_cache()->create_count());
4898 RunTransactionTest(cache.http_cache(), transaction2);
4899 EXPECT_EQ(2, cache.disk_cache()->create_count());
4901 RemoveMockTransaction(&kRangeGET_TransactionOK);
4904 // Tests that we can handle a 200 response when dealing with sparse entries.
4905 TEST(HttpCache, RangeRequestResultsIn200) {
4906 MockHttpCache cache;
4907 AddMockTransaction(&kRangeGET_TransactionOK);
4908 std::string headers;
4910 // Write to the cache (70-79).
4911 MockTransaction transaction(kRangeGET_TransactionOK);
4912 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
4913 transaction.data = "rg: 70-79 ";
4914 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4916 Verify206Response(headers, 70, 79);
4917 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4918 EXPECT_EQ(0, cache.disk_cache()->open_count());
4919 EXPECT_EQ(1, cache.disk_cache()->create_count());
4921 // Now we'll issue a request that results in a plain 200 response, but to
4922 // the to the same URL that we used to store sparse data, and making sure
4923 // that we ask for a range.
4924 RemoveMockTransaction(&kRangeGET_TransactionOK);
4925 MockTransaction transaction2(kSimpleGET_Transaction);
4926 transaction2.url = kRangeGET_TransactionOK.url;
4927 transaction2.request_headers = kRangeGET_TransactionOK.request_headers;
4928 AddMockTransaction(&transaction2);
4930 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
4932 std::string expected_headers(kSimpleGET_Transaction.status);
4933 expected_headers.append("\n");
4934 expected_headers.append(kSimpleGET_Transaction.response_headers);
4935 EXPECT_EQ(expected_headers, headers);
4936 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4937 EXPECT_EQ(1, cache.disk_cache()->open_count());
4938 EXPECT_EQ(1, cache.disk_cache()->create_count());
4940 RemoveMockTransaction(&transaction2);
4943 // Tests that a range request that falls outside of the size that we know about
4944 // only deletes the entry if the resource has indeed changed.
4945 TEST(HttpCache, RangeGET_MoreThanCurrentSize) {
4946 MockHttpCache cache;
4947 AddMockTransaction(&kRangeGET_TransactionOK);
4948 std::string headers;
4950 // Write to the cache (40-49).
4951 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
4952 &headers);
4954 Verify206Response(headers, 40, 49);
4955 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4956 EXPECT_EQ(0, cache.disk_cache()->open_count());
4957 EXPECT_EQ(1, cache.disk_cache()->create_count());
4959 // A weird request should not delete this entry. Ask for bytes 120-.
4960 MockTransaction transaction(kRangeGET_TransactionOK);
4961 transaction.request_headers = "Range: bytes = 120-\r\n" EXTRA_HEADER;
4962 transaction.data = "";
4963 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4965 EXPECT_EQ(0U, headers.find("HTTP/1.1 416 "));
4966 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4967 EXPECT_EQ(1, cache.disk_cache()->open_count());
4968 EXPECT_EQ(1, cache.disk_cache()->create_count());
4970 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
4971 EXPECT_EQ(2, cache.disk_cache()->open_count());
4972 EXPECT_EQ(1, cache.disk_cache()->create_count());
4974 RemoveMockTransaction(&kRangeGET_TransactionOK);
4977 // Tests that we don't delete a sparse entry when we cancel a request.
4978 TEST(HttpCache, RangeGET_Cancel) {
4979 MockHttpCache cache;
4980 AddMockTransaction(&kRangeGET_TransactionOK);
4982 MockHttpRequest request(kRangeGET_TransactionOK);
4984 Context* c = new Context();
4985 int rv = cache.CreateTransaction(&c->trans);
4986 ASSERT_EQ(net::OK, rv);
4988 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
4989 if (rv == net::ERR_IO_PENDING)
4990 rv = c->callback.WaitForResult();
4992 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4993 EXPECT_EQ(0, cache.disk_cache()->open_count());
4994 EXPECT_EQ(1, cache.disk_cache()->create_count());
4996 // Make sure that the entry has some data stored.
4997 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10));
4998 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
4999 if (rv == net::ERR_IO_PENDING)
5000 rv = c->callback.WaitForResult();
5001 EXPECT_EQ(buf->size(), rv);
5003 // Destroy the transaction.
5004 delete c;
5006 // Verify that the entry has not been deleted.
5007 disk_cache::Entry* entry;
5008 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
5009 entry->Close();
5010 RemoveMockTransaction(&kRangeGET_TransactionOK);
5013 // Tests that we don't delete a sparse entry when we start a new request after
5014 // cancelling the previous one.
5015 TEST(HttpCache, RangeGET_Cancel2) {
5016 MockHttpCache cache;
5017 AddMockTransaction(&kRangeGET_TransactionOK);
5019 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5020 MockHttpRequest request(kRangeGET_TransactionOK);
5021 request.load_flags |= net::LOAD_VALIDATE_CACHE;
5023 Context* c = new Context();
5024 int rv = cache.CreateTransaction(&c->trans);
5025 ASSERT_EQ(net::OK, rv);
5027 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
5028 if (rv == net::ERR_IO_PENDING)
5029 rv = c->callback.WaitForResult();
5031 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5032 EXPECT_EQ(1, cache.disk_cache()->open_count());
5033 EXPECT_EQ(1, cache.disk_cache()->create_count());
5035 // Make sure that we revalidate the entry and read from the cache (a single
5036 // read will return while waiting for the network).
5037 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(5));
5038 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
5039 EXPECT_EQ(5, c->callback.GetResult(rv));
5040 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
5041 EXPECT_EQ(net::ERR_IO_PENDING, rv);
5043 // Destroy the transaction before completing the read.
5044 delete c;
5046 // We have the read and the delete (OnProcessPendingQueue) waiting on the
5047 // message loop. This means that a new transaction will just reuse the same
5048 // active entry (no open or create).
5050 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5052 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5053 EXPECT_EQ(1, cache.disk_cache()->open_count());
5054 EXPECT_EQ(1, cache.disk_cache()->create_count());
5055 RemoveMockTransaction(&kRangeGET_TransactionOK);
5058 // A slight variation of the previous test, this time we cancel two requests in
5059 // a row, making sure that the second is waiting for the entry to be ready.
5060 TEST(HttpCache, RangeGET_Cancel3) {
5061 MockHttpCache cache;
5062 AddMockTransaction(&kRangeGET_TransactionOK);
5064 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5065 MockHttpRequest request(kRangeGET_TransactionOK);
5066 request.load_flags |= net::LOAD_VALIDATE_CACHE;
5068 Context* c = new Context();
5069 int rv = cache.CreateTransaction(&c->trans);
5070 ASSERT_EQ(net::OK, rv);
5072 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
5073 EXPECT_EQ(net::ERR_IO_PENDING, rv);
5074 rv = c->callback.WaitForResult();
5076 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5077 EXPECT_EQ(1, cache.disk_cache()->open_count());
5078 EXPECT_EQ(1, cache.disk_cache()->create_count());
5080 // Make sure that we revalidate the entry and read from the cache (a single
5081 // read will return while waiting for the network).
5082 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(5));
5083 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
5084 EXPECT_EQ(5, c->callback.GetResult(rv));
5085 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
5086 EXPECT_EQ(net::ERR_IO_PENDING, rv);
5088 // Destroy the transaction before completing the read.
5089 delete c;
5091 // We have the read and the delete (OnProcessPendingQueue) waiting on the
5092 // message loop. This means that a new transaction will just reuse the same
5093 // active entry (no open or create).
5095 c = new Context();
5096 rv = cache.CreateTransaction(&c->trans);
5097 ASSERT_EQ(net::OK, rv);
5099 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
5100 EXPECT_EQ(net::ERR_IO_PENDING, rv);
5102 MockDiskEntry::IgnoreCallbacks(true);
5103 base::MessageLoop::current()->RunUntilIdle();
5104 MockDiskEntry::IgnoreCallbacks(false);
5106 // The new transaction is waiting for the query range callback.
5107 delete c;
5109 // And we should not crash when the callback is delivered.
5110 base::MessageLoop::current()->RunUntilIdle();
5112 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5113 EXPECT_EQ(1, cache.disk_cache()->open_count());
5114 EXPECT_EQ(1, cache.disk_cache()->create_count());
5115 RemoveMockTransaction(&kRangeGET_TransactionOK);
5118 // Tests that an invalid range response results in no cached entry.
5119 TEST(HttpCache, RangeGET_InvalidResponse1) {
5120 MockHttpCache cache;
5121 std::string headers;
5123 MockTransaction transaction(kRangeGET_TransactionOK);
5124 transaction.handler = NULL;
5125 transaction.response_headers = "Content-Range: bytes 40-49/45\n"
5126 "Content-Length: 10\n";
5127 AddMockTransaction(&transaction);
5128 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5130 std::string expected(transaction.status);
5131 expected.append("\n");
5132 expected.append(transaction.response_headers);
5133 EXPECT_EQ(expected, headers);
5135 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5136 EXPECT_EQ(0, cache.disk_cache()->open_count());
5137 EXPECT_EQ(1, cache.disk_cache()->create_count());
5139 // Verify that we don't have a cached entry.
5140 disk_cache::Entry* entry;
5141 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
5143 RemoveMockTransaction(&kRangeGET_TransactionOK);
5146 // Tests that we reject a range that doesn't match the content-length.
5147 TEST(HttpCache, RangeGET_InvalidResponse2) {
5148 MockHttpCache cache;
5149 std::string headers;
5151 MockTransaction transaction(kRangeGET_TransactionOK);
5152 transaction.handler = NULL;
5153 transaction.response_headers = "Content-Range: bytes 40-49/80\n"
5154 "Content-Length: 20\n";
5155 AddMockTransaction(&transaction);
5156 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5158 std::string expected(transaction.status);
5159 expected.append("\n");
5160 expected.append(transaction.response_headers);
5161 EXPECT_EQ(expected, headers);
5163 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5164 EXPECT_EQ(0, cache.disk_cache()->open_count());
5165 EXPECT_EQ(1, cache.disk_cache()->create_count());
5167 // Verify that we don't have a cached entry.
5168 disk_cache::Entry* entry;
5169 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
5171 RemoveMockTransaction(&kRangeGET_TransactionOK);
5174 // Tests that if a server tells us conflicting information about a resource we
5175 // drop the entry.
5176 TEST(HttpCache, RangeGET_InvalidResponse3) {
5177 MockHttpCache cache;
5178 std::string headers;
5180 MockTransaction transaction(kRangeGET_TransactionOK);
5181 transaction.handler = NULL;
5182 transaction.request_headers = "Range: bytes = 50-59\r\n" EXTRA_HEADER;
5183 std::string response_headers(transaction.response_headers);
5184 response_headers.append("Content-Range: bytes 50-59/160\n");
5185 transaction.response_headers = response_headers.c_str();
5186 AddMockTransaction(&transaction);
5187 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5189 Verify206Response(headers, 50, 59);
5190 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5191 EXPECT_EQ(0, cache.disk_cache()->open_count());
5192 EXPECT_EQ(1, cache.disk_cache()->create_count());
5194 RemoveMockTransaction(&transaction);
5195 AddMockTransaction(&kRangeGET_TransactionOK);
5197 // This transaction will report a resource size of 80 bytes, and we think it's
5198 // 160 so we should ignore the response.
5199 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
5200 &headers);
5202 Verify206Response(headers, 40, 49);
5203 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5204 EXPECT_EQ(1, cache.disk_cache()->open_count());
5205 EXPECT_EQ(1, cache.disk_cache()->create_count());
5207 // Verify that the entry is gone.
5208 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5209 EXPECT_EQ(1, cache.disk_cache()->open_count());
5210 EXPECT_EQ(2, cache.disk_cache()->create_count());
5211 RemoveMockTransaction(&kRangeGET_TransactionOK);
5214 // Tests that we handle large range values properly.
5215 TEST(HttpCache, RangeGET_LargeValues) {
5216 // We need a real sparse cache for this test.
5217 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(1024 * 1024));
5218 std::string headers;
5220 MockTransaction transaction(kRangeGET_TransactionOK);
5221 transaction.handler = NULL;
5222 transaction.request_headers = "Range: bytes = 4294967288-4294967297\r\n"
5223 EXTRA_HEADER;
5224 transaction.response_headers =
5225 "ETag: \"foo\"\n"
5226 "Content-Range: bytes 4294967288-4294967297/4294967299\n"
5227 "Content-Length: 10\n";
5228 AddMockTransaction(&transaction);
5229 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5231 std::string expected(transaction.status);
5232 expected.append("\n");
5233 expected.append(transaction.response_headers);
5234 EXPECT_EQ(expected, headers);
5236 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5238 // Verify that we have a cached entry.
5239 disk_cache::Entry* en;
5240 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &en));
5241 en->Close();
5243 RemoveMockTransaction(&kRangeGET_TransactionOK);
5246 // Tests that we don't crash with a range request if the disk cache was not
5247 // initialized properly.
5248 TEST(HttpCache, RangeGET_NoDiskCache) {
5249 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
5250 factory->set_fail(true);
5251 factory->FinishCreation(); // We'll complete synchronously.
5252 MockHttpCache cache(factory);
5254 AddMockTransaction(&kRangeGET_TransactionOK);
5256 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5257 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5259 RemoveMockTransaction(&kRangeGET_TransactionOK);
5262 // Tests that we handle byte range requests that skip the cache.
5263 TEST(HttpCache, RangeHEAD) {
5264 MockHttpCache cache;
5265 AddMockTransaction(&kRangeGET_TransactionOK);
5267 MockTransaction transaction(kRangeGET_TransactionOK);
5268 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
5269 transaction.method = "HEAD";
5270 transaction.data = "rg: 70-79 ";
5272 std::string headers;
5273 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5275 Verify206Response(headers, 70, 79);
5276 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5277 EXPECT_EQ(0, cache.disk_cache()->open_count());
5278 EXPECT_EQ(0, cache.disk_cache()->create_count());
5280 RemoveMockTransaction(&kRangeGET_TransactionOK);
5283 // Tests that we don't crash when after reading from the cache we issue a
5284 // request for the next range and the server gives us a 200 synchronously.
5285 TEST(HttpCache, RangeGET_FastFlakyServer) {
5286 MockHttpCache cache;
5288 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
5289 transaction.request_headers = "Range: bytes = 40-\r\n" EXTRA_HEADER;
5290 transaction.test_mode = TEST_MODE_SYNC_NET_START;
5291 transaction.load_flags |= net::LOAD_VALIDATE_CACHE;
5293 // Write to the cache.
5294 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5296 // And now read from the cache and the network.
5297 RangeTransactionServer handler;
5298 handler.set_bad_200(true);
5299 transaction.data = "Not a range";
5300 net::CapturingBoundNetLog log;
5301 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound());
5303 EXPECT_EQ(3, cache.network_layer()->transaction_count());
5304 EXPECT_EQ(1, cache.disk_cache()->open_count());
5305 EXPECT_EQ(1, cache.disk_cache()->create_count());
5306 EXPECT_TRUE(LogContainsEventType(
5307 log, net::NetLog::TYPE_HTTP_CACHE_RE_SEND_PARTIAL_REQUEST));
5310 // Tests that when the server gives us less data than expected, we don't keep
5311 // asking for more data.
5312 TEST(HttpCache, RangeGET_FastFlakyServer2) {
5313 MockHttpCache cache;
5315 // First, check with an empty cache (WRITE mode).
5316 MockTransaction transaction(kRangeGET_TransactionOK);
5317 transaction.request_headers = "Range: bytes = 40-49\r\n" EXTRA_HEADER;
5318 transaction.data = "rg: 40-"; // Less than expected.
5319 transaction.handler = NULL;
5320 std::string headers(transaction.response_headers);
5321 headers.append("Content-Range: bytes 40-49/80\n");
5322 transaction.response_headers = headers.c_str();
5324 AddMockTransaction(&transaction);
5326 // Write to the cache.
5327 RunTransactionTest(cache.http_cache(), transaction);
5329 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5330 EXPECT_EQ(0, cache.disk_cache()->open_count());
5331 EXPECT_EQ(1, cache.disk_cache()->create_count());
5333 // Now verify that even in READ_WRITE mode, we forward the bad response to
5334 // the caller.
5335 transaction.request_headers = "Range: bytes = 60-69\r\n" EXTRA_HEADER;
5336 transaction.data = "rg: 60-"; // Less than expected.
5337 headers = kRangeGET_TransactionOK.response_headers;
5338 headers.append("Content-Range: bytes 60-69/80\n");
5339 transaction.response_headers = headers.c_str();
5341 RunTransactionTest(cache.http_cache(), transaction);
5343 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5344 EXPECT_EQ(1, cache.disk_cache()->open_count());
5345 EXPECT_EQ(1, cache.disk_cache()->create_count());
5347 RemoveMockTransaction(&transaction);
5350 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
5351 // This test hits a NOTREACHED so it is a release mode only test.
5352 TEST(HttpCache, RangeGET_OK_LoadOnlyFromCache) {
5353 MockHttpCache cache;
5354 AddMockTransaction(&kRangeGET_TransactionOK);
5356 // Write to the cache (40-49).
5357 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5358 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5359 EXPECT_EQ(0, cache.disk_cache()->open_count());
5360 EXPECT_EQ(1, cache.disk_cache()->create_count());
5362 // Force this transaction to read from the cache.
5363 MockTransaction transaction(kRangeGET_TransactionOK);
5364 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
5366 MockHttpRequest request(transaction);
5367 net::TestCompletionCallback callback;
5369 scoped_ptr<net::HttpTransaction> trans;
5370 int rv = cache.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, &trans);
5371 EXPECT_EQ(net::OK, rv);
5372 ASSERT_TRUE(trans.get());
5374 rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
5375 if (rv == net::ERR_IO_PENDING)
5376 rv = callback.WaitForResult();
5377 ASSERT_EQ(net::ERR_CACHE_MISS, rv);
5379 trans.reset();
5381 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5382 EXPECT_EQ(1, cache.disk_cache()->open_count());
5383 EXPECT_EQ(1, cache.disk_cache()->create_count());
5385 RemoveMockTransaction(&kRangeGET_TransactionOK);
5387 #endif
5389 // Tests the handling of the "truncation" flag.
5390 TEST(HttpCache, WriteResponseInfo_Truncated) {
5391 MockHttpCache cache;
5392 disk_cache::Entry* entry;
5393 ASSERT_TRUE(cache.CreateBackendEntry("http://www.google.com", &entry,
5394 NULL));
5396 std::string headers("HTTP/1.1 200 OK");
5397 headers = net::HttpUtil::AssembleRawHeaders(headers.data(), headers.size());
5398 net::HttpResponseInfo response;
5399 response.headers = new net::HttpResponseHeaders(headers);
5401 // Set the last argument for this to be an incomplete request.
5402 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true));
5403 bool truncated = false;
5404 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
5405 EXPECT_TRUE(truncated);
5407 // And now test the opposite case.
5408 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
5409 truncated = true;
5410 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
5411 EXPECT_FALSE(truncated);
5412 entry->Close();
5415 // Tests basic pickling/unpickling of HttpResponseInfo.
5416 TEST(HttpCache, PersistHttpResponseInfo) {
5417 // Set some fields (add more if needed.)
5418 net::HttpResponseInfo response1;
5419 response1.was_cached = false;
5420 response1.socket_address = net::HostPortPair("1.2.3.4", 80);
5421 response1.headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK");
5423 // Pickle.
5424 Pickle pickle;
5425 response1.Persist(&pickle, false, false);
5427 // Unpickle.
5428 net::HttpResponseInfo response2;
5429 bool response_truncated;
5430 EXPECT_TRUE(response2.InitFromPickle(pickle, &response_truncated));
5431 EXPECT_FALSE(response_truncated);
5433 // Verify fields.
5434 EXPECT_TRUE(response2.was_cached); // InitFromPickle sets this flag.
5435 EXPECT_EQ("1.2.3.4", response2.socket_address.host());
5436 EXPECT_EQ(80, response2.socket_address.port());
5437 EXPECT_EQ("HTTP/1.1 200 OK", response2.headers->GetStatusLine());
5440 // Tests that we delete an entry when the request is cancelled before starting
5441 // to read from the network.
5442 TEST(HttpCache, DoomOnDestruction) {
5443 MockHttpCache cache;
5445 MockHttpRequest request(kSimpleGET_Transaction);
5447 Context* c = new Context();
5448 int rv = cache.CreateTransaction(&c->trans);
5449 ASSERT_EQ(net::OK, rv);
5451 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
5452 if (rv == net::ERR_IO_PENDING)
5453 c->result = c->callback.WaitForResult();
5455 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5456 EXPECT_EQ(0, cache.disk_cache()->open_count());
5457 EXPECT_EQ(1, cache.disk_cache()->create_count());
5459 // Destroy the transaction. We only have the headers so we should delete this
5460 // entry.
5461 delete c;
5463 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
5465 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5466 EXPECT_EQ(0, cache.disk_cache()->open_count());
5467 EXPECT_EQ(2, cache.disk_cache()->create_count());
5470 // Tests that we delete an entry when the request is cancelled if the response
5471 // does not have content-length and strong validators.
5472 TEST(HttpCache, DoomOnDestruction2) {
5473 MockHttpCache cache;
5475 MockHttpRequest request(kSimpleGET_Transaction);
5477 Context* c = new Context();
5478 int rv = cache.CreateTransaction(&c->trans);
5479 ASSERT_EQ(net::OK, rv);
5481 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
5482 if (rv == net::ERR_IO_PENDING)
5483 rv = c->callback.WaitForResult();
5485 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5486 EXPECT_EQ(0, cache.disk_cache()->open_count());
5487 EXPECT_EQ(1, cache.disk_cache()->create_count());
5489 // Make sure that the entry has some data stored.
5490 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10));
5491 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
5492 if (rv == net::ERR_IO_PENDING)
5493 rv = c->callback.WaitForResult();
5494 EXPECT_EQ(buf->size(), rv);
5496 // Destroy the transaction.
5497 delete c;
5499 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
5501 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5502 EXPECT_EQ(0, cache.disk_cache()->open_count());
5503 EXPECT_EQ(2, cache.disk_cache()->create_count());
5506 // Tests that we delete an entry when the request is cancelled if the response
5507 // has an "Accept-Ranges: none" header.
5508 TEST(HttpCache, DoomOnDestruction3) {
5509 MockHttpCache cache;
5511 MockTransaction transaction(kSimpleGET_Transaction);
5512 transaction.response_headers =
5513 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
5514 "Content-Length: 22\n"
5515 "Accept-Ranges: none\n"
5516 "Etag: \"foopy\"\n";
5517 AddMockTransaction(&transaction);
5518 MockHttpRequest request(transaction);
5520 Context* c = new Context();
5521 int rv = cache.CreateTransaction(&c->trans);
5522 ASSERT_EQ(net::OK, rv);
5524 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
5525 if (rv == net::ERR_IO_PENDING)
5526 rv = c->callback.WaitForResult();
5528 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5529 EXPECT_EQ(0, cache.disk_cache()->open_count());
5530 EXPECT_EQ(1, cache.disk_cache()->create_count());
5532 // Make sure that the entry has some data stored.
5533 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10));
5534 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
5535 if (rv == net::ERR_IO_PENDING)
5536 rv = c->callback.WaitForResult();
5537 EXPECT_EQ(buf->size(), rv);
5539 // Destroy the transaction.
5540 delete c;
5542 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
5544 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5545 EXPECT_EQ(0, cache.disk_cache()->open_count());
5546 EXPECT_EQ(2, cache.disk_cache()->create_count());
5548 RemoveMockTransaction(&transaction);
5551 // Tests that we mark an entry as incomplete when the request is cancelled.
5552 TEST(HttpCache, SetTruncatedFlag) {
5553 MockHttpCache cache;
5555 MockTransaction transaction(kSimpleGET_Transaction);
5556 transaction.response_headers =
5557 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
5558 "Content-Length: 22\n"
5559 "Etag: \"foopy\"\n";
5560 AddMockTransaction(&transaction);
5561 MockHttpRequest request(transaction);
5563 scoped_ptr<Context> c(new Context());
5565 int rv = cache.CreateTransaction(&c->trans);
5566 ASSERT_EQ(net::OK, rv);
5568 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
5569 if (rv == net::ERR_IO_PENDING)
5570 rv = c->callback.WaitForResult();
5572 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5573 EXPECT_EQ(0, cache.disk_cache()->open_count());
5574 EXPECT_EQ(1, cache.disk_cache()->create_count());
5576 // Make sure that the entry has some data stored.
5577 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10));
5578 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
5579 if (rv == net::ERR_IO_PENDING)
5580 rv = c->callback.WaitForResult();
5581 EXPECT_EQ(buf->size(), rv);
5583 // We want to cancel the request when the transaction is busy.
5584 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
5585 EXPECT_EQ(net::ERR_IO_PENDING, rv);
5586 EXPECT_FALSE(c->callback.have_result());
5588 MockHttpCache::SetTestMode(TEST_MODE_SYNC_ALL);
5590 // Destroy the transaction.
5591 c->trans.reset();
5592 MockHttpCache::SetTestMode(0);
5595 // Make sure that we don't invoke the callback. We may have an issue if the
5596 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we
5597 // could end up with the transaction being deleted twice if we send any
5598 // notification from the transaction destructor (see http://crbug.com/31723).
5599 EXPECT_FALSE(c->callback.have_result());
5601 // Verify that the entry is marked as incomplete.
5602 disk_cache::Entry* entry;
5603 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry));
5604 net::HttpResponseInfo response;
5605 bool truncated = false;
5606 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
5607 EXPECT_TRUE(truncated);
5608 entry->Close();
5610 RemoveMockTransaction(&transaction);
5613 // Tests that we don't mark an entry as truncated when we read everything.
5614 TEST(HttpCache, DontSetTruncatedFlag) {
5615 MockHttpCache cache;
5617 MockTransaction transaction(kSimpleGET_Transaction);
5618 transaction.response_headers =
5619 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
5620 "Content-Length: 22\n"
5621 "Etag: \"foopy\"\n";
5622 AddMockTransaction(&transaction);
5623 MockHttpRequest request(transaction);
5625 scoped_ptr<Context> c(new Context());
5626 int rv = cache.CreateTransaction(&c->trans);
5627 ASSERT_EQ(net::OK, rv);
5629 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
5630 EXPECT_EQ(net::OK, c->callback.GetResult(rv));
5632 // Read everything.
5633 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(22));
5634 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
5635 EXPECT_EQ(buf->size(), c->callback.GetResult(rv));
5637 // Destroy the transaction.
5638 c->trans.reset();
5640 // Verify that the entry is not marked as truncated.
5641 disk_cache::Entry* entry;
5642 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry));
5643 net::HttpResponseInfo response;
5644 bool truncated = true;
5645 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
5646 EXPECT_FALSE(truncated);
5647 entry->Close();
5649 RemoveMockTransaction(&transaction);
5652 // Tests that we can continue with a request that was interrupted.
5653 TEST(HttpCache, GET_IncompleteResource) {
5654 MockHttpCache cache;
5655 AddMockTransaction(&kRangeGET_TransactionOK);
5657 std::string raw_headers("HTTP/1.1 200 OK\n"
5658 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
5659 "ETag: \"foo\"\n"
5660 "Accept-Ranges: bytes\n"
5661 "Content-Length: 80\n");
5662 CreateTruncatedEntry(raw_headers, &cache);
5664 // Now make a regular request.
5665 std::string headers;
5666 MockTransaction transaction(kRangeGET_TransactionOK);
5667 transaction.request_headers = EXTRA_HEADER;
5668 transaction.data = kFullRangeData;
5669 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5671 // We update the headers with the ones received while revalidating.
5672 std::string expected_headers(
5673 "HTTP/1.1 200 OK\n"
5674 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
5675 "Accept-Ranges: bytes\n"
5676 "ETag: \"foo\"\n"
5677 "Content-Length: 80\n");
5679 EXPECT_EQ(expected_headers, headers);
5680 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5681 EXPECT_EQ(1, cache.disk_cache()->open_count());
5682 EXPECT_EQ(1, cache.disk_cache()->create_count());
5684 // Verify that the disk entry was updated.
5685 disk_cache::Entry* entry;
5686 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
5687 EXPECT_EQ(80, entry->GetDataSize(1));
5688 bool truncated = true;
5689 net::HttpResponseInfo response;
5690 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
5691 EXPECT_FALSE(truncated);
5692 entry->Close();
5694 RemoveMockTransaction(&kRangeGET_TransactionOK);
5697 // Tests the handling of no-store when revalidating a truncated entry.
5698 TEST(HttpCache, GET_IncompleteResource_NoStore) {
5699 MockHttpCache cache;
5700 AddMockTransaction(&kRangeGET_TransactionOK);
5702 std::string raw_headers("HTTP/1.1 200 OK\n"
5703 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
5704 "ETag: \"foo\"\n"
5705 "Accept-Ranges: bytes\n"
5706 "Content-Length: 80\n");
5707 CreateTruncatedEntry(raw_headers, &cache);
5708 RemoveMockTransaction(&kRangeGET_TransactionOK);
5710 // Now make a regular request.
5711 MockTransaction transaction(kRangeGET_TransactionOK);
5712 transaction.request_headers = EXTRA_HEADER;
5713 std::string response_headers(transaction.response_headers);
5714 response_headers += ("Cache-Control: no-store\n");
5715 transaction.response_headers = response_headers.c_str();
5716 transaction.data = kFullRangeData;
5717 AddMockTransaction(&transaction);
5719 std::string headers;
5720 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5722 // We update the headers with the ones received while revalidating.
5723 std::string expected_headers(
5724 "HTTP/1.1 200 OK\n"
5725 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
5726 "Accept-Ranges: bytes\n"
5727 "Cache-Control: no-store\n"
5728 "ETag: \"foo\"\n"
5729 "Content-Length: 80\n");
5731 EXPECT_EQ(expected_headers, headers);
5732 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5733 EXPECT_EQ(1, cache.disk_cache()->open_count());
5734 EXPECT_EQ(1, cache.disk_cache()->create_count());
5736 // Verify that the disk entry was deleted.
5737 disk_cache::Entry* entry;
5738 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
5739 RemoveMockTransaction(&transaction);
5742 // Tests cancelling a request after the server sent no-store.
5743 TEST(HttpCache, GET_IncompleteResource_Cancel) {
5744 MockHttpCache cache;
5745 AddMockTransaction(&kRangeGET_TransactionOK);
5747 std::string raw_headers("HTTP/1.1 200 OK\n"
5748 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
5749 "ETag: \"foo\"\n"
5750 "Accept-Ranges: bytes\n"
5751 "Content-Length: 80\n");
5752 CreateTruncatedEntry(raw_headers, &cache);
5753 RemoveMockTransaction(&kRangeGET_TransactionOK);
5755 // Now make a regular request.
5756 MockTransaction transaction(kRangeGET_TransactionOK);
5757 transaction.request_headers = EXTRA_HEADER;
5758 std::string response_headers(transaction.response_headers);
5759 response_headers += ("Cache-Control: no-store\n");
5760 transaction.response_headers = response_headers.c_str();
5761 transaction.data = kFullRangeData;
5762 AddMockTransaction(&transaction);
5764 MockHttpRequest request(transaction);
5765 Context* c = new Context();
5767 int rv = cache.CreateTransaction(&c->trans);
5768 ASSERT_EQ(net::OK, rv);
5770 // Queue another request to this transaction. We have to start this request
5771 // before the first one gets the response from the server and dooms the entry,
5772 // otherwise it will just create a new entry without being queued to the first
5773 // request.
5774 Context* pending = new Context();
5775 ASSERT_EQ(net::OK, cache.CreateTransaction(&pending->trans));
5777 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
5778 EXPECT_EQ(net::ERR_IO_PENDING,
5779 pending->trans->Start(&request, pending->callback.callback(),
5780 net::BoundNetLog()));
5781 EXPECT_EQ(net::OK, c->callback.GetResult(rv));
5783 // Make sure that the entry has some data stored.
5784 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(5));
5785 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
5786 EXPECT_EQ(5, c->callback.GetResult(rv));
5788 // Cancel the requests.
5789 delete c;
5790 delete pending;
5792 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5793 EXPECT_EQ(1, cache.disk_cache()->open_count());
5794 EXPECT_EQ(2, cache.disk_cache()->create_count());
5796 base::MessageLoop::current()->RunUntilIdle();
5797 RemoveMockTransaction(&transaction);
5800 // Tests that we delete truncated entries if the server changes its mind midway.
5801 TEST(HttpCache, GET_IncompleteResource2) {
5802 MockHttpCache cache;
5803 AddMockTransaction(&kRangeGET_TransactionOK);
5805 // Content-length will be intentionally bad.
5806 std::string raw_headers("HTTP/1.1 200 OK\n"
5807 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
5808 "ETag: \"foo\"\n"
5809 "Accept-Ranges: bytes\n"
5810 "Content-Length: 50\n");
5811 CreateTruncatedEntry(raw_headers, &cache);
5813 // Now make a regular request. We expect the code to fail the validation and
5814 // retry the request without using byte ranges.
5815 std::string headers;
5816 MockTransaction transaction(kRangeGET_TransactionOK);
5817 transaction.request_headers = EXTRA_HEADER;
5818 transaction.data = "Not a range";
5819 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5821 // The server will return 200 instead of a byte range.
5822 std::string expected_headers(
5823 "HTTP/1.1 200 OK\n"
5824 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n");
5826 EXPECT_EQ(expected_headers, headers);
5827 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5828 EXPECT_EQ(1, cache.disk_cache()->open_count());
5829 EXPECT_EQ(1, cache.disk_cache()->create_count());
5831 // Verify that the disk entry was deleted.
5832 disk_cache::Entry* entry;
5833 ASSERT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
5834 RemoveMockTransaction(&kRangeGET_TransactionOK);
5837 // Tests that we always validate a truncated request.
5838 TEST(HttpCache, GET_IncompleteResource3) {
5839 MockHttpCache cache;
5840 AddMockTransaction(&kRangeGET_TransactionOK);
5842 // This should not require validation for 10 hours.
5843 std::string raw_headers("HTTP/1.1 200 OK\n"
5844 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
5845 "ETag: \"foo\"\n"
5846 "Cache-Control: max-age= 36000\n"
5847 "Accept-Ranges: bytes\n"
5848 "Content-Length: 80\n");
5849 CreateTruncatedEntry(raw_headers, &cache);
5851 // Now make a regular request.
5852 std::string headers;
5853 MockTransaction transaction(kRangeGET_TransactionOK);
5854 transaction.request_headers = EXTRA_HEADER;
5855 transaction.data = kFullRangeData;
5857 scoped_ptr<Context> c(new Context);
5858 int rv = cache.CreateTransaction(&c->trans);
5859 ASSERT_EQ(net::OK, rv);
5861 MockHttpRequest request(transaction);
5862 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
5863 EXPECT_EQ(net::OK, c->callback.GetResult(rv));
5865 // We should have checked with the server before finishing Start().
5866 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5867 EXPECT_EQ(1, cache.disk_cache()->open_count());
5868 EXPECT_EQ(1, cache.disk_cache()->create_count());
5870 RemoveMockTransaction(&kRangeGET_TransactionOK);
5873 // Tests that we handle 401s for truncated resources.
5874 TEST(HttpCache, GET_IncompleteResourceWithAuth) {
5875 MockHttpCache cache;
5876 AddMockTransaction(&kRangeGET_TransactionOK);
5878 std::string raw_headers("HTTP/1.1 200 OK\n"
5879 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
5880 "ETag: \"foo\"\n"
5881 "Accept-Ranges: bytes\n"
5882 "Content-Length: 80\n");
5883 CreateTruncatedEntry(raw_headers, &cache);
5885 // Now make a regular request.
5886 MockTransaction transaction(kRangeGET_TransactionOK);
5887 transaction.request_headers = "X-Require-Mock-Auth: dummy\r\n"
5888 EXTRA_HEADER;
5889 transaction.data = kFullRangeData;
5890 RangeTransactionServer handler;
5892 scoped_ptr<Context> c(new Context);
5893 int rv = cache.CreateTransaction(&c->trans);
5894 ASSERT_EQ(net::OK, rv);
5896 MockHttpRequest request(transaction);
5897 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
5898 EXPECT_EQ(net::OK, c->callback.GetResult(rv));
5900 const net::HttpResponseInfo* response = c->trans->GetResponseInfo();
5901 ASSERT_TRUE(response);
5902 ASSERT_EQ(401, response->headers->response_code());
5903 rv = c->trans->RestartWithAuth(net::AuthCredentials(),
5904 c->callback.callback());
5905 EXPECT_EQ(net::OK, c->callback.GetResult(rv));
5906 response = c->trans->GetResponseInfo();
5907 ASSERT_TRUE(response);
5908 ASSERT_EQ(200, response->headers->response_code());
5910 ReadAndVerifyTransaction(c->trans.get(), transaction);
5911 c.reset(); // The destructor could delete the entry.
5912 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5914 // Verify that the entry was not deleted.
5915 disk_cache::Entry* entry;
5916 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
5917 entry->Close();
5919 RemoveMockTransaction(&kRangeGET_TransactionOK);
5922 // Tests that we cache a 200 response to the validation request.
5923 TEST(HttpCache, GET_IncompleteResource4) {
5924 MockHttpCache cache;
5925 AddMockTransaction(&kRangeGET_TransactionOK);
5927 std::string raw_headers("HTTP/1.1 200 OK\n"
5928 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
5929 "ETag: \"foo\"\n"
5930 "Accept-Ranges: bytes\n"
5931 "Content-Length: 80\n");
5932 CreateTruncatedEntry(raw_headers, &cache);
5934 // Now make a regular request.
5935 std::string headers;
5936 MockTransaction transaction(kRangeGET_TransactionOK);
5937 transaction.request_headers = EXTRA_HEADER;
5938 transaction.data = "Not a range";
5939 RangeTransactionServer handler;
5940 handler.set_bad_200(true);
5941 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5943 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5944 EXPECT_EQ(1, cache.disk_cache()->open_count());
5945 EXPECT_EQ(1, cache.disk_cache()->create_count());
5947 // Verify that the disk entry was updated.
5948 disk_cache::Entry* entry;
5949 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
5950 EXPECT_EQ(11, entry->GetDataSize(1));
5951 bool truncated = true;
5952 net::HttpResponseInfo response;
5953 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
5954 EXPECT_FALSE(truncated);
5955 entry->Close();
5957 RemoveMockTransaction(&kRangeGET_TransactionOK);
5960 // Tests that when we cancel a request that was interrupted, we mark it again
5961 // as truncated.
5962 TEST(HttpCache, GET_CancelIncompleteResource) {
5963 MockHttpCache cache;
5964 AddMockTransaction(&kRangeGET_TransactionOK);
5966 std::string raw_headers("HTTP/1.1 200 OK\n"
5967 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
5968 "ETag: \"foo\"\n"
5969 "Accept-Ranges: bytes\n"
5970 "Content-Length: 80\n");
5971 CreateTruncatedEntry(raw_headers, &cache);
5973 // Now make a regular request.
5974 MockTransaction transaction(kRangeGET_TransactionOK);
5975 transaction.request_headers = EXTRA_HEADER;
5977 MockHttpRequest request(transaction);
5978 Context* c = new Context();
5979 int rv = cache.CreateTransaction(&c->trans);
5980 ASSERT_EQ(net::OK, rv);
5982 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
5983 EXPECT_EQ(net::OK, c->callback.GetResult(rv));
5985 // Read 20 bytes from the cache, and 10 from the net.
5986 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100));
5987 rv = c->trans->Read(buf.get(), 20, c->callback.callback());
5988 EXPECT_EQ(20, c->callback.GetResult(rv));
5989 rv = c->trans->Read(buf.get(), 10, c->callback.callback());
5990 EXPECT_EQ(10, c->callback.GetResult(rv));
5992 // At this point, we are already reading so canceling the request should leave
5993 // a truncated one.
5994 delete c;
5996 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5997 EXPECT_EQ(1, cache.disk_cache()->open_count());
5998 EXPECT_EQ(1, cache.disk_cache()->create_count());
6000 // Verify that the disk entry was updated: now we have 30 bytes.
6001 disk_cache::Entry* entry;
6002 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
6003 EXPECT_EQ(30, entry->GetDataSize(1));
6004 bool truncated = false;
6005 net::HttpResponseInfo response;
6006 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
6007 EXPECT_TRUE(truncated);
6008 entry->Close();
6009 RemoveMockTransaction(&kRangeGET_TransactionOK);
6012 // Tests that we can handle range requests when we have a truncated entry.
6013 TEST(HttpCache, RangeGET_IncompleteResource) {
6014 MockHttpCache cache;
6015 AddMockTransaction(&kRangeGET_TransactionOK);
6017 // Content-length will be intentionally bogus.
6018 std::string raw_headers("HTTP/1.1 200 OK\n"
6019 "Last-Modified: something\n"
6020 "ETag: \"foo\"\n"
6021 "Accept-Ranges: bytes\n"
6022 "Content-Length: 10\n");
6023 CreateTruncatedEntry(raw_headers, &cache);
6025 // Now make a range request.
6026 std::string headers;
6027 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
6028 &headers);
6030 Verify206Response(headers, 40, 49);
6031 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6032 EXPECT_EQ(1, cache.disk_cache()->open_count());
6033 EXPECT_EQ(2, cache.disk_cache()->create_count());
6035 RemoveMockTransaction(&kRangeGET_TransactionOK);
6038 TEST(HttpCache, SyncRead) {
6039 MockHttpCache cache;
6041 // This test ensures that a read that completes synchronously does not cause
6042 // any problems.
6044 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6045 transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START |
6046 TEST_MODE_SYNC_CACHE_READ |
6047 TEST_MODE_SYNC_CACHE_WRITE);
6049 MockHttpRequest r1(transaction),
6050 r2(transaction),
6051 r3(transaction);
6053 TestTransactionConsumer c1(net::DEFAULT_PRIORITY, cache.http_cache()),
6054 c2(net::DEFAULT_PRIORITY, cache.http_cache()),
6055 c3(net::DEFAULT_PRIORITY, cache.http_cache());
6057 c1.Start(&r1, net::BoundNetLog());
6059 r2.load_flags |= net::LOAD_ONLY_FROM_CACHE;
6060 c2.Start(&r2, net::BoundNetLog());
6062 r3.load_flags |= net::LOAD_ONLY_FROM_CACHE;
6063 c3.Start(&r3, net::BoundNetLog());
6065 base::MessageLoop::current()->Run();
6067 EXPECT_TRUE(c1.is_done());
6068 EXPECT_TRUE(c2.is_done());
6069 EXPECT_TRUE(c3.is_done());
6071 EXPECT_EQ(net::OK, c1.error());
6072 EXPECT_EQ(net::OK, c2.error());
6073 EXPECT_EQ(net::OK, c3.error());
6076 TEST(HttpCache, ValidationResultsIn200) {
6077 MockHttpCache cache;
6079 // This test ensures that a conditional request, which results in a 200
6080 // instead of a 304, properly truncates the existing response data.
6082 // write to the cache
6083 RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
6085 // force this transaction to validate the cache
6086 MockTransaction transaction(kETagGET_Transaction);
6087 transaction.load_flags |= net::LOAD_VALIDATE_CACHE;
6088 RunTransactionTest(cache.http_cache(), transaction);
6090 // read from the cache
6091 RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
6094 TEST(HttpCache, CachedRedirect) {
6095 MockHttpCache cache;
6097 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction);
6098 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently";
6099 kTestTransaction.response_headers = "Location: http://www.bar.com/\n";
6101 MockHttpRequest request(kTestTransaction);
6102 net::TestCompletionCallback callback;
6104 // Write to the cache.
6106 scoped_ptr<net::HttpTransaction> trans;
6107 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
6109 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
6110 if (rv == net::ERR_IO_PENDING)
6111 rv = callback.WaitForResult();
6112 ASSERT_EQ(net::OK, rv);
6114 const net::HttpResponseInfo* info = trans->GetResponseInfo();
6115 ASSERT_TRUE(info);
6117 EXPECT_EQ(info->headers->response_code(), 301);
6119 std::string location;
6120 info->headers->EnumerateHeader(NULL, "Location", &location);
6121 EXPECT_EQ(location, "http://www.bar.com/");
6123 // Mark the transaction as completed so it is cached.
6124 trans->DoneReading();
6126 // Destroy transaction when going out of scope. We have not actually
6127 // read the response body -- want to test that it is still getting cached.
6129 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6130 EXPECT_EQ(0, cache.disk_cache()->open_count());
6131 EXPECT_EQ(1, cache.disk_cache()->create_count());
6133 // Active entries in the cache are not retired synchronously. Make
6134 // sure the next run hits the MockHttpCache and open_count is
6135 // correct.
6136 base::MessageLoop::current()->RunUntilIdle();
6138 // Read from the cache.
6140 scoped_ptr<net::HttpTransaction> trans;
6141 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
6143 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
6144 if (rv == net::ERR_IO_PENDING)
6145 rv = callback.WaitForResult();
6146 ASSERT_EQ(net::OK, rv);
6148 const net::HttpResponseInfo* info = trans->GetResponseInfo();
6149 ASSERT_TRUE(info);
6151 EXPECT_EQ(info->headers->response_code(), 301);
6153 std::string location;
6154 info->headers->EnumerateHeader(NULL, "Location", &location);
6155 EXPECT_EQ(location, "http://www.bar.com/");
6157 // Mark the transaction as completed so it is cached.
6158 trans->DoneReading();
6160 // Destroy transaction when going out of scope. We have not actually
6161 // read the response body -- want to test that it is still getting cached.
6163 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6164 EXPECT_EQ(1, cache.disk_cache()->open_count());
6165 EXPECT_EQ(1, cache.disk_cache()->create_count());
6168 // Verify that no-cache resources are stored in cache, but are not fetched from
6169 // cache during normal loads.
6170 TEST(HttpCache, CacheControlNoCacheNormalLoad) {
6171 MockHttpCache cache;
6173 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6174 transaction.response_headers = "cache-control: no-cache\n";
6176 // Initial load.
6177 RunTransactionTest(cache.http_cache(), transaction);
6179 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6180 EXPECT_EQ(0, cache.disk_cache()->open_count());
6181 EXPECT_EQ(1, cache.disk_cache()->create_count());
6183 // Try loading again; it should result in a network fetch.
6184 RunTransactionTest(cache.http_cache(), transaction);
6186 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6187 EXPECT_EQ(1, cache.disk_cache()->open_count());
6188 EXPECT_EQ(1, cache.disk_cache()->create_count());
6190 disk_cache::Entry* entry;
6191 EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry));
6192 entry->Close();
6195 // Verify that no-cache resources are stored in cache and fetched from cache
6196 // when the LOAD_PREFERRING_CACHE flag is set.
6197 TEST(HttpCache, CacheControlNoCacheHistoryLoad) {
6198 MockHttpCache cache;
6200 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6201 transaction.response_headers = "cache-control: no-cache\n";
6203 // Initial load.
6204 RunTransactionTest(cache.http_cache(), transaction);
6206 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6207 EXPECT_EQ(0, cache.disk_cache()->open_count());
6208 EXPECT_EQ(1, cache.disk_cache()->create_count());
6210 // Try loading again with LOAD_PREFERRING_CACHE.
6211 transaction.load_flags = net::LOAD_PREFERRING_CACHE;
6212 RunTransactionTest(cache.http_cache(), transaction);
6214 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6215 EXPECT_EQ(1, cache.disk_cache()->open_count());
6216 EXPECT_EQ(1, cache.disk_cache()->create_count());
6218 disk_cache::Entry* entry;
6219 EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry));
6220 entry->Close();
6223 TEST(HttpCache, CacheControlNoStore) {
6224 MockHttpCache cache;
6226 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6227 transaction.response_headers = "cache-control: no-store\n";
6229 // initial load
6230 RunTransactionTest(cache.http_cache(), transaction);
6232 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6233 EXPECT_EQ(0, cache.disk_cache()->open_count());
6234 EXPECT_EQ(1, cache.disk_cache()->create_count());
6236 // try loading again; it should result in a network fetch
6237 RunTransactionTest(cache.http_cache(), transaction);
6239 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6240 EXPECT_EQ(0, cache.disk_cache()->open_count());
6241 EXPECT_EQ(2, cache.disk_cache()->create_count());
6243 disk_cache::Entry* entry;
6244 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry));
6247 TEST(HttpCache, CacheControlNoStore2) {
6248 // this test is similar to the above test, except that the initial response
6249 // is cachable, but when it is validated, no-store is received causing the
6250 // cached document to be deleted.
6251 MockHttpCache cache;
6253 ScopedMockTransaction transaction(kETagGET_Transaction);
6255 // initial load
6256 RunTransactionTest(cache.http_cache(), transaction);
6258 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6259 EXPECT_EQ(0, cache.disk_cache()->open_count());
6260 EXPECT_EQ(1, cache.disk_cache()->create_count());
6262 // try loading again; it should result in a network fetch
6263 transaction.load_flags = net::LOAD_VALIDATE_CACHE;
6264 transaction.response_headers = "cache-control: no-store\n";
6265 RunTransactionTest(cache.http_cache(), transaction);
6267 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6268 EXPECT_EQ(1, cache.disk_cache()->open_count());
6269 EXPECT_EQ(1, cache.disk_cache()->create_count());
6271 disk_cache::Entry* entry;
6272 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry));
6275 TEST(HttpCache, CacheControlNoStore3) {
6276 // this test is similar to the above test, except that the response is a 304
6277 // instead of a 200. this should never happen in practice, but it seems like
6278 // a good thing to verify that we still destroy the cache entry.
6279 MockHttpCache cache;
6281 ScopedMockTransaction transaction(kETagGET_Transaction);
6283 // initial load
6284 RunTransactionTest(cache.http_cache(), transaction);
6286 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6287 EXPECT_EQ(0, cache.disk_cache()->open_count());
6288 EXPECT_EQ(1, cache.disk_cache()->create_count());
6290 // try loading again; it should result in a network fetch
6291 transaction.load_flags = net::LOAD_VALIDATE_CACHE;
6292 transaction.response_headers = "cache-control: no-store\n";
6293 transaction.status = "HTTP/1.1 304 Not Modified";
6294 RunTransactionTest(cache.http_cache(), transaction);
6296 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6297 EXPECT_EQ(1, cache.disk_cache()->open_count());
6298 EXPECT_EQ(1, cache.disk_cache()->create_count());
6300 disk_cache::Entry* entry;
6301 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry));
6304 // Ensure that we don't cache requests served over bad HTTPS.
6305 TEST(HttpCache, SimpleGET_SSLError) {
6306 MockHttpCache cache;
6308 MockTransaction transaction = kSimpleGET_Transaction;
6309 transaction.cert_status = net::CERT_STATUS_REVOKED;
6310 ScopedMockTransaction scoped_transaction(transaction);
6312 // write to the cache
6313 RunTransactionTest(cache.http_cache(), transaction);
6315 // Test that it was not cached.
6316 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
6318 MockHttpRequest request(transaction);
6319 net::TestCompletionCallback callback;
6321 scoped_ptr<net::HttpTransaction> trans;
6322 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
6324 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
6325 if (rv == net::ERR_IO_PENDING)
6326 rv = callback.WaitForResult();
6327 ASSERT_EQ(net::ERR_CACHE_MISS, rv);
6330 // Ensure that we don't crash by if left-behind transactions.
6331 TEST(HttpCache, OutlivedTransactions) {
6332 MockHttpCache* cache = new MockHttpCache;
6334 scoped_ptr<net::HttpTransaction> trans;
6335 EXPECT_EQ(net::OK, cache->CreateTransaction(&trans));
6337 delete cache;
6338 trans.reset();
6341 // Test that the disabled mode works.
6342 TEST(HttpCache, CacheDisabledMode) {
6343 MockHttpCache cache;
6345 // write to the cache
6346 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
6348 // go into disabled mode
6349 cache.http_cache()->set_mode(net::HttpCache::DISABLE);
6351 // force this transaction to write to the cache again
6352 MockTransaction transaction(kSimpleGET_Transaction);
6354 RunTransactionTest(cache.http_cache(), transaction);
6356 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6357 EXPECT_EQ(0, cache.disk_cache()->open_count());
6358 EXPECT_EQ(1, cache.disk_cache()->create_count());
6361 // Other tests check that the response headers of the cached response
6362 // get updated on 304. Here we specifically check that the
6363 // HttpResponseHeaders::request_time and HttpResponseHeaders::response_time
6364 // fields also gets updated.
6365 // http://crbug.com/20594.
6366 TEST(HttpCache, UpdatesRequestResponseTimeOn304) {
6367 MockHttpCache cache;
6369 const char kUrl[] = "http://foobar";
6370 const char kData[] = "body";
6372 MockTransaction mock_network_response = { 0 };
6373 mock_network_response.url = kUrl;
6375 AddMockTransaction(&mock_network_response);
6377 // Request |kUrl|, causing |kNetResponse1| to be written to the cache.
6379 MockTransaction request = { 0 };
6380 request.url = kUrl;
6381 request.method = "GET";
6382 request.request_headers = "\r\n";
6383 request.data = kData;
6385 static const Response kNetResponse1 = {
6386 "HTTP/1.1 200 OK",
6387 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
6388 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
6389 kData
6392 kNetResponse1.AssignTo(&mock_network_response);
6394 RunTransactionTest(cache.http_cache(), request);
6396 // Request |kUrl| again, this time validating the cache and getting
6397 // a 304 back.
6399 request.load_flags = net::LOAD_VALIDATE_CACHE;
6401 static const Response kNetResponse2 = {
6402 "HTTP/1.1 304 Not Modified",
6403 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n",
6407 kNetResponse2.AssignTo(&mock_network_response);
6409 base::Time request_time = base::Time() + base::TimeDelta::FromHours(1234);
6410 base::Time response_time = base::Time() + base::TimeDelta::FromHours(1235);
6412 mock_network_response.request_time = request_time;
6413 mock_network_response.response_time = response_time;
6415 net::HttpResponseInfo response;
6416 RunTransactionTestWithResponseInfo(cache.http_cache(), request, &response);
6418 // The request and response times should have been updated.
6419 EXPECT_EQ(request_time.ToInternalValue(),
6420 response.request_time.ToInternalValue());
6421 EXPECT_EQ(response_time.ToInternalValue(),
6422 response.response_time.ToInternalValue());
6424 std::string headers;
6425 response.headers->GetNormalizedHeaders(&headers);
6427 EXPECT_EQ("HTTP/1.1 200 OK\n"
6428 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
6429 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
6430 headers);
6432 RemoveMockTransaction(&mock_network_response);
6435 // Tests that we can write metadata to an entry.
6436 TEST(HttpCache, WriteMetadata_OK) {
6437 MockHttpCache cache;
6439 // Write to the cache
6440 net::HttpResponseInfo response;
6441 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
6442 &response);
6443 EXPECT_TRUE(response.metadata.get() == NULL);
6445 // Trivial call.
6446 cache.http_cache()->WriteMetadata(GURL("foo"), net::DEFAULT_PRIORITY,
6447 Time::Now().ToDoubleT(), NULL, 0);
6449 // Write meta data to the same entry.
6450 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50));
6451 memset(buf->data(), 0, buf->size());
6452 base::strlcpy(buf->data(), "Hi there", buf->size());
6453 cache.http_cache()->WriteMetadata(
6454 GURL(kSimpleGET_Transaction.url), net::DEFAULT_PRIORITY,
6455 response.response_time.ToDoubleT(), buf.get(), buf->size());
6457 // Release the buffer before the operation takes place.
6458 buf = NULL;
6460 // Makes sure we finish pending operations.
6461 base::MessageLoop::current()->RunUntilIdle();
6463 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
6464 &response);
6465 ASSERT_TRUE(response.metadata.get() != NULL);
6466 EXPECT_EQ(50, response.metadata->size());
6467 EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there"));
6469 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6470 EXPECT_EQ(2, cache.disk_cache()->open_count());
6471 EXPECT_EQ(1, cache.disk_cache()->create_count());
6474 // Tests that we only write metadata to an entry if the time stamp matches.
6475 TEST(HttpCache, WriteMetadata_Fail) {
6476 MockHttpCache cache;
6478 // Write to the cache
6479 net::HttpResponseInfo response;
6480 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
6481 &response);
6482 EXPECT_TRUE(response.metadata.get() == NULL);
6484 // Attempt to write meta data to the same entry.
6485 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50));
6486 memset(buf->data(), 0, buf->size());
6487 base::strlcpy(buf->data(), "Hi there", buf->size());
6488 base::Time expected_time = response.response_time -
6489 base::TimeDelta::FromMilliseconds(20);
6490 cache.http_cache()->WriteMetadata(
6491 GURL(kSimpleGET_Transaction.url), net::DEFAULT_PRIORITY,
6492 expected_time.ToDoubleT(), buf.get(), buf->size());
6494 // Makes sure we finish pending operations.
6495 base::MessageLoop::current()->RunUntilIdle();
6497 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
6498 &response);
6499 EXPECT_TRUE(response.metadata.get() == NULL);
6501 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6502 EXPECT_EQ(2, cache.disk_cache()->open_count());
6503 EXPECT_EQ(1, cache.disk_cache()->create_count());
6506 // Tests that we can read metadata after validating the entry and with READ mode
6507 // transactions.
6508 TEST(HttpCache, ReadMetadata) {
6509 MockHttpCache cache;
6511 // Write to the cache
6512 net::HttpResponseInfo response;
6513 RunTransactionTestWithResponseInfo(cache.http_cache(),
6514 kTypicalGET_Transaction, &response);
6515 EXPECT_TRUE(response.metadata.get() == NULL);
6517 // Write meta data to the same entry.
6518 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50));
6519 memset(buf->data(), 0, buf->size());
6520 base::strlcpy(buf->data(), "Hi there", buf->size());
6521 cache.http_cache()->WriteMetadata(
6522 GURL(kTypicalGET_Transaction.url), net::DEFAULT_PRIORITY,
6523 response.response_time.ToDoubleT(), buf.get(), buf->size());
6525 // Makes sure we finish pending operations.
6526 base::MessageLoop::current()->RunUntilIdle();
6528 // Start with a READ mode transaction.
6529 MockTransaction trans1(kTypicalGET_Transaction);
6530 trans1.load_flags = net::LOAD_ONLY_FROM_CACHE;
6532 RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response);
6533 ASSERT_TRUE(response.metadata.get() != NULL);
6534 EXPECT_EQ(50, response.metadata->size());
6535 EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there"));
6537 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6538 EXPECT_EQ(2, cache.disk_cache()->open_count());
6539 EXPECT_EQ(1, cache.disk_cache()->create_count());
6540 base::MessageLoop::current()->RunUntilIdle();
6542 // Now make sure that the entry is re-validated with the server.
6543 trans1.load_flags = net::LOAD_VALIDATE_CACHE;
6544 trans1.status = "HTTP/1.1 304 Not Modified";
6545 AddMockTransaction(&trans1);
6547 response.metadata = NULL;
6548 RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response);
6549 EXPECT_TRUE(response.metadata.get() != NULL);
6551 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6552 EXPECT_EQ(3, cache.disk_cache()->open_count());
6553 EXPECT_EQ(1, cache.disk_cache()->create_count());
6554 base::MessageLoop::current()->RunUntilIdle();
6555 RemoveMockTransaction(&trans1);
6557 // Now return 200 when validating the entry so the metadata will be lost.
6558 MockTransaction trans2(kTypicalGET_Transaction);
6559 trans2.load_flags = net::LOAD_VALIDATE_CACHE;
6560 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response);
6561 EXPECT_TRUE(response.metadata.get() == NULL);
6563 EXPECT_EQ(3, cache.network_layer()->transaction_count());
6564 EXPECT_EQ(4, cache.disk_cache()->open_count());
6565 EXPECT_EQ(1, cache.disk_cache()->create_count());
6568 // Tests that we don't mark entries as truncated when a filter detects the end
6569 // of the stream.
6570 TEST(HttpCache, FilterCompletion) {
6571 MockHttpCache cache;
6572 net::TestCompletionCallback callback;
6575 scoped_ptr<net::HttpTransaction> trans;
6576 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
6578 MockHttpRequest request(kSimpleGET_Transaction);
6579 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
6580 EXPECT_EQ(net::OK, callback.GetResult(rv));
6582 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256));
6583 rv = trans->Read(buf.get(), 256, callback.callback());
6584 EXPECT_GT(callback.GetResult(rv), 0);
6586 // Now make sure that the entry is preserved.
6587 trans->DoneReading();
6590 // Make sure that the ActiveEntry is gone.
6591 base::MessageLoop::current()->RunUntilIdle();
6593 // Read from the cache.
6594 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
6596 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6597 EXPECT_EQ(1, cache.disk_cache()->open_count());
6598 EXPECT_EQ(1, cache.disk_cache()->create_count());
6601 // Tests that we don't mark entries as truncated and release the cache
6602 // entry when DoneReading() is called before any Read() calls, such as
6603 // for a redirect.
6604 TEST(HttpCache, DoneReading) {
6605 MockHttpCache cache;
6606 net::TestCompletionCallback callback;
6608 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6609 transaction.data = "";
6611 scoped_ptr<net::HttpTransaction> trans;
6612 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
6614 MockHttpRequest request(transaction);
6615 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
6616 EXPECT_EQ(net::OK, callback.GetResult(rv));
6618 trans->DoneReading();
6619 // Leave the transaction around.
6621 // Make sure that the ActiveEntry is gone.
6622 base::MessageLoop::current()->RunUntilIdle();
6624 // Read from the cache. This should not deadlock.
6625 RunTransactionTest(cache.http_cache(), transaction);
6627 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6628 EXPECT_EQ(1, cache.disk_cache()->open_count());
6629 EXPECT_EQ(1, cache.disk_cache()->create_count());
6632 // Tests that we stop caching when told.
6633 TEST(HttpCache, StopCachingDeletesEntry) {
6634 MockHttpCache cache;
6635 net::TestCompletionCallback callback;
6636 MockHttpRequest request(kSimpleGET_Transaction);
6639 scoped_ptr<net::HttpTransaction> trans;
6640 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
6642 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
6643 EXPECT_EQ(net::OK, callback.GetResult(rv));
6645 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256));
6646 rv = trans->Read(buf.get(), 10, callback.callback());
6647 EXPECT_EQ(10, callback.GetResult(rv));
6649 trans->StopCaching();
6651 // We should be able to keep reading.
6652 rv = trans->Read(buf.get(), 256, callback.callback());
6653 EXPECT_GT(callback.GetResult(rv), 0);
6654 rv = trans->Read(buf.get(), 256, callback.callback());
6655 EXPECT_EQ(0, callback.GetResult(rv));
6658 // Make sure that the ActiveEntry is gone.
6659 base::MessageLoop::current()->RunUntilIdle();
6661 // Verify that the entry is gone.
6662 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
6664 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6665 EXPECT_EQ(0, cache.disk_cache()->open_count());
6666 EXPECT_EQ(2, cache.disk_cache()->create_count());
6669 // Tests that we stop caching when told, even if DoneReading is called
6670 // after StopCaching.
6671 TEST(HttpCache, StopCachingThenDoneReadingDeletesEntry) {
6672 MockHttpCache cache;
6673 net::TestCompletionCallback callback;
6674 MockHttpRequest request(kSimpleGET_Transaction);
6677 scoped_ptr<net::HttpTransaction> trans;
6678 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
6680 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
6681 EXPECT_EQ(net::OK, callback.GetResult(rv));
6683 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256));
6684 rv = trans->Read(buf.get(), 10, callback.callback());
6685 EXPECT_EQ(10, callback.GetResult(rv));
6687 trans->StopCaching();
6689 // We should be able to keep reading.
6690 rv = trans->Read(buf.get(), 256, callback.callback());
6691 EXPECT_GT(callback.GetResult(rv), 0);
6692 rv = trans->Read(buf.get(), 256, callback.callback());
6693 EXPECT_EQ(0, callback.GetResult(rv));
6695 // We should be able to call DoneReading.
6696 trans->DoneReading();
6699 // Make sure that the ActiveEntry is gone.
6700 base::MessageLoop::current()->RunUntilIdle();
6702 // Verify that the entry is gone.
6703 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
6705 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6706 EXPECT_EQ(0, cache.disk_cache()->open_count());
6707 EXPECT_EQ(2, cache.disk_cache()->create_count());
6710 // Tests that we stop caching when told, when using auth.
6711 TEST(HttpCache, StopCachingWithAuthDeletesEntry) {
6712 MockHttpCache cache;
6713 net::TestCompletionCallback callback;
6714 MockTransaction mock_transaction(kSimpleGET_Transaction);
6715 mock_transaction.status = "HTTP/1.1 401 Unauthorized";
6716 AddMockTransaction(&mock_transaction);
6717 MockHttpRequest request(mock_transaction);
6720 scoped_ptr<net::HttpTransaction> trans;
6721 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
6723 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
6724 EXPECT_EQ(net::OK, callback.GetResult(rv));
6726 trans->StopCaching();
6728 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256));
6729 rv = trans->Read(buf.get(), 10, callback.callback());
6730 EXPECT_EQ(callback.GetResult(rv), 10);
6732 RemoveMockTransaction(&mock_transaction);
6734 // Make sure that the ActiveEntry is gone.
6735 base::MessageLoop::current()->RunUntilIdle();
6737 // Verify that the entry is gone.
6738 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
6740 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6741 EXPECT_EQ(0, cache.disk_cache()->open_count());
6742 EXPECT_EQ(2, cache.disk_cache()->create_count());
6745 // Tests that when we are told to stop caching we don't throw away valid data.
6746 TEST(HttpCache, StopCachingSavesEntry) {
6747 MockHttpCache cache;
6748 net::TestCompletionCallback callback;
6749 MockHttpRequest request(kSimpleGET_Transaction);
6752 scoped_ptr<net::HttpTransaction> trans;
6753 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
6755 // Force a response that can be resumed.
6756 MockTransaction mock_transaction(kSimpleGET_Transaction);
6757 AddMockTransaction(&mock_transaction);
6758 mock_transaction.response_headers = "Cache-Control: max-age=10000\n"
6759 "Content-Length: 42\n"
6760 "Etag: \"foo\"\n";
6762 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
6763 EXPECT_EQ(net::OK, callback.GetResult(rv));
6765 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256));
6766 rv = trans->Read(buf.get(), 10, callback.callback());
6767 EXPECT_EQ(callback.GetResult(rv), 10);
6769 trans->StopCaching();
6771 // We should be able to keep reading.
6772 rv = trans->Read(buf.get(), 256, callback.callback());
6773 EXPECT_GT(callback.GetResult(rv), 0);
6774 rv = trans->Read(buf.get(), 256, callback.callback());
6775 EXPECT_EQ(callback.GetResult(rv), 0);
6777 RemoveMockTransaction(&mock_transaction);
6780 // Verify that the entry is marked as incomplete.
6781 disk_cache::Entry* entry;
6782 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry));
6783 net::HttpResponseInfo response;
6784 bool truncated = false;
6785 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
6786 EXPECT_TRUE(truncated);
6787 entry->Close();
6790 // Tests that we handle truncated enries when StopCaching is called.
6791 TEST(HttpCache, StopCachingTruncatedEntry) {
6792 MockHttpCache cache;
6793 net::TestCompletionCallback callback;
6794 MockHttpRequest request(kRangeGET_TransactionOK);
6795 request.extra_headers.Clear();
6796 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE);
6797 AddMockTransaction(&kRangeGET_TransactionOK);
6799 std::string raw_headers("HTTP/1.1 200 OK\n"
6800 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
6801 "ETag: \"foo\"\n"
6802 "Accept-Ranges: bytes\n"
6803 "Content-Length: 80\n");
6804 CreateTruncatedEntry(raw_headers, &cache);
6807 // Now make a regular request.
6808 scoped_ptr<net::HttpTransaction> trans;
6809 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans));
6811 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
6812 EXPECT_EQ(net::OK, callback.GetResult(rv));
6814 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256));
6815 rv = trans->Read(buf.get(), 10, callback.callback());
6816 EXPECT_EQ(callback.GetResult(rv), 10);
6818 // This is actually going to do nothing.
6819 trans->StopCaching();
6821 // We should be able to keep reading.
6822 rv = trans->Read(buf.get(), 256, callback.callback());
6823 EXPECT_GT(callback.GetResult(rv), 0);
6824 rv = trans->Read(buf.get(), 256, callback.callback());
6825 EXPECT_GT(callback.GetResult(rv), 0);
6826 rv = trans->Read(buf.get(), 256, callback.callback());
6827 EXPECT_EQ(callback.GetResult(rv), 0);
6830 // Verify that the disk entry was updated.
6831 disk_cache::Entry* entry;
6832 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
6833 EXPECT_EQ(80, entry->GetDataSize(1));
6834 bool truncated = true;
6835 net::HttpResponseInfo response;
6836 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
6837 EXPECT_FALSE(truncated);
6838 entry->Close();
6840 RemoveMockTransaction(&kRangeGET_TransactionOK);
6843 // Tests that we detect truncated resources from the net when there is
6844 // a Content-Length header.
6845 TEST(HttpCache, TruncatedByContentLength) {
6846 MockHttpCache cache;
6847 net::TestCompletionCallback callback;
6849 MockTransaction transaction(kSimpleGET_Transaction);
6850 AddMockTransaction(&transaction);
6851 transaction.response_headers = "Cache-Control: max-age=10000\n"
6852 "Content-Length: 100\n";
6853 RunTransactionTest(cache.http_cache(), transaction);
6854 RemoveMockTransaction(&transaction);
6856 // Read from the cache.
6857 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
6859 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6860 EXPECT_EQ(0, cache.disk_cache()->open_count());
6861 EXPECT_EQ(2, cache.disk_cache()->create_count());
6864 // Tests that we actually flag entries as truncated when we detect an error
6865 // from the net.
6866 TEST(HttpCache, TruncatedByContentLength2) {
6867 MockHttpCache cache;
6868 net::TestCompletionCallback callback;
6870 MockTransaction transaction(kSimpleGET_Transaction);
6871 AddMockTransaction(&transaction);
6872 transaction.response_headers = "Cache-Control: max-age=10000\n"
6873 "Content-Length: 100\n"
6874 "Etag: \"foo\"\n";
6875 RunTransactionTest(cache.http_cache(), transaction);
6876 RemoveMockTransaction(&transaction);
6878 // Verify that the entry is marked as incomplete.
6879 disk_cache::Entry* entry;
6880 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry));
6881 net::HttpResponseInfo response;
6882 bool truncated = false;
6883 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
6884 EXPECT_TRUE(truncated);
6885 entry->Close();
6888 // Make sure that calling SetPriority on a cache transaction passes on
6889 // its priority updates to its underlying network transaction.
6890 TEST(HttpCache, SetPriority) {
6891 MockHttpCache cache;
6893 scoped_ptr<net::HttpTransaction> trans;
6894 ASSERT_EQ(net::OK, cache.http_cache()->CreateTransaction(net::IDLE, &trans));
6896 // Shouldn't crash, but doesn't do anything either.
6897 trans->SetPriority(net::LOW);
6899 EXPECT_FALSE(cache.network_layer()->last_transaction());
6900 EXPECT_EQ(net::DEFAULT_PRIORITY,
6901 cache.network_layer()->last_create_transaction_priority());
6903 net::HttpRequestInfo info;
6904 info.url = GURL(kSimpleGET_Transaction.url);
6905 net::TestCompletionCallback callback;
6906 EXPECT_EQ(net::ERR_IO_PENDING,
6907 trans->Start(&info, callback.callback(), net::BoundNetLog()));
6909 EXPECT_TRUE(cache.network_layer()->last_transaction());
6910 if (cache.network_layer()->last_transaction()) {
6911 EXPECT_EQ(net::LOW,
6912 cache.network_layer()->last_create_transaction_priority());
6913 EXPECT_EQ(net::LOW,
6914 cache.network_layer()->last_transaction()->priority());
6917 trans->SetPriority(net::HIGHEST);
6919 if (cache.network_layer()->last_transaction()) {
6920 EXPECT_EQ(net::LOW,
6921 cache.network_layer()->last_create_transaction_priority());
6922 EXPECT_EQ(net::HIGHEST,
6923 cache.network_layer()->last_transaction()->priority());
6926 EXPECT_EQ(net::OK, callback.WaitForResult());
6929 // Make sure that calling SetWebSocketHandshakeStreamCreateHelper on a cache
6930 // transaction passes on its argument to the underlying network transaction.
6931 TEST(HttpCache, SetWebSocketHandshakeStreamCreateHelper) {
6932 MockHttpCache cache;
6934 FakeWebSocketHandshakeStreamCreateHelper create_helper;
6935 scoped_ptr<net::HttpTransaction> trans;
6936 ASSERT_EQ(net::OK, cache.http_cache()->CreateTransaction(net::IDLE, &trans));
6938 EXPECT_FALSE(cache.network_layer()->last_transaction());
6940 net::HttpRequestInfo info;
6941 info.url = GURL(kSimpleGET_Transaction.url);
6942 net::TestCompletionCallback callback;
6943 EXPECT_EQ(net::ERR_IO_PENDING,
6944 trans->Start(&info, callback.callback(), net::BoundNetLog()));
6946 ASSERT_TRUE(cache.network_layer()->last_transaction());
6947 EXPECT_FALSE(cache.network_layer()->last_transaction()->
6948 websocket_handshake_stream_create_helper());
6949 trans->SetWebSocketHandshakeStreamCreateHelper(&create_helper);
6950 EXPECT_EQ(&create_helper,
6951 cache.network_layer()->last_transaction()->
6952 websocket_handshake_stream_create_helper());
6953 EXPECT_EQ(net::OK, callback.WaitForResult());
6956 // Make sure that a cache transaction passes on its priority to
6957 // newly-created network transactions.
6958 TEST(HttpCache, SetPriorityNewTransaction) {
6959 MockHttpCache cache;
6960 AddMockTransaction(&kRangeGET_TransactionOK);
6962 std::string raw_headers("HTTP/1.1 200 OK\n"
6963 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
6964 "ETag: \"foo\"\n"
6965 "Accept-Ranges: bytes\n"
6966 "Content-Length: 80\n");
6967 CreateTruncatedEntry(raw_headers, &cache);
6969 // Now make a regular request.
6970 std::string headers;
6971 MockTransaction transaction(kRangeGET_TransactionOK);
6972 transaction.request_headers = EXTRA_HEADER;
6973 transaction.data = kFullRangeData;
6975 scoped_ptr<net::HttpTransaction> trans;
6976 ASSERT_EQ(net::OK,
6977 cache.http_cache()->CreateTransaction(net::MEDIUM, &trans));
6978 EXPECT_EQ(net::DEFAULT_PRIORITY,
6979 cache.network_layer()->last_create_transaction_priority());
6981 MockHttpRequest info(transaction);
6982 net::TestCompletionCallback callback;
6983 EXPECT_EQ(net::ERR_IO_PENDING,
6984 trans->Start(&info, callback.callback(), net::BoundNetLog()));
6985 EXPECT_EQ(net::OK, callback.WaitForResult());
6987 EXPECT_EQ(net::MEDIUM,
6988 cache.network_layer()->last_create_transaction_priority());
6990 trans->SetPriority(net::HIGHEST);
6991 // Should trigger a new network transaction and pick up the new
6992 // priority.
6993 ReadAndVerifyTransaction(trans.get(), transaction);
6995 EXPECT_EQ(net::HIGHEST,
6996 cache.network_layer()->last_create_transaction_priority());
6998 RemoveMockTransaction(&kRangeGET_TransactionOK);
7001 int64 RunTransactionAndGetReceivedBytes(
7002 MockHttpCache& cache,
7003 const MockTransaction& trans_info) {
7004 int64 received_bytes = -1;
7005 RunTransactionTestBase(cache.http_cache(), trans_info,
7006 MockHttpRequest(trans_info), NULL, net::BoundNetLog(),
7007 NULL, &received_bytes);
7008 return received_bytes;
7011 int64 TransactionSize(const MockTransaction& transaction) {
7012 return strlen(transaction.status) + strlen(transaction.response_headers) +
7013 strlen(transaction.data);
7016 TEST(HttpCache, ReceivedBytesCacheMissAndThenHit) {
7017 MockHttpCache cache;
7019 MockTransaction transaction(kSimpleGET_Transaction);
7020 int64 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction);
7021 EXPECT_EQ(TransactionSize(transaction), received_bytes);
7023 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction);
7024 EXPECT_EQ(0, received_bytes);
7027 TEST(HttpCache, ReceivedBytesConditionalRequest304) {
7028 MockHttpCache cache;
7030 ScopedMockTransaction transaction(kETagGET_Transaction);
7031 int64 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction);
7032 EXPECT_EQ(TransactionSize(transaction), received_bytes);
7034 transaction.load_flags = net::LOAD_VALIDATE_CACHE;
7035 transaction.handler = ETagGet_ConditionalRequest_Handler;
7036 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction);
7037 EXPECT_EQ(TransactionSize(transaction), received_bytes);
7040 TEST(HttpCache, ReceivedBytesConditionalRequest200) {
7041 MockHttpCache cache;
7043 MockTransaction transaction(kTypicalGET_Transaction);
7044 transaction.request_headers = "Foo: bar\r\n";
7045 transaction.response_headers =
7046 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
7047 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
7048 "Etag: \"foopy\"\n"
7049 "Cache-Control: max-age=0\n"
7050 "Vary: Foo\n";
7051 AddMockTransaction(&transaction);
7052 int64 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction);
7053 EXPECT_EQ(TransactionSize(transaction), received_bytes);
7055 RevalidationServer server;
7056 transaction.handler = server.Handler;
7057 transaction.request_headers = "Foo: none\r\n";
7058 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction);
7059 EXPECT_EQ(TransactionSize(transaction), received_bytes);
7061 RemoveMockTransaction(&transaction);
7064 TEST(HttpCache, ReceivedBytesRange) {
7065 MockHttpCache cache;
7066 AddMockTransaction(&kRangeGET_TransactionOK);
7067 MockTransaction transaction(kRangeGET_TransactionOK);
7069 // Read bytes 40-49 from the network.
7070 int64 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction);
7071 int64 range_response_size = TransactionSize(transaction);
7072 EXPECT_EQ(range_response_size, received_bytes);
7074 // Read bytes 40-49 from the cache.
7075 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction);
7076 EXPECT_EQ(0, received_bytes);
7077 base::MessageLoop::current()->RunUntilIdle();
7079 // Read bytes 30-39 from the network.
7080 transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER;
7081 transaction.data = "rg: 30-39 ";
7082 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction);
7083 EXPECT_EQ(range_response_size, received_bytes);
7084 base::MessageLoop::current()->RunUntilIdle();
7086 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache.
7087 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER;
7088 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 ";
7089 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction);
7090 EXPECT_EQ(range_response_size * 2, received_bytes);
7092 RemoveMockTransaction(&kRangeGET_TransactionOK);
7095 class HttpCachePrefetchValidationTest : public ::testing::Test {
7096 protected:
7097 static const int kMaxAgeSecs = 100;
7098 static const int kRequireValidationSecs = kMaxAgeSecs + 1;
7100 HttpCachePrefetchValidationTest() : transaction_(kSimpleGET_Transaction) {
7101 DCHECK_LT(kMaxAgeSecs, prefetch_reuse_mins() * net::kNumSecondsPerMinute);
7103 clock_ = new base::SimpleTestClock();
7104 cache_.http_cache()->SetClockForTesting(make_scoped_ptr(clock_));
7105 cache_.network_layer()->SetClock(clock_);
7107 transaction_.response_headers = "Cache-Control: max-age=100\n";
7110 bool TransactionRequiredNetwork(int load_flags) {
7111 int pre_transaction_count = transaction_count();
7112 transaction_.load_flags = load_flags;
7113 RunTransactionTest(cache_.http_cache(), transaction_);
7114 return pre_transaction_count != transaction_count();
7117 void AdvanceTime(int seconds) {
7118 clock_->Advance(base::TimeDelta::FromSeconds(seconds));
7121 int prefetch_reuse_mins() { return net::HttpCache::kPrefetchReuseMins; }
7123 // How many times this test has sent requests to the (fake) origin
7124 // server. Every test case needs to make at least one request to initialise
7125 // the cache.
7126 int transaction_count() {
7127 return cache_.network_layer()->transaction_count();
7130 MockHttpCache cache_;
7131 ScopedMockTransaction transaction_;
7132 std::string response_headers_;
7133 base::SimpleTestClock* clock_;
7136 TEST_F(HttpCachePrefetchValidationTest, SkipValidationShortlyAfterPrefetch) {
7137 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
7138 AdvanceTime(kRequireValidationSecs);
7139 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL));
7142 TEST_F(HttpCachePrefetchValidationTest, ValidateLongAfterPrefetch) {
7143 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
7144 AdvanceTime(prefetch_reuse_mins() * net::kNumSecondsPerMinute);
7145 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL));
7148 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceOnly) {
7149 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
7150 AdvanceTime(kRequireValidationSecs);
7151 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL));
7152 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL));
7155 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceReadOnly) {
7156 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
7157 AdvanceTime(kRequireValidationSecs);
7158 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_ONLY_FROM_CACHE));
7159 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL));
7162 TEST_F(HttpCachePrefetchValidationTest, BypassCacheOverwritesPrefetch) {
7163 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
7164 AdvanceTime(kRequireValidationSecs);
7165 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_BYPASS_CACHE));
7166 AdvanceTime(kRequireValidationSecs);
7167 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL));
7170 TEST_F(HttpCachePrefetchValidationTest,
7171 SkipValidationOnExistingEntryThatNeedsValidation) {
7172 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL));
7173 AdvanceTime(kRequireValidationSecs);
7174 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
7175 AdvanceTime(kRequireValidationSecs);
7176 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL));
7177 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL));
7180 TEST_F(HttpCachePrefetchValidationTest,
7181 SkipValidationOnExistingEntryThatDoesNotNeedValidation) {
7182 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL));
7183 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
7184 AdvanceTime(kRequireValidationSecs);
7185 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL));
7186 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL));
7189 TEST_F(HttpCachePrefetchValidationTest, PrefetchMultipleTimes) {
7190 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
7191 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
7192 AdvanceTime(kRequireValidationSecs);
7193 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL));
7196 TEST_F(HttpCachePrefetchValidationTest, ValidateOnDelayedSecondPrefetch) {
7197 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
7198 AdvanceTime(kRequireValidationSecs);
7199 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
7200 AdvanceTime(kRequireValidationSecs);
7201 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL));
7204 // Framework for tests of stale-while-revalidate related functionality. With
7205 // the default settings (age=3601,stale-while-revalidate=7200,max-age=3600) it
7206 // will trigger the stale-while-revalidate asynchronous revalidation. Setting
7207 // |age_| to < 3600 will prevent any revalidation, and |age_| > 10800 will cause
7208 // synchronous revalidation.
7209 class HttpCacheStaleWhileRevalidateTest : public ::testing::Test {
7210 protected:
7211 HttpCacheStaleWhileRevalidateTest()
7212 : transaction_(kSimpleGET_Transaction),
7213 age_(3601),
7214 stale_while_revalidate_(7200),
7215 validator_("Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT") {
7216 cache_.http_cache()->set_use_stale_while_revalidate_for_testing(true);
7219 // RunTransactionTest() with the arguments from this fixture.
7220 void RunFixtureTransactionTest() {
7221 std::string response_headers = base::StringPrintf(
7222 "%s\n"
7223 "Age: %d\n"
7224 "Cache-Control: max-age=3600,stale-while-revalidate=%d\n",
7225 validator_.c_str(),
7226 age_,
7227 stale_while_revalidate_);
7228 transaction_.response_headers = response_headers.c_str();
7229 RunTransactionTest(cache_.http_cache(), transaction_);
7230 transaction_.response_headers = "";
7233 // How many times this test has sent requests to the (fake) origin
7234 // server. Every test case needs to make at least one request to initialise
7235 // the cache.
7236 int transaction_count() {
7237 return cache_.network_layer()->transaction_count();
7240 // How many times an existing cache entry was opened during the test case.
7241 int open_count() { return cache_.disk_cache()->open_count(); }
7243 MockHttpCache cache_;
7244 ScopedMockTransaction transaction_;
7245 int age_;
7246 int stale_while_revalidate_;
7247 std::string validator_;
7250 static void CheckResourceFreshnessHeader(const net::HttpRequestInfo* request,
7251 std::string* response_status,
7252 std::string* response_headers,
7253 std::string* response_data) {
7254 std::string value;
7255 EXPECT_TRUE(request->extra_headers.GetHeader("Resource-Freshness", &value));
7256 EXPECT_EQ("max-age=3600,stale-while-revalidate=7200,age=10801", value);
7259 // Verify that the Resource-Freshness header is sent on a revalidation if the
7260 // stale-while-revalidate directive was on the response.
7261 TEST_F(HttpCacheStaleWhileRevalidateTest, ResourceFreshnessHeaderSent) {
7262 age_ = 10801; // Outside the stale-while-revalidate window.
7264 // Write to the cache.
7265 RunFixtureTransactionTest();
7267 EXPECT_EQ(1, transaction_count());
7269 // Send the request again and check that Resource-Freshness header is added.
7270 transaction_.handler = CheckResourceFreshnessHeader;
7272 RunFixtureTransactionTest();
7274 EXPECT_EQ(2, transaction_count());
7277 static void CheckResourceFreshnessAbsent(const net::HttpRequestInfo* request,
7278 std::string* response_status,
7279 std::string* response_headers,
7280 std::string* response_data) {
7281 EXPECT_FALSE(request->extra_headers.HasHeader("Resource-Freshness"));
7284 // Verify that the Resource-Freshness header is not sent when
7285 // stale-while-revalidate is 0.
7286 TEST_F(HttpCacheStaleWhileRevalidateTest, ResourceFreshnessHeaderNotSent) {
7287 age_ = 10801;
7288 stale_while_revalidate_ = 0;
7290 // Write to the cache.
7291 RunFixtureTransactionTest();
7293 EXPECT_EQ(1, transaction_count());
7295 // Send the request again and check that Resource-Freshness header is absent.
7296 transaction_.handler = CheckResourceFreshnessAbsent;
7298 RunFixtureTransactionTest();
7300 EXPECT_EQ(2, transaction_count());
7303 // Verify that when stale-while-revalidate applies the response is read from
7304 // cache.
7305 TEST_F(HttpCacheStaleWhileRevalidateTest, ReadFromCache) {
7306 // Write to the cache.
7307 RunFixtureTransactionTest();
7309 EXPECT_EQ(0, open_count());
7310 EXPECT_EQ(1, transaction_count());
7312 // Read back from the cache.
7313 RunFixtureTransactionTest();
7315 EXPECT_EQ(1, open_count());
7316 EXPECT_EQ(1, transaction_count());
7319 // Verify that when stale-while-revalidate applies an asynchronous request is
7320 // sent.
7321 TEST_F(HttpCacheStaleWhileRevalidateTest, AsyncRequestSent) {
7322 // Write to the cache.
7323 RunFixtureTransactionTest();
7325 EXPECT_EQ(1, transaction_count());
7327 // Read back from the cache.
7328 RunFixtureTransactionTest();
7330 EXPECT_EQ(1, transaction_count());
7332 // Let the async request execute.
7333 base::RunLoop().RunUntilIdle();
7334 EXPECT_EQ(2, transaction_count());
7337 // Verify that tearing down the HttpCache with an async revalidation in progress
7338 // does not break anything (this test is most likely to find problems when run
7339 // with a memory checker such as AddressSanitizer).
7340 TEST_F(HttpCacheStaleWhileRevalidateTest, AsyncTearDown) {
7341 // Write to the cache.
7342 RunFixtureTransactionTest();
7344 // Read back from the cache.
7345 RunFixtureTransactionTest();
7348 static void CheckIfModifiedSinceHeader(const net::HttpRequestInfo* request,
7349 std::string* response_status,
7350 std::string* response_headers,
7351 std::string* response_data) {
7352 std::string value;
7353 EXPECT_TRUE(request->extra_headers.GetHeader("If-Modified-Since", &value));
7354 EXPECT_EQ("Sat, 18 Apr 2007 01:10:43 GMT", value);
7357 // Verify that the async revalidation contains an If-Modified-Since header.
7358 TEST_F(HttpCacheStaleWhileRevalidateTest, AsyncRequestIfModifiedSince) {
7359 // Write to the cache.
7360 RunFixtureTransactionTest();
7362 transaction_.handler = CheckIfModifiedSinceHeader;
7364 // Read back from the cache.
7365 RunFixtureTransactionTest();
7368 static void CheckIfNoneMatchHeader(const net::HttpRequestInfo* request,
7369 std::string* response_status,
7370 std::string* response_headers,
7371 std::string* response_data) {
7372 std::string value;
7373 EXPECT_TRUE(request->extra_headers.GetHeader("If-None-Match", &value));
7374 EXPECT_EQ("\"40a1-1320-4f6adefa22a40\"", value);
7377 // If the response had ETag rather than Last-Modified, then that is used to
7378 // conditionalise the response.
7379 TEST_F(HttpCacheStaleWhileRevalidateTest, AsyncRequestIfNoneMatch) {
7380 validator_ = "Etag: \"40a1-1320-4f6adefa22a40\"";
7382 // Write to the cache.
7383 RunFixtureTransactionTest();
7385 transaction_.handler = CheckIfNoneMatchHeader;
7387 // Read back from the cache.
7388 RunFixtureTransactionTest();
7391 static void CheckResourceFreshnessHeaderPresent(
7392 const net::HttpRequestInfo* request,
7393 std::string* response_status,
7394 std::string* response_headers,
7395 std::string* response_data) {
7396 EXPECT_TRUE(request->extra_headers.HasHeader("Resource-Freshness"));
7399 TEST_F(HttpCacheStaleWhileRevalidateTest, AsyncRequestHasResourceFreshness) {
7400 // Write to the cache.
7401 RunFixtureTransactionTest();
7403 transaction_.handler = CheckResourceFreshnessHeaderPresent;
7405 // Read back from the cache.
7406 RunFixtureTransactionTest();
7409 // Verify that when age > max-age + stale-while-revalidate stale results are
7410 // not returned.
7411 TEST_F(HttpCacheStaleWhileRevalidateTest, NotAppliedIfTooStale) {
7412 age_ = 10801;
7414 // Write to the cache.
7415 RunFixtureTransactionTest();
7417 EXPECT_EQ(0, open_count());
7418 EXPECT_EQ(1, transaction_count());
7420 // Reading back reads from the network.
7421 RunFixtureTransactionTest();
7423 EXPECT_EQ(1, open_count());
7424 EXPECT_EQ(2, transaction_count());
7427 // HEAD requests should be able to take advantage of stale-while-revalidate.
7428 TEST_F(HttpCacheStaleWhileRevalidateTest, WorksForHeadMethod) {
7429 // Write to the cache. This has to be a GET request; HEAD requests don't
7430 // create new cache entries.
7431 RunFixtureTransactionTest();
7433 EXPECT_EQ(0, open_count());
7434 EXPECT_EQ(1, transaction_count());
7436 // Read back from the cache, and trigger an asynchronous HEAD request.
7437 transaction_.method = "HEAD";
7438 transaction_.data = "";
7440 RunFixtureTransactionTest();
7442 EXPECT_EQ(1, open_count());
7443 EXPECT_EQ(1, transaction_count());
7445 // Let the network request proceed.
7446 base::RunLoop().RunUntilIdle();
7448 EXPECT_EQ(2, transaction_count());
7451 // POST requests should not use stale-while-revalidate.
7452 TEST_F(HttpCacheStaleWhileRevalidateTest, NotAppliedToPost) {
7453 transaction_ = ScopedMockTransaction(kSimplePOST_Transaction);
7455 // Write to the cache.
7456 RunFixtureTransactionTest();
7458 EXPECT_EQ(0, open_count());
7459 EXPECT_EQ(1, transaction_count());
7461 // Reading back reads from the network.
7462 RunFixtureTransactionTest();
7464 EXPECT_EQ(0, open_count());
7465 EXPECT_EQ(2, transaction_count());
7468 static void CheckUrlMatches(const net::HttpRequestInfo* request,
7469 std::string* response_status,
7470 std::string* response_headers,
7471 std::string* response_data) {
7472 EXPECT_EQ("http://www.google.com/", request->url.spec());
7475 // Async revalidation is issued to the original URL.
7476 TEST_F(HttpCacheStaleWhileRevalidateTest, AsyncRequestUrlMatches) {
7477 transaction_.url = "http://www.google.com/";
7478 // Write to the cache.
7479 RunFixtureTransactionTest();
7481 // Read back from the cache.
7482 RunFixtureTransactionTest();
7484 EXPECT_EQ(1, transaction_count());
7486 transaction_.handler = CheckUrlMatches;
7488 // Let the async request execute and perform the check.
7489 base::RunLoop().RunUntilIdle();
7490 EXPECT_EQ(2, transaction_count());
7493 class SyncLoadFlagTest : public HttpCacheStaleWhileRevalidateTest,
7494 public ::testing::WithParamInterface<int> {};
7496 // Flags which should always cause the request to be synchronous.
7497 TEST_P(SyncLoadFlagTest, MustBeSynchronous) {
7498 transaction_.load_flags |= GetParam();
7499 // Write to the cache.
7500 RunFixtureTransactionTest();
7502 EXPECT_EQ(1, transaction_count());
7504 // Reading back reads from the network.
7505 RunFixtureTransactionTest();
7507 EXPECT_EQ(2, transaction_count());
7510 INSTANTIATE_TEST_CASE_P(HttpCacheStaleWhileRevalidate,
7511 SyncLoadFlagTest,
7512 ::testing::Values(net::LOAD_VALIDATE_CACHE,
7513 net::LOAD_BYPASS_CACHE,
7514 net::LOAD_DISABLE_CACHE));
7516 TEST_F(HttpCacheStaleWhileRevalidateTest,
7517 PreferringCacheDoesNotTriggerAsyncRequest) {
7518 transaction_.load_flags |= net::LOAD_PREFERRING_CACHE;
7519 // Write to the cache.
7520 RunFixtureTransactionTest();
7522 EXPECT_EQ(1, transaction_count());
7524 // Reading back reads from the cache.
7525 RunFixtureTransactionTest();
7527 EXPECT_EQ(1, transaction_count());
7529 // If there was an async transaction created, it would run now.
7530 base::RunLoop().RunUntilIdle();
7532 // There was no async transaction.
7533 EXPECT_EQ(1, transaction_count());
7536 TEST_F(HttpCacheStaleWhileRevalidateTest, NotUsedWhenDisabled) {
7537 cache_.http_cache()->set_use_stale_while_revalidate_for_testing(false);
7538 // Write to the cache.
7539 RunFixtureTransactionTest();
7541 EXPECT_EQ(1, transaction_count());
7543 // A synchronous revalidation is performed.
7544 RunFixtureTransactionTest();
7546 EXPECT_EQ(2, transaction_count());
7549 TEST_F(HttpCacheStaleWhileRevalidateTest,
7550 OnlyFromCacheDoesNotTriggerAsyncRequest) {
7551 transaction_.load_flags |= net::LOAD_ONLY_FROM_CACHE;
7552 transaction_.return_code = net::ERR_CACHE_MISS;
7554 // Writing to the cache should fail, because we are avoiding the network.
7555 RunFixtureTransactionTest();
7557 EXPECT_EQ(0, transaction_count());
7559 base::RunLoop().RunUntilIdle();
7561 // Still nothing.
7562 EXPECT_EQ(0, transaction_count());
7565 // A certificate error during an asynchronous fetch should cause the next fetch
7566 // to proceed synchronously.
7567 // TODO(ricea): In future, only certificate errors which require user
7568 // interaction should fail the asynchronous revalidation, and they should cause
7569 // the next revalidation to be synchronous rather than requiring a total
7570 // refetch. This test will need to be updated appropriately.
7571 TEST_F(HttpCacheStaleWhileRevalidateTest, CertificateErrorCausesRefetch) {
7572 // Write to the cache.
7573 RunFixtureTransactionTest();
7575 EXPECT_EQ(1, transaction_count());
7577 // Now read back. RunTransactionTestBase() expects to receive the network
7578 // error back from the HttpCache::Transaction, but since the cache request
7579 // will return OK we need to duplicate some of its implementation here.
7580 transaction_.return_code = net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED;
7581 net::TestCompletionCallback callback;
7582 scoped_ptr<net::HttpTransaction> trans;
7583 int rv =
7584 cache_.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, &trans);
7585 EXPECT_EQ(net::OK, rv);
7586 ASSERT_TRUE(trans.get());
7588 MockHttpRequest request(transaction_);
7589 rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
7590 ASSERT_EQ(net::ERR_IO_PENDING, rv);
7591 ASSERT_EQ(net::OK, callback.WaitForResult());
7592 ReadAndVerifyTransaction(trans.get(), transaction_);
7594 EXPECT_EQ(1, transaction_count());
7596 // Allow the asynchronous fetch to run.
7597 base::RunLoop().RunUntilIdle();
7599 EXPECT_EQ(2, transaction_count());
7601 // Now run the transaction again. It should run synchronously.
7602 transaction_.return_code = net::OK;
7603 RunFixtureTransactionTest();
7605 EXPECT_EQ(3, transaction_count());
7608 // Ensure that the response cached by the asynchronous request is not truncated,
7609 // even if the server is slow.
7610 TEST_F(HttpCacheStaleWhileRevalidateTest, EntireResponseCached) {
7611 transaction_.test_mode = TEST_MODE_SLOW_READ;
7612 // Write to the cache.
7613 RunFixtureTransactionTest();
7615 // Read back from the cache.
7616 RunFixtureTransactionTest();
7618 // Let the async request execute.
7619 base::RunLoop().RunUntilIdle();
7621 // The cache entry should still be complete.
7622 transaction_.load_flags = net::LOAD_ONLY_FROM_CACHE;
7623 RunFixtureTransactionTest();
7626 // Verify that there are no race conditions in the completely synchronous case.
7627 TEST_F(HttpCacheStaleWhileRevalidateTest, SynchronousCaseWorks) {
7628 transaction_.test_mode = TEST_MODE_SYNC_ALL;
7629 // Write to the cache.
7630 RunFixtureTransactionTest();
7632 EXPECT_EQ(1, transaction_count());
7634 // Read back from the cache.
7635 RunFixtureTransactionTest();
7637 EXPECT_EQ(1, transaction_count());
7639 // Let the async request execute.
7640 base::RunLoop().RunUntilIdle();
7641 EXPECT_EQ(2, transaction_count());
7644 static void CheckLoadFlagsAsyncRevalidation(const net::HttpRequestInfo* request,
7645 std::string* response_status,
7646 std::string* response_headers,
7647 std::string* response_data) {
7648 EXPECT_EQ(net::LOAD_ASYNC_REVALIDATION, request->load_flags);
7651 // Check that the load flags on the async request are the same as the load flags
7652 // on the original request, plus LOAD_ASYNC_REVALIDATION.
7653 TEST_F(HttpCacheStaleWhileRevalidateTest, LoadFlagsAsyncRevalidation) {
7654 transaction_.load_flags = net::LOAD_NORMAL;
7655 // Write to the cache.
7656 RunFixtureTransactionTest();
7658 EXPECT_EQ(1, transaction_count());
7660 // Read back from the cache.
7661 RunFixtureTransactionTest();
7663 EXPECT_EQ(1, transaction_count());
7665 transaction_.handler = CheckLoadFlagsAsyncRevalidation;
7666 // Let the async request execute.
7667 base::RunLoop().RunUntilIdle();
7668 EXPECT_EQ(2, transaction_count());
7671 static void SimpleMockAuthHandler(const net::HttpRequestInfo* request,
7672 std::string* response_status,
7673 std::string* response_headers,
7674 std::string* response_data) {
7675 if (request->extra_headers.HasHeader("X-Require-Mock-Auth") &&
7676 !request->extra_headers.HasHeader("Authorization")) {
7677 response_status->assign("HTTP/1.1 401 Unauthorized");
7678 response_headers->assign("WWW-Authenticate: Basic realm=\"mars\"\n");
7679 return;
7681 response_status->assign("HTTP/1.1 200 OK");
7684 TEST_F(HttpCacheStaleWhileRevalidateTest, RestartForAuth) {
7685 // Write to the cache.
7686 RunFixtureTransactionTest();
7688 EXPECT_EQ(1, transaction_count());
7690 // Now make the transaction require auth.
7691 transaction_.request_headers = "X-Require-Mock-Auth: dummy\r\n\r\n";
7692 transaction_.handler = SimpleMockAuthHandler;
7694 // Read back from the cache.
7695 RunFixtureTransactionTest();
7697 EXPECT_EQ(1, transaction_count());
7699 // Let the async request execute.
7700 base::RunLoop().RunUntilIdle();
7702 EXPECT_EQ(2, transaction_count());
7705 // Tests that we allow multiple simultaneous, non-overlapping transactions to
7706 // take place on a sparse entry.
7707 TEST(HttpCache, RangeGET_MultipleRequests) {
7708 MockHttpCache cache;
7710 // Create a transaction for bytes 0-9.
7711 MockHttpRequest request(kRangeGET_TransactionOK);
7712 MockTransaction transaction(kRangeGET_TransactionOK);
7713 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
7714 transaction.data = "rg: 00-09 ";
7715 AddMockTransaction(&transaction);
7717 net::TestCompletionCallback callback;
7718 scoped_ptr<net::HttpTransaction> trans;
7719 int rv = cache.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, &trans);
7720 EXPECT_EQ(net::OK, rv);
7721 ASSERT_TRUE(trans.get());
7723 // Start our transaction.
7724 trans->Start(&request, callback.callback(), net::BoundNetLog());
7726 // A second transaction on a different part of the file (the default
7727 // kRangeGET_TransactionOK requests 40-49) should not be blocked by
7728 // the already pending transaction.
7729 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
7731 // Let the first transaction complete.
7732 callback.WaitForResult();
7734 RemoveMockTransaction(&transaction);
7737 // Makes sure that a request stops using the cache when the response headers
7738 // with "Cache-Control: no-store" arrives. That means that another request for
7739 // the same URL can be processed before the response body of the original
7740 // request arrives.
7741 TEST(HttpCache, NoStoreResponseShouldNotBlockFollowingRequests) {
7742 MockHttpCache cache;
7743 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction);
7744 mock_transaction.response_headers = "Cache-Control: no-store\n";
7745 MockHttpRequest request(mock_transaction);
7747 scoped_ptr<Context> first(new Context);
7748 first->result = cache.CreateTransaction(&first->trans);
7749 ASSERT_EQ(net::OK, first->result);
7750 EXPECT_EQ(net::LOAD_STATE_IDLE, first->trans->GetLoadState());
7751 first->result = first->trans->Start(
7752 &request, first->callback.callback(), net::BoundNetLog());
7753 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, first->trans->GetLoadState());
7755 base::MessageLoop::current()->RunUntilIdle();
7756 EXPECT_EQ(net::LOAD_STATE_IDLE, first->trans->GetLoadState());
7757 ASSERT_TRUE(first->trans->GetResponseInfo());
7758 EXPECT_TRUE(first->trans->GetResponseInfo()->headers->HasHeaderValue(
7759 "Cache-Control", "no-store"));
7760 // Here we have read the response header but not read the response body yet.
7762 // Let us create the second (read) transaction.
7763 scoped_ptr<Context> second(new Context);
7764 second->result = cache.CreateTransaction(&second->trans);
7765 ASSERT_EQ(net::OK, second->result);
7766 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState());
7767 second->result = second->trans->Start(
7768 &request, second->callback.callback(), net::BoundNetLog());
7770 // Here the second transaction proceeds without reading the first body.
7771 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState());
7772 base::MessageLoop::current()->RunUntilIdle();
7773 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState());
7774 ASSERT_TRUE(second->trans->GetResponseInfo());
7775 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue(
7776 "Cache-Control", "no-store"));
7777 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction);