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

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

Issue 339088: Http cache: Always preserve extra headers when dealing with... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « net/http/http_cache.cc ('k') | net/http/partial_data.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2009 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/hash_tables.h" 7 #include "base/hash_tables.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/scoped_vector.h" 9 #include "base/scoped_vector.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 std::string* response_data); 571 std::string* response_data);
572 572
573 private: 573 private:
574 static bool not_modified_; 574 static bool not_modified_;
575 static bool modified_; 575 static bool modified_;
576 DISALLOW_COPY_AND_ASSIGN(RangeTransactionServer); 576 DISALLOW_COPY_AND_ASSIGN(RangeTransactionServer);
577 }; 577 };
578 bool RangeTransactionServer::not_modified_ = false; 578 bool RangeTransactionServer::not_modified_ = false;
579 bool RangeTransactionServer::modified_ = false; 579 bool RangeTransactionServer::modified_ = false;
580 580
581 // A dummy extra header that must be preserved on a given request.
582 // TODO(rvargas): Add tests using this without byte ranges.
583 #define EXTRA_HEADER "Extra: header\r\n"
584
581 // Static. 585 // Static.
582 void RangeTransactionServer::RangeHandler(const net::HttpRequestInfo* request, 586 void RangeTransactionServer::RangeHandler(const net::HttpRequestInfo* request,
583 std::string* response_status, 587 std::string* response_status,
584 std::string* response_headers, 588 std::string* response_headers,
585 std::string* response_data) { 589 std::string* response_data) {
586 if (request->extra_headers.empty()) { 590 if (request->extra_headers.empty()) {
587 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); 591 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable");
588 return; 592 return;
589 } 593 }
590 594
595 // We want to make sure we don't delete extra headers.
596 if (request->extra_headers.find(EXTRA_HEADER) == std::string::npos) {
597 response_status->assign("HTTP/1.1 403 Forbidden");
598 return;
599 }
600
591 if (not_modified_) { 601 if (not_modified_) {
592 response_status->assign("HTTP/1.1 304 Not Modified"); 602 response_status->assign("HTTP/1.1 304 Not Modified");
593 return; 603 return;
594 } 604 }
595 605
596 std::vector<net::HttpByteRange> ranges; 606 std::vector<net::HttpByteRange> ranges;
597 if (!net::HttpUtil::ParseRanges(request->extra_headers, &ranges) || 607 if (!net::HttpUtil::ParseRanges(request->extra_headers, &ranges) ||
598 ranges.size() != 1) 608 ranges.size() != 1)
599 return; 609 return;
600 // We can handle this range request. 610 // We can handle this range request.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
633 } else { 643 } else {
634 response_status->assign("HTTP/1.1 304 Not Modified"); 644 response_status->assign("HTTP/1.1 304 Not Modified");
635 response_data->clear(); 645 response_data->clear();
636 } 646 }
637 } 647 }
638 648
639 const MockTransaction kRangeGET_TransactionOK = { 649 const MockTransaction kRangeGET_TransactionOK = {
640 "http://www.google.com/range", 650 "http://www.google.com/range",
641 "GET", 651 "GET",
642 base::Time(), 652 base::Time(),
643 "Range: bytes = 40-49\r\n", 653 "Range: bytes = 40-49\r\n"
654 EXTRA_HEADER,
644 net::LOAD_NORMAL, 655 net::LOAD_NORMAL,
645 "HTTP/1.1 206 Partial Content", 656 "HTTP/1.1 206 Partial Content",
646 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" 657 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
647 "ETag: \"foo\"\n" 658 "ETag: \"foo\"\n"
648 "Accept-Ranges: bytes\n" 659 "Accept-Ranges: bytes\n"
649 "Content-Length: 10\n", 660 "Content-Length: 10\n",
650 base::Time(), 661 base::Time(),
651 "rg: 40-49 ", 662 "rg: 40-49 ",
652 TEST_MODE_NORMAL, 663 TEST_MODE_NORMAL,
653 &RangeTransactionServer::RangeHandler, 664 &RangeTransactionServer::RangeHandler,
(...skipping 1355 matching lines...) Expand 10 before | Expand all | Expand 10 after
2009 } 2020 }
2010 2021
2011 // Test that we skip the cache for range requests that include a validation 2022 // Test that we skip the cache for range requests that include a validation
2012 // header. 2023 // header.
2013 TEST(HttpCache, RangeGET_SkipsCache2) { 2024 TEST(HttpCache, RangeGET_SkipsCache2) {
2014 MockHttpCache cache; 2025 MockHttpCache cache;
2015 cache.http_cache()->set_enable_range_support(true); 2026 cache.http_cache()->set_enable_range_support(true);
2016 2027
2017 MockTransaction transaction(kRangeGET_Transaction); 2028 MockTransaction transaction(kRangeGET_Transaction);
2018 transaction.request_headers = "If-None-Match: foo\n" 2029 transaction.request_headers = "If-None-Match: foo\n"
2030 EXTRA_HEADER
2019 "Range: bytes = 40-49\n"; 2031 "Range: bytes = 40-49\n";
2020 RunTransactionTest(cache.http_cache(), transaction); 2032 RunTransactionTest(cache.http_cache(), transaction);
2021 2033
2022 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2034 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2023 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2035 EXPECT_EQ(0, cache.disk_cache()->open_count());
2024 EXPECT_EQ(0, cache.disk_cache()->create_count()); 2036 EXPECT_EQ(0, cache.disk_cache()->create_count());
2025 2037
2026 transaction.request_headers = 2038 transaction.request_headers =
2027 "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\n" 2039 "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\n"
2040 EXTRA_HEADER
2028 "Range: bytes = 40-49\n"; 2041 "Range: bytes = 40-49\n";
2029 RunTransactionTest(cache.http_cache(), transaction); 2042 RunTransactionTest(cache.http_cache(), transaction);
2030 2043
2031 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 2044 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2032 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2045 EXPECT_EQ(0, cache.disk_cache()->open_count());
2033 EXPECT_EQ(0, cache.disk_cache()->create_count()); 2046 EXPECT_EQ(0, cache.disk_cache()->create_count());
2034 2047
2035 transaction.request_headers = "If-Range: bla\n" 2048 transaction.request_headers = "If-Range: bla\n"
2049 EXTRA_HEADER
2036 "Range: bytes = 40-49\n"; 2050 "Range: bytes = 40-49\n";
2037 RunTransactionTest(cache.http_cache(), transaction); 2051 RunTransactionTest(cache.http_cache(), transaction);
2038 2052
2039 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 2053 EXPECT_EQ(3, cache.network_layer()->transaction_count());
2040 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2054 EXPECT_EQ(0, cache.disk_cache()->open_count());
2041 EXPECT_EQ(0, cache.disk_cache()->create_count()); 2055 EXPECT_EQ(0, cache.disk_cache()->create_count());
2042 } 2056 }
2043 2057
2044 // Tests that receiving 206 for a regular request is handled correctly. 2058 // Tests that receiving 206 for a regular request is handled correctly.
2045 TEST(HttpCache, GET_Crazy206) { 2059 TEST(HttpCache, GET_Crazy206) {
2046 MockHttpCache cache; 2060 MockHttpCache cache;
2047 cache.http_cache()->set_enable_range_support(true); 2061 cache.http_cache()->set_enable_range_support(true);
2048 2062
2049 // Write to the cache. 2063 // Write to the cache.
2050 MockTransaction transaction(kRangeGET_TransactionOK); 2064 MockTransaction transaction(kRangeGET_TransactionOK);
2051 AddMockTransaction(&transaction); 2065 AddMockTransaction(&transaction);
2052 transaction.request_headers = ""; 2066 transaction.request_headers = EXTRA_HEADER;
2053 transaction.handler = NULL; 2067 transaction.handler = NULL;
2054 RunTransactionTest(cache.http_cache(), transaction); 2068 RunTransactionTest(cache.http_cache(), transaction);
2055 2069
2056 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2070 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2057 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2071 EXPECT_EQ(0, cache.disk_cache()->open_count());
2058 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2072 EXPECT_EQ(1, cache.disk_cache()->create_count());
2059 2073
2060 // This should read again from the net. 2074 // This should read again from the net.
2061 RunTransactionTest(cache.http_cache(), transaction); 2075 RunTransactionTest(cache.http_cache(), transaction);
2062 2076
(...skipping 27 matching lines...) Expand all
2090 EXPECT_TRUE(Verify206Response(headers, 40, 49)); 2104 EXPECT_TRUE(Verify206Response(headers, 40, 49));
2091 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 2105 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2092 EXPECT_EQ(1, cache.disk_cache()->open_count()); 2106 EXPECT_EQ(1, cache.disk_cache()->open_count());
2093 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2107 EXPECT_EQ(1, cache.disk_cache()->create_count());
2094 2108
2095 // Make sure we are done with the previous transaction. 2109 // Make sure we are done with the previous transaction.
2096 MessageLoop::current()->RunAllPending(); 2110 MessageLoop::current()->RunAllPending();
2097 2111
2098 // Write to the cache (30-39). 2112 // Write to the cache (30-39).
2099 MockTransaction transaction(kRangeGET_TransactionOK); 2113 MockTransaction transaction(kRangeGET_TransactionOK);
2100 transaction.request_headers = "Range: bytes = 30-39\r\n"; 2114 transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER;
2101 transaction.data = "rg: 30-39 "; 2115 transaction.data = "rg: 30-39 ";
2102 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 2116 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2103 2117
2104 EXPECT_TRUE(Verify206Response(headers, 30, 39)); 2118 EXPECT_TRUE(Verify206Response(headers, 30, 39));
2105 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 2119 EXPECT_EQ(3, cache.network_layer()->transaction_count());
2106 EXPECT_EQ(2, cache.disk_cache()->open_count()); 2120 EXPECT_EQ(2, cache.disk_cache()->open_count());
2107 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2121 EXPECT_EQ(1, cache.disk_cache()->create_count());
2108 2122
2109 // Make sure we are done with the previous transaction. 2123 // Make sure we are done with the previous transaction.
2110 MessageLoop::current()->RunAllPending(); 2124 MessageLoop::current()->RunAllPending();
2111 2125
2112 // Write and read from the cache (20-59). 2126 // Write and read from the cache (20-59).
2113 transaction.request_headers = "Range: bytes = 20-59\r\n"; 2127 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER;
2114 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; 2128 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 ";
2115 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 2129 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2116 2130
2117 EXPECT_TRUE(Verify206Response(headers, 20, 59)); 2131 EXPECT_TRUE(Verify206Response(headers, 20, 59));
2118 EXPECT_EQ(5, cache.network_layer()->transaction_count()); 2132 EXPECT_EQ(5, cache.network_layer()->transaction_count());
2119 EXPECT_EQ(3, cache.disk_cache()->open_count()); 2133 EXPECT_EQ(3, cache.disk_cache()->open_count());
2120 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2134 EXPECT_EQ(1, cache.disk_cache()->create_count());
2121 2135
2122 RemoveMockTransaction(&kRangeGET_TransactionOK); 2136 RemoveMockTransaction(&kRangeGET_TransactionOK);
2123 } 2137 }
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
2191 // Tests that we can cache range requests when the start or end is unknown. 2205 // Tests that we can cache range requests when the start or end is unknown.
2192 // We start with one suffix request, followed by a request from a given point. 2206 // We start with one suffix request, followed by a request from a given point.
2193 TEST(HttpCache, UnknownRangeGET_1) { 2207 TEST(HttpCache, UnknownRangeGET_1) {
2194 MockHttpCache cache; 2208 MockHttpCache cache;
2195 cache.http_cache()->set_enable_range_support(true); 2209 cache.http_cache()->set_enable_range_support(true);
2196 AddMockTransaction(&kRangeGET_TransactionOK); 2210 AddMockTransaction(&kRangeGET_TransactionOK);
2197 std::string headers; 2211 std::string headers;
2198 2212
2199 // Write to the cache (70-79). 2213 // Write to the cache (70-79).
2200 MockTransaction transaction(kRangeGET_TransactionOK); 2214 MockTransaction transaction(kRangeGET_TransactionOK);
2201 transaction.request_headers = "Range: bytes = -10\r\n"; 2215 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
2202 transaction.data = "rg: 70-79 "; 2216 transaction.data = "rg: 70-79 ";
2203 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 2217 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2204 2218
2205 EXPECT_TRUE(Verify206Response(headers, 70, 79)); 2219 EXPECT_TRUE(Verify206Response(headers, 70, 79));
2206 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2220 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2207 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2221 EXPECT_EQ(0, cache.disk_cache()->open_count());
2208 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2222 EXPECT_EQ(1, cache.disk_cache()->create_count());
2209 2223
2210 // Make sure we are done with the previous transaction. 2224 // Make sure we are done with the previous transaction.
2211 MessageLoop::current()->RunAllPending(); 2225 MessageLoop::current()->RunAllPending();
2212 2226
2213 // Write and read from the cache (60-79). 2227 // Write and read from the cache (60-79).
2214 transaction.request_headers = "Range: bytes = 60-\r\n"; 2228 transaction.request_headers = "Range: bytes = 60-\r\n" EXTRA_HEADER;
2215 transaction.data = "rg: 60-69 rg: 70-79 "; 2229 transaction.data = "rg: 60-69 rg: 70-79 ";
2216 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 2230 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2217 2231
2218 EXPECT_TRUE(Verify206Response(headers, 60, 79)); 2232 EXPECT_TRUE(Verify206Response(headers, 60, 79));
2219 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 2233 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2220 EXPECT_EQ(1, cache.disk_cache()->open_count()); 2234 EXPECT_EQ(1, cache.disk_cache()->open_count());
2221 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2235 EXPECT_EQ(1, cache.disk_cache()->create_count());
2222 2236
2223 RemoveMockTransaction(&kRangeGET_TransactionOK); 2237 RemoveMockTransaction(&kRangeGET_TransactionOK);
2224 } 2238 }
2225 2239
2226 // Tests that we can cache range requests when the start or end is unknown. 2240 // Tests that we can cache range requests when the start or end is unknown.
2227 // We start with one request from a given point, followed by a suffix request. 2241 // We start with one request from a given point, followed by a suffix request.
2228 // We'll also verify that synchronous cache responses work as intended. 2242 // We'll also verify that synchronous cache responses work as intended.
2229 TEST(HttpCache, UnknownRangeGET_2) { 2243 TEST(HttpCache, UnknownRangeGET_2) {
2230 MockHttpCache cache; 2244 MockHttpCache cache;
2231 cache.http_cache()->set_enable_range_support(true); 2245 cache.http_cache()->set_enable_range_support(true);
2232 std::string headers; 2246 std::string headers;
2233 2247
2234 MockTransaction transaction(kRangeGET_TransactionOK); 2248 MockTransaction transaction(kRangeGET_TransactionOK);
2235 transaction.test_mode = TEST_MODE_SYNC_CACHE_START | 2249 transaction.test_mode = TEST_MODE_SYNC_CACHE_START |
2236 TEST_MODE_SYNC_CACHE_READ | 2250 TEST_MODE_SYNC_CACHE_READ |
2237 TEST_MODE_SYNC_CACHE_WRITE; 2251 TEST_MODE_SYNC_CACHE_WRITE;
2238 AddMockTransaction(&transaction); 2252 AddMockTransaction(&transaction);
2239 2253
2240 // Write to the cache (70-79). 2254 // Write to the cache (70-79).
2241 transaction.request_headers = "Range: bytes = 70-\r\n"; 2255 transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER;
2242 transaction.data = "rg: 70-79 "; 2256 transaction.data = "rg: 70-79 ";
2243 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 2257 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2244 2258
2245 EXPECT_TRUE(Verify206Response(headers, 70, 79)); 2259 EXPECT_TRUE(Verify206Response(headers, 70, 79));
2246 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2260 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2247 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2261 EXPECT_EQ(0, cache.disk_cache()->open_count());
2248 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2262 EXPECT_EQ(1, cache.disk_cache()->create_count());
2249 2263
2250 // Make sure we are done with the previous transaction. 2264 // Make sure we are done with the previous transaction.
2251 MessageLoop::current()->RunAllPending(); 2265 MessageLoop::current()->RunAllPending();
2252 2266
2253 // Write and read from the cache (60-79). 2267 // Write and read from the cache (60-79).
2254 transaction.request_headers = "Range: bytes = -20\r\n"; 2268 transaction.request_headers = "Range: bytes = -20\r\n" EXTRA_HEADER;
2255 transaction.data = "rg: 60-69 rg: 70-79 "; 2269 transaction.data = "rg: 60-69 rg: 70-79 ";
2256 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 2270 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2257 2271
2258 EXPECT_TRUE(Verify206Response(headers, 60, 79)); 2272 EXPECT_TRUE(Verify206Response(headers, 60, 79));
2259 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 2273 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2260 EXPECT_EQ(1, cache.disk_cache()->open_count()); 2274 EXPECT_EQ(1, cache.disk_cache()->open_count());
2261 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2275 EXPECT_EQ(1, cache.disk_cache()->create_count());
2262 2276
2263 RemoveMockTransaction(&transaction); 2277 RemoveMockTransaction(&transaction);
2264 } 2278 }
2265 2279
2266 // Tests that receiving Not Modified when asking for an open range doesn't mess 2280 // Tests that receiving Not Modified when asking for an open range doesn't mess
2267 // up things. 2281 // up things.
2268 TEST(HttpCache, UnknownRangeGET_304) { 2282 TEST(HttpCache, UnknownRangeGET_304) {
2269 MockHttpCache cache; 2283 MockHttpCache cache;
2270 cache.http_cache()->set_enable_range_support(true); 2284 cache.http_cache()->set_enable_range_support(true);
2271 std::string headers; 2285 std::string headers;
2272 2286
2273 MockTransaction transaction(kRangeGET_TransactionOK); 2287 MockTransaction transaction(kRangeGET_TransactionOK);
2274 AddMockTransaction(&transaction); 2288 AddMockTransaction(&transaction);
2275 2289
2276 RangeTransactionServer handler; 2290 RangeTransactionServer handler;
2277 handler.set_not_modified(true); 2291 handler.set_not_modified(true);
2278 2292
2279 // Ask for the end of the file, without knowing the length. 2293 // Ask for the end of the file, without knowing the length.
2280 transaction.request_headers = "Range: bytes = 70-\r\n"; 2294 transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER;
2281 transaction.data = "rg: 70-79 "; 2295 transaction.data = "rg: 70-79 ";
2282 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 2296 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2283 2297
2284 // We just bypass the cache. 2298 // We just bypass the cache.
2285 EXPECT_EQ(0U, headers.find("HTTP/1.1 304 Not Modified\n")); 2299 EXPECT_EQ(0U, headers.find("HTTP/1.1 304 Not Modified\n"));
2286 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2300 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2287 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2301 EXPECT_EQ(0, cache.disk_cache()->open_count());
2288 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2302 EXPECT_EQ(1, cache.disk_cache()->create_count());
2289 2303
2290 RunTransactionTest(cache.http_cache(), transaction); 2304 RunTransactionTest(cache.http_cache(), transaction);
(...skipping 13 matching lines...) Expand all
2304 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, 2318 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
2305 &headers); 2319 &headers);
2306 2320
2307 EXPECT_TRUE(Verify206Response(headers, 40, 49)); 2321 EXPECT_TRUE(Verify206Response(headers, 40, 49));
2308 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2322 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2309 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2323 EXPECT_EQ(0, cache.disk_cache()->open_count());
2310 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2324 EXPECT_EQ(1, cache.disk_cache()->create_count());
2311 2325
2312 // Write and read from the cache (0-79), when not asked for a range. 2326 // Write and read from the cache (0-79), when not asked for a range.
2313 MockTransaction transaction(kRangeGET_TransactionOK); 2327 MockTransaction transaction(kRangeGET_TransactionOK);
2314 transaction.request_headers = ""; 2328 transaction.request_headers = EXTRA_HEADER;
2315 transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " 2329 transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
2316 "rg: 50-59 rg: 60-69 rg: 70-79 "; 2330 "rg: 50-59 rg: 60-69 rg: 70-79 ";
2317 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 2331 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2318 2332
2319 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); 2333 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
2320 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 2334 EXPECT_EQ(3, cache.network_layer()->transaction_count());
2321 EXPECT_EQ(1, cache.disk_cache()->open_count()); 2335 EXPECT_EQ(1, cache.disk_cache()->open_count());
2322 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2336 EXPECT_EQ(1, cache.disk_cache()->create_count());
2323 2337
2324 RemoveMockTransaction(&kRangeGET_TransactionOK); 2338 RemoveMockTransaction(&kRangeGET_TransactionOK);
2325 } 2339 }
2326 2340
2327 // Tests that we can handle non-range requests when we have cached the first 2341 // Tests that we can handle non-range requests when we have cached the first
2328 // part of the object and server replies with 304 (Not Modified). 2342 // part of the object and server replies with 304 (Not Modified).
2329 TEST(HttpCache, GET_Previous206_NotModified) { 2343 TEST(HttpCache, GET_Previous206_NotModified) {
2330 MockHttpCache cache; 2344 MockHttpCache cache;
2331 cache.http_cache()->set_enable_range_support(true); 2345 cache.http_cache()->set_enable_range_support(true);
2332 2346
2333 MockTransaction transaction(kRangeGET_TransactionOK); 2347 MockTransaction transaction(kRangeGET_TransactionOK);
2334 transaction.request_headers = "Range: bytes = 0-9\r\n"; 2348 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
2335 transaction.data = "rg: 00-09 "; 2349 transaction.data = "rg: 00-09 ";
2336 AddMockTransaction(&transaction); 2350 AddMockTransaction(&transaction);
2337 std::string headers; 2351 std::string headers;
2338 2352
2339 // Write to the cache (0-9). 2353 // Write to the cache (0-9).
2340 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 2354 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2341 2355
2342 EXPECT_TRUE(Verify206Response(headers, 0, 9)); 2356 EXPECT_TRUE(Verify206Response(headers, 0, 9));
2343 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2357 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2344 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2358 EXPECT_EQ(0, cache.disk_cache()->open_count());
2345 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2359 EXPECT_EQ(1, cache.disk_cache()->create_count());
2346 2360
2347 // Read from the cache (0-9), write and read from cache (10 - 79), 2361 // Read from the cache (0-9), write and read from cache (10 - 79),
2348 MockTransaction transaction2(kRangeGET_TransactionOK); 2362 MockTransaction transaction2(kRangeGET_TransactionOK);
2349 transaction2.request_headers = "Foo: bar\r\n"; 2363 transaction2.request_headers = "Foo: bar\r\n" EXTRA_HEADER;
2350 transaction2.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " 2364 transaction2.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
2351 "rg: 50-59 rg: 60-69 rg: 70-79 "; 2365 "rg: 50-59 rg: 60-69 rg: 70-79 ";
2352 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); 2366 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
2353 2367
2354 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); 2368 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
2355 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 2369 EXPECT_EQ(3, cache.network_layer()->transaction_count());
2356 EXPECT_EQ(1, cache.disk_cache()->open_count()); 2370 EXPECT_EQ(1, cache.disk_cache()->open_count());
2357 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2371 EXPECT_EQ(1, cache.disk_cache()->create_count());
2358 2372
2359 RemoveMockTransaction(&transaction); 2373 RemoveMockTransaction(&transaction);
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
2494 2508
2495 // Tests that we can handle a 200 response when dealing with sparse entries. 2509 // Tests that we can handle a 200 response when dealing with sparse entries.
2496 TEST(HttpCache, RangeRequestResultsIn200) { 2510 TEST(HttpCache, RangeRequestResultsIn200) {
2497 MockHttpCache cache; 2511 MockHttpCache cache;
2498 cache.http_cache()->set_enable_range_support(true); 2512 cache.http_cache()->set_enable_range_support(true);
2499 AddMockTransaction(&kRangeGET_TransactionOK); 2513 AddMockTransaction(&kRangeGET_TransactionOK);
2500 std::string headers; 2514 std::string headers;
2501 2515
2502 // Write to the cache (70-79). 2516 // Write to the cache (70-79).
2503 MockTransaction transaction(kRangeGET_TransactionOK); 2517 MockTransaction transaction(kRangeGET_TransactionOK);
2504 transaction.request_headers = "Range: bytes = -10\r\n"; 2518 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
2505 transaction.data = "rg: 70-79 "; 2519 transaction.data = "rg: 70-79 ";
2506 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 2520 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2507 2521
2508 EXPECT_TRUE(Verify206Response(headers, 70, 79)); 2522 EXPECT_TRUE(Verify206Response(headers, 70, 79));
2509 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2523 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2510 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2524 EXPECT_EQ(0, cache.disk_cache()->open_count());
2511 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2525 EXPECT_EQ(1, cache.disk_cache()->create_count());
2512 2526
2513 // Now we'll issue a request that results in a plain 200 response, but to 2527 // Now we'll issue a request that results in a plain 200 response, but to
2514 // the to the same URL that we used to store sparse data, and making sure 2528 // the to the same URL that we used to store sparse data, and making sure
(...skipping 29 matching lines...) Expand all
2544 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, 2558 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
2545 &headers); 2559 &headers);
2546 2560
2547 EXPECT_TRUE(Verify206Response(headers, 40, 49)); 2561 EXPECT_TRUE(Verify206Response(headers, 40, 49));
2548 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2562 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2549 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2563 EXPECT_EQ(0, cache.disk_cache()->open_count());
2550 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2564 EXPECT_EQ(1, cache.disk_cache()->create_count());
2551 2565
2552 // A weird request should not delete this entry. Ask for bytes 120-. 2566 // A weird request should not delete this entry. Ask for bytes 120-.
2553 MockTransaction transaction(kRangeGET_TransactionOK); 2567 MockTransaction transaction(kRangeGET_TransactionOK);
2554 transaction.request_headers = "Range: bytes = 120-\r\n"; 2568 transaction.request_headers = "Range: bytes = 120-\r\n" EXTRA_HEADER;
2555 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 2569 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2556 2570
2557 EXPECT_EQ(0U, headers.find("HTTP/1.1 416 ")); 2571 EXPECT_EQ(0U, headers.find("HTTP/1.1 416 "));
2558 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 2572 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2559 EXPECT_EQ(1, cache.disk_cache()->open_count()); 2573 EXPECT_EQ(1, cache.disk_cache()->open_count());
2560 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2574 EXPECT_EQ(1, cache.disk_cache()->create_count());
2561 2575
2562 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); 2576 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
2563 EXPECT_EQ(2, cache.disk_cache()->open_count()); 2577 EXPECT_EQ(2, cache.disk_cache()->open_count());
2564 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2578 EXPECT_EQ(1, cache.disk_cache()->create_count());
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
2710 2724
2711 // Tests that if a server tells us conflicting information about a resource we 2725 // Tests that if a server tells us conflicting information about a resource we
2712 // ignore the response. 2726 // ignore the response.
2713 TEST(HttpCache, RangeGET_InvalidResponse3) { 2727 TEST(HttpCache, RangeGET_InvalidResponse3) {
2714 MockHttpCache cache; 2728 MockHttpCache cache;
2715 cache.http_cache()->set_enable_range_support(true); 2729 cache.http_cache()->set_enable_range_support(true);
2716 std::string headers; 2730 std::string headers;
2717 2731
2718 MockTransaction transaction(kRangeGET_TransactionOK); 2732 MockTransaction transaction(kRangeGET_TransactionOK);
2719 transaction.handler = NULL; 2733 transaction.handler = NULL;
2720 transaction.request_headers = "Range: bytes = 50-59\r\n"; 2734 transaction.request_headers = "Range: bytes = 50-59\r\n" EXTRA_HEADER;
2721 std::string response_headers(transaction.response_headers); 2735 std::string response_headers(transaction.response_headers);
2722 response_headers.append("Content-Range: bytes 50-59/160\n"); 2736 response_headers.append("Content-Range: bytes 50-59/160\n");
2723 transaction.response_headers = response_headers.c_str(); 2737 transaction.response_headers = response_headers.c_str();
2724 AddMockTransaction(&transaction); 2738 AddMockTransaction(&transaction);
2725 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 2739 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2726 2740
2727 EXPECT_TRUE(Verify206Response(headers, 50, 59)); 2741 EXPECT_TRUE(Verify206Response(headers, 50, 59));
2728 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2742 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2729 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2743 EXPECT_EQ(0, cache.disk_cache()->open_count());
2730 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2744 EXPECT_EQ(1, cache.disk_cache()->create_count());
(...skipping 26 matching lines...) Expand all
2757 TEST(HttpCache, RangeGET_LargeValues) { 2771 TEST(HttpCache, RangeGET_LargeValues) {
2758 // We need a real sparse cache for this test. 2772 // We need a real sparse cache for this test.
2759 disk_cache::Backend* disk_cache = 2773 disk_cache::Backend* disk_cache =
2760 disk_cache::CreateInMemoryCacheBackend(1024 * 1024); 2774 disk_cache::CreateInMemoryCacheBackend(1024 * 1024);
2761 MockHttpCache cache(disk_cache); 2775 MockHttpCache cache(disk_cache);
2762 cache.http_cache()->set_enable_range_support(true); 2776 cache.http_cache()->set_enable_range_support(true);
2763 std::string headers; 2777 std::string headers;
2764 2778
2765 MockTransaction transaction(kRangeGET_TransactionOK); 2779 MockTransaction transaction(kRangeGET_TransactionOK);
2766 transaction.handler = NULL; 2780 transaction.handler = NULL;
2767 transaction.request_headers = "Range: bytes = 4294967288-4294967297\r\n"; 2781 transaction.request_headers = "Range: bytes = 4294967288-4294967297\r\n"
2782 EXTRA_HEADER;
2768 transaction.response_headers = 2783 transaction.response_headers =
2769 "Content-Range: bytes 4294967288-4294967297/4294967299\n" 2784 "Content-Range: bytes 4294967288-4294967297/4294967299\n"
2770 "Content-Length: 10\n"; 2785 "Content-Length: 10\n";
2771 AddMockTransaction(&transaction); 2786 AddMockTransaction(&transaction);
2772 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 2787 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2773 2788
2774 std::string expected(transaction.status); 2789 std::string expected(transaction.status);
2775 expected.append("\n"); 2790 expected.append("\n");
2776 expected.append(transaction.response_headers); 2791 expected.append(transaction.response_headers);
2777 EXPECT_EQ(expected, headers); 2792 EXPECT_EQ(expected, headers);
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
2961 EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, true)); 2976 EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, true));
2962 2977
2963 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100)); 2978 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100));
2964 int len = static_cast<int>(base::strlcpy(buf->data(), 2979 int len = static_cast<int>(base::strlcpy(buf->data(),
2965 "rg: 00-09 rg: 10-19 ", 100)); 2980 "rg: 00-09 rg: 10-19 ", 100));
2966 EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true)); 2981 EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true));
2967 2982
2968 // Now make a regular request. 2983 // Now make a regular request.
2969 std::string headers; 2984 std::string headers;
2970 MockTransaction transaction(kRangeGET_TransactionOK); 2985 MockTransaction transaction(kRangeGET_TransactionOK);
2971 transaction.request_headers = ""; 2986 transaction.request_headers = EXTRA_HEADER;
2972 transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 " 2987 transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
2973 "rg: 50-59 rg: 60-69 rg: 70-79 "; 2988 "rg: 50-59 rg: 60-69 rg: 70-79 ";
2974 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 2989 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2975 2990
2976 // We update the headers with the ones received while revalidating. 2991 // We update the headers with the ones received while revalidating.
2977 std::string expected_headers( 2992 std::string expected_headers(
2978 "HTTP/1.1 200 OK\n" 2993 "HTTP/1.1 200 OK\n"
2979 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" 2994 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
2980 "Accept-Ranges: bytes\n" 2995 "Accept-Ranges: bytes\n"
2981 "ETag: \"foo\"\n" 2996 "ETag: \"foo\"\n"
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after
3373 std::string headers; 3388 std::string headers;
3374 response.headers->GetNormalizedHeaders(&headers); 3389 response.headers->GetNormalizedHeaders(&headers);
3375 3390
3376 EXPECT_EQ("HTTP/1.1 200 OK\n" 3391 EXPECT_EQ("HTTP/1.1 200 OK\n"
3377 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" 3392 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
3378 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", 3393 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
3379 headers); 3394 headers);
3380 3395
3381 RemoveMockTransaction(&mock_network_response); 3396 RemoveMockTransaction(&mock_network_response);
3382 } 3397 }
OLDNEW
« no previous file with comments | « net/http/http_cache.cc ('k') | net/http/partial_data.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698