Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: net/http/http_cache_unittest.cc

Issue 2668563002: Change MockHttpCache constructor to take boolean to set quic server info factory (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 621 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 return false; 632 return false;
633 } 633 }
634 634
635 } // namespace 635 } // namespace
636 636
637 637
638 //----------------------------------------------------------------------------- 638 //-----------------------------------------------------------------------------
639 // Tests. 639 // Tests.
640 640
641 TEST(HttpCache, CreateThenDestroy) { 641 TEST(HttpCache, CreateThenDestroy) {
642 MockHttpCache cache; 642 MockHttpCache cache(false);
643 643
644 std::unique_ptr<HttpTransaction> trans; 644 std::unique_ptr<HttpTransaction> trans;
645 EXPECT_THAT(cache.CreateTransaction(&trans), IsOk()); 645 EXPECT_THAT(cache.CreateTransaction(&trans), IsOk());
646 ASSERT_TRUE(trans.get()); 646 ASSERT_TRUE(trans.get());
647 } 647 }
648 648
649 TEST(HttpCache, GetBackend) { 649 TEST(HttpCache, GetBackend) {
650 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(0)); 650 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(0), false);
651 651
652 disk_cache::Backend* backend; 652 disk_cache::Backend* backend;
653 TestCompletionCallback cb; 653 TestCompletionCallback cb;
654 // This will lazily initialize the backend. 654 // This will lazily initialize the backend.
655 int rv = cache.http_cache()->GetBackend(&backend, cb.callback()); 655 int rv = cache.http_cache()->GetBackend(&backend, cb.callback());
656 EXPECT_THAT(cb.GetResult(rv), IsOk()); 656 EXPECT_THAT(cb.GetResult(rv), IsOk());
657 } 657 }
658 658
659 TEST(HttpCache, SimpleGET) { 659 TEST(HttpCache, SimpleGET) {
660 MockHttpCache cache; 660 MockHttpCache cache(false);
661 BoundTestNetLog log; 661 BoundTestNetLog log;
662 LoadTimingInfo load_timing_info; 662 LoadTimingInfo load_timing_info;
663 663
664 // Write to the cache. 664 // Write to the cache.
665 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction, 665 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction,
666 log.bound(), &load_timing_info); 666 log.bound(), &load_timing_info);
667 667
668 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 668 EXPECT_EQ(1, cache.network_layer()->transaction_count());
669 EXPECT_EQ(0, cache.disk_cache()->open_count()); 669 EXPECT_EQ(0, cache.disk_cache()->open_count());
670 EXPECT_EQ(1, cache.disk_cache()->create_count()); 670 EXPECT_EQ(1, cache.disk_cache()->create_count());
671 TestLoadTimingNetworkRequest(load_timing_info); 671 TestLoadTimingNetworkRequest(load_timing_info);
672 } 672 }
673 673
674 TEST(HttpCache, SimpleGETNoDiskCache) { 674 TEST(HttpCache, SimpleGETNoDiskCache) {
675 MockHttpCache cache; 675 MockHttpCache cache(false);
676 676
677 cache.disk_cache()->set_fail_requests(); 677 cache.disk_cache()->set_fail_requests();
678 678
679 BoundTestNetLog log; 679 BoundTestNetLog log;
680 LoadTimingInfo load_timing_info; 680 LoadTimingInfo load_timing_info;
681 681
682 // Read from the network, and don't use the cache. 682 // Read from the network, and don't use the cache.
683 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction, 683 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction,
684 log.bound(), &load_timing_info); 684 log.bound(), &load_timing_info);
685 685
(...skipping 22 matching lines...) Expand all
708 EXPECT_EQ(0, cache.disk_cache()->create_count()); 708 EXPECT_EQ(0, cache.disk_cache()->create_count());
709 TestLoadTimingNetworkRequest(load_timing_info); 709 TestLoadTimingNetworkRequest(load_timing_info);
710 } 710 }
711 711
712 TEST(HttpCache, SimpleGETNoDiskCache2) { 712 TEST(HttpCache, SimpleGETNoDiskCache2) {
713 // This will initialize a cache object with NULL backend. 713 // This will initialize a cache object with NULL backend.
714 std::unique_ptr<MockBlockingBackendFactory> factory( 714 std::unique_ptr<MockBlockingBackendFactory> factory(
715 new MockBlockingBackendFactory()); 715 new MockBlockingBackendFactory());
716 factory->set_fail(true); 716 factory->set_fail(true);
717 factory->FinishCreation(); // We'll complete synchronously. 717 factory->FinishCreation(); // We'll complete synchronously.
718 MockHttpCache cache(std::move(factory)); 718 MockHttpCache cache(std::move(factory), false);
719 719
720 // Read from the network, and don't use the cache. 720 // Read from the network, and don't use the cache.
721 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 721 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
722 722
723 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 723 EXPECT_EQ(1, cache.network_layer()->transaction_count());
724 EXPECT_FALSE(cache.http_cache()->GetCurrentBackend()); 724 EXPECT_FALSE(cache.http_cache()->GetCurrentBackend());
725 } 725 }
726 726
727 // Tests that IOBuffers are not referenced after IO completes. 727 // Tests that IOBuffers are not referenced after IO completes.
728 TEST(HttpCache, ReleaseBuffer) { 728 TEST(HttpCache, ReleaseBuffer) {
729 MockHttpCache cache; 729 MockHttpCache cache(false);
730 730
731 // Write to the cache. 731 // Write to the cache.
732 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 732 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
733 733
734 MockHttpRequest request(kSimpleGET_Transaction); 734 MockHttpRequest request(kSimpleGET_Transaction);
735 std::unique_ptr<HttpTransaction> trans; 735 std::unique_ptr<HttpTransaction> trans;
736 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 736 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
737 737
738 const int kBufferSize = 10; 738 const int kBufferSize = 10;
739 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); 739 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize));
740 ReleaseBufferCompletionCallback cb(buffer.get()); 740 ReleaseBufferCompletionCallback cb(buffer.get());
741 741
742 int rv = trans->Start(&request, cb.callback(), NetLogWithSource()); 742 int rv = trans->Start(&request, cb.callback(), NetLogWithSource());
743 EXPECT_THAT(cb.GetResult(rv), IsOk()); 743 EXPECT_THAT(cb.GetResult(rv), IsOk());
744 744
745 rv = trans->Read(buffer.get(), kBufferSize, cb.callback()); 745 rv = trans->Read(buffer.get(), kBufferSize, cb.callback());
746 EXPECT_EQ(kBufferSize, cb.GetResult(rv)); 746 EXPECT_EQ(kBufferSize, cb.GetResult(rv));
747 } 747 }
748 748
749 TEST(HttpCache, SimpleGETWithDiskFailures) { 749 TEST(HttpCache, SimpleGETWithDiskFailures) {
750 MockHttpCache cache; 750 MockHttpCache cache(false);
751 751
752 cache.disk_cache()->set_soft_failures(true); 752 cache.disk_cache()->set_soft_failures(true);
753 753
754 // Read from the network, and fail to write to the cache. 754 // Read from the network, and fail to write to the cache.
755 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 755 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
756 756
757 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 757 EXPECT_EQ(1, cache.network_layer()->transaction_count());
758 EXPECT_EQ(0, cache.disk_cache()->open_count()); 758 EXPECT_EQ(0, cache.disk_cache()->open_count());
759 EXPECT_EQ(1, cache.disk_cache()->create_count()); 759 EXPECT_EQ(1, cache.disk_cache()->create_count());
760 760
761 // This one should see an empty cache again. 761 // This one should see an empty cache again.
762 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 762 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
763 763
764 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 764 EXPECT_EQ(2, cache.network_layer()->transaction_count());
765 EXPECT_EQ(0, cache.disk_cache()->open_count()); 765 EXPECT_EQ(0, cache.disk_cache()->open_count());
766 EXPECT_EQ(2, cache.disk_cache()->create_count()); 766 EXPECT_EQ(2, cache.disk_cache()->create_count());
767 } 767 }
768 768
769 // Tests that disk failures after the transaction has started don't cause the 769 // Tests that disk failures after the transaction has started don't cause the
770 // request to fail. 770 // request to fail.
771 TEST(HttpCache, SimpleGETWithDiskFailures2) { 771 TEST(HttpCache, SimpleGETWithDiskFailures2) {
772 MockHttpCache cache; 772 MockHttpCache cache(false);
773 773
774 MockHttpRequest request(kSimpleGET_Transaction); 774 MockHttpRequest request(kSimpleGET_Transaction);
775 775
776 std::unique_ptr<Context> c(new Context()); 776 std::unique_ptr<Context> c(new Context());
777 int rv = cache.CreateTransaction(&c->trans); 777 int rv = cache.CreateTransaction(&c->trans);
778 ASSERT_THAT(rv, IsOk()); 778 ASSERT_THAT(rv, IsOk());
779 779
780 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); 780 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
781 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); 781 EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
782 rv = c->callback.WaitForResult(); 782 rv = c->callback.WaitForResult();
(...skipping 16 matching lines...) Expand all
799 // This one should see an empty cache again. 799 // This one should see an empty cache again.
800 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 800 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
801 801
802 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 802 EXPECT_EQ(2, cache.network_layer()->transaction_count());
803 EXPECT_EQ(1, cache.disk_cache()->open_count()); 803 EXPECT_EQ(1, cache.disk_cache()->open_count());
804 EXPECT_EQ(2, cache.disk_cache()->create_count()); 804 EXPECT_EQ(2, cache.disk_cache()->create_count());
805 } 805 }
806 806
807 // Tests that we handle failures to read from the cache. 807 // Tests that we handle failures to read from the cache.
808 TEST(HttpCache, SimpleGETWithDiskFailures3) { 808 TEST(HttpCache, SimpleGETWithDiskFailures3) {
809 MockHttpCache cache; 809 MockHttpCache cache(false);
810 810
811 // Read from the network, and write to the cache. 811 // Read from the network, and write to the cache.
812 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 812 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
813 813
814 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 814 EXPECT_EQ(1, cache.network_layer()->transaction_count());
815 EXPECT_EQ(0, cache.disk_cache()->open_count()); 815 EXPECT_EQ(0, cache.disk_cache()->open_count());
816 EXPECT_EQ(1, cache.disk_cache()->create_count()); 816 EXPECT_EQ(1, cache.disk_cache()->create_count());
817 817
818 cache.disk_cache()->set_soft_failures(true); 818 cache.disk_cache()->set_soft_failures(true);
819 819
(...skipping 14 matching lines...) Expand all
834 EXPECT_EQ(2, cache.disk_cache()->create_count()); 834 EXPECT_EQ(2, cache.disk_cache()->create_count());
835 835
836 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 836 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
837 837
838 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 838 EXPECT_EQ(3, cache.network_layer()->transaction_count());
839 EXPECT_EQ(1, cache.disk_cache()->open_count()); 839 EXPECT_EQ(1, cache.disk_cache()->open_count());
840 EXPECT_EQ(3, cache.disk_cache()->create_count()); 840 EXPECT_EQ(3, cache.disk_cache()->create_count());
841 } 841 }
842 842
843 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) { 843 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) {
844 MockHttpCache cache; 844 MockHttpCache cache(false);
845 845
846 BoundTestNetLog log; 846 BoundTestNetLog log;
847 LoadTimingInfo load_timing_info; 847 LoadTimingInfo load_timing_info;
848 848
849 // Write to the cache. 849 // Write to the cache.
850 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction, 850 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction,
851 log.bound(), &load_timing_info); 851 log.bound(), &load_timing_info);
852 852
853 // Check that the NetLog was filled as expected. 853 // Check that the NetLog was filled as expected.
854 TestNetLogEntry::List entries; 854 TestNetLogEntry::List entries;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
906 EXPECT_TRUE( 906 EXPECT_TRUE(
907 LogContainsEndEvent(entries, 7, NetLogEventType::HTTP_CACHE_READ_INFO)); 907 LogContainsEndEvent(entries, 7, NetLogEventType::HTTP_CACHE_READ_INFO));
908 908
909 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 909 EXPECT_EQ(1, cache.network_layer()->transaction_count());
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(false);
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 | LOAD_SKIP_CACHE_VALIDATION; 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(false);
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_SKIP_CACHE_VALIDATION; 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(false);
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_SKIP_CACHE_VALIDATION; 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_SKIP_CACHE_VALIDATION 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(false);
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_SKIP_CACHE_VALIDATION; 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_SKIP_CACHE_VALIDATION 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(false);
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_SKIP_CACHE_VALIDATION; 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);
1018 RemoveMockTransaction(&transaction); 1018 RemoveMockTransaction(&transaction);
1019 } 1019 }
1020 1020
1021 // Tests that was_cached was set properly on a failure, even if the cached 1021 // Tests that was_cached was set properly on a failure, even if the cached
1022 // response wasn't returned. 1022 // response wasn't returned.
1023 TEST(HttpCache, SimpleGET_CacheSignal_Failure) { 1023 TEST(HttpCache, SimpleGET_CacheSignal_Failure) {
1024 MockHttpCache cache; 1024 MockHttpCache cache(false);
1025 1025
1026 // Prime cache. 1026 // Prime cache.
1027 MockTransaction transaction(kSimpleGET_Transaction); 1027 MockTransaction transaction(kSimpleGET_Transaction);
1028 transaction.response_headers = "Cache-Control: no-cache\n"; 1028 transaction.response_headers = "Cache-Control: no-cache\n";
1029 1029
1030 AddMockTransaction(&transaction); 1030 AddMockTransaction(&transaction);
1031 RunTransactionTest(cache.http_cache(), transaction); 1031 RunTransactionTest(cache.http_cache(), transaction);
1032 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1032 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1033 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1033 EXPECT_EQ(1, cache.disk_cache()->create_count());
1034 RemoveMockTransaction(&transaction); 1034 RemoveMockTransaction(&transaction);
(...skipping 14 matching lines...) Expand all
1049 const HttpResponseInfo* response_info = trans->GetResponseInfo(); 1049 const HttpResponseInfo* response_info = trans->GetResponseInfo();
1050 ASSERT_TRUE(response_info); 1050 ASSERT_TRUE(response_info);
1051 EXPECT_TRUE(response_info->was_cached); 1051 EXPECT_TRUE(response_info->was_cached);
1052 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1052 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1053 1053
1054 RemoveMockTransaction(&transaction); 1054 RemoveMockTransaction(&transaction);
1055 } 1055 }
1056 1056
1057 // Confirm if we have an empty cache, a read is marked as network verified. 1057 // Confirm if we have an empty cache, a read is marked as network verified.
1058 TEST(HttpCache, SimpleGET_NetworkAccessed_Network) { 1058 TEST(HttpCache, SimpleGET_NetworkAccessed_Network) {
1059 MockHttpCache cache; 1059 MockHttpCache cache(false);
1060 1060
1061 // write to the cache 1061 // write to the cache
1062 HttpResponseInfo response_info; 1062 HttpResponseInfo response_info;
1063 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, 1063 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
1064 &response_info); 1064 &response_info);
1065 1065
1066 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1066 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1067 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1067 EXPECT_EQ(0, cache.disk_cache()->open_count());
1068 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1068 EXPECT_EQ(1, cache.disk_cache()->create_count());
1069 EXPECT_TRUE(response_info.network_accessed); 1069 EXPECT_TRUE(response_info.network_accessed);
1070 EXPECT_EQ(CacheEntryStatus::ENTRY_NOT_IN_CACHE, 1070 EXPECT_EQ(CacheEntryStatus::ENTRY_NOT_IN_CACHE,
1071 response_info.cache_entry_status); 1071 response_info.cache_entry_status);
1072 } 1072 }
1073 1073
1074 // Confirm if we have a fresh entry in cache, it isn't marked as 1074 // Confirm if we have a fresh entry in cache, it isn't marked as
1075 // network verified. 1075 // network verified.
1076 TEST(HttpCache, SimpleGET_NetworkAccessed_Cache) { 1076 TEST(HttpCache, SimpleGET_NetworkAccessed_Cache) {
1077 MockHttpCache cache; 1077 MockHttpCache cache(false);
1078 1078
1079 // Prime cache. 1079 // Prime cache.
1080 MockTransaction transaction(kSimpleGET_Transaction); 1080 MockTransaction transaction(kSimpleGET_Transaction);
1081 1081
1082 RunTransactionTest(cache.http_cache(), transaction); 1082 RunTransactionTest(cache.http_cache(), transaction);
1083 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1083 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1084 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1084 EXPECT_EQ(1, cache.disk_cache()->create_count());
1085 1085
1086 // Re-run transaction; make sure we don't mark the network as accessed. 1086 // Re-run transaction; make sure we don't mark the network as accessed.
1087 HttpResponseInfo response_info; 1087 HttpResponseInfo response_info;
1088 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, 1088 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
1089 &response_info); 1089 &response_info);
1090 1090
1091 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1091 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1092 EXPECT_FALSE(response_info.server_data_unavailable); 1092 EXPECT_FALSE(response_info.server_data_unavailable);
1093 EXPECT_FALSE(response_info.network_accessed); 1093 EXPECT_FALSE(response_info.network_accessed);
1094 EXPECT_EQ(CacheEntryStatus::ENTRY_USED, response_info.cache_entry_status); 1094 EXPECT_EQ(CacheEntryStatus::ENTRY_USED, response_info.cache_entry_status);
1095 } 1095 }
1096 1096
1097 TEST(HttpCache, SimpleGET_LoadBypassCache) { 1097 TEST(HttpCache, SimpleGET_LoadBypassCache) {
1098 MockHttpCache cache; 1098 MockHttpCache cache(false);
1099 1099
1100 // Write to the cache. 1100 // Write to the cache.
1101 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 1101 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1102 1102
1103 // Force this transaction to write to the cache again. 1103 // Force this transaction to write to the cache again.
1104 MockTransaction transaction(kSimpleGET_Transaction); 1104 MockTransaction transaction(kSimpleGET_Transaction);
1105 transaction.load_flags |= LOAD_BYPASS_CACHE; 1105 transaction.load_flags |= LOAD_BYPASS_CACHE;
1106 1106
1107 BoundTestNetLog log; 1107 BoundTestNetLog log;
1108 LoadTimingInfo load_timing_info; 1108 LoadTimingInfo load_timing_info;
(...skipping 25 matching lines...) Expand all
1134 EXPECT_TRUE(LogContainsEndEvent(entries, 7, 1134 EXPECT_TRUE(LogContainsEndEvent(entries, 7,
1135 NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY)); 1135 NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY));
1136 1136
1137 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1137 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1138 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1138 EXPECT_EQ(0, cache.disk_cache()->open_count());
1139 EXPECT_EQ(2, cache.disk_cache()->create_count()); 1139 EXPECT_EQ(2, cache.disk_cache()->create_count());
1140 TestLoadTimingNetworkRequest(load_timing_info); 1140 TestLoadTimingNetworkRequest(load_timing_info);
1141 } 1141 }
1142 1142
1143 TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit) { 1143 TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit) {
1144 MockHttpCache cache; 1144 MockHttpCache cache(false);
1145 1145
1146 // write to the cache 1146 // write to the cache
1147 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 1147 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1148 1148
1149 // force this transaction to write to the cache again 1149 // force this transaction to write to the cache again
1150 MockTransaction transaction(kSimpleGET_Transaction); 1150 MockTransaction transaction(kSimpleGET_Transaction);
1151 transaction.request_headers = "pragma: no-cache\r\n"; 1151 transaction.request_headers = "pragma: no-cache\r\n";
1152 1152
1153 RunTransactionTest(cache.http_cache(), transaction); 1153 RunTransactionTest(cache.http_cache(), transaction);
1154 1154
1155 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1155 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1156 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1156 EXPECT_EQ(0, cache.disk_cache()->open_count());
1157 EXPECT_EQ(2, cache.disk_cache()->create_count()); 1157 EXPECT_EQ(2, cache.disk_cache()->create_count());
1158 } 1158 }
1159 1159
1160 TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit2) { 1160 TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit2) {
1161 MockHttpCache cache; 1161 MockHttpCache cache(false);
1162 1162
1163 // write to the cache 1163 // write to the cache
1164 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 1164 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1165 1165
1166 // force this transaction to write to the cache again 1166 // force this transaction to write to the cache again
1167 MockTransaction transaction(kSimpleGET_Transaction); 1167 MockTransaction transaction(kSimpleGET_Transaction);
1168 transaction.request_headers = "cache-control: no-cache\r\n"; 1168 transaction.request_headers = "cache-control: no-cache\r\n";
1169 1169
1170 RunTransactionTest(cache.http_cache(), transaction); 1170 RunTransactionTest(cache.http_cache(), transaction);
1171 1171
1172 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1172 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1173 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1173 EXPECT_EQ(0, cache.disk_cache()->open_count());
1174 EXPECT_EQ(2, cache.disk_cache()->create_count()); 1174 EXPECT_EQ(2, cache.disk_cache()->create_count());
1175 } 1175 }
1176 1176
1177 TEST(HttpCache, SimpleGET_LoadValidateCache) { 1177 TEST(HttpCache, SimpleGET_LoadValidateCache) {
1178 MockHttpCache cache; 1178 MockHttpCache cache(false);
1179 1179
1180 // Write to the cache. 1180 // Write to the cache.
1181 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 1181 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1182 1182
1183 // Read from the cache. 1183 // Read from the cache.
1184 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 1184 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1185 1185
1186 // Force this transaction to validate the cache. 1186 // Force this transaction to validate the cache.
1187 MockTransaction transaction(kSimpleGET_Transaction); 1187 MockTransaction transaction(kSimpleGET_Transaction);
1188 transaction.load_flags |= LOAD_VALIDATE_CACHE; 1188 transaction.load_flags |= LOAD_VALIDATE_CACHE;
1189 1189
1190 HttpResponseInfo response_info; 1190 HttpResponseInfo response_info;
1191 BoundTestNetLog log; 1191 BoundTestNetLog log;
1192 LoadTimingInfo load_timing_info; 1192 LoadTimingInfo load_timing_info;
1193 RunTransactionTestWithResponseInfoAndGetTiming( 1193 RunTransactionTestWithResponseInfoAndGetTiming(
1194 cache.http_cache(), transaction, &response_info, log.bound(), 1194 cache.http_cache(), transaction, &response_info, log.bound(),
1195 &load_timing_info); 1195 &load_timing_info);
1196 1196
1197 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1197 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1198 EXPECT_EQ(1, cache.disk_cache()->open_count()); 1198 EXPECT_EQ(1, cache.disk_cache()->open_count());
1199 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1199 EXPECT_EQ(1, cache.disk_cache()->create_count());
1200 EXPECT_TRUE(response_info.network_accessed); 1200 EXPECT_TRUE(response_info.network_accessed);
1201 TestLoadTimingNetworkRequest(load_timing_info); 1201 TestLoadTimingNetworkRequest(load_timing_info);
1202 } 1202 }
1203 1203
1204 TEST(HttpCache, SimpleGET_LoadValidateCache_Implicit) { 1204 TEST(HttpCache, SimpleGET_LoadValidateCache_Implicit) {
1205 MockHttpCache cache; 1205 MockHttpCache cache(false);
1206 1206
1207 // write to the cache 1207 // write to the cache
1208 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 1208 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1209 1209
1210 // read from the cache 1210 // read from the cache
1211 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 1211 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1212 1212
1213 // force this transaction to validate the cache 1213 // force this transaction to validate the cache
1214 MockTransaction transaction(kSimpleGET_Transaction); 1214 MockTransaction transaction(kSimpleGET_Transaction);
1215 transaction.request_headers = "cache-control: max-age=0\r\n"; 1215 transaction.request_headers = "cache-control: max-age=0\r\n";
1216 1216
1217 RunTransactionTest(cache.http_cache(), transaction); 1217 RunTransactionTest(cache.http_cache(), transaction);
1218 1218
1219 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1219 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1220 EXPECT_EQ(1, cache.disk_cache()->open_count()); 1220 EXPECT_EQ(1, cache.disk_cache()->open_count());
1221 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1221 EXPECT_EQ(1, cache.disk_cache()->create_count());
1222 } 1222 }
1223 1223
1224 // Tests that |unused_since_prefetch| is updated accordingly (e.g. it is set to 1224 // Tests that |unused_since_prefetch| is updated accordingly (e.g. it is set to
1225 // true after a prefetch and set back to false when the prefetch is used). 1225 // true after a prefetch and set back to false when the prefetch is used).
1226 TEST(HttpCache, SimpleGET_UnusedSincePrefetch) { 1226 TEST(HttpCache, SimpleGET_UnusedSincePrefetch) {
1227 MockHttpCache cache; 1227 MockHttpCache cache(false);
1228 HttpResponseInfo response_info; 1228 HttpResponseInfo response_info;
1229 1229
1230 // A normal load does not have |unused_since_prefetch| set. 1230 // A normal load does not have |unused_since_prefetch| set.
1231 RunTransactionTestWithResponseInfoAndGetTiming( 1231 RunTransactionTestWithResponseInfoAndGetTiming(
1232 cache.http_cache(), kSimpleGET_Transaction, &response_info, 1232 cache.http_cache(), kSimpleGET_Transaction, &response_info,
1233 BoundTestNetLog().bound(), nullptr); 1233 BoundTestNetLog().bound(), nullptr);
1234 EXPECT_FALSE(response_info.unused_since_prefetch); 1234 EXPECT_FALSE(response_info.unused_since_prefetch);
1235 EXPECT_FALSE(response_info.was_cached); 1235 EXPECT_FALSE(response_info.was_cached);
1236 1236
1237 // The prefetch itself does not have |unused_since_prefetch| set. 1237 // The prefetch itself does not have |unused_since_prefetch| set.
(...skipping 29 matching lines...) Expand all
1267 1267
1268 static void PreserveRequestHeaders_Handler(const HttpRequestInfo* request, 1268 static void PreserveRequestHeaders_Handler(const HttpRequestInfo* request,
1269 std::string* response_status, 1269 std::string* response_status,
1270 std::string* response_headers, 1270 std::string* response_headers,
1271 std::string* response_data) { 1271 std::string* response_data) {
1272 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); 1272 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey));
1273 } 1273 }
1274 1274
1275 // Tests that we don't remove extra headers for simple requests. 1275 // Tests that we don't remove extra headers for simple requests.
1276 TEST(HttpCache, SimpleGET_PreserveRequestHeaders) { 1276 TEST(HttpCache, SimpleGET_PreserveRequestHeaders) {
1277 MockHttpCache cache; 1277 MockHttpCache cache(false);
1278 1278
1279 MockTransaction transaction(kSimpleGET_Transaction); 1279 MockTransaction transaction(kSimpleGET_Transaction);
1280 transaction.handler = PreserveRequestHeaders_Handler; 1280 transaction.handler = PreserveRequestHeaders_Handler;
1281 transaction.request_headers = EXTRA_HEADER; 1281 transaction.request_headers = EXTRA_HEADER;
1282 transaction.response_headers = "Cache-Control: max-age=0\n"; 1282 transaction.response_headers = "Cache-Control: max-age=0\n";
1283 AddMockTransaction(&transaction); 1283 AddMockTransaction(&transaction);
1284 1284
1285 // Write, then revalidate the entry. 1285 // Write, then revalidate the entry.
1286 RunTransactionTest(cache.http_cache(), transaction); 1286 RunTransactionTest(cache.http_cache(), transaction);
1287 RunTransactionTest(cache.http_cache(), transaction); 1287 RunTransactionTest(cache.http_cache(), transaction);
1288 1288
1289 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1289 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1290 EXPECT_EQ(1, cache.disk_cache()->open_count()); 1290 EXPECT_EQ(1, cache.disk_cache()->open_count());
1291 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1291 EXPECT_EQ(1, cache.disk_cache()->create_count());
1292 RemoveMockTransaction(&transaction); 1292 RemoveMockTransaction(&transaction);
1293 } 1293 }
1294 1294
1295 // Tests that we don't remove extra headers for conditionalized requests. 1295 // Tests that we don't remove extra headers for conditionalized requests.
1296 TEST(HttpCache, ConditionalizedGET_PreserveRequestHeaders) { 1296 TEST(HttpCache, ConditionalizedGET_PreserveRequestHeaders) {
1297 MockHttpCache cache; 1297 MockHttpCache cache(false);
1298 1298
1299 // Write to the cache. 1299 // Write to the cache.
1300 RunTransactionTest(cache.http_cache(), kETagGET_Transaction); 1300 RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
1301 1301
1302 MockTransaction transaction(kETagGET_Transaction); 1302 MockTransaction transaction(kETagGET_Transaction);
1303 transaction.handler = PreserveRequestHeaders_Handler; 1303 transaction.handler = PreserveRequestHeaders_Handler;
1304 transaction.request_headers = "If-None-Match: \"foopy\"\r\n" 1304 transaction.request_headers = "If-None-Match: \"foopy\"\r\n"
1305 EXTRA_HEADER; 1305 EXTRA_HEADER;
1306 AddMockTransaction(&transaction); 1306 AddMockTransaction(&transaction);
1307 1307
1308 RunTransactionTest(cache.http_cache(), transaction); 1308 RunTransactionTest(cache.http_cache(), transaction);
1309 1309
1310 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1310 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1311 EXPECT_EQ(1, cache.disk_cache()->open_count()); 1311 EXPECT_EQ(1, cache.disk_cache()->open_count());
1312 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1312 EXPECT_EQ(1, cache.disk_cache()->create_count());
1313 RemoveMockTransaction(&transaction); 1313 RemoveMockTransaction(&transaction);
1314 } 1314 }
1315 1315
1316 TEST(HttpCache, SimpleGET_ManyReaders) { 1316 TEST(HttpCache, SimpleGET_ManyReaders) {
1317 MockHttpCache cache; 1317 MockHttpCache cache(false);
1318 1318
1319 MockHttpRequest request(kSimpleGET_Transaction); 1319 MockHttpRequest request(kSimpleGET_Transaction);
1320 1320
1321 std::vector<Context*> context_list; 1321 std::vector<Context*> context_list;
1322 const int kNumTransactions = 5; 1322 const int kNumTransactions = 5;
1323 1323
1324 for (int i = 0; i < kNumTransactions; ++i) { 1324 for (int i = 0; i < kNumTransactions; ++i) {
1325 context_list.push_back(new Context()); 1325 context_list.push_back(new Context());
1326 Context* c = context_list[i]; 1326 Context* c = context_list[i];
1327 1327
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1373 Context* c = context_list[i]; 1373 Context* c = context_list[i];
1374 delete c; 1374 delete c;
1375 } 1375 }
1376 } 1376 }
1377 1377
1378 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. 1378 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769.
1379 // If cancelling a request is racing with another request for the same resource 1379 // If cancelling a request is racing with another request for the same resource
1380 // finishing, we have to make sure that we remove both transactions from the 1380 // finishing, we have to make sure that we remove both transactions from the
1381 // entry. 1381 // entry.
1382 TEST(HttpCache, SimpleGET_RacingReaders) { 1382 TEST(HttpCache, SimpleGET_RacingReaders) {
1383 MockHttpCache cache; 1383 MockHttpCache cache(false);
1384 1384
1385 MockHttpRequest request(kSimpleGET_Transaction); 1385 MockHttpRequest request(kSimpleGET_Transaction);
1386 MockHttpRequest reader_request(kSimpleGET_Transaction); 1386 MockHttpRequest reader_request(kSimpleGET_Transaction);
1387 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 1387 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
1388 1388
1389 std::vector<Context*> context_list; 1389 std::vector<Context*> context_list;
1390 const int kNumTransactions = 5; 1390 const int kNumTransactions = 5;
1391 1391
1392 for (int i = 0; i < kNumTransactions; ++i) { 1392 for (int i = 0; i < kNumTransactions; ++i) {
1393 context_list.push_back(new Context()); 1393 context_list.push_back(new Context());
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1457 Context* c = context_list[i]; 1457 Context* c = context_list[i];
1458 delete c; 1458 delete c;
1459 } 1459 }
1460 } 1460 }
1461 1461
1462 // Tests that we can doom an entry with pending transactions and delete one of 1462 // Tests that we can doom an entry with pending transactions and delete one of
1463 // the pending transactions before the first one completes. 1463 // the pending transactions before the first one completes.
1464 // See http://code.google.com/p/chromium/issues/detail?id=25588 1464 // See http://code.google.com/p/chromium/issues/detail?id=25588
1465 TEST(HttpCache, SimpleGET_DoomWithPending) { 1465 TEST(HttpCache, SimpleGET_DoomWithPending) {
1466 // We need simultaneous doomed / not_doomed entries so let's use a real cache. 1466 // We need simultaneous doomed / not_doomed entries so let's use a real cache.
1467 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(1024 * 1024)); 1467 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(1024 * 1024), false);
1468 1468
1469 MockHttpRequest request(kSimpleGET_Transaction); 1469 MockHttpRequest request(kSimpleGET_Transaction);
1470 MockHttpRequest writer_request(kSimpleGET_Transaction); 1470 MockHttpRequest writer_request(kSimpleGET_Transaction);
1471 writer_request.load_flags = LOAD_BYPASS_CACHE; 1471 writer_request.load_flags = LOAD_BYPASS_CACHE;
1472 1472
1473 std::vector<std::unique_ptr<Context>> context_list; 1473 std::vector<std::unique_ptr<Context>> context_list;
1474 const int kNumTransactions = 4; 1474 const int kNumTransactions = 4;
1475 1475
1476 for (int i = 0; i < kNumTransactions; ++i) { 1476 for (int i = 0; i < kNumTransactions; ++i) {
1477 context_list.push_back(base::WrapUnique(new Context())); 1477 context_list.push_back(base::WrapUnique(new Context()));
(...skipping 25 matching lines...) Expand all
1503 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); 1503 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
1504 c->result = c->callback.WaitForResult(); 1504 c->result = c->callback.WaitForResult();
1505 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1505 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1506 } 1506 }
1507 } 1507 }
1508 1508
1509 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4731. 1509 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4731.
1510 // We may attempt to delete an entry synchronously with the act of adding a new 1510 // We may attempt to delete an entry synchronously with the act of adding a new
1511 // transaction to said entry. 1511 // transaction to said entry.
1512 TEST(HttpCache, FastNoStoreGET_DoneWithPending) { 1512 TEST(HttpCache, FastNoStoreGET_DoneWithPending) {
1513 MockHttpCache cache; 1513 MockHttpCache cache(false);
1514 1514
1515 // The headers will be served right from the call to Start() the request. 1515 // The headers will be served right from the call to Start() the request.
1516 MockHttpRequest request(kFastNoStoreGET_Transaction); 1516 MockHttpRequest request(kFastNoStoreGET_Transaction);
1517 FastTransactionServer request_handler; 1517 FastTransactionServer request_handler;
1518 AddMockTransaction(&kFastNoStoreGET_Transaction); 1518 AddMockTransaction(&kFastNoStoreGET_Transaction);
1519 1519
1520 std::vector<Context*> context_list; 1520 std::vector<Context*> context_list;
1521 const int kNumTransactions = 3; 1521 const int kNumTransactions = 3;
1522 1522
1523 for (int i = 0; i < kNumTransactions; ++i) { 1523 for (int i = 0; i < kNumTransactions; ++i) {
(...skipping 29 matching lines...) Expand all
1553 } 1553 }
1554 1554
1555 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 1555 EXPECT_EQ(3, cache.network_layer()->transaction_count());
1556 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1556 EXPECT_EQ(0, cache.disk_cache()->open_count());
1557 EXPECT_EQ(2, cache.disk_cache()->create_count()); 1557 EXPECT_EQ(2, cache.disk_cache()->create_count());
1558 1558
1559 RemoveMockTransaction(&kFastNoStoreGET_Transaction); 1559 RemoveMockTransaction(&kFastNoStoreGET_Transaction);
1560 } 1560 }
1561 1561
1562 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { 1562 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) {
1563 MockHttpCache cache; 1563 MockHttpCache cache(false);
1564 1564
1565 MockHttpRequest request(kSimpleGET_Transaction); 1565 MockHttpRequest request(kSimpleGET_Transaction);
1566 1566
1567 std::vector<Context*> context_list; 1567 std::vector<Context*> context_list;
1568 const int kNumTransactions = 2; 1568 const int kNumTransactions = 2;
1569 1569
1570 for (int i = 0; i < kNumTransactions; ++i) { 1570 for (int i = 0; i < kNumTransactions; ++i) {
1571 context_list.push_back(new Context()); 1571 context_list.push_back(new Context());
1572 Context* c = context_list[i]; 1572 Context* c = context_list[i];
1573 1573
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1613 1613
1614 for (int i = 1; i < kNumTransactions; ++i) { 1614 for (int i = 1; i < kNumTransactions; ++i) {
1615 Context* c = context_list[i]; 1615 Context* c = context_list[i];
1616 delete c; 1616 delete c;
1617 } 1617 }
1618 } 1618 }
1619 1619
1620 // Tests that we can cancel requests that are queued waiting to open the disk 1620 // Tests that we can cancel requests that are queued waiting to open the disk
1621 // cache entry. 1621 // cache entry.
1622 TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) { 1622 TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) {
1623 MockHttpCache cache; 1623 MockHttpCache cache(false);
1624 1624
1625 MockHttpRequest request(kSimpleGET_Transaction); 1625 MockHttpRequest request(kSimpleGET_Transaction);
1626 1626
1627 std::vector<Context*> context_list; 1627 std::vector<Context*> context_list;
1628 const int kNumTransactions = 5; 1628 const int kNumTransactions = 5;
1629 1629
1630 for (int i = 0; i < kNumTransactions; i++) { 1630 for (int i = 0; i < kNumTransactions; i++) {
1631 context_list.push_back(new Context()); 1631 context_list.push_back(new Context());
1632 Context* c = context_list[i]; 1632 Context* c = context_list[i];
1633 1633
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1669 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1669 EXPECT_EQ(0, cache.disk_cache()->open_count());
1670 EXPECT_EQ(2, cache.disk_cache()->create_count()); 1670 EXPECT_EQ(2, cache.disk_cache()->create_count());
1671 1671
1672 for (int i = 1; i < kNumTransactions; ++i) { 1672 for (int i = 1; i < kNumTransactions; ++i) {
1673 delete context_list[i]; 1673 delete context_list[i];
1674 } 1674 }
1675 } 1675 }
1676 1676
1677 // Tests that we can cancel a single request to open a disk cache entry. 1677 // Tests that we can cancel a single request to open a disk cache entry.
1678 TEST(HttpCache, SimpleGET_CancelCreate) { 1678 TEST(HttpCache, SimpleGET_CancelCreate) {
1679 MockHttpCache cache; 1679 MockHttpCache cache(false);
1680 1680
1681 MockHttpRequest request(kSimpleGET_Transaction); 1681 MockHttpRequest request(kSimpleGET_Transaction);
1682 1682
1683 Context* c = new Context(); 1683 Context* c = new Context();
1684 1684
1685 c->result = cache.CreateTransaction(&c->trans); 1685 c->result = cache.CreateTransaction(&c->trans);
1686 ASSERT_THAT(c->result, IsOk()); 1686 ASSERT_THAT(c->result, IsOk());
1687 1687
1688 c->result = 1688 c->result =
1689 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); 1689 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1690 EXPECT_THAT(c->result, IsError(ERR_IO_PENDING)); 1690 EXPECT_THAT(c->result, IsError(ERR_IO_PENDING));
1691 1691
1692 // Release the reference that the mock disk cache keeps for this entry, so 1692 // Release the reference that the mock disk cache keeps for this entry, so
1693 // that we test that the http cache handles the cancellation correctly. 1693 // that we test that the http cache handles the cancellation correctly.
1694 cache.disk_cache()->ReleaseAll(); 1694 cache.disk_cache()->ReleaseAll();
1695 delete c; 1695 delete c;
1696 1696
1697 base::RunLoop().RunUntilIdle(); 1697 base::RunLoop().RunUntilIdle();
1698 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1698 EXPECT_EQ(1, cache.disk_cache()->create_count());
1699 } 1699 }
1700 1700
1701 // Tests that we delete/create entries even if multiple requests are queued. 1701 // Tests that we delete/create entries even if multiple requests are queued.
1702 TEST(HttpCache, SimpleGET_ManyWriters_BypassCache) { 1702 TEST(HttpCache, SimpleGET_ManyWriters_BypassCache) {
1703 MockHttpCache cache; 1703 MockHttpCache cache(false);
1704 1704
1705 MockHttpRequest request(kSimpleGET_Transaction); 1705 MockHttpRequest request(kSimpleGET_Transaction);
1706 request.load_flags = LOAD_BYPASS_CACHE; 1706 request.load_flags = LOAD_BYPASS_CACHE;
1707 1707
1708 std::vector<Context*> context_list; 1708 std::vector<Context*> context_list;
1709 const int kNumTransactions = 5; 1709 const int kNumTransactions = 5;
1710 1710
1711 for (int i = 0; i < kNumTransactions; i++) { 1711 for (int i = 0; i < kNumTransactions; i++) {
1712 context_list.push_back(new Context()); 1712 context_list.push_back(new Context());
1713 Context* c = context_list[i]; 1713 Context* c = context_list[i];
(...skipping 26 matching lines...) Expand all
1740 EXPECT_EQ(5, cache.disk_cache()->create_count()); 1740 EXPECT_EQ(5, cache.disk_cache()->create_count());
1741 1741
1742 for (int i = 0; i < kNumTransactions; ++i) { 1742 for (int i = 0; i < kNumTransactions; ++i) {
1743 delete context_list[i]; 1743 delete context_list[i];
1744 } 1744 }
1745 } 1745 }
1746 1746
1747 // Tests that a (simulated) timeout allows transactions waiting on the cache 1747 // Tests that a (simulated) timeout allows transactions waiting on the cache
1748 // lock to continue. 1748 // lock to continue.
1749 TEST(HttpCache, SimpleGET_WriterTimeout) { 1749 TEST(HttpCache, SimpleGET_WriterTimeout) {
1750 MockHttpCache cache; 1750 MockHttpCache cache(false);
1751 cache.BypassCacheLock(); 1751 cache.BypassCacheLock();
1752 1752
1753 MockHttpRequest request(kSimpleGET_Transaction); 1753 MockHttpRequest request(kSimpleGET_Transaction);
1754 Context c1, c2; 1754 Context c1, c2;
1755 ASSERT_THAT(cache.CreateTransaction(&c1.trans), IsOk()); 1755 ASSERT_THAT(cache.CreateTransaction(&c1.trans), IsOk());
1756 ASSERT_EQ(ERR_IO_PENDING, c1.trans->Start(&request, c1.callback.callback(), 1756 ASSERT_EQ(ERR_IO_PENDING, c1.trans->Start(&request, c1.callback.callback(),
1757 NetLogWithSource())); 1757 NetLogWithSource()));
1758 ASSERT_THAT(cache.CreateTransaction(&c2.trans), IsOk()); 1758 ASSERT_THAT(cache.CreateTransaction(&c2.trans), IsOk());
1759 ASSERT_EQ(ERR_IO_PENDING, c2.trans->Start(&request, c2.callback.callback(), 1759 ASSERT_EQ(ERR_IO_PENDING, c2.trans->Start(&request, c2.callback.callback(),
1760 NetLogWithSource())); 1760 NetLogWithSource()));
1761 1761
1762 // The second request is queued after the first one. 1762 // The second request is queued after the first one.
1763 1763
1764 c2.callback.WaitForResult(); 1764 c2.callback.WaitForResult();
1765 ReadAndVerifyTransaction(c2.trans.get(), kSimpleGET_Transaction); 1765 ReadAndVerifyTransaction(c2.trans.get(), kSimpleGET_Transaction);
1766 1766
1767 // Complete the first transaction. 1767 // Complete the first transaction.
1768 c1.callback.WaitForResult(); 1768 c1.callback.WaitForResult();
1769 ReadAndVerifyTransaction(c1.trans.get(), kSimpleGET_Transaction); 1769 ReadAndVerifyTransaction(c1.trans.get(), kSimpleGET_Transaction);
1770 } 1770 }
1771 1771
1772 TEST(HttpCache, SimpleGET_AbandonedCacheRead) { 1772 TEST(HttpCache, SimpleGET_AbandonedCacheRead) {
1773 MockHttpCache cache; 1773 MockHttpCache cache(false);
1774 1774
1775 // write to the cache 1775 // write to the cache
1776 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 1776 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
1777 1777
1778 MockHttpRequest request(kSimpleGET_Transaction); 1778 MockHttpRequest request(kSimpleGET_Transaction);
1779 TestCompletionCallback callback; 1779 TestCompletionCallback callback;
1780 1780
1781 std::unique_ptr<HttpTransaction> trans; 1781 std::unique_ptr<HttpTransaction> trans;
1782 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 1782 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
1783 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 1783 int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
(...skipping 11 matching lines...) Expand all
1795 1795
1796 // Make sure we pump any pending events, which should include a call to 1796 // Make sure we pump any pending events, which should include a call to
1797 // HttpCache::Transaction::OnCacheReadCompleted. 1797 // HttpCache::Transaction::OnCacheReadCompleted.
1798 base::RunLoop().RunUntilIdle(); 1798 base::RunLoop().RunUntilIdle();
1799 } 1799 }
1800 1800
1801 // Tests that we can delete the HttpCache and deal with queued transactions 1801 // Tests that we can delete the HttpCache and deal with queued transactions
1802 // ("waiting for the backend" as opposed to Active or Doomed entries). 1802 // ("waiting for the backend" as opposed to Active or Doomed entries).
1803 TEST(HttpCache, SimpleGET_ManyWriters_DeleteCache) { 1803 TEST(HttpCache, SimpleGET_ManyWriters_DeleteCache) {
1804 std::unique_ptr<MockHttpCache> cache( 1804 std::unique_ptr<MockHttpCache> cache(
1805 new MockHttpCache(base::WrapUnique(new MockBackendNoCbFactory()))); 1805 new MockHttpCache(base::WrapUnique(new MockBackendNoCbFactory()), false));
1806 1806
1807 MockHttpRequest request(kSimpleGET_Transaction); 1807 MockHttpRequest request(kSimpleGET_Transaction);
1808 1808
1809 std::vector<Context*> context_list; 1809 std::vector<Context*> context_list;
1810 const int kNumTransactions = 5; 1810 const int kNumTransactions = 5;
1811 1811
1812 for (int i = 0; i < kNumTransactions; i++) { 1812 for (int i = 0; i < kNumTransactions; i++) {
1813 context_list.push_back(new Context()); 1813 context_list.push_back(new Context());
1814 Context* c = context_list[i]; 1814 Context* c = context_list[i];
1815 1815
(...skipping 16 matching lines...) Expand all
1832 // There is not much to do with the transactions at this point... they are 1832 // There is not much to do with the transactions at this point... they are
1833 // waiting for a callback that will not fire. 1833 // waiting for a callback that will not fire.
1834 for (int i = 0; i < kNumTransactions; ++i) { 1834 for (int i = 0; i < kNumTransactions; ++i) {
1835 delete context_list[i]; 1835 delete context_list[i];
1836 } 1836 }
1837 } 1837 }
1838 1838
1839 // Tests that we queue requests when initializing the backend. 1839 // Tests that we queue requests when initializing the backend.
1840 TEST(HttpCache, SimpleGET_WaitForBackend) { 1840 TEST(HttpCache, SimpleGET_WaitForBackend) {
1841 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); 1841 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
1842 MockHttpCache cache(base::WrapUnique(factory)); 1842 MockHttpCache cache(base::WrapUnique(factory), false);
1843 1843
1844 MockHttpRequest request0(kSimpleGET_Transaction); 1844 MockHttpRequest request0(kSimpleGET_Transaction);
1845 MockHttpRequest request1(kTypicalGET_Transaction); 1845 MockHttpRequest request1(kTypicalGET_Transaction);
1846 MockHttpRequest request2(kETagGET_Transaction); 1846 MockHttpRequest request2(kETagGET_Transaction);
1847 1847
1848 std::vector<Context*> context_list; 1848 std::vector<Context*> context_list;
1849 const int kNumTransactions = 3; 1849 const int kNumTransactions = 3;
1850 1850
1851 for (int i = 0; i < kNumTransactions; i++) { 1851 for (int i = 0; i < kNumTransactions; i++) {
1852 context_list.push_back(new Context()); 1852 context_list.push_back(new Context());
(...skipping 25 matching lines...) Expand all
1878 for (int i = 0; i < kNumTransactions; ++i) { 1878 for (int i = 0; i < kNumTransactions; ++i) {
1879 EXPECT_TRUE(context_list[i]->callback.have_result()); 1879 EXPECT_TRUE(context_list[i]->callback.have_result());
1880 delete context_list[i]; 1880 delete context_list[i];
1881 } 1881 }
1882 } 1882 }
1883 1883
1884 // Tests that we can cancel requests that are queued waiting for the backend 1884 // Tests that we can cancel requests that are queued waiting for the backend
1885 // to be initialized. 1885 // to be initialized.
1886 TEST(HttpCache, SimpleGET_WaitForBackend_CancelCreate) { 1886 TEST(HttpCache, SimpleGET_WaitForBackend_CancelCreate) {
1887 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); 1887 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
1888 MockHttpCache cache(base::WrapUnique(factory)); 1888 MockHttpCache cache(base::WrapUnique(factory), false);
1889 1889
1890 MockHttpRequest request0(kSimpleGET_Transaction); 1890 MockHttpRequest request0(kSimpleGET_Transaction);
1891 MockHttpRequest request1(kTypicalGET_Transaction); 1891 MockHttpRequest request1(kTypicalGET_Transaction);
1892 MockHttpRequest request2(kETagGET_Transaction); 1892 MockHttpRequest request2(kETagGET_Transaction);
1893 1893
1894 std::vector<Context*> context_list; 1894 std::vector<Context*> context_list;
1895 const int kNumTransactions = 3; 1895 const int kNumTransactions = 3;
1896 1896
1897 for (int i = 0; i < kNumTransactions; i++) { 1897 for (int i = 0; i < kNumTransactions; i++) {
1898 context_list.push_back(new Context()); 1898 context_list.push_back(new Context());
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1933 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1933 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1934 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1934 EXPECT_EQ(1, cache.disk_cache()->create_count());
1935 1935
1936 delete context_list[2]; 1936 delete context_list[2];
1937 } 1937 }
1938 1938
1939 // Tests that we can delete the cache while creating the backend. 1939 // Tests that we can delete the cache while creating the backend.
1940 TEST(HttpCache, DeleteCacheWaitingForBackend) { 1940 TEST(HttpCache, DeleteCacheWaitingForBackend) {
1941 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); 1941 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
1942 std::unique_ptr<MockHttpCache> cache( 1942 std::unique_ptr<MockHttpCache> cache(
1943 new MockHttpCache(base::WrapUnique(factory))); 1943 new MockHttpCache(base::WrapUnique(factory), false));
1944 1944
1945 MockHttpRequest request(kSimpleGET_Transaction); 1945 MockHttpRequest request(kSimpleGET_Transaction);
1946 1946
1947 std::unique_ptr<Context> c(new Context()); 1947 std::unique_ptr<Context> c(new Context());
1948 c->result = cache->CreateTransaction(&c->trans); 1948 c->result = cache->CreateTransaction(&c->trans);
1949 ASSERT_THAT(c->result, IsOk()); 1949 ASSERT_THAT(c->result, IsOk());
1950 1950
1951 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); 1951 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1952 1952
1953 // Just to make sure that everything is still pending. 1953 // Just to make sure that everything is still pending.
(...skipping 11 matching lines...) Expand all
1965 base::RunLoop().RunUntilIdle(); 1965 base::RunLoop().RunUntilIdle();
1966 1966
1967 backend->reset(); 1967 backend->reset();
1968 callback.Run(ERR_ABORTED); 1968 callback.Run(ERR_ABORTED);
1969 } 1969 }
1970 1970
1971 // Tests that we can delete the cache while creating the backend, from within 1971 // Tests that we can delete the cache while creating the backend, from within
1972 // one of the callbacks. 1972 // one of the callbacks.
1973 TEST(HttpCache, DeleteCacheWaitingForBackend2) { 1973 TEST(HttpCache, DeleteCacheWaitingForBackend2) {
1974 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); 1974 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
1975 MockHttpCache* cache = new MockHttpCache(base::WrapUnique(factory)); 1975 MockHttpCache* cache = new MockHttpCache(base::WrapUnique(factory), false);
1976 1976
1977 DeleteCacheCompletionCallback cb(cache); 1977 DeleteCacheCompletionCallback cb(cache);
1978 disk_cache::Backend* backend; 1978 disk_cache::Backend* backend;
1979 int rv = cache->http_cache()->GetBackend(&backend, cb.callback()); 1979 int rv = cache->http_cache()->GetBackend(&backend, cb.callback());
1980 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); 1980 EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
1981 1981
1982 // Now let's queue a regular transaction 1982 // Now let's queue a regular transaction
1983 MockHttpRequest request(kSimpleGET_Transaction); 1983 MockHttpRequest request(kSimpleGET_Transaction);
1984 1984
1985 std::unique_ptr<Context> c(new Context()); 1985 std::unique_ptr<Context> c(new Context());
(...skipping 24 matching lines...) Expand all
2010 } 2010 }
2011 2011
2012 // Fails only on bots. crbug.com/533640 2012 // Fails only on bots. crbug.com/533640
2013 #if defined(OS_ANDROID) 2013 #if defined(OS_ANDROID)
2014 #define MAYBE_TypicalGET_ConditionalRequest \ 2014 #define MAYBE_TypicalGET_ConditionalRequest \
2015 DISABLED_TypicalGET_ConditionalRequest 2015 DISABLED_TypicalGET_ConditionalRequest
2016 #else 2016 #else
2017 #define MAYBE_TypicalGET_ConditionalRequest TypicalGET_ConditionalRequest 2017 #define MAYBE_TypicalGET_ConditionalRequest TypicalGET_ConditionalRequest
2018 #endif 2018 #endif
2019 TEST(HttpCache, MAYBE_TypicalGET_ConditionalRequest) { 2019 TEST(HttpCache, MAYBE_TypicalGET_ConditionalRequest) {
2020 MockHttpCache cache; 2020 MockHttpCache cache(false);
2021 2021
2022 // write to the cache 2022 // write to the cache
2023 RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction); 2023 RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction);
2024 2024
2025 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2025 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2026 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2026 EXPECT_EQ(0, cache.disk_cache()->open_count());
2027 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2027 EXPECT_EQ(1, cache.disk_cache()->create_count());
2028 2028
2029 // Get the same URL again, but this time we expect it to result 2029 // Get the same URL again, but this time we expect it to result
2030 // in a conditional request. 2030 // in a conditional request.
(...skipping 13 matching lines...) Expand all
2044 std::string* response_headers, 2044 std::string* response_headers,
2045 std::string* response_data) { 2045 std::string* response_data) {
2046 EXPECT_TRUE( 2046 EXPECT_TRUE(
2047 request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch)); 2047 request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch));
2048 response_status->assign("HTTP/1.1 304 Not Modified"); 2048 response_status->assign("HTTP/1.1 304 Not Modified");
2049 response_headers->assign(kETagGET_Transaction.response_headers); 2049 response_headers->assign(kETagGET_Transaction.response_headers);
2050 response_data->clear(); 2050 response_data->clear();
2051 } 2051 }
2052 2052
2053 TEST(HttpCache, ETagGET_ConditionalRequest_304) { 2053 TEST(HttpCache, ETagGET_ConditionalRequest_304) {
2054 MockHttpCache cache; 2054 MockHttpCache cache(false);
2055 2055
2056 ScopedMockTransaction transaction(kETagGET_Transaction); 2056 ScopedMockTransaction transaction(kETagGET_Transaction);
2057 2057
2058 // write to the cache 2058 // write to the cache
2059 RunTransactionTest(cache.http_cache(), transaction); 2059 RunTransactionTest(cache.http_cache(), transaction);
2060 2060
2061 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2061 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2062 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2062 EXPECT_EQ(0, cache.disk_cache()->open_count());
2063 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2063 EXPECT_EQ(1, cache.disk_cache()->create_count());
2064 2064
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
2120 response_data->clear(); 2120 response_data->clear();
2121 } else { 2121 } else {
2122 response_status->assign(kTypicalGET_Transaction.status); 2122 response_status->assign(kTypicalGET_Transaction.status);
2123 response_headers->assign(kTypicalGET_Transaction.response_headers); 2123 response_headers->assign(kTypicalGET_Transaction.response_headers);
2124 response_data->assign(kTypicalGET_Transaction.data); 2124 response_data->assign(kTypicalGET_Transaction.data);
2125 } 2125 }
2126 } 2126 }
2127 2127
2128 // Tests revalidation after a vary match. 2128 // Tests revalidation after a vary match.
2129 TEST(HttpCache, GET_ValidateCache_VaryMatch) { 2129 TEST(HttpCache, GET_ValidateCache_VaryMatch) {
2130 MockHttpCache cache; 2130 MockHttpCache cache(false);
2131 2131
2132 // Write to the cache. 2132 // Write to the cache.
2133 MockTransaction transaction(kTypicalGET_Transaction); 2133 MockTransaction transaction(kTypicalGET_Transaction);
2134 transaction.request_headers = "Foo: bar\r\n"; 2134 transaction.request_headers = "Foo: bar\r\n";
2135 transaction.response_headers = 2135 transaction.response_headers =
2136 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" 2136 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
2137 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" 2137 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
2138 "Etag: \"foopy\"\n" 2138 "Etag: \"foopy\"\n"
2139 "Cache-Control: max-age=0\n" 2139 "Cache-Control: max-age=0\n"
2140 "Vary: Foo\n"; 2140 "Vary: Foo\n";
(...skipping 12 matching lines...) Expand all
2153 EXPECT_TRUE(server.LastModifiedUsed()); 2153 EXPECT_TRUE(server.LastModifiedUsed());
2154 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 2154 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2155 EXPECT_EQ(1, cache.disk_cache()->open_count()); 2155 EXPECT_EQ(1, cache.disk_cache()->open_count());
2156 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2156 EXPECT_EQ(1, cache.disk_cache()->create_count());
2157 TestLoadTimingNetworkRequest(load_timing_info); 2157 TestLoadTimingNetworkRequest(load_timing_info);
2158 RemoveMockTransaction(&transaction); 2158 RemoveMockTransaction(&transaction);
2159 } 2159 }
2160 2160
2161 // Tests revalidation after a vary mismatch if etag is present. 2161 // Tests revalidation after a vary mismatch if etag is present.
2162 TEST(HttpCache, GET_ValidateCache_VaryMismatch) { 2162 TEST(HttpCache, GET_ValidateCache_VaryMismatch) {
2163 MockHttpCache cache; 2163 MockHttpCache cache(false);
2164 2164
2165 // Write to the cache. 2165 // Write to the cache.
2166 MockTransaction transaction(kTypicalGET_Transaction); 2166 MockTransaction transaction(kTypicalGET_Transaction);
2167 transaction.request_headers = "Foo: bar\r\n"; 2167 transaction.request_headers = "Foo: bar\r\n";
2168 transaction.response_headers = 2168 transaction.response_headers =
2169 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" 2169 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
2170 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" 2170 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
2171 "Etag: \"foopy\"\n" 2171 "Etag: \"foopy\"\n"
2172 "Cache-Control: max-age=0\n" 2172 "Cache-Control: max-age=0\n"
2173 "Vary: Foo\n"; 2173 "Vary: Foo\n";
(...skipping 13 matching lines...) Expand all
2187 EXPECT_FALSE(server.LastModifiedUsed()); 2187 EXPECT_FALSE(server.LastModifiedUsed());
2188 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 2188 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2189 EXPECT_EQ(1, cache.disk_cache()->open_count()); 2189 EXPECT_EQ(1, cache.disk_cache()->open_count());
2190 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2190 EXPECT_EQ(1, cache.disk_cache()->create_count());
2191 TestLoadTimingNetworkRequest(load_timing_info); 2191 TestLoadTimingNetworkRequest(load_timing_info);
2192 RemoveMockTransaction(&transaction); 2192 RemoveMockTransaction(&transaction);
2193 } 2193 }
2194 2194
2195 // Tests lack of revalidation after a vary mismatch and no etag. 2195 // Tests lack of revalidation after a vary mismatch and no etag.
2196 TEST(HttpCache, GET_DontValidateCache_VaryMismatch) { 2196 TEST(HttpCache, GET_DontValidateCache_VaryMismatch) {
2197 MockHttpCache cache; 2197 MockHttpCache cache(false);
2198 2198
2199 // Write to the cache. 2199 // Write to the cache.
2200 MockTransaction transaction(kTypicalGET_Transaction); 2200 MockTransaction transaction(kTypicalGET_Transaction);
2201 transaction.request_headers = "Foo: bar\r\n"; 2201 transaction.request_headers = "Foo: bar\r\n";
2202 transaction.response_headers = 2202 transaction.response_headers =
2203 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" 2203 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
2204 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" 2204 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
2205 "Cache-Control: max-age=0\n" 2205 "Cache-Control: max-age=0\n"
2206 "Vary: Foo\n"; 2206 "Vary: Foo\n";
2207 AddMockTransaction(&transaction); 2207 AddMockTransaction(&transaction);
(...skipping 12 matching lines...) Expand all
2220 EXPECT_FALSE(server.LastModifiedUsed()); 2220 EXPECT_FALSE(server.LastModifiedUsed());
2221 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 2221 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2222 EXPECT_EQ(1, cache.disk_cache()->open_count()); 2222 EXPECT_EQ(1, cache.disk_cache()->open_count());
2223 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2223 EXPECT_EQ(1, cache.disk_cache()->create_count());
2224 TestLoadTimingNetworkRequest(load_timing_info); 2224 TestLoadTimingNetworkRequest(load_timing_info);
2225 RemoveMockTransaction(&transaction); 2225 RemoveMockTransaction(&transaction);
2226 } 2226 }
2227 2227
2228 // Tests that a new vary header provided when revalidating an entry is saved. 2228 // Tests that a new vary header provided when revalidating an entry is saved.
2229 TEST(HttpCache, GET_ValidateCache_VaryMatch_UpdateVary) { 2229 TEST(HttpCache, GET_ValidateCache_VaryMatch_UpdateVary) {
2230 MockHttpCache cache; 2230 MockHttpCache cache(false);
2231 2231
2232 // Write to the cache. 2232 // Write to the cache.
2233 ScopedMockTransaction transaction(kTypicalGET_Transaction); 2233 ScopedMockTransaction transaction(kTypicalGET_Transaction);
2234 transaction.request_headers = "Foo: bar\r\n Name: bar\r\n"; 2234 transaction.request_headers = "Foo: bar\r\n Name: bar\r\n";
2235 transaction.response_headers = 2235 transaction.response_headers =
2236 "Etag: \"foopy\"\n" 2236 "Etag: \"foopy\"\n"
2237 "Cache-Control: max-age=0\n" 2237 "Cache-Control: max-age=0\n"
2238 "Vary: Foo\n"; 2238 "Vary: Foo\n";
2239 RunTransactionTest(cache.http_cache(), transaction); 2239 RunTransactionTest(cache.http_cache(), transaction);
2240 2240
(...skipping 18 matching lines...) Expand all
2259 RunTransactionTest(cache.http_cache(), transaction); 2259 RunTransactionTest(cache.http_cache(), transaction);
2260 2260
2261 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 2261 EXPECT_EQ(3, cache.network_layer()->transaction_count());
2262 EXPECT_EQ(2, cache.disk_cache()->open_count()); 2262 EXPECT_EQ(2, cache.disk_cache()->open_count());
2263 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2263 EXPECT_EQ(1, cache.disk_cache()->create_count());
2264 } 2264 }
2265 2265
2266 // Tests that new request headers causing a vary mismatch are paired with the 2266 // Tests that new request headers causing a vary mismatch are paired with the
2267 // new response when the server says the old response can be used. 2267 // new response when the server says the old response can be used.
2268 TEST(HttpCache, GET_ValidateCache_VaryMismatch_UpdateRequestHeader) { 2268 TEST(HttpCache, GET_ValidateCache_VaryMismatch_UpdateRequestHeader) {
2269 MockHttpCache cache; 2269 MockHttpCache cache(false);
2270 2270
2271 // Write to the cache. 2271 // Write to the cache.
2272 ScopedMockTransaction transaction(kTypicalGET_Transaction); 2272 ScopedMockTransaction transaction(kTypicalGET_Transaction);
2273 transaction.request_headers = "Foo: bar\r\n"; 2273 transaction.request_headers = "Foo: bar\r\n";
2274 transaction.response_headers = 2274 transaction.response_headers =
2275 "Etag: \"foopy\"\n" 2275 "Etag: \"foopy\"\n"
2276 "Cache-Control: max-age=3600\n" 2276 "Cache-Control: max-age=3600\n"
2277 "Vary: Foo\n"; 2277 "Vary: Foo\n";
2278 RunTransactionTest(cache.http_cache(), transaction); 2278 RunTransactionTest(cache.http_cache(), transaction);
2279 2279
(...skipping 14 matching lines...) Expand all
2294 RunTransactionTest(cache.http_cache(), transaction); 2294 RunTransactionTest(cache.http_cache(), transaction);
2295 2295
2296 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 2296 EXPECT_EQ(3, cache.network_layer()->transaction_count());
2297 EXPECT_EQ(2, cache.disk_cache()->open_count()); 2297 EXPECT_EQ(2, cache.disk_cache()->open_count());
2298 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2298 EXPECT_EQ(1, cache.disk_cache()->create_count());
2299 } 2299 }
2300 2300
2301 // Tests that a 304 without vary headers doesn't delete the previously stored 2301 // Tests that a 304 without vary headers doesn't delete the previously stored
2302 // vary data after a vary match revalidation. 2302 // vary data after a vary match revalidation.
2303 TEST(HttpCache, GET_ValidateCache_VaryMatch_DontDeleteVary) { 2303 TEST(HttpCache, GET_ValidateCache_VaryMatch_DontDeleteVary) {
2304 MockHttpCache cache; 2304 MockHttpCache cache(false);
2305 2305
2306 // Write to the cache. 2306 // Write to the cache.
2307 ScopedMockTransaction transaction(kTypicalGET_Transaction); 2307 ScopedMockTransaction transaction(kTypicalGET_Transaction);
2308 transaction.request_headers = "Foo: bar\r\n"; 2308 transaction.request_headers = "Foo: bar\r\n";
2309 transaction.response_headers = 2309 transaction.response_headers =
2310 "Etag: \"foopy\"\n" 2310 "Etag: \"foopy\"\n"
2311 "Cache-Control: max-age=0\n" 2311 "Cache-Control: max-age=0\n"
2312 "Vary: Foo\n"; 2312 "Vary: Foo\n";
2313 RunTransactionTest(cache.http_cache(), transaction); 2313 RunTransactionTest(cache.http_cache(), transaction);
2314 2314
(...skipping 16 matching lines...) Expand all
2331 RunTransactionTest(cache.http_cache(), transaction); 2331 RunTransactionTest(cache.http_cache(), transaction);
2332 2332
2333 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 2333 EXPECT_EQ(3, cache.network_layer()->transaction_count());
2334 EXPECT_EQ(2, cache.disk_cache()->open_count()); 2334 EXPECT_EQ(2, cache.disk_cache()->open_count());
2335 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2335 EXPECT_EQ(1, cache.disk_cache()->create_count());
2336 } 2336 }
2337 2337
2338 // Tests that a 304 without vary headers doesn't delete the previously stored 2338 // Tests that a 304 without vary headers doesn't delete the previously stored
2339 // vary data after a vary mismatch. 2339 // vary data after a vary mismatch.
2340 TEST(HttpCache, GET_ValidateCache_VaryMismatch_DontDeleteVary) { 2340 TEST(HttpCache, GET_ValidateCache_VaryMismatch_DontDeleteVary) {
2341 MockHttpCache cache; 2341 MockHttpCache cache(false);
2342 2342
2343 // Write to the cache. 2343 // Write to the cache.
2344 ScopedMockTransaction transaction(kTypicalGET_Transaction); 2344 ScopedMockTransaction transaction(kTypicalGET_Transaction);
2345 transaction.request_headers = "Foo: bar\r\n"; 2345 transaction.request_headers = "Foo: bar\r\n";
2346 transaction.response_headers = 2346 transaction.response_headers =
2347 "Etag: \"foopy\"\n" 2347 "Etag: \"foopy\"\n"
2348 "Cache-Control: max-age=3600\n" 2348 "Cache-Control: max-age=3600\n"
2349 "Vary: Foo\n"; 2349 "Vary: Foo\n";
2350 RunTransactionTest(cache.http_cache(), transaction); 2350 RunTransactionTest(cache.http_cache(), transaction);
2351 2351
(...skipping 23 matching lines...) Expand all
2375 2375
2376 static void ETagGet_UnconditionalRequest_Handler(const HttpRequestInfo* request, 2376 static void ETagGet_UnconditionalRequest_Handler(const HttpRequestInfo* request,
2377 std::string* response_status, 2377 std::string* response_status,
2378 std::string* response_headers, 2378 std::string* response_headers,
2379 std::string* response_data) { 2379 std::string* response_data) {
2380 EXPECT_FALSE( 2380 EXPECT_FALSE(
2381 request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch)); 2381 request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch));
2382 } 2382 }
2383 2383
2384 TEST(HttpCache, ETagGET_Http10) { 2384 TEST(HttpCache, ETagGET_Http10) {
2385 MockHttpCache cache; 2385 MockHttpCache cache(false);
2386 2386
2387 ScopedMockTransaction transaction(kETagGET_Transaction); 2387 ScopedMockTransaction transaction(kETagGET_Transaction);
2388 transaction.status = "HTTP/1.0 200 OK"; 2388 transaction.status = "HTTP/1.0 200 OK";
2389 2389
2390 // Write to the cache. 2390 // Write to the cache.
2391 RunTransactionTest(cache.http_cache(), transaction); 2391 RunTransactionTest(cache.http_cache(), transaction);
2392 2392
2393 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2393 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2394 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2394 EXPECT_EQ(0, cache.disk_cache()->open_count());
2395 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2395 EXPECT_EQ(1, cache.disk_cache()->create_count());
2396 2396
2397 // Get the same URL again, without generating a conditional request. 2397 // Get the same URL again, without generating a conditional request.
2398 transaction.load_flags = LOAD_VALIDATE_CACHE; 2398 transaction.load_flags = LOAD_VALIDATE_CACHE;
2399 transaction.handler = ETagGet_UnconditionalRequest_Handler; 2399 transaction.handler = ETagGet_UnconditionalRequest_Handler;
2400 RunTransactionTest(cache.http_cache(), transaction); 2400 RunTransactionTest(cache.http_cache(), transaction);
2401 2401
2402 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 2402 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2403 EXPECT_EQ(1, cache.disk_cache()->open_count()); 2403 EXPECT_EQ(1, cache.disk_cache()->open_count());
2404 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2404 EXPECT_EQ(1, cache.disk_cache()->create_count());
2405 } 2405 }
2406 2406
2407 TEST(HttpCache, ETagGET_Http10_Range) { 2407 TEST(HttpCache, ETagGET_Http10_Range) {
2408 MockHttpCache cache; 2408 MockHttpCache cache(false);
2409 2409
2410 ScopedMockTransaction transaction(kETagGET_Transaction); 2410 ScopedMockTransaction transaction(kETagGET_Transaction);
2411 transaction.status = "HTTP/1.0 200 OK"; 2411 transaction.status = "HTTP/1.0 200 OK";
2412 2412
2413 // Write to the cache. 2413 // Write to the cache.
2414 RunTransactionTest(cache.http_cache(), transaction); 2414 RunTransactionTest(cache.http_cache(), transaction);
2415 2415
2416 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2416 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2417 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2417 EXPECT_EQ(0, cache.disk_cache()->open_count());
2418 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2418 EXPECT_EQ(1, cache.disk_cache()->create_count());
(...skipping 15 matching lines...) Expand all
2434 std::string* response_headers, 2434 std::string* response_headers,
2435 std::string* response_data) { 2435 std::string* response_data) {
2436 EXPECT_TRUE( 2436 EXPECT_TRUE(
2437 request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch)); 2437 request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch));
2438 response_status->assign("HTTP/1.1 304 Not Modified"); 2438 response_status->assign("HTTP/1.1 304 Not Modified");
2439 response_headers->assign("Cache-Control: no-store\n"); 2439 response_headers->assign("Cache-Control: no-store\n");
2440 response_data->clear(); 2440 response_data->clear();
2441 } 2441 }
2442 2442
2443 TEST(HttpCache, ETagGET_ConditionalRequest_304_NoStore) { 2443 TEST(HttpCache, ETagGET_ConditionalRequest_304_NoStore) {
2444 MockHttpCache cache; 2444 MockHttpCache cache(false);
2445 2445
2446 ScopedMockTransaction transaction(kETagGET_Transaction); 2446 ScopedMockTransaction transaction(kETagGET_Transaction);
2447 2447
2448 // Write to the cache. 2448 // Write to the cache.
2449 RunTransactionTest(cache.http_cache(), transaction); 2449 RunTransactionTest(cache.http_cache(), transaction);
2450 2450
2451 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2451 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2452 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2452 EXPECT_EQ(0, cache.disk_cache()->open_count());
2453 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2453 EXPECT_EQ(1, cache.disk_cache()->create_count());
2454 2454
(...skipping 23 matching lines...) Expand all
2478 // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned. 2478 // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned.
2479 // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to 2479 // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to
2480 // be returned. 2480 // be returned.
2481 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be 2481 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be
2482 // returned. 2482 // returned.
2483 static void ConditionalizedRequestUpdatesCacheHelper( 2483 static void ConditionalizedRequestUpdatesCacheHelper(
2484 const Response& net_response_1, 2484 const Response& net_response_1,
2485 const Response& net_response_2, 2485 const Response& net_response_2,
2486 const Response& cached_response_2, 2486 const Response& cached_response_2,
2487 const char* extra_request_headers) { 2487 const char* extra_request_headers) {
2488 MockHttpCache cache; 2488 MockHttpCache cache(false);
2489 2489
2490 // The URL we will be requesting. 2490 // The URL we will be requesting.
2491 const char kUrl[] = "http://foobar.com/main.css"; 2491 const char kUrl[] = "http://foobar.com/main.css";
2492 2492
2493 // Junk network response. 2493 // Junk network response.
2494 static const Response kUnexpectedResponse = { 2494 static const Response kUnexpectedResponse = {
2495 "HTTP/1.1 500 Unexpected", 2495 "HTTP/1.1 500 Unexpected",
2496 "Server: unexpected_header", 2496 "Server: unexpected_header",
2497 "unexpected body" 2497 "unexpected body"
2498 }; 2498 };
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
2667 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"; 2667 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
2668 2668
2669 ConditionalizedRequestUpdatesCacheHelper( 2669 ConditionalizedRequestUpdatesCacheHelper(
2670 kNetResponse1, kNetResponse2, kCachedResponse2, extra_headers); 2670 kNetResponse1, kNetResponse2, kCachedResponse2, extra_headers);
2671 } 2671 }
2672 2672
2673 // Test that when doing an externally conditionalized if-modified-since 2673 // Test that when doing an externally conditionalized if-modified-since
2674 // and there is no corresponding cache entry, a new cache entry is NOT 2674 // and there is no corresponding cache entry, a new cache entry is NOT
2675 // created (304 response). 2675 // created (304 response).
2676 TEST(HttpCache, ConditionalizedRequestUpdatesCache4) { 2676 TEST(HttpCache, ConditionalizedRequestUpdatesCache4) {
2677 MockHttpCache cache; 2677 MockHttpCache cache(false);
2678 2678
2679 const char kUrl[] = "http://foobar.com/main.css"; 2679 const char kUrl[] = "http://foobar.com/main.css";
2680 2680
2681 static const Response kNetResponse = { 2681 static const Response kNetResponse = {
2682 "HTTP/1.1 304 Not Modified", 2682 "HTTP/1.1 304 Not Modified",
2683 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" 2683 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2684 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", 2684 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2685 "" 2685 ""
2686 }; 2686 };
2687 2687
(...skipping 23 matching lines...) Expand all
2711 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2711 EXPECT_EQ(0, cache.disk_cache()->open_count());
2712 EXPECT_EQ(0, cache.disk_cache()->create_count()); 2712 EXPECT_EQ(0, cache.disk_cache()->create_count());
2713 2713
2714 RemoveMockTransaction(&mock_network_response); 2714 RemoveMockTransaction(&mock_network_response);
2715 } 2715 }
2716 2716
2717 // Test that when doing an externally conditionalized if-modified-since 2717 // Test that when doing an externally conditionalized if-modified-since
2718 // and there is no corresponding cache entry, a new cache entry is NOT 2718 // and there is no corresponding cache entry, a new cache entry is NOT
2719 // created (200 response). 2719 // created (200 response).
2720 TEST(HttpCache, ConditionalizedRequestUpdatesCache5) { 2720 TEST(HttpCache, ConditionalizedRequestUpdatesCache5) {
2721 MockHttpCache cache; 2721 MockHttpCache cache(false);
2722 2722
2723 const char kUrl[] = "http://foobar.com/main.css"; 2723 const char kUrl[] = "http://foobar.com/main.css";
2724 2724
2725 static const Response kNetResponse = { 2725 static const Response kNetResponse = {
2726 "HTTP/1.1 200 OK", 2726 "HTTP/1.1 200 OK",
2727 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" 2727 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2728 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", 2728 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2729 "foobar!!!" 2729 "foobar!!!"
2730 }; 2730 };
2731 2731
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
2897 // The modification date doesn't match what we have stored. 2897 // The modification date doesn't match what we have stored.
2898 const char kExtraRequestHeaders[] = 2898 const char kExtraRequestHeaders[] =
2899 "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n" 2899 "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n"
2900 "If-None-Match: \"Foo1\"\r\n"; 2900 "If-None-Match: \"Foo1\"\r\n";
2901 2901
2902 ConditionalizedRequestUpdatesCacheHelper( 2902 ConditionalizedRequestUpdatesCacheHelper(
2903 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); 2903 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
2904 } 2904 }
2905 2905
2906 TEST(HttpCache, UrlContainingHash) { 2906 TEST(HttpCache, UrlContainingHash) {
2907 MockHttpCache cache; 2907 MockHttpCache cache(false);
2908 2908
2909 // Do a typical GET request -- should write an entry into our cache. 2909 // Do a typical GET request -- should write an entry into our cache.
2910 MockTransaction trans(kTypicalGET_Transaction); 2910 MockTransaction trans(kTypicalGET_Transaction);
2911 RunTransactionTest(cache.http_cache(), trans); 2911 RunTransactionTest(cache.http_cache(), trans);
2912 2912
2913 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2913 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2914 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2914 EXPECT_EQ(0, cache.disk_cache()->open_count());
2915 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2915 EXPECT_EQ(1, cache.disk_cache()->create_count());
2916 2916
2917 // Request the same URL, but this time with a reference section (hash). 2917 // Request the same URL, but this time with a reference section (hash).
2918 // Since the cache key strips the hash sections, this should be a cache hit. 2918 // Since the cache key strips the hash sections, this should be a cache hit.
2919 std::string url_with_hash = std::string(trans.url) + "#multiple#hashes"; 2919 std::string url_with_hash = std::string(trans.url) + "#multiple#hashes";
2920 trans.url = url_with_hash.c_str(); 2920 trans.url = url_with_hash.c_str();
2921 trans.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 2921 trans.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
2922 2922
2923 RunTransactionTest(cache.http_cache(), trans); 2923 RunTransactionTest(cache.http_cache(), trans);
2924 2924
2925 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2925 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2926 EXPECT_EQ(1, cache.disk_cache()->open_count()); 2926 EXPECT_EQ(1, cache.disk_cache()->open_count());
2927 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2927 EXPECT_EQ(1, cache.disk_cache()->create_count());
2928 } 2928 }
2929 2929
2930 // Tests that we skip the cache for POST requests that do not have an upload 2930 // Tests that we skip the cache for POST requests that do not have an upload
2931 // identifier. 2931 // identifier.
2932 TEST(HttpCache, SimplePOST_SkipsCache) { 2932 TEST(HttpCache, SimplePOST_SkipsCache) {
2933 MockHttpCache cache; 2933 MockHttpCache cache(false);
2934 2934
2935 RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction); 2935 RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction);
2936 2936
2937 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2937 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2938 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2938 EXPECT_EQ(0, cache.disk_cache()->open_count());
2939 EXPECT_EQ(0, cache.disk_cache()->create_count()); 2939 EXPECT_EQ(0, cache.disk_cache()->create_count());
2940 } 2940 }
2941 2941
2942 // Tests POST handling with a disabled cache (no DCHECK). 2942 // Tests POST handling with a disabled cache (no DCHECK).
2943 TEST(HttpCache, SimplePOST_DisabledCache) { 2943 TEST(HttpCache, SimplePOST_DisabledCache) {
2944 MockHttpCache cache; 2944 MockHttpCache cache(false);
2945 cache.http_cache()->set_mode(HttpCache::Mode::DISABLE); 2945 cache.http_cache()->set_mode(HttpCache::Mode::DISABLE);
2946 2946
2947 RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction); 2947 RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction);
2948 2948
2949 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2949 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2950 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2950 EXPECT_EQ(0, cache.disk_cache()->open_count());
2951 EXPECT_EQ(0, cache.disk_cache()->create_count()); 2951 EXPECT_EQ(0, cache.disk_cache()->create_count());
2952 } 2952 }
2953 2953
2954 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) { 2954 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) {
2955 MockHttpCache cache; 2955 MockHttpCache cache(false);
2956 2956
2957 MockTransaction transaction(kSimplePOST_Transaction); 2957 MockTransaction transaction(kSimplePOST_Transaction);
2958 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 2958 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
2959 2959
2960 MockHttpRequest request(transaction); 2960 MockHttpRequest request(transaction);
2961 TestCompletionCallback callback; 2961 TestCompletionCallback callback;
2962 2962
2963 std::unique_ptr<HttpTransaction> trans; 2963 std::unique_ptr<HttpTransaction> trans;
2964 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 2964 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
2965 ASSERT_TRUE(trans.get()); 2965 ASSERT_TRUE(trans.get());
2966 2966
2967 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 2967 int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
2968 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS)); 2968 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS));
2969 2969
2970 trans.reset(); 2970 trans.reset();
2971 2971
2972 EXPECT_EQ(0, cache.network_layer()->transaction_count()); 2972 EXPECT_EQ(0, cache.network_layer()->transaction_count());
2973 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2973 EXPECT_EQ(0, cache.disk_cache()->open_count());
2974 EXPECT_EQ(0, cache.disk_cache()->create_count()); 2974 EXPECT_EQ(0, cache.disk_cache()->create_count());
2975 } 2975 }
2976 2976
2977 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Hit) { 2977 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Hit) {
2978 MockHttpCache cache; 2978 MockHttpCache cache(false);
2979 2979
2980 // Test that we hit the cache for POST requests. 2980 // Test that we hit the cache for POST requests.
2981 2981
2982 MockTransaction transaction(kSimplePOST_Transaction); 2982 MockTransaction transaction(kSimplePOST_Transaction);
2983 2983
2984 const int64_t kUploadId = 1; // Just a dummy value. 2984 const int64_t kUploadId = 1; // Just a dummy value.
2985 2985
2986 std::vector<std::unique_ptr<UploadElementReader>> element_readers; 2986 std::vector<std::unique_ptr<UploadElementReader>> element_readers;
2987 element_readers.push_back( 2987 element_readers.push_back(
2988 base::MakeUnique<UploadBytesElementReader>("hello", 5)); 2988 base::MakeUnique<UploadBytesElementReader>("hello", 5));
(...skipping 13 matching lines...) Expand all
3002 request.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 3002 request.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
3003 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); 3003 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL);
3004 3004
3005 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3005 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3006 EXPECT_EQ(1, cache.disk_cache()->open_count()); 3006 EXPECT_EQ(1, cache.disk_cache()->open_count());
3007 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3007 EXPECT_EQ(1, cache.disk_cache()->create_count());
3008 } 3008 }
3009 3009
3010 // Test that we don't hit the cache for POST requests if there is a byte range. 3010 // Test that we don't hit the cache for POST requests if there is a byte range.
3011 TEST(HttpCache, SimplePOST_WithRanges) { 3011 TEST(HttpCache, SimplePOST_WithRanges) {
3012 MockHttpCache cache; 3012 MockHttpCache cache(false);
3013 3013
3014 MockTransaction transaction(kSimplePOST_Transaction); 3014 MockTransaction transaction(kSimplePOST_Transaction);
3015 transaction.request_headers = "Range: bytes = 0-4\r\n"; 3015 transaction.request_headers = "Range: bytes = 0-4\r\n";
3016 3016
3017 const int64_t kUploadId = 1; // Just a dummy value. 3017 const int64_t kUploadId = 1; // Just a dummy value.
3018 3018
3019 std::vector<std::unique_ptr<UploadElementReader>> element_readers; 3019 std::vector<std::unique_ptr<UploadElementReader>> element_readers;
3020 element_readers.push_back( 3020 element_readers.push_back(
3021 base::WrapUnique(new UploadBytesElementReader("hello", 5))); 3021 base::WrapUnique(new UploadBytesElementReader("hello", 5)));
3022 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 3022 ElementsUploadDataStream upload_data_stream(std::move(element_readers),
3023 kUploadId); 3023 kUploadId);
3024 3024
3025 MockHttpRequest request(transaction); 3025 MockHttpRequest request(transaction);
3026 request.upload_data_stream = &upload_data_stream; 3026 request.upload_data_stream = &upload_data_stream;
3027 3027
3028 // Attempt to populate the cache. 3028 // Attempt to populate the cache.
3029 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); 3029 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL);
3030 3030
3031 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3031 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3032 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3032 EXPECT_EQ(0, cache.disk_cache()->open_count());
3033 EXPECT_EQ(0, cache.disk_cache()->create_count()); 3033 EXPECT_EQ(0, cache.disk_cache()->create_count());
3034 } 3034 }
3035 3035
3036 // Tests that a POST is cached separately from a GET. 3036 // Tests that a POST is cached separately from a GET.
3037 TEST(HttpCache, SimplePOST_SeparateCache) { 3037 TEST(HttpCache, SimplePOST_SeparateCache) {
3038 MockHttpCache cache; 3038 MockHttpCache cache(false);
3039 3039
3040 std::vector<std::unique_ptr<UploadElementReader>> element_readers; 3040 std::vector<std::unique_ptr<UploadElementReader>> element_readers;
3041 element_readers.push_back( 3041 element_readers.push_back(
3042 base::WrapUnique(new UploadBytesElementReader("hello", 5))); 3042 base::WrapUnique(new UploadBytesElementReader("hello", 5)));
3043 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1); 3043 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1);
3044 3044
3045 MockTransaction transaction(kSimplePOST_Transaction); 3045 MockTransaction transaction(kSimplePOST_Transaction);
3046 MockHttpRequest req1(transaction); 3046 MockHttpRequest req1(transaction);
3047 req1.upload_data_stream = &upload_data_stream; 3047 req1.upload_data_stream = &upload_data_stream;
3048 3048
3049 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); 3049 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3050 3050
3051 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3051 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3052 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3052 EXPECT_EQ(0, cache.disk_cache()->open_count());
3053 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3053 EXPECT_EQ(1, cache.disk_cache()->create_count());
3054 3054
3055 transaction.method = "GET"; 3055 transaction.method = "GET";
3056 MockHttpRequest req2(transaction); 3056 MockHttpRequest req2(transaction);
3057 3057
3058 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); 3058 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL);
3059 3059
3060 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 3060 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3061 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3061 EXPECT_EQ(0, cache.disk_cache()->open_count());
3062 EXPECT_EQ(2, cache.disk_cache()->create_count()); 3062 EXPECT_EQ(2, cache.disk_cache()->create_count());
3063 } 3063 }
3064 3064
3065 // Tests that a successful POST invalidates a previously cached GET. 3065 // Tests that a successful POST invalidates a previously cached GET.
3066 TEST(HttpCache, SimplePOST_Invalidate_205) { 3066 TEST(HttpCache, SimplePOST_Invalidate_205) {
3067 MockHttpCache cache; 3067 MockHttpCache cache(false);
3068 3068
3069 MockTransaction transaction(kSimpleGET_Transaction); 3069 MockTransaction transaction(kSimpleGET_Transaction);
3070 AddMockTransaction(&transaction); 3070 AddMockTransaction(&transaction);
3071 MockHttpRequest req1(transaction); 3071 MockHttpRequest req1(transaction);
3072 3072
3073 // Attempt to populate the cache. 3073 // Attempt to populate the cache.
3074 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); 3074 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3075 3075
3076 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3076 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3077 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3077 EXPECT_EQ(0, cache.disk_cache()->open_count());
(...skipping 19 matching lines...) Expand all
3097 3097
3098 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 3098 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3099 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3099 EXPECT_EQ(0, cache.disk_cache()->open_count());
3100 EXPECT_EQ(3, cache.disk_cache()->create_count()); 3100 EXPECT_EQ(3, cache.disk_cache()->create_count());
3101 RemoveMockTransaction(&transaction); 3101 RemoveMockTransaction(&transaction);
3102 } 3102 }
3103 3103
3104 // Tests that a successful POST invalidates a previously cached GET, even when 3104 // Tests that a successful POST invalidates a previously cached GET, even when
3105 // there is no upload identifier. 3105 // there is no upload identifier.
3106 TEST(HttpCache, SimplePOST_NoUploadId_Invalidate_205) { 3106 TEST(HttpCache, SimplePOST_NoUploadId_Invalidate_205) {
3107 MockHttpCache cache; 3107 MockHttpCache cache(false);
3108 3108
3109 MockTransaction transaction(kSimpleGET_Transaction); 3109 MockTransaction transaction(kSimpleGET_Transaction);
3110 AddMockTransaction(&transaction); 3110 AddMockTransaction(&transaction);
3111 MockHttpRequest req1(transaction); 3111 MockHttpRequest req1(transaction);
3112 3112
3113 // Attempt to populate the cache. 3113 // Attempt to populate the cache.
3114 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); 3114 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3115 3115
3116 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3116 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3117 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3117 EXPECT_EQ(0, cache.disk_cache()->open_count());
(...skipping 23 matching lines...) Expand all
3141 RemoveMockTransaction(&transaction); 3141 RemoveMockTransaction(&transaction);
3142 } 3142 }
3143 3143
3144 // Tests that processing a POST before creating the backend doesn't crash. 3144 // Tests that processing a POST before creating the backend doesn't crash.
3145 TEST(HttpCache, SimplePOST_NoUploadId_NoBackend) { 3145 TEST(HttpCache, SimplePOST_NoUploadId_NoBackend) {
3146 // This will initialize a cache object with NULL backend. 3146 // This will initialize a cache object with NULL backend.
3147 std::unique_ptr<MockBlockingBackendFactory> factory( 3147 std::unique_ptr<MockBlockingBackendFactory> factory(
3148 new MockBlockingBackendFactory()); 3148 new MockBlockingBackendFactory());
3149 factory->set_fail(true); 3149 factory->set_fail(true);
3150 factory->FinishCreation(); 3150 factory->FinishCreation();
3151 MockHttpCache cache(std::move(factory)); 3151 MockHttpCache cache(std::move(factory), false);
3152 3152
3153 std::vector<std::unique_ptr<UploadElementReader>> element_readers; 3153 std::vector<std::unique_ptr<UploadElementReader>> element_readers;
3154 element_readers.push_back( 3154 element_readers.push_back(
3155 base::WrapUnique(new UploadBytesElementReader("hello", 5))); 3155 base::WrapUnique(new UploadBytesElementReader("hello", 5)));
3156 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); 3156 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0);
3157 3157
3158 MockTransaction transaction(kSimplePOST_Transaction); 3158 MockTransaction transaction(kSimplePOST_Transaction);
3159 AddMockTransaction(&transaction); 3159 AddMockTransaction(&transaction);
3160 MockHttpRequest req(transaction); 3160 MockHttpRequest req(transaction);
3161 req.upload_data_stream = &upload_data_stream; 3161 req.upload_data_stream = &upload_data_stream;
3162 3162
3163 RunTransactionTestWithRequest(cache.http_cache(), transaction, req, NULL); 3163 RunTransactionTestWithRequest(cache.http_cache(), transaction, req, NULL);
3164 3164
3165 RemoveMockTransaction(&transaction); 3165 RemoveMockTransaction(&transaction);
3166 } 3166 }
3167 3167
3168 // Tests that we don't invalidate entries as a result of a failed POST. 3168 // Tests that we don't invalidate entries as a result of a failed POST.
3169 TEST(HttpCache, SimplePOST_DontInvalidate_100) { 3169 TEST(HttpCache, SimplePOST_DontInvalidate_100) {
3170 MockHttpCache cache; 3170 MockHttpCache cache(false);
3171 3171
3172 MockTransaction transaction(kSimpleGET_Transaction); 3172 MockTransaction transaction(kSimpleGET_Transaction);
3173 AddMockTransaction(&transaction); 3173 AddMockTransaction(&transaction);
3174 MockHttpRequest req1(transaction); 3174 MockHttpRequest req1(transaction);
3175 3175
3176 // Attempt to populate the cache. 3176 // Attempt to populate the cache.
3177 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); 3177 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3178 3178
3179 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3179 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3180 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3180 EXPECT_EQ(0, cache.disk_cache()->open_count());
(...skipping 18 matching lines...) Expand all
3199 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); 3199 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3200 3200
3201 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 3201 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3202 EXPECT_EQ(1, cache.disk_cache()->open_count()); 3202 EXPECT_EQ(1, cache.disk_cache()->open_count());
3203 EXPECT_EQ(2, cache.disk_cache()->create_count()); 3203 EXPECT_EQ(2, cache.disk_cache()->create_count());
3204 RemoveMockTransaction(&transaction); 3204 RemoveMockTransaction(&transaction);
3205 } 3205 }
3206 3206
3207 // Tests that a HEAD request is not cached by itself. 3207 // Tests that a HEAD request is not cached by itself.
3208 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Miss) { 3208 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Miss) {
3209 MockHttpCache cache; 3209 MockHttpCache cache(false);
3210 MockTransaction transaction(kSimplePOST_Transaction); 3210 MockTransaction transaction(kSimplePOST_Transaction);
3211 AddMockTransaction(&transaction); 3211 AddMockTransaction(&transaction);
3212 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 3212 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
3213 transaction.method = "HEAD"; 3213 transaction.method = "HEAD";
3214 3214
3215 MockHttpRequest request(transaction); 3215 MockHttpRequest request(transaction);
3216 TestCompletionCallback callback; 3216 TestCompletionCallback callback;
3217 3217
3218 std::unique_ptr<HttpTransaction> trans; 3218 std::unique_ptr<HttpTransaction> trans;
3219 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 3219 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
3220 ASSERT_TRUE(trans.get()); 3220 ASSERT_TRUE(trans.get());
3221 3221
3222 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 3222 int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
3223 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS)); 3223 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS));
3224 3224
3225 trans.reset(); 3225 trans.reset();
3226 3226
3227 EXPECT_EQ(0, cache.network_layer()->transaction_count()); 3227 EXPECT_EQ(0, cache.network_layer()->transaction_count());
3228 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3228 EXPECT_EQ(0, cache.disk_cache()->open_count());
3229 EXPECT_EQ(0, cache.disk_cache()->create_count()); 3229 EXPECT_EQ(0, cache.disk_cache()->create_count());
3230 RemoveMockTransaction(&transaction); 3230 RemoveMockTransaction(&transaction);
3231 } 3231 }
3232 3232
3233 // Tests that a HEAD request is served from a cached GET. 3233 // Tests that a HEAD request is served from a cached GET.
3234 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Hit) { 3234 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Hit) {
3235 MockHttpCache cache; 3235 MockHttpCache cache(false);
3236 MockTransaction transaction(kSimpleGET_Transaction); 3236 MockTransaction transaction(kSimpleGET_Transaction);
3237 AddMockTransaction(&transaction); 3237 AddMockTransaction(&transaction);
3238 3238
3239 // Populate the cache. 3239 // Populate the cache.
3240 RunTransactionTest(cache.http_cache(), transaction); 3240 RunTransactionTest(cache.http_cache(), transaction);
3241 3241
3242 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3242 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3243 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3243 EXPECT_EQ(0, cache.disk_cache()->open_count());
3244 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3244 EXPECT_EQ(1, cache.disk_cache()->create_count());
3245 3245
3246 // Load from cache. 3246 // Load from cache.
3247 transaction.method = "HEAD"; 3247 transaction.method = "HEAD";
3248 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 3248 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
3249 transaction.data = ""; 3249 transaction.data = "";
3250 RunTransactionTest(cache.http_cache(), transaction); 3250 RunTransactionTest(cache.http_cache(), transaction);
3251 3251
3252 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3252 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3253 EXPECT_EQ(1, cache.disk_cache()->open_count()); 3253 EXPECT_EQ(1, cache.disk_cache()->open_count());
3254 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3254 EXPECT_EQ(1, cache.disk_cache()->create_count());
3255 RemoveMockTransaction(&transaction); 3255 RemoveMockTransaction(&transaction);
3256 } 3256 }
3257 3257
3258 // Tests that a read-only request served from the cache preserves CL. 3258 // Tests that a read-only request served from the cache preserves CL.
3259 TEST(HttpCache, SimpleHEAD_ContentLengthOnHit_Read) { 3259 TEST(HttpCache, SimpleHEAD_ContentLengthOnHit_Read) {
3260 MockHttpCache cache; 3260 MockHttpCache cache(false);
3261 MockTransaction transaction(kSimpleGET_Transaction); 3261 MockTransaction transaction(kSimpleGET_Transaction);
3262 AddMockTransaction(&transaction); 3262 AddMockTransaction(&transaction);
3263 transaction.response_headers = "Content-Length: 42\n"; 3263 transaction.response_headers = "Content-Length: 42\n";
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.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 3270 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
3271 transaction.data = ""; 3271 transaction.data = "";
3272 std::string headers; 3272 std::string headers;
3273 3273
3274 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 3274 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3275 3275
3276 EXPECT_EQ("HTTP/1.1 200 OK\nContent-Length: 42\n", headers); 3276 EXPECT_EQ("HTTP/1.1 200 OK\nContent-Length: 42\n", headers);
3277 RemoveMockTransaction(&transaction); 3277 RemoveMockTransaction(&transaction);
3278 } 3278 }
3279 3279
3280 // Tests that a read-write request served from the cache preserves CL. 3280 // Tests that a read-write request served from the cache preserves CL.
3281 TEST(HttpCache, ETagHEAD_ContentLengthOnHit_ReadWrite) { 3281 TEST(HttpCache, ETagHEAD_ContentLengthOnHit_ReadWrite) {
3282 MockHttpCache cache; 3282 MockHttpCache cache(false);
3283 MockTransaction transaction(kETagGET_Transaction); 3283 MockTransaction transaction(kETagGET_Transaction);
3284 AddMockTransaction(&transaction); 3284 AddMockTransaction(&transaction);
3285 std::string server_headers(kETagGET_Transaction.response_headers); 3285 std::string server_headers(kETagGET_Transaction.response_headers);
3286 server_headers.append("Content-Length: 42\n"); 3286 server_headers.append("Content-Length: 42\n");
3287 transaction.response_headers = server_headers.data(); 3287 transaction.response_headers = server_headers.data();
3288 3288
3289 // Populate the cache. 3289 // Populate the cache.
3290 RunTransactionTest(cache.http_cache(), transaction); 3290 RunTransactionTest(cache.http_cache(), transaction);
3291 3291
3292 // Load from cache. 3292 // Load from cache.
3293 transaction.method = "HEAD"; 3293 transaction.method = "HEAD";
3294 transaction.data = ""; 3294 transaction.data = "";
3295 std::string headers; 3295 std::string headers;
3296 3296
3297 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 3297 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3298 3298
3299 EXPECT_NE(std::string::npos, headers.find("Content-Length: 42\n")); 3299 EXPECT_NE(std::string::npos, headers.find("Content-Length: 42\n"));
3300 RemoveMockTransaction(&transaction); 3300 RemoveMockTransaction(&transaction);
3301 } 3301 }
3302 3302
3303 // Tests that a HEAD request that includes byte ranges bypasses the cache. 3303 // Tests that a HEAD request that includes byte ranges bypasses the cache.
3304 TEST(HttpCache, SimpleHEAD_WithRanges) { 3304 TEST(HttpCache, SimpleHEAD_WithRanges) {
3305 MockHttpCache cache; 3305 MockHttpCache cache(false);
3306 MockTransaction transaction(kSimpleGET_Transaction); 3306 MockTransaction transaction(kSimpleGET_Transaction);
3307 AddMockTransaction(&transaction); 3307 AddMockTransaction(&transaction);
3308 3308
3309 // Populate the cache. 3309 // Populate the cache.
3310 RunTransactionTest(cache.http_cache(), transaction); 3310 RunTransactionTest(cache.http_cache(), transaction);
3311 3311
3312 // Load from cache. 3312 // Load from cache.
3313 transaction.method = "HEAD"; 3313 transaction.method = "HEAD";
3314 transaction.request_headers = "Range: bytes = 0-4\r\n"; 3314 transaction.request_headers = "Range: bytes = 0-4\r\n";
3315 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 3315 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
3316 transaction.return_code = ERR_CACHE_MISS; 3316 transaction.return_code = ERR_CACHE_MISS;
3317 RunTransactionTest(cache.http_cache(), transaction); 3317 RunTransactionTest(cache.http_cache(), transaction);
3318 3318
3319 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3319 EXPECT_EQ(0, cache.disk_cache()->open_count());
3320 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3320 EXPECT_EQ(1, cache.disk_cache()->create_count());
3321 RemoveMockTransaction(&transaction); 3321 RemoveMockTransaction(&transaction);
3322 } 3322 }
3323 3323
3324 // Tests that a HEAD request can be served from a partialy cached resource. 3324 // Tests that a HEAD request can be served from a partialy cached resource.
3325 TEST(HttpCache, SimpleHEAD_WithCachedRanges) { 3325 TEST(HttpCache, SimpleHEAD_WithCachedRanges) {
3326 MockHttpCache cache; 3326 MockHttpCache cache(false);
3327 AddMockTransaction(&kRangeGET_TransactionOK); 3327 AddMockTransaction(&kRangeGET_TransactionOK);
3328 3328
3329 // Write to the cache (40-49). 3329 // Write to the cache (40-49).
3330 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); 3330 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
3331 RemoveMockTransaction(&kRangeGET_TransactionOK); 3331 RemoveMockTransaction(&kRangeGET_TransactionOK);
3332 3332
3333 MockTransaction transaction(kSimpleGET_Transaction); 3333 MockTransaction transaction(kSimpleGET_Transaction);
3334 3334
3335 transaction.url = kRangeGET_TransactionOK.url; 3335 transaction.url = kRangeGET_TransactionOK.url;
3336 transaction.method = "HEAD"; 3336 transaction.method = "HEAD";
3337 transaction.data = ""; 3337 transaction.data = "";
3338 AddMockTransaction(&transaction); 3338 AddMockTransaction(&transaction);
3339 std::string headers; 3339 std::string headers;
3340 3340
3341 // Load from cache. 3341 // Load from cache.
3342 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 3342 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3343 3343
3344 EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n")); 3344 EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n"));
3345 EXPECT_NE(std::string::npos, headers.find("Content-Length: 80\n")); 3345 EXPECT_NE(std::string::npos, headers.find("Content-Length: 80\n"));
3346 EXPECT_EQ(std::string::npos, headers.find("Content-Range")); 3346 EXPECT_EQ(std::string::npos, headers.find("Content-Range"));
3347 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3347 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3348 EXPECT_EQ(1, cache.disk_cache()->open_count()); 3348 EXPECT_EQ(1, cache.disk_cache()->open_count());
3349 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3349 EXPECT_EQ(1, cache.disk_cache()->create_count());
3350 RemoveMockTransaction(&transaction); 3350 RemoveMockTransaction(&transaction);
3351 } 3351 }
3352 3352
3353 // Tests that a HEAD request can be served from a truncated resource. 3353 // Tests that a HEAD request can be served from a truncated resource.
3354 TEST(HttpCache, SimpleHEAD_WithTruncatedEntry) { 3354 TEST(HttpCache, SimpleHEAD_WithTruncatedEntry) {
3355 MockHttpCache cache; 3355 MockHttpCache cache(false);
3356 AddMockTransaction(&kRangeGET_TransactionOK); 3356 AddMockTransaction(&kRangeGET_TransactionOK);
3357 3357
3358 std::string raw_headers("HTTP/1.1 200 OK\n" 3358 std::string raw_headers("HTTP/1.1 200 OK\n"
3359 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 3359 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
3360 "ETag: \"foo\"\n" 3360 "ETag: \"foo\"\n"
3361 "Accept-Ranges: bytes\n" 3361 "Accept-Ranges: bytes\n"
3362 "Content-Length: 80\n"); 3362 "Content-Length: 80\n");
3363 CreateTruncatedEntry(raw_headers, &cache); 3363 CreateTruncatedEntry(raw_headers, &cache);
3364 RemoveMockTransaction(&kRangeGET_TransactionOK); 3364 RemoveMockTransaction(&kRangeGET_TransactionOK);
3365 3365
(...skipping 12 matching lines...) Expand all
3378 EXPECT_NE(std::string::npos, headers.find("Content-Length: 80\n")); 3378 EXPECT_NE(std::string::npos, headers.find("Content-Length: 80\n"));
3379 EXPECT_EQ(std::string::npos, headers.find("Content-Range")); 3379 EXPECT_EQ(std::string::npos, headers.find("Content-Range"));
3380 EXPECT_EQ(0, cache.network_layer()->transaction_count()); 3380 EXPECT_EQ(0, cache.network_layer()->transaction_count());
3381 EXPECT_EQ(1, cache.disk_cache()->open_count()); 3381 EXPECT_EQ(1, cache.disk_cache()->open_count());
3382 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3382 EXPECT_EQ(1, cache.disk_cache()->create_count());
3383 RemoveMockTransaction(&transaction); 3383 RemoveMockTransaction(&transaction);
3384 } 3384 }
3385 3385
3386 // Tests that a HEAD request updates the cached response. 3386 // Tests that a HEAD request updates the cached response.
3387 TEST(HttpCache, TypicalHEAD_UpdatesResponse) { 3387 TEST(HttpCache, TypicalHEAD_UpdatesResponse) {
3388 MockHttpCache cache; 3388 MockHttpCache cache(false);
3389 MockTransaction transaction(kTypicalGET_Transaction); 3389 MockTransaction transaction(kTypicalGET_Transaction);
3390 AddMockTransaction(&transaction); 3390 AddMockTransaction(&transaction);
3391 3391
3392 // Populate the cache. 3392 // Populate the cache.
3393 RunTransactionTest(cache.http_cache(), transaction); 3393 RunTransactionTest(cache.http_cache(), transaction);
3394 3394
3395 // Update the cache. 3395 // Update the cache.
3396 transaction.method = "HEAD"; 3396 transaction.method = "HEAD";
3397 transaction.response_headers = "Foo: bar\n"; 3397 transaction.response_headers = "Foo: bar\n";
3398 transaction.data = ""; 3398 transaction.data = "";
(...skipping 17 matching lines...) Expand all
3416 3416
3417 EXPECT_NE(std::string::npos, headers.find("Foo: bar\n")); 3417 EXPECT_NE(std::string::npos, headers.find("Foo: bar\n"));
3418 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 3418 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3419 EXPECT_EQ(2, cache.disk_cache()->open_count()); 3419 EXPECT_EQ(2, cache.disk_cache()->open_count());
3420 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3420 EXPECT_EQ(1, cache.disk_cache()->create_count());
3421 RemoveMockTransaction(&transaction2); 3421 RemoveMockTransaction(&transaction2);
3422 } 3422 }
3423 3423
3424 // Tests that an externally conditionalized HEAD request updates the cache. 3424 // Tests that an externally conditionalized HEAD request updates the cache.
3425 TEST(HttpCache, TypicalHEAD_ConditionalizedRequestUpdatesResponse) { 3425 TEST(HttpCache, TypicalHEAD_ConditionalizedRequestUpdatesResponse) {
3426 MockHttpCache cache; 3426 MockHttpCache cache(false);
3427 MockTransaction transaction(kTypicalGET_Transaction); 3427 MockTransaction transaction(kTypicalGET_Transaction);
3428 AddMockTransaction(&transaction); 3428 AddMockTransaction(&transaction);
3429 3429
3430 // Populate the cache. 3430 // Populate the cache.
3431 RunTransactionTest(cache.http_cache(), transaction); 3431 RunTransactionTest(cache.http_cache(), transaction);
3432 3432
3433 // Update the cache. 3433 // Update the cache.
3434 transaction.method = "HEAD"; 3434 transaction.method = "HEAD";
3435 transaction.request_headers = 3435 transaction.request_headers =
3436 "If-Modified-Since: Wed, 28 Nov 2007 00:40:09 GMT\r\n"; 3436 "If-Modified-Since: Wed, 28 Nov 2007 00:40:09 GMT\r\n";
(...skipping 19 matching lines...) Expand all
3456 3456
3457 EXPECT_NE(std::string::npos, headers.find("Foo: bar\n")); 3457 EXPECT_NE(std::string::npos, headers.find("Foo: bar\n"));
3458 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 3458 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3459 EXPECT_EQ(2, cache.disk_cache()->open_count()); 3459 EXPECT_EQ(2, cache.disk_cache()->open_count());
3460 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3460 EXPECT_EQ(1, cache.disk_cache()->create_count());
3461 RemoveMockTransaction(&transaction2); 3461 RemoveMockTransaction(&transaction2);
3462 } 3462 }
3463 3463
3464 // Tests that a HEAD request invalidates an old cached entry. 3464 // Tests that a HEAD request invalidates an old cached entry.
3465 TEST(HttpCache, SimpleHEAD_InvalidatesEntry) { 3465 TEST(HttpCache, SimpleHEAD_InvalidatesEntry) {
3466 MockHttpCache cache; 3466 MockHttpCache cache(false);
3467 MockTransaction transaction(kTypicalGET_Transaction); 3467 MockTransaction transaction(kTypicalGET_Transaction);
3468 AddMockTransaction(&transaction); 3468 AddMockTransaction(&transaction);
3469 3469
3470 // Populate the cache. 3470 // Populate the cache.
3471 RunTransactionTest(cache.http_cache(), transaction); 3471 RunTransactionTest(cache.http_cache(), transaction);
3472 3472
3473 // Update the cache. 3473 // Update the cache.
3474 transaction.method = "HEAD"; 3474 transaction.method = "HEAD";
3475 transaction.data = ""; 3475 transaction.data = "";
3476 RunTransactionTest(cache.http_cache(), transaction); 3476 RunTransactionTest(cache.http_cache(), transaction);
3477 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 3477 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3478 3478
3479 // Load from the cache. 3479 // Load from the cache.
3480 transaction.method = "GET"; 3480 transaction.method = "GET";
3481 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 3481 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
3482 transaction.return_code = ERR_CACHE_MISS; 3482 transaction.return_code = ERR_CACHE_MISS;
3483 RunTransactionTest(cache.http_cache(), transaction); 3483 RunTransactionTest(cache.http_cache(), transaction);
3484 3484
3485 RemoveMockTransaction(&transaction); 3485 RemoveMockTransaction(&transaction);
3486 } 3486 }
3487 3487
3488 // Tests that we do not cache the response of a PUT. 3488 // Tests that we do not cache the response of a PUT.
3489 TEST(HttpCache, SimplePUT_Miss) { 3489 TEST(HttpCache, SimplePUT_Miss) {
3490 MockHttpCache cache; 3490 MockHttpCache cache(false);
3491 3491
3492 MockTransaction transaction(kSimplePOST_Transaction); 3492 MockTransaction transaction(kSimplePOST_Transaction);
3493 transaction.method = "PUT"; 3493 transaction.method = "PUT";
3494 3494
3495 std::vector<std::unique_ptr<UploadElementReader>> element_readers; 3495 std::vector<std::unique_ptr<UploadElementReader>> element_readers;
3496 element_readers.push_back( 3496 element_readers.push_back(
3497 base::WrapUnique(new UploadBytesElementReader("hello", 5))); 3497 base::WrapUnique(new UploadBytesElementReader("hello", 5)));
3498 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); 3498 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0);
3499 3499
3500 MockHttpRequest request(transaction); 3500 MockHttpRequest request(transaction);
3501 request.upload_data_stream = &upload_data_stream; 3501 request.upload_data_stream = &upload_data_stream;
3502 3502
3503 // Attempt to populate the cache. 3503 // Attempt to populate the cache.
3504 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); 3504 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL);
3505 3505
3506 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3506 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3507 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3507 EXPECT_EQ(0, cache.disk_cache()->open_count());
3508 EXPECT_EQ(0, cache.disk_cache()->create_count()); 3508 EXPECT_EQ(0, cache.disk_cache()->create_count());
3509 } 3509 }
3510 3510
3511 // Tests that we invalidate entries as a result of a PUT. 3511 // Tests that we invalidate entries as a result of a PUT.
3512 TEST(HttpCache, SimplePUT_Invalidate) { 3512 TEST(HttpCache, SimplePUT_Invalidate) {
3513 MockHttpCache cache; 3513 MockHttpCache cache(false);
3514 3514
3515 MockTransaction transaction(kSimpleGET_Transaction); 3515 MockTransaction transaction(kSimpleGET_Transaction);
3516 MockHttpRequest req1(transaction); 3516 MockHttpRequest req1(transaction);
3517 3517
3518 // Attempt to populate the cache. 3518 // Attempt to populate the cache.
3519 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); 3519 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3520 3520
3521 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3521 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3522 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3522 EXPECT_EQ(0, cache.disk_cache()->open_count());
3523 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3523 EXPECT_EQ(1, cache.disk_cache()->create_count());
(...skipping 15 matching lines...) Expand all
3539 3539
3540 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); 3540 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3541 3541
3542 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 3542 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3543 EXPECT_EQ(1, cache.disk_cache()->open_count()); 3543 EXPECT_EQ(1, cache.disk_cache()->open_count());
3544 EXPECT_EQ(2, cache.disk_cache()->create_count()); 3544 EXPECT_EQ(2, cache.disk_cache()->create_count());
3545 } 3545 }
3546 3546
3547 // Tests that we invalidate entries as a result of a PUT. 3547 // Tests that we invalidate entries as a result of a PUT.
3548 TEST(HttpCache, SimplePUT_Invalidate_305) { 3548 TEST(HttpCache, SimplePUT_Invalidate_305) {
3549 MockHttpCache cache; 3549 MockHttpCache cache(false);
3550 3550
3551 MockTransaction transaction(kSimpleGET_Transaction); 3551 MockTransaction transaction(kSimpleGET_Transaction);
3552 AddMockTransaction(&transaction); 3552 AddMockTransaction(&transaction);
3553 MockHttpRequest req1(transaction); 3553 MockHttpRequest req1(transaction);
3554 3554
3555 // Attempt to populate the cache. 3555 // Attempt to populate the cache.
3556 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); 3556 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3557 3557
3558 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3558 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3559 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3559 EXPECT_EQ(0, cache.disk_cache()->open_count());
(...skipping 18 matching lines...) Expand all
3578 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); 3578 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3579 3579
3580 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 3580 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3581 EXPECT_EQ(1, cache.disk_cache()->open_count()); 3581 EXPECT_EQ(1, cache.disk_cache()->open_count());
3582 EXPECT_EQ(2, cache.disk_cache()->create_count()); 3582 EXPECT_EQ(2, cache.disk_cache()->create_count());
3583 RemoveMockTransaction(&transaction); 3583 RemoveMockTransaction(&transaction);
3584 } 3584 }
3585 3585
3586 // Tests that we don't invalidate entries as a result of a failed PUT. 3586 // Tests that we don't invalidate entries as a result of a failed PUT.
3587 TEST(HttpCache, SimplePUT_DontInvalidate_404) { 3587 TEST(HttpCache, SimplePUT_DontInvalidate_404) {
3588 MockHttpCache cache; 3588 MockHttpCache cache(false);
3589 3589
3590 MockTransaction transaction(kSimpleGET_Transaction); 3590 MockTransaction transaction(kSimpleGET_Transaction);
3591 AddMockTransaction(&transaction); 3591 AddMockTransaction(&transaction);
3592 MockHttpRequest req1(transaction); 3592 MockHttpRequest req1(transaction);
3593 3593
3594 // Attempt to populate the cache. 3594 // Attempt to populate the cache.
3595 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); 3595 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3596 3596
3597 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3597 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3598 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3598 EXPECT_EQ(0, cache.disk_cache()->open_count());
(...skipping 18 matching lines...) Expand all
3617 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); 3617 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3618 3618
3619 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 3619 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3620 EXPECT_EQ(2, cache.disk_cache()->open_count()); 3620 EXPECT_EQ(2, cache.disk_cache()->open_count());
3621 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3621 EXPECT_EQ(1, cache.disk_cache()->create_count());
3622 RemoveMockTransaction(&transaction); 3622 RemoveMockTransaction(&transaction);
3623 } 3623 }
3624 3624
3625 // Tests that we do not cache the response of a DELETE. 3625 // Tests that we do not cache the response of a DELETE.
3626 TEST(HttpCache, SimpleDELETE_Miss) { 3626 TEST(HttpCache, SimpleDELETE_Miss) {
3627 MockHttpCache cache; 3627 MockHttpCache cache(false);
3628 3628
3629 MockTransaction transaction(kSimplePOST_Transaction); 3629 MockTransaction transaction(kSimplePOST_Transaction);
3630 transaction.method = "DELETE"; 3630 transaction.method = "DELETE";
3631 3631
3632 std::vector<std::unique_ptr<UploadElementReader>> element_readers; 3632 std::vector<std::unique_ptr<UploadElementReader>> element_readers;
3633 element_readers.push_back( 3633 element_readers.push_back(
3634 base::WrapUnique(new UploadBytesElementReader("hello", 5))); 3634 base::WrapUnique(new UploadBytesElementReader("hello", 5)));
3635 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); 3635 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0);
3636 3636
3637 MockHttpRequest request(transaction); 3637 MockHttpRequest request(transaction);
3638 request.upload_data_stream = &upload_data_stream; 3638 request.upload_data_stream = &upload_data_stream;
3639 3639
3640 // Attempt to populate the cache. 3640 // Attempt to populate the cache.
3641 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); 3641 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL);
3642 3642
3643 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3643 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3644 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3644 EXPECT_EQ(0, cache.disk_cache()->open_count());
3645 EXPECT_EQ(0, cache.disk_cache()->create_count()); 3645 EXPECT_EQ(0, cache.disk_cache()->create_count());
3646 } 3646 }
3647 3647
3648 // Tests that we invalidate entries as a result of a DELETE. 3648 // Tests that we invalidate entries as a result of a DELETE.
3649 TEST(HttpCache, SimpleDELETE_Invalidate) { 3649 TEST(HttpCache, SimpleDELETE_Invalidate) {
3650 MockHttpCache cache; 3650 MockHttpCache cache(false);
3651 3651
3652 MockTransaction transaction(kSimpleGET_Transaction); 3652 MockTransaction transaction(kSimpleGET_Transaction);
3653 MockHttpRequest req1(transaction); 3653 MockHttpRequest req1(transaction);
3654 3654
3655 // Attempt to populate the cache. 3655 // Attempt to populate the cache.
3656 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); 3656 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3657 3657
3658 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3658 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3659 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3659 EXPECT_EQ(0, cache.disk_cache()->open_count());
3660 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3660 EXPECT_EQ(1, cache.disk_cache()->create_count());
(...skipping 15 matching lines...) Expand all
3676 3676
3677 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); 3677 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL);
3678 3678
3679 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 3679 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3680 EXPECT_EQ(1, cache.disk_cache()->open_count()); 3680 EXPECT_EQ(1, cache.disk_cache()->open_count());
3681 EXPECT_EQ(2, cache.disk_cache()->create_count()); 3681 EXPECT_EQ(2, cache.disk_cache()->create_count());
3682 } 3682 }
3683 3683
3684 // Tests that we invalidate entries as a result of a DELETE. 3684 // Tests that we invalidate entries as a result of a DELETE.
3685 TEST(HttpCache, SimpleDELETE_Invalidate_301) { 3685 TEST(HttpCache, SimpleDELETE_Invalidate_301) {
3686 MockHttpCache cache; 3686 MockHttpCache cache(false);
3687 3687
3688 MockTransaction transaction(kSimpleGET_Transaction); 3688 MockTransaction transaction(kSimpleGET_Transaction);
3689 AddMockTransaction(&transaction); 3689 AddMockTransaction(&transaction);
3690 3690
3691 // Attempt to populate the cache. 3691 // Attempt to populate the cache.
3692 RunTransactionTest(cache.http_cache(), transaction); 3692 RunTransactionTest(cache.http_cache(), transaction);
3693 3693
3694 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3694 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3695 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3695 EXPECT_EQ(0, cache.disk_cache()->open_count());
3696 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3696 EXPECT_EQ(1, cache.disk_cache()->create_count());
(...skipping 11 matching lines...) Expand all
3708 RunTransactionTest(cache.http_cache(), transaction); 3708 RunTransactionTest(cache.http_cache(), transaction);
3709 3709
3710 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 3710 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3711 EXPECT_EQ(1, cache.disk_cache()->open_count()); 3711 EXPECT_EQ(1, cache.disk_cache()->open_count());
3712 EXPECT_EQ(2, cache.disk_cache()->create_count()); 3712 EXPECT_EQ(2, cache.disk_cache()->create_count());
3713 RemoveMockTransaction(&transaction); 3713 RemoveMockTransaction(&transaction);
3714 } 3714 }
3715 3715
3716 // Tests that we don't invalidate entries as a result of a failed DELETE. 3716 // Tests that we don't invalidate entries as a result of a failed DELETE.
3717 TEST(HttpCache, SimpleDELETE_DontInvalidate_416) { 3717 TEST(HttpCache, SimpleDELETE_DontInvalidate_416) {
3718 MockHttpCache cache; 3718 MockHttpCache cache(false);
3719 3719
3720 MockTransaction transaction(kSimpleGET_Transaction); 3720 MockTransaction transaction(kSimpleGET_Transaction);
3721 AddMockTransaction(&transaction); 3721 AddMockTransaction(&transaction);
3722 3722
3723 // Attempt to populate the cache. 3723 // Attempt to populate the cache.
3724 RunTransactionTest(cache.http_cache(), transaction); 3724 RunTransactionTest(cache.http_cache(), transaction);
3725 3725
3726 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3726 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3727 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3727 EXPECT_EQ(0, cache.disk_cache()->open_count());
3728 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3728 EXPECT_EQ(1, cache.disk_cache()->create_count());
(...skipping 12 matching lines...) Expand all
3741 RunTransactionTest(cache.http_cache(), transaction); 3741 RunTransactionTest(cache.http_cache(), transaction);
3742 3742
3743 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 3743 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3744 EXPECT_EQ(2, cache.disk_cache()->open_count()); 3744 EXPECT_EQ(2, cache.disk_cache()->open_count());
3745 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3745 EXPECT_EQ(1, cache.disk_cache()->create_count());
3746 RemoveMockTransaction(&transaction); 3746 RemoveMockTransaction(&transaction);
3747 } 3747 }
3748 3748
3749 // Tests that we don't invalidate entries after a failed network transaction. 3749 // Tests that we don't invalidate entries after a failed network transaction.
3750 TEST(HttpCache, SimpleGET_DontInvalidateOnFailure) { 3750 TEST(HttpCache, SimpleGET_DontInvalidateOnFailure) {
3751 MockHttpCache cache; 3751 MockHttpCache cache(false);
3752 3752
3753 // Populate the cache. 3753 // Populate the cache.
3754 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 3754 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
3755 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3755 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3756 3756
3757 // Fail the network request. 3757 // Fail the network request.
3758 MockTransaction transaction(kSimpleGET_Transaction); 3758 MockTransaction transaction(kSimpleGET_Transaction);
3759 transaction.return_code = ERR_FAILED; 3759 transaction.return_code = ERR_FAILED;
3760 transaction.load_flags |= LOAD_VALIDATE_CACHE; 3760 transaction.load_flags |= LOAD_VALIDATE_CACHE;
3761 3761
3762 AddMockTransaction(&transaction); 3762 AddMockTransaction(&transaction);
3763 RunTransactionTest(cache.http_cache(), transaction); 3763 RunTransactionTest(cache.http_cache(), transaction);
3764 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 3764 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3765 RemoveMockTransaction(&transaction); 3765 RemoveMockTransaction(&transaction);
3766 3766
3767 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 3767 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
3768 transaction.return_code = OK; 3768 transaction.return_code = OK;
3769 AddMockTransaction(&transaction); 3769 AddMockTransaction(&transaction);
3770 RunTransactionTest(cache.http_cache(), transaction); 3770 RunTransactionTest(cache.http_cache(), transaction);
3771 3771
3772 // Make sure the transaction didn't reach the network. 3772 // Make sure the transaction didn't reach the network.
3773 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 3773 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3774 RemoveMockTransaction(&transaction); 3774 RemoveMockTransaction(&transaction);
3775 } 3775 }
3776 3776
3777 TEST(HttpCache, RangeGET_SkipsCache) { 3777 TEST(HttpCache, RangeGET_SkipsCache) {
3778 MockHttpCache cache; 3778 MockHttpCache cache(false);
3779 3779
3780 // Test that we skip the cache for range GET requests. Eventually, we will 3780 // Test that we skip the cache for range GET requests. Eventually, we will
3781 // want to cache these, but we'll still have cases where skipping the cache 3781 // want to cache these, but we'll still have cases where skipping the cache
3782 // makes sense, so we want to make sure that it works properly. 3782 // makes sense, so we want to make sure that it works properly.
3783 3783
3784 RunTransactionTest(cache.http_cache(), kRangeGET_Transaction); 3784 RunTransactionTest(cache.http_cache(), kRangeGET_Transaction);
3785 3785
3786 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3786 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3787 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3787 EXPECT_EQ(0, cache.disk_cache()->open_count());
3788 EXPECT_EQ(0, cache.disk_cache()->create_count()); 3788 EXPECT_EQ(0, cache.disk_cache()->create_count());
(...skipping 11 matching lines...) Expand all
3800 RunTransactionTest(cache.http_cache(), transaction); 3800 RunTransactionTest(cache.http_cache(), transaction);
3801 3801
3802 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 3802 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3803 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3803 EXPECT_EQ(0, cache.disk_cache()->open_count());
3804 EXPECT_EQ(0, cache.disk_cache()->create_count()); 3804 EXPECT_EQ(0, cache.disk_cache()->create_count());
3805 } 3805 }
3806 3806
3807 // Test that we skip the cache for range requests that include a validation 3807 // Test that we skip the cache for range requests that include a validation
3808 // header. 3808 // header.
3809 TEST(HttpCache, RangeGET_SkipsCache2) { 3809 TEST(HttpCache, RangeGET_SkipsCache2) {
3810 MockHttpCache cache; 3810 MockHttpCache cache(false);
3811 3811
3812 MockTransaction transaction(kRangeGET_Transaction); 3812 MockTransaction transaction(kRangeGET_Transaction);
3813 transaction.request_headers = "If-None-Match: foo\r\n" 3813 transaction.request_headers = "If-None-Match: foo\r\n"
3814 EXTRA_HEADER 3814 EXTRA_HEADER
3815 "Range: bytes = 40-49\r\n"; 3815 "Range: bytes = 40-49\r\n";
3816 RunTransactionTest(cache.http_cache(), transaction); 3816 RunTransactionTest(cache.http_cache(), transaction);
3817 3817
3818 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3818 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3819 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3819 EXPECT_EQ(0, cache.disk_cache()->open_count());
3820 EXPECT_EQ(0, cache.disk_cache()->create_count()); 3820 EXPECT_EQ(0, cache.disk_cache()->create_count());
(...skipping 12 matching lines...) Expand all
3833 EXTRA_HEADER 3833 EXTRA_HEADER
3834 "Range: bytes = 40-49\r\n"; 3834 "Range: bytes = 40-49\r\n";
3835 RunTransactionTest(cache.http_cache(), transaction); 3835 RunTransactionTest(cache.http_cache(), transaction);
3836 3836
3837 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 3837 EXPECT_EQ(3, cache.network_layer()->transaction_count());
3838 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3838 EXPECT_EQ(0, cache.disk_cache()->open_count());
3839 EXPECT_EQ(0, cache.disk_cache()->create_count()); 3839 EXPECT_EQ(0, cache.disk_cache()->create_count());
3840 } 3840 }
3841 3841
3842 TEST(HttpCache, SimpleGET_DoesntLogHeaders) { 3842 TEST(HttpCache, SimpleGET_DoesntLogHeaders) {
3843 MockHttpCache cache; 3843 MockHttpCache cache(false);
3844 3844
3845 BoundTestNetLog log; 3845 BoundTestNetLog log;
3846 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, 3846 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction,
3847 log.bound()); 3847 log.bound());
3848 3848
3849 EXPECT_FALSE(LogContainsEventType( 3849 EXPECT_FALSE(LogContainsEventType(
3850 log, NetLogEventType::HTTP_CACHE_CALLER_REQUEST_HEADERS)); 3850 log, NetLogEventType::HTTP_CACHE_CALLER_REQUEST_HEADERS));
3851 } 3851 }
3852 3852
3853 TEST(HttpCache, RangeGET_LogsHeaders) { 3853 TEST(HttpCache, RangeGET_LogsHeaders) {
3854 MockHttpCache cache; 3854 MockHttpCache cache(false);
3855 3855
3856 BoundTestNetLog log; 3856 BoundTestNetLog log;
3857 RunTransactionTestWithLog(cache.http_cache(), kRangeGET_Transaction, 3857 RunTransactionTestWithLog(cache.http_cache(), kRangeGET_Transaction,
3858 log.bound()); 3858 log.bound());
3859 3859
3860 EXPECT_TRUE(LogContainsEventType( 3860 EXPECT_TRUE(LogContainsEventType(
3861 log, NetLogEventType::HTTP_CACHE_CALLER_REQUEST_HEADERS)); 3861 log, NetLogEventType::HTTP_CACHE_CALLER_REQUEST_HEADERS));
3862 } 3862 }
3863 3863
3864 TEST(HttpCache, ExternalValidation_LogsHeaders) { 3864 TEST(HttpCache, ExternalValidation_LogsHeaders) {
3865 MockHttpCache cache; 3865 MockHttpCache cache(false);
3866 3866
3867 BoundTestNetLog log; 3867 BoundTestNetLog log;
3868 MockTransaction transaction(kSimpleGET_Transaction); 3868 MockTransaction transaction(kSimpleGET_Transaction);
3869 transaction.request_headers = "If-None-Match: foo\r\n" EXTRA_HEADER; 3869 transaction.request_headers = "If-None-Match: foo\r\n" EXTRA_HEADER;
3870 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); 3870 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound());
3871 3871
3872 EXPECT_TRUE(LogContainsEventType( 3872 EXPECT_TRUE(LogContainsEventType(
3873 log, NetLogEventType::HTTP_CACHE_CALLER_REQUEST_HEADERS)); 3873 log, NetLogEventType::HTTP_CACHE_CALLER_REQUEST_HEADERS));
3874 } 3874 }
3875 3875
3876 TEST(HttpCache, SpecialHeaders_LogsHeaders) { 3876 TEST(HttpCache, SpecialHeaders_LogsHeaders) {
3877 MockHttpCache cache; 3877 MockHttpCache cache(false);
3878 3878
3879 BoundTestNetLog log; 3879 BoundTestNetLog log;
3880 MockTransaction transaction(kSimpleGET_Transaction); 3880 MockTransaction transaction(kSimpleGET_Transaction);
3881 transaction.request_headers = "cache-control: no-cache\r\n" EXTRA_HEADER; 3881 transaction.request_headers = "cache-control: no-cache\r\n" EXTRA_HEADER;
3882 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); 3882 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound());
3883 3883
3884 EXPECT_TRUE(LogContainsEventType( 3884 EXPECT_TRUE(LogContainsEventType(
3885 log, NetLogEventType::HTTP_CACHE_CALLER_REQUEST_HEADERS)); 3885 log, NetLogEventType::HTTP_CACHE_CALLER_REQUEST_HEADERS));
3886 } 3886 }
3887 3887
3888 // Tests that receiving 206 for a regular request is handled correctly. 3888 // Tests that receiving 206 for a regular request is handled correctly.
3889 TEST(HttpCache, GET_Crazy206) { 3889 TEST(HttpCache, GET_Crazy206) {
3890 MockHttpCache cache; 3890 MockHttpCache cache(false);
3891 3891
3892 // Write to the cache. 3892 // Write to the cache.
3893 MockTransaction transaction(kRangeGET_TransactionOK); 3893 MockTransaction transaction(kRangeGET_TransactionOK);
3894 AddMockTransaction(&transaction); 3894 AddMockTransaction(&transaction);
3895 transaction.request_headers = EXTRA_HEADER; 3895 transaction.request_headers = EXTRA_HEADER;
3896 transaction.handler = NULL; 3896 transaction.handler = NULL;
3897 RunTransactionTest(cache.http_cache(), transaction); 3897 RunTransactionTest(cache.http_cache(), transaction);
3898 3898
3899 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3899 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3900 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3900 EXPECT_EQ(0, cache.disk_cache()->open_count());
3901 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3901 EXPECT_EQ(1, cache.disk_cache()->create_count());
3902 3902
3903 // This should read again from the net. 3903 // This should read again from the net.
3904 RunTransactionTest(cache.http_cache(), transaction); 3904 RunTransactionTest(cache.http_cache(), transaction);
3905 3905
3906 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 3906 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3907 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3907 EXPECT_EQ(0, cache.disk_cache()->open_count());
3908 EXPECT_EQ(2, cache.disk_cache()->create_count()); 3908 EXPECT_EQ(2, cache.disk_cache()->create_count());
3909 RemoveMockTransaction(&transaction); 3909 RemoveMockTransaction(&transaction);
3910 } 3910 }
3911 3911
3912 // Tests that receiving 416 for a regular request is handled correctly. 3912 // Tests that receiving 416 for a regular request is handled correctly.
3913 TEST(HttpCache, GET_Crazy416) { 3913 TEST(HttpCache, GET_Crazy416) {
3914 MockHttpCache cache; 3914 MockHttpCache cache(false);
3915 3915
3916 // Write to the cache. 3916 // Write to the cache.
3917 MockTransaction transaction(kSimpleGET_Transaction); 3917 MockTransaction transaction(kSimpleGET_Transaction);
3918 AddMockTransaction(&transaction); 3918 AddMockTransaction(&transaction);
3919 transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable"; 3919 transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable";
3920 RunTransactionTest(cache.http_cache(), transaction); 3920 RunTransactionTest(cache.http_cache(), transaction);
3921 3921
3922 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3922 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3923 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3923 EXPECT_EQ(0, cache.disk_cache()->open_count());
3924 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3924 EXPECT_EQ(1, cache.disk_cache()->create_count());
3925 3925
3926 RemoveMockTransaction(&transaction); 3926 RemoveMockTransaction(&transaction);
3927 } 3927 }
3928 3928
3929 // Tests that we don't store partial responses that can't be validated. 3929 // Tests that we don't store partial responses that can't be validated.
3930 TEST(HttpCache, RangeGET_NoStrongValidators) { 3930 TEST(HttpCache, RangeGET_NoStrongValidators) {
3931 MockHttpCache cache; 3931 MockHttpCache cache(false);
3932 std::string headers; 3932 std::string headers;
3933 3933
3934 // Attempt to write to the cache (40-49). 3934 // Attempt to write to the cache (40-49).
3935 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 3935 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
3936 transaction.response_headers = "Content-Length: 10\n" 3936 transaction.response_headers = "Content-Length: 10\n"
3937 "Cache-Control: max-age=3600\n" 3937 "Cache-Control: max-age=3600\n"
3938 "ETag: w/\"foo\"\n"; 3938 "ETag: w/\"foo\"\n";
3939 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 3939 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3940 3940
3941 Verify206Response(headers, 40, 49); 3941 Verify206Response(headers, 40, 49);
3942 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3942 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3943 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3943 EXPECT_EQ(0, cache.disk_cache()->open_count());
3944 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3944 EXPECT_EQ(1, cache.disk_cache()->create_count());
3945 3945
3946 // Now verify that there's no cached data. 3946 // Now verify that there's no cached data.
3947 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, 3947 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
3948 &headers); 3948 &headers);
3949 3949
3950 Verify206Response(headers, 40, 49); 3950 Verify206Response(headers, 40, 49);
3951 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 3951 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3952 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3952 EXPECT_EQ(0, cache.disk_cache()->open_count());
3953 EXPECT_EQ(2, cache.disk_cache()->create_count()); 3953 EXPECT_EQ(2, cache.disk_cache()->create_count());
3954 } 3954 }
3955 3955
3956 // Tests failures to conditionalize byte range requests. 3956 // Tests failures to conditionalize byte range requests.
3957 TEST(HttpCache, RangeGET_NoConditionalization) { 3957 TEST(HttpCache, RangeGET_NoConditionalization) {
3958 MockHttpCache cache; 3958 MockHttpCache cache(false);
3959 cache.FailConditionalizations(); 3959 cache.FailConditionalizations();
3960 std::string headers; 3960 std::string headers;
3961 3961
3962 // Write to the cache (40-49). 3962 // Write to the cache (40-49).
3963 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 3963 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
3964 transaction.response_headers = "Content-Length: 10\n" 3964 transaction.response_headers = "Content-Length: 10\n"
3965 "ETag: \"foo\"\n"; 3965 "ETag: \"foo\"\n";
3966 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 3966 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
3967 3967
3968 Verify206Response(headers, 40, 49); 3968 Verify206Response(headers, 40, 49);
3969 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3969 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3970 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3970 EXPECT_EQ(0, cache.disk_cache()->open_count());
3971 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3971 EXPECT_EQ(1, cache.disk_cache()->create_count());
3972 3972
3973 // Now verify that the cached data is not used. 3973 // Now verify that the cached data is not used.
3974 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, 3974 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
3975 &headers); 3975 &headers);
3976 3976
3977 Verify206Response(headers, 40, 49); 3977 Verify206Response(headers, 40, 49);
3978 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 3978 EXPECT_EQ(2, cache.network_layer()->transaction_count());
3979 EXPECT_EQ(1, cache.disk_cache()->open_count()); 3979 EXPECT_EQ(1, cache.disk_cache()->open_count());
3980 EXPECT_EQ(2, cache.disk_cache()->create_count()); 3980 EXPECT_EQ(2, cache.disk_cache()->create_count());
3981 } 3981 }
3982 3982
3983 // Tests that restarting a partial request when the cached data cannot be 3983 // Tests that restarting a partial request when the cached data cannot be
3984 // revalidated logs an event. 3984 // revalidated logs an event.
3985 TEST(HttpCache, RangeGET_NoValidation_LogsRestart) { 3985 TEST(HttpCache, RangeGET_NoValidation_LogsRestart) {
3986 MockHttpCache cache; 3986 MockHttpCache cache(false);
3987 cache.FailConditionalizations(); 3987 cache.FailConditionalizations();
3988 3988
3989 // Write to the cache (40-49). 3989 // Write to the cache (40-49).
3990 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 3990 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
3991 transaction.response_headers = "Content-Length: 10\n" 3991 transaction.response_headers = "Content-Length: 10\n"
3992 "ETag: \"foo\"\n"; 3992 "ETag: \"foo\"\n";
3993 RunTransactionTest(cache.http_cache(), transaction); 3993 RunTransactionTest(cache.http_cache(), transaction);
3994 3994
3995 // Now verify that the cached data is not used. 3995 // Now verify that the cached data is not used.
3996 BoundTestNetLog log; 3996 BoundTestNetLog log;
3997 RunTransactionTestWithLog(cache.http_cache(), kRangeGET_TransactionOK, 3997 RunTransactionTestWithLog(cache.http_cache(), kRangeGET_TransactionOK,
3998 log.bound()); 3998 log.bound());
3999 3999
4000 EXPECT_TRUE(LogContainsEventType( 4000 EXPECT_TRUE(LogContainsEventType(
4001 log, NetLogEventType::HTTP_CACHE_RESTART_PARTIAL_REQUEST)); 4001 log, NetLogEventType::HTTP_CACHE_RESTART_PARTIAL_REQUEST));
4002 } 4002 }
4003 4003
4004 // Tests that a failure to conditionalize a regular request (no range) with a 4004 // Tests that a failure to conditionalize a regular request (no range) with a
4005 // sparse entry results in a full response. 4005 // sparse entry results in a full response.
4006 TEST(HttpCache, GET_NoConditionalization) { 4006 TEST(HttpCache, GET_NoConditionalization) {
4007 MockHttpCache cache; 4007 MockHttpCache cache(false);
4008 cache.FailConditionalizations(); 4008 cache.FailConditionalizations();
4009 std::string headers; 4009 std::string headers;
4010 4010
4011 // Write to the cache (40-49). 4011 // Write to the cache (40-49).
4012 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 4012 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4013 transaction.response_headers = "Content-Length: 10\n" 4013 transaction.response_headers = "Content-Length: 10\n"
4014 "ETag: \"foo\"\n"; 4014 "ETag: \"foo\"\n";
4015 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4015 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4016 4016
4017 Verify206Response(headers, 40, 49); 4017 Verify206Response(headers, 40, 49);
(...skipping 18 matching lines...) Expand all
4036 RunTransactionTest(cache.http_cache(), transaction); 4036 RunTransactionTest(cache.http_cache(), transaction);
4037 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 4037 EXPECT_EQ(3, cache.network_layer()->transaction_count());
4038 EXPECT_EQ(2, cache.disk_cache()->open_count()); 4038 EXPECT_EQ(2, cache.disk_cache()->open_count());
4039 EXPECT_EQ(2, cache.disk_cache()->create_count()); 4039 EXPECT_EQ(2, cache.disk_cache()->create_count());
4040 } 4040 }
4041 4041
4042 // Verifies that conditionalization failures when asking for a range that would 4042 // Verifies that conditionalization failures when asking for a range that would
4043 // require the cache to modify the range to ask, result in a network request 4043 // require the cache to modify the range to ask, result in a network request
4044 // that matches the user's one. 4044 // that matches the user's one.
4045 TEST(HttpCache, RangeGET_NoConditionalization2) { 4045 TEST(HttpCache, RangeGET_NoConditionalization2) {
4046 MockHttpCache cache; 4046 MockHttpCache cache(false);
4047 cache.FailConditionalizations(); 4047 cache.FailConditionalizations();
4048 std::string headers; 4048 std::string headers;
4049 4049
4050 // Write to the cache (40-49). 4050 // Write to the cache (40-49).
4051 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 4051 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4052 transaction.response_headers = "Content-Length: 10\n" 4052 transaction.response_headers = "Content-Length: 10\n"
4053 "ETag: \"foo\"\n"; 4053 "ETag: \"foo\"\n";
4054 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4054 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4055 4055
4056 Verify206Response(headers, 40, 49); 4056 Verify206Response(headers, 40, 49);
(...skipping 17 matching lines...) Expand all
4074 4074
4075 // The last response was saved. 4075 // The last response was saved.
4076 RunTransactionTest(cache.http_cache(), transaction); 4076 RunTransactionTest(cache.http_cache(), transaction);
4077 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 4077 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4078 EXPECT_EQ(2, cache.disk_cache()->open_count()); 4078 EXPECT_EQ(2, cache.disk_cache()->open_count());
4079 EXPECT_EQ(2, cache.disk_cache()->create_count()); 4079 EXPECT_EQ(2, cache.disk_cache()->create_count());
4080 } 4080 }
4081 4081
4082 // Tests that we cache partial responses that lack content-length. 4082 // Tests that we cache partial responses that lack content-length.
4083 TEST(HttpCache, RangeGET_NoContentLength) { 4083 TEST(HttpCache, RangeGET_NoContentLength) {
4084 MockHttpCache cache; 4084 MockHttpCache cache(false);
4085 std::string headers; 4085 std::string headers;
4086 4086
4087 // Attempt to write to the cache (40-49). 4087 // Attempt to write to the cache (40-49).
4088 MockTransaction transaction(kRangeGET_TransactionOK); 4088 MockTransaction transaction(kRangeGET_TransactionOK);
4089 AddMockTransaction(&transaction); 4089 AddMockTransaction(&transaction);
4090 transaction.response_headers = "ETag: \"foo\"\n" 4090 transaction.response_headers = "ETag: \"foo\"\n"
4091 "Accept-Ranges: bytes\n" 4091 "Accept-Ranges: bytes\n"
4092 "Content-Range: bytes 40-49/80\n"; 4092 "Content-Range: bytes 40-49/80\n";
4093 transaction.handler = NULL; 4093 transaction.handler = NULL;
4094 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4094 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
(...skipping 17 matching lines...) Expand all
4112 4112
4113 // Fails only on bots. crbug.com/533640 4113 // Fails only on bots. crbug.com/533640
4114 #if defined(OS_ANDROID) 4114 #if defined(OS_ANDROID)
4115 #define MAYBE_RangeGET_OK DISABLED_RangeGET_OK 4115 #define MAYBE_RangeGET_OK DISABLED_RangeGET_OK
4116 #else 4116 #else
4117 #define MAYBE_RangeGET_OK RangeGET_OK 4117 #define MAYBE_RangeGET_OK RangeGET_OK
4118 #endif 4118 #endif
4119 // Tests that we can cache range requests and fetch random blocks from the 4119 // Tests that we can cache range requests and fetch random blocks from the
4120 // cache and the network. 4120 // cache and the network.
4121 TEST(HttpCache, MAYBE_RangeGET_OK) { 4121 TEST(HttpCache, MAYBE_RangeGET_OK) {
4122 MockHttpCache cache; 4122 MockHttpCache cache(false);
4123 AddMockTransaction(&kRangeGET_TransactionOK); 4123 AddMockTransaction(&kRangeGET_TransactionOK);
4124 std::string headers; 4124 std::string headers;
4125 4125
4126 // Write to the cache (40-49). 4126 // Write to the cache (40-49).
4127 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, 4127 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
4128 &headers); 4128 &headers);
4129 4129
4130 Verify206Response(headers, 40, 49); 4130 Verify206Response(headers, 40, 49);
4131 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 4131 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4132 EXPECT_EQ(0, cache.disk_cache()->open_count()); 4132 EXPECT_EQ(0, cache.disk_cache()->open_count());
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
4178 4178
4179 // Fails only on bots. crbug.com/533640 4179 // Fails only on bots. crbug.com/533640
4180 #if defined(OS_ANDROID) 4180 #if defined(OS_ANDROID)
4181 #define MAYBE_RangeGET_SyncOK DISABLED_RangeGET_SyncOK 4181 #define MAYBE_RangeGET_SyncOK DISABLED_RangeGET_SyncOK
4182 #else 4182 #else
4183 #define MAYBE_RangeGET_SyncOK RangeGET_SyncOK 4183 #define MAYBE_RangeGET_SyncOK RangeGET_SyncOK
4184 #endif 4184 #endif
4185 // Tests that we can cache range requests and fetch random blocks from the 4185 // Tests that we can cache range requests and fetch random blocks from the
4186 // cache and the network, with synchronous responses. 4186 // cache and the network, with synchronous responses.
4187 TEST(HttpCache, MAYBE_RangeGET_SyncOK) { 4187 TEST(HttpCache, MAYBE_RangeGET_SyncOK) {
4188 MockHttpCache cache; 4188 MockHttpCache cache(false);
4189 4189
4190 MockTransaction transaction(kRangeGET_TransactionOK); 4190 MockTransaction transaction(kRangeGET_TransactionOK);
4191 transaction.test_mode = TEST_MODE_SYNC_ALL; 4191 transaction.test_mode = TEST_MODE_SYNC_ALL;
4192 AddMockTransaction(&transaction); 4192 AddMockTransaction(&transaction);
4193 4193
4194 // Write to the cache (40-49). 4194 // Write to the cache (40-49).
4195 std::string headers; 4195 std::string headers;
4196 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4196 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4197 4197
4198 Verify206Response(headers, 40, 49); 4198 Verify206Response(headers, 40, 49);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
4239 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4239 EXPECT_EQ(1, cache.disk_cache()->create_count());
4240 TestLoadTimingNetworkRequest(load_timing_info); 4240 TestLoadTimingNetworkRequest(load_timing_info);
4241 4241
4242 RemoveMockTransaction(&transaction); 4242 RemoveMockTransaction(&transaction);
4243 } 4243 }
4244 4244
4245 // Tests that if the previous transaction is cancelled while busy (doing sparse 4245 // Tests that if the previous transaction is cancelled while busy (doing sparse
4246 // IO), a new transaction (that reuses that same ActiveEntry) waits until the 4246 // IO), a new transaction (that reuses that same ActiveEntry) waits until the
4247 // entry is ready again. 4247 // entry is ready again.
4248 TEST(HttpCache, Sparse_WaitForEntry) { 4248 TEST(HttpCache, Sparse_WaitForEntry) {
4249 MockHttpCache cache; 4249 MockHttpCache cache(false);
4250 4250
4251 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 4251 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4252 4252
4253 // Create a sparse entry. 4253 // Create a sparse entry.
4254 RunTransactionTest(cache.http_cache(), transaction); 4254 RunTransactionTest(cache.http_cache(), transaction);
4255 4255
4256 // Simulate a previous transaction being cancelled. 4256 // Simulate a previous transaction being cancelled.
4257 disk_cache::Entry* entry; 4257 disk_cache::Entry* entry;
4258 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); 4258 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
4259 entry->CancelSparseIO(); 4259 entry->CancelSparseIO();
4260 4260
4261 // Test with a range request. 4261 // Test with a range request.
4262 RunTransactionTest(cache.http_cache(), transaction); 4262 RunTransactionTest(cache.http_cache(), transaction);
4263 4263
4264 // Now test with a regular request. 4264 // Now test with a regular request.
4265 entry->CancelSparseIO(); 4265 entry->CancelSparseIO();
4266 transaction.request_headers = EXTRA_HEADER; 4266 transaction.request_headers = EXTRA_HEADER;
4267 transaction.data = kFullRangeData; 4267 transaction.data = kFullRangeData;
4268 RunTransactionTest(cache.http_cache(), transaction); 4268 RunTransactionTest(cache.http_cache(), transaction);
4269 4269
4270 entry->Close(); 4270 entry->Close();
4271 } 4271 }
4272 4272
4273 // Tests that we don't revalidate an entry unless we are required to do so. 4273 // Tests that we don't revalidate an entry unless we are required to do so.
4274 TEST(HttpCache, RangeGET_Revalidate1) { 4274 TEST(HttpCache, RangeGET_Revalidate1) {
4275 MockHttpCache cache; 4275 MockHttpCache cache(false);
4276 std::string headers; 4276 std::string headers;
4277 4277
4278 // Write to the cache (40-49). 4278 // Write to the cache (40-49).
4279 MockTransaction transaction(kRangeGET_TransactionOK); 4279 MockTransaction transaction(kRangeGET_TransactionOK);
4280 transaction.response_headers = 4280 transaction.response_headers =
4281 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" 4281 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
4282 "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n" // Should never expire. 4282 "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n" // Should never expire.
4283 "ETag: \"foo\"\n" 4283 "ETag: \"foo\"\n"
4284 "Accept-Ranges: bytes\n" 4284 "Accept-Ranges: bytes\n"
4285 "Content-Length: 10\n"; 4285 "Content-Length: 10\n";
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
4320 } 4320 }
4321 4321
4322 // Fails only on bots. crbug.com/533640 4322 // Fails only on bots. crbug.com/533640
4323 #if defined(OS_ANDROID) 4323 #if defined(OS_ANDROID)
4324 #define MAYBE_RangeGET_Revalidate2 DISABLED_RangeGET_Revalidate2 4324 #define MAYBE_RangeGET_Revalidate2 DISABLED_RangeGET_Revalidate2
4325 #else 4325 #else
4326 #define MAYBE_RangeGET_Revalidate2 RangeGET_Revalidate2 4326 #define MAYBE_RangeGET_Revalidate2 RangeGET_Revalidate2
4327 #endif 4327 #endif
4328 // Checks that we revalidate an entry when the headers say so. 4328 // Checks that we revalidate an entry when the headers say so.
4329 TEST(HttpCache, MAYBE_RangeGET_Revalidate2) { 4329 TEST(HttpCache, MAYBE_RangeGET_Revalidate2) {
4330 MockHttpCache cache; 4330 MockHttpCache cache(false);
4331 std::string headers; 4331 std::string headers;
4332 4332
4333 // Write to the cache (40-49). 4333 // Write to the cache (40-49).
4334 MockTransaction transaction(kRangeGET_TransactionOK); 4334 MockTransaction transaction(kRangeGET_TransactionOK);
4335 transaction.response_headers = 4335 transaction.response_headers =
4336 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" 4336 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
4337 "Expires: Sat, 18 Apr 2009 01:10:43 GMT\n" // Expired. 4337 "Expires: Sat, 18 Apr 2009 01:10:43 GMT\n" // Expired.
4338 "ETag: \"foo\"\n" 4338 "ETag: \"foo\"\n"
4339 "Accept-Ranges: bytes\n" 4339 "Accept-Ranges: bytes\n"
4340 "Content-Length: 10\n"; 4340 "Content-Length: 10\n";
(...skipping 11 matching lines...) Expand all
4352 4352
4353 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 4353 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4354 EXPECT_EQ(1, cache.disk_cache()->open_count()); 4354 EXPECT_EQ(1, cache.disk_cache()->open_count());
4355 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4355 EXPECT_EQ(1, cache.disk_cache()->create_count());
4356 4356
4357 RemoveMockTransaction(&transaction); 4357 RemoveMockTransaction(&transaction);
4358 } 4358 }
4359 4359
4360 // Tests that we deal with 304s for range requests. 4360 // Tests that we deal with 304s for range requests.
4361 TEST(HttpCache, RangeGET_304) { 4361 TEST(HttpCache, RangeGET_304) {
4362 MockHttpCache cache; 4362 MockHttpCache cache(false);
4363 AddMockTransaction(&kRangeGET_TransactionOK); 4363 AddMockTransaction(&kRangeGET_TransactionOK);
4364 std::string headers; 4364 std::string headers;
4365 4365
4366 // Write to the cache (40-49). 4366 // Write to the cache (40-49).
4367 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, 4367 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
4368 &headers); 4368 &headers);
4369 4369
4370 Verify206Response(headers, 40, 49); 4370 Verify206Response(headers, 40, 49);
4371 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 4371 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4372 EXPECT_EQ(0, cache.disk_cache()->open_count()); 4372 EXPECT_EQ(0, cache.disk_cache()->open_count());
4373 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4373 EXPECT_EQ(1, cache.disk_cache()->create_count());
4374 4374
4375 // Read from the cache (40-49). 4375 // Read from the cache (40-49).
4376 RangeTransactionServer handler; 4376 RangeTransactionServer handler;
4377 handler.set_not_modified(true); 4377 handler.set_not_modified(true);
4378 MockTransaction transaction(kRangeGET_TransactionOK); 4378 MockTransaction transaction(kRangeGET_TransactionOK);
4379 transaction.load_flags |= LOAD_VALIDATE_CACHE; 4379 transaction.load_flags |= LOAD_VALIDATE_CACHE;
4380 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4380 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4381 4381
4382 Verify206Response(headers, 40, 49); 4382 Verify206Response(headers, 40, 49);
4383 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 4383 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4384 EXPECT_EQ(1, cache.disk_cache()->open_count()); 4384 EXPECT_EQ(1, cache.disk_cache()->open_count());
4385 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4385 EXPECT_EQ(1, cache.disk_cache()->create_count());
4386 4386
4387 RemoveMockTransaction(&kRangeGET_TransactionOK); 4387 RemoveMockTransaction(&kRangeGET_TransactionOK);
4388 } 4388 }
4389 4389
4390 // Tests that we deal with 206s when revalidating range requests. 4390 // Tests that we deal with 206s when revalidating range requests.
4391 TEST(HttpCache, RangeGET_ModifiedResult) { 4391 TEST(HttpCache, RangeGET_ModifiedResult) {
4392 MockHttpCache cache; 4392 MockHttpCache cache(false);
4393 AddMockTransaction(&kRangeGET_TransactionOK); 4393 AddMockTransaction(&kRangeGET_TransactionOK);
4394 std::string headers; 4394 std::string headers;
4395 4395
4396 // Write to the cache (40-49). 4396 // Write to the cache (40-49).
4397 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, 4397 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
4398 &headers); 4398 &headers);
4399 4399
4400 Verify206Response(headers, 40, 49); 4400 Verify206Response(headers, 40, 49);
4401 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 4401 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4402 EXPECT_EQ(0, cache.disk_cache()->open_count()); 4402 EXPECT_EQ(0, cache.disk_cache()->open_count());
(...skipping 19 matching lines...) Expand all
4422 4422
4423 RemoveMockTransaction(&kRangeGET_TransactionOK); 4423 RemoveMockTransaction(&kRangeGET_TransactionOK);
4424 } 4424 }
4425 4425
4426 // Tests that when a server returns 206 with a sub-range of the requested range, 4426 // Tests that when a server returns 206 with a sub-range of the requested range,
4427 // and there is nothing stored in the cache, the returned response is passed to 4427 // and there is nothing stored in the cache, the returned response is passed to
4428 // the caller as is. In this context, a subrange means a response that starts 4428 // the caller as is. In this context, a subrange means a response that starts
4429 // with the same byte that was requested, but that is not the whole range that 4429 // with the same byte that was requested, but that is not the whole range that
4430 // was requested. 4430 // was requested.
4431 TEST(HttpCache, RangeGET_206ReturnsSubrangeRange_NoCachedContent) { 4431 TEST(HttpCache, RangeGET_206ReturnsSubrangeRange_NoCachedContent) {
4432 MockHttpCache cache; 4432 MockHttpCache cache(false);
4433 std::string headers; 4433 std::string headers;
4434 4434
4435 // Request a large range (40-59). The server sends 40-49. 4435 // Request a large range (40-59). The server sends 40-49.
4436 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 4436 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4437 transaction.request_headers = "Range: bytes = 40-59\r\n" EXTRA_HEADER; 4437 transaction.request_headers = "Range: bytes = 40-59\r\n" EXTRA_HEADER;
4438 transaction.response_headers = 4438 transaction.response_headers =
4439 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 4439 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
4440 "ETag: \"foo\"\n" 4440 "ETag: \"foo\"\n"
4441 "Accept-Ranges: bytes\n" 4441 "Accept-Ranges: bytes\n"
4442 "Content-Length: 10\n" 4442 "Content-Length: 10\n"
4443 "Content-Range: bytes 40-49/80\n"; 4443 "Content-Range: bytes 40-49/80\n";
4444 transaction.handler = nullptr; 4444 transaction.handler = nullptr;
4445 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4445 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4446 4446
4447 Verify206Response(headers, 40, 49); 4447 Verify206Response(headers, 40, 49);
4448 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 4448 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4449 EXPECT_EQ(0, cache.disk_cache()->open_count()); 4449 EXPECT_EQ(0, cache.disk_cache()->open_count());
4450 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4450 EXPECT_EQ(1, cache.disk_cache()->create_count());
4451 } 4451 }
4452 4452
4453 // Tests that when a server returns 206 with a sub-range of the requested range, 4453 // Tests that when a server returns 206 with a sub-range of the requested range,
4454 // and there was an entry stored in the cache, the cache gets out of the way. 4454 // and there was an entry stored in the cache, the cache gets out of the way.
4455 TEST(HttpCache, RangeGET_206ReturnsSubrangeRange_CachedContent) { 4455 TEST(HttpCache, RangeGET_206ReturnsSubrangeRange_CachedContent) {
4456 MockHttpCache cache; 4456 MockHttpCache cache(false);
4457 std::string headers; 4457 std::string headers;
4458 4458
4459 // Write to the cache (70-79). 4459 // Write to the cache (70-79).
4460 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 4460 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4461 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER; 4461 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER;
4462 transaction.data = "rg: 70-79 "; 4462 transaction.data = "rg: 70-79 ";
4463 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4463 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4464 Verify206Response(headers, 70, 79); 4464 Verify206Response(headers, 70, 79);
4465 4465
4466 // Request a large range (40-79). The cache will ask the server for 40-59. 4466 // Request a large range (40-79). The cache will ask the server for 40-59.
(...skipping 20 matching lines...) Expand all
4487 RunTransactionTest(cache.http_cache(), transaction); 4487 RunTransactionTest(cache.http_cache(), transaction);
4488 EXPECT_EQ(4, cache.network_layer()->transaction_count()); 4488 EXPECT_EQ(4, cache.network_layer()->transaction_count());
4489 EXPECT_EQ(1, cache.disk_cache()->open_count()); 4489 EXPECT_EQ(1, cache.disk_cache()->open_count());
4490 EXPECT_EQ(2, cache.disk_cache()->create_count()); 4490 EXPECT_EQ(2, cache.disk_cache()->create_count());
4491 } 4491 }
4492 4492
4493 // Tests that when a server returns 206 with a sub-range of the requested range, 4493 // Tests that when a server returns 206 with a sub-range of the requested range,
4494 // and there was an entry stored in the cache, the cache gets out of the way, 4494 // and there was an entry stored in the cache, the cache gets out of the way,
4495 // when the caller is not using ranges. 4495 // when the caller is not using ranges.
4496 TEST(HttpCache, GET_206ReturnsSubrangeRange_CachedContent) { 4496 TEST(HttpCache, GET_206ReturnsSubrangeRange_CachedContent) {
4497 MockHttpCache cache; 4497 MockHttpCache cache(false);
4498 std::string headers; 4498 std::string headers;
4499 4499
4500 // Write to the cache (70-79). 4500 // Write to the cache (70-79).
4501 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 4501 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4502 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER; 4502 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER;
4503 transaction.data = "rg: 70-79 "; 4503 transaction.data = "rg: 70-79 ";
4504 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4504 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4505 Verify206Response(headers, 70, 79); 4505 Verify206Response(headers, 70, 79);
4506 4506
4507 // Don't ask for a range. The cache will ask the server for 0-69. 4507 // Don't ask for a range. The cache will ask the server for 0-69.
(...skipping 17 matching lines...) Expand all
4525 EXPECT_EQ(2, cache.disk_cache()->create_count()); 4525 EXPECT_EQ(2, cache.disk_cache()->create_count());
4526 } 4526 }
4527 4527
4528 // Tests that when a server returns 206 with a random range and there is 4528 // Tests that when a server returns 206 with a random range and there is
4529 // nothing stored in the cache, the returned response is passed to the caller 4529 // nothing stored in the cache, the returned response is passed to the caller
4530 // as is. In this context, a WrongRange means that the returned range may or may 4530 // as is. In this context, a WrongRange means that the returned range may or may
4531 // not have any relationship with the requested range (may or may not be 4531 // not have any relationship with the requested range (may or may not be
4532 // contained). The important part is that the first byte doesn't match the first 4532 // contained). The important part is that the first byte doesn't match the first
4533 // requested byte. 4533 // requested byte.
4534 TEST(HttpCache, RangeGET_206ReturnsWrongRange_NoCachedContent) { 4534 TEST(HttpCache, RangeGET_206ReturnsWrongRange_NoCachedContent) {
4535 MockHttpCache cache; 4535 MockHttpCache cache(false);
4536 std::string headers; 4536 std::string headers;
4537 4537
4538 // Request a large range (30-59). The server sends (40-49). 4538 // Request a large range (30-59). The server sends (40-49).
4539 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 4539 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4540 transaction.request_headers = "Range: bytes = 30-59\r\n" EXTRA_HEADER; 4540 transaction.request_headers = "Range: bytes = 30-59\r\n" EXTRA_HEADER;
4541 transaction.response_headers = 4541 transaction.response_headers =
4542 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 4542 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
4543 "ETag: \"foo\"\n" 4543 "ETag: \"foo\"\n"
4544 "Accept-Ranges: bytes\n" 4544 "Accept-Ranges: bytes\n"
4545 "Content-Length: 10\n" 4545 "Content-Length: 10\n"
4546 "Content-Range: bytes 40-49/80\n"; 4546 "Content-Range: bytes 40-49/80\n";
4547 transaction.handler = nullptr; 4547 transaction.handler = nullptr;
4548 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4548 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4549 4549
4550 Verify206Response(headers, 40, 49); 4550 Verify206Response(headers, 40, 49);
4551 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 4551 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4552 EXPECT_EQ(0, cache.disk_cache()->open_count()); 4552 EXPECT_EQ(0, cache.disk_cache()->open_count());
4553 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4553 EXPECT_EQ(1, cache.disk_cache()->create_count());
4554 4554
4555 // The entry was deleted. 4555 // The entry was deleted.
4556 RunTransactionTest(cache.http_cache(), transaction); 4556 RunTransactionTest(cache.http_cache(), transaction);
4557 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 4557 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4558 EXPECT_EQ(0, cache.disk_cache()->open_count()); 4558 EXPECT_EQ(0, cache.disk_cache()->open_count());
4559 EXPECT_EQ(2, cache.disk_cache()->create_count()); 4559 EXPECT_EQ(2, cache.disk_cache()->create_count());
4560 } 4560 }
4561 4561
4562 // Tests that when a server returns 206 with a random range and there is 4562 // Tests that when a server returns 206 with a random range and there is
4563 // an entry stored in the cache, the cache gets out of the way. 4563 // an entry stored in the cache, the cache gets out of the way.
4564 TEST(HttpCache, RangeGET_206ReturnsWrongRange_CachedContent) { 4564 TEST(HttpCache, RangeGET_206ReturnsWrongRange_CachedContent) {
4565 MockHttpCache cache; 4565 MockHttpCache cache(false);
4566 std::string headers; 4566 std::string headers;
4567 4567
4568 // Write to the cache (70-79). 4568 // Write to the cache (70-79).
4569 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 4569 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4570 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER; 4570 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER;
4571 transaction.data = "rg: 70-79 "; 4571 transaction.data = "rg: 70-79 ";
4572 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4572 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4573 Verify206Response(headers, 70, 79); 4573 Verify206Response(headers, 70, 79);
4574 4574
4575 // Request a large range (30-79). The cache will ask the server for 30-69. 4575 // Request a large range (30-79). The cache will ask the server for 30-69.
(...skipping 17 matching lines...) Expand all
4593 // The entry was deleted. 4593 // The entry was deleted.
4594 RunTransactionTest(cache.http_cache(), transaction); 4594 RunTransactionTest(cache.http_cache(), transaction);
4595 EXPECT_EQ(4, cache.network_layer()->transaction_count()); 4595 EXPECT_EQ(4, cache.network_layer()->transaction_count());
4596 EXPECT_EQ(1, cache.disk_cache()->open_count()); 4596 EXPECT_EQ(1, cache.disk_cache()->open_count());
4597 EXPECT_EQ(2, cache.disk_cache()->create_count()); 4597 EXPECT_EQ(2, cache.disk_cache()->create_count());
4598 } 4598 }
4599 4599
4600 // Tests that when a caller asks for a range beyond EOF, with an empty cache, 4600 // Tests that when a caller asks for a range beyond EOF, with an empty cache,
4601 // the response matches the one provided by the server. 4601 // the response matches the one provided by the server.
4602 TEST(HttpCache, RangeGET_206ReturnsSmallerFile_NoCachedContent) { 4602 TEST(HttpCache, RangeGET_206ReturnsSmallerFile_NoCachedContent) {
4603 MockHttpCache cache; 4603 MockHttpCache cache(false);
4604 std::string headers; 4604 std::string headers;
4605 4605
4606 // Request a large range (70-99). The server sends 70-79. 4606 // Request a large range (70-99). The server sends 70-79.
4607 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 4607 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4608 transaction.request_headers = "Range: bytes = 70-99\r\n" EXTRA_HEADER; 4608 transaction.request_headers = "Range: bytes = 70-99\r\n" EXTRA_HEADER;
4609 transaction.data = "rg: 70-79 "; 4609 transaction.data = "rg: 70-79 ";
4610 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4610 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4611 4611
4612 Verify206Response(headers, 70, 79); 4612 Verify206Response(headers, 70, 79);
4613 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 4613 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4614 EXPECT_EQ(0, cache.disk_cache()->open_count()); 4614 EXPECT_EQ(0, cache.disk_cache()->open_count());
4615 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4615 EXPECT_EQ(1, cache.disk_cache()->create_count());
4616 4616
4617 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); 4617 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
4618 EXPECT_EQ(1, cache.disk_cache()->open_count()); 4618 EXPECT_EQ(1, cache.disk_cache()->open_count());
4619 } 4619 }
4620 4620
4621 // Tests that when a caller asks for a range beyond EOF, with a cached entry, 4621 // Tests that when a caller asks for a range beyond EOF, with a cached entry,
4622 // the cache automatically fixes the request. 4622 // the cache automatically fixes the request.
4623 TEST(HttpCache, RangeGET_206ReturnsSmallerFile_CachedContent) { 4623 TEST(HttpCache, RangeGET_206ReturnsSmallerFile_CachedContent) {
4624 MockHttpCache cache; 4624 MockHttpCache cache(false);
4625 std::string headers; 4625 std::string headers;
4626 4626
4627 // Write to the cache (40-49). 4627 // Write to the cache (40-49).
4628 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 4628 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4629 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4629 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4630 4630
4631 // Request a large range (70-99). The server sends 70-79. 4631 // Request a large range (70-99). The server sends 70-79.
4632 transaction.request_headers = "Range: bytes = 70-99\r\n" EXTRA_HEADER; 4632 transaction.request_headers = "Range: bytes = 70-99\r\n" EXTRA_HEADER;
4633 transaction.data = "rg: 70-79 "; 4633 transaction.data = "rg: 70-79 ";
4634 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4634 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4635 4635
4636 Verify206Response(headers, 70, 79); 4636 Verify206Response(headers, 70, 79);
4637 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 4637 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4638 EXPECT_EQ(1, cache.disk_cache()->open_count()); 4638 EXPECT_EQ(1, cache.disk_cache()->open_count());
4639 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4639 EXPECT_EQ(1, cache.disk_cache()->create_count());
4640 4640
4641 // The entry was not deleted (the range was automatically fixed). 4641 // The entry was not deleted (the range was automatically fixed).
4642 RunTransactionTest(cache.http_cache(), transaction); 4642 RunTransactionTest(cache.http_cache(), transaction);
4643 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 4643 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4644 EXPECT_EQ(2, cache.disk_cache()->open_count()); 4644 EXPECT_EQ(2, cache.disk_cache()->open_count());
4645 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4645 EXPECT_EQ(1, cache.disk_cache()->create_count());
4646 } 4646 }
4647 4647
4648 // Tests that when a caller asks for a not-satisfiable range, the server's 4648 // Tests that when a caller asks for a not-satisfiable range, the server's
4649 // response is forwarded to the caller. 4649 // response is forwarded to the caller.
4650 TEST(HttpCache, RangeGET_416_NoCachedContent) { 4650 TEST(HttpCache, RangeGET_416_NoCachedContent) {
4651 MockHttpCache cache; 4651 MockHttpCache cache(false);
4652 std::string headers; 4652 std::string headers;
4653 4653
4654 // Request a range beyond EOF (80-99). 4654 // Request a range beyond EOF (80-99).
4655 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 4655 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4656 transaction.request_headers = "Range: bytes = 80-99\r\n" EXTRA_HEADER; 4656 transaction.request_headers = "Range: bytes = 80-99\r\n" EXTRA_HEADER;
4657 transaction.data = ""; 4657 transaction.data = "";
4658 transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable"; 4658 transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable";
4659 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4659 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4660 4660
4661 EXPECT_EQ(0U, headers.find(transaction.status)); 4661 EXPECT_EQ(0U, headers.find(transaction.status));
4662 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 4662 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4663 EXPECT_EQ(0, cache.disk_cache()->open_count()); 4663 EXPECT_EQ(0, cache.disk_cache()->open_count());
4664 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4664 EXPECT_EQ(1, cache.disk_cache()->create_count());
4665 4665
4666 // The entry was deleted. 4666 // The entry was deleted.
4667 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); 4667 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
4668 EXPECT_EQ(2, cache.disk_cache()->create_count()); 4668 EXPECT_EQ(2, cache.disk_cache()->create_count());
4669 } 4669 }
4670 4670
4671 // Tests that we cache 301s for range requests. 4671 // Tests that we cache 301s for range requests.
4672 TEST(HttpCache, RangeGET_301) { 4672 TEST(HttpCache, RangeGET_301) {
4673 MockHttpCache cache; 4673 MockHttpCache cache(false);
4674 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 4674 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
4675 transaction.status = "HTTP/1.1 301 Moved Permanently"; 4675 transaction.status = "HTTP/1.1 301 Moved Permanently";
4676 transaction.response_headers = "Location: http://www.bar.com/\n"; 4676 transaction.response_headers = "Location: http://www.bar.com/\n";
4677 transaction.data = ""; 4677 transaction.data = "";
4678 transaction.handler = NULL; 4678 transaction.handler = NULL;
4679 4679
4680 // Write to the cache. 4680 // Write to the cache.
4681 RunTransactionTest(cache.http_cache(), transaction); 4681 RunTransactionTest(cache.http_cache(), transaction);
4682 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 4682 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4683 EXPECT_EQ(0, cache.disk_cache()->open_count()); 4683 EXPECT_EQ(0, cache.disk_cache()->open_count());
4684 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4684 EXPECT_EQ(1, cache.disk_cache()->create_count());
4685 4685
4686 // Read from the cache. 4686 // Read from the cache.
4687 RunTransactionTest(cache.http_cache(), transaction); 4687 RunTransactionTest(cache.http_cache(), transaction);
4688 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 4688 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4689 EXPECT_EQ(1, cache.disk_cache()->open_count()); 4689 EXPECT_EQ(1, cache.disk_cache()->open_count());
4690 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4690 EXPECT_EQ(1, cache.disk_cache()->create_count());
4691 } 4691 }
4692 4692
4693 // Tests that we can cache range requests when the start or end is unknown. 4693 // Tests that we can cache range requests when the start or end is unknown.
4694 // We start with one suffix request, followed by a request from a given point. 4694 // We start with one suffix request, followed by a request from a given point.
4695 TEST(HttpCache, UnknownRangeGET_1) { 4695 TEST(HttpCache, UnknownRangeGET_1) {
4696 MockHttpCache cache; 4696 MockHttpCache cache(false);
4697 AddMockTransaction(&kRangeGET_TransactionOK); 4697 AddMockTransaction(&kRangeGET_TransactionOK);
4698 std::string headers; 4698 std::string headers;
4699 4699
4700 // Write to the cache (70-79). 4700 // Write to the cache (70-79).
4701 MockTransaction transaction(kRangeGET_TransactionOK); 4701 MockTransaction transaction(kRangeGET_TransactionOK);
4702 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; 4702 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
4703 transaction.data = "rg: 70-79 "; 4703 transaction.data = "rg: 70-79 ";
4704 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4704 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4705 4705
4706 Verify206Response(headers, 70, 79); 4706 Verify206Response(headers, 70, 79);
(...skipping 14 matching lines...) Expand all
4721 EXPECT_EQ(1, cache.disk_cache()->open_count()); 4721 EXPECT_EQ(1, cache.disk_cache()->open_count());
4722 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4722 EXPECT_EQ(1, cache.disk_cache()->create_count());
4723 4723
4724 RemoveMockTransaction(&kRangeGET_TransactionOK); 4724 RemoveMockTransaction(&kRangeGET_TransactionOK);
4725 } 4725 }
4726 4726
4727 // Tests that we can cache range requests when the start or end is unknown. 4727 // Tests that we can cache range requests when the start or end is unknown.
4728 // We start with one request from a given point, followed by a suffix request. 4728 // We start with one request from a given point, followed by a suffix request.
4729 // We'll also verify that synchronous cache responses work as intended. 4729 // We'll also verify that synchronous cache responses work as intended.
4730 TEST(HttpCache, UnknownRangeGET_2) { 4730 TEST(HttpCache, UnknownRangeGET_2) {
4731 MockHttpCache cache; 4731 MockHttpCache cache(false);
4732 std::string headers; 4732 std::string headers;
4733 4733
4734 MockTransaction transaction(kRangeGET_TransactionOK); 4734 MockTransaction transaction(kRangeGET_TransactionOK);
4735 transaction.test_mode = TEST_MODE_SYNC_CACHE_START | 4735 transaction.test_mode = TEST_MODE_SYNC_CACHE_START |
4736 TEST_MODE_SYNC_CACHE_READ | 4736 TEST_MODE_SYNC_CACHE_READ |
4737 TEST_MODE_SYNC_CACHE_WRITE; 4737 TEST_MODE_SYNC_CACHE_WRITE;
4738 AddMockTransaction(&transaction); 4738 AddMockTransaction(&transaction);
4739 4739
4740 // Write to the cache (70-79). 4740 // Write to the cache (70-79).
4741 transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER; 4741 transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER;
(...skipping 17 matching lines...) Expand all
4759 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 4759 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4760 EXPECT_EQ(1, cache.disk_cache()->open_count()); 4760 EXPECT_EQ(1, cache.disk_cache()->open_count());
4761 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4761 EXPECT_EQ(1, cache.disk_cache()->create_count());
4762 4762
4763 RemoveMockTransaction(&transaction); 4763 RemoveMockTransaction(&transaction);
4764 } 4764 }
4765 4765
4766 // Tests that receiving Not Modified when asking for an open range doesn't mess 4766 // Tests that receiving Not Modified when asking for an open range doesn't mess
4767 // up things. 4767 // up things.
4768 TEST(HttpCache, UnknownRangeGET_304) { 4768 TEST(HttpCache, UnknownRangeGET_304) {
4769 MockHttpCache cache; 4769 MockHttpCache cache(false);
4770 std::string headers; 4770 std::string headers;
4771 4771
4772 MockTransaction transaction(kRangeGET_TransactionOK); 4772 MockTransaction transaction(kRangeGET_TransactionOK);
4773 AddMockTransaction(&transaction); 4773 AddMockTransaction(&transaction);
4774 4774
4775 RangeTransactionServer handler; 4775 RangeTransactionServer handler;
4776 handler.set_not_modified(true); 4776 handler.set_not_modified(true);
4777 4777
4778 // Ask for the end of the file, without knowing the length. 4778 // Ask for the end of the file, without knowing the length.
4779 transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER; 4779 transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER;
4780 transaction.data = ""; 4780 transaction.data = "";
4781 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4781 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4782 4782
4783 // We just bypass the cache. 4783 // We just bypass the cache.
4784 EXPECT_EQ(0U, headers.find("HTTP/1.1 304 Not Modified\n")); 4784 EXPECT_EQ(0U, headers.find("HTTP/1.1 304 Not Modified\n"));
4785 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 4785 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4786 EXPECT_EQ(0, cache.disk_cache()->open_count()); 4786 EXPECT_EQ(0, cache.disk_cache()->open_count());
4787 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4787 EXPECT_EQ(1, cache.disk_cache()->create_count());
4788 4788
4789 RunTransactionTest(cache.http_cache(), transaction); 4789 RunTransactionTest(cache.http_cache(), transaction);
4790 EXPECT_EQ(2, cache.disk_cache()->create_count()); 4790 EXPECT_EQ(2, cache.disk_cache()->create_count());
4791 4791
4792 RemoveMockTransaction(&transaction); 4792 RemoveMockTransaction(&transaction);
4793 } 4793 }
4794 4794
4795 // Tests that we can handle non-range requests when we have cached a range. 4795 // Tests that we can handle non-range requests when we have cached a range.
4796 TEST(HttpCache, GET_Previous206) { 4796 TEST(HttpCache, GET_Previous206) {
4797 MockHttpCache cache; 4797 MockHttpCache cache(false);
4798 AddMockTransaction(&kRangeGET_TransactionOK); 4798 AddMockTransaction(&kRangeGET_TransactionOK);
4799 std::string headers; 4799 std::string headers;
4800 BoundTestNetLog log; 4800 BoundTestNetLog log;
4801 LoadTimingInfo load_timing_info; 4801 LoadTimingInfo load_timing_info;
4802 4802
4803 // Write to the cache (40-49). 4803 // Write to the cache (40-49).
4804 RunTransactionTestWithResponseAndGetTiming( 4804 RunTransactionTestWithResponseAndGetTiming(
4805 cache.http_cache(), kRangeGET_TransactionOK, &headers, log.bound(), 4805 cache.http_cache(), kRangeGET_TransactionOK, &headers, log.bound(),
4806 &load_timing_info); 4806 &load_timing_info);
4807 4807
(...skipping 16 matching lines...) Expand all
4824 EXPECT_EQ(1, cache.disk_cache()->open_count()); 4824 EXPECT_EQ(1, cache.disk_cache()->open_count());
4825 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4825 EXPECT_EQ(1, cache.disk_cache()->create_count());
4826 TestLoadTimingNetworkRequest(load_timing_info); 4826 TestLoadTimingNetworkRequest(load_timing_info);
4827 4827
4828 RemoveMockTransaction(&kRangeGET_TransactionOK); 4828 RemoveMockTransaction(&kRangeGET_TransactionOK);
4829 } 4829 }
4830 4830
4831 // Tests that we can handle non-range requests when we have cached the first 4831 // Tests that we can handle non-range requests when we have cached the first
4832 // part of the object and the server replies with 304 (Not Modified). 4832 // part of the object and the server replies with 304 (Not Modified).
4833 TEST(HttpCache, GET_Previous206_NotModified) { 4833 TEST(HttpCache, GET_Previous206_NotModified) {
4834 MockHttpCache cache; 4834 MockHttpCache cache(false);
4835 4835
4836 MockTransaction transaction(kRangeGET_TransactionOK); 4836 MockTransaction transaction(kRangeGET_TransactionOK);
4837 AddMockTransaction(&transaction); 4837 AddMockTransaction(&transaction);
4838 std::string headers; 4838 std::string headers;
4839 BoundTestNetLog log; 4839 BoundTestNetLog log;
4840 LoadTimingInfo load_timing_info; 4840 LoadTimingInfo load_timing_info;
4841 4841
4842 // Write to the cache (0-9). 4842 // Write to the cache (0-9).
4843 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; 4843 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
4844 transaction.data = "rg: 00-09 "; 4844 transaction.data = "rg: 00-09 ";
(...skipping 29 matching lines...) Expand all
4874 EXPECT_EQ(2, cache.disk_cache()->open_count()); 4874 EXPECT_EQ(2, cache.disk_cache()->open_count());
4875 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4875 EXPECT_EQ(1, cache.disk_cache()->create_count());
4876 TestLoadTimingNetworkRequest(load_timing_info); 4876 TestLoadTimingNetworkRequest(load_timing_info);
4877 4877
4878 RemoveMockTransaction(&transaction); 4878 RemoveMockTransaction(&transaction);
4879 } 4879 }
4880 4880
4881 // Tests that we can handle a regular request to a sparse entry, that results in 4881 // Tests that we can handle a regular request to a sparse entry, that results in
4882 // new content provided by the server (206). 4882 // new content provided by the server (206).
4883 TEST(HttpCache, GET_Previous206_NewContent) { 4883 TEST(HttpCache, GET_Previous206_NewContent) {
4884 MockHttpCache cache; 4884 MockHttpCache cache(false);
4885 AddMockTransaction(&kRangeGET_TransactionOK); 4885 AddMockTransaction(&kRangeGET_TransactionOK);
4886 std::string headers; 4886 std::string headers;
4887 4887
4888 // Write to the cache (0-9). 4888 // Write to the cache (0-9).
4889 MockTransaction transaction(kRangeGET_TransactionOK); 4889 MockTransaction transaction(kRangeGET_TransactionOK);
4890 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; 4890 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
4891 transaction.data = "rg: 00-09 "; 4891 transaction.data = "rg: 00-09 ";
4892 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 4892 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
4893 4893
4894 Verify206Response(headers, 0, 9); 4894 Verify206Response(headers, 0, 9);
(...skipping 25 matching lines...) Expand all
4920 4920
4921 // Verify that the previous request deleted the entry. 4921 // Verify that the previous request deleted the entry.
4922 RunTransactionTest(cache.http_cache(), transaction); 4922 RunTransactionTest(cache.http_cache(), transaction);
4923 EXPECT_EQ(2, cache.disk_cache()->create_count()); 4923 EXPECT_EQ(2, cache.disk_cache()->create_count());
4924 4924
4925 RemoveMockTransaction(&transaction); 4925 RemoveMockTransaction(&transaction);
4926 } 4926 }
4927 4927
4928 // Tests that we can handle cached 206 responses that are not sparse. 4928 // Tests that we can handle cached 206 responses that are not sparse.
4929 TEST(HttpCache, GET_Previous206_NotSparse) { 4929 TEST(HttpCache, GET_Previous206_NotSparse) {
4930 MockHttpCache cache; 4930 MockHttpCache cache(false);
4931 4931
4932 // Create a disk cache entry that stores 206 headers while not being sparse. 4932 // Create a disk cache entry that stores 206 headers while not being sparse.
4933 disk_cache::Entry* entry; 4933 disk_cache::Entry* entry;
4934 ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry, 4934 ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry,
4935 NULL)); 4935 NULL));
4936 4936
4937 std::string raw_headers(kRangeGET_TransactionOK.status); 4937 std::string raw_headers(kRangeGET_TransactionOK.status);
4938 raw_headers.append("\n"); 4938 raw_headers.append("\n");
4939 raw_headers.append(kRangeGET_TransactionOK.response_headers); 4939 raw_headers.append(kRangeGET_TransactionOK.response_headers);
4940 raw_headers = 4940 raw_headers =
(...skipping 26 matching lines...) Expand all
4967 EXPECT_EQ(expected_headers, headers); 4967 EXPECT_EQ(expected_headers, headers);
4968 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 4968 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4969 EXPECT_EQ(1, cache.disk_cache()->open_count()); 4969 EXPECT_EQ(1, cache.disk_cache()->open_count());
4970 EXPECT_EQ(2, cache.disk_cache()->create_count()); 4970 EXPECT_EQ(2, cache.disk_cache()->create_count());
4971 TestLoadTimingNetworkRequest(load_timing_info); 4971 TestLoadTimingNetworkRequest(load_timing_info);
4972 } 4972 }
4973 4973
4974 // Tests that we can handle cached 206 responses that are not sparse. This time 4974 // Tests that we can handle cached 206 responses that are not sparse. This time
4975 // we issue a range request and expect to receive a range. 4975 // we issue a range request and expect to receive a range.
4976 TEST(HttpCache, RangeGET_Previous206_NotSparse_2) { 4976 TEST(HttpCache, RangeGET_Previous206_NotSparse_2) {
4977 MockHttpCache cache; 4977 MockHttpCache cache(false);
4978 AddMockTransaction(&kRangeGET_TransactionOK); 4978 AddMockTransaction(&kRangeGET_TransactionOK);
4979 4979
4980 // Create a disk cache entry that stores 206 headers while not being sparse. 4980 // Create a disk cache entry that stores 206 headers while not being sparse.
4981 disk_cache::Entry* entry; 4981 disk_cache::Entry* entry;
4982 ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry, 4982 ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry,
4983 NULL)); 4983 NULL));
4984 4984
4985 std::string raw_headers(kRangeGET_TransactionOK.status); 4985 std::string raw_headers(kRangeGET_TransactionOK.status);
4986 raw_headers.append("\n"); 4986 raw_headers.append("\n");
4987 raw_headers.append(kRangeGET_TransactionOK.response_headers); 4987 raw_headers.append(kRangeGET_TransactionOK.response_headers);
(...skipping 21 matching lines...) Expand all
5009 Verify206Response(headers, 40, 49); 5009 Verify206Response(headers, 40, 49);
5010 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5010 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5011 EXPECT_EQ(1, cache.disk_cache()->open_count()); 5011 EXPECT_EQ(1, cache.disk_cache()->open_count());
5012 EXPECT_EQ(2, cache.disk_cache()->create_count()); 5012 EXPECT_EQ(2, cache.disk_cache()->create_count());
5013 5013
5014 RemoveMockTransaction(&kRangeGET_TransactionOK); 5014 RemoveMockTransaction(&kRangeGET_TransactionOK);
5015 } 5015 }
5016 5016
5017 // Tests that we can handle cached 206 responses that can't be validated. 5017 // Tests that we can handle cached 206 responses that can't be validated.
5018 TEST(HttpCache, GET_Previous206_NotValidation) { 5018 TEST(HttpCache, GET_Previous206_NotValidation) {
5019 MockHttpCache cache; 5019 MockHttpCache cache(false);
5020 5020
5021 // Create a disk cache entry that stores 206 headers. 5021 // Create a disk cache entry that stores 206 headers.
5022 disk_cache::Entry* entry; 5022 disk_cache::Entry* entry;
5023 ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry, 5023 ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry,
5024 NULL)); 5024 NULL));
5025 5025
5026 // Make sure that the headers cannot be validated with the server. 5026 // Make sure that the headers cannot be validated with the server.
5027 std::string raw_headers(kRangeGET_TransactionOK.status); 5027 std::string raw_headers(kRangeGET_TransactionOK.status);
5028 raw_headers.append("\n"); 5028 raw_headers.append("\n");
5029 raw_headers.append("Content-Length: 80\n"); 5029 raw_headers.append("Content-Length: 80\n");
(...skipping 28 matching lines...) Expand all
5058 } 5058 }
5059 5059
5060 // Fails only on bots. crbug.com/533640 5060 // Fails only on bots. crbug.com/533640
5061 #if defined(OS_ANDROID) 5061 #if defined(OS_ANDROID)
5062 #define MAYBE_RangeGET_Previous200 DISABLED_RangeGET_Previous200 5062 #define MAYBE_RangeGET_Previous200 DISABLED_RangeGET_Previous200
5063 #else 5063 #else
5064 #define MAYBE_RangeGET_Previous200 RangeGET_Previous200 5064 #define MAYBE_RangeGET_Previous200 RangeGET_Previous200
5065 #endif 5065 #endif
5066 // Tests that we can handle range requests with cached 200 responses. 5066 // Tests that we can handle range requests with cached 200 responses.
5067 TEST(HttpCache, MAYBE_RangeGET_Previous200) { 5067 TEST(HttpCache, MAYBE_RangeGET_Previous200) {
5068 MockHttpCache cache; 5068 MockHttpCache cache(false);
5069 5069
5070 // Store the whole thing with status 200. 5070 // Store the whole thing with status 200.
5071 MockTransaction transaction(kTypicalGET_Transaction); 5071 MockTransaction transaction(kTypicalGET_Transaction);
5072 transaction.url = kRangeGET_TransactionOK.url; 5072 transaction.url = kRangeGET_TransactionOK.url;
5073 transaction.data = kFullRangeData; 5073 transaction.data = kFullRangeData;
5074 AddMockTransaction(&transaction); 5074 AddMockTransaction(&transaction);
5075 RunTransactionTest(cache.http_cache(), transaction); 5075 RunTransactionTest(cache.http_cache(), transaction);
5076 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5076 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5077 EXPECT_EQ(0, cache.disk_cache()->open_count()); 5077 EXPECT_EQ(0, cache.disk_cache()->open_count());
5078 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5078 EXPECT_EQ(1, cache.disk_cache()->create_count());
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
5127 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5127 EXPECT_EQ(1, cache.disk_cache()->create_count());
5128 5128
5129 RunTransactionTest(cache.http_cache(), transaction2); 5129 RunTransactionTest(cache.http_cache(), transaction2);
5130 EXPECT_EQ(2, cache.disk_cache()->create_count()); 5130 EXPECT_EQ(2, cache.disk_cache()->create_count());
5131 5131
5132 RemoveMockTransaction(&kRangeGET_TransactionOK); 5132 RemoveMockTransaction(&kRangeGET_TransactionOK);
5133 } 5133 }
5134 5134
5135 // Tests that we can handle a 200 response when dealing with sparse entries. 5135 // Tests that we can handle a 200 response when dealing with sparse entries.
5136 TEST(HttpCache, RangeRequestResultsIn200) { 5136 TEST(HttpCache, RangeRequestResultsIn200) {
5137 MockHttpCache cache; 5137 MockHttpCache cache(false);
5138 AddMockTransaction(&kRangeGET_TransactionOK); 5138 AddMockTransaction(&kRangeGET_TransactionOK);
5139 std::string headers; 5139 std::string headers;
5140 5140
5141 // Write to the cache (70-79). 5141 // Write to the cache (70-79).
5142 MockTransaction transaction(kRangeGET_TransactionOK); 5142 MockTransaction transaction(kRangeGET_TransactionOK);
5143 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; 5143 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
5144 transaction.data = "rg: 70-79 "; 5144 transaction.data = "rg: 70-79 ";
5145 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 5145 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5146 5146
5147 Verify206Response(headers, 70, 79); 5147 Verify206Response(headers, 70, 79);
(...skipping 19 matching lines...) Expand all
5167 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 5167 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5168 EXPECT_EQ(1, cache.disk_cache()->open_count()); 5168 EXPECT_EQ(1, cache.disk_cache()->open_count());
5169 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5169 EXPECT_EQ(1, cache.disk_cache()->create_count());
5170 5170
5171 RemoveMockTransaction(&transaction2); 5171 RemoveMockTransaction(&transaction2);
5172 } 5172 }
5173 5173
5174 // Tests that a range request that falls outside of the size that we know about 5174 // Tests that a range request that falls outside of the size that we know about
5175 // only deletes the entry if the resource has indeed changed. 5175 // only deletes the entry if the resource has indeed changed.
5176 TEST(HttpCache, RangeGET_MoreThanCurrentSize) { 5176 TEST(HttpCache, RangeGET_MoreThanCurrentSize) {
5177 MockHttpCache cache; 5177 MockHttpCache cache(false);
5178 AddMockTransaction(&kRangeGET_TransactionOK); 5178 AddMockTransaction(&kRangeGET_TransactionOK);
5179 std::string headers; 5179 std::string headers;
5180 5180
5181 // Write to the cache (40-49). 5181 // Write to the cache (40-49).
5182 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, 5182 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
5183 &headers); 5183 &headers);
5184 5184
5185 Verify206Response(headers, 40, 49); 5185 Verify206Response(headers, 40, 49);
5186 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5186 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5187 EXPECT_EQ(0, cache.disk_cache()->open_count()); 5187 EXPECT_EQ(0, cache.disk_cache()->open_count());
(...skipping 18 matching lines...) Expand all
5206 } 5206 }
5207 5207
5208 // Fails only on bots. crbug.com/533640 5208 // Fails only on bots. crbug.com/533640
5209 #if defined(OS_ANDROID) 5209 #if defined(OS_ANDROID)
5210 #define MAYBE_RangeGET_Cancel DISABLED_RangeGET_Cancel 5210 #define MAYBE_RangeGET_Cancel DISABLED_RangeGET_Cancel
5211 #else 5211 #else
5212 #define MAYBE_RangeGET_Cancel RangeGET_Cancel 5212 #define MAYBE_RangeGET_Cancel RangeGET_Cancel
5213 #endif 5213 #endif
5214 // Tests that we don't delete a sparse entry when we cancel a request. 5214 // Tests that we don't delete a sparse entry when we cancel a request.
5215 TEST(HttpCache, MAYBE_RangeGET_Cancel) { 5215 TEST(HttpCache, MAYBE_RangeGET_Cancel) {
5216 MockHttpCache cache; 5216 MockHttpCache cache(false);
5217 AddMockTransaction(&kRangeGET_TransactionOK); 5217 AddMockTransaction(&kRangeGET_TransactionOK);
5218 5218
5219 MockHttpRequest request(kRangeGET_TransactionOK); 5219 MockHttpRequest request(kRangeGET_TransactionOK);
5220 5220
5221 Context* c = new Context(); 5221 Context* c = new Context();
5222 int rv = cache.CreateTransaction(&c->trans); 5222 int rv = cache.CreateTransaction(&c->trans);
5223 ASSERT_THAT(rv, IsOk()); 5223 ASSERT_THAT(rv, IsOk());
5224 5224
5225 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); 5225 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
5226 if (rv == ERR_IO_PENDING) 5226 if (rv == ERR_IO_PENDING)
(...skipping 22 matching lines...) Expand all
5249 5249
5250 // Fails only on bots. crbug.com/533640 5250 // Fails only on bots. crbug.com/533640
5251 #if defined(OS_ANDROID) 5251 #if defined(OS_ANDROID)
5252 #define MAYBE_RangeGET_Cancel2 DISABLED_RangeGET_Cancel2 5252 #define MAYBE_RangeGET_Cancel2 DISABLED_RangeGET_Cancel2
5253 #else 5253 #else
5254 #define MAYBE_RangeGET_Cancel2 RangeGET_Cancel2 5254 #define MAYBE_RangeGET_Cancel2 RangeGET_Cancel2
5255 #endif 5255 #endif
5256 // Tests that we don't delete a sparse entry when we start a new request after 5256 // Tests that we don't delete a sparse entry when we start a new request after
5257 // cancelling the previous one. 5257 // cancelling the previous one.
5258 TEST(HttpCache, MAYBE_RangeGET_Cancel2) { 5258 TEST(HttpCache, MAYBE_RangeGET_Cancel2) {
5259 MockHttpCache cache; 5259 MockHttpCache cache(false);
5260 AddMockTransaction(&kRangeGET_TransactionOK); 5260 AddMockTransaction(&kRangeGET_TransactionOK);
5261 5261
5262 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); 5262 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5263 MockHttpRequest request(kRangeGET_TransactionOK); 5263 MockHttpRequest request(kRangeGET_TransactionOK);
5264 request.load_flags |= LOAD_VALIDATE_CACHE; 5264 request.load_flags |= LOAD_VALIDATE_CACHE;
5265 5265
5266 Context* c = new Context(); 5266 Context* c = new Context();
5267 int rv = cache.CreateTransaction(&c->trans); 5267 int rv = cache.CreateTransaction(&c->trans);
5268 ASSERT_THAT(rv, IsOk()); 5268 ASSERT_THAT(rv, IsOk());
5269 5269
(...skipping 24 matching lines...) Expand all
5294 5294
5295 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 5295 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5296 EXPECT_EQ(1, cache.disk_cache()->open_count()); 5296 EXPECT_EQ(1, cache.disk_cache()->open_count());
5297 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5297 EXPECT_EQ(1, cache.disk_cache()->create_count());
5298 RemoveMockTransaction(&kRangeGET_TransactionOK); 5298 RemoveMockTransaction(&kRangeGET_TransactionOK);
5299 } 5299 }
5300 5300
5301 // A slight variation of the previous test, this time we cancel two requests in 5301 // A slight variation of the previous test, this time we cancel two requests in
5302 // a row, making sure that the second is waiting for the entry to be ready. 5302 // a row, making sure that the second is waiting for the entry to be ready.
5303 TEST(HttpCache, RangeGET_Cancel3) { 5303 TEST(HttpCache, RangeGET_Cancel3) {
5304 MockHttpCache cache; 5304 MockHttpCache cache(false);
5305 AddMockTransaction(&kRangeGET_TransactionOK); 5305 AddMockTransaction(&kRangeGET_TransactionOK);
5306 5306
5307 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); 5307 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5308 MockHttpRequest request(kRangeGET_TransactionOK); 5308 MockHttpRequest request(kRangeGET_TransactionOK);
5309 request.load_flags |= LOAD_VALIDATE_CACHE; 5309 request.load_flags |= LOAD_VALIDATE_CACHE;
5310 5310
5311 Context* c = new Context(); 5311 Context* c = new Context();
5312 int rv = cache.CreateTransaction(&c->trans); 5312 int rv = cache.CreateTransaction(&c->trans);
5313 ASSERT_THAT(rv, IsOk()); 5313 ASSERT_THAT(rv, IsOk());
5314 5314
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
5353 base::RunLoop().RunUntilIdle(); 5353 base::RunLoop().RunUntilIdle();
5354 5354
5355 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 5355 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5356 EXPECT_EQ(1, cache.disk_cache()->open_count()); 5356 EXPECT_EQ(1, cache.disk_cache()->open_count());
5357 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5357 EXPECT_EQ(1, cache.disk_cache()->create_count());
5358 RemoveMockTransaction(&kRangeGET_TransactionOK); 5358 RemoveMockTransaction(&kRangeGET_TransactionOK);
5359 } 5359 }
5360 5360
5361 // Tests that an invalid range response results in no cached entry. 5361 // Tests that an invalid range response results in no cached entry.
5362 TEST(HttpCache, RangeGET_InvalidResponse1) { 5362 TEST(HttpCache, RangeGET_InvalidResponse1) {
5363 MockHttpCache cache; 5363 MockHttpCache cache(false);
5364 std::string headers; 5364 std::string headers;
5365 5365
5366 MockTransaction transaction(kRangeGET_TransactionOK); 5366 MockTransaction transaction(kRangeGET_TransactionOK);
5367 transaction.handler = NULL; 5367 transaction.handler = NULL;
5368 transaction.response_headers = "Content-Range: bytes 40-49/45\n" 5368 transaction.response_headers = "Content-Range: bytes 40-49/45\n"
5369 "Content-Length: 10\n"; 5369 "Content-Length: 10\n";
5370 AddMockTransaction(&transaction); 5370 AddMockTransaction(&transaction);
5371 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 5371 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5372 5372
5373 std::string expected(transaction.status); 5373 std::string expected(transaction.status);
5374 expected.append("\n"); 5374 expected.append("\n");
5375 expected.append(transaction.response_headers); 5375 expected.append(transaction.response_headers);
5376 EXPECT_EQ(expected, headers); 5376 EXPECT_EQ(expected, headers);
5377 5377
5378 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5378 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5379 EXPECT_EQ(0, cache.disk_cache()->open_count()); 5379 EXPECT_EQ(0, cache.disk_cache()->open_count());
5380 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5380 EXPECT_EQ(1, cache.disk_cache()->create_count());
5381 5381
5382 // Verify that we don't have a cached entry. 5382 // Verify that we don't have a cached entry.
5383 disk_cache::Entry* entry; 5383 disk_cache::Entry* entry;
5384 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); 5384 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
5385 5385
5386 RemoveMockTransaction(&kRangeGET_TransactionOK); 5386 RemoveMockTransaction(&kRangeGET_TransactionOK);
5387 } 5387 }
5388 5388
5389 // Tests that we reject a range that doesn't match the content-length. 5389 // Tests that we reject a range that doesn't match the content-length.
5390 TEST(HttpCache, RangeGET_InvalidResponse2) { 5390 TEST(HttpCache, RangeGET_InvalidResponse2) {
5391 MockHttpCache cache; 5391 MockHttpCache cache(false);
5392 std::string headers; 5392 std::string headers;
5393 5393
5394 MockTransaction transaction(kRangeGET_TransactionOK); 5394 MockTransaction transaction(kRangeGET_TransactionOK);
5395 transaction.handler = NULL; 5395 transaction.handler = NULL;
5396 transaction.response_headers = "Content-Range: bytes 40-49/80\n" 5396 transaction.response_headers = "Content-Range: bytes 40-49/80\n"
5397 "Content-Length: 20\n"; 5397 "Content-Length: 20\n";
5398 AddMockTransaction(&transaction); 5398 AddMockTransaction(&transaction);
5399 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 5399 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5400 5400
5401 std::string expected(transaction.status); 5401 std::string expected(transaction.status);
5402 expected.append("\n"); 5402 expected.append("\n");
5403 expected.append(transaction.response_headers); 5403 expected.append(transaction.response_headers);
5404 EXPECT_EQ(expected, headers); 5404 EXPECT_EQ(expected, headers);
5405 5405
5406 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5406 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5407 EXPECT_EQ(0, cache.disk_cache()->open_count()); 5407 EXPECT_EQ(0, cache.disk_cache()->open_count());
5408 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5408 EXPECT_EQ(1, cache.disk_cache()->create_count());
5409 5409
5410 // Verify that we don't have a cached entry. 5410 // Verify that we don't have a cached entry.
5411 disk_cache::Entry* entry; 5411 disk_cache::Entry* entry;
5412 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); 5412 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
5413 5413
5414 RemoveMockTransaction(&kRangeGET_TransactionOK); 5414 RemoveMockTransaction(&kRangeGET_TransactionOK);
5415 } 5415 }
5416 5416
5417 // Tests that if a server tells us conflicting information about a resource we 5417 // Tests that if a server tells us conflicting information about a resource we
5418 // drop the entry. 5418 // drop the entry.
5419 TEST(HttpCache, RangeGET_InvalidResponse3) { 5419 TEST(HttpCache, RangeGET_InvalidResponse3) {
5420 MockHttpCache cache; 5420 MockHttpCache cache(false);
5421 std::string headers; 5421 std::string headers;
5422 5422
5423 MockTransaction transaction(kRangeGET_TransactionOK); 5423 MockTransaction transaction(kRangeGET_TransactionOK);
5424 transaction.handler = NULL; 5424 transaction.handler = NULL;
5425 transaction.request_headers = "Range: bytes = 50-59\r\n" EXTRA_HEADER; 5425 transaction.request_headers = "Range: bytes = 50-59\r\n" EXTRA_HEADER;
5426 std::string response_headers(transaction.response_headers); 5426 std::string response_headers(transaction.response_headers);
5427 response_headers.append("Content-Range: bytes 50-59/160\n"); 5427 response_headers.append("Content-Range: bytes 50-59/160\n");
5428 transaction.response_headers = response_headers.c_str(); 5428 transaction.response_headers = response_headers.c_str();
5429 AddMockTransaction(&transaction); 5429 AddMockTransaction(&transaction);
5430 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 5430 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
(...skipping 19 matching lines...) Expand all
5450 // Verify that the entry is gone. 5450 // Verify that the entry is gone.
5451 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); 5451 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5452 EXPECT_EQ(1, cache.disk_cache()->open_count()); 5452 EXPECT_EQ(1, cache.disk_cache()->open_count());
5453 EXPECT_EQ(2, cache.disk_cache()->create_count()); 5453 EXPECT_EQ(2, cache.disk_cache()->create_count());
5454 RemoveMockTransaction(&kRangeGET_TransactionOK); 5454 RemoveMockTransaction(&kRangeGET_TransactionOK);
5455 } 5455 }
5456 5456
5457 // Tests that we handle large range values properly. 5457 // Tests that we handle large range values properly.
5458 TEST(HttpCache, RangeGET_LargeValues) { 5458 TEST(HttpCache, RangeGET_LargeValues) {
5459 // We need a real sparse cache for this test. 5459 // We need a real sparse cache for this test.
5460 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(1024 * 1024)); 5460 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(1024 * 1024), false);
5461 std::string headers; 5461 std::string headers;
5462 5462
5463 MockTransaction transaction(kRangeGET_TransactionOK); 5463 MockTransaction transaction(kRangeGET_TransactionOK);
5464 transaction.handler = NULL; 5464 transaction.handler = NULL;
5465 transaction.request_headers = "Range: bytes = 4294967288-4294967297\r\n" 5465 transaction.request_headers = "Range: bytes = 4294967288-4294967297\r\n"
5466 EXTRA_HEADER; 5466 EXTRA_HEADER;
5467 transaction.response_headers = 5467 transaction.response_headers =
5468 "ETag: \"foo\"\n" 5468 "ETag: \"foo\"\n"
5469 "Content-Range: bytes 4294967288-4294967297/4294967299\n" 5469 "Content-Range: bytes 4294967288-4294967297/4294967299\n"
5470 "Content-Length: 10\n"; 5470 "Content-Length: 10\n";
(...skipping 15 matching lines...) Expand all
5486 RemoveMockTransaction(&kRangeGET_TransactionOK); 5486 RemoveMockTransaction(&kRangeGET_TransactionOK);
5487 } 5487 }
5488 5488
5489 // Tests that we don't crash with a range request if the disk cache was not 5489 // Tests that we don't crash with a range request if the disk cache was not
5490 // initialized properly. 5490 // initialized properly.
5491 TEST(HttpCache, RangeGET_NoDiskCache) { 5491 TEST(HttpCache, RangeGET_NoDiskCache) {
5492 std::unique_ptr<MockBlockingBackendFactory> factory( 5492 std::unique_ptr<MockBlockingBackendFactory> factory(
5493 new MockBlockingBackendFactory()); 5493 new MockBlockingBackendFactory());
5494 factory->set_fail(true); 5494 factory->set_fail(true);
5495 factory->FinishCreation(); // We'll complete synchronously. 5495 factory->FinishCreation(); // We'll complete synchronously.
5496 MockHttpCache cache(std::move(factory)); 5496 MockHttpCache cache(std::move(factory), false);
5497 5497
5498 AddMockTransaction(&kRangeGET_TransactionOK); 5498 AddMockTransaction(&kRangeGET_TransactionOK);
5499 5499
5500 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); 5500 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5501 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5501 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5502 5502
5503 RemoveMockTransaction(&kRangeGET_TransactionOK); 5503 RemoveMockTransaction(&kRangeGET_TransactionOK);
5504 } 5504 }
5505 5505
5506 // Tests that we handle byte range requests that skip the cache. 5506 // Tests that we handle byte range requests that skip the cache.
5507 TEST(HttpCache, RangeHEAD) { 5507 TEST(HttpCache, RangeHEAD) {
5508 MockHttpCache cache; 5508 MockHttpCache cache(false);
5509 AddMockTransaction(&kRangeGET_TransactionOK); 5509 AddMockTransaction(&kRangeGET_TransactionOK);
5510 5510
5511 MockTransaction transaction(kRangeGET_TransactionOK); 5511 MockTransaction transaction(kRangeGET_TransactionOK);
5512 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; 5512 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
5513 transaction.method = "HEAD"; 5513 transaction.method = "HEAD";
5514 transaction.data = "rg: 70-79 "; 5514 transaction.data = "rg: 70-79 ";
5515 5515
5516 std::string headers; 5516 std::string headers;
5517 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 5517 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5518 5518
5519 Verify206Response(headers, 70, 79); 5519 Verify206Response(headers, 70, 79);
5520 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5520 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5521 EXPECT_EQ(0, cache.disk_cache()->open_count()); 5521 EXPECT_EQ(0, cache.disk_cache()->open_count());
5522 EXPECT_EQ(0, cache.disk_cache()->create_count()); 5522 EXPECT_EQ(0, cache.disk_cache()->create_count());
5523 5523
5524 RemoveMockTransaction(&kRangeGET_TransactionOK); 5524 RemoveMockTransaction(&kRangeGET_TransactionOK);
5525 } 5525 }
5526 5526
5527 // Tests that we don't crash when after reading from the cache we issue a 5527 // Tests that we don't crash when after reading from the cache we issue a
5528 // request for the next range and the server gives us a 200 synchronously. 5528 // request for the next range and the server gives us a 200 synchronously.
5529 TEST(HttpCache, RangeGET_FastFlakyServer) { 5529 TEST(HttpCache, RangeGET_FastFlakyServer) {
5530 MockHttpCache cache; 5530 MockHttpCache cache(false);
5531 5531
5532 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 5532 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
5533 transaction.request_headers = "Range: bytes = 40-\r\n" EXTRA_HEADER; 5533 transaction.request_headers = "Range: bytes = 40-\r\n" EXTRA_HEADER;
5534 transaction.test_mode = TEST_MODE_SYNC_NET_START; 5534 transaction.test_mode = TEST_MODE_SYNC_NET_START;
5535 transaction.load_flags |= LOAD_VALIDATE_CACHE; 5535 transaction.load_flags |= LOAD_VALIDATE_CACHE;
5536 5536
5537 // Write to the cache. 5537 // Write to the cache.
5538 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); 5538 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5539 5539
5540 // And now read from the cache and the network. 5540 // And now read from the cache and the network.
5541 RangeTransactionServer handler; 5541 RangeTransactionServer handler;
5542 handler.set_bad_200(true); 5542 handler.set_bad_200(true);
5543 transaction.data = "Not a range"; 5543 transaction.data = "Not a range";
5544 BoundTestNetLog log; 5544 BoundTestNetLog log;
5545 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); 5545 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound());
5546 5546
5547 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 5547 EXPECT_EQ(3, cache.network_layer()->transaction_count());
5548 EXPECT_EQ(1, cache.disk_cache()->open_count()); 5548 EXPECT_EQ(1, cache.disk_cache()->open_count());
5549 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5549 EXPECT_EQ(1, cache.disk_cache()->create_count());
5550 EXPECT_TRUE(LogContainsEventType( 5550 EXPECT_TRUE(LogContainsEventType(
5551 log, NetLogEventType::HTTP_CACHE_RE_SEND_PARTIAL_REQUEST)); 5551 log, NetLogEventType::HTTP_CACHE_RE_SEND_PARTIAL_REQUEST));
5552 } 5552 }
5553 5553
5554 // Tests that when the server gives us less data than expected, we don't keep 5554 // Tests that when the server gives us less data than expected, we don't keep
5555 // asking for more data. 5555 // asking for more data.
5556 TEST(HttpCache, RangeGET_FastFlakyServer2) { 5556 TEST(HttpCache, RangeGET_FastFlakyServer2) {
5557 MockHttpCache cache; 5557 MockHttpCache cache(false);
5558 5558
5559 // First, check with an empty cache (WRITE mode). 5559 // First, check with an empty cache (WRITE mode).
5560 MockTransaction transaction(kRangeGET_TransactionOK); 5560 MockTransaction transaction(kRangeGET_TransactionOK);
5561 transaction.request_headers = "Range: bytes = 40-49\r\n" EXTRA_HEADER; 5561 transaction.request_headers = "Range: bytes = 40-49\r\n" EXTRA_HEADER;
5562 transaction.data = "rg: 40-"; // Less than expected. 5562 transaction.data = "rg: 40-"; // Less than expected.
5563 transaction.handler = NULL; 5563 transaction.handler = NULL;
5564 std::string headers(transaction.response_headers); 5564 std::string headers(transaction.response_headers);
5565 headers.append("Content-Range: bytes 40-49/80\n"); 5565 headers.append("Content-Range: bytes 40-49/80\n");
5566 transaction.response_headers = headers.c_str(); 5566 transaction.response_headers = headers.c_str();
5567 5567
(...skipping 19 matching lines...) Expand all
5587 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 5587 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5588 EXPECT_EQ(1, cache.disk_cache()->open_count()); 5588 EXPECT_EQ(1, cache.disk_cache()->open_count());
5589 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5589 EXPECT_EQ(1, cache.disk_cache()->create_count());
5590 5590
5591 RemoveMockTransaction(&transaction); 5591 RemoveMockTransaction(&transaction);
5592 } 5592 }
5593 5593
5594 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) 5594 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
5595 // This test hits a NOTREACHED so it is a release mode only test. 5595 // This test hits a NOTREACHED so it is a release mode only test.
5596 TEST(HttpCache, RangeGET_OK_LoadOnlyFromCache) { 5596 TEST(HttpCache, RangeGET_OK_LoadOnlyFromCache) {
5597 MockHttpCache cache; 5597 MockHttpCache cache(false);
5598 AddMockTransaction(&kRangeGET_TransactionOK); 5598 AddMockTransaction(&kRangeGET_TransactionOK);
5599 5599
5600 // Write to the cache (40-49). 5600 // Write to the cache (40-49).
5601 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); 5601 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
5602 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5602 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5603 EXPECT_EQ(0, cache.disk_cache()->open_count()); 5603 EXPECT_EQ(0, cache.disk_cache()->open_count());
5604 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5604 EXPECT_EQ(1, cache.disk_cache()->create_count());
5605 5605
5606 // Force this transaction to read from the cache. 5606 // Force this transaction to read from the cache.
5607 MockTransaction transaction(kRangeGET_TransactionOK); 5607 MockTransaction transaction(kRangeGET_TransactionOK);
(...skipping 17 matching lines...) Expand all
5625 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5625 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5626 EXPECT_EQ(1, cache.disk_cache()->open_count()); 5626 EXPECT_EQ(1, cache.disk_cache()->open_count());
5627 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5627 EXPECT_EQ(1, cache.disk_cache()->create_count());
5628 5628
5629 RemoveMockTransaction(&kRangeGET_TransactionOK); 5629 RemoveMockTransaction(&kRangeGET_TransactionOK);
5630 } 5630 }
5631 #endif 5631 #endif
5632 5632
5633 // Tests the handling of the "truncation" flag. 5633 // Tests the handling of the "truncation" flag.
5634 TEST(HttpCache, WriteResponseInfo_Truncated) { 5634 TEST(HttpCache, WriteResponseInfo_Truncated) {
5635 MockHttpCache cache; 5635 MockHttpCache cache(false);
5636 disk_cache::Entry* entry; 5636 disk_cache::Entry* entry;
5637 ASSERT_TRUE(cache.CreateBackendEntry("http://www.google.com", &entry, 5637 ASSERT_TRUE(cache.CreateBackendEntry("http://www.google.com", &entry,
5638 NULL)); 5638 NULL));
5639 5639
5640 std::string headers("HTTP/1.1 200 OK"); 5640 std::string headers("HTTP/1.1 200 OK");
5641 headers = HttpUtil::AssembleRawHeaders(headers.data(), headers.size()); 5641 headers = HttpUtil::AssembleRawHeaders(headers.data(), headers.size());
5642 HttpResponseInfo response; 5642 HttpResponseInfo response;
5643 response.headers = new HttpResponseHeaders(headers); 5643 response.headers = new HttpResponseHeaders(headers);
5644 5644
5645 // Set the last argument for this to be an incomplete request. 5645 // Set the last argument for this to be an incomplete request.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
5677 // Verify fields. 5677 // Verify fields.
5678 EXPECT_TRUE(response2.was_cached); // InitFromPickle sets this flag. 5678 EXPECT_TRUE(response2.was_cached); // InitFromPickle sets this flag.
5679 EXPECT_EQ("1.2.3.4", response2.socket_address.host()); 5679 EXPECT_EQ("1.2.3.4", response2.socket_address.host());
5680 EXPECT_EQ(80, response2.socket_address.port()); 5680 EXPECT_EQ(80, response2.socket_address.port());
5681 EXPECT_EQ("HTTP/1.1 200 OK", response2.headers->GetStatusLine()); 5681 EXPECT_EQ("HTTP/1.1 200 OK", response2.headers->GetStatusLine());
5682 } 5682 }
5683 5683
5684 // Tests that we delete an entry when the request is cancelled before starting 5684 // Tests that we delete an entry when the request is cancelled before starting
5685 // to read from the network. 5685 // to read from the network.
5686 TEST(HttpCache, DoomOnDestruction) { 5686 TEST(HttpCache, DoomOnDestruction) {
5687 MockHttpCache cache; 5687 MockHttpCache cache(false);
5688 5688
5689 MockHttpRequest request(kSimpleGET_Transaction); 5689 MockHttpRequest request(kSimpleGET_Transaction);
5690 5690
5691 Context* c = new Context(); 5691 Context* c = new Context();
5692 int rv = cache.CreateTransaction(&c->trans); 5692 int rv = cache.CreateTransaction(&c->trans);
5693 ASSERT_THAT(rv, IsOk()); 5693 ASSERT_THAT(rv, IsOk());
5694 5694
5695 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); 5695 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
5696 if (rv == ERR_IO_PENDING) 5696 if (rv == ERR_IO_PENDING)
5697 c->result = c->callback.WaitForResult(); 5697 c->result = c->callback.WaitForResult();
5698 5698
5699 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5699 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5700 EXPECT_EQ(0, cache.disk_cache()->open_count()); 5700 EXPECT_EQ(0, cache.disk_cache()->open_count());
5701 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5701 EXPECT_EQ(1, cache.disk_cache()->create_count());
5702 5702
5703 // Destroy the transaction. We only have the headers so we should delete this 5703 // Destroy the transaction. We only have the headers so we should delete this
5704 // entry. 5704 // entry.
5705 delete c; 5705 delete c;
5706 5706
5707 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 5707 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
5708 5708
5709 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 5709 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5710 EXPECT_EQ(0, cache.disk_cache()->open_count()); 5710 EXPECT_EQ(0, cache.disk_cache()->open_count());
5711 EXPECT_EQ(2, cache.disk_cache()->create_count()); 5711 EXPECT_EQ(2, cache.disk_cache()->create_count());
5712 } 5712 }
5713 5713
5714 // Tests that we delete an entry when the request is cancelled if the response 5714 // Tests that we delete an entry when the request is cancelled if the response
5715 // does not have content-length and strong validators. 5715 // does not have content-length and strong validators.
5716 TEST(HttpCache, DoomOnDestruction2) { 5716 TEST(HttpCache, DoomOnDestruction2) {
5717 MockHttpCache cache; 5717 MockHttpCache cache(false);
5718 5718
5719 MockHttpRequest request(kSimpleGET_Transaction); 5719 MockHttpRequest request(kSimpleGET_Transaction);
5720 5720
5721 Context* c = new Context(); 5721 Context* c = new Context();
5722 int rv = cache.CreateTransaction(&c->trans); 5722 int rv = cache.CreateTransaction(&c->trans);
5723 ASSERT_THAT(rv, IsOk()); 5723 ASSERT_THAT(rv, IsOk());
5724 5724
5725 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); 5725 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
5726 if (rv == ERR_IO_PENDING) 5726 if (rv == ERR_IO_PENDING)
5727 rv = c->callback.WaitForResult(); 5727 rv = c->callback.WaitForResult();
(...skipping 15 matching lines...) Expand all
5743 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 5743 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
5744 5744
5745 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 5745 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5746 EXPECT_EQ(0, cache.disk_cache()->open_count()); 5746 EXPECT_EQ(0, cache.disk_cache()->open_count());
5747 EXPECT_EQ(2, cache.disk_cache()->create_count()); 5747 EXPECT_EQ(2, cache.disk_cache()->create_count());
5748 } 5748 }
5749 5749
5750 // Tests that we delete an entry when the request is cancelled if the response 5750 // Tests that we delete an entry when the request is cancelled if the response
5751 // has an "Accept-Ranges: none" header. 5751 // has an "Accept-Ranges: none" header.
5752 TEST(HttpCache, DoomOnDestruction3) { 5752 TEST(HttpCache, DoomOnDestruction3) {
5753 MockHttpCache cache; 5753 MockHttpCache cache(false);
5754 5754
5755 MockTransaction transaction(kSimpleGET_Transaction); 5755 MockTransaction transaction(kSimpleGET_Transaction);
5756 transaction.response_headers = 5756 transaction.response_headers =
5757 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" 5757 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
5758 "Content-Length: 22\n" 5758 "Content-Length: 22\n"
5759 "Accept-Ranges: none\n" 5759 "Accept-Ranges: none\n"
5760 "Etag: \"foopy\"\n"; 5760 "Etag: \"foopy\"\n";
5761 AddMockTransaction(&transaction); 5761 AddMockTransaction(&transaction);
5762 MockHttpRequest request(transaction); 5762 MockHttpRequest request(transaction);
5763 5763
(...skipping 23 matching lines...) Expand all
5787 5787
5788 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 5788 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5789 EXPECT_EQ(0, cache.disk_cache()->open_count()); 5789 EXPECT_EQ(0, cache.disk_cache()->open_count());
5790 EXPECT_EQ(2, cache.disk_cache()->create_count()); 5790 EXPECT_EQ(2, cache.disk_cache()->create_count());
5791 5791
5792 RemoveMockTransaction(&transaction); 5792 RemoveMockTransaction(&transaction);
5793 } 5793 }
5794 5794
5795 // Tests that we mark an entry as incomplete when the request is cancelled. 5795 // Tests that we mark an entry as incomplete when the request is cancelled.
5796 TEST(HttpCache, SetTruncatedFlag) { 5796 TEST(HttpCache, SetTruncatedFlag) {
5797 MockHttpCache cache; 5797 MockHttpCache cache(false);
5798 5798
5799 ScopedMockTransaction transaction(kSimpleGET_Transaction); 5799 ScopedMockTransaction transaction(kSimpleGET_Transaction);
5800 transaction.response_headers = 5800 transaction.response_headers =
5801 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" 5801 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
5802 "Content-Length: 22\n" 5802 "Content-Length: 22\n"
5803 "Etag: \"foopy\"\n"; 5803 "Etag: \"foopy\"\n";
5804 MockHttpRequest request(transaction); 5804 MockHttpRequest request(transaction);
5805 5805
5806 std::unique_ptr<Context> c(new Context()); 5806 std::unique_ptr<Context> c(new Context());
5807 5807
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
5840 // could end up with the transaction being deleted twice if we send any 5840 // could end up with the transaction being deleted twice if we send any
5841 // notification from the transaction destructor (see http://crbug.com/31723). 5841 // notification from the transaction destructor (see http://crbug.com/31723).
5842 EXPECT_FALSE(c->callback.have_result()); 5842 EXPECT_FALSE(c->callback.have_result());
5843 5843
5844 // Verify that the entry is marked as incomplete. 5844 // Verify that the entry is marked as incomplete.
5845 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); 5845 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0);
5846 } 5846 }
5847 5847
5848 // Tests that we don't mark an entry as truncated when we read everything. 5848 // Tests that we don't mark an entry as truncated when we read everything.
5849 TEST(HttpCache, DontSetTruncatedFlag) { 5849 TEST(HttpCache, DontSetTruncatedFlag) {
5850 MockHttpCache cache; 5850 MockHttpCache cache(false);
5851 5851
5852 ScopedMockTransaction transaction(kSimpleGET_Transaction); 5852 ScopedMockTransaction transaction(kSimpleGET_Transaction);
5853 transaction.response_headers = 5853 transaction.response_headers =
5854 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" 5854 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
5855 "Content-Length: 22\n" 5855 "Content-Length: 22\n"
5856 "Etag: \"foopy\"\n"; 5856 "Etag: \"foopy\"\n";
5857 MockHttpRequest request(transaction); 5857 MockHttpRequest request(transaction);
5858 5858
5859 std::unique_ptr<Context> c(new Context()); 5859 std::unique_ptr<Context> c(new Context());
5860 int rv = cache.CreateTransaction(&c->trans); 5860 int rv = cache.CreateTransaction(&c->trans);
5861 ASSERT_THAT(rv, IsOk()); 5861 ASSERT_THAT(rv, IsOk());
5862 5862
5863 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); 5863 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
5864 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); 5864 EXPECT_THAT(c->callback.GetResult(rv), IsOk());
5865 5865
5866 // Read everything. 5866 // Read everything.
5867 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(22)); 5867 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(22));
5868 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); 5868 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
5869 EXPECT_EQ(buf->size(), c->callback.GetResult(rv)); 5869 EXPECT_EQ(buf->size(), c->callback.GetResult(rv));
5870 5870
5871 // Destroy the transaction. 5871 // Destroy the transaction.
5872 c->trans.reset(); 5872 c->trans.reset();
5873 5873
5874 // Verify that the entry is not marked as truncated. 5874 // Verify that the entry is not marked as truncated.
5875 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, false, 0); 5875 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, false, 0);
5876 } 5876 }
5877 5877
5878 // Tests that sparse entries don't set the truncate flag. 5878 // Tests that sparse entries don't set the truncate flag.
5879 TEST(HttpCache, RangeGET_DontTruncate) { 5879 TEST(HttpCache, RangeGET_DontTruncate) {
5880 MockHttpCache cache; 5880 MockHttpCache cache(false);
5881 5881
5882 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 5882 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
5883 transaction.request_headers = "Range: bytes = 0-19\r\n" EXTRA_HEADER; 5883 transaction.request_headers = "Range: bytes = 0-19\r\n" EXTRA_HEADER;
5884 5884
5885 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); 5885 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction));
5886 std::unique_ptr<HttpTransaction> trans; 5886 std::unique_ptr<HttpTransaction> trans;
5887 5887
5888 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); 5888 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
5889 EXPECT_THAT(rv, IsOk()); 5889 EXPECT_THAT(rv, IsOk());
5890 5890
5891 TestCompletionCallback cb; 5891 TestCompletionCallback cb;
5892 rv = trans->Start(request.get(), cb.callback(), NetLogWithSource()); 5892 rv = trans->Start(request.get(), cb.callback(), NetLogWithSource());
5893 EXPECT_EQ(0, cb.GetResult(rv)); 5893 EXPECT_EQ(0, cb.GetResult(rv));
5894 5894
5895 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); 5895 scoped_refptr<IOBuffer> buf(new IOBuffer(10));
5896 rv = trans->Read(buf.get(), 10, cb.callback()); 5896 rv = trans->Read(buf.get(), 10, cb.callback());
5897 EXPECT_EQ(10, cb.GetResult(rv)); 5897 EXPECT_EQ(10, cb.GetResult(rv));
5898 5898
5899 // Should not trigger any DCHECK. 5899 // Should not trigger any DCHECK.
5900 trans.reset(); 5900 trans.reset();
5901 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); 5901 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0);
5902 } 5902 }
5903 5903
5904 // Tests that sparse entries don't set the truncate flag (when the byte range 5904 // Tests that sparse entries don't set the truncate flag (when the byte range
5905 // starts after 0). 5905 // starts after 0).
5906 TEST(HttpCache, RangeGET_DontTruncate2) { 5906 TEST(HttpCache, RangeGET_DontTruncate2) {
5907 MockHttpCache cache; 5907 MockHttpCache cache(false);
5908 5908
5909 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 5909 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
5910 transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER; 5910 transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER;
5911 5911
5912 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); 5912 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction));
5913 std::unique_ptr<HttpTransaction> trans; 5913 std::unique_ptr<HttpTransaction> trans;
5914 5914
5915 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); 5915 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
5916 EXPECT_THAT(rv, IsOk()); 5916 EXPECT_THAT(rv, IsOk());
5917 5917
5918 TestCompletionCallback cb; 5918 TestCompletionCallback cb;
5919 rv = trans->Start(request.get(), cb.callback(), NetLogWithSource()); 5919 rv = trans->Start(request.get(), cb.callback(), NetLogWithSource());
5920 EXPECT_EQ(0, cb.GetResult(rv)); 5920 EXPECT_EQ(0, cb.GetResult(rv));
5921 5921
5922 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); 5922 scoped_refptr<IOBuffer> buf(new IOBuffer(10));
5923 rv = trans->Read(buf.get(), 10, cb.callback()); 5923 rv = trans->Read(buf.get(), 10, cb.callback());
5924 EXPECT_EQ(10, cb.GetResult(rv)); 5924 EXPECT_EQ(10, cb.GetResult(rv));
5925 5925
5926 // Should not trigger any DCHECK. 5926 // Should not trigger any DCHECK.
5927 trans.reset(); 5927 trans.reset();
5928 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); 5928 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0);
5929 } 5929 }
5930 5930
5931 // Tests that we can continue with a request that was interrupted. 5931 // Tests that we can continue with a request that was interrupted.
5932 TEST(HttpCache, GET_IncompleteResource) { 5932 TEST(HttpCache, GET_IncompleteResource) {
5933 MockHttpCache cache; 5933 MockHttpCache cache(false);
5934 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 5934 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
5935 5935
5936 std::string raw_headers("HTTP/1.1 200 OK\n" 5936 std::string raw_headers("HTTP/1.1 200 OK\n"
5937 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 5937 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
5938 "ETag: \"foo\"\n" 5938 "ETag: \"foo\"\n"
5939 "Accept-Ranges: bytes\n" 5939 "Accept-Ranges: bytes\n"
5940 "Content-Length: 80\n"); 5940 "Content-Length: 80\n");
5941 CreateTruncatedEntry(raw_headers, &cache); 5941 CreateTruncatedEntry(raw_headers, &cache);
5942 5942
5943 // Now make a regular request. 5943 // Now make a regular request.
(...skipping 14 matching lines...) Expand all
5958 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 5958 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5959 EXPECT_EQ(1, cache.disk_cache()->open_count()); 5959 EXPECT_EQ(1, cache.disk_cache()->open_count());
5960 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5960 EXPECT_EQ(1, cache.disk_cache()->create_count());
5961 5961
5962 // Verify that the disk entry was updated. 5962 // Verify that the disk entry was updated.
5963 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80); 5963 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80);
5964 } 5964 }
5965 5965
5966 // Tests the handling of no-store when revalidating a truncated entry. 5966 // Tests the handling of no-store when revalidating a truncated entry.
5967 TEST(HttpCache, GET_IncompleteResource_NoStore) { 5967 TEST(HttpCache, GET_IncompleteResource_NoStore) {
5968 MockHttpCache cache; 5968 MockHttpCache cache(false);
5969 AddMockTransaction(&kRangeGET_TransactionOK); 5969 AddMockTransaction(&kRangeGET_TransactionOK);
5970 5970
5971 std::string raw_headers("HTTP/1.1 200 OK\n" 5971 std::string raw_headers("HTTP/1.1 200 OK\n"
5972 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 5972 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
5973 "ETag: \"foo\"\n" 5973 "ETag: \"foo\"\n"
5974 "Accept-Ranges: bytes\n" 5974 "Accept-Ranges: bytes\n"
5975 "Content-Length: 80\n"); 5975 "Content-Length: 80\n");
5976 CreateTruncatedEntry(raw_headers, &cache); 5976 CreateTruncatedEntry(raw_headers, &cache);
5977 RemoveMockTransaction(&kRangeGET_TransactionOK); 5977 RemoveMockTransaction(&kRangeGET_TransactionOK);
5978 5978
(...skipping 24 matching lines...) Expand all
6003 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6003 EXPECT_EQ(1, cache.disk_cache()->create_count());
6004 6004
6005 // Verify that the disk entry was deleted. 6005 // Verify that the disk entry was deleted.
6006 disk_cache::Entry* entry; 6006 disk_cache::Entry* entry;
6007 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); 6007 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
6008 RemoveMockTransaction(&transaction); 6008 RemoveMockTransaction(&transaction);
6009 } 6009 }
6010 6010
6011 // Tests cancelling a request after the server sent no-store. 6011 // Tests cancelling a request after the server sent no-store.
6012 TEST(HttpCache, GET_IncompleteResource_Cancel) { 6012 TEST(HttpCache, GET_IncompleteResource_Cancel) {
6013 MockHttpCache cache; 6013 MockHttpCache cache(false);
6014 AddMockTransaction(&kRangeGET_TransactionOK); 6014 AddMockTransaction(&kRangeGET_TransactionOK);
6015 6015
6016 std::string raw_headers("HTTP/1.1 200 OK\n" 6016 std::string raw_headers("HTTP/1.1 200 OK\n"
6017 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 6017 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
6018 "ETag: \"foo\"\n" 6018 "ETag: \"foo\"\n"
6019 "Accept-Ranges: bytes\n" 6019 "Accept-Ranges: bytes\n"
6020 "Content-Length: 80\n"); 6020 "Content-Length: 80\n");
6021 CreateTruncatedEntry(raw_headers, &cache); 6021 CreateTruncatedEntry(raw_headers, &cache);
6022 RemoveMockTransaction(&kRangeGET_TransactionOK); 6022 RemoveMockTransaction(&kRangeGET_TransactionOK);
6023 6023
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
6061 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6061 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6062 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6062 EXPECT_EQ(1, cache.disk_cache()->open_count());
6063 EXPECT_EQ(2, cache.disk_cache()->create_count()); 6063 EXPECT_EQ(2, cache.disk_cache()->create_count());
6064 6064
6065 base::RunLoop().RunUntilIdle(); 6065 base::RunLoop().RunUntilIdle();
6066 RemoveMockTransaction(&transaction); 6066 RemoveMockTransaction(&transaction);
6067 } 6067 }
6068 6068
6069 // Tests that we delete truncated entries if the server changes its mind midway. 6069 // Tests that we delete truncated entries if the server changes its mind midway.
6070 TEST(HttpCache, GET_IncompleteResource2) { 6070 TEST(HttpCache, GET_IncompleteResource2) {
6071 MockHttpCache cache; 6071 MockHttpCache cache(false);
6072 AddMockTransaction(&kRangeGET_TransactionOK); 6072 AddMockTransaction(&kRangeGET_TransactionOK);
6073 6073
6074 // Content-length will be intentionally bad. 6074 // Content-length will be intentionally bad.
6075 std::string raw_headers("HTTP/1.1 200 OK\n" 6075 std::string raw_headers("HTTP/1.1 200 OK\n"
6076 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 6076 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
6077 "ETag: \"foo\"\n" 6077 "ETag: \"foo\"\n"
6078 "Accept-Ranges: bytes\n" 6078 "Accept-Ranges: bytes\n"
6079 "Content-Length: 50\n"); 6079 "Content-Length: 50\n");
6080 CreateTruncatedEntry(raw_headers, &cache); 6080 CreateTruncatedEntry(raw_headers, &cache);
6081 6081
(...skipping 16 matching lines...) Expand all
6098 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6098 EXPECT_EQ(1, cache.disk_cache()->create_count());
6099 6099
6100 // Verify that the disk entry was deleted. 6100 // Verify that the disk entry was deleted.
6101 disk_cache::Entry* entry; 6101 disk_cache::Entry* entry;
6102 ASSERT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); 6102 ASSERT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
6103 RemoveMockTransaction(&kRangeGET_TransactionOK); 6103 RemoveMockTransaction(&kRangeGET_TransactionOK);
6104 } 6104 }
6105 6105
6106 // Tests that we always validate a truncated request. 6106 // Tests that we always validate a truncated request.
6107 TEST(HttpCache, GET_IncompleteResource3) { 6107 TEST(HttpCache, GET_IncompleteResource3) {
6108 MockHttpCache cache; 6108 MockHttpCache cache(false);
6109 AddMockTransaction(&kRangeGET_TransactionOK); 6109 AddMockTransaction(&kRangeGET_TransactionOK);
6110 6110
6111 // This should not require validation for 10 hours. 6111 // This should not require validation for 10 hours.
6112 std::string raw_headers("HTTP/1.1 200 OK\n" 6112 std::string raw_headers("HTTP/1.1 200 OK\n"
6113 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" 6113 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
6114 "ETag: \"foo\"\n" 6114 "ETag: \"foo\"\n"
6115 "Cache-Control: max-age= 36000\n" 6115 "Cache-Control: max-age= 36000\n"
6116 "Accept-Ranges: bytes\n" 6116 "Accept-Ranges: bytes\n"
6117 "Content-Length: 80\n"); 6117 "Content-Length: 80\n");
6118 CreateTruncatedEntry(raw_headers, &cache); 6118 CreateTruncatedEntry(raw_headers, &cache);
(...skipping 15 matching lines...) Expand all
6134 // We should have checked with the server before finishing Start(). 6134 // We should have checked with the server before finishing Start().
6135 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6135 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6136 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6136 EXPECT_EQ(1, cache.disk_cache()->open_count());
6137 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6137 EXPECT_EQ(1, cache.disk_cache()->create_count());
6138 6138
6139 RemoveMockTransaction(&kRangeGET_TransactionOK); 6139 RemoveMockTransaction(&kRangeGET_TransactionOK);
6140 } 6140 }
6141 6141
6142 // Tests that we handle 401s for truncated resources. 6142 // Tests that we handle 401s for truncated resources.
6143 TEST(HttpCache, GET_IncompleteResourceWithAuth) { 6143 TEST(HttpCache, GET_IncompleteResourceWithAuth) {
6144 MockHttpCache cache; 6144 MockHttpCache cache(false);
6145 AddMockTransaction(&kRangeGET_TransactionOK); 6145 AddMockTransaction(&kRangeGET_TransactionOK);
6146 6146
6147 std::string raw_headers("HTTP/1.1 200 OK\n" 6147 std::string raw_headers("HTTP/1.1 200 OK\n"
6148 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 6148 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
6149 "ETag: \"foo\"\n" 6149 "ETag: \"foo\"\n"
6150 "Accept-Ranges: bytes\n" 6150 "Accept-Ranges: bytes\n"
6151 "Content-Length: 80\n"); 6151 "Content-Length: 80\n");
6152 CreateTruncatedEntry(raw_headers, &cache); 6152 CreateTruncatedEntry(raw_headers, &cache);
6153 6153
6154 // Now make a regular request. 6154 // Now make a regular request.
(...skipping 28 matching lines...) Expand all
6183 disk_cache::Entry* entry; 6183 disk_cache::Entry* entry;
6184 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); 6184 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry));
6185 entry->Close(); 6185 entry->Close();
6186 6186
6187 RemoveMockTransaction(&kRangeGET_TransactionOK); 6187 RemoveMockTransaction(&kRangeGET_TransactionOK);
6188 } 6188 }
6189 6189
6190 // Test that the transaction won't retry failed partial requests 6190 // Test that the transaction won't retry failed partial requests
6191 // after it starts reading data. http://crbug.com/474835 6191 // after it starts reading data. http://crbug.com/474835
6192 TEST(HttpCache, TransactionRetryLimit) { 6192 TEST(HttpCache, TransactionRetryLimit) {
6193 MockHttpCache cache; 6193 MockHttpCache cache(false);
6194 6194
6195 // Cache 0-9, so that we have data to read before failing. 6195 // Cache 0-9, so that we have data to read before failing.
6196 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 6196 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
6197 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; 6197 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
6198 transaction.data = "rg: 00-09 "; 6198 transaction.data = "rg: 00-09 ";
6199 6199
6200 // Write to the cache. 6200 // Write to the cache.
6201 RunTransactionTest(cache.http_cache(), transaction); 6201 RunTransactionTest(cache.http_cache(), transaction);
6202 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6202 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6203 6203
(...skipping 14 matching lines...) Expand all
6218 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); 6218 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
6219 if (rv == ERR_IO_PENDING) 6219 if (rv == ERR_IO_PENDING)
6220 rv = c->callback.WaitForResult(); 6220 rv = c->callback.WaitForResult();
6221 std::string content; 6221 std::string content;
6222 rv = ReadTransaction(c->trans.get(), &content); 6222 rv = ReadTransaction(c->trans.get(), &content);
6223 EXPECT_THAT(rv, IsError(ERR_CACHE_AUTH_FAILURE_AFTER_READ)); 6223 EXPECT_THAT(rv, IsError(ERR_CACHE_AUTH_FAILURE_AFTER_READ));
6224 } 6224 }
6225 6225
6226 // Tests that we cache a 200 response to the validation request. 6226 // Tests that we cache a 200 response to the validation request.
6227 TEST(HttpCache, GET_IncompleteResource4) { 6227 TEST(HttpCache, GET_IncompleteResource4) {
6228 MockHttpCache cache; 6228 MockHttpCache cache(false);
6229 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 6229 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
6230 6230
6231 std::string raw_headers("HTTP/1.1 200 OK\n" 6231 std::string raw_headers("HTTP/1.1 200 OK\n"
6232 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" 6232 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
6233 "ETag: \"foo\"\n" 6233 "ETag: \"foo\"\n"
6234 "Accept-Ranges: bytes\n" 6234 "Accept-Ranges: bytes\n"
6235 "Content-Length: 80\n"); 6235 "Content-Length: 80\n");
6236 CreateTruncatedEntry(raw_headers, &cache); 6236 CreateTruncatedEntry(raw_headers, &cache);
6237 6237
6238 // Now make a regular request. 6238 // Now make a regular request.
6239 std::string headers; 6239 std::string headers;
6240 transaction.request_headers = EXTRA_HEADER; 6240 transaction.request_headers = EXTRA_HEADER;
6241 transaction.data = "Not a range"; 6241 transaction.data = "Not a range";
6242 RangeTransactionServer handler; 6242 RangeTransactionServer handler;
6243 handler.set_bad_200(true); 6243 handler.set_bad_200(true);
6244 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 6244 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
6245 6245
6246 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6246 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6247 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6247 EXPECT_EQ(1, cache.disk_cache()->open_count());
6248 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6248 EXPECT_EQ(1, cache.disk_cache()->create_count());
6249 6249
6250 // Verify that the disk entry was updated. 6250 // Verify that the disk entry was updated.
6251 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 11); 6251 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 11);
6252 } 6252 }
6253 6253
6254 // Tests that when we cancel a request that was interrupted, we mark it again 6254 // Tests that when we cancel a request that was interrupted, we mark it again
6255 // as truncated. 6255 // as truncated.
6256 TEST(HttpCache, GET_CancelIncompleteResource) { 6256 TEST(HttpCache, GET_CancelIncompleteResource) {
6257 MockHttpCache cache; 6257 MockHttpCache cache(false);
6258 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 6258 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
6259 6259
6260 std::string raw_headers("HTTP/1.1 200 OK\n" 6260 std::string raw_headers("HTTP/1.1 200 OK\n"
6261 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" 6261 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
6262 "ETag: \"foo\"\n" 6262 "ETag: \"foo\"\n"
6263 "Accept-Ranges: bytes\n" 6263 "Accept-Ranges: bytes\n"
6264 "Content-Length: 80\n"); 6264 "Content-Length: 80\n");
6265 CreateTruncatedEntry(raw_headers, &cache); 6265 CreateTruncatedEntry(raw_headers, &cache);
6266 6266
6267 // Now make a regular request. 6267 // Now make a regular request.
(...skipping 21 matching lines...) Expand all
6289 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 6289 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6290 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6290 EXPECT_EQ(1, cache.disk_cache()->open_count());
6291 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6291 EXPECT_EQ(1, cache.disk_cache()->create_count());
6292 6292
6293 // Verify that the disk entry was updated: now we have 30 bytes. 6293 // Verify that the disk entry was updated: now we have 30 bytes.
6294 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, true, 30); 6294 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, true, 30);
6295 } 6295 }
6296 6296
6297 // Tests that we can handle range requests when we have a truncated entry. 6297 // Tests that we can handle range requests when we have a truncated entry.
6298 TEST(HttpCache, RangeGET_IncompleteResource) { 6298 TEST(HttpCache, RangeGET_IncompleteResource) {
6299 MockHttpCache cache; 6299 MockHttpCache cache(false);
6300 AddMockTransaction(&kRangeGET_TransactionOK); 6300 AddMockTransaction(&kRangeGET_TransactionOK);
6301 6301
6302 // Content-length will be intentionally bogus. 6302 // Content-length will be intentionally bogus.
6303 std::string raw_headers("HTTP/1.1 200 OK\n" 6303 std::string raw_headers("HTTP/1.1 200 OK\n"
6304 "Last-Modified: something\n" 6304 "Last-Modified: something\n"
6305 "ETag: \"foo\"\n" 6305 "ETag: \"foo\"\n"
6306 "Accept-Ranges: bytes\n" 6306 "Accept-Ranges: bytes\n"
6307 "Content-Length: 10\n"); 6307 "Content-Length: 10\n");
6308 CreateTruncatedEntry(raw_headers, &cache); 6308 CreateTruncatedEntry(raw_headers, &cache);
6309 6309
6310 // Now make a range request. 6310 // Now make a range request.
6311 std::string headers; 6311 std::string headers;
6312 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, 6312 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
6313 &headers); 6313 &headers);
6314 6314
6315 Verify206Response(headers, 40, 49); 6315 Verify206Response(headers, 40, 49);
6316 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6316 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6317 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6317 EXPECT_EQ(1, cache.disk_cache()->open_count());
6318 EXPECT_EQ(2, cache.disk_cache()->create_count()); 6318 EXPECT_EQ(2, cache.disk_cache()->create_count());
6319 6319
6320 RemoveMockTransaction(&kRangeGET_TransactionOK); 6320 RemoveMockTransaction(&kRangeGET_TransactionOK);
6321 } 6321 }
6322 6322
6323 TEST(HttpCache, SyncRead) { 6323 TEST(HttpCache, SyncRead) {
6324 MockHttpCache cache; 6324 MockHttpCache cache(false);
6325 6325
6326 // This test ensures that a read that completes synchronously does not cause 6326 // This test ensures that a read that completes synchronously does not cause
6327 // any problems. 6327 // any problems.
6328 6328
6329 ScopedMockTransaction transaction(kSimpleGET_Transaction); 6329 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6330 transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START | 6330 transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START |
6331 TEST_MODE_SYNC_CACHE_READ | 6331 TEST_MODE_SYNC_CACHE_READ |
6332 TEST_MODE_SYNC_CACHE_WRITE); 6332 TEST_MODE_SYNC_CACHE_WRITE);
6333 6333
6334 MockHttpRequest r1(transaction), 6334 MockHttpRequest r1(transaction),
(...skipping 17 matching lines...) Expand all
6352 EXPECT_TRUE(c1.is_done()); 6352 EXPECT_TRUE(c1.is_done());
6353 EXPECT_TRUE(c2.is_done()); 6353 EXPECT_TRUE(c2.is_done());
6354 EXPECT_TRUE(c3.is_done()); 6354 EXPECT_TRUE(c3.is_done());
6355 6355
6356 EXPECT_THAT(c1.error(), IsOk()); 6356 EXPECT_THAT(c1.error(), IsOk());
6357 EXPECT_THAT(c2.error(), IsOk()); 6357 EXPECT_THAT(c2.error(), IsOk());
6358 EXPECT_THAT(c3.error(), IsOk()); 6358 EXPECT_THAT(c3.error(), IsOk());
6359 } 6359 }
6360 6360
6361 TEST(HttpCache, ValidationResultsIn200) { 6361 TEST(HttpCache, ValidationResultsIn200) {
6362 MockHttpCache cache; 6362 MockHttpCache cache(false);
6363 6363
6364 // This test ensures that a conditional request, which results in a 200 6364 // This test ensures that a conditional request, which results in a 200
6365 // instead of a 304, properly truncates the existing response data. 6365 // instead of a 304, properly truncates the existing response data.
6366 6366
6367 // write to the cache 6367 // write to the cache
6368 RunTransactionTest(cache.http_cache(), kETagGET_Transaction); 6368 RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
6369 6369
6370 // force this transaction to validate the cache 6370 // force this transaction to validate the cache
6371 MockTransaction transaction(kETagGET_Transaction); 6371 MockTransaction transaction(kETagGET_Transaction);
6372 transaction.load_flags |= LOAD_VALIDATE_CACHE; 6372 transaction.load_flags |= LOAD_VALIDATE_CACHE;
6373 RunTransactionTest(cache.http_cache(), transaction); 6373 RunTransactionTest(cache.http_cache(), transaction);
6374 6374
6375 // read from the cache 6375 // read from the cache
6376 RunTransactionTest(cache.http_cache(), kETagGET_Transaction); 6376 RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
6377 } 6377 }
6378 6378
6379 TEST(HttpCache, CachedRedirect) { 6379 TEST(HttpCache, CachedRedirect) {
6380 MockHttpCache cache; 6380 MockHttpCache cache(false);
6381 6381
6382 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); 6382 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction);
6383 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; 6383 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently";
6384 kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; 6384 kTestTransaction.response_headers = "Location: http://www.bar.com/\n";
6385 6385
6386 MockHttpRequest request(kTestTransaction); 6386 MockHttpRequest request(kTestTransaction);
6387 TestCompletionCallback callback; 6387 TestCompletionCallback callback;
6388 6388
6389 // Write to the cache. 6389 // Write to the cache.
6390 { 6390 {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
6446 // read the response body -- want to test that it is still getting cached. 6446 // read the response body -- want to test that it is still getting cached.
6447 } 6447 }
6448 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6448 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6449 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6449 EXPECT_EQ(1, cache.disk_cache()->open_count());
6450 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6450 EXPECT_EQ(1, cache.disk_cache()->create_count());
6451 } 6451 }
6452 6452
6453 // Verify that no-cache resources are stored in cache, but are not fetched from 6453 // Verify that no-cache resources are stored in cache, but are not fetched from
6454 // cache during normal loads. 6454 // cache during normal loads.
6455 TEST(HttpCache, CacheControlNoCacheNormalLoad) { 6455 TEST(HttpCache, CacheControlNoCacheNormalLoad) {
6456 MockHttpCache cache; 6456 MockHttpCache cache(false);
6457 6457
6458 ScopedMockTransaction transaction(kSimpleGET_Transaction); 6458 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6459 transaction.response_headers = "cache-control: no-cache\n"; 6459 transaction.response_headers = "cache-control: no-cache\n";
6460 6460
6461 // Initial load. 6461 // Initial load.
6462 RunTransactionTest(cache.http_cache(), transaction); 6462 RunTransactionTest(cache.http_cache(), transaction);
6463 6463
6464 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6464 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6465 EXPECT_EQ(0, cache.disk_cache()->open_count()); 6465 EXPECT_EQ(0, cache.disk_cache()->open_count());
6466 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6466 EXPECT_EQ(1, cache.disk_cache()->create_count());
6467 6467
6468 // Try loading again; it should result in a network fetch. 6468 // Try loading again; it should result in a network fetch.
6469 RunTransactionTest(cache.http_cache(), transaction); 6469 RunTransactionTest(cache.http_cache(), transaction);
6470 6470
6471 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 6471 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6472 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6472 EXPECT_EQ(1, cache.disk_cache()->open_count());
6473 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6473 EXPECT_EQ(1, cache.disk_cache()->create_count());
6474 6474
6475 disk_cache::Entry* entry; 6475 disk_cache::Entry* entry;
6476 EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry)); 6476 EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry));
6477 entry->Close(); 6477 entry->Close();
6478 } 6478 }
6479 6479
6480 // Verify that no-cache resources are stored in cache and fetched from cache 6480 // Verify that no-cache resources are stored in cache and fetched from cache
6481 // when the LOAD_SKIP_CACHE_VALIDATION flag is set. 6481 // when the LOAD_SKIP_CACHE_VALIDATION flag is set.
6482 TEST(HttpCache, CacheControlNoCacheHistoryLoad) { 6482 TEST(HttpCache, CacheControlNoCacheHistoryLoad) {
6483 MockHttpCache cache; 6483 MockHttpCache cache(false);
6484 6484
6485 ScopedMockTransaction transaction(kSimpleGET_Transaction); 6485 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6486 transaction.response_headers = "cache-control: no-cache\n"; 6486 transaction.response_headers = "cache-control: no-cache\n";
6487 6487
6488 // Initial load. 6488 // Initial load.
6489 RunTransactionTest(cache.http_cache(), transaction); 6489 RunTransactionTest(cache.http_cache(), transaction);
6490 6490
6491 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6491 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6492 EXPECT_EQ(0, cache.disk_cache()->open_count()); 6492 EXPECT_EQ(0, cache.disk_cache()->open_count());
6493 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6493 EXPECT_EQ(1, cache.disk_cache()->create_count());
6494 6494
6495 // Try loading again with LOAD_SKIP_CACHE_VALIDATION. 6495 // Try loading again with LOAD_SKIP_CACHE_VALIDATION.
6496 transaction.load_flags = LOAD_SKIP_CACHE_VALIDATION; 6496 transaction.load_flags = LOAD_SKIP_CACHE_VALIDATION;
6497 RunTransactionTest(cache.http_cache(), transaction); 6497 RunTransactionTest(cache.http_cache(), transaction);
6498 6498
6499 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6499 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6500 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6500 EXPECT_EQ(1, cache.disk_cache()->open_count());
6501 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6501 EXPECT_EQ(1, cache.disk_cache()->create_count());
6502 6502
6503 disk_cache::Entry* entry; 6503 disk_cache::Entry* entry;
6504 EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry)); 6504 EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry));
6505 entry->Close(); 6505 entry->Close();
6506 } 6506 }
6507 6507
6508 TEST(HttpCache, CacheControlNoStore) { 6508 TEST(HttpCache, CacheControlNoStore) {
6509 MockHttpCache cache; 6509 MockHttpCache cache(false);
6510 6510
6511 ScopedMockTransaction transaction(kSimpleGET_Transaction); 6511 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6512 transaction.response_headers = "cache-control: no-store\n"; 6512 transaction.response_headers = "cache-control: no-store\n";
6513 6513
6514 // initial load 6514 // initial load
6515 RunTransactionTest(cache.http_cache(), transaction); 6515 RunTransactionTest(cache.http_cache(), transaction);
6516 6516
6517 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6517 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6518 EXPECT_EQ(0, cache.disk_cache()->open_count()); 6518 EXPECT_EQ(0, cache.disk_cache()->open_count());
6519 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6519 EXPECT_EQ(1, cache.disk_cache()->create_count());
6520 6520
6521 // try loading again; it should result in a network fetch 6521 // try loading again; it should result in a network fetch
6522 RunTransactionTest(cache.http_cache(), transaction); 6522 RunTransactionTest(cache.http_cache(), transaction);
6523 6523
6524 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 6524 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6525 EXPECT_EQ(0, cache.disk_cache()->open_count()); 6525 EXPECT_EQ(0, cache.disk_cache()->open_count());
6526 EXPECT_EQ(2, cache.disk_cache()->create_count()); 6526 EXPECT_EQ(2, cache.disk_cache()->create_count());
6527 6527
6528 disk_cache::Entry* entry; 6528 disk_cache::Entry* entry;
6529 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); 6529 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry));
6530 } 6530 }
6531 6531
6532 TEST(HttpCache, CacheControlNoStore2) { 6532 TEST(HttpCache, CacheControlNoStore2) {
6533 // this test is similar to the above test, except that the initial response 6533 // this test is similar to the above test, except that the initial response
6534 // is cachable, but when it is validated, no-store is received causing the 6534 // is cachable, but when it is validated, no-store is received causing the
6535 // cached document to be deleted. 6535 // cached document to be deleted.
6536 MockHttpCache cache; 6536 MockHttpCache cache(false);
6537 6537
6538 ScopedMockTransaction transaction(kETagGET_Transaction); 6538 ScopedMockTransaction transaction(kETagGET_Transaction);
6539 6539
6540 // initial load 6540 // initial load
6541 RunTransactionTest(cache.http_cache(), transaction); 6541 RunTransactionTest(cache.http_cache(), transaction);
6542 6542
6543 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6543 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6544 EXPECT_EQ(0, cache.disk_cache()->open_count()); 6544 EXPECT_EQ(0, cache.disk_cache()->open_count());
6545 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6545 EXPECT_EQ(1, cache.disk_cache()->create_count());
6546 6546
6547 // try loading again; it should result in a network fetch 6547 // try loading again; it should result in a network fetch
6548 transaction.load_flags = LOAD_VALIDATE_CACHE; 6548 transaction.load_flags = LOAD_VALIDATE_CACHE;
6549 transaction.response_headers = "cache-control: no-store\n"; 6549 transaction.response_headers = "cache-control: no-store\n";
6550 RunTransactionTest(cache.http_cache(), transaction); 6550 RunTransactionTest(cache.http_cache(), transaction);
6551 6551
6552 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 6552 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6553 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6553 EXPECT_EQ(1, cache.disk_cache()->open_count());
6554 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6554 EXPECT_EQ(1, cache.disk_cache()->create_count());
6555 6555
6556 disk_cache::Entry* entry; 6556 disk_cache::Entry* entry;
6557 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); 6557 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry));
6558 } 6558 }
6559 6559
6560 TEST(HttpCache, CacheControlNoStore3) { 6560 TEST(HttpCache, CacheControlNoStore3) {
6561 // this test is similar to the above test, except that the response is a 304 6561 // this test is similar to the above test, except that the response is a 304
6562 // instead of a 200. this should never happen in practice, but it seems like 6562 // instead of a 200. this should never happen in practice, but it seems like
6563 // a good thing to verify that we still destroy the cache entry. 6563 // a good thing to verify that we still destroy the cache entry.
6564 MockHttpCache cache; 6564 MockHttpCache cache(false);
6565 6565
6566 ScopedMockTransaction transaction(kETagGET_Transaction); 6566 ScopedMockTransaction transaction(kETagGET_Transaction);
6567 6567
6568 // initial load 6568 // initial load
6569 RunTransactionTest(cache.http_cache(), transaction); 6569 RunTransactionTest(cache.http_cache(), transaction);
6570 6570
6571 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6571 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6572 EXPECT_EQ(0, cache.disk_cache()->open_count()); 6572 EXPECT_EQ(0, cache.disk_cache()->open_count());
6573 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6573 EXPECT_EQ(1, cache.disk_cache()->create_count());
6574 6574
6575 // try loading again; it should result in a network fetch 6575 // try loading again; it should result in a network fetch
6576 transaction.load_flags = LOAD_VALIDATE_CACHE; 6576 transaction.load_flags = LOAD_VALIDATE_CACHE;
6577 transaction.response_headers = "cache-control: no-store\n"; 6577 transaction.response_headers = "cache-control: no-store\n";
6578 transaction.status = "HTTP/1.1 304 Not Modified"; 6578 transaction.status = "HTTP/1.1 304 Not Modified";
6579 RunTransactionTest(cache.http_cache(), transaction); 6579 RunTransactionTest(cache.http_cache(), transaction);
6580 6580
6581 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 6581 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6582 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6582 EXPECT_EQ(1, cache.disk_cache()->open_count());
6583 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6583 EXPECT_EQ(1, cache.disk_cache()->create_count());
6584 6584
6585 disk_cache::Entry* entry; 6585 disk_cache::Entry* entry;
6586 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); 6586 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry));
6587 } 6587 }
6588 6588
6589 // Ensure that we don't cache requests served over bad HTTPS. 6589 // Ensure that we don't cache requests served over bad HTTPS.
6590 TEST(HttpCache, SimpleGET_SSLError) { 6590 TEST(HttpCache, SimpleGET_SSLError) {
6591 MockHttpCache cache; 6591 MockHttpCache cache(false);
6592 6592
6593 MockTransaction transaction = kSimpleGET_Transaction; 6593 MockTransaction transaction = kSimpleGET_Transaction;
6594 transaction.cert_status = CERT_STATUS_REVOKED; 6594 transaction.cert_status = CERT_STATUS_REVOKED;
6595 ScopedMockTransaction scoped_transaction(transaction); 6595 ScopedMockTransaction scoped_transaction(transaction);
6596 6596
6597 // write to the cache 6597 // write to the cache
6598 RunTransactionTest(cache.http_cache(), transaction); 6598 RunTransactionTest(cache.http_cache(), transaction);
6599 6599
6600 // Test that it was not cached. 6600 // Test that it was not cached.
6601 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 6601 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
6602 6602
6603 MockHttpRequest request(transaction); 6603 MockHttpRequest request(transaction);
6604 TestCompletionCallback callback; 6604 TestCompletionCallback callback;
6605 6605
6606 std::unique_ptr<HttpTransaction> trans; 6606 std::unique_ptr<HttpTransaction> trans;
6607 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 6607 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
6608 6608
6609 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 6609 int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
6610 if (rv == ERR_IO_PENDING) 6610 if (rv == ERR_IO_PENDING)
6611 rv = callback.WaitForResult(); 6611 rv = callback.WaitForResult();
6612 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); 6612 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS));
6613 } 6613 }
6614 6614
6615 // Ensure that we don't crash by if left-behind transactions. 6615 // Ensure that we don't crash by if left-behind transactions.
6616 TEST(HttpCache, OutlivedTransactions) { 6616 TEST(HttpCache, OutlivedTransactions) {
6617 MockHttpCache* cache = new MockHttpCache; 6617 MockHttpCache* cache = new MockHttpCache(false);
6618 6618
6619 std::unique_ptr<HttpTransaction> trans; 6619 std::unique_ptr<HttpTransaction> trans;
6620 EXPECT_THAT(cache->CreateTransaction(&trans), IsOk()); 6620 EXPECT_THAT(cache->CreateTransaction(&trans), IsOk());
6621 6621
6622 delete cache; 6622 delete cache;
6623 trans.reset(); 6623 trans.reset();
6624 } 6624 }
6625 6625
6626 // Test that the disabled mode works. 6626 // Test that the disabled mode works.
6627 TEST(HttpCache, CacheDisabledMode) { 6627 TEST(HttpCache, CacheDisabledMode) {
6628 MockHttpCache cache; 6628 MockHttpCache cache(false);
6629 6629
6630 // write to the cache 6630 // write to the cache
6631 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 6631 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
6632 6632
6633 // go into disabled mode 6633 // go into disabled mode
6634 cache.http_cache()->set_mode(HttpCache::DISABLE); 6634 cache.http_cache()->set_mode(HttpCache::DISABLE);
6635 6635
6636 // force this transaction to write to the cache again 6636 // force this transaction to write to the cache again
6637 MockTransaction transaction(kSimpleGET_Transaction); 6637 MockTransaction transaction(kSimpleGET_Transaction);
6638 6638
6639 RunTransactionTest(cache.http_cache(), transaction); 6639 RunTransactionTest(cache.http_cache(), transaction);
6640 6640
6641 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 6641 EXPECT_EQ(2, cache.network_layer()->transaction_count());
6642 EXPECT_EQ(0, cache.disk_cache()->open_count()); 6642 EXPECT_EQ(0, cache.disk_cache()->open_count());
6643 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6643 EXPECT_EQ(1, cache.disk_cache()->create_count());
6644 } 6644 }
6645 6645
6646 // Other tests check that the response headers of the cached response 6646 // Other tests check that the response headers of the cached response
6647 // get updated on 304. Here we specifically check that the 6647 // get updated on 304. Here we specifically check that the
6648 // HttpResponseHeaders::request_time and HttpResponseHeaders::response_time 6648 // HttpResponseHeaders::request_time and HttpResponseHeaders::response_time
6649 // fields also gets updated. 6649 // fields also gets updated.
6650 // http://crbug.com/20594. 6650 // http://crbug.com/20594.
6651 TEST(HttpCache, UpdatesRequestResponseTimeOn304) { 6651 TEST(HttpCache, UpdatesRequestResponseTimeOn304) {
6652 MockHttpCache cache; 6652 MockHttpCache cache(false);
6653 6653
6654 const char kUrl[] = "http://foobar"; 6654 const char kUrl[] = "http://foobar";
6655 const char kData[] = "body"; 6655 const char kData[] = "body";
6656 6656
6657 MockTransaction mock_network_response = { 0 }; 6657 MockTransaction mock_network_response = { 0 };
6658 mock_network_response.url = kUrl; 6658 mock_network_response.url = kUrl;
6659 6659
6660 AddMockTransaction(&mock_network_response); 6660 AddMockTransaction(&mock_network_response);
6661 6661
6662 // Request |kUrl|, causing |kNetResponse1| to be written to the cache. 6662 // Request |kUrl|, causing |kNetResponse1| to be written to the cache.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
6712 EXPECT_EQ("HTTP/1.1 200 OK\n" 6712 EXPECT_EQ("HTTP/1.1 200 OK\n"
6713 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" 6713 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
6714 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", 6714 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
6715 headers); 6715 headers);
6716 6716
6717 RemoveMockTransaction(&mock_network_response); 6717 RemoveMockTransaction(&mock_network_response);
6718 } 6718 }
6719 6719
6720 // Tests that we can write metadata to an entry. 6720 // Tests that we can write metadata to an entry.
6721 TEST(HttpCache, WriteMetadata_OK) { 6721 TEST(HttpCache, WriteMetadata_OK) {
6722 MockHttpCache cache; 6722 MockHttpCache cache(false);
6723 6723
6724 // Write to the cache 6724 // Write to the cache
6725 HttpResponseInfo response; 6725 HttpResponseInfo response;
6726 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, 6726 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
6727 &response); 6727 &response);
6728 EXPECT_TRUE(response.metadata.get() == NULL); 6728 EXPECT_TRUE(response.metadata.get() == NULL);
6729 6729
6730 // Trivial call. 6730 // Trivial call.
6731 cache.http_cache()->WriteMetadata(GURL("foo"), DEFAULT_PRIORITY, Time::Now(), 6731 cache.http_cache()->WriteMetadata(GURL("foo"), DEFAULT_PRIORITY, Time::Now(),
6732 NULL, 0); 6732 NULL, 0);
(...skipping 18 matching lines...) Expand all
6751 EXPECT_EQ(50, response.metadata->size()); 6751 EXPECT_EQ(50, response.metadata->size());
6752 EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there")); 6752 EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there"));
6753 6753
6754 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6754 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6755 EXPECT_EQ(2, cache.disk_cache()->open_count()); 6755 EXPECT_EQ(2, cache.disk_cache()->open_count());
6756 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6756 EXPECT_EQ(1, cache.disk_cache()->create_count());
6757 } 6757 }
6758 6758
6759 // Tests that we only write metadata to an entry if the time stamp matches. 6759 // Tests that we only write metadata to an entry if the time stamp matches.
6760 TEST(HttpCache, WriteMetadata_Fail) { 6760 TEST(HttpCache, WriteMetadata_Fail) {
6761 MockHttpCache cache; 6761 MockHttpCache cache(false);
6762 6762
6763 // Write to the cache 6763 // Write to the cache
6764 HttpResponseInfo response; 6764 HttpResponseInfo response;
6765 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, 6765 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
6766 &response); 6766 &response);
6767 EXPECT_TRUE(response.metadata.get() == NULL); 6767 EXPECT_TRUE(response.metadata.get() == NULL);
6768 6768
6769 // Attempt to write meta data to the same entry. 6769 // Attempt to write meta data to the same entry.
6770 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50)); 6770 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50));
6771 memset(buf->data(), 0, buf->size()); 6771 memset(buf->data(), 0, buf->size());
(...skipping 12 matching lines...) Expand all
6784 EXPECT_TRUE(response.metadata.get() == NULL); 6784 EXPECT_TRUE(response.metadata.get() == NULL);
6785 6785
6786 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6786 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6787 EXPECT_EQ(2, cache.disk_cache()->open_count()); 6787 EXPECT_EQ(2, cache.disk_cache()->open_count());
6788 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6788 EXPECT_EQ(1, cache.disk_cache()->create_count());
6789 } 6789 }
6790 6790
6791 // Tests that we ignore VARY checks when writing metadata since the request 6791 // Tests that we ignore VARY checks when writing metadata since the request
6792 // headers for the WriteMetadata transaction are made up. 6792 // headers for the WriteMetadata transaction are made up.
6793 TEST(HttpCache, WriteMetadata_IgnoreVary) { 6793 TEST(HttpCache, WriteMetadata_IgnoreVary) {
6794 MockHttpCache cache; 6794 MockHttpCache cache(false);
6795 6795
6796 // Write to the cache 6796 // Write to the cache
6797 HttpResponseInfo response; 6797 HttpResponseInfo response;
6798 ScopedMockTransaction transaction(kSimpleGET_Transaction); 6798 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6799 transaction.request_headers = "accept-encoding: gzip\r\n"; 6799 transaction.request_headers = "accept-encoding: gzip\r\n";
6800 transaction.response_headers = 6800 transaction.response_headers =
6801 "Vary: accept-encoding\n" 6801 "Vary: accept-encoding\n"
6802 "Cache-Control: max-age=10000\n"; 6802 "Cache-Control: max-age=10000\n";
6803 6803
6804 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, 6804 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
(...skipping 16 matching lines...) Expand all
6821 ASSERT_TRUE(response.metadata); 6821 ASSERT_TRUE(response.metadata);
6822 EXPECT_EQ(50, response.metadata->size()); 6822 EXPECT_EQ(50, response.metadata->size());
6823 EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there")); 6823 EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there"));
6824 6824
6825 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6825 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6826 EXPECT_EQ(2, cache.disk_cache()->open_count()); 6826 EXPECT_EQ(2, cache.disk_cache()->open_count());
6827 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6827 EXPECT_EQ(1, cache.disk_cache()->create_count());
6828 } 6828 }
6829 6829
6830 TEST(HttpCache, SkipVaryCheck) { 6830 TEST(HttpCache, SkipVaryCheck) {
6831 MockHttpCache cache; 6831 MockHttpCache cache(false);
6832 6832
6833 // Write a simple vary transaction to the cache. 6833 // Write a simple vary transaction to the cache.
6834 HttpResponseInfo response; 6834 HttpResponseInfo response;
6835 ScopedMockTransaction transaction(kSimpleGET_Transaction); 6835 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6836 transaction.request_headers = "accept-encoding: gzip\r\n"; 6836 transaction.request_headers = "accept-encoding: gzip\r\n";
6837 transaction.response_headers = 6837 transaction.response_headers =
6838 "Vary: accept-encoding\n" 6838 "Vary: accept-encoding\n"
6839 "Cache-Control: max-age=10000\n"; 6839 "Cache-Control: max-age=10000\n";
6840 RunTransactionTest(cache.http_cache(), transaction); 6840 RunTransactionTest(cache.http_cache(), transaction);
6841 6841
6842 // Change the request headers so that the request doesn't match due to vary. 6842 // Change the request headers so that the request doesn't match due to vary.
6843 // The request should fail. 6843 // The request should fail.
6844 transaction.load_flags = LOAD_ONLY_FROM_CACHE; 6844 transaction.load_flags = LOAD_ONLY_FROM_CACHE;
6845 transaction.request_headers = "accept-encoding: foo\r\n"; 6845 transaction.request_headers = "accept-encoding: foo\r\n";
6846 transaction.return_code = ERR_CACHE_MISS; 6846 transaction.return_code = ERR_CACHE_MISS;
6847 RunTransactionTest(cache.http_cache(), transaction); 6847 RunTransactionTest(cache.http_cache(), transaction);
6848 6848
6849 // Change the load flags to ignore vary checks, the request should now hit. 6849 // Change the load flags to ignore vary checks, the request should now hit.
6850 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_VARY_CHECK; 6850 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_VARY_CHECK;
6851 transaction.return_code = OK; 6851 transaction.return_code = OK;
6852 RunTransactionTest(cache.http_cache(), transaction); 6852 RunTransactionTest(cache.http_cache(), transaction);
6853 } 6853 }
6854 6854
6855 // Tests that we only return valid entries with LOAD_ONLY_FROM_CACHE 6855 // Tests that we only return valid entries with LOAD_ONLY_FROM_CACHE
6856 // transactions unless LOAD_SKIP_CACHE_VALIDATION is set. 6856 // transactions unless LOAD_SKIP_CACHE_VALIDATION is set.
6857 TEST(HttpCache, ValidLoadOnlyFromCache) { 6857 TEST(HttpCache, ValidLoadOnlyFromCache) {
6858 MockHttpCache cache; 6858 MockHttpCache cache(false);
6859 base::SimpleTestClock* clock = new base::SimpleTestClock(); 6859 base::SimpleTestClock* clock = new base::SimpleTestClock();
6860 cache.http_cache()->SetClockForTesting(base::WrapUnique(clock)); 6860 cache.http_cache()->SetClockForTesting(base::WrapUnique(clock));
6861 cache.network_layer()->SetClock(clock); 6861 cache.network_layer()->SetClock(clock);
6862 6862
6863 // Write a resource that will expire in 100 seconds. 6863 // Write a resource that will expire in 100 seconds.
6864 ScopedMockTransaction transaction(kSimpleGET_Transaction); 6864 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6865 transaction.response_headers = "Cache-Control: max-age=100\n"; 6865 transaction.response_headers = "Cache-Control: max-age=100\n";
6866 RunTransactionTest(cache.http_cache(), transaction); 6866 RunTransactionTest(cache.http_cache(), transaction);
6867 6867
6868 // Move forward in time such that the cached response is no longer valid. 6868 // Move forward in time such that the cached response is no longer valid.
6869 clock->Advance(base::TimeDelta::FromSeconds(101)); 6869 clock->Advance(base::TimeDelta::FromSeconds(101));
6870 6870
6871 // Skipping cache validation should still return a response. 6871 // Skipping cache validation should still return a response.
6872 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 6872 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
6873 RunTransactionTest(cache.http_cache(), transaction); 6873 RunTransactionTest(cache.http_cache(), transaction);
6874 6874
6875 // If the cache entry is checked for validitiy, it should fail. 6875 // If the cache entry is checked for validitiy, it should fail.
6876 transaction.load_flags = LOAD_ONLY_FROM_CACHE; 6876 transaction.load_flags = LOAD_ONLY_FROM_CACHE;
6877 transaction.return_code = ERR_CACHE_MISS; 6877 transaction.return_code = ERR_CACHE_MISS;
6878 RunTransactionTest(cache.http_cache(), transaction); 6878 RunTransactionTest(cache.http_cache(), transaction);
6879 } 6879 }
6880 6880
6881 TEST(HttpCache, InvalidLoadFlagCombination) { 6881 TEST(HttpCache, InvalidLoadFlagCombination) {
6882 MockHttpCache cache; 6882 MockHttpCache cache(false);
6883 6883
6884 // Put the resource in the cache. 6884 // Put the resource in the cache.
6885 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 6885 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
6886 6886
6887 // Now try to fetch it again, but with a flag combination disallowing both 6887 // Now try to fetch it again, but with a flag combination disallowing both
6888 // cache and network access. 6888 // cache and network access.
6889 ScopedMockTransaction transaction(kSimpleGET_Transaction); 6889 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6890 // DevTools relies on this combination of flags for "disable cache" mode 6890 // DevTools relies on this combination of flags for "disable cache" mode
6891 // when a resource is only supposed to be loaded from cache. 6891 // when a resource is only supposed to be loaded from cache.
6892 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_BYPASS_CACHE; 6892 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_BYPASS_CACHE;
6893 transaction.return_code = ERR_CACHE_MISS; 6893 transaction.return_code = ERR_CACHE_MISS;
6894 RunTransactionTest(cache.http_cache(), transaction); 6894 RunTransactionTest(cache.http_cache(), transaction);
6895 } 6895 }
6896 6896
6897 // Tests that we can read metadata after validating the entry and with READ mode 6897 // Tests that we can read metadata after validating the entry and with READ mode
6898 // transactions. 6898 // transactions.
6899 TEST(HttpCache, ReadMetadata) { 6899 TEST(HttpCache, ReadMetadata) {
6900 MockHttpCache cache; 6900 MockHttpCache cache(false);
6901 6901
6902 // Write to the cache 6902 // Write to the cache
6903 HttpResponseInfo response; 6903 HttpResponseInfo response;
6904 RunTransactionTestWithResponseInfo(cache.http_cache(), 6904 RunTransactionTestWithResponseInfo(cache.http_cache(),
6905 kTypicalGET_Transaction, &response); 6905 kTypicalGET_Transaction, &response);
6906 EXPECT_TRUE(response.metadata.get() == NULL); 6906 EXPECT_TRUE(response.metadata.get() == NULL);
6907 6907
6908 // Write meta data to the same entry. 6908 // Write meta data to the same entry.
6909 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50)); 6909 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50));
6910 memset(buf->data(), 0, buf->size()); 6910 memset(buf->data(), 0, buf->size());
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
6952 EXPECT_TRUE(response.metadata.get() == NULL); 6952 EXPECT_TRUE(response.metadata.get() == NULL);
6953 6953
6954 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 6954 EXPECT_EQ(3, cache.network_layer()->transaction_count());
6955 EXPECT_EQ(4, cache.disk_cache()->open_count()); 6955 EXPECT_EQ(4, cache.disk_cache()->open_count());
6956 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6956 EXPECT_EQ(1, cache.disk_cache()->create_count());
6957 } 6957 }
6958 6958
6959 // Tests that we don't mark entries as truncated when a filter detects the end 6959 // Tests that we don't mark entries as truncated when a filter detects the end
6960 // of the stream. 6960 // of the stream.
6961 TEST(HttpCache, FilterCompletion) { 6961 TEST(HttpCache, FilterCompletion) {
6962 MockHttpCache cache; 6962 MockHttpCache cache(false);
6963 TestCompletionCallback callback; 6963 TestCompletionCallback callback;
6964 6964
6965 { 6965 {
6966 std::unique_ptr<HttpTransaction> trans; 6966 std::unique_ptr<HttpTransaction> trans;
6967 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 6967 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
6968 6968
6969 MockHttpRequest request(kSimpleGET_Transaction); 6969 MockHttpRequest request(kSimpleGET_Transaction);
6970 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 6970 int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
6971 EXPECT_THAT(callback.GetResult(rv), IsOk()); 6971 EXPECT_THAT(callback.GetResult(rv), IsOk());
6972 6972
(...skipping 13 matching lines...) Expand all
6986 6986
6987 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6987 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6988 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6988 EXPECT_EQ(1, cache.disk_cache()->open_count());
6989 EXPECT_EQ(1, cache.disk_cache()->create_count()); 6989 EXPECT_EQ(1, cache.disk_cache()->create_count());
6990 } 6990 }
6991 6991
6992 // Tests that we don't mark entries as truncated and release the cache 6992 // Tests that we don't mark entries as truncated and release the cache
6993 // entry when DoneReading() is called before any Read() calls, such as 6993 // entry when DoneReading() is called before any Read() calls, such as
6994 // for a redirect. 6994 // for a redirect.
6995 TEST(HttpCache, DoneReading) { 6995 TEST(HttpCache, DoneReading) {
6996 MockHttpCache cache; 6996 MockHttpCache cache(false);
6997 TestCompletionCallback callback; 6997 TestCompletionCallback callback;
6998 6998
6999 ScopedMockTransaction transaction(kSimpleGET_Transaction); 6999 ScopedMockTransaction transaction(kSimpleGET_Transaction);
7000 transaction.data = ""; 7000 transaction.data = "";
7001 7001
7002 std::unique_ptr<HttpTransaction> trans; 7002 std::unique_ptr<HttpTransaction> trans;
7003 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 7003 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
7004 7004
7005 MockHttpRequest request(transaction); 7005 MockHttpRequest request(transaction);
7006 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 7006 int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
7007 EXPECT_THAT(callback.GetResult(rv), IsOk()); 7007 EXPECT_THAT(callback.GetResult(rv), IsOk());
7008 7008
7009 trans->DoneReading(); 7009 trans->DoneReading();
7010 // Leave the transaction around. 7010 // Leave the transaction around.
7011 7011
7012 // Make sure that the ActiveEntry is gone. 7012 // Make sure that the ActiveEntry is gone.
7013 base::RunLoop().RunUntilIdle(); 7013 base::RunLoop().RunUntilIdle();
7014 7014
7015 // Read from the cache. This should not deadlock. 7015 // Read from the cache. This should not deadlock.
7016 RunTransactionTest(cache.http_cache(), transaction); 7016 RunTransactionTest(cache.http_cache(), transaction);
7017 7017
7018 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 7018 EXPECT_EQ(1, cache.network_layer()->transaction_count());
7019 EXPECT_EQ(1, cache.disk_cache()->open_count()); 7019 EXPECT_EQ(1, cache.disk_cache()->open_count());
7020 EXPECT_EQ(1, cache.disk_cache()->create_count()); 7020 EXPECT_EQ(1, cache.disk_cache()->create_count());
7021 } 7021 }
7022 7022
7023 // Tests that we stop caching when told. 7023 // Tests that we stop caching when told.
7024 TEST(HttpCache, StopCachingDeletesEntry) { 7024 TEST(HttpCache, StopCachingDeletesEntry) {
7025 MockHttpCache cache; 7025 MockHttpCache cache(false);
7026 TestCompletionCallback callback; 7026 TestCompletionCallback callback;
7027 MockHttpRequest request(kSimpleGET_Transaction); 7027 MockHttpRequest request(kSimpleGET_Transaction);
7028 7028
7029 { 7029 {
7030 std::unique_ptr<HttpTransaction> trans; 7030 std::unique_ptr<HttpTransaction> trans;
7031 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 7031 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
7032 7032
7033 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 7033 int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
7034 EXPECT_THAT(callback.GetResult(rv), IsOk()); 7034 EXPECT_THAT(callback.GetResult(rv), IsOk());
7035 7035
(...skipping 17 matching lines...) Expand all
7053 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 7053 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
7054 7054
7055 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 7055 EXPECT_EQ(2, cache.network_layer()->transaction_count());
7056 EXPECT_EQ(0, cache.disk_cache()->open_count()); 7056 EXPECT_EQ(0, cache.disk_cache()->open_count());
7057 EXPECT_EQ(2, cache.disk_cache()->create_count()); 7057 EXPECT_EQ(2, cache.disk_cache()->create_count());
7058 } 7058 }
7059 7059
7060 // Tests that we stop caching when told, even if DoneReading is called 7060 // Tests that we stop caching when told, even if DoneReading is called
7061 // after StopCaching. 7061 // after StopCaching.
7062 TEST(HttpCache, StopCachingThenDoneReadingDeletesEntry) { 7062 TEST(HttpCache, StopCachingThenDoneReadingDeletesEntry) {
7063 MockHttpCache cache; 7063 MockHttpCache cache(false);
7064 TestCompletionCallback callback; 7064 TestCompletionCallback callback;
7065 MockHttpRequest request(kSimpleGET_Transaction); 7065 MockHttpRequest request(kSimpleGET_Transaction);
7066 7066
7067 { 7067 {
7068 std::unique_ptr<HttpTransaction> trans; 7068 std::unique_ptr<HttpTransaction> trans;
7069 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 7069 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
7070 7070
7071 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 7071 int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
7072 EXPECT_THAT(callback.GetResult(rv), IsOk()); 7072 EXPECT_THAT(callback.GetResult(rv), IsOk());
7073 7073
(...skipping 19 matching lines...) Expand all
7093 // Verify that the entry is gone. 7093 // Verify that the entry is gone.
7094 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 7094 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
7095 7095
7096 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 7096 EXPECT_EQ(2, cache.network_layer()->transaction_count());
7097 EXPECT_EQ(0, cache.disk_cache()->open_count()); 7097 EXPECT_EQ(0, cache.disk_cache()->open_count());
7098 EXPECT_EQ(2, cache.disk_cache()->create_count()); 7098 EXPECT_EQ(2, cache.disk_cache()->create_count());
7099 } 7099 }
7100 7100
7101 // Tests that we stop caching when told, when using auth. 7101 // Tests that we stop caching when told, when using auth.
7102 TEST(HttpCache, StopCachingWithAuthDeletesEntry) { 7102 TEST(HttpCache, StopCachingWithAuthDeletesEntry) {
7103 MockHttpCache cache; 7103 MockHttpCache cache(false);
7104 TestCompletionCallback callback; 7104 TestCompletionCallback callback;
7105 MockTransaction mock_transaction(kSimpleGET_Transaction); 7105 MockTransaction mock_transaction(kSimpleGET_Transaction);
7106 mock_transaction.status = "HTTP/1.1 401 Unauthorized"; 7106 mock_transaction.status = "HTTP/1.1 401 Unauthorized";
7107 AddMockTransaction(&mock_transaction); 7107 AddMockTransaction(&mock_transaction);
7108 MockHttpRequest request(mock_transaction); 7108 MockHttpRequest request(mock_transaction);
7109 7109
7110 { 7110 {
7111 std::unique_ptr<HttpTransaction> trans; 7111 std::unique_ptr<HttpTransaction> trans;
7112 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 7112 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
7113 7113
(...skipping 14 matching lines...) Expand all
7128 // Verify that the entry is gone. 7128 // Verify that the entry is gone.
7129 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 7129 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
7130 7130
7131 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 7131 EXPECT_EQ(2, cache.network_layer()->transaction_count());
7132 EXPECT_EQ(0, cache.disk_cache()->open_count()); 7132 EXPECT_EQ(0, cache.disk_cache()->open_count());
7133 EXPECT_EQ(2, cache.disk_cache()->create_count()); 7133 EXPECT_EQ(2, cache.disk_cache()->create_count());
7134 } 7134 }
7135 7135
7136 // Tests that when we are told to stop caching we don't throw away valid data. 7136 // Tests that when we are told to stop caching we don't throw away valid data.
7137 TEST(HttpCache, StopCachingSavesEntry) { 7137 TEST(HttpCache, StopCachingSavesEntry) {
7138 MockHttpCache cache; 7138 MockHttpCache cache(false);
7139 TestCompletionCallback callback; 7139 TestCompletionCallback callback;
7140 MockHttpRequest request(kSimpleGET_Transaction); 7140 MockHttpRequest request(kSimpleGET_Transaction);
7141 7141
7142 { 7142 {
7143 std::unique_ptr<HttpTransaction> trans; 7143 std::unique_ptr<HttpTransaction> trans;
7144 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 7144 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
7145 7145
7146 // Force a response that can be resumed. 7146 // Force a response that can be resumed.
7147 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction); 7147 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction);
7148 AddMockTransaction(&mock_transaction); 7148 AddMockTransaction(&mock_transaction);
(...skipping 16 matching lines...) Expand all
7165 rv = trans->Read(buf.get(), 256, callback.callback()); 7165 rv = trans->Read(buf.get(), 256, callback.callback());
7166 EXPECT_EQ(callback.GetResult(rv), 0); 7166 EXPECT_EQ(callback.GetResult(rv), 0);
7167 } 7167 }
7168 7168
7169 // Verify that the entry is marked as incomplete. 7169 // Verify that the entry is marked as incomplete.
7170 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); 7170 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0);
7171 } 7171 }
7172 7172
7173 // Tests that we handle truncated enries when StopCaching is called. 7173 // Tests that we handle truncated enries when StopCaching is called.
7174 TEST(HttpCache, StopCachingTruncatedEntry) { 7174 TEST(HttpCache, StopCachingTruncatedEntry) {
7175 MockHttpCache cache; 7175 MockHttpCache cache(false);
7176 TestCompletionCallback callback; 7176 TestCompletionCallback callback;
7177 MockHttpRequest request(kRangeGET_TransactionOK); 7177 MockHttpRequest request(kRangeGET_TransactionOK);
7178 request.extra_headers.Clear(); 7178 request.extra_headers.Clear();
7179 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); 7179 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE);
7180 AddMockTransaction(&kRangeGET_TransactionOK); 7180 AddMockTransaction(&kRangeGET_TransactionOK);
7181 7181
7182 std::string raw_headers("HTTP/1.1 200 OK\n" 7182 std::string raw_headers("HTTP/1.1 200 OK\n"
7183 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 7183 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
7184 "ETag: \"foo\"\n" 7184 "ETag: \"foo\"\n"
7185 "Accept-Ranges: bytes\n" 7185 "Accept-Ranges: bytes\n"
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
7415 // fetched via GET. Various combinations of cache state and when StopCaching() 7415 // fetched via GET. Various combinations of cache state and when StopCaching()
7416 // is called is controlled by the parameter passed into the test via the 7416 // is called is controlled by the parameter passed into the test via the
7417 // INSTANTIATE_TEST_CASE_P invocation above. 7417 // INSTANTIATE_TEST_CASE_P invocation above.
7418 TEST_P(HttpCacheHugeResourceTest, 7418 TEST_P(HttpCacheHugeResourceTest,
7419 StopCachingFollowedByReadForHugeTruncatedResource) { 7419 StopCachingFollowedByReadForHugeTruncatedResource) {
7420 // This test is going to be repeated for all combinations of TransactionPhase 7420 // This test is going to be repeated for all combinations of TransactionPhase
7421 // and CacheInitializers returned by GetTestModes(). 7421 // and CacheInitializers returned by GetTestModes().
7422 const TransactionPhase stop_caching_phase = GetParam().first; 7422 const TransactionPhase stop_caching_phase = GetParam().first;
7423 const CacheInitializer cache_initializer = GetParam().second; 7423 const CacheInitializer cache_initializer = GetParam().second;
7424 7424
7425 MockHttpCache cache; 7425 MockHttpCache cache(false);
7426 (*cache_initializer)(&cache); 7426 (*cache_initializer)(&cache);
7427 7427
7428 MockTransaction transaction(kSimpleGET_Transaction); 7428 MockTransaction transaction(kSimpleGET_Transaction);
7429 transaction.url = kRangeGET_TransactionOK.url; 7429 transaction.url = kRangeGET_TransactionOK.url;
7430 transaction.handler = &LargeResourceTransactionHandler; 7430 transaction.handler = &LargeResourceTransactionHandler;
7431 transaction.read_handler = &LargeBufferReader; 7431 transaction.read_handler = &LargeBufferReader;
7432 ScopedMockTransaction scoped_transaction(transaction); 7432 ScopedMockTransaction scoped_transaction(transaction);
7433 7433
7434 MockHttpRequest request(transaction); 7434 MockHttpRequest request(transaction);
7435 net::TestCompletionCallback callback; 7435 net::TestCompletionCallback callback;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
7482 7482
7483 // The only verification we are going to do is that the received resource has 7483 // The only verification we are going to do is that the received resource has
7484 // the correct size. This is sufficient to verify that the state machine 7484 // the correct size. This is sufficient to verify that the state machine
7485 // didn't terminate abruptly due to the StopCaching() call. 7485 // didn't terminate abruptly due to the StopCaching() call.
7486 EXPECT_EQ(kTotalSize, total_bytes_received); 7486 EXPECT_EQ(kTotalSize, total_bytes_received);
7487 } 7487 }
7488 7488
7489 // Tests that we detect truncated resources from the net when there is 7489 // Tests that we detect truncated resources from the net when there is
7490 // a Content-Length header. 7490 // a Content-Length header.
7491 TEST(HttpCache, TruncatedByContentLength) { 7491 TEST(HttpCache, TruncatedByContentLength) {
7492 MockHttpCache cache; 7492 MockHttpCache cache(false);
7493 TestCompletionCallback callback; 7493 TestCompletionCallback callback;
7494 7494
7495 MockTransaction transaction(kSimpleGET_Transaction); 7495 MockTransaction transaction(kSimpleGET_Transaction);
7496 AddMockTransaction(&transaction); 7496 AddMockTransaction(&transaction);
7497 transaction.response_headers = "Cache-Control: max-age=10000\n" 7497 transaction.response_headers = "Cache-Control: max-age=10000\n"
7498 "Content-Length: 100\n"; 7498 "Content-Length: 100\n";
7499 RunTransactionTest(cache.http_cache(), transaction); 7499 RunTransactionTest(cache.http_cache(), transaction);
7500 RemoveMockTransaction(&transaction); 7500 RemoveMockTransaction(&transaction);
7501 7501
7502 // Read from the cache. 7502 // Read from the cache.
7503 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 7503 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
7504 7504
7505 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 7505 EXPECT_EQ(2, cache.network_layer()->transaction_count());
7506 EXPECT_EQ(0, cache.disk_cache()->open_count()); 7506 EXPECT_EQ(0, cache.disk_cache()->open_count());
7507 EXPECT_EQ(2, cache.disk_cache()->create_count()); 7507 EXPECT_EQ(2, cache.disk_cache()->create_count());
7508 } 7508 }
7509 7509
7510 // Tests that we actually flag entries as truncated when we detect an error 7510 // Tests that we actually flag entries as truncated when we detect an error
7511 // from the net. 7511 // from the net.
7512 TEST(HttpCache, TruncatedByContentLength2) { 7512 TEST(HttpCache, TruncatedByContentLength2) {
7513 MockHttpCache cache; 7513 MockHttpCache cache(false);
7514 TestCompletionCallback callback; 7514 TestCompletionCallback callback;
7515 7515
7516 MockTransaction transaction(kSimpleGET_Transaction); 7516 MockTransaction transaction(kSimpleGET_Transaction);
7517 AddMockTransaction(&transaction); 7517 AddMockTransaction(&transaction);
7518 transaction.response_headers = "Cache-Control: max-age=10000\n" 7518 transaction.response_headers = "Cache-Control: max-age=10000\n"
7519 "Content-Length: 100\n" 7519 "Content-Length: 100\n"
7520 "Etag: \"foo\"\n"; 7520 "Etag: \"foo\"\n";
7521 RunTransactionTest(cache.http_cache(), transaction); 7521 RunTransactionTest(cache.http_cache(), transaction);
7522 RemoveMockTransaction(&transaction); 7522 RemoveMockTransaction(&transaction);
7523 7523
7524 // Verify that the entry is marked as incomplete. 7524 // Verify that the entry is marked as incomplete.
7525 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); 7525 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0);
7526 } 7526 }
7527 7527
7528 // Make sure that calling SetPriority on a cache transaction passes on 7528 // Make sure that calling SetPriority on a cache transaction passes on
7529 // its priority updates to its underlying network transaction. 7529 // its priority updates to its underlying network transaction.
7530 TEST(HttpCache, SetPriority) { 7530 TEST(HttpCache, SetPriority) {
7531 MockHttpCache cache; 7531 MockHttpCache cache(false);
7532 7532
7533 std::unique_ptr<HttpTransaction> trans; 7533 std::unique_ptr<HttpTransaction> trans;
7534 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &trans), IsOk()); 7534 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &trans), IsOk());
7535 7535
7536 // Shouldn't crash, but doesn't do anything either. 7536 // Shouldn't crash, but doesn't do anything either.
7537 trans->SetPriority(LOW); 7537 trans->SetPriority(LOW);
7538 7538
7539 EXPECT_FALSE(cache.network_layer()->last_transaction()); 7539 EXPECT_FALSE(cache.network_layer()->last_transaction());
7540 EXPECT_EQ(DEFAULT_PRIORITY, 7540 EXPECT_EQ(DEFAULT_PRIORITY,
7541 cache.network_layer()->last_create_transaction_priority()); 7541 cache.network_layer()->last_create_transaction_priority());
(...skipping 16 matching lines...) Expand all
7558 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority()); 7558 EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority());
7559 EXPECT_EQ(HIGHEST, cache.network_layer()->last_transaction()->priority()); 7559 EXPECT_EQ(HIGHEST, cache.network_layer()->last_transaction()->priority());
7560 } 7560 }
7561 7561
7562 EXPECT_THAT(callback.WaitForResult(), IsOk()); 7562 EXPECT_THAT(callback.WaitForResult(), IsOk());
7563 } 7563 }
7564 7564
7565 // Make sure that calling SetWebSocketHandshakeStreamCreateHelper on a cache 7565 // Make sure that calling SetWebSocketHandshakeStreamCreateHelper on a cache
7566 // transaction passes on its argument to the underlying network transaction. 7566 // transaction passes on its argument to the underlying network transaction.
7567 TEST(HttpCache, SetWebSocketHandshakeStreamCreateHelper) { 7567 TEST(HttpCache, SetWebSocketHandshakeStreamCreateHelper) {
7568 MockHttpCache cache; 7568 MockHttpCache cache(false);
7569 7569
7570 FakeWebSocketHandshakeStreamCreateHelper create_helper; 7570 FakeWebSocketHandshakeStreamCreateHelper create_helper;
7571 std::unique_ptr<HttpTransaction> trans; 7571 std::unique_ptr<HttpTransaction> trans;
7572 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &trans), IsOk()); 7572 ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &trans), IsOk());
7573 7573
7574 EXPECT_FALSE(cache.network_layer()->last_transaction()); 7574 EXPECT_FALSE(cache.network_layer()->last_transaction());
7575 7575
7576 HttpRequestInfo info; 7576 HttpRequestInfo info;
7577 info.url = GURL(kSimpleGET_Transaction.url); 7577 info.url = GURL(kSimpleGET_Transaction.url);
7578 TestCompletionCallback callback; 7578 TestCompletionCallback callback;
7579 EXPECT_EQ(ERR_IO_PENDING, 7579 EXPECT_EQ(ERR_IO_PENDING,
7580 trans->Start(&info, callback.callback(), NetLogWithSource())); 7580 trans->Start(&info, callback.callback(), NetLogWithSource()));
7581 7581
7582 ASSERT_TRUE(cache.network_layer()->last_transaction()); 7582 ASSERT_TRUE(cache.network_layer()->last_transaction());
7583 EXPECT_FALSE(cache.network_layer()->last_transaction()-> 7583 EXPECT_FALSE(cache.network_layer()->last_transaction()->
7584 websocket_handshake_stream_create_helper()); 7584 websocket_handshake_stream_create_helper());
7585 trans->SetWebSocketHandshakeStreamCreateHelper(&create_helper); 7585 trans->SetWebSocketHandshakeStreamCreateHelper(&create_helper);
7586 EXPECT_EQ(&create_helper, 7586 EXPECT_EQ(&create_helper,
7587 cache.network_layer()->last_transaction()-> 7587 cache.network_layer()->last_transaction()->
7588 websocket_handshake_stream_create_helper()); 7588 websocket_handshake_stream_create_helper());
7589 EXPECT_THAT(callback.WaitForResult(), IsOk()); 7589 EXPECT_THAT(callback.WaitForResult(), IsOk());
7590 } 7590 }
7591 7591
7592 // Make sure that a cache transaction passes on its priority to 7592 // Make sure that a cache transaction passes on its priority to
7593 // newly-created network transactions. 7593 // newly-created network transactions.
7594 TEST(HttpCache, SetPriorityNewTransaction) { 7594 TEST(HttpCache, SetPriorityNewTransaction) {
7595 MockHttpCache cache; 7595 MockHttpCache cache(false);
7596 AddMockTransaction(&kRangeGET_TransactionOK); 7596 AddMockTransaction(&kRangeGET_TransactionOK);
7597 7597
7598 std::string raw_headers("HTTP/1.1 200 OK\n" 7598 std::string raw_headers("HTTP/1.1 200 OK\n"
7599 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 7599 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
7600 "ETag: \"foo\"\n" 7600 "ETag: \"foo\"\n"
7601 "Accept-Ranges: bytes\n" 7601 "Accept-Ranges: bytes\n"
7602 "Content-Length: 80\n"); 7602 "Content-Length: 80\n");
7603 CreateTruncatedEntry(raw_headers, &cache); 7603 CreateTruncatedEntry(raw_headers, &cache);
7604 7604
7605 // Now make a regular request. 7605 // Now make a regular request.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
7638 int64_t* sent_bytes, 7638 int64_t* sent_bytes,
7639 int64_t* received_bytes) { 7639 int64_t* received_bytes) {
7640 RunTransactionTestBase( 7640 RunTransactionTestBase(
7641 cache.http_cache(), trans_info, MockHttpRequest(trans_info), nullptr, 7641 cache.http_cache(), trans_info, MockHttpRequest(trans_info), nullptr,
7642 NetLogWithSource(), nullptr, sent_bytes, received_bytes, nullptr); 7642 NetLogWithSource(), nullptr, sent_bytes, received_bytes, nullptr);
7643 } 7643 }
7644 7644
7645 } // namespace 7645 } // namespace
7646 7646
7647 TEST(HttpCache, NetworkBytesCacheMissAndThenHit) { 7647 TEST(HttpCache, NetworkBytesCacheMissAndThenHit) {
7648 MockHttpCache cache; 7648 MockHttpCache cache(false);
7649 7649
7650 MockTransaction transaction(kSimpleGET_Transaction); 7650 MockTransaction transaction(kSimpleGET_Transaction);
7651 int64_t sent, received; 7651 int64_t sent, received;
7652 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); 7652 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7653 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); 7653 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
7654 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); 7654 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
7655 7655
7656 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); 7656 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7657 EXPECT_EQ(0, sent); 7657 EXPECT_EQ(0, sent);
7658 EXPECT_EQ(0, received); 7658 EXPECT_EQ(0, received);
7659 } 7659 }
7660 7660
7661 TEST(HttpCache, NetworkBytesConditionalRequest304) { 7661 TEST(HttpCache, NetworkBytesConditionalRequest304) {
7662 MockHttpCache cache; 7662 MockHttpCache cache(false);
7663 7663
7664 ScopedMockTransaction transaction(kETagGET_Transaction); 7664 ScopedMockTransaction transaction(kETagGET_Transaction);
7665 int64_t sent, received; 7665 int64_t sent, received;
7666 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); 7666 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7667 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); 7667 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
7668 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); 7668 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
7669 7669
7670 transaction.load_flags = LOAD_VALIDATE_CACHE; 7670 transaction.load_flags = LOAD_VALIDATE_CACHE;
7671 transaction.handler = ETagGet_ConditionalRequest_Handler; 7671 transaction.handler = ETagGet_ConditionalRequest_Handler;
7672 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); 7672 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7673 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); 7673 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
7674 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); 7674 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
7675 } 7675 }
7676 7676
7677 TEST(HttpCache, NetworkBytesConditionalRequest200) { 7677 TEST(HttpCache, NetworkBytesConditionalRequest200) {
7678 MockHttpCache cache; 7678 MockHttpCache cache(false);
7679 7679
7680 MockTransaction transaction(kTypicalGET_Transaction); 7680 MockTransaction transaction(kTypicalGET_Transaction);
7681 transaction.request_headers = "Foo: bar\r\n"; 7681 transaction.request_headers = "Foo: bar\r\n";
7682 transaction.response_headers = 7682 transaction.response_headers =
7683 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" 7683 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
7684 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" 7684 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
7685 "Etag: \"foopy\"\n" 7685 "Etag: \"foopy\"\n"
7686 "Cache-Control: max-age=0\n" 7686 "Cache-Control: max-age=0\n"
7687 "Vary: Foo\n"; 7687 "Vary: Foo\n";
7688 AddMockTransaction(&transaction); 7688 AddMockTransaction(&transaction);
7689 int64_t sent, received; 7689 int64_t sent, received;
7690 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); 7690 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7691 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); 7691 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
7692 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); 7692 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
7693 7693
7694 RevalidationServer server; 7694 RevalidationServer server;
7695 transaction.handler = server.Handler; 7695 transaction.handler = server.Handler;
7696 transaction.request_headers = "Foo: none\r\n"; 7696 transaction.request_headers = "Foo: none\r\n";
7697 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); 7697 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7698 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); 7698 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
7699 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); 7699 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
7700 7700
7701 RemoveMockTransaction(&transaction); 7701 RemoveMockTransaction(&transaction);
7702 } 7702 }
7703 7703
7704 TEST(HttpCache, NetworkBytesRange) { 7704 TEST(HttpCache, NetworkBytesRange) {
7705 MockHttpCache cache; 7705 MockHttpCache cache(false);
7706 AddMockTransaction(&kRangeGET_TransactionOK); 7706 AddMockTransaction(&kRangeGET_TransactionOK);
7707 MockTransaction transaction(kRangeGET_TransactionOK); 7707 MockTransaction transaction(kRangeGET_TransactionOK);
7708 7708
7709 // Read bytes 40-49 from the network. 7709 // Read bytes 40-49 from the network.
7710 int64_t sent, received; 7710 int64_t sent, received;
7711 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received); 7711 RunTransactionAndGetNetworkBytes(cache, transaction, &sent, &received);
7712 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent); 7712 EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
7713 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received); 7713 EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
7714 7714
7715 // Read bytes 40-49 from the cache. 7715 // Read bytes 40-49 from the cache.
(...skipping 19 matching lines...) Expand all
7735 7735
7736 RemoveMockTransaction(&kRangeGET_TransactionOK); 7736 RemoveMockTransaction(&kRangeGET_TransactionOK);
7737 } 7737 }
7738 7738
7739 class HttpCachePrefetchValidationTest : public ::testing::Test { 7739 class HttpCachePrefetchValidationTest : public ::testing::Test {
7740 protected: 7740 protected:
7741 static const int kNumSecondsPerMinute = 60; 7741 static const int kNumSecondsPerMinute = 60;
7742 static const int kMaxAgeSecs = 100; 7742 static const int kMaxAgeSecs = 100;
7743 static const int kRequireValidationSecs = kMaxAgeSecs + 1; 7743 static const int kRequireValidationSecs = kMaxAgeSecs + 1;
7744 7744
7745 HttpCachePrefetchValidationTest() : transaction_(kSimpleGET_Transaction) { 7745 HttpCachePrefetchValidationTest()
7746 : cache_(false), transaction_(kSimpleGET_Transaction) {
7746 DCHECK_LT(kMaxAgeSecs, prefetch_reuse_mins() * kNumSecondsPerMinute); 7747 DCHECK_LT(kMaxAgeSecs, prefetch_reuse_mins() * kNumSecondsPerMinute);
7747 7748
7748 clock_ = new base::SimpleTestClock(); 7749 clock_ = new base::SimpleTestClock();
7749 cache_.http_cache()->SetClockForTesting(base::WrapUnique(clock_)); 7750 cache_.http_cache()->SetClockForTesting(base::WrapUnique(clock_));
7750 cache_.network_layer()->SetClock(clock_); 7751 cache_.network_layer()->SetClock(clock_);
7751 7752
7752 transaction_.response_headers = "Cache-Control: max-age=100\n"; 7753 transaction_.response_headers = "Cache-Control: max-age=100\n";
7753 } 7754 }
7754 7755
7755 bool TransactionRequiredNetwork(int load_flags) { 7756 bool TransactionRequiredNetwork(int load_flags) {
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
7852 std::string* response_headers, 7853 std::string* response_headers,
7853 std::string* response_data) { 7854 std::string* response_data) {
7854 std::string value; 7855 std::string value;
7855 EXPECT_TRUE(request->extra_headers.GetHeader("Resource-Freshness", &value)); 7856 EXPECT_TRUE(request->extra_headers.GetHeader("Resource-Freshness", &value));
7856 EXPECT_EQ("max-age=3600,stale-while-revalidate=7200,age=10801", value); 7857 EXPECT_EQ("max-age=3600,stale-while-revalidate=7200,age=10801", value);
7857 } 7858 }
7858 7859
7859 // Verify that the Resource-Freshness header is sent on a revalidation if the 7860 // Verify that the Resource-Freshness header is sent on a revalidation if the
7860 // stale-while-revalidate directive was on the response. 7861 // stale-while-revalidate directive was on the response.
7861 TEST(HttpCache, ResourceFreshnessHeaderSent) { 7862 TEST(HttpCache, ResourceFreshnessHeaderSent) {
7862 MockHttpCache cache; 7863 MockHttpCache cache(false);
7863 7864
7864 ScopedMockTransaction stale_while_revalidate_transaction( 7865 ScopedMockTransaction stale_while_revalidate_transaction(
7865 kSimpleGET_Transaction); 7866 kSimpleGET_Transaction);
7866 stale_while_revalidate_transaction.response_headers = 7867 stale_while_revalidate_transaction.response_headers =
7867 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 7868 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
7868 "Age: 10801\n" 7869 "Age: 10801\n"
7869 "Cache-Control: max-age=3600,stale-while-revalidate=7200\n"; 7870 "Cache-Control: max-age=3600,stale-while-revalidate=7200\n";
7870 7871
7871 // Write to the cache. 7872 // Write to the cache.
7872 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); 7873 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
(...skipping 11 matching lines...) Expand all
7884 static void CheckResourceFreshnessAbsent(const HttpRequestInfo* request, 7885 static void CheckResourceFreshnessAbsent(const HttpRequestInfo* request,
7885 std::string* response_status, 7886 std::string* response_status,
7886 std::string* response_headers, 7887 std::string* response_headers,
7887 std::string* response_data) { 7888 std::string* response_data) {
7888 EXPECT_FALSE(request->extra_headers.HasHeader("Resource-Freshness")); 7889 EXPECT_FALSE(request->extra_headers.HasHeader("Resource-Freshness"));
7889 } 7890 }
7890 7891
7891 // Verify that the Resource-Freshness header is not sent when 7892 // Verify that the Resource-Freshness header is not sent when
7892 // stale-while-revalidate is 0. 7893 // stale-while-revalidate is 0.
7893 TEST(HttpCache, ResourceFreshnessHeaderNotSent) { 7894 TEST(HttpCache, ResourceFreshnessHeaderNotSent) {
7894 MockHttpCache cache; 7895 MockHttpCache cache(false);
7895 7896
7896 ScopedMockTransaction stale_while_revalidate_transaction( 7897 ScopedMockTransaction stale_while_revalidate_transaction(
7897 kSimpleGET_Transaction); 7898 kSimpleGET_Transaction);
7898 stale_while_revalidate_transaction.response_headers = 7899 stale_while_revalidate_transaction.response_headers =
7899 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 7900 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
7900 "Age: 10801\n" 7901 "Age: 10801\n"
7901 "Cache-Control: max-age=3600,stale-while-revalidate=0\n"; 7902 "Cache-Control: max-age=3600,stale-while-revalidate=0\n";
7902 7903
7903 // Write to the cache. 7904 // Write to the cache.
7904 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); 7905 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
7905 7906
7906 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 7907 EXPECT_EQ(1, cache.network_layer()->transaction_count());
7907 7908
7908 // Send the request again and check that Resource-Freshness header is absent. 7909 // Send the request again and check that Resource-Freshness header is absent.
7909 stale_while_revalidate_transaction.handler = CheckResourceFreshnessAbsent; 7910 stale_while_revalidate_transaction.handler = CheckResourceFreshnessAbsent;
7910 7911
7911 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); 7912 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
7912 7913
7913 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 7914 EXPECT_EQ(2, cache.network_layer()->transaction_count());
7914 } 7915 }
7915 7916
7916 TEST(HttpCache, StaleContentNotUsedWhenLoadFlagNotSet) { 7917 TEST(HttpCache, StaleContentNotUsedWhenLoadFlagNotSet) {
7917 MockHttpCache cache; 7918 MockHttpCache cache(false);
7918 7919
7919 ScopedMockTransaction stale_while_revalidate_transaction( 7920 ScopedMockTransaction stale_while_revalidate_transaction(
7920 kSimpleGET_Transaction); 7921 kSimpleGET_Transaction);
7921 7922
7922 stale_while_revalidate_transaction.response_headers = 7923 stale_while_revalidate_transaction.response_headers =
7923 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 7924 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
7924 "Age: 10801\n" 7925 "Age: 10801\n"
7925 "Cache-Control: max-age=0,stale-while-revalidate=86400\n"; 7926 "Cache-Control: max-age=0,stale-while-revalidate=86400\n";
7926 7927
7927 // Write to the cache. 7928 // Write to the cache.
7928 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); 7929 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
7929 7930
7930 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 7931 EXPECT_EQ(1, cache.network_layer()->transaction_count());
7931 7932
7932 // Send the request again and check that it is sent to the network again. 7933 // Send the request again and check that it is sent to the network again.
7933 HttpResponseInfo response_info; 7934 HttpResponseInfo response_info;
7934 RunTransactionTestWithResponseInfo( 7935 RunTransactionTestWithResponseInfo(
7935 cache.http_cache(), stale_while_revalidate_transaction, &response_info); 7936 cache.http_cache(), stale_while_revalidate_transaction, &response_info);
7936 7937
7937 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 7938 EXPECT_EQ(2, cache.network_layer()->transaction_count());
7938 EXPECT_FALSE(response_info.async_revalidation_required); 7939 EXPECT_FALSE(response_info.async_revalidation_required);
7939 } 7940 }
7940 7941
7941 TEST(HttpCache, StaleContentUsedWhenLoadFlagSetAndUsable) { 7942 TEST(HttpCache, StaleContentUsedWhenLoadFlagSetAndUsable) {
7942 MockHttpCache cache; 7943 MockHttpCache cache(false);
7943 7944
7944 ScopedMockTransaction stale_while_revalidate_transaction( 7945 ScopedMockTransaction stale_while_revalidate_transaction(
7945 kSimpleGET_Transaction); 7946 kSimpleGET_Transaction);
7946 stale_while_revalidate_transaction.load_flags |= 7947 stale_while_revalidate_transaction.load_flags |=
7947 LOAD_SUPPORT_ASYNC_REVALIDATION; 7948 LOAD_SUPPORT_ASYNC_REVALIDATION;
7948 stale_while_revalidate_transaction.response_headers = 7949 stale_while_revalidate_transaction.response_headers =
7949 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 7950 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
7950 "Age: 10801\n" 7951 "Age: 10801\n"
7951 "Cache-Control: max-age=0,stale-while-revalidate=86400\n"; 7952 "Cache-Control: max-age=0,stale-while-revalidate=86400\n";
7952 7953
7953 // Write to the cache. 7954 // Write to the cache.
7954 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); 7955 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
7955 7956
7956 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 7957 EXPECT_EQ(1, cache.network_layer()->transaction_count());
7957 7958
7958 // Send the request again and check that it is not sent to the network again. 7959 // Send the request again and check that it is not sent to the network again.
7959 HttpResponseInfo response_info; 7960 HttpResponseInfo response_info;
7960 RunTransactionTestWithResponseInfo( 7961 RunTransactionTestWithResponseInfo(
7961 cache.http_cache(), stale_while_revalidate_transaction, &response_info); 7962 cache.http_cache(), stale_while_revalidate_transaction, &response_info);
7962 7963
7963 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 7964 EXPECT_EQ(1, cache.network_layer()->transaction_count());
7964 EXPECT_TRUE(response_info.async_revalidation_required); 7965 EXPECT_TRUE(response_info.async_revalidation_required);
7965 } 7966 }
7966 7967
7967 TEST(HttpCache, StaleContentNotUsedWhenUnusable) { 7968 TEST(HttpCache, StaleContentNotUsedWhenUnusable) {
7968 MockHttpCache cache; 7969 MockHttpCache cache(false);
7969 7970
7970 ScopedMockTransaction stale_while_revalidate_transaction( 7971 ScopedMockTransaction stale_while_revalidate_transaction(
7971 kSimpleGET_Transaction); 7972 kSimpleGET_Transaction);
7972 stale_while_revalidate_transaction.load_flags |= 7973 stale_while_revalidate_transaction.load_flags |=
7973 LOAD_SUPPORT_ASYNC_REVALIDATION; 7974 LOAD_SUPPORT_ASYNC_REVALIDATION;
7974 stale_while_revalidate_transaction.response_headers = 7975 stale_while_revalidate_transaction.response_headers =
7975 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 7976 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
7976 "Age: 10801\n" 7977 "Age: 10801\n"
7977 "Cache-Control: max-age=0,stale-while-revalidate=1800\n"; 7978 "Cache-Control: max-age=0,stale-while-revalidate=1800\n";
7978 7979
7979 // Write to the cache. 7980 // Write to the cache.
7980 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); 7981 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
7981 7982
7982 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 7983 EXPECT_EQ(1, cache.network_layer()->transaction_count());
7983 7984
7984 // Send the request again and check that it is sent to the network again. 7985 // Send the request again and check that it is sent to the network again.
7985 HttpResponseInfo response_info; 7986 HttpResponseInfo response_info;
7986 RunTransactionTestWithResponseInfo( 7987 RunTransactionTestWithResponseInfo(
7987 cache.http_cache(), stale_while_revalidate_transaction, &response_info); 7988 cache.http_cache(), stale_while_revalidate_transaction, &response_info);
7988 7989
7989 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 7990 EXPECT_EQ(2, cache.network_layer()->transaction_count());
7990 EXPECT_FALSE(response_info.async_revalidation_required); 7991 EXPECT_FALSE(response_info.async_revalidation_required);
7991 } 7992 }
7992 7993
7993 // Tests that we allow multiple simultaneous, non-overlapping transactions to 7994 // Tests that we allow multiple simultaneous, non-overlapping transactions to
7994 // take place on a sparse entry. 7995 // take place on a sparse entry.
7995 TEST(HttpCache, RangeGET_MultipleRequests) { 7996 TEST(HttpCache, RangeGET_MultipleRequests) {
7996 MockHttpCache cache; 7997 MockHttpCache cache(false);
7997 7998
7998 // Create a transaction for bytes 0-9. 7999 // Create a transaction for bytes 0-9.
7999 MockHttpRequest request(kRangeGET_TransactionOK); 8000 MockHttpRequest request(kRangeGET_TransactionOK);
8000 MockTransaction transaction(kRangeGET_TransactionOK); 8001 MockTransaction transaction(kRangeGET_TransactionOK);
8001 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; 8002 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
8002 transaction.data = "rg: 00-09 "; 8003 transaction.data = "rg: 00-09 ";
8003 AddMockTransaction(&transaction); 8004 AddMockTransaction(&transaction);
8004 8005
8005 TestCompletionCallback callback; 8006 TestCompletionCallback callback;
8006 std::unique_ptr<HttpTransaction> trans; 8007 std::unique_ptr<HttpTransaction> trans;
(...skipping 13 matching lines...) Expand all
8020 callback.WaitForResult(); 8021 callback.WaitForResult();
8021 8022
8022 RemoveMockTransaction(&transaction); 8023 RemoveMockTransaction(&transaction);
8023 } 8024 }
8024 8025
8025 // Makes sure that a request stops using the cache when the response headers 8026 // Makes sure that a request stops using the cache when the response headers
8026 // with "Cache-Control: no-store" arrives. That means that another request for 8027 // with "Cache-Control: no-store" arrives. That means that another request for
8027 // the same URL can be processed before the response body of the original 8028 // the same URL can be processed before the response body of the original
8028 // request arrives. 8029 // request arrives.
8029 TEST(HttpCache, NoStoreResponseShouldNotBlockFollowingRequests) { 8030 TEST(HttpCache, NoStoreResponseShouldNotBlockFollowingRequests) {
8030 MockHttpCache cache; 8031 MockHttpCache cache(false);
8031 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction); 8032 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction);
8032 mock_transaction.response_headers = "Cache-Control: no-store\n"; 8033 mock_transaction.response_headers = "Cache-Control: no-store\n";
8033 MockHttpRequest request(mock_transaction); 8034 MockHttpRequest request(mock_transaction);
8034 8035
8035 std::unique_ptr<Context> first(new Context); 8036 std::unique_ptr<Context> first(new Context);
8036 first->result = cache.CreateTransaction(&first->trans); 8037 first->result = cache.CreateTransaction(&first->trans);
8037 ASSERT_THAT(first->result, IsOk()); 8038 ASSERT_THAT(first->result, IsOk());
8038 EXPECT_EQ(LOAD_STATE_IDLE, first->trans->GetLoadState()); 8039 EXPECT_EQ(LOAD_STATE_IDLE, first->trans->GetLoadState());
8039 first->result = first->trans->Start(&request, first->callback.callback(), 8040 first->result = first->trans->Start(&request, first->callback.callback(),
8040 NetLogWithSource()); 8041 NetLogWithSource());
(...skipping 29 matching lines...) Expand all
8070 TEST(HttpCache, CachePreservesSSLInfo) { 8071 TEST(HttpCache, CachePreservesSSLInfo) {
8071 static const uint16_t kTLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0xc02f; 8072 static const uint16_t kTLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0xc02f;
8072 int status = 0; 8073 int status = 0;
8073 SSLConnectionStatusSetCipherSuite(kTLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 8074 SSLConnectionStatusSetCipherSuite(kTLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
8074 &status); 8075 &status);
8075 SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2, &status); 8076 SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2, &status);
8076 8077
8077 scoped_refptr<X509Certificate> cert = 8078 scoped_refptr<X509Certificate> cert =
8078 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem"); 8079 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
8079 8080
8080 MockHttpCache cache; 8081 MockHttpCache cache(false);
8081 8082
8082 ScopedMockTransaction transaction(kSimpleGET_Transaction); 8083 ScopedMockTransaction transaction(kSimpleGET_Transaction);
8083 transaction.cert = cert; 8084 transaction.cert = cert;
8084 transaction.ssl_connection_status = status; 8085 transaction.ssl_connection_status = status;
8085 8086
8086 // Fetch the resource. 8087 // Fetch the resource.
8087 HttpResponseInfo response_info; 8088 HttpResponseInfo response_info;
8088 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, 8089 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
8089 &response_info); 8090 &response_info);
8090 8091
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
8123 int status2 = 0; 8124 int status2 = 0;
8124 SSLConnectionStatusSetCipherSuite(kTLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 8125 SSLConnectionStatusSetCipherSuite(kTLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
8125 &status2); 8126 &status2);
8126 SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2, &status2); 8127 SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2, &status2);
8127 8128
8128 scoped_refptr<X509Certificate> cert1 = 8129 scoped_refptr<X509Certificate> cert1 =
8129 ImportCertFromFile(GetTestCertsDirectory(), "expired_cert.pem"); 8130 ImportCertFromFile(GetTestCertsDirectory(), "expired_cert.pem");
8130 scoped_refptr<X509Certificate> cert2 = 8131 scoped_refptr<X509Certificate> cert2 =
8131 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem"); 8132 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
8132 8133
8133 MockHttpCache cache; 8134 MockHttpCache cache(false);
8134 8135
8135 ScopedMockTransaction transaction(kTypicalGET_Transaction); 8136 ScopedMockTransaction transaction(kTypicalGET_Transaction);
8136 transaction.cert = cert1; 8137 transaction.cert = cert1;
8137 transaction.ssl_connection_status = status1; 8138 transaction.ssl_connection_status = status1;
8138 8139
8139 // Fetch the resource. 8140 // Fetch the resource.
8140 HttpResponseInfo response_info; 8141 HttpResponseInfo response_info;
8141 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, 8142 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
8142 &response_info); 8143 &response_info);
8143 8144
(...skipping 23 matching lines...) Expand all
8167 EXPECT_EQ(1, cache.disk_cache()->open_count()); 8168 EXPECT_EQ(1, cache.disk_cache()->open_count());
8168 EXPECT_EQ(1, cache.disk_cache()->create_count()); 8169 EXPECT_EQ(1, cache.disk_cache()->create_count());
8169 EXPECT_TRUE(response_info.was_cached); 8170 EXPECT_TRUE(response_info.was_cached);
8170 8171
8171 // The new SSL state is reported. 8172 // The new SSL state is reported.
8172 EXPECT_EQ(status2, response_info.ssl_info.connection_status); 8173 EXPECT_EQ(status2, response_info.ssl_info.connection_status);
8173 EXPECT_TRUE(cert2->Equals(response_info.ssl_info.cert.get())); 8174 EXPECT_TRUE(cert2->Equals(response_info.ssl_info.cert.get()));
8174 } 8175 }
8175 8176
8176 TEST(HttpCache, CacheEntryStatusOther) { 8177 TEST(HttpCache, CacheEntryStatusOther) {
8177 MockHttpCache cache; 8178 MockHttpCache cache(false);
8178 8179
8179 HttpResponseInfo response_info; 8180 HttpResponseInfo response_info;
8180 RunTransactionTestWithResponseInfo(cache.http_cache(), kRangeGET_Transaction, 8181 RunTransactionTestWithResponseInfo(cache.http_cache(), kRangeGET_Transaction,
8181 &response_info); 8182 &response_info);
8182 8183
8183 EXPECT_FALSE(response_info.was_cached); 8184 EXPECT_FALSE(response_info.was_cached);
8184 EXPECT_TRUE(response_info.network_accessed); 8185 EXPECT_TRUE(response_info.network_accessed);
8185 EXPECT_EQ(CacheEntryStatus::ENTRY_OTHER, response_info.cache_entry_status); 8186 EXPECT_EQ(CacheEntryStatus::ENTRY_OTHER, response_info.cache_entry_status);
8186 } 8187 }
8187 8188
8188 TEST(HttpCache, CacheEntryStatusNotInCache) { 8189 TEST(HttpCache, CacheEntryStatusNotInCache) {
8189 MockHttpCache cache; 8190 MockHttpCache cache(false);
8190 8191
8191 HttpResponseInfo response_info; 8192 HttpResponseInfo response_info;
8192 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, 8193 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
8193 &response_info); 8194 &response_info);
8194 8195
8195 EXPECT_FALSE(response_info.was_cached); 8196 EXPECT_FALSE(response_info.was_cached);
8196 EXPECT_TRUE(response_info.network_accessed); 8197 EXPECT_TRUE(response_info.network_accessed);
8197 EXPECT_EQ(CacheEntryStatus::ENTRY_NOT_IN_CACHE, 8198 EXPECT_EQ(CacheEntryStatus::ENTRY_NOT_IN_CACHE,
8198 response_info.cache_entry_status); 8199 response_info.cache_entry_status);
8199 } 8200 }
8200 8201
8201 TEST(HttpCache, CacheEntryStatusUsed) { 8202 TEST(HttpCache, CacheEntryStatusUsed) {
8202 MockHttpCache cache; 8203 MockHttpCache cache(false);
8203 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 8204 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
8204 8205
8205 HttpResponseInfo response_info; 8206 HttpResponseInfo response_info;
8206 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, 8207 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
8207 &response_info); 8208 &response_info);
8208 8209
8209 EXPECT_TRUE(response_info.was_cached); 8210 EXPECT_TRUE(response_info.was_cached);
8210 EXPECT_FALSE(response_info.network_accessed); 8211 EXPECT_FALSE(response_info.network_accessed);
8211 EXPECT_EQ(CacheEntryStatus::ENTRY_USED, response_info.cache_entry_status); 8212 EXPECT_EQ(CacheEntryStatus::ENTRY_USED, response_info.cache_entry_status);
8212 } 8213 }
8213 8214
8214 TEST(HttpCache, CacheEntryStatusValidated) { 8215 TEST(HttpCache, CacheEntryStatusValidated) {
8215 MockHttpCache cache; 8216 MockHttpCache cache(false);
8216 RunTransactionTest(cache.http_cache(), kETagGET_Transaction); 8217 RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
8217 8218
8218 ScopedMockTransaction still_valid(kETagGET_Transaction); 8219 ScopedMockTransaction still_valid(kETagGET_Transaction);
8219 still_valid.load_flags = LOAD_VALIDATE_CACHE; // Force a validation. 8220 still_valid.load_flags = LOAD_VALIDATE_CACHE; // Force a validation.
8220 still_valid.handler = ETagGet_ConditionalRequest_Handler; 8221 still_valid.handler = ETagGet_ConditionalRequest_Handler;
8221 8222
8222 HttpResponseInfo response_info; 8223 HttpResponseInfo response_info;
8223 RunTransactionTestWithResponseInfo(cache.http_cache(), still_valid, 8224 RunTransactionTestWithResponseInfo(cache.http_cache(), still_valid,
8224 &response_info); 8225 &response_info);
8225 8226
8226 EXPECT_TRUE(response_info.was_cached); 8227 EXPECT_TRUE(response_info.was_cached);
8227 EXPECT_TRUE(response_info.network_accessed); 8228 EXPECT_TRUE(response_info.network_accessed);
8228 EXPECT_EQ(CacheEntryStatus::ENTRY_VALIDATED, 8229 EXPECT_EQ(CacheEntryStatus::ENTRY_VALIDATED,
8229 response_info.cache_entry_status); 8230 response_info.cache_entry_status);
8230 } 8231 }
8231 8232
8232 TEST(HttpCache, CacheEntryStatusUpdated) { 8233 TEST(HttpCache, CacheEntryStatusUpdated) {
8233 MockHttpCache cache; 8234 MockHttpCache cache(false);
8234 RunTransactionTest(cache.http_cache(), kETagGET_Transaction); 8235 RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
8235 8236
8236 ScopedMockTransaction update(kETagGET_Transaction); 8237 ScopedMockTransaction update(kETagGET_Transaction);
8237 update.load_flags = LOAD_VALIDATE_CACHE; // Force a validation. 8238 update.load_flags = LOAD_VALIDATE_CACHE; // Force a validation.
8238 8239
8239 HttpResponseInfo response_info; 8240 HttpResponseInfo response_info;
8240 RunTransactionTestWithResponseInfo(cache.http_cache(), update, 8241 RunTransactionTestWithResponseInfo(cache.http_cache(), update,
8241 &response_info); 8242 &response_info);
8242 8243
8243 EXPECT_FALSE(response_info.was_cached); 8244 EXPECT_FALSE(response_info.was_cached);
8244 EXPECT_TRUE(response_info.network_accessed); 8245 EXPECT_TRUE(response_info.network_accessed);
8245 EXPECT_EQ(CacheEntryStatus::ENTRY_UPDATED, response_info.cache_entry_status); 8246 EXPECT_EQ(CacheEntryStatus::ENTRY_UPDATED, response_info.cache_entry_status);
8246 } 8247 }
8247 8248
8248 TEST(HttpCache, CacheEntryStatusCantConditionalize) { 8249 TEST(HttpCache, CacheEntryStatusCantConditionalize) {
8249 MockHttpCache cache; 8250 MockHttpCache cache(false);
8250 cache.FailConditionalizations(); 8251 cache.FailConditionalizations();
8251 RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction); 8252 RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction);
8252 8253
8253 HttpResponseInfo response_info; 8254 HttpResponseInfo response_info;
8254 RunTransactionTestWithResponseInfo(cache.http_cache(), 8255 RunTransactionTestWithResponseInfo(cache.http_cache(),
8255 kTypicalGET_Transaction, &response_info); 8256 kTypicalGET_Transaction, &response_info);
8256 8257
8257 EXPECT_FALSE(response_info.was_cached); 8258 EXPECT_FALSE(response_info.was_cached);
8258 EXPECT_TRUE(response_info.network_accessed); 8259 EXPECT_TRUE(response_info.network_accessed);
8259 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, 8260 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE,
8260 response_info.cache_entry_status); 8261 response_info.cache_entry_status);
8261 } 8262 }
8262 8263
8263 } // namespace net 8264 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698