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 6425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6436 base::MessageLoop::current()->RunUntilIdle(); | 6436 base::MessageLoop::current()->RunUntilIdle(); |
6437 | 6437 |
6438 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. | 6438 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. |
6439 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; | 6439 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
6440 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; | 6440 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
6441 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 6441 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); |
6442 EXPECT_EQ(range_response_size * 2, received_bytes); | 6442 EXPECT_EQ(range_response_size * 2, received_bytes); |
6443 | 6443 |
6444 RemoveMockTransaction(&kRangeGET_TransactionOK); | 6444 RemoveMockTransaction(&kRangeGET_TransactionOK); |
6445 } | 6445 } |
6446 | |
6447 static void CheckResourceFreshnessHeader(const net::HttpRequestInfo* request, | |
6448 std::string* response_status, | |
6449 std::string* response_headers, | |
6450 std::string* response_data) { | |
6451 std::string value; | |
6452 EXPECT_TRUE( | |
6453 request->extra_headers.GetHeader("Chromium-Resource-Freshness", &value)); | |
6454 EXPECT_EQ("max-age=3600,stale-while-revalidate=7200,age=10801", value); | |
6455 } | |
6456 | |
6457 // Verify that the Chromium-Resource-Freshness header is sent on a revalidation | |
6458 // if the stale-while-revalidate directive was on the response. | |
6459 // TODO(ricea): Rename this test when a final name for the header is decided. | |
6460 TEST(HttpCache, ResourceFreshnessHeaderSent) { | |
6461 MockHttpCache cache; | |
6462 | |
6463 const MockTransaction kStaleWhileRevalidateTransaction = { | |
rvargas (doing something else)
2014/07/28 19:24:00
It may be more clear to use a predefined transacti
Adam Rice
2014/07/29 17:04:45
Done.
| |
6464 "http://www.example.com/stale-while-revalidate", | |
6465 "GET", | |
6466 base::Time(), | |
6467 "", | |
6468 net::LOAD_NORMAL, | |
6469 "HTTP/1.1 200 OK", | |
6470 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
6471 "Age: 10801\n" | |
6472 "Cache-Control: max-age=3600,stale-while-revalidate=7200\n", | |
6473 base::Time(), | |
6474 "<title>Testing stale-while-revalidate</title>", | |
6475 TEST_MODE_SYNC_NET_START, | |
6476 NULL, | |
6477 0, | |
6478 net::OK}; | |
6479 | |
6480 ScopedMockTransaction stale_while_revalidate_transaction( | |
6481 kStaleWhileRevalidateTransaction); | |
6482 | |
6483 // Write to the cache. | |
6484 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | |
6485 | |
6486 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6487 | |
6488 // Send the request again and check that Chromium-Resource-Freshness header is | |
6489 // added. | |
6490 stale_while_revalidate_transaction.handler = CheckResourceFreshnessHeader; | |
6491 | |
6492 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | |
6493 | |
6494 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
6495 } | |
6496 | |
6497 static void CheckResourceFreshnessAbsent(const net::HttpRequestInfo* request, | |
6498 std::string* response_status, | |
6499 std::string* response_headers, | |
6500 std::string* response_data) { | |
6501 EXPECT_FALSE(request->extra_headers.HasHeader("Chromium-Resource-Freshness")); | |
6502 } | |
6503 | |
6504 // Verify that the Chromium-Resource-Freshness header is not sent when | |
6505 // stale-while-revalidate is 0. | |
6506 TEST(HttpCache, ResourceFreshnessHeaderNotSent) { | |
6507 MockHttpCache cache; | |
6508 | |
6509 const MockTransaction kStaleWhileRevalidateTransaction = { | |
6510 "http://www.example.com/stale-while-revalidate", | |
6511 "GET", | |
6512 base::Time(), | |
6513 "", | |
6514 net::LOAD_NORMAL, | |
6515 "HTTP/1.1 200 OK", | |
6516 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
6517 "Age: 10801\n" | |
6518 "Cache-Control: max-age=3600,stale-while-revalidate=0\n", | |
6519 base::Time(), | |
6520 "<title>Testing stale-while-revalidate</title>", | |
6521 TEST_MODE_SYNC_NET_START, | |
6522 NULL, | |
6523 0, | |
6524 net::OK}; | |
6525 | |
6526 ScopedMockTransaction stale_while_revalidate_transaction( | |
6527 kStaleWhileRevalidateTransaction); | |
6528 | |
6529 // Write to the cache. | |
6530 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | |
6531 | |
6532 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6533 | |
6534 // Send the request again and check that Chromium-Resource-Freshness header is | |
6535 // absent. | |
6536 stale_while_revalidate_transaction.handler = CheckResourceFreshnessAbsent; | |
6537 | |
6538 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | |
6539 | |
6540 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
6541 } | |
OLD | NEW |