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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 6426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6437 MockHttpCache cache; | 6437 MockHttpCache cache; |
6438 | 6438 |
6439 // Write to the cache | 6439 // Write to the cache |
6440 net::HttpResponseInfo response; | 6440 net::HttpResponseInfo response; |
6441 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | 6441 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
6442 &response); | 6442 &response); |
6443 EXPECT_TRUE(response.metadata.get() == NULL); | 6443 EXPECT_TRUE(response.metadata.get() == NULL); |
6444 | 6444 |
6445 // Trivial call. | 6445 // Trivial call. |
6446 cache.http_cache()->WriteMetadata(GURL("foo"), net::DEFAULT_PRIORITY, | 6446 cache.http_cache()->WriteMetadata(GURL("foo"), net::DEFAULT_PRIORITY, |
6447 Time::Now(), NULL, 0); | 6447 Time::Now().ToDoubleT(), NULL, 0); |
6448 | 6448 |
6449 // Write meta data to the same entry. | 6449 // Write meta data to the same entry. |
6450 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); | 6450 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); |
6451 memset(buf->data(), 0, buf->size()); | 6451 memset(buf->data(), 0, buf->size()); |
6452 base::strlcpy(buf->data(), "Hi there", buf->size()); | 6452 base::strlcpy(buf->data(), "Hi there", buf->size()); |
6453 cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url), | 6453 cache.http_cache()->WriteMetadata( |
6454 net::DEFAULT_PRIORITY, | 6454 GURL(kSimpleGET_Transaction.url), net::DEFAULT_PRIORITY, |
6455 response.response_time, | 6455 response.response_time.ToDoubleT(), buf.get(), buf->size()); |
6456 buf.get(), | |
6457 buf->size()); | |
6458 | 6456 |
6459 // Release the buffer before the operation takes place. | 6457 // Release the buffer before the operation takes place. |
6460 buf = NULL; | 6458 buf = NULL; |
6461 | 6459 |
6462 // Makes sure we finish pending operations. | 6460 // Makes sure we finish pending operations. |
6463 base::MessageLoop::current()->RunUntilIdle(); | 6461 base::MessageLoop::current()->RunUntilIdle(); |
6464 | 6462 |
6465 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | 6463 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
6466 &response); | 6464 &response); |
6467 ASSERT_TRUE(response.metadata.get() != NULL); | 6465 ASSERT_TRUE(response.metadata.get() != NULL); |
(...skipping 14 matching lines...) Expand all Loading... |
6482 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | 6480 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
6483 &response); | 6481 &response); |
6484 EXPECT_TRUE(response.metadata.get() == NULL); | 6482 EXPECT_TRUE(response.metadata.get() == NULL); |
6485 | 6483 |
6486 // Attempt to write meta data to the same entry. | 6484 // Attempt to write meta data to the same entry. |
6487 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); | 6485 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); |
6488 memset(buf->data(), 0, buf->size()); | 6486 memset(buf->data(), 0, buf->size()); |
6489 base::strlcpy(buf->data(), "Hi there", buf->size()); | 6487 base::strlcpy(buf->data(), "Hi there", buf->size()); |
6490 base::Time expected_time = response.response_time - | 6488 base::Time expected_time = response.response_time - |
6491 base::TimeDelta::FromMilliseconds(20); | 6489 base::TimeDelta::FromMilliseconds(20); |
6492 cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url), | 6490 cache.http_cache()->WriteMetadata( |
6493 net::DEFAULT_PRIORITY, | 6491 GURL(kSimpleGET_Transaction.url), net::DEFAULT_PRIORITY, |
6494 expected_time, | 6492 expected_time.ToDoubleT(), buf.get(), buf->size()); |
6495 buf.get(), | |
6496 buf->size()); | |
6497 | 6493 |
6498 // Makes sure we finish pending operations. | 6494 // Makes sure we finish pending operations. |
6499 base::MessageLoop::current()->RunUntilIdle(); | 6495 base::MessageLoop::current()->RunUntilIdle(); |
6500 | 6496 |
6501 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | 6497 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
6502 &response); | 6498 &response); |
6503 EXPECT_TRUE(response.metadata.get() == NULL); | 6499 EXPECT_TRUE(response.metadata.get() == NULL); |
6504 | 6500 |
6505 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6501 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
6506 EXPECT_EQ(2, cache.disk_cache()->open_count()); | 6502 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
6507 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6503 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
6508 } | 6504 } |
6509 | 6505 |
6510 // Tests that we can read metadata after validating the entry and with READ mode | 6506 // Tests that we can read metadata after validating the entry and with READ mode |
6511 // transactions. | 6507 // transactions. |
6512 TEST(HttpCache, ReadMetadata) { | 6508 TEST(HttpCache, ReadMetadata) { |
6513 MockHttpCache cache; | 6509 MockHttpCache cache; |
6514 | 6510 |
6515 // Write to the cache | 6511 // Write to the cache |
6516 net::HttpResponseInfo response; | 6512 net::HttpResponseInfo response; |
6517 RunTransactionTestWithResponseInfo(cache.http_cache(), | 6513 RunTransactionTestWithResponseInfo(cache.http_cache(), |
6518 kTypicalGET_Transaction, &response); | 6514 kTypicalGET_Transaction, &response); |
6519 EXPECT_TRUE(response.metadata.get() == NULL); | 6515 EXPECT_TRUE(response.metadata.get() == NULL); |
6520 | 6516 |
6521 // Write meta data to the same entry. | 6517 // Write meta data to the same entry. |
6522 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); | 6518 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); |
6523 memset(buf->data(), 0, buf->size()); | 6519 memset(buf->data(), 0, buf->size()); |
6524 base::strlcpy(buf->data(), "Hi there", buf->size()); | 6520 base::strlcpy(buf->data(), "Hi there", buf->size()); |
6525 cache.http_cache()->WriteMetadata(GURL(kTypicalGET_Transaction.url), | 6521 cache.http_cache()->WriteMetadata( |
6526 net::DEFAULT_PRIORITY, | 6522 GURL(kTypicalGET_Transaction.url), net::DEFAULT_PRIORITY, |
6527 response.response_time, | 6523 response.response_time.ToDoubleT(), buf.get(), buf->size()); |
6528 buf.get(), | |
6529 buf->size()); | |
6530 | 6524 |
6531 // Makes sure we finish pending operations. | 6525 // Makes sure we finish pending operations. |
6532 base::MessageLoop::current()->RunUntilIdle(); | 6526 base::MessageLoop::current()->RunUntilIdle(); |
6533 | 6527 |
6534 // Start with a READ mode transaction. | 6528 // Start with a READ mode transaction. |
6535 MockTransaction trans1(kTypicalGET_Transaction); | 6529 MockTransaction trans1(kTypicalGET_Transaction); |
6536 trans1.load_flags = net::LOAD_ONLY_FROM_CACHE; | 6530 trans1.load_flags = net::LOAD_ONLY_FROM_CACHE; |
6537 | 6531 |
6538 RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response); | 6532 RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response); |
6539 ASSERT_TRUE(response.metadata.get() != NULL); | 6533 ASSERT_TRUE(response.metadata.get() != NULL); |
(...skipping 1235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7775 | 7769 |
7776 // Here the second transaction proceeds without reading the first body. | 7770 // Here the second transaction proceeds without reading the first body. |
7777 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); | 7771 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); |
7778 base::MessageLoop::current()->RunUntilIdle(); | 7772 base::MessageLoop::current()->RunUntilIdle(); |
7779 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState()); | 7773 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState()); |
7780 ASSERT_TRUE(second->trans->GetResponseInfo()); | 7774 ASSERT_TRUE(second->trans->GetResponseInfo()); |
7781 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( | 7775 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( |
7782 "Cache-Control", "no-store")); | 7776 "Cache-Control", "no-store")); |
7783 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); | 7777 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); |
7784 } | 7778 } |
OLD | NEW |