| 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 859 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 870 NetLogEventType::HTTP_CACHE_CREATE_ENTRY)); | 870 NetLogEventType::HTTP_CACHE_CREATE_ENTRY)); |
| 871 EXPECT_TRUE(LogContainsBeginEvent(entries, 6, | 871 EXPECT_TRUE(LogContainsBeginEvent(entries, 6, |
| 872 NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY)); | 872 NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY)); |
| 873 EXPECT_TRUE(LogContainsEndEvent(entries, 7, | 873 EXPECT_TRUE(LogContainsEndEvent(entries, 7, |
| 874 NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY)); | 874 NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY)); |
| 875 | 875 |
| 876 TestLoadTimingNetworkRequest(load_timing_info); | 876 TestLoadTimingNetworkRequest(load_timing_info); |
| 877 | 877 |
| 878 // Force this transaction to read from the cache. | 878 // Force this transaction to read from the cache. |
| 879 MockTransaction transaction(kSimpleGET_Transaction); | 879 MockTransaction transaction(kSimpleGET_Transaction); |
| 880 transaction.load_flags |= LOAD_ONLY_FROM_CACHE; | 880 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 881 | 881 |
| 882 log.Clear(); | 882 log.Clear(); |
| 883 | 883 |
| 884 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), | 884 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 885 &load_timing_info); | 885 &load_timing_info); |
| 886 | 886 |
| 887 // Check that the NetLog was filled as expected. | 887 // Check that the NetLog was filled as expected. |
| 888 log.GetEntries(&entries); | 888 log.GetEntries(&entries); |
| 889 FilterLogEntries(&entries); | 889 FilterLogEntries(&entries); |
| 890 | 890 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 910 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 910 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 911 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 911 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 912 TestLoadTimingCachedResponse(load_timing_info); | 912 TestLoadTimingCachedResponse(load_timing_info); |
| 913 } | 913 } |
| 914 | 914 |
| 915 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { | 915 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { |
| 916 MockHttpCache cache; | 916 MockHttpCache cache; |
| 917 | 917 |
| 918 // force this transaction to read from the cache | 918 // force this transaction to read from the cache |
| 919 MockTransaction transaction(kSimpleGET_Transaction); | 919 MockTransaction transaction(kSimpleGET_Transaction); |
| 920 transaction.load_flags |= LOAD_ONLY_FROM_CACHE; | 920 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 921 | 921 |
| 922 MockHttpRequest request(transaction); | 922 MockHttpRequest request(transaction); |
| 923 TestCompletionCallback callback; | 923 TestCompletionCallback callback; |
| 924 | 924 |
| 925 std::unique_ptr<HttpTransaction> trans; | 925 std::unique_ptr<HttpTransaction> trans; |
| 926 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 926 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); |
| 927 | 927 |
| 928 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 928 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 929 if (rv == ERR_IO_PENDING) | 929 if (rv == ERR_IO_PENDING) |
| 930 rv = callback.WaitForResult(); | 930 rv = callback.WaitForResult(); |
| 931 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); | 931 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); |
| 932 | 932 |
| 933 trans.reset(); | 933 trans.reset(); |
| 934 | 934 |
| 935 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | 935 EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 936 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 936 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 937 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 937 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 938 } | 938 } |
| 939 | 939 |
| 940 TEST(HttpCache, SimpleGET_LoadPreferringCache_Hit) { | 940 TEST(HttpCache, SimpleGET_LoadPreferringCache_Hit) { |
| 941 MockHttpCache cache; | 941 MockHttpCache cache; |
| 942 | 942 |
| 943 // write to the cache | 943 // write to the cache |
| 944 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 944 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 945 | 945 |
| 946 // force this transaction to read from the cache if valid | 946 // force this transaction to read from the cache if valid |
| 947 MockTransaction transaction(kSimpleGET_Transaction); | 947 MockTransaction transaction(kSimpleGET_Transaction); |
| 948 transaction.load_flags |= LOAD_PREFERRING_CACHE; | 948 transaction.load_flags |= LOAD_SKIP_CACHE_VALIDATION; |
| 949 | 949 |
| 950 RunTransactionTest(cache.http_cache(), transaction); | 950 RunTransactionTest(cache.http_cache(), transaction); |
| 951 | 951 |
| 952 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 952 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 953 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 953 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 954 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 954 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 955 } | 955 } |
| 956 | 956 |
| 957 TEST(HttpCache, SimpleGET_LoadPreferringCache_Miss) { | 957 TEST(HttpCache, SimpleGET_LoadPreferringCache_Miss) { |
| 958 MockHttpCache cache; | 958 MockHttpCache cache; |
| 959 | 959 |
| 960 // force this transaction to read from the cache if valid | 960 // force this transaction to read from the cache if valid |
| 961 MockTransaction transaction(kSimpleGET_Transaction); | 961 MockTransaction transaction(kSimpleGET_Transaction); |
| 962 transaction.load_flags |= LOAD_PREFERRING_CACHE; | 962 transaction.load_flags |= LOAD_SKIP_CACHE_VALIDATION; |
| 963 | 963 |
| 964 RunTransactionTest(cache.http_cache(), transaction); | 964 RunTransactionTest(cache.http_cache(), transaction); |
| 965 | 965 |
| 966 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 966 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 967 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 967 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 968 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 968 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 969 } | 969 } |
| 970 | 970 |
| 971 // Tests LOAD_PREFERRING_CACHE in the presence of vary headers. | 971 // Tests LOAD_SKIP_CACHE_VALIDATION in the presence of vary headers. |
| 972 TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMatch) { | 972 TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMatch) { |
| 973 MockHttpCache cache; | 973 MockHttpCache cache; |
| 974 | 974 |
| 975 // Write to the cache. | 975 // Write to the cache. |
| 976 MockTransaction transaction(kSimpleGET_Transaction); | 976 MockTransaction transaction(kSimpleGET_Transaction); |
| 977 transaction.request_headers = "Foo: bar\r\n"; | 977 transaction.request_headers = "Foo: bar\r\n"; |
| 978 transaction.response_headers = "Cache-Control: max-age=10000\n" | 978 transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 979 "Vary: Foo\n"; | 979 "Vary: Foo\n"; |
| 980 AddMockTransaction(&transaction); | 980 AddMockTransaction(&transaction); |
| 981 RunTransactionTest(cache.http_cache(), transaction); | 981 RunTransactionTest(cache.http_cache(), transaction); |
| 982 | 982 |
| 983 // Read from the cache. | 983 // Read from the cache. |
| 984 transaction.load_flags |= LOAD_PREFERRING_CACHE; | 984 transaction.load_flags |= LOAD_SKIP_CACHE_VALIDATION; |
| 985 RunTransactionTest(cache.http_cache(), transaction); | 985 RunTransactionTest(cache.http_cache(), transaction); |
| 986 | 986 |
| 987 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 987 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 988 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 988 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 989 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 989 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 990 RemoveMockTransaction(&transaction); | 990 RemoveMockTransaction(&transaction); |
| 991 } | 991 } |
| 992 | 992 |
| 993 // Tests LOAD_PREFERRING_CACHE in the presence of vary headers. | 993 // Tests LOAD_SKIP_CACHE_VALIDATION in the presence of vary headers. |
| 994 TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMismatch) { | 994 TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMismatch) { |
| 995 MockHttpCache cache; | 995 MockHttpCache cache; |
| 996 | 996 |
| 997 // Write to the cache. | 997 // Write to the cache. |
| 998 MockTransaction transaction(kSimpleGET_Transaction); | 998 MockTransaction transaction(kSimpleGET_Transaction); |
| 999 transaction.request_headers = "Foo: bar\r\n"; | 999 transaction.request_headers = "Foo: bar\r\n"; |
| 1000 transaction.response_headers = "Cache-Control: max-age=10000\n" | 1000 transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 1001 "Vary: Foo\n"; | 1001 "Vary: Foo\n"; |
| 1002 AddMockTransaction(&transaction); | 1002 AddMockTransaction(&transaction); |
| 1003 RunTransactionTest(cache.http_cache(), transaction); | 1003 RunTransactionTest(cache.http_cache(), transaction); |
| 1004 | 1004 |
| 1005 // Attempt to read from the cache... this is a vary mismatch that must reach | 1005 // Attempt to read from the cache... this is a vary mismatch that must reach |
| 1006 // the network again. | 1006 // the network again. |
| 1007 transaction.load_flags |= LOAD_PREFERRING_CACHE; | 1007 transaction.load_flags |= LOAD_SKIP_CACHE_VALIDATION; |
| 1008 transaction.request_headers = "Foo: none\r\n"; | 1008 transaction.request_headers = "Foo: none\r\n"; |
| 1009 BoundTestNetLog log; | 1009 BoundTestNetLog log; |
| 1010 LoadTimingInfo load_timing_info; | 1010 LoadTimingInfo load_timing_info; |
| 1011 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), | 1011 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
| 1012 &load_timing_info); | 1012 &load_timing_info); |
| 1013 | 1013 |
| 1014 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 1014 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1015 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 1015 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1016 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1016 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1017 TestLoadTimingNetworkRequest(load_timing_info); | 1017 TestLoadTimingNetworkRequest(load_timing_info); |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1333 | 1333 |
| 1334 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. | 1334 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. |
| 1335 // If cancelling a request is racing with another request for the same resource | 1335 // If cancelling a request is racing with another request for the same resource |
| 1336 // finishing, we have to make sure that we remove both transactions from the | 1336 // finishing, we have to make sure that we remove both transactions from the |
| 1337 // entry. | 1337 // entry. |
| 1338 TEST(HttpCache, SimpleGET_RacingReaders) { | 1338 TEST(HttpCache, SimpleGET_RacingReaders) { |
| 1339 MockHttpCache cache; | 1339 MockHttpCache cache; |
| 1340 | 1340 |
| 1341 MockHttpRequest request(kSimpleGET_Transaction); | 1341 MockHttpRequest request(kSimpleGET_Transaction); |
| 1342 MockHttpRequest reader_request(kSimpleGET_Transaction); | 1342 MockHttpRequest reader_request(kSimpleGET_Transaction); |
| 1343 reader_request.load_flags = LOAD_ONLY_FROM_CACHE; | 1343 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 1344 | 1344 |
| 1345 std::vector<Context*> context_list; | 1345 std::vector<Context*> context_list; |
| 1346 const int kNumTransactions = 5; | 1346 const int kNumTransactions = 5; |
| 1347 | 1347 |
| 1348 for (int i = 0; i < kNumTransactions; ++i) { | 1348 for (int i = 0; i < kNumTransactions; ++i) { |
| 1349 context_list.push_back(new Context()); | 1349 context_list.push_back(new Context()); |
| 1350 Context* c = context_list[i]; | 1350 Context* c = context_list[i]; |
| 1351 | 1351 |
| 1352 c->result = cache.CreateTransaction(&c->trans); | 1352 c->result = cache.CreateTransaction(&c->trans); |
| 1353 ASSERT_THAT(c->result, IsOk()); | 1353 ASSERT_THAT(c->result, IsOk()); |
| (...skipping 1121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2475 cache.http_cache(), request, &response_headers); | 2475 cache.http_cache(), request, &response_headers); |
| 2476 | 2476 |
| 2477 EXPECT_EQ(net_response_1.status_and_headers(), response_headers); | 2477 EXPECT_EQ(net_response_1.status_and_headers(), response_headers); |
| 2478 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2478 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2479 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2479 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2480 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2480 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2481 | 2481 |
| 2482 // Request |kUrl| a second time. Now |kNetResponse1| it is in the HTTP | 2482 // Request |kUrl| a second time. Now |kNetResponse1| it is in the HTTP |
| 2483 // cache, so we don't hit the network. | 2483 // cache, so we don't hit the network. |
| 2484 | 2484 |
| 2485 request.load_flags = LOAD_ONLY_FROM_CACHE; | 2485 request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 2486 | 2486 |
| 2487 kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. | 2487 kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. |
| 2488 net_response_1.AssignTo(&request); // Expected result. | 2488 net_response_1.AssignTo(&request); // Expected result. |
| 2489 | 2489 |
| 2490 RunTransactionTestWithResponse( | 2490 RunTransactionTestWithResponse( |
| 2491 cache.http_cache(), request, &response_headers); | 2491 cache.http_cache(), request, &response_headers); |
| 2492 | 2492 |
| 2493 EXPECT_EQ(net_response_1.status_and_headers(), response_headers); | 2493 EXPECT_EQ(net_response_1.status_and_headers(), response_headers); |
| 2494 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2494 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2495 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 2495 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 2513 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 2513 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2514 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 2514 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2515 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2515 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2516 | 2516 |
| 2517 // Finally, request |kUrl| again. This request should be serviced from | 2517 // Finally, request |kUrl| again. This request should be serviced from |
| 2518 // the cache. Moreover, the value in the cache should be |kNetResponse2| | 2518 // the cache. Moreover, the value in the cache should be |kNetResponse2| |
| 2519 // and NOT |kNetResponse1|. The previous step should have replaced the | 2519 // and NOT |kNetResponse1|. The previous step should have replaced the |
| 2520 // value in the cache with the modified response. | 2520 // value in the cache with the modified response. |
| 2521 | 2521 |
| 2522 request.request_headers = ""; | 2522 request.request_headers = ""; |
| 2523 request.load_flags = LOAD_ONLY_FROM_CACHE; | 2523 request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 2524 | 2524 |
| 2525 kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. | 2525 kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. |
| 2526 cached_response_2.AssignTo(&request); // Expected result. | 2526 cached_response_2.AssignTo(&request); // Expected result. |
| 2527 | 2527 |
| 2528 RunTransactionTestWithResponse( | 2528 RunTransactionTestWithResponse( |
| 2529 cache.http_cache(), request, &response_headers); | 2529 cache.http_cache(), request, &response_headers); |
| 2530 | 2530 |
| 2531 EXPECT_EQ(cached_response_2.status_and_headers(), response_headers); | 2531 EXPECT_EQ(cached_response_2.status_and_headers(), response_headers); |
| 2532 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 2532 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2533 EXPECT_EQ(2, cache.disk_cache()->open_count()); | 2533 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| (...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2867 RunTransactionTest(cache.http_cache(), trans); | 2867 RunTransactionTest(cache.http_cache(), trans); |
| 2868 | 2868 |
| 2869 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2869 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2870 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2870 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2871 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2871 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2872 | 2872 |
| 2873 // Request the same URL, but this time with a reference section (hash). | 2873 // Request the same URL, but this time with a reference section (hash). |
| 2874 // Since the cache key strips the hash sections, this should be a cache hit. | 2874 // Since the cache key strips the hash sections, this should be a cache hit. |
| 2875 std::string url_with_hash = std::string(trans.url) + "#multiple#hashes"; | 2875 std::string url_with_hash = std::string(trans.url) + "#multiple#hashes"; |
| 2876 trans.url = url_with_hash.c_str(); | 2876 trans.url = url_with_hash.c_str(); |
| 2877 trans.load_flags = LOAD_ONLY_FROM_CACHE; | 2877 trans.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 2878 | 2878 |
| 2879 RunTransactionTest(cache.http_cache(), trans); | 2879 RunTransactionTest(cache.http_cache(), trans); |
| 2880 | 2880 |
| 2881 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2881 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2882 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 2882 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2883 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2883 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2884 } | 2884 } |
| 2885 | 2885 |
| 2886 // Tests that we skip the cache for POST requests that do not have an upload | 2886 // Tests that we skip the cache for POST requests that do not have an upload |
| 2887 // identifier. | 2887 // identifier. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 2904 | 2904 |
| 2905 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2905 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2906 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2906 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2907 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 2907 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 2908 } | 2908 } |
| 2909 | 2909 |
| 2910 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) { | 2910 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) { |
| 2911 MockHttpCache cache; | 2911 MockHttpCache cache; |
| 2912 | 2912 |
| 2913 MockTransaction transaction(kSimplePOST_Transaction); | 2913 MockTransaction transaction(kSimplePOST_Transaction); |
| 2914 transaction.load_flags |= LOAD_ONLY_FROM_CACHE; | 2914 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 2915 | 2915 |
| 2916 MockHttpRequest request(transaction); | 2916 MockHttpRequest request(transaction); |
| 2917 TestCompletionCallback callback; | 2917 TestCompletionCallback callback; |
| 2918 | 2918 |
| 2919 std::unique_ptr<HttpTransaction> trans; | 2919 std::unique_ptr<HttpTransaction> trans; |
| 2920 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 2920 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); |
| 2921 ASSERT_TRUE(trans.get()); | 2921 ASSERT_TRUE(trans.get()); |
| 2922 | 2922 |
| 2923 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 2923 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 2924 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS)); | 2924 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 2948 request.upload_data_stream = &upload_data_stream; | 2948 request.upload_data_stream = &upload_data_stream; |
| 2949 | 2949 |
| 2950 // Populate the cache. | 2950 // Populate the cache. |
| 2951 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); | 2951 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
| 2952 | 2952 |
| 2953 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2953 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2954 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2954 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2955 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2955 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2956 | 2956 |
| 2957 // Load from cache. | 2957 // Load from cache. |
| 2958 request.load_flags |= LOAD_ONLY_FROM_CACHE; | 2958 request.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 2959 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); | 2959 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
| 2960 | 2960 |
| 2961 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2961 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2962 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 2962 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2963 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2963 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2964 } | 2964 } |
| 2965 | 2965 |
| 2966 // Test that we don't hit the cache for POST requests if there is a byte range. | 2966 // Test that we don't hit the cache for POST requests if there is a byte range. |
| 2967 TEST(HttpCache, SimplePOST_WithRanges) { | 2967 TEST(HttpCache, SimplePOST_WithRanges) { |
| 2968 MockHttpCache cache; | 2968 MockHttpCache cache; |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3158 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 3158 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3159 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 3159 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3160 RemoveMockTransaction(&transaction); | 3160 RemoveMockTransaction(&transaction); |
| 3161 } | 3161 } |
| 3162 | 3162 |
| 3163 // Tests that a HEAD request is not cached by itself. | 3163 // Tests that a HEAD request is not cached by itself. |
| 3164 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Miss) { | 3164 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Miss) { |
| 3165 MockHttpCache cache; | 3165 MockHttpCache cache; |
| 3166 MockTransaction transaction(kSimplePOST_Transaction); | 3166 MockTransaction transaction(kSimplePOST_Transaction); |
| 3167 AddMockTransaction(&transaction); | 3167 AddMockTransaction(&transaction); |
| 3168 transaction.load_flags |= LOAD_ONLY_FROM_CACHE; | 3168 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 3169 transaction.method = "HEAD"; | 3169 transaction.method = "HEAD"; |
| 3170 | 3170 |
| 3171 MockHttpRequest request(transaction); | 3171 MockHttpRequest request(transaction); |
| 3172 TestCompletionCallback callback; | 3172 TestCompletionCallback callback; |
| 3173 | 3173 |
| 3174 std::unique_ptr<HttpTransaction> trans; | 3174 std::unique_ptr<HttpTransaction> trans; |
| 3175 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 3175 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); |
| 3176 ASSERT_TRUE(trans.get()); | 3176 ASSERT_TRUE(trans.get()); |
| 3177 | 3177 |
| 3178 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 3178 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 3194 | 3194 |
| 3195 // Populate the cache. | 3195 // Populate the cache. |
| 3196 RunTransactionTest(cache.http_cache(), transaction); | 3196 RunTransactionTest(cache.http_cache(), transaction); |
| 3197 | 3197 |
| 3198 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3198 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3199 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3199 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3200 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3200 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3201 | 3201 |
| 3202 // Load from cache. | 3202 // Load from cache. |
| 3203 transaction.method = "HEAD"; | 3203 transaction.method = "HEAD"; |
| 3204 transaction.load_flags |= LOAD_ONLY_FROM_CACHE; | 3204 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 3205 transaction.data = ""; | 3205 transaction.data = ""; |
| 3206 RunTransactionTest(cache.http_cache(), transaction); | 3206 RunTransactionTest(cache.http_cache(), transaction); |
| 3207 | 3207 |
| 3208 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3208 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3209 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 3209 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3210 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3210 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3211 RemoveMockTransaction(&transaction); | 3211 RemoveMockTransaction(&transaction); |
| 3212 } | 3212 } |
| 3213 | 3213 |
| 3214 // Tests that a read-only request served from the cache preserves CL. | 3214 // Tests that a read-only request served from the cache preserves CL. |
| 3215 TEST(HttpCache, SimpleHEAD_ContentLengthOnHit_Read) { | 3215 TEST(HttpCache, SimpleHEAD_ContentLengthOnHit_Read) { |
| 3216 MockHttpCache cache; | 3216 MockHttpCache cache; |
| 3217 MockTransaction transaction(kSimpleGET_Transaction); | 3217 MockTransaction transaction(kSimpleGET_Transaction); |
| 3218 AddMockTransaction(&transaction); | 3218 AddMockTransaction(&transaction); |
| 3219 transaction.response_headers = "Content-Length: 42\n"; | 3219 transaction.response_headers = "Content-Length: 42\n"; |
| 3220 | 3220 |
| 3221 // Populate the cache. | 3221 // Populate the cache. |
| 3222 RunTransactionTest(cache.http_cache(), transaction); | 3222 RunTransactionTest(cache.http_cache(), transaction); |
| 3223 | 3223 |
| 3224 // Load from cache. | 3224 // Load from cache. |
| 3225 transaction.method = "HEAD"; | 3225 transaction.method = "HEAD"; |
| 3226 transaction.load_flags |= LOAD_ONLY_FROM_CACHE; | 3226 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 3227 transaction.data = ""; | 3227 transaction.data = ""; |
| 3228 std::string headers; | 3228 std::string headers; |
| 3229 | 3229 |
| 3230 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | 3230 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3231 | 3231 |
| 3232 EXPECT_EQ("HTTP/1.1 200 OK\nContent-Length: 42\n", headers); | 3232 EXPECT_EQ("HTTP/1.1 200 OK\nContent-Length: 42\n", headers); |
| 3233 RemoveMockTransaction(&transaction); | 3233 RemoveMockTransaction(&transaction); |
| 3234 } | 3234 } |
| 3235 | 3235 |
| 3236 // Tests that a read-write request served from the cache preserves CL. | 3236 // Tests that a read-write request served from the cache preserves CL. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 3261 MockHttpCache cache; | 3261 MockHttpCache cache; |
| 3262 MockTransaction transaction(kSimpleGET_Transaction); | 3262 MockTransaction transaction(kSimpleGET_Transaction); |
| 3263 AddMockTransaction(&transaction); | 3263 AddMockTransaction(&transaction); |
| 3264 | 3264 |
| 3265 // Populate the cache. | 3265 // Populate the cache. |
| 3266 RunTransactionTest(cache.http_cache(), transaction); | 3266 RunTransactionTest(cache.http_cache(), transaction); |
| 3267 | 3267 |
| 3268 // Load from cache. | 3268 // Load from cache. |
| 3269 transaction.method = "HEAD"; | 3269 transaction.method = "HEAD"; |
| 3270 transaction.request_headers = "Range: bytes = 0-4\r\n"; | 3270 transaction.request_headers = "Range: bytes = 0-4\r\n"; |
| 3271 transaction.load_flags |= LOAD_ONLY_FROM_CACHE; | 3271 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 3272 transaction.return_code = ERR_CACHE_MISS; | 3272 transaction.return_code = ERR_CACHE_MISS; |
| 3273 RunTransactionTest(cache.http_cache(), transaction); | 3273 RunTransactionTest(cache.http_cache(), transaction); |
| 3274 | 3274 |
| 3275 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3275 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3276 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3276 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3277 RemoveMockTransaction(&transaction); | 3277 RemoveMockTransaction(&transaction); |
| 3278 } | 3278 } |
| 3279 | 3279 |
| 3280 // Tests that a HEAD request can be served from a partialy cached resource. | 3280 // Tests that a HEAD request can be served from a partialy cached resource. |
| 3281 TEST(HttpCache, SimpleHEAD_WithCachedRanges) { | 3281 TEST(HttpCache, SimpleHEAD_WithCachedRanges) { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3360 EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n")); | 3360 EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n")); |
| 3361 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3361 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3362 | 3362 |
| 3363 MockTransaction transaction2(kTypicalGET_Transaction); | 3363 MockTransaction transaction2(kTypicalGET_Transaction); |
| 3364 AddMockTransaction(&transaction2); | 3364 AddMockTransaction(&transaction2); |
| 3365 | 3365 |
| 3366 // Make sure we are done with the previous transaction. | 3366 // Make sure we are done with the previous transaction. |
| 3367 base::RunLoop().RunUntilIdle(); | 3367 base::RunLoop().RunUntilIdle(); |
| 3368 | 3368 |
| 3369 // Load from the cache. | 3369 // Load from the cache. |
| 3370 transaction2.load_flags |= LOAD_ONLY_FROM_CACHE; | 3370 transaction2.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 3371 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); | 3371 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
| 3372 | 3372 |
| 3373 EXPECT_NE(std::string::npos, headers.find("Foo: bar\n")); | 3373 EXPECT_NE(std::string::npos, headers.find("Foo: bar\n")); |
| 3374 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3374 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3375 EXPECT_EQ(2, cache.disk_cache()->open_count()); | 3375 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 3376 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3376 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3377 RemoveMockTransaction(&transaction2); | 3377 RemoveMockTransaction(&transaction2); |
| 3378 } | 3378 } |
| 3379 | 3379 |
| 3380 // Tests that an externally conditionalized HEAD request updates the cache. | 3380 // Tests that an externally conditionalized HEAD request updates the cache. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 3400 EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 304 Not Modified\n")); | 3400 EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 304 Not Modified\n")); |
| 3401 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3401 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3402 | 3402 |
| 3403 MockTransaction transaction2(kTypicalGET_Transaction); | 3403 MockTransaction transaction2(kTypicalGET_Transaction); |
| 3404 AddMockTransaction(&transaction2); | 3404 AddMockTransaction(&transaction2); |
| 3405 | 3405 |
| 3406 // Make sure we are done with the previous transaction. | 3406 // Make sure we are done with the previous transaction. |
| 3407 base::RunLoop().RunUntilIdle(); | 3407 base::RunLoop().RunUntilIdle(); |
| 3408 | 3408 |
| 3409 // Load from the cache. | 3409 // Load from the cache. |
| 3410 transaction2.load_flags |= LOAD_ONLY_FROM_CACHE; | 3410 transaction2.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 3411 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); | 3411 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
| 3412 | 3412 |
| 3413 EXPECT_NE(std::string::npos, headers.find("Foo: bar\n")); | 3413 EXPECT_NE(std::string::npos, headers.find("Foo: bar\n")); |
| 3414 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3414 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3415 EXPECT_EQ(2, cache.disk_cache()->open_count()); | 3415 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 3416 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3416 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3417 RemoveMockTransaction(&transaction2); | 3417 RemoveMockTransaction(&transaction2); |
| 3418 } | 3418 } |
| 3419 | 3419 |
| 3420 // Tests that a HEAD request invalidates an old cached entry. | 3420 // Tests that a HEAD request invalidates an old cached entry. |
| 3421 TEST(HttpCache, SimpleHEAD_InvalidatesEntry) { | 3421 TEST(HttpCache, SimpleHEAD_InvalidatesEntry) { |
| 3422 MockHttpCache cache; | 3422 MockHttpCache cache; |
| 3423 MockTransaction transaction(kTypicalGET_Transaction); | 3423 MockTransaction transaction(kTypicalGET_Transaction); |
| 3424 AddMockTransaction(&transaction); | 3424 AddMockTransaction(&transaction); |
| 3425 | 3425 |
| 3426 // Populate the cache. | 3426 // Populate the cache. |
| 3427 RunTransactionTest(cache.http_cache(), transaction); | 3427 RunTransactionTest(cache.http_cache(), transaction); |
| 3428 | 3428 |
| 3429 // Update the cache. | 3429 // Update the cache. |
| 3430 transaction.method = "HEAD"; | 3430 transaction.method = "HEAD"; |
| 3431 transaction.data = ""; | 3431 transaction.data = ""; |
| 3432 RunTransactionTest(cache.http_cache(), transaction); | 3432 RunTransactionTest(cache.http_cache(), transaction); |
| 3433 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3433 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3434 | 3434 |
| 3435 // Load from the cache. | 3435 // Load from the cache. |
| 3436 transaction.method = "GET"; | 3436 transaction.method = "GET"; |
| 3437 transaction.load_flags |= LOAD_ONLY_FROM_CACHE; | 3437 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 3438 transaction.return_code = ERR_CACHE_MISS; | 3438 transaction.return_code = ERR_CACHE_MISS; |
| 3439 RunTransactionTest(cache.http_cache(), transaction); | 3439 RunTransactionTest(cache.http_cache(), transaction); |
| 3440 | 3440 |
| 3441 RemoveMockTransaction(&transaction); | 3441 RemoveMockTransaction(&transaction); |
| 3442 } | 3442 } |
| 3443 | 3443 |
| 3444 // Tests that we do not cache the response of a PUT. | 3444 // Tests that we do not cache the response of a PUT. |
| 3445 TEST(HttpCache, SimplePUT_Miss) { | 3445 TEST(HttpCache, SimplePUT_Miss) { |
| 3446 MockHttpCache cache; | 3446 MockHttpCache cache; |
| 3447 | 3447 |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3713 // Fail the network request. | 3713 // Fail the network request. |
| 3714 MockTransaction transaction(kSimpleGET_Transaction); | 3714 MockTransaction transaction(kSimpleGET_Transaction); |
| 3715 transaction.return_code = ERR_FAILED; | 3715 transaction.return_code = ERR_FAILED; |
| 3716 transaction.load_flags |= LOAD_VALIDATE_CACHE; | 3716 transaction.load_flags |= LOAD_VALIDATE_CACHE; |
| 3717 | 3717 |
| 3718 AddMockTransaction(&transaction); | 3718 AddMockTransaction(&transaction); |
| 3719 RunTransactionTest(cache.http_cache(), transaction); | 3719 RunTransactionTest(cache.http_cache(), transaction); |
| 3720 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3720 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3721 RemoveMockTransaction(&transaction); | 3721 RemoveMockTransaction(&transaction); |
| 3722 | 3722 |
| 3723 transaction.load_flags = LOAD_ONLY_FROM_CACHE; | 3723 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 3724 transaction.return_code = OK; | 3724 transaction.return_code = OK; |
| 3725 AddMockTransaction(&transaction); | 3725 AddMockTransaction(&transaction); |
| 3726 RunTransactionTest(cache.http_cache(), transaction); | 3726 RunTransactionTest(cache.http_cache(), transaction); |
| 3727 | 3727 |
| 3728 // Make sure the transaction didn't reach the network. | 3728 // Make sure the transaction didn't reach the network. |
| 3729 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3729 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3730 RemoveMockTransaction(&transaction); | 3730 RemoveMockTransaction(&transaction); |
| 3731 } | 3731 } |
| 3732 | 3732 |
| 3733 TEST(HttpCache, RangeGET_SkipsCache) { | 3733 TEST(HttpCache, RangeGET_SkipsCache) { |
| (...skipping 1315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5049 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 5049 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5050 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5050 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5051 | 5051 |
| 5052 // The last transaction has finished so make sure the entry is deactivated. | 5052 // The last transaction has finished so make sure the entry is deactivated. |
| 5053 base::RunLoop().RunUntilIdle(); | 5053 base::RunLoop().RunUntilIdle(); |
| 5054 | 5054 |
| 5055 // Make a request for an invalid range. | 5055 // Make a request for an invalid range. |
| 5056 MockTransaction transaction3(kRangeGET_TransactionOK); | 5056 MockTransaction transaction3(kRangeGET_TransactionOK); |
| 5057 transaction3.request_headers = "Range: bytes = 80-90\r\n" EXTRA_HEADER; | 5057 transaction3.request_headers = "Range: bytes = 80-90\r\n" EXTRA_HEADER; |
| 5058 transaction3.data = transaction.data; | 5058 transaction3.data = transaction.data; |
| 5059 transaction3.load_flags = LOAD_PREFERRING_CACHE; | 5059 transaction3.load_flags = LOAD_SKIP_CACHE_VALIDATION; |
| 5060 RunTransactionTestWithResponse(cache.http_cache(), transaction3, &headers); | 5060 RunTransactionTestWithResponse(cache.http_cache(), transaction3, &headers); |
| 5061 EXPECT_EQ(2, cache.disk_cache()->open_count()); | 5061 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 5062 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 ")); | 5062 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 ")); |
| 5063 EXPECT_EQ(std::string::npos, headers.find("Content-Range:")); | 5063 EXPECT_EQ(std::string::npos, headers.find("Content-Range:")); |
| 5064 EXPECT_EQ(std::string::npos, headers.find("Content-Length: 80")); | 5064 EXPECT_EQ(std::string::npos, headers.find("Content-Length: 80")); |
| 5065 | 5065 |
| 5066 // Make sure the entry is deactivated. | 5066 // Make sure the entry is deactivated. |
| 5067 base::RunLoop().RunUntilIdle(); | 5067 base::RunLoop().RunUntilIdle(); |
| 5068 | 5068 |
| 5069 // Even though the request was invalid, we should have the entry. | 5069 // Even though the request was invalid, we should have the entry. |
| (...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5554 AddMockTransaction(&kRangeGET_TransactionOK); | 5554 AddMockTransaction(&kRangeGET_TransactionOK); |
| 5555 | 5555 |
| 5556 // Write to the cache (40-49). | 5556 // Write to the cache (40-49). |
| 5557 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | 5557 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 5558 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 5558 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5559 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 5559 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5560 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5560 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5561 | 5561 |
| 5562 // Force this transaction to read from the cache. | 5562 // Force this transaction to read from the cache. |
| 5563 MockTransaction transaction(kRangeGET_TransactionOK); | 5563 MockTransaction transaction(kRangeGET_TransactionOK); |
| 5564 transaction.load_flags |= LOAD_ONLY_FROM_CACHE; | 5564 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 5565 | 5565 |
| 5566 MockHttpRequest request(transaction); | 5566 MockHttpRequest request(transaction); |
| 5567 TestCompletionCallback callback; | 5567 TestCompletionCallback callback; |
| 5568 | 5568 |
| 5569 std::unique_ptr<HttpTransaction> trans; | 5569 std::unique_ptr<HttpTransaction> trans; |
| 5570 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); | 5570 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); |
| 5571 EXPECT_THAT(rv, IsOk()); | 5571 EXPECT_THAT(rv, IsOk()); |
| 5572 ASSERT_TRUE(trans.get()); | 5572 ASSERT_TRUE(trans.get()); |
| 5573 | 5573 |
| 5574 rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 5574 rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| (...skipping 715 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6290 MockHttpRequest r1(transaction), | 6290 MockHttpRequest r1(transaction), |
| 6291 r2(transaction), | 6291 r2(transaction), |
| 6292 r3(transaction); | 6292 r3(transaction); |
| 6293 | 6293 |
| 6294 TestTransactionConsumer c1(DEFAULT_PRIORITY, cache.http_cache()), | 6294 TestTransactionConsumer c1(DEFAULT_PRIORITY, cache.http_cache()), |
| 6295 c2(DEFAULT_PRIORITY, cache.http_cache()), | 6295 c2(DEFAULT_PRIORITY, cache.http_cache()), |
| 6296 c3(DEFAULT_PRIORITY, cache.http_cache()); | 6296 c3(DEFAULT_PRIORITY, cache.http_cache()); |
| 6297 | 6297 |
| 6298 c1.Start(&r1, NetLogWithSource()); | 6298 c1.Start(&r1, NetLogWithSource()); |
| 6299 | 6299 |
| 6300 r2.load_flags |= LOAD_ONLY_FROM_CACHE; | 6300 r2.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 6301 c2.Start(&r2, NetLogWithSource()); | 6301 c2.Start(&r2, NetLogWithSource()); |
| 6302 | 6302 |
| 6303 r3.load_flags |= LOAD_ONLY_FROM_CACHE; | 6303 r3.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 6304 c3.Start(&r3, NetLogWithSource()); | 6304 c3.Start(&r3, NetLogWithSource()); |
| 6305 | 6305 |
| 6306 base::RunLoop().Run(); | 6306 base::RunLoop().Run(); |
| 6307 | 6307 |
| 6308 EXPECT_TRUE(c1.is_done()); | 6308 EXPECT_TRUE(c1.is_done()); |
| 6309 EXPECT_TRUE(c2.is_done()); | 6309 EXPECT_TRUE(c2.is_done()); |
| 6310 EXPECT_TRUE(c3.is_done()); | 6310 EXPECT_TRUE(c3.is_done()); |
| 6311 | 6311 |
| 6312 EXPECT_THAT(c1.error(), IsOk()); | 6312 EXPECT_THAT(c1.error(), IsOk()); |
| 6313 EXPECT_THAT(c2.error(), IsOk()); | 6313 EXPECT_THAT(c2.error(), IsOk()); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6427 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 6427 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6428 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6428 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6429 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6429 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6430 | 6430 |
| 6431 disk_cache::Entry* entry; | 6431 disk_cache::Entry* entry; |
| 6432 EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry)); | 6432 EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry)); |
| 6433 entry->Close(); | 6433 entry->Close(); |
| 6434 } | 6434 } |
| 6435 | 6435 |
| 6436 // Verify that no-cache resources are stored in cache and fetched from cache | 6436 // Verify that no-cache resources are stored in cache and fetched from cache |
| 6437 // when the LOAD_PREFERRING_CACHE flag is set. | 6437 // when the LOAD_SKIP_CACHE_VALIDATION flag is set. |
| 6438 TEST(HttpCache, CacheControlNoCacheHistoryLoad) { | 6438 TEST(HttpCache, CacheControlNoCacheHistoryLoad) { |
| 6439 MockHttpCache cache; | 6439 MockHttpCache cache; |
| 6440 | 6440 |
| 6441 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 6441 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 6442 transaction.response_headers = "cache-control: no-cache\n"; | 6442 transaction.response_headers = "cache-control: no-cache\n"; |
| 6443 | 6443 |
| 6444 // Initial load. | 6444 // Initial load. |
| 6445 RunTransactionTest(cache.http_cache(), transaction); | 6445 RunTransactionTest(cache.http_cache(), transaction); |
| 6446 | 6446 |
| 6447 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6447 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6448 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 6448 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 6449 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6449 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6450 | 6450 |
| 6451 // Try loading again with LOAD_PREFERRING_CACHE. | 6451 // Try loading again with LOAD_SKIP_CACHE_VALIDATION. |
| 6452 transaction.load_flags = LOAD_PREFERRING_CACHE; | 6452 transaction.load_flags = LOAD_SKIP_CACHE_VALIDATION; |
| 6453 RunTransactionTest(cache.http_cache(), transaction); | 6453 RunTransactionTest(cache.http_cache(), transaction); |
| 6454 | 6454 |
| 6455 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6455 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6456 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6456 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6457 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6457 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6458 | 6458 |
| 6459 disk_cache::Entry* entry; | 6459 disk_cache::Entry* entry; |
| 6460 EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry)); | 6460 EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry)); |
| 6461 entry->Close(); | 6461 entry->Close(); |
| 6462 } | 6462 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6547 MockHttpCache cache; | 6547 MockHttpCache cache; |
| 6548 | 6548 |
| 6549 MockTransaction transaction = kSimpleGET_Transaction; | 6549 MockTransaction transaction = kSimpleGET_Transaction; |
| 6550 transaction.cert_status = CERT_STATUS_REVOKED; | 6550 transaction.cert_status = CERT_STATUS_REVOKED; |
| 6551 ScopedMockTransaction scoped_transaction(transaction); | 6551 ScopedMockTransaction scoped_transaction(transaction); |
| 6552 | 6552 |
| 6553 // write to the cache | 6553 // write to the cache |
| 6554 RunTransactionTest(cache.http_cache(), transaction); | 6554 RunTransactionTest(cache.http_cache(), transaction); |
| 6555 | 6555 |
| 6556 // Test that it was not cached. | 6556 // Test that it was not cached. |
| 6557 transaction.load_flags |= LOAD_ONLY_FROM_CACHE; | 6557 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 6558 | 6558 |
| 6559 MockHttpRequest request(transaction); | 6559 MockHttpRequest request(transaction); |
| 6560 TestCompletionCallback callback; | 6560 TestCompletionCallback callback; |
| 6561 | 6561 |
| 6562 std::unique_ptr<HttpTransaction> trans; | 6562 std::unique_ptr<HttpTransaction> trans; |
| 6563 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 6563 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); |
| 6564 | 6564 |
| 6565 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 6565 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 6566 if (rv == ERR_IO_PENDING) | 6566 if (rv == ERR_IO_PENDING) |
| 6567 rv = callback.WaitForResult(); | 6567 rv = callback.WaitForResult(); |
| (...skipping 169 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 only return valid entries with LOAD_ONLY_FROM_CACHE |
| 6748 // transactions unless LOAD_SKIP_CACHE_VALIDATION is set. |
| 6749 TEST(HttpCache, ValidLoadOnlyFromCache) { |
| 6750 MockHttpCache cache; |
| 6751 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
| 6752 cache.http_cache()->SetClockForTesting(base::WrapUnique(clock)); |
| 6753 cache.network_layer()->SetClock(clock); |
| 6754 |
| 6755 // Write a resource that will expire in 100 seconds. |
| 6756 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 6757 transaction.response_headers = "Cache-Control: max-age=100\n"; |
| 6758 RunTransactionTest(cache.http_cache(), transaction); |
| 6759 |
| 6760 // Move forward in time such that the cached response is no longer valid. |
| 6761 clock->Advance(base::TimeDelta::FromSeconds(101)); |
| 6762 |
| 6763 // Skipping cache validation should still return a response. |
| 6764 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 6765 RunTransactionTest(cache.http_cache(), transaction); |
| 6766 |
| 6767 // If the cache entry is checked for validitiy, it should fail. |
| 6768 transaction.load_flags = LOAD_ONLY_FROM_CACHE; |
| 6769 transaction.return_code = ERR_CACHE_MISS; |
| 6770 RunTransactionTest(cache.http_cache(), transaction); |
| 6771 } |
| 6772 |
| 6747 // Tests that we can read metadata after validating the entry and with READ mode | 6773 // Tests that we can read metadata after validating the entry and with READ mode |
| 6748 // transactions. | 6774 // transactions. |
| 6749 TEST(HttpCache, ReadMetadata) { | 6775 TEST(HttpCache, ReadMetadata) { |
| 6750 MockHttpCache cache; | 6776 MockHttpCache cache; |
| 6751 | 6777 |
| 6752 // Write to the cache | 6778 // Write to the cache |
| 6753 HttpResponseInfo response; | 6779 HttpResponseInfo response; |
| 6754 RunTransactionTestWithResponseInfo(cache.http_cache(), | 6780 RunTransactionTestWithResponseInfo(cache.http_cache(), |
| 6755 kTypicalGET_Transaction, &response); | 6781 kTypicalGET_Transaction, &response); |
| 6756 EXPECT_TRUE(response.metadata.get() == NULL); | 6782 EXPECT_TRUE(response.metadata.get() == NULL); |
| 6757 | 6783 |
| 6758 // Write meta data to the same entry. | 6784 // Write meta data to the same entry. |
| 6759 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50)); | 6785 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50)); |
| 6760 memset(buf->data(), 0, buf->size()); | 6786 memset(buf->data(), 0, buf->size()); |
| 6761 base::strlcpy(buf->data(), "Hi there", buf->size()); | 6787 base::strlcpy(buf->data(), "Hi there", buf->size()); |
| 6762 cache.http_cache()->WriteMetadata(GURL(kTypicalGET_Transaction.url), | 6788 cache.http_cache()->WriteMetadata(GURL(kTypicalGET_Transaction.url), |
| 6763 DEFAULT_PRIORITY, response.response_time, | 6789 DEFAULT_PRIORITY, response.response_time, |
| 6764 buf.get(), buf->size()); | 6790 buf.get(), buf->size()); |
| 6765 | 6791 |
| 6766 // Makes sure we finish pending operations. | 6792 // Makes sure we finish pending operations. |
| 6767 base::RunLoop().RunUntilIdle(); | 6793 base::RunLoop().RunUntilIdle(); |
| 6768 | 6794 |
| 6769 // Start with a READ mode transaction. | 6795 // Start with a READ mode transaction. |
| 6770 MockTransaction trans1(kTypicalGET_Transaction); | 6796 MockTransaction trans1(kTypicalGET_Transaction); |
| 6771 trans1.load_flags = LOAD_ONLY_FROM_CACHE; | 6797 trans1.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 6772 | 6798 |
| 6773 RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response); | 6799 RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response); |
| 6774 ASSERT_TRUE(response.metadata.get() != NULL); | 6800 ASSERT_TRUE(response.metadata.get() != NULL); |
| 6775 EXPECT_EQ(50, response.metadata->size()); | 6801 EXPECT_EQ(50, response.metadata->size()); |
| 6776 EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there")); | 6802 EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there")); |
| 6777 | 6803 |
| 6778 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6804 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6779 EXPECT_EQ(2, cache.disk_cache()->open_count()); | 6805 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 6780 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6806 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6781 base::RunLoop().RunUntilIdle(); | 6807 base::RunLoop().RunUntilIdle(); |
| (...skipping 860 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7642 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceOnly) { | 7668 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceOnly) { |
| 7643 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); | 7669 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
| 7644 AdvanceTime(kRequireValidationSecs); | 7670 AdvanceTime(kRequireValidationSecs); |
| 7645 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL)); | 7671 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL)); |
| 7646 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL)); | 7672 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL)); |
| 7647 } | 7673 } |
| 7648 | 7674 |
| 7649 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceReadOnly) { | 7675 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceReadOnly) { |
| 7650 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); | 7676 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
| 7651 AdvanceTime(kRequireValidationSecs); | 7677 AdvanceTime(kRequireValidationSecs); |
| 7652 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_ONLY_FROM_CACHE)); | 7678 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_ONLY_FROM_CACHE | |
| 7679 LOAD_SKIP_CACHE_VALIDATION)); |
| 7653 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL)); | 7680 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL)); |
| 7654 } | 7681 } |
| 7655 | 7682 |
| 7656 TEST_F(HttpCachePrefetchValidationTest, BypassCacheOverwritesPrefetch) { | 7683 TEST_F(HttpCachePrefetchValidationTest, BypassCacheOverwritesPrefetch) { |
| 7657 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); | 7684 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
| 7658 AdvanceTime(kRequireValidationSecs); | 7685 AdvanceTime(kRequireValidationSecs); |
| 7659 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_BYPASS_CACHE)); | 7686 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_BYPASS_CACHE)); |
| 7660 AdvanceTime(kRequireValidationSecs); | 7687 AdvanceTime(kRequireValidationSecs); |
| 7661 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL)); | 7688 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL)); |
| 7662 } | 7689 } |
| (...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8102 RunTransactionTestWithResponseInfo(cache.http_cache(), | 8129 RunTransactionTestWithResponseInfo(cache.http_cache(), |
| 8103 kTypicalGET_Transaction, &response_info); | 8130 kTypicalGET_Transaction, &response_info); |
| 8104 | 8131 |
| 8105 EXPECT_FALSE(response_info.was_cached); | 8132 EXPECT_FALSE(response_info.was_cached); |
| 8106 EXPECT_TRUE(response_info.network_accessed); | 8133 EXPECT_TRUE(response_info.network_accessed); |
| 8107 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, | 8134 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, |
| 8108 response_info.cache_entry_status); | 8135 response_info.cache_entry_status); |
| 8109 } | 8136 } |
| 8110 | 8137 |
| 8111 } // namespace net | 8138 } // namespace net |
| OLD | NEW |