Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/http/http_cache.h" | 5 #include "net/http/http_cache.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 5617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 5628 | 5628 |
| 5629 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 5629 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 5630 | 5630 |
| 5631 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 5631 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5632 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 5632 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5633 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 5633 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5634 | 5634 |
| 5635 RemoveMockTransaction(&transaction); | 5635 RemoveMockTransaction(&transaction); |
| 5636 } | 5636 } |
| 5637 | 5637 |
| 5638 namespace { | |
| 5639 | |
| 5640 // Verifies that there's an entry with this |key| with the truncated flag set to | |
| 5641 // |flag_value|, and with an optional |data_size| (if not zero). | |
| 5642 void VerifyTruncatedFlag(MockHttpCache* cache, | |
|
asanka
2015/09/17 14:43:34
Nit: I'd move this to the top since it's not speci
rvargas (doing something else)
2015/09/17 20:18:37
I only placed it here 'cause I saw you doing that
asanka
2015/09/17 22:26:00
Hehe. I caught one of these strays in my CL and mo
| |
| 5643 const std::string& key, | |
| 5644 bool flag_value, | |
| 5645 int data_size) { | |
| 5646 disk_cache::Entry* entry; | |
| 5647 ASSERT_TRUE(cache->OpenBackendEntry(key, &entry)); | |
| 5648 disk_cache::ScopedEntryPtr closer(entry); | |
| 5649 | |
| 5650 HttpResponseInfo response; | |
| 5651 bool truncated = !flag_value; | |
| 5652 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 5653 EXPECT_EQ(flag_value, truncated); | |
| 5654 if (data_size) | |
| 5655 EXPECT_EQ(data_size, entry->GetDataSize(1)); | |
| 5656 } | |
| 5657 | |
| 5658 } // namespace | |
| 5659 | |
| 5638 // Tests that we mark an entry as incomplete when the request is cancelled. | 5660 // Tests that we mark an entry as incomplete when the request is cancelled. |
| 5639 TEST(HttpCache, SetTruncatedFlag) { | 5661 TEST(HttpCache, SetTruncatedFlag) { |
| 5640 MockHttpCache cache; | 5662 MockHttpCache cache; |
| 5641 | 5663 |
| 5642 MockTransaction transaction(kSimpleGET_Transaction); | 5664 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 5643 transaction.response_headers = | 5665 transaction.response_headers = |
| 5644 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | 5666 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 5645 "Content-Length: 22\n" | 5667 "Content-Length: 22\n" |
| 5646 "Etag: \"foopy\"\n"; | 5668 "Etag: \"foopy\"\n"; |
| 5647 AddMockTransaction(&transaction); | |
| 5648 MockHttpRequest request(transaction); | 5669 MockHttpRequest request(transaction); |
| 5649 | 5670 |
| 5650 scoped_ptr<Context> c(new Context()); | 5671 scoped_ptr<Context> c(new Context()); |
| 5651 | 5672 |
| 5652 int rv = cache.CreateTransaction(&c->trans); | 5673 int rv = cache.CreateTransaction(&c->trans); |
| 5653 ASSERT_EQ(OK, rv); | 5674 ASSERT_EQ(OK, rv); |
| 5654 | 5675 |
| 5655 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); | 5676 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 5656 if (rv == ERR_IO_PENDING) | 5677 if (rv == ERR_IO_PENDING) |
| 5657 rv = c->callback.WaitForResult(); | 5678 rv = c->callback.WaitForResult(); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 5679 MockHttpCache::SetTestMode(0); | 5700 MockHttpCache::SetTestMode(0); |
| 5680 | 5701 |
| 5681 | 5702 |
| 5682 // Make sure that we don't invoke the callback. We may have an issue if the | 5703 // Make sure that we don't invoke the callback. We may have an issue if the |
| 5683 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we | 5704 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we |
| 5684 // could end up with the transaction being deleted twice if we send any | 5705 // could end up with the transaction being deleted twice if we send any |
| 5685 // notification from the transaction destructor (see http://crbug.com/31723). | 5706 // notification from the transaction destructor (see http://crbug.com/31723). |
| 5686 EXPECT_FALSE(c->callback.have_result()); | 5707 EXPECT_FALSE(c->callback.have_result()); |
| 5687 | 5708 |
| 5688 // Verify that the entry is marked as incomplete. | 5709 // Verify that the entry is marked as incomplete. |
| 5689 disk_cache::Entry* entry; | 5710 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); |
| 5690 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | |
| 5691 HttpResponseInfo response; | |
| 5692 bool truncated = false; | |
| 5693 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 5694 EXPECT_TRUE(truncated); | |
| 5695 entry->Close(); | |
| 5696 | |
| 5697 RemoveMockTransaction(&transaction); | |
| 5698 } | 5711 } |
| 5699 | 5712 |
| 5700 // Tests that we don't mark an entry as truncated when we read everything. | 5713 // Tests that we don't mark an entry as truncated when we read everything. |
| 5701 TEST(HttpCache, DontSetTruncatedFlag) { | 5714 TEST(HttpCache, DontSetTruncatedFlag) { |
| 5702 MockHttpCache cache; | 5715 MockHttpCache cache; |
| 5703 | 5716 |
| 5704 MockTransaction transaction(kSimpleGET_Transaction); | 5717 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 5705 transaction.response_headers = | 5718 transaction.response_headers = |
| 5706 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | 5719 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 5707 "Content-Length: 22\n" | 5720 "Content-Length: 22\n" |
| 5708 "Etag: \"foopy\"\n"; | 5721 "Etag: \"foopy\"\n"; |
| 5709 AddMockTransaction(&transaction); | |
| 5710 MockHttpRequest request(transaction); | 5722 MockHttpRequest request(transaction); |
| 5711 | 5723 |
| 5712 scoped_ptr<Context> c(new Context()); | 5724 scoped_ptr<Context> c(new Context()); |
| 5713 int rv = cache.CreateTransaction(&c->trans); | 5725 int rv = cache.CreateTransaction(&c->trans); |
| 5714 ASSERT_EQ(OK, rv); | 5726 ASSERT_EQ(OK, rv); |
| 5715 | 5727 |
| 5716 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); | 5728 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 5717 EXPECT_EQ(OK, c->callback.GetResult(rv)); | 5729 EXPECT_EQ(OK, c->callback.GetResult(rv)); |
| 5718 | 5730 |
| 5719 // Read everything. | 5731 // Read everything. |
| 5720 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(22)); | 5732 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(22)); |
| 5721 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | 5733 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
| 5722 EXPECT_EQ(buf->size(), c->callback.GetResult(rv)); | 5734 EXPECT_EQ(buf->size(), c->callback.GetResult(rv)); |
| 5723 | 5735 |
| 5724 // Destroy the transaction. | 5736 // Destroy the transaction. |
| 5725 c->trans.reset(); | 5737 c->trans.reset(); |
| 5726 | 5738 |
| 5727 // Verify that the entry is not marked as truncated. | 5739 // Verify that the entry is not marked as truncated. |
| 5728 disk_cache::Entry* entry; | 5740 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, false, 0); |
| 5729 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | 5741 } |
| 5730 HttpResponseInfo response; | |
| 5731 bool truncated = true; | |
| 5732 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 5733 EXPECT_FALSE(truncated); | |
| 5734 entry->Close(); | |
| 5735 | 5742 |
| 5736 RemoveMockTransaction(&transaction); | 5743 // Tests that sparse entries don't set the truncate flag. |
| 5744 TEST(HttpCache, RangeGET_DontTruncate) { | |
| 5745 MockHttpCache cache; | |
| 5746 | |
| 5747 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
| 5748 transaction.request_headers = "Range: bytes = 0-19\r\n" EXTRA_HEADER; | |
| 5749 | |
| 5750 scoped_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); | |
| 5751 scoped_ptr<HttpTransaction> trans; | |
| 5752 | |
| 5753 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); | |
| 5754 EXPECT_EQ(OK, rv); | |
| 5755 | |
| 5756 TestCompletionCallback cb; | |
| 5757 rv = trans->Start(request.get(), cb.callback(), BoundNetLog()); | |
| 5758 EXPECT_EQ(0, cb.GetResult(rv)); | |
| 5759 | |
| 5760 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); | |
| 5761 rv = trans->Read(buf.get(), 10, cb.callback()); | |
| 5762 EXPECT_EQ(10, cb.GetResult(rv)); | |
| 5763 | |
| 5764 // Should not trigger any DCHECK. | |
| 5765 trans.reset(); | |
| 5766 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); | |
| 5767 } | |
| 5768 | |
| 5769 // Tests that sparse entries don't set the truncate flag (when the byte range | |
| 5770 // starts after 0). | |
| 5771 TEST(HttpCache, RangeGET_DontTruncate2) { | |
| 5772 MockHttpCache cache; | |
| 5773 | |
| 5774 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
| 5775 transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER; | |
| 5776 | |
| 5777 scoped_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); | |
| 5778 scoped_ptr<HttpTransaction> trans; | |
| 5779 | |
| 5780 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); | |
| 5781 EXPECT_EQ(OK, rv); | |
| 5782 | |
| 5783 TestCompletionCallback cb; | |
| 5784 rv = trans->Start(request.get(), cb.callback(), BoundNetLog()); | |
| 5785 EXPECT_EQ(0, cb.GetResult(rv)); | |
| 5786 | |
| 5787 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); | |
| 5788 rv = trans->Read(buf.get(), 10, cb.callback()); | |
| 5789 EXPECT_EQ(10, cb.GetResult(rv)); | |
| 5790 | |
| 5791 // Should not trigger any DCHECK. | |
| 5792 trans.reset(); | |
| 5793 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); | |
| 5737 } | 5794 } |
| 5738 | 5795 |
| 5739 // Tests that we can continue with a request that was interrupted. | 5796 // Tests that we can continue with a request that was interrupted. |
| 5740 TEST(HttpCache, GET_IncompleteResource) { | 5797 TEST(HttpCache, GET_IncompleteResource) { |
| 5741 MockHttpCache cache; | 5798 MockHttpCache cache; |
| 5742 AddMockTransaction(&kRangeGET_TransactionOK); | 5799 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 5743 | 5800 |
| 5744 std::string raw_headers("HTTP/1.1 200 OK\n" | 5801 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 5745 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 5802 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 5746 "ETag: \"foo\"\n" | 5803 "ETag: \"foo\"\n" |
| 5747 "Accept-Ranges: bytes\n" | 5804 "Accept-Ranges: bytes\n" |
| 5748 "Content-Length: 80\n"); | 5805 "Content-Length: 80\n"); |
| 5749 CreateTruncatedEntry(raw_headers, &cache); | 5806 CreateTruncatedEntry(raw_headers, &cache); |
| 5750 | 5807 |
| 5751 // Now make a regular request. | 5808 // Now make a regular request. |
| 5752 std::string headers; | 5809 std::string headers; |
| 5753 MockTransaction transaction(kRangeGET_TransactionOK); | |
| 5754 transaction.request_headers = EXTRA_HEADER; | 5810 transaction.request_headers = EXTRA_HEADER; |
| 5755 transaction.data = kFullRangeData; | 5811 transaction.data = kFullRangeData; |
| 5756 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | 5812 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 5757 | 5813 |
| 5758 // We update the headers with the ones received while revalidating. | 5814 // We update the headers with the ones received while revalidating. |
| 5759 std::string expected_headers( | 5815 std::string expected_headers( |
| 5760 "HTTP/1.1 200 OK\n" | 5816 "HTTP/1.1 200 OK\n" |
| 5761 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 5817 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 5762 "Accept-Ranges: bytes\n" | 5818 "Accept-Ranges: bytes\n" |
| 5763 "ETag: \"foo\"\n" | 5819 "ETag: \"foo\"\n" |
| 5764 "Content-Length: 80\n"); | 5820 "Content-Length: 80\n"); |
| 5765 | 5821 |
| 5766 EXPECT_EQ(expected_headers, headers); | 5822 EXPECT_EQ(expected_headers, headers); |
| 5767 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 5823 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5768 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 5824 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5769 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5825 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5770 | 5826 |
| 5771 // Verify that the disk entry was updated. | 5827 // Verify that the disk entry was updated. |
| 5772 disk_cache::Entry* entry; | 5828 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80); |
| 5773 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
| 5774 EXPECT_EQ(80, entry->GetDataSize(1)); | |
| 5775 bool truncated = true; | |
| 5776 HttpResponseInfo response; | |
| 5777 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 5778 EXPECT_FALSE(truncated); | |
| 5779 entry->Close(); | |
| 5780 | |
| 5781 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
| 5782 } | 5829 } |
| 5783 | 5830 |
| 5784 // Tests the handling of no-store when revalidating a truncated entry. | 5831 // Tests the handling of no-store when revalidating a truncated entry. |
| 5785 TEST(HttpCache, GET_IncompleteResource_NoStore) { | 5832 TEST(HttpCache, GET_IncompleteResource_NoStore) { |
| 5786 MockHttpCache cache; | 5833 MockHttpCache cache; |
| 5787 AddMockTransaction(&kRangeGET_TransactionOK); | 5834 AddMockTransaction(&kRangeGET_TransactionOK); |
| 5788 | 5835 |
| 5789 std::string raw_headers("HTTP/1.1 200 OK\n" | 5836 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 5790 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 5837 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 5791 "ETag: \"foo\"\n" | 5838 "ETag: \"foo\"\n" |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 6037 if (rv == ERR_IO_PENDING) | 6084 if (rv == ERR_IO_PENDING) |
| 6038 rv = c->callback.WaitForResult(); | 6085 rv = c->callback.WaitForResult(); |
| 6039 std::string content; | 6086 std::string content; |
| 6040 rv = ReadTransaction(c->trans.get(), &content); | 6087 rv = ReadTransaction(c->trans.get(), &content); |
| 6041 EXPECT_EQ(ERR_CACHE_AUTH_FAILURE_AFTER_READ, rv); | 6088 EXPECT_EQ(ERR_CACHE_AUTH_FAILURE_AFTER_READ, rv); |
| 6042 } | 6089 } |
| 6043 | 6090 |
| 6044 // Tests that we cache a 200 response to the validation request. | 6091 // Tests that we cache a 200 response to the validation request. |
| 6045 TEST(HttpCache, GET_IncompleteResource4) { | 6092 TEST(HttpCache, GET_IncompleteResource4) { |
| 6046 MockHttpCache cache; | 6093 MockHttpCache cache; |
| 6047 AddMockTransaction(&kRangeGET_TransactionOK); | 6094 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 6048 | 6095 |
| 6049 std::string raw_headers("HTTP/1.1 200 OK\n" | 6096 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 6050 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" | 6097 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 6051 "ETag: \"foo\"\n" | 6098 "ETag: \"foo\"\n" |
| 6052 "Accept-Ranges: bytes\n" | 6099 "Accept-Ranges: bytes\n" |
| 6053 "Content-Length: 80\n"); | 6100 "Content-Length: 80\n"); |
| 6054 CreateTruncatedEntry(raw_headers, &cache); | 6101 CreateTruncatedEntry(raw_headers, &cache); |
| 6055 | 6102 |
| 6056 // Now make a regular request. | 6103 // Now make a regular request. |
| 6057 std::string headers; | 6104 std::string headers; |
| 6058 MockTransaction transaction(kRangeGET_TransactionOK); | |
| 6059 transaction.request_headers = EXTRA_HEADER; | 6105 transaction.request_headers = EXTRA_HEADER; |
| 6060 transaction.data = "Not a range"; | 6106 transaction.data = "Not a range"; |
| 6061 RangeTransactionServer handler; | 6107 RangeTransactionServer handler; |
| 6062 handler.set_bad_200(true); | 6108 handler.set_bad_200(true); |
| 6063 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | 6109 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 6064 | 6110 |
| 6065 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6111 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6066 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6112 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6067 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6113 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6068 | 6114 |
| 6069 // Verify that the disk entry was updated. | 6115 // Verify that the disk entry was updated. |
| 6070 disk_cache::Entry* entry; | 6116 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 11); |
| 6071 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
| 6072 EXPECT_EQ(11, entry->GetDataSize(1)); | |
| 6073 bool truncated = true; | |
| 6074 HttpResponseInfo response; | |
| 6075 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 6076 EXPECT_FALSE(truncated); | |
| 6077 entry->Close(); | |
| 6078 | |
| 6079 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
| 6080 } | 6117 } |
| 6081 | 6118 |
| 6082 // Tests that when we cancel a request that was interrupted, we mark it again | 6119 // Tests that when we cancel a request that was interrupted, we mark it again |
| 6083 // as truncated. | 6120 // as truncated. |
| 6084 TEST(HttpCache, GET_CancelIncompleteResource) { | 6121 TEST(HttpCache, GET_CancelIncompleteResource) { |
| 6085 MockHttpCache cache; | 6122 MockHttpCache cache; |
| 6086 AddMockTransaction(&kRangeGET_TransactionOK); | 6123 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 6087 | 6124 |
| 6088 std::string raw_headers("HTTP/1.1 200 OK\n" | 6125 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 6089 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" | 6126 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 6090 "ETag: \"foo\"\n" | 6127 "ETag: \"foo\"\n" |
| 6091 "Accept-Ranges: bytes\n" | 6128 "Accept-Ranges: bytes\n" |
| 6092 "Content-Length: 80\n"); | 6129 "Content-Length: 80\n"); |
| 6093 CreateTruncatedEntry(raw_headers, &cache); | 6130 CreateTruncatedEntry(raw_headers, &cache); |
| 6094 | 6131 |
| 6095 // Now make a regular request. | 6132 // Now make a regular request. |
| 6096 MockTransaction transaction(kRangeGET_TransactionOK); | |
| 6097 transaction.request_headers = EXTRA_HEADER; | 6133 transaction.request_headers = EXTRA_HEADER; |
| 6098 | 6134 |
| 6099 MockHttpRequest request(transaction); | 6135 MockHttpRequest request(transaction); |
| 6100 Context* c = new Context(); | 6136 Context* c = new Context(); |
| 6101 int rv = cache.CreateTransaction(&c->trans); | 6137 int rv = cache.CreateTransaction(&c->trans); |
| 6102 ASSERT_EQ(OK, rv); | 6138 ASSERT_EQ(OK, rv); |
| 6103 | 6139 |
| 6104 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); | 6140 rv = c->trans->Start(&request, c->callback.callback(), BoundNetLog()); |
| 6105 EXPECT_EQ(OK, c->callback.GetResult(rv)); | 6141 EXPECT_EQ(OK, c->callback.GetResult(rv)); |
| 6106 | 6142 |
| 6107 // Read 20 bytes from the cache, and 10 from the net. | 6143 // Read 20 bytes from the cache, and 10 from the net. |
| 6108 scoped_refptr<IOBuffer> buf(new IOBuffer(100)); | 6144 scoped_refptr<IOBuffer> buf(new IOBuffer(100)); |
| 6109 rv = c->trans->Read(buf.get(), 20, c->callback.callback()); | 6145 rv = c->trans->Read(buf.get(), 20, c->callback.callback()); |
| 6110 EXPECT_EQ(20, c->callback.GetResult(rv)); | 6146 EXPECT_EQ(20, c->callback.GetResult(rv)); |
| 6111 rv = c->trans->Read(buf.get(), 10, c->callback.callback()); | 6147 rv = c->trans->Read(buf.get(), 10, c->callback.callback()); |
| 6112 EXPECT_EQ(10, c->callback.GetResult(rv)); | 6148 EXPECT_EQ(10, c->callback.GetResult(rv)); |
| 6113 | 6149 |
| 6114 // At this point, we are already reading so canceling the request should leave | 6150 // At this point, we are already reading so canceling the request should leave |
| 6115 // a truncated one. | 6151 // a truncated one. |
| 6116 delete c; | 6152 delete c; |
| 6117 | 6153 |
| 6118 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 6154 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6119 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6155 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6120 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6156 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6121 | 6157 |
| 6122 // Verify that the disk entry was updated: now we have 30 bytes. | 6158 // Verify that the disk entry was updated: now we have 30 bytes. |
| 6123 disk_cache::Entry* entry; | 6159 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, true, 30); |
| 6124 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
| 6125 EXPECT_EQ(30, entry->GetDataSize(1)); | |
| 6126 bool truncated = false; | |
| 6127 HttpResponseInfo response; | |
| 6128 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 6129 EXPECT_TRUE(truncated); | |
| 6130 entry->Close(); | |
| 6131 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
| 6132 } | 6160 } |
| 6133 | 6161 |
| 6134 // Tests that we can handle range requests when we have a truncated entry. | 6162 // Tests that we can handle range requests when we have a truncated entry. |
| 6135 TEST(HttpCache, RangeGET_IncompleteResource) { | 6163 TEST(HttpCache, RangeGET_IncompleteResource) { |
| 6136 MockHttpCache cache; | 6164 MockHttpCache cache; |
| 6137 AddMockTransaction(&kRangeGET_TransactionOK); | 6165 AddMockTransaction(&kRangeGET_TransactionOK); |
| 6138 | 6166 |
| 6139 // Content-length will be intentionally bogus. | 6167 // Content-length will be intentionally bogus. |
| 6140 std::string raw_headers("HTTP/1.1 200 OK\n" | 6168 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 6141 "Last-Modified: something\n" | 6169 "Last-Modified: something\n" |
| (...skipping 726 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 6868 TEST(HttpCache, StopCachingSavesEntry) { | 6896 TEST(HttpCache, StopCachingSavesEntry) { |
| 6869 MockHttpCache cache; | 6897 MockHttpCache cache; |
| 6870 TestCompletionCallback callback; | 6898 TestCompletionCallback callback; |
| 6871 MockHttpRequest request(kSimpleGET_Transaction); | 6899 MockHttpRequest request(kSimpleGET_Transaction); |
| 6872 | 6900 |
| 6873 { | 6901 { |
| 6874 scoped_ptr<HttpTransaction> trans; | 6902 scoped_ptr<HttpTransaction> trans; |
| 6875 ASSERT_EQ(OK, cache.CreateTransaction(&trans)); | 6903 ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
| 6876 | 6904 |
| 6877 // Force a response that can be resumed. | 6905 // Force a response that can be resumed. |
| 6878 MockTransaction mock_transaction(kSimpleGET_Transaction); | 6906 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction); |
| 6879 AddMockTransaction(&mock_transaction); | 6907 AddMockTransaction(&mock_transaction); |
| 6880 mock_transaction.response_headers = "Cache-Control: max-age=10000\n" | 6908 mock_transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 6881 "Content-Length: 42\n" | 6909 "Content-Length: 42\n" |
| 6882 "Etag: \"foo\"\n"; | 6910 "Etag: \"foo\"\n"; |
| 6883 | 6911 |
| 6884 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | 6912 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 6885 EXPECT_EQ(OK, callback.GetResult(rv)); | 6913 EXPECT_EQ(OK, callback.GetResult(rv)); |
| 6886 | 6914 |
| 6887 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 6915 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); |
| 6888 rv = trans->Read(buf.get(), 10, callback.callback()); | 6916 rv = trans->Read(buf.get(), 10, callback.callback()); |
| 6889 EXPECT_EQ(callback.GetResult(rv), 10); | 6917 EXPECT_EQ(callback.GetResult(rv), 10); |
| 6890 | 6918 |
| 6891 trans->StopCaching(); | 6919 trans->StopCaching(); |
| 6892 | 6920 |
| 6893 // We should be able to keep reading. | 6921 // We should be able to keep reading. |
| 6894 rv = trans->Read(buf.get(), 256, callback.callback()); | 6922 rv = trans->Read(buf.get(), 256, callback.callback()); |
| 6895 EXPECT_GT(callback.GetResult(rv), 0); | 6923 EXPECT_GT(callback.GetResult(rv), 0); |
| 6896 rv = trans->Read(buf.get(), 256, callback.callback()); | 6924 rv = trans->Read(buf.get(), 256, callback.callback()); |
| 6897 EXPECT_EQ(callback.GetResult(rv), 0); | 6925 EXPECT_EQ(callback.GetResult(rv), 0); |
| 6898 | |
| 6899 RemoveMockTransaction(&mock_transaction); | |
| 6900 } | 6926 } |
| 6901 | 6927 |
| 6902 // Verify that the entry is marked as incomplete. | 6928 // Verify that the entry is marked as incomplete. |
| 6903 disk_cache::Entry* entry; | 6929 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); |
| 6904 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | |
| 6905 HttpResponseInfo response; | |
| 6906 bool truncated = false; | |
| 6907 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 6908 EXPECT_TRUE(truncated); | |
| 6909 entry->Close(); | |
| 6910 } | 6930 } |
| 6911 | 6931 |
| 6912 // Tests that we handle truncated enries when StopCaching is called. | 6932 // Tests that we handle truncated enries when StopCaching is called. |
| 6913 TEST(HttpCache, StopCachingTruncatedEntry) { | 6933 TEST(HttpCache, StopCachingTruncatedEntry) { |
| 6914 MockHttpCache cache; | 6934 MockHttpCache cache; |
| 6915 TestCompletionCallback callback; | 6935 TestCompletionCallback callback; |
| 6916 MockHttpRequest request(kRangeGET_TransactionOK); | 6936 MockHttpRequest request(kRangeGET_TransactionOK); |
| 6917 request.extra_headers.Clear(); | 6937 request.extra_headers.Clear(); |
| 6918 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); | 6938 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); |
| 6919 AddMockTransaction(&kRangeGET_TransactionOK); | 6939 AddMockTransaction(&kRangeGET_TransactionOK); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 6943 // We should be able to keep reading. | 6963 // We should be able to keep reading. |
| 6944 rv = trans->Read(buf.get(), 256, callback.callback()); | 6964 rv = trans->Read(buf.get(), 256, callback.callback()); |
| 6945 EXPECT_GT(callback.GetResult(rv), 0); | 6965 EXPECT_GT(callback.GetResult(rv), 0); |
| 6946 rv = trans->Read(buf.get(), 256, callback.callback()); | 6966 rv = trans->Read(buf.get(), 256, callback.callback()); |
| 6947 EXPECT_GT(callback.GetResult(rv), 0); | 6967 EXPECT_GT(callback.GetResult(rv), 0); |
| 6948 rv = trans->Read(buf.get(), 256, callback.callback()); | 6968 rv = trans->Read(buf.get(), 256, callback.callback()); |
| 6949 EXPECT_EQ(callback.GetResult(rv), 0); | 6969 EXPECT_EQ(callback.GetResult(rv), 0); |
| 6950 } | 6970 } |
| 6951 | 6971 |
| 6952 // Verify that the disk entry was updated. | 6972 // Verify that the disk entry was updated. |
| 6953 disk_cache::Entry* entry; | 6973 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 80); |
| 6954 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
| 6955 EXPECT_EQ(80, entry->GetDataSize(1)); | |
| 6956 bool truncated = true; | |
| 6957 HttpResponseInfo response; | |
| 6958 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 6959 EXPECT_FALSE(truncated); | |
| 6960 entry->Close(); | |
| 6961 | |
| 6962 RemoveMockTransaction(&kRangeGET_TransactionOK); | 6974 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 6963 } | 6975 } |
| 6964 | 6976 |
| 6965 // Tests that we detect truncated resources from the net when there is | 6977 // Tests that we detect truncated resources from the net when there is |
| 6966 // a Content-Length header. | 6978 // a Content-Length header. |
| 6967 TEST(HttpCache, TruncatedByContentLength) { | 6979 TEST(HttpCache, TruncatedByContentLength) { |
| 6968 MockHttpCache cache; | 6980 MockHttpCache cache; |
| 6969 TestCompletionCallback callback; | 6981 TestCompletionCallback callback; |
| 6970 | 6982 |
| 6971 MockTransaction transaction(kSimpleGET_Transaction); | 6983 MockTransaction transaction(kSimpleGET_Transaction); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 6991 | 7003 |
| 6992 MockTransaction transaction(kSimpleGET_Transaction); | 7004 MockTransaction transaction(kSimpleGET_Transaction); |
| 6993 AddMockTransaction(&transaction); | 7005 AddMockTransaction(&transaction); |
| 6994 transaction.response_headers = "Cache-Control: max-age=10000\n" | 7006 transaction.response_headers = "Cache-Control: max-age=10000\n" |
| 6995 "Content-Length: 100\n" | 7007 "Content-Length: 100\n" |
| 6996 "Etag: \"foo\"\n"; | 7008 "Etag: \"foo\"\n"; |
| 6997 RunTransactionTest(cache.http_cache(), transaction); | 7009 RunTransactionTest(cache.http_cache(), transaction); |
| 6998 RemoveMockTransaction(&transaction); | 7010 RemoveMockTransaction(&transaction); |
| 6999 | 7011 |
| 7000 // Verify that the entry is marked as incomplete. | 7012 // Verify that the entry is marked as incomplete. |
| 7001 disk_cache::Entry* entry; | 7013 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0); |
| 7002 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | |
| 7003 HttpResponseInfo response; | |
| 7004 bool truncated = false; | |
| 7005 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
| 7006 EXPECT_TRUE(truncated); | |
| 7007 entry->Close(); | |
| 7008 } | 7014 } |
| 7009 | 7015 |
| 7010 // Make sure that calling SetPriority on a cache transaction passes on | 7016 // Make sure that calling SetPriority on a cache transaction passes on |
| 7011 // its priority updates to its underlying network transaction. | 7017 // its priority updates to its underlying network transaction. |
| 7012 TEST(HttpCache, SetPriority) { | 7018 TEST(HttpCache, SetPriority) { |
| 7013 MockHttpCache cache; | 7019 MockHttpCache cache; |
| 7014 | 7020 |
| 7015 scoped_ptr<HttpTransaction> trans; | 7021 scoped_ptr<HttpTransaction> trans; |
| 7016 ASSERT_EQ(OK, cache.http_cache()->CreateTransaction(IDLE, &trans)); | 7022 ASSERT_EQ(OK, cache.http_cache()->CreateTransaction(IDLE, &trans)); |
| 7017 | 7023 |
| (...skipping 629 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 7647 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 7653 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 7648 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 7654 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 7649 EXPECT_TRUE(response_info.was_cached); | 7655 EXPECT_TRUE(response_info.was_cached); |
| 7650 | 7656 |
| 7651 // The new SSL state is reported. | 7657 // The new SSL state is reported. |
| 7652 EXPECT_EQ(status2, response_info.ssl_info.connection_status); | 7658 EXPECT_EQ(status2, response_info.ssl_info.connection_status); |
| 7653 EXPECT_TRUE(cert2->Equals(response_info.ssl_info.cert.get())); | 7659 EXPECT_TRUE(cert2->Equals(response_info.ssl_info.cert.get())); |
| 7654 } | 7660 } |
| 7655 | 7661 |
| 7656 } // namespace net | 7662 } // namespace net |
| OLD | NEW |