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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 5315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5326 TEST(HttpCache, CachedRedirect) { | 5326 TEST(HttpCache, CachedRedirect) { |
5327 MockHttpCache cache; | 5327 MockHttpCache cache; |
5328 | 5328 |
5329 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); | 5329 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); |
5330 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; | 5330 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; |
5331 kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; | 5331 kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; |
5332 | 5332 |
5333 MockHttpRequest request(kTestTransaction); | 5333 MockHttpRequest request(kTestTransaction); |
5334 net::TestCompletionCallback callback; | 5334 net::TestCompletionCallback callback; |
5335 | 5335 |
5336 // write to the cache | 5336 // Write to the cache. |
5337 { | 5337 { |
5338 scoped_ptr<net::HttpTransaction> trans; | 5338 scoped_ptr<net::HttpTransaction> trans; |
5339 int rv = cache.http_cache()->CreateTransaction( | 5339 int rv = cache.http_cache()->CreateTransaction( |
5340 net::DEFAULT_PRIORITY, &trans, NULL); | 5340 net::DEFAULT_PRIORITY, &trans, NULL); |
5341 EXPECT_EQ(net::OK, rv); | 5341 EXPECT_EQ(net::OK, rv); |
5342 ASSERT_TRUE(trans.get()); | 5342 ASSERT_TRUE(trans.get()); |
5343 | 5343 |
5344 rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | 5344 rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
5345 if (rv == net::ERR_IO_PENDING) | 5345 if (rv == net::ERR_IO_PENDING) |
5346 rv = callback.WaitForResult(); | 5346 rv = callback.WaitForResult(); |
5347 ASSERT_EQ(net::OK, rv); | 5347 ASSERT_EQ(net::OK, rv); |
5348 | 5348 |
5349 const net::HttpResponseInfo* info = trans->GetResponseInfo(); | 5349 const net::HttpResponseInfo* info = trans->GetResponseInfo(); |
5350 ASSERT_TRUE(info); | 5350 ASSERT_TRUE(info); |
5351 | 5351 |
5352 EXPECT_EQ(info->headers->response_code(), 301); | 5352 EXPECT_EQ(info->headers->response_code(), 301); |
5353 | 5353 |
5354 std::string location; | 5354 std::string location; |
5355 info->headers->EnumerateHeader(NULL, "Location", &location); | 5355 info->headers->EnumerateHeader(NULL, "Location", &location); |
5356 EXPECT_EQ(location, "http://www.bar.com/"); | 5356 EXPECT_EQ(location, "http://www.bar.com/"); |
5357 | 5357 |
| 5358 // Mark the transaction as completed so it is cached. |
| 5359 trans->DoneReading(); |
| 5360 |
5358 // Destroy transaction when going out of scope. We have not actually | 5361 // Destroy transaction when going out of scope. We have not actually |
5359 // read the response body -- want to test that it is still getting cached. | 5362 // read the response body -- want to test that it is still getting cached. |
5360 } | 5363 } |
5361 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 5364 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
5362 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 5365 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
5363 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5366 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
5364 | 5367 |
5365 // read from the cache | 5368 // Active entries in the cache are not retired synchronously. Make |
| 5369 // sure the next run hits the MockHttpCache and open_count is |
| 5370 // correct. |
| 5371 base::MessageLoop::current()->RunUntilIdle(); |
| 5372 |
| 5373 // Read from the cache. |
5366 { | 5374 { |
5367 scoped_ptr<net::HttpTransaction> trans; | 5375 scoped_ptr<net::HttpTransaction> trans; |
5368 int rv = cache.http_cache()->CreateTransaction( | 5376 int rv = cache.http_cache()->CreateTransaction( |
5369 net::DEFAULT_PRIORITY, &trans, NULL); | 5377 net::DEFAULT_PRIORITY, &trans, NULL); |
5370 EXPECT_EQ(net::OK, rv); | 5378 EXPECT_EQ(net::OK, rv); |
5371 ASSERT_TRUE(trans.get()); | 5379 ASSERT_TRUE(trans.get()); |
5372 | 5380 |
5373 rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | 5381 rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
5374 if (rv == net::ERR_IO_PENDING) | 5382 if (rv == net::ERR_IO_PENDING) |
5375 rv = callback.WaitForResult(); | 5383 rv = callback.WaitForResult(); |
5376 ASSERT_EQ(net::OK, rv); | 5384 ASSERT_EQ(net::OK, rv); |
5377 | 5385 |
5378 const net::HttpResponseInfo* info = trans->GetResponseInfo(); | 5386 const net::HttpResponseInfo* info = trans->GetResponseInfo(); |
5379 ASSERT_TRUE(info); | 5387 ASSERT_TRUE(info); |
5380 | 5388 |
5381 EXPECT_EQ(info->headers->response_code(), 301); | 5389 EXPECT_EQ(info->headers->response_code(), 301); |
5382 | 5390 |
5383 std::string location; | 5391 std::string location; |
5384 info->headers->EnumerateHeader(NULL, "Location", &location); | 5392 info->headers->EnumerateHeader(NULL, "Location", &location); |
5385 EXPECT_EQ(location, "http://www.bar.com/"); | 5393 EXPECT_EQ(location, "http://www.bar.com/"); |
5386 | 5394 |
| 5395 // Mark the transaction as completed so it is cached. |
| 5396 trans->DoneReading(); |
| 5397 |
5387 // Destroy transaction when going out of scope. We have not actually | 5398 // Destroy transaction when going out of scope. We have not actually |
5388 // read the response body -- want to test that it is still getting cached. | 5399 // read the response body -- want to test that it is still getting cached. |
5389 } | 5400 } |
5390 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 5401 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
5391 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 5402 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
5392 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5403 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
5393 } | 5404 } |
5394 | 5405 |
5395 // Verify that no-cache resources are stored in cache, but are not fetched from | 5406 // Verify that no-cache resources are stored in cache, but are not fetched from |
5396 // cache during normal loads. | 5407 // cache during normal loads. |
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5831 base::MessageLoop::current()->RunUntilIdle(); | 5842 base::MessageLoop::current()->RunUntilIdle(); |
5832 | 5843 |
5833 // Read from the cache. | 5844 // Read from the cache. |
5834 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 5845 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
5835 | 5846 |
5836 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 5847 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
5837 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 5848 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
5838 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5849 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
5839 } | 5850 } |
5840 | 5851 |
| 5852 // Tests that we don't mark entries as truncated and release the cache |
| 5853 // entry when DoneReading() is called before any Read() calls, such as |
| 5854 // for a redirect. |
| 5855 TEST(HttpCache, DoneReading) { |
| 5856 MockHttpCache cache; |
| 5857 net::TestCompletionCallback callback; |
| 5858 |
| 5859 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 5860 transaction.data = ""; |
| 5861 |
| 5862 scoped_ptr<net::HttpTransaction> trans; |
| 5863 int rv = cache.http_cache()->CreateTransaction( |
| 5864 net::DEFAULT_PRIORITY, &trans, NULL); |
| 5865 EXPECT_EQ(net::OK, rv); |
| 5866 |
| 5867 MockHttpRequest request(transaction); |
| 5868 rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
| 5869 EXPECT_EQ(net::OK, callback.GetResult(rv)); |
| 5870 |
| 5871 trans->DoneReading(); |
| 5872 // Leave the transaction around. |
| 5873 |
| 5874 // Make sure that the ActiveEntry is gone. |
| 5875 base::MessageLoop::current()->RunUntilIdle(); |
| 5876 |
| 5877 // Read from the cache. This should not deadlock. |
| 5878 RunTransactionTest(cache.http_cache(), transaction); |
| 5879 |
| 5880 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5881 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5882 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5883 } |
| 5884 |
5841 // Tests that we stop caching when told. | 5885 // Tests that we stop caching when told. |
5842 TEST(HttpCache, StopCachingDeletesEntry) { | 5886 TEST(HttpCache, StopCachingDeletesEntry) { |
5843 MockHttpCache cache; | 5887 MockHttpCache cache; |
5844 net::TestCompletionCallback callback; | 5888 net::TestCompletionCallback callback; |
5845 MockHttpRequest request(kSimpleGET_Transaction); | 5889 MockHttpRequest request(kSimpleGET_Transaction); |
5846 | 5890 |
5847 { | 5891 { |
5848 scoped_ptr<net::HttpTransaction> trans; | 5892 scoped_ptr<net::HttpTransaction> trans; |
5849 int rv = cache.http_cache()->CreateTransaction( | 5893 int rv = cache.http_cache()->CreateTransaction( |
5850 net::DEFAULT_PRIORITY, &trans, NULL); | 5894 net::DEFAULT_PRIORITY, &trans, NULL); |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6162 trans->SetPriority(net::HIGHEST); | 6206 trans->SetPriority(net::HIGHEST); |
6163 // Should trigger a new network transaction and pick up the new | 6207 // Should trigger a new network transaction and pick up the new |
6164 // priority. | 6208 // priority. |
6165 ReadAndVerifyTransaction(trans.get(), transaction); | 6209 ReadAndVerifyTransaction(trans.get(), transaction); |
6166 | 6210 |
6167 EXPECT_EQ(net::HIGHEST, | 6211 EXPECT_EQ(net::HIGHEST, |
6168 cache.network_layer()->last_create_transaction_priority()); | 6212 cache.network_layer()->last_create_transaction_priority()); |
6169 | 6213 |
6170 RemoveMockTransaction(&kRangeGET_TransactionOK); | 6214 RemoveMockTransaction(&kRangeGET_TransactionOK); |
6171 } | 6215 } |
OLD | NEW |