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 3376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3387 | 3387 |
3388 Verify206Response(headers, 20, 59); | 3388 Verify206Response(headers, 20, 59); |
3389 EXPECT_EQ(4, cache.network_layer()->transaction_count()); | 3389 EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
3390 EXPECT_EQ(3, cache.disk_cache()->open_count()); | 3390 EXPECT_EQ(3, cache.disk_cache()->open_count()); |
3391 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3391 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
3392 TestLoadTimingNetworkRequest(load_timing_info); | 3392 TestLoadTimingNetworkRequest(load_timing_info); |
3393 | 3393 |
3394 RemoveMockTransaction(&kRangeGET_TransactionOK); | 3394 RemoveMockTransaction(&kRangeGET_TransactionOK); |
3395 } | 3395 } |
3396 | 3396 |
| 3397 #if defined(OS_ANDROID) |
| 3398 |
| 3399 // Checks that with a cache backend having Sparse IO unimplementes the cache |
| 3400 // entry would be doomed after a range request. |
| 3401 // TODO(pasko): remove when the SimpleBackendImpl implements Sparse IO. |
| 3402 TEST(HttpCache, RangeGET_SparseNotImplemented) { |
| 3403 MockHttpCache cache; |
| 3404 cache.disk_cache()->set_fail_sparse_requests(); |
| 3405 |
| 3406 // Run a cacheable request to prime the cache. |
| 3407 MockTransaction transaction(kTypicalGET_Transaction); |
| 3408 transaction.url = kRangeGET_TransactionOK.url; |
| 3409 AddMockTransaction(&transaction); |
| 3410 RunTransactionTest(cache.http_cache(), transaction); |
| 3411 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3412 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3413 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3414 |
| 3415 // Verify that we added the entry. |
| 3416 disk_cache::Entry* entry; |
| 3417 net::TestCompletionCallback cb; |
| 3418 int rv = cache.disk_cache()->OpenEntry(transaction.url, |
| 3419 &entry, |
| 3420 cb.callback()); |
| 3421 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 3422 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3423 entry->Close(); |
| 3424 RemoveMockTransaction(&transaction); |
| 3425 |
| 3426 // Request the range with the backend that does not support it. |
| 3427 MockTransaction transaction2(kRangeGET_TransactionOK); |
| 3428 std::string headers; |
| 3429 AddMockTransaction(&transaction2); |
| 3430 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); |
| 3431 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3432 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 3433 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3434 |
| 3435 // Mock cache would return net::ERR_CACHE_OPEN_FAILURE on a doomed entry, even |
| 3436 // if it was re-created later, so this effectively checks that the old data is |
| 3437 // gone. |
| 3438 disk_cache::Entry* entry2; |
| 3439 rv = cache.disk_cache()->OpenEntry(transaction2.url, |
| 3440 &entry2, |
| 3441 cb.callback()); |
| 3442 ASSERT_EQ(net::ERR_CACHE_OPEN_FAILURE, cb.GetResult(rv)); |
| 3443 RemoveMockTransaction(&transaction2); |
| 3444 } |
| 3445 |
| 3446 TEST(HttpCache, RangeGET_SparseNotImplementedOnEmptyCache) { |
| 3447 MockHttpCache cache; |
| 3448 cache.disk_cache()->set_fail_sparse_requests(); |
| 3449 |
| 3450 // Request the range with the backend that does not support it. |
| 3451 MockTransaction transaction(kRangeGET_TransactionOK); |
| 3452 std::string headers; |
| 3453 AddMockTransaction(&transaction); |
| 3454 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 3455 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3456 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3457 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3458 |
| 3459 // Mock cache would return net::ERR_CACHE_OPEN_FAILURE on a doomed entry, even |
| 3460 // if it was re-created later, so this effectively checks that the old data is |
| 3461 // gone as a result of a failed range write. |
| 3462 disk_cache::Entry* entry; |
| 3463 net::TestCompletionCallback cb; |
| 3464 int rv = cache.disk_cache()->OpenEntry(transaction.url, |
| 3465 &entry, |
| 3466 cb.callback()); |
| 3467 ASSERT_EQ(net::ERR_CACHE_OPEN_FAILURE, cb.GetResult(rv)); |
| 3468 RemoveMockTransaction(&transaction); |
| 3469 } |
| 3470 |
| 3471 #endif // OS_ANDROID |
| 3472 |
3397 // Tests that we can cache range requests and fetch random blocks from the | 3473 // Tests that we can cache range requests and fetch random blocks from the |
3398 // cache and the network, with synchronous responses. | 3474 // cache and the network, with synchronous responses. |
3399 TEST(HttpCache, RangeGET_SyncOK) { | 3475 TEST(HttpCache, RangeGET_SyncOK) { |
3400 MockHttpCache cache; | 3476 MockHttpCache cache; |
3401 | 3477 |
3402 MockTransaction transaction(kRangeGET_TransactionOK); | 3478 MockTransaction transaction(kRangeGET_TransactionOK); |
3403 transaction.test_mode = TEST_MODE_SYNC_ALL; | 3479 transaction.test_mode = TEST_MODE_SYNC_ALL; |
3404 AddMockTransaction(&transaction); | 3480 AddMockTransaction(&transaction); |
3405 | 3481 |
3406 // Write to the cache (40-49). | 3482 // Write to the cache (40-49). |
(...skipping 2642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6049 trans->SetPriority(net::HIGHEST); | 6125 trans->SetPriority(net::HIGHEST); |
6050 // Should trigger a new network transaction and pick up the new | 6126 // Should trigger a new network transaction and pick up the new |
6051 // priority. | 6127 // priority. |
6052 ReadAndVerifyTransaction(trans.get(), transaction); | 6128 ReadAndVerifyTransaction(trans.get(), transaction); |
6053 | 6129 |
6054 EXPECT_EQ(net::HIGHEST, | 6130 EXPECT_EQ(net::HIGHEST, |
6055 cache.network_layer()->last_create_transaction_priority()); | 6131 cache.network_layer()->last_create_transaction_priority()); |
6056 | 6132 |
6057 RemoveMockTransaction(&kRangeGET_TransactionOK); | 6133 RemoveMockTransaction(&kRangeGET_TransactionOK); |
6058 } | 6134 } |
OLD | NEW |