OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_cache.h" | 5 #include "net/http/http_cache.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "net/base/cache_type.h" | 12 #include "net/base/cache_type.h" |
13 #include "net/base/cert_status_flags.h" | 13 #include "net/base/cert_status_flags.h" |
14 #include "net/base/completion_callback.h" | |
15 #include "net/base/host_port_pair.h" | 14 #include "net/base/host_port_pair.h" |
16 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
17 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
18 #include "net/base/net_log_unittest.h" | 17 #include "net/base/net_log_unittest.h" |
19 #include "net/base/ssl_cert_request_info.h" | 18 #include "net/base/ssl_cert_request_info.h" |
20 #include "net/disk_cache/disk_cache.h" | 19 #include "net/disk_cache/disk_cache.h" |
21 #include "net/http/http_byte_range.h" | 20 #include "net/http/http_byte_range.h" |
22 #include "net/http/http_request_headers.h" | 21 #include "net/http/http_request_headers.h" |
23 #include "net/http/http_request_info.h" | 22 #include "net/http/http_request_info.h" |
24 #include "net/http/http_response_headers.h" | 23 #include "net/http/http_response_headers.h" |
25 #include "net/http/http_response_info.h" | 24 #include "net/http/http_response_info.h" |
26 #include "net/http/http_transaction.h" | 25 #include "net/http/http_transaction.h" |
27 #include "net/http/http_transaction_unittest.h" | 26 #include "net/http/http_transaction_unittest.h" |
28 #include "net/http/http_util.h" | 27 #include "net/http/http_util.h" |
29 #include "net/http/mock_http_cache.h" | 28 #include "net/http/mock_http_cache.h" |
30 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
31 | 30 |
32 using base::Time; | 31 using base::Time; |
33 | 32 |
34 namespace { | 33 namespace { |
35 | 34 |
36 class DeleteCacheCompletionCallback : public TestCompletionCallbackBase { | 35 class DeleteCacheOldCompletionCallback : public TestOldCompletionCallback { |
37 public: | 36 public: |
38 explicit DeleteCacheCompletionCallback(MockHttpCache* cache) | 37 explicit DeleteCacheOldCompletionCallback(MockHttpCache* cache) |
39 : cache_(cache), | 38 : cache_(cache) {} |
40 ALLOW_THIS_IN_INITIALIZER_LIST(callback_( | 39 |
41 base::Bind(&DeleteCacheCompletionCallback::OnComplete, | 40 virtual void RunWithParams(const Tuple1<int>& params) { |
42 base::Unretained(this)))) { | 41 delete cache_; |
| 42 TestOldCompletionCallback::RunWithParams(params); |
43 } | 43 } |
44 | 44 |
45 const net::CompletionCallback& callback() const { return callback_; } | |
46 | |
47 private: | 45 private: |
48 void OnComplete(int result) { | |
49 delete cache_; | |
50 SetResult(result); | |
51 } | |
52 | |
53 MockHttpCache* cache_; | 46 MockHttpCache* cache_; |
54 const net::CompletionCallback callback_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(DeleteCacheCompletionCallback); | |
57 }; | 47 }; |
58 | 48 |
59 //----------------------------------------------------------------------------- | 49 //----------------------------------------------------------------------------- |
60 // helpers | 50 // helpers |
61 | 51 |
62 void ReadAndVerifyTransaction(net::HttpTransaction* trans, | 52 void ReadAndVerifyTransaction(net::HttpTransaction* trans, |
63 const MockTransaction& trans_info) { | 53 const MockTransaction& trans_info) { |
64 std::string content; | 54 std::string content; |
65 int rv = ReadTransaction(trans, &content); | 55 int rv = ReadTransaction(trans, &content); |
66 | 56 |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 scoped_ptr<net::HttpTransaction> trans; | 388 scoped_ptr<net::HttpTransaction> trans; |
399 int rv = cache.http_cache()->CreateTransaction(&trans); | 389 int rv = cache.http_cache()->CreateTransaction(&trans); |
400 EXPECT_EQ(net::OK, rv); | 390 EXPECT_EQ(net::OK, rv); |
401 ASSERT_TRUE(trans.get()); | 391 ASSERT_TRUE(trans.get()); |
402 } | 392 } |
403 | 393 |
404 TEST(HttpCache, GetBackend) { | 394 TEST(HttpCache, GetBackend) { |
405 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(0)); | 395 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(0)); |
406 | 396 |
407 disk_cache::Backend* backend; | 397 disk_cache::Backend* backend; |
408 net::TestCompletionCallback cb; | 398 TestOldCompletionCallback cb; |
409 // This will lazily initialize the backend. | 399 // This will lazily initialize the backend. |
410 int rv = cache.http_cache()->GetBackend(&backend, cb.callback()); | 400 int rv = cache.http_cache()->GetBackend(&backend, &cb); |
411 EXPECT_EQ(net::OK, cb.GetResult(rv)); | 401 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
412 } | 402 } |
413 | 403 |
414 TEST(HttpCache, SimpleGET) { | 404 TEST(HttpCache, SimpleGET) { |
415 MockHttpCache cache; | 405 MockHttpCache cache; |
416 | 406 |
417 // write to the cache | 407 // write to the cache |
418 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 408 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
419 | 409 |
420 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 410 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
(...skipping 1047 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1468 *backend = NULL; | 1458 *backend = NULL; |
1469 callback->Run(net::ERR_ABORTED); | 1459 callback->Run(net::ERR_ABORTED); |
1470 } | 1460 } |
1471 | 1461 |
1472 // Tests that we can delete the cache while creating the backend, from within | 1462 // Tests that we can delete the cache while creating the backend, from within |
1473 // one of the callbacks. | 1463 // one of the callbacks. |
1474 TEST(HttpCache, DeleteCacheWaitingForBackend2) { | 1464 TEST(HttpCache, DeleteCacheWaitingForBackend2) { |
1475 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | 1465 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
1476 MockHttpCache* cache = new MockHttpCache(factory); | 1466 MockHttpCache* cache = new MockHttpCache(factory); |
1477 | 1467 |
1478 DeleteCacheCompletionCallback cb(cache); | 1468 DeleteCacheOldCompletionCallback cb(cache); |
1479 disk_cache::Backend* backend; | 1469 disk_cache::Backend* backend; |
1480 int rv = cache->http_cache()->GetBackend(&backend, cb.callback()); | 1470 int rv = cache->http_cache()->GetBackend(&backend, &cb); |
1481 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 1471 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
1482 | 1472 |
1483 // Now let's queue a regular transaction | 1473 // Now let's queue a regular transaction |
1484 MockHttpRequest request(kSimpleGET_Transaction); | 1474 MockHttpRequest request(kSimpleGET_Transaction); |
1485 | 1475 |
1486 scoped_ptr<Context> c(new Context()); | 1476 scoped_ptr<Context> c(new Context()); |
1487 c->result = cache->http_cache()->CreateTransaction(&c->trans); | 1477 c->result = cache->http_cache()->CreateTransaction(&c->trans); |
1488 EXPECT_EQ(net::OK, c->result); | 1478 EXPECT_EQ(net::OK, c->result); |
1489 | 1479 |
1490 c->trans->Start(&request, &c->callback, net::BoundNetLog()); | 1480 c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
1491 | 1481 |
1492 // And another direct backend request. | 1482 // And another direct backend request. |
1493 net::TestCompletionCallback cb2; | 1483 TestOldCompletionCallback cb2; |
1494 rv = cache->http_cache()->GetBackend(&backend, cb2.callback()); | 1484 rv = cache->http_cache()->GetBackend(&backend, &cb2); |
1495 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 1485 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
1496 | 1486 |
1497 // Just to make sure that everything is still pending. | 1487 // Just to make sure that everything is still pending. |
1498 MessageLoop::current()->RunAllPending(); | 1488 MessageLoop::current()->RunAllPending(); |
1499 | 1489 |
1500 // The request should be queued. | 1490 // The request should be queued. |
1501 EXPECT_FALSE(c->callback.have_result()); | 1491 EXPECT_FALSE(c->callback.have_result()); |
1502 | 1492 |
1503 // Generate the callback. | 1493 // Generate the callback. |
1504 factory->FinishCreation(); | 1494 factory->FinishCreation(); |
(...skipping 3158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4663 | 4653 |
4664 // Verify that the entry is marked as incomplete. | 4654 // Verify that the entry is marked as incomplete. |
4665 disk_cache::Entry* entry; | 4655 disk_cache::Entry* entry; |
4666 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | 4656 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); |
4667 net::HttpResponseInfo response; | 4657 net::HttpResponseInfo response; |
4668 bool truncated = false; | 4658 bool truncated = false; |
4669 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | 4659 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
4670 EXPECT_TRUE(truncated); | 4660 EXPECT_TRUE(truncated); |
4671 entry->Close(); | 4661 entry->Close(); |
4672 } | 4662 } |
OLD | NEW |