OLD | NEW |
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 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/hash_tables.h" | 7 #include "base/hash_tables.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/scoped_vector.h" | 9 #include "base/scoped_vector.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 explicit MockHttpCache(net::HttpCache::BackendFactory* disk_cache_factory) | 584 explicit MockHttpCache(net::HttpCache::BackendFactory* disk_cache_factory) |
585 : http_cache_(new MockNetworkLayer(), disk_cache_factory) { | 585 : http_cache_(new MockNetworkLayer(), disk_cache_factory) { |
586 } | 586 } |
587 | 587 |
588 net::HttpCache* http_cache() { return &http_cache_; } | 588 net::HttpCache* http_cache() { return &http_cache_; } |
589 | 589 |
590 MockNetworkLayer* network_layer() { | 590 MockNetworkLayer* network_layer() { |
591 return static_cast<MockNetworkLayer*>(http_cache_.network_layer()); | 591 return static_cast<MockNetworkLayer*>(http_cache_.network_layer()); |
592 } | 592 } |
593 MockDiskCache* disk_cache() { | 593 MockDiskCache* disk_cache() { |
594 return static_cast<MockDiskCache*>(http_cache_.GetBackend()); | 594 TestCompletionCallback cb; |
| 595 disk_cache::Backend* backend; |
| 596 int rv = http_cache_.GetBackend(&backend, &cb); |
| 597 rv = cb.GetResult(rv); |
| 598 return (rv == net::OK) ? static_cast<MockDiskCache*>(backend) : NULL; |
595 } | 599 } |
596 | 600 |
597 // Helper function for reading response info from the disk cache. | 601 // Helper function for reading response info from the disk cache. |
598 static bool ReadResponseInfo(disk_cache::Entry* disk_entry, | 602 static bool ReadResponseInfo(disk_cache::Entry* disk_entry, |
599 net::HttpResponseInfo* response_info, | 603 net::HttpResponseInfo* response_info, |
600 bool* response_truncated) { | 604 bool* response_truncated) { |
601 int size = disk_entry->GetDataSize(0); | 605 int size = disk_entry->GetDataSize(0); |
602 | 606 |
603 TestCompletionCallback cb; | 607 TestCompletionCallback cb; |
604 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(size); | 608 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(size); |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1011 | 1015 |
1012 scoped_ptr<net::HttpTransaction> trans; | 1016 scoped_ptr<net::HttpTransaction> trans; |
1013 int rv = cache.http_cache()->CreateTransaction(&trans); | 1017 int rv = cache.http_cache()->CreateTransaction(&trans); |
1014 EXPECT_EQ(net::OK, rv); | 1018 EXPECT_EQ(net::OK, rv); |
1015 ASSERT_TRUE(trans.get()); | 1019 ASSERT_TRUE(trans.get()); |
1016 } | 1020 } |
1017 | 1021 |
1018 TEST(HttpCache, GetBackend) { | 1022 TEST(HttpCache, GetBackend) { |
1019 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(0)); | 1023 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(0)); |
1020 | 1024 |
| 1025 disk_cache::Backend* backend; |
| 1026 TestCompletionCallback cb; |
1021 // This will lazily initialize the backend. | 1027 // This will lazily initialize the backend. |
1022 EXPECT_TRUE(cache.http_cache()->GetBackend()); | 1028 int rv = cache.http_cache()->GetBackend(&backend, &cb); |
| 1029 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
1023 } | 1030 } |
1024 | 1031 |
1025 TEST(HttpCache, SimpleGET) { | 1032 TEST(HttpCache, SimpleGET) { |
1026 MockHttpCache cache; | 1033 MockHttpCache cache; |
1027 | 1034 |
1028 // write to the cache | 1035 // write to the cache |
1029 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 1036 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
1030 | 1037 |
1031 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1038 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1032 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1039 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1069 // This will initialize a cache object with NULL backend. | 1076 // This will initialize a cache object with NULL backend. |
1070 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | 1077 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
1071 factory->set_fail(true); | 1078 factory->set_fail(true); |
1072 factory->FinishCreation(); // We'll complete synchronously. | 1079 factory->FinishCreation(); // We'll complete synchronously. |
1073 MockHttpCache cache(factory); | 1080 MockHttpCache cache(factory); |
1074 | 1081 |
1075 // Read from the network, and don't use the cache. | 1082 // Read from the network, and don't use the cache. |
1076 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 1083 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
1077 | 1084 |
1078 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1085 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1079 EXPECT_FALSE(cache.http_cache()->GetBackend()); | 1086 EXPECT_FALSE(cache.http_cache()->GetCurrentBackend()); |
1080 } | 1087 } |
1081 | 1088 |
1082 TEST(HttpCache, SimpleGETWithDiskFailures) { | 1089 TEST(HttpCache, SimpleGETWithDiskFailures) { |
1083 MockHttpCache cache; | 1090 MockHttpCache cache; |
1084 | 1091 |
1085 cache.disk_cache()->set_soft_failures(true); | 1092 cache.disk_cache()->set_soft_failures(true); |
1086 | 1093 |
1087 // Read from the network, and fail to write to the cache. | 1094 // Read from the network, and fail to write to the cache. |
1088 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 1095 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
1089 | 1096 |
(...skipping 3539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4629 // Now return 200 when validating the entry so the metadata will be lost. | 4636 // Now return 200 when validating the entry so the metadata will be lost. |
4630 MockTransaction trans2(kTypicalGET_Transaction); | 4637 MockTransaction trans2(kTypicalGET_Transaction); |
4631 trans2.load_flags = net::LOAD_VALIDATE_CACHE; | 4638 trans2.load_flags = net::LOAD_VALIDATE_CACHE; |
4632 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); | 4639 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); |
4633 EXPECT_TRUE(response.metadata.get() == NULL); | 4640 EXPECT_TRUE(response.metadata.get() == NULL); |
4634 | 4641 |
4635 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 4642 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
4636 EXPECT_EQ(4, cache.disk_cache()->open_count()); | 4643 EXPECT_EQ(4, cache.disk_cache()->open_count()); |
4637 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 4644 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
4638 } | 4645 } |
OLD | NEW |