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

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

Issue 197016: Http Cache: Add support for resuming downloading a... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 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 | 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/string_util.h" 9 #include "base/string_util.h"
10 #include "net/base/net_errors.h" 10 #include "net/base/net_errors.h"
(...skipping 1995 matching lines...) Expand 10 before | Expand all | Expand 10 after
2006 &entry)); 2006 &entry));
2007 2007
2008 std::string raw_headers(kRangeGET_TransactionOK.status); 2008 std::string raw_headers(kRangeGET_TransactionOK.status);
2009 raw_headers.append("\n"); 2009 raw_headers.append("\n");
2010 raw_headers.append(kRangeGET_TransactionOK.response_headers); 2010 raw_headers.append(kRangeGET_TransactionOK.response_headers);
2011 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), 2011 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
2012 raw_headers.size()); 2012 raw_headers.size());
2013 2013
2014 net::HttpResponseInfo response; 2014 net::HttpResponseInfo response;
2015 response.headers = new net::HttpResponseHeaders(raw_headers); 2015 response.headers = new net::HttpResponseHeaders(raw_headers);
2016 net::HttpCache::WriteResponseInfo(entry, &response, true); 2016 EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, false));
2017 2017
2018 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500)); 2018 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500));
2019 int len = static_cast<int>(base::strlcpy(buf->data(), 2019 int len = static_cast<int>(base::strlcpy(buf->data(),
2020 kRangeGET_TransactionOK.data, 500)); 2020 kRangeGET_TransactionOK.data, 500));
2021 EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true)); 2021 EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true));
2022 entry->Close(); 2022 entry->Close();
2023 2023
2024 // Now see that we don't use the stored entry. 2024 // Now see that we don't use the stored entry.
2025 std::string headers; 2025 std::string headers;
2026 RunTransactionTestWithResponse(cache.http_cache(), kSimpleGET_Transaction, 2026 RunTransactionTestWithResponse(cache.http_cache(), kSimpleGET_Transaction,
(...skipping 22 matching lines...) Expand all
2049 &entry)); 2049 &entry));
2050 2050
2051 std::string raw_headers(kRangeGET_TransactionOK.status); 2051 std::string raw_headers(kRangeGET_TransactionOK.status);
2052 raw_headers.append("\n"); 2052 raw_headers.append("\n");
2053 raw_headers.append(kRangeGET_TransactionOK.response_headers); 2053 raw_headers.append(kRangeGET_TransactionOK.response_headers);
2054 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), 2054 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
2055 raw_headers.size()); 2055 raw_headers.size());
2056 2056
2057 net::HttpResponseInfo response; 2057 net::HttpResponseInfo response;
2058 response.headers = new net::HttpResponseHeaders(raw_headers); 2058 response.headers = new net::HttpResponseHeaders(raw_headers);
2059 net::HttpCache::WriteResponseInfo(entry, &response, true); 2059 EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, false));
2060 2060
2061 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500)); 2061 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500));
2062 int len = static_cast<int>(base::strlcpy(buf->data(), 2062 int len = static_cast<int>(base::strlcpy(buf->data(),
2063 kRangeGET_TransactionOK.data, 500)); 2063 kRangeGET_TransactionOK.data, 500));
2064 EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true)); 2064 EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true));
2065 entry->Close(); 2065 entry->Close();
2066 2066
2067 // Now see that we don't use the stored entry. 2067 // Now see that we don't use the stored entry.
2068 std::string headers; 2068 std::string headers;
2069 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, 2069 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
2231 trans.reset(); 2231 trans.reset();
2232 2232
2233 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2233 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2234 EXPECT_EQ(1, cache.disk_cache()->open_count()); 2234 EXPECT_EQ(1, cache.disk_cache()->open_count());
2235 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2235 EXPECT_EQ(1, cache.disk_cache()->create_count());
2236 2236
2237 RemoveMockTransaction(&kRangeGET_TransactionOK); 2237 RemoveMockTransaction(&kRangeGET_TransactionOK);
2238 } 2238 }
2239 #endif 2239 #endif
2240 2240
2241 // Tests the handling of the "truncation" flag.
2242 TEST(HttpCache, WriteResponseInfo_Truncated) {
2243 MockHttpCache cache;
2244 disk_cache::Entry* entry;
2245 ASSERT_TRUE(cache.disk_cache()->CreateEntry("http://www.google.com", &entry));
2246
2247 std::string headers("HTTP/1.1 200 OK");
2248 headers = net::HttpUtil::AssembleRawHeaders(headers.data(), headers.size());
2249 net::HttpResponseInfo response;
2250 response.headers = new net::HttpResponseHeaders(headers);
2251
2252 // Set the last argument for this to be an incomplete request.
2253 EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, true));
2254 bool truncated = false;
2255 EXPECT_TRUE(net::HttpCache::ReadResponseInfo(entry, &response, &truncated));
2256 EXPECT_TRUE(truncated);
2257
2258 // And now test the opposite case.
2259 EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, false));
2260 truncated = true;
2261 EXPECT_TRUE(net::HttpCache::ReadResponseInfo(entry, &response, &truncated));
2262 EXPECT_FALSE(truncated);
2263 entry->Close();
2264 }
2265
2266 // Tests that we delete an entry when the request is cancelled before starting
2267 // to read from the network.
2268 TEST(HttpCache, DoomOnDestruction) {
2269 MockHttpCache cache;
2270 cache.http_cache()->set_enable_range_support(true);
2271
2272 MockHttpRequest request(kSimpleGET_Transaction);
2273
2274 Context* c = new Context(cache.http_cache()->CreateTransaction());
2275
2276 int rv = c->trans->Start(&request, &c->callback, NULL);
2277 if (rv == net::ERR_IO_PENDING)
2278 c->result = c->callback.WaitForResult();
2279
2280 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2281 EXPECT_EQ(0, cache.disk_cache()->open_count());
2282 EXPECT_EQ(1, cache.disk_cache()->create_count());
2283
2284 // Destroy the transaction. We only have the headers so we should delete this
2285 // entry.
2286 delete c;
2287
2288 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
2289
2290 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2291 EXPECT_EQ(0, cache.disk_cache()->open_count());
2292 EXPECT_EQ(2, cache.disk_cache()->create_count());
2293 }
2294
2295 // Tests that we mark an entry as incomplete when the request is cancelled.
2296 TEST(HttpCache, Set_Truncated_Flag) {
2297 MockHttpCache cache;
2298 cache.http_cache()->set_enable_range_support(true);
2299
2300 MockHttpRequest request(kSimpleGET_Transaction);
2301
2302 Context* c = new Context(cache.http_cache()->CreateTransaction());
2303
2304 int rv = c->trans->Start(&request, &c->callback, NULL);
2305 if (rv == net::ERR_IO_PENDING)
2306 rv = c->callback.WaitForResult();
2307
2308 EXPECT_EQ(1, cache.network_layer()->transaction_count());
2309 EXPECT_EQ(0, cache.disk_cache()->open_count());
2310 EXPECT_EQ(1, cache.disk_cache()->create_count());
2311
2312 // Make sure that the entry has some data stored.
2313 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10);
2314 rv = c->trans->Read(buf, buf->size(), &c->callback);
2315 if (rv == net::ERR_IO_PENDING)
2316 rv = c->callback.WaitForResult();
2317 EXPECT_EQ(buf->size(), rv);
2318
2319 // Destroy the transaction.
2320 delete c;
2321
2322 // Verify that the entry is marked as incomplete.
2323 disk_cache::Entry* entry;
2324 ASSERT_TRUE(cache.disk_cache()->OpenEntry(kSimpleGET_Transaction.url,
2325 &entry));
2326 net::HttpResponseInfo response;
2327 bool truncated = false;
2328 EXPECT_TRUE(net::HttpCache::ReadResponseInfo(entry, &response, &truncated));
2329 EXPECT_TRUE(truncated);
2330 entry->Close();
2331 }
2332
2333 // Tests that we can continue with a request that was interrupted.
2334 TEST(HttpCache, GET_IncompleteResource) {
2335 MockHttpCache cache;
2336 cache.http_cache()->set_enable_range_support(true);
2337 AddMockTransaction(&kRangeGET_TransactionOK);
2338
2339 // Create a disk cache entry that stores an incomplete resource.
2340 disk_cache::Entry* entry;
2341 ASSERT_TRUE(cache.disk_cache()->CreateEntry(kRangeGET_TransactionOK.url,
2342 &entry));
2343
2344 // Content-length will be intentionally bogus.
2345 std::string raw_headers("HTTP/1.1 200 OK\n"
2346 "Last-Modified: something\n"
2347 "ETag: \"foo\"\n"
2348 "Accept-Ranges: bytes\n"
2349 "Content-Length: 10\n");
2350 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(),
2351 raw_headers.size());
2352
2353 net::HttpResponseInfo response;
2354 response.headers = new net::HttpResponseHeaders(raw_headers);
2355 // Set the last argument for this to be an incomplete request.
2356 EXPECT_TRUE(net::HttpCache::WriteResponseInfo(entry, &response, true, true));
2357
2358 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100));
2359 int len = static_cast<int>(base::strlcpy(buf->data(),
2360 "rg: 00-09 rg: 10-19 ", 100));
2361 EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true));
2362
2363 // Now make a regular request.
2364 std::string headers;
2365 MockTransaction transaction(kRangeGET_TransactionOK);
2366 transaction.request_headers = "";
2367 transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49 "
2368 "rg: 50-59 rg: 60-69 rg: 70-79 ";
2369 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
2370
2371 // We update the headers with the ones received while revalidating.
2372 std::string expected_headers(
2373 "HTTP/1.1 200 OK\n"
2374 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
2375 "Accept-Ranges: bytes\n"
2376 "ETag: \"foo\"\n"
2377 "Content-Length: 10\n");
2378
2379 EXPECT_EQ(expected_headers, headers);
2380 EXPECT_EQ(2, cache.network_layer()->transaction_count());
2381 EXPECT_EQ(1, cache.disk_cache()->open_count());
2382 EXPECT_EQ(1, cache.disk_cache()->create_count());
2383
2384 RemoveMockTransaction(&kRangeGET_TransactionOK);
2385
2386 // Verify that the disk entry was updated.
2387 EXPECT_EQ(80, entry->GetDataSize(1));
2388 bool truncated = true;
2389 EXPECT_TRUE(net::HttpCache::ReadResponseInfo(entry, &response, &truncated));
2390 EXPECT_FALSE(truncated);
2391 entry->Close();
2392 }
2393
2241 TEST(HttpCache, SyncRead) { 2394 TEST(HttpCache, SyncRead) {
2242 MockHttpCache cache; 2395 MockHttpCache cache;
2243 2396
2244 // This test ensures that a read that completes synchronously does not cause 2397 // This test ensures that a read that completes synchronously does not cause
2245 // any problems. 2398 // any problems.
2246 2399
2247 ScopedMockTransaction transaction(kSimpleGET_Transaction); 2400 ScopedMockTransaction transaction(kSimpleGET_Transaction);
2248 transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START | 2401 transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START |
2249 TEST_MODE_SYNC_CACHE_READ); 2402 TEST_MODE_SYNC_CACHE_READ);
2250 2403
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
2564 std::string headers; 2717 std::string headers;
2565 response.headers->GetNormalizedHeaders(&headers); 2718 response.headers->GetNormalizedHeaders(&headers);
2566 2719
2567 EXPECT_EQ("HTTP/1.1 200 OK\n" 2720 EXPECT_EQ("HTTP/1.1 200 OK\n"
2568 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" 2721 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
2569 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", 2722 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
2570 headers); 2723 headers);
2571 2724
2572 RemoveMockTransaction(&mock_network_response); 2725 RemoveMockTransaction(&mock_network_response);
2573 } 2726 }
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