| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 6726 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6737 | 6737 |
| 6738 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | 6738 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 6739 &response); | 6739 &response); |
| 6740 EXPECT_TRUE(response.metadata.get() == NULL); | 6740 EXPECT_TRUE(response.metadata.get() == NULL); |
| 6741 | 6741 |
| 6742 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6742 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6743 EXPECT_EQ(2, cache.disk_cache()->open_count()); | 6743 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 6744 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6744 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6745 } | 6745 } |
| 6746 | 6746 |
| 6747 // Tests that we ignore VARY checks when writing metadata since the request |
| 6748 // headers for the WriteMetadata transaction are made up. |
| 6749 TEST(HttpCache, WriteMetadata_IgnoreVary) { |
| 6750 MockHttpCache cache; |
| 6751 |
| 6752 // Write to the cache |
| 6753 HttpResponseInfo response; |
| 6754 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 6755 transaction.request_headers = "accept-encoding: gzip\r\n"; |
| 6756 transaction.response_headers = |
| 6757 "Vary: accept-encoding\n" |
| 6758 "Cache-Control: max-age=10000\n"; |
| 6759 |
| 6760 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
| 6761 &response); |
| 6762 EXPECT_FALSE(response.metadata); |
| 6763 |
| 6764 // Attempt to write meta data to the same entry. |
| 6765 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50)); |
| 6766 memset(buf->data(), 0, buf->size()); |
| 6767 base::strlcpy(buf->data(), "Hi there", buf->size()); |
| 6768 cache.http_cache()->WriteMetadata(GURL(transaction.url), DEFAULT_PRIORITY, |
| 6769 response.response_time, buf.get(), |
| 6770 buf->size()); |
| 6771 |
| 6772 // Makes sure we finish pending operations. |
| 6773 base::RunLoop().RunUntilIdle(); |
| 6774 |
| 6775 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
| 6776 &response); |
| 6777 ASSERT_TRUE(response.metadata); |
| 6778 EXPECT_EQ(50, response.metadata->size()); |
| 6779 EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there")); |
| 6780 |
| 6781 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6782 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 6783 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6784 } |
| 6785 |
| 6786 TEST(HttpCache, SkipVaryCheck) { |
| 6787 MockHttpCache cache; |
| 6788 |
| 6789 // Write a simple vary transaction to the cache. |
| 6790 HttpResponseInfo response; |
| 6791 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 6792 transaction.request_headers = "accept-encoding: gzip\r\n"; |
| 6793 transaction.response_headers = |
| 6794 "Vary: accept-encoding\n" |
| 6795 "Cache-Control: max-age=10000\n"; |
| 6796 RunTransactionTest(cache.http_cache(), transaction); |
| 6797 |
| 6798 // Change the request headers so that the request doesn't match due to vary. |
| 6799 // The request should fail. |
| 6800 transaction.load_flags = LOAD_ONLY_FROM_CACHE; |
| 6801 transaction.request_headers = "accept-encoding: foo\r\n"; |
| 6802 transaction.return_code = ERR_CACHE_MISS; |
| 6803 RunTransactionTest(cache.http_cache(), transaction); |
| 6804 |
| 6805 // Change the load flags to ignore vary checks, the request should now hit. |
| 6806 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_VARY_CHECK; |
| 6807 transaction.return_code = OK; |
| 6808 RunTransactionTest(cache.http_cache(), transaction); |
| 6809 } |
| 6810 |
| 6747 // Tests that we only return valid entries with LOAD_ONLY_FROM_CACHE | 6811 // Tests that we only return valid entries with LOAD_ONLY_FROM_CACHE |
| 6748 // transactions unless LOAD_SKIP_CACHE_VALIDATION is set. | 6812 // transactions unless LOAD_SKIP_CACHE_VALIDATION is set. |
| 6749 TEST(HttpCache, ValidLoadOnlyFromCache) { | 6813 TEST(HttpCache, ValidLoadOnlyFromCache) { |
| 6750 MockHttpCache cache; | 6814 MockHttpCache cache; |
| 6751 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 6815 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
| 6752 cache.http_cache()->SetClockForTesting(base::WrapUnique(clock)); | 6816 cache.http_cache()->SetClockForTesting(base::WrapUnique(clock)); |
| 6753 cache.network_layer()->SetClock(clock); | 6817 cache.network_layer()->SetClock(clock); |
| 6754 | 6818 |
| 6755 // Write a resource that will expire in 100 seconds. | 6819 // Write a resource that will expire in 100 seconds. |
| 6756 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 6820 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| (...skipping 1388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8145 RunTransactionTestWithResponseInfo(cache.http_cache(), | 8209 RunTransactionTestWithResponseInfo(cache.http_cache(), |
| 8146 kTypicalGET_Transaction, &response_info); | 8210 kTypicalGET_Transaction, &response_info); |
| 8147 | 8211 |
| 8148 EXPECT_FALSE(response_info.was_cached); | 8212 EXPECT_FALSE(response_info.was_cached); |
| 8149 EXPECT_TRUE(response_info.network_accessed); | 8213 EXPECT_TRUE(response_info.network_accessed); |
| 8150 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, | 8214 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, |
| 8151 response_info.cache_entry_status); | 8215 response_info.cache_entry_status); |
| 8152 } | 8216 } |
| 8153 | 8217 |
| 8154 } // namespace net | 8218 } // namespace net |
| OLD | NEW |