| OLD | NEW |
| 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 2269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2280 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | 2280 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2281 | 2281 |
| 2282 EXPECT_TRUE(Verify206Response(headers, 20, 59)); | 2282 EXPECT_TRUE(Verify206Response(headers, 20, 59)); |
| 2283 EXPECT_EQ(5, cache.network_layer()->transaction_count()); | 2283 EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
| 2284 EXPECT_EQ(3, cache.disk_cache()->open_count()); | 2284 EXPECT_EQ(3, cache.disk_cache()->open_count()); |
| 2285 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2285 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2286 | 2286 |
| 2287 RemoveMockTransaction(&kRangeGET_TransactionOK); | 2287 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 2288 } | 2288 } |
| 2289 | 2289 |
| 2290 // Tests that we can cache range requests and fetch random blocks from the |
| 2291 // cache and the network, with synchronous responses. |
| 2292 TEST(HttpCache, RangeGET_SyncOK) { |
| 2293 MockHttpCache cache; |
| 2294 cache.http_cache()->set_enable_range_support(true); |
| 2295 |
| 2296 MockTransaction transaction(kRangeGET_TransactionOK); |
| 2297 transaction.test_mode = TEST_MODE_SYNC_ALL; |
| 2298 AddMockTransaction(&transaction); |
| 2299 |
| 2300 // Write to the cache (40-49). |
| 2301 std::string headers; |
| 2302 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2303 |
| 2304 EXPECT_TRUE(Verify206Response(headers, 40, 49)); |
| 2305 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2306 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2307 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2308 |
| 2309 // Read from the cache (40-49). |
| 2310 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2311 |
| 2312 EXPECT_TRUE(Verify206Response(headers, 40, 49)); |
| 2313 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2314 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2315 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2316 |
| 2317 // Make sure we are done with the previous transaction. |
| 2318 MessageLoop::current()->RunAllPending(); |
| 2319 |
| 2320 // Write to the cache (30-39). |
| 2321 transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; |
| 2322 transaction.data = "rg: 30-39 "; |
| 2323 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2324 |
| 2325 EXPECT_TRUE(Verify206Response(headers, 30, 39)); |
| 2326 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2327 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2328 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2329 |
| 2330 // Make sure we are done with the previous transaction. |
| 2331 MessageLoop::current()->RunAllPending(); |
| 2332 |
| 2333 // Write and read from the cache (20-59). |
| 2334 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
| 2335 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
| 2336 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
| 2337 |
| 2338 EXPECT_TRUE(Verify206Response(headers, 20, 59)); |
| 2339 EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
| 2340 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 2341 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2342 |
| 2343 RemoveMockTransaction(&transaction); |
| 2344 } |
| 2345 |
| 2290 // Tests that we deal with 304s for range requests. | 2346 // Tests that we deal with 304s for range requests. |
| 2291 TEST(HttpCache, RangeGET_304) { | 2347 TEST(HttpCache, RangeGET_304) { |
| 2292 MockHttpCache cache; | 2348 MockHttpCache cache; |
| 2293 cache.http_cache()->set_enable_range_support(true); | 2349 cache.http_cache()->set_enable_range_support(true); |
| 2294 AddMockTransaction(&kRangeGET_TransactionOK); | 2350 AddMockTransaction(&kRangeGET_TransactionOK); |
| 2295 std::string headers; | 2351 std::string headers; |
| 2296 | 2352 |
| 2297 // Write to the cache (40-49). | 2353 // Write to the cache (40-49). |
| 2298 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, | 2354 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 2299 &headers); | 2355 &headers); |
| (...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2830 rv = c->callback.WaitForResult(); | 2886 rv = c->callback.WaitForResult(); |
| 2831 | 2887 |
| 2832 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 2888 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2833 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 2889 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2834 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2890 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2835 | 2891 |
| 2836 // Make sure that we revalidate the entry and read from the cache (a single | 2892 // Make sure that we revalidate the entry and read from the cache (a single |
| 2837 // read will return while waiting for the network). | 2893 // read will return while waiting for the network). |
| 2838 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(5); | 2894 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(5); |
| 2839 rv = c->trans->Read(buf, buf->size(), &c->callback); | 2895 rv = c->trans->Read(buf, buf->size(), &c->callback); |
| 2840 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 2896 EXPECT_EQ(5, c->callback.GetResult(rv)); |
| 2841 rv = c->callback.WaitForResult(); | |
| 2842 rv = c->trans->Read(buf, buf->size(), &c->callback); | 2897 rv = c->trans->Read(buf, buf->size(), &c->callback); |
| 2843 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 2898 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 2844 | 2899 |
| 2845 // Destroy the transaction before completing the read. | 2900 // Destroy the transaction before completing the read. |
| 2846 delete c; | 2901 delete c; |
| 2847 | 2902 |
| 2848 // We have the read and the delete (OnProcessPendingQueue) waiting on the | 2903 // We have the read and the delete (OnProcessPendingQueue) waiting on the |
| 2849 // message loop. This means that a new transaction will just reuse the same | 2904 // message loop. This means that a new transaction will just reuse the same |
| 2850 // active entry (no open or create). | 2905 // active entry (no open or create). |
| 2851 | 2906 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 2876 rv = c->callback.WaitForResult(); | 2931 rv = c->callback.WaitForResult(); |
| 2877 | 2932 |
| 2878 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 2933 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2879 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 2934 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 2880 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2935 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2881 | 2936 |
| 2882 // Make sure that we revalidate the entry and read from the cache (a single | 2937 // Make sure that we revalidate the entry and read from the cache (a single |
| 2883 // read will return while waiting for the network). | 2938 // read will return while waiting for the network). |
| 2884 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(5); | 2939 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(5); |
| 2885 rv = c->trans->Read(buf, buf->size(), &c->callback); | 2940 rv = c->trans->Read(buf, buf->size(), &c->callback); |
| 2886 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 2941 EXPECT_EQ(5, c->callback.GetResult(rv)); |
| 2887 rv = c->callback.WaitForResult(); | |
| 2888 rv = c->trans->Read(buf, buf->size(), &c->callback); | 2942 rv = c->trans->Read(buf, buf->size(), &c->callback); |
| 2889 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 2943 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 2890 | 2944 |
| 2891 // Destroy the transaction before completing the read. | 2945 // Destroy the transaction before completing the read. |
| 2892 delete c; | 2946 delete c; |
| 2893 | 2947 |
| 2894 // We have the read and the delete (OnProcessPendingQueue) waiting on the | 2948 // We have the read and the delete (OnProcessPendingQueue) waiting on the |
| 2895 // message loop. This means that a new transaction will just reuse the same | 2949 // message loop. This means that a new transaction will just reuse the same |
| 2896 // active entry (no open or create). | 2950 // active entry (no open or create). |
| 2897 | 2951 |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3346 EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true)); | 3400 EXPECT_EQ(len, entry->WriteData(1, 0, buf, len, NULL, true)); |
| 3347 | 3401 |
| 3348 // Now make a regular request. | 3402 // Now make a regular request. |
| 3349 MockTransaction transaction(kRangeGET_TransactionOK); | 3403 MockTransaction transaction(kRangeGET_TransactionOK); |
| 3350 transaction.request_headers = EXTRA_HEADER; | 3404 transaction.request_headers = EXTRA_HEADER; |
| 3351 | 3405 |
| 3352 MockHttpRequest request(transaction); | 3406 MockHttpRequest request(transaction); |
| 3353 Context* c = new Context(); | 3407 Context* c = new Context(); |
| 3354 EXPECT_EQ(net::OK, cache.http_cache()->CreateTransaction(&c->trans)); | 3408 EXPECT_EQ(net::OK, cache.http_cache()->CreateTransaction(&c->trans)); |
| 3355 | 3409 |
| 3356 EXPECT_EQ(net::ERR_IO_PENDING, c->trans->Start(&request, &c->callback, NULL)); | 3410 int rv = c->trans->Start(&request, &c->callback, NULL); |
| 3357 EXPECT_EQ(net::OK, c->callback.WaitForResult()); | 3411 EXPECT_EQ(net::OK, c->callback.GetResult(rv)); |
| 3358 | 3412 |
| 3359 // Read 20 bytes from the cache, and 10 from the net. | 3413 // Read 20 bytes from the cache, and 10 from the net. |
| 3360 EXPECT_EQ(net::ERR_IO_PENDING, c->trans->Read(buf, len, &c->callback)); | 3414 rv = c->trans->Read(buf, len, &c->callback); |
| 3361 EXPECT_EQ(len, c->callback.WaitForResult()); | 3415 EXPECT_EQ(len, c->callback.GetResult(rv)); |
| 3362 EXPECT_EQ(net::ERR_IO_PENDING, c->trans->Read(buf, 10, &c->callback)); | 3416 rv = c->trans->Read(buf, 10, &c->callback); |
| 3363 EXPECT_EQ(10, c->callback.WaitForResult()); | 3417 EXPECT_EQ(10, c->callback.GetResult(rv)); |
| 3364 | 3418 |
| 3365 // At this point, we are already reading so canceling the request should leave | 3419 // At this point, we are already reading so canceling the request should leave |
| 3366 // a truncated one. | 3420 // a truncated one. |
| 3367 delete c; | 3421 delete c; |
| 3368 | 3422 |
| 3369 RemoveMockTransaction(&kRangeGET_TransactionOK); | 3423 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 3370 | 3424 |
| 3371 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3425 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3372 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 3426 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 3373 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3427 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3757 std::string headers; | 3811 std::string headers; |
| 3758 response.headers->GetNormalizedHeaders(&headers); | 3812 response.headers->GetNormalizedHeaders(&headers); |
| 3759 | 3813 |
| 3760 EXPECT_EQ("HTTP/1.1 200 OK\n" | 3814 EXPECT_EQ("HTTP/1.1 200 OK\n" |
| 3761 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | 3815 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 3762 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | 3816 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 3763 headers); | 3817 headers); |
| 3764 | 3818 |
| 3765 RemoveMockTransaction(&mock_network_response); | 3819 RemoveMockTransaction(&mock_network_response); |
| 3766 } | 3820 } |
| OLD | NEW |