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

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

Issue 23710059: Release the cache entry on deferred redirect. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 3 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 "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 483 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 "Accept-Ranges: bytes\n" 494 "Accept-Ranges: bytes\n"
495 "Content-Length: 10\n", 495 "Content-Length: 10\n",
496 base::Time(), 496 base::Time(),
497 "rg: 40-49 ", 497 "rg: 40-49 ",
498 TEST_MODE_NORMAL, 498 TEST_MODE_NORMAL,
499 &RangeTransactionServer::RangeHandler, 499 &RangeTransactionServer::RangeHandler,
500 0, 500 0,
501 net::OK 501 net::OK
502 }; 502 };
503 503
504 // The cache should return no response, so data is empty, but the
505 // network will return data. (For redirects, DoneReading() is used to
506 // discard the response.)
507 void DiscardedResponseHandler(const net::HttpRequestInfo* request,
508 std::string* response_status,
509 std::string* response_headers,
510 std::string* response_data) {
511 *response_data = "hello";
512 }
513
514 const MockTransaction kDiscardedResponse_Transaction = {
rvargas (doing something else) 2013/09/18 18:37:10 If we keep this transaction, move it to right befo
davidben 2013/09/18 19:35:43 (Removed it)
515 "http://www.google.com/",
516 "GET",
517 base::Time(),
518 "",
519 net::LOAD_NORMAL,
520 "HTTP/1.1 302 Found",
521 "Cache-Control: max-age=10000\n"
522 "Location: http://www.google.com/destination\n",
523 base::Time(),
524 "",
525 TEST_MODE_NORMAL,
526 DiscardedResponseHandler,
rvargas (doing something else) 2013/09/18 18:37:10 From the point of view of the test, or the contrac
davidben 2013/09/18 19:35:43 (Removed it)
527 0,
528 net::OK
529 };
530
504 // Verifies the response headers (|response|) match a partial content 531 // Verifies the response headers (|response|) match a partial content
505 // response for the range starting at |start| and ending at |end|. 532 // response for the range starting at |start| and ending at |end|.
506 void Verify206Response(std::string response, int start, int end) { 533 void Verify206Response(std::string response, int start, int end) {
507 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), 534 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(),
508 response.size())); 535 response.size()));
509 scoped_refptr<net::HttpResponseHeaders> headers( 536 scoped_refptr<net::HttpResponseHeaders> headers(
510 new net::HttpResponseHeaders(raw_headers)); 537 new net::HttpResponseHeaders(raw_headers));
511 538
512 ASSERT_EQ(206, headers->response_code()); 539 ASSERT_EQ(206, headers->response_code());
513 540
(...skipping 4812 matching lines...) Expand 10 before | Expand all | Expand 10 after
5326 TEST(HttpCache, CachedRedirect) { 5353 TEST(HttpCache, CachedRedirect) {
5327 MockHttpCache cache; 5354 MockHttpCache cache;
5328 5355
5329 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); 5356 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction);
5330 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; 5357 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently";
5331 kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; 5358 kTestTransaction.response_headers = "Location: http://www.bar.com/\n";
5332 5359
5333 MockHttpRequest request(kTestTransaction); 5360 MockHttpRequest request(kTestTransaction);
5334 net::TestCompletionCallback callback; 5361 net::TestCompletionCallback callback;
5335 5362
5336 // write to the cache 5363 // Write to the cache.
5337 { 5364 {
5338 scoped_ptr<net::HttpTransaction> trans; 5365 scoped_ptr<net::HttpTransaction> trans;
5339 int rv = cache.http_cache()->CreateTransaction( 5366 int rv = cache.http_cache()->CreateTransaction(
5340 net::DEFAULT_PRIORITY, &trans, NULL); 5367 net::DEFAULT_PRIORITY, &trans, NULL);
5341 EXPECT_EQ(net::OK, rv); 5368 EXPECT_EQ(net::OK, rv);
5342 ASSERT_TRUE(trans.get()); 5369 ASSERT_TRUE(trans.get());
5343 5370
5344 rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); 5371 rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
5345 if (rv == net::ERR_IO_PENDING) 5372 if (rv == net::ERR_IO_PENDING)
5346 rv = callback.WaitForResult(); 5373 rv = callback.WaitForResult();
5347 ASSERT_EQ(net::OK, rv); 5374 ASSERT_EQ(net::OK, rv);
5348 5375
5349 const net::HttpResponseInfo* info = trans->GetResponseInfo(); 5376 const net::HttpResponseInfo* info = trans->GetResponseInfo();
5350 ASSERT_TRUE(info); 5377 ASSERT_TRUE(info);
5351 5378
5352 EXPECT_EQ(info->headers->response_code(), 301); 5379 EXPECT_EQ(info->headers->response_code(), 301);
5353 5380
5354 std::string location; 5381 std::string location;
5355 info->headers->EnumerateHeader(NULL, "Location", &location); 5382 info->headers->EnumerateHeader(NULL, "Location", &location);
5356 EXPECT_EQ(location, "http://www.bar.com/"); 5383 EXPECT_EQ(location, "http://www.bar.com/");
5357 5384
5385 // Mark the transaction as completed so it is cached.
5386 trans->DoneReading();
rvargas (doing something else) 2013/09/18 18:37:10 This would not be needed.
davidben 2013/09/18 19:35:43 (Left this in here for now pending discussion abou
5387
5358 // Destroy transaction when going out of scope. We have not actually 5388 // 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. 5389 // read the response body -- want to test that it is still getting cached.
5360 } 5390 }
5361 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5391 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5362 EXPECT_EQ(0, cache.disk_cache()->open_count()); 5392 EXPECT_EQ(0, cache.disk_cache()->open_count());
5363 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5393 EXPECT_EQ(1, cache.disk_cache()->create_count());
5364 5394
5365 // read from the cache 5395 // Active entries in the cache are not retired synchronously. Make
5396 // sure the next run hits the MockHttpCache and open_count is
5397 // correct.
5398 base::MessageLoop::current()->RunUntilIdle();
5399
5400 // Read from the cache.
5366 { 5401 {
5367 scoped_ptr<net::HttpTransaction> trans; 5402 scoped_ptr<net::HttpTransaction> trans;
5368 int rv = cache.http_cache()->CreateTransaction( 5403 int rv = cache.http_cache()->CreateTransaction(
5369 net::DEFAULT_PRIORITY, &trans, NULL); 5404 net::DEFAULT_PRIORITY, &trans, NULL);
5370 EXPECT_EQ(net::OK, rv); 5405 EXPECT_EQ(net::OK, rv);
5371 ASSERT_TRUE(trans.get()); 5406 ASSERT_TRUE(trans.get());
5372 5407
5373 rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); 5408 rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
5374 if (rv == net::ERR_IO_PENDING) 5409 if (rv == net::ERR_IO_PENDING)
5375 rv = callback.WaitForResult(); 5410 rv = callback.WaitForResult();
5376 ASSERT_EQ(net::OK, rv); 5411 ASSERT_EQ(net::OK, rv);
5377 5412
5378 const net::HttpResponseInfo* info = trans->GetResponseInfo(); 5413 const net::HttpResponseInfo* info = trans->GetResponseInfo();
5379 ASSERT_TRUE(info); 5414 ASSERT_TRUE(info);
5380 5415
5381 EXPECT_EQ(info->headers->response_code(), 301); 5416 EXPECT_EQ(info->headers->response_code(), 301);
5382 5417
5383 std::string location; 5418 std::string location;
5384 info->headers->EnumerateHeader(NULL, "Location", &location); 5419 info->headers->EnumerateHeader(NULL, "Location", &location);
5385 EXPECT_EQ(location, "http://www.bar.com/"); 5420 EXPECT_EQ(location, "http://www.bar.com/");
5386 5421
5422 // Mark the transaction as completed so it is cached.
5423 trans->DoneReading();
5424
5387 // Destroy transaction when going out of scope. We have not actually 5425 // 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. 5426 // read the response body -- want to test that it is still getting cached.
5389 } 5427 }
5390 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5428 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5391 EXPECT_EQ(1, cache.disk_cache()->open_count()); 5429 EXPECT_EQ(1, cache.disk_cache()->open_count());
5392 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5430 EXPECT_EQ(1, cache.disk_cache()->create_count());
5393 } 5431 }
5394 5432
5395 // Verify that no-cache resources are stored in cache, but are not fetched from 5433 // Verify that no-cache resources are stored in cache, but are not fetched from
5396 // cache during normal loads. 5434 // cache during normal loads.
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after
5831 base::MessageLoop::current()->RunUntilIdle(); 5869 base::MessageLoop::current()->RunUntilIdle();
5832 5870
5833 // Read from the cache. 5871 // Read from the cache.
5834 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 5872 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
5835 5873
5836 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5874 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5837 EXPECT_EQ(1, cache.disk_cache()->open_count()); 5875 EXPECT_EQ(1, cache.disk_cache()->open_count());
5838 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5876 EXPECT_EQ(1, cache.disk_cache()->create_count());
5839 } 5877 }
5840 5878
5879 // Tests that we don't mark entries as truncated and release the cache
5880 // entry when DoneReading() is called before any Read() calls, such as
5881 // for a redirect.
5882 TEST(HttpCache, DiscardedResponse) {
rvargas (doing something else) 2013/09/18 18:37:10 nit: DiscardedResponse -> DoneReading ?
davidben 2013/09/18 19:35:43 Done.
5883 MockHttpCache cache;
5884 net::TestCompletionCallback callback;
5885
5886 scoped_ptr<net::HttpTransaction> trans;
5887 int rv = cache.http_cache()->CreateTransaction(
5888 net::DEFAULT_PRIORITY, &trans, NULL);
5889 EXPECT_EQ(net::OK, rv);
5890
5891 MockHttpRequest request(kDiscardedResponse_Transaction);
rvargas (doing something else) 2013/09/18 18:37:10 I would prefer using kSimpleGET_Transaction and a
davidben 2013/09/18 19:35:43 Done.
5892 rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
5893 EXPECT_EQ(net::OK, callback.GetResult(rv));
5894
5895 trans->DoneReading();
5896 // Leave the transaction around.
5897
5898 // Make sure that the ActiveEntry is gone.
5899 base::MessageLoop::current()->RunUntilIdle();
5900
5901 // Read from the cache. This should not deadlock.
5902 RunTransactionTest(cache.http_cache(), kDiscardedResponse_Transaction);
5903
5904 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5905 EXPECT_EQ(1, cache.disk_cache()->open_count());
5906 EXPECT_EQ(1, cache.disk_cache()->create_count());
5907 }
5908
5841 // Tests that we stop caching when told. 5909 // Tests that we stop caching when told.
5842 TEST(HttpCache, StopCachingDeletesEntry) { 5910 TEST(HttpCache, StopCachingDeletesEntry) {
5843 MockHttpCache cache; 5911 MockHttpCache cache;
5844 net::TestCompletionCallback callback; 5912 net::TestCompletionCallback callback;
5845 MockHttpRequest request(kSimpleGET_Transaction); 5913 MockHttpRequest request(kSimpleGET_Transaction);
5846 5914
5847 { 5915 {
5848 scoped_ptr<net::HttpTransaction> trans; 5916 scoped_ptr<net::HttpTransaction> trans;
5849 int rv = cache.http_cache()->CreateTransaction( 5917 int rv = cache.http_cache()->CreateTransaction(
5850 net::DEFAULT_PRIORITY, &trans, NULL); 5918 net::DEFAULT_PRIORITY, &trans, NULL);
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
6162 trans->SetPriority(net::HIGHEST); 6230 trans->SetPriority(net::HIGHEST);
6163 // Should trigger a new network transaction and pick up the new 6231 // Should trigger a new network transaction and pick up the new
6164 // priority. 6232 // priority.
6165 ReadAndVerifyTransaction(trans.get(), transaction); 6233 ReadAndVerifyTransaction(trans.get(), transaction);
6166 6234
6167 EXPECT_EQ(net::HIGHEST, 6235 EXPECT_EQ(net::HIGHEST,
6168 cache.network_layer()->last_create_transaction_priority()); 6236 cache.network_layer()->last_create_transaction_priority());
6169 6237
6170 RemoveMockTransaction(&kRangeGET_TransactionOK); 6238 RemoveMockTransaction(&kRangeGET_TransactionOK);
6171 } 6239 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698