OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/cache_storage/cache_storage_cache.h" | 5 #include "content/browser/cache_storage/cache_storage_cache.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 base::RunLoop().RunUntilIdle(); | 299 base::RunLoop().RunUntilIdle(); |
300 } | 300 } |
301 | 301 |
302 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) { | 302 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) { |
303 ServiceWorkerHeaderMap headers; | 303 ServiceWorkerHeaderMap headers; |
304 headers.insert(std::make_pair("a", "a")); | 304 headers.insert(std::make_pair("a", "a")); |
305 headers.insert(std::make_pair("b", "b")); | 305 headers.insert(std::make_pair("b", "b")); |
306 body_request_ = | 306 body_request_ = |
307 ServiceWorkerFetchRequest(GURL("http://example.com/body.html"), "GET", | 307 ServiceWorkerFetchRequest(GURL("http://example.com/body.html"), "GET", |
308 headers, Referrer(), false); | 308 headers, Referrer(), false); |
| 309 body_request_with_query_ = ServiceWorkerFetchRequest( |
| 310 GURL("http://example.com/body.html?query=test"), "GET", headers, |
| 311 Referrer(), false); |
309 no_body_request_ = | 312 no_body_request_ = |
310 ServiceWorkerFetchRequest(GURL("http://example.com/no_body.html"), | 313 ServiceWorkerFetchRequest(GURL("http://example.com/no_body.html"), |
311 "GET", headers, Referrer(), false); | 314 "GET", headers, Referrer(), false); |
312 | 315 |
313 std::string expected_response; | 316 std::string expected_response; |
314 for (int i = 0; i < 100; ++i) | 317 for (int i = 0; i < 100; ++i) |
315 expected_blob_data_ += kTestData; | 318 expected_blob_data_ += kTestData; |
316 | 319 |
317 scoped_ptr<storage::BlobDataBuilder> blob_data( | 320 scoped_ptr<storage::BlobDataBuilder> blob_data( |
318 new storage::BlobDataBuilder("blob-id:myblob")); | 321 new storage::BlobDataBuilder("blob-id:myblob")); |
319 blob_data->AppendData(expected_blob_data_); | 322 blob_data->AppendData(expected_blob_data_); |
320 | 323 |
321 blob_handle_ = | 324 blob_handle_ = |
322 blob_storage_context->context()->AddFinishedBlob(blob_data.get()); | 325 blob_storage_context->context()->AddFinishedBlob(blob_data.get()); |
323 | 326 |
324 body_response_ = ServiceWorkerResponse( | 327 body_response_ = ServiceWorkerResponse( |
325 GURL("http://example.com/body.html"), 200, "OK", | 328 GURL("http://example.com/body.html"), 200, "OK", |
326 blink::WebServiceWorkerResponseTypeDefault, headers, | 329 blink::WebServiceWorkerResponseTypeDefault, headers, |
327 blob_handle_->uuid(), expected_blob_data_.size(), GURL(), | 330 blob_handle_->uuid(), expected_blob_data_.size(), GURL(), |
328 blink::WebServiceWorkerResponseErrorUnknown); | 331 blink::WebServiceWorkerResponseErrorUnknown); |
329 | 332 |
| 333 body_response_with_query_ = ServiceWorkerResponse( |
| 334 GURL("http://example.com/body.html?query=test"), 200, "OK", |
| 335 blink::WebServiceWorkerResponseTypeDefault, headers, |
| 336 blob_handle_->uuid(), expected_blob_data_.size(), GURL(), |
| 337 blink::WebServiceWorkerResponseErrorUnknown); |
| 338 |
330 no_body_response_ = ServiceWorkerResponse( | 339 no_body_response_ = ServiceWorkerResponse( |
331 GURL("http://example.com/no_body.html"), 200, "OK", | 340 GURL("http://example.com/no_body.html"), 200, "OK", |
332 blink::WebServiceWorkerResponseTypeDefault, headers, "", 0, GURL(), | 341 blink::WebServiceWorkerResponseTypeDefault, headers, "", 0, GURL(), |
333 blink::WebServiceWorkerResponseErrorUnknown); | 342 blink::WebServiceWorkerResponseErrorUnknown); |
334 } | 343 } |
335 | 344 |
336 scoped_ptr<ServiceWorkerFetchRequest> CopyFetchRequest( | 345 scoped_ptr<ServiceWorkerFetchRequest> CopyFetchRequest( |
337 const ServiceWorkerFetchRequest& request) { | 346 const ServiceWorkerFetchRequest& request) { |
338 return make_scoped_ptr(new ServiceWorkerFetchRequest( | 347 return make_scoped_ptr(new ServiceWorkerFetchRequest( |
339 request.url, request.method, request.headers, request.referrer, | 348 request.url, request.method, request.headers, request.referrer, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 | 381 |
373 cache_->Match( | 382 cache_->Match( |
374 CopyFetchRequest(request), | 383 CopyFetchRequest(request), |
375 base::Bind(&CacheStorageCacheTest::ResponseAndErrorCallback, | 384 base::Bind(&CacheStorageCacheTest::ResponseAndErrorCallback, |
376 base::Unretained(this), base::Unretained(loop.get()))); | 385 base::Unretained(this), base::Unretained(loop.get()))); |
377 loop->Run(); | 386 loop->Run(); |
378 | 387 |
379 return callback_error_ == CACHE_STORAGE_OK; | 388 return callback_error_ == CACHE_STORAGE_OK; |
380 } | 389 } |
381 | 390 |
| 391 bool MatchAll(const ServiceWorkerFetchRequest& request, |
| 392 const CacheStorageCacheQueryParams& match_params, |
| 393 scoped_ptr<CacheStorageCache::Responses>* responses, |
| 394 scoped_ptr<CacheStorageCache::BlobDataHandles>* body_handles) { |
| 395 base::RunLoop loop; |
| 396 cache_->MatchAll( |
| 397 CopyFetchRequest(request), match_params, |
| 398 base::Bind(&CacheStorageCacheTest::ResponsesAndErrorCallback, |
| 399 base::Unretained(this), loop.QuitClosure(), responses, |
| 400 body_handles)); |
| 401 loop.Run(); |
| 402 return callback_error_ == CACHE_STORAGE_OK; |
| 403 } |
| 404 |
382 bool MatchAll(scoped_ptr<CacheStorageCache::Responses>* responses, | 405 bool MatchAll(scoped_ptr<CacheStorageCache::Responses>* responses, |
383 scoped_ptr<CacheStorageCache::BlobDataHandles>* body_handles) { | 406 scoped_ptr<CacheStorageCache::BlobDataHandles>* body_handles) { |
384 base::RunLoop loop; | 407 return MatchAll(ServiceWorkerFetchRequest(), CacheStorageCacheQueryParams(), |
385 cache_->MatchAll(base::Bind( | 408 responses, body_handles); |
386 &CacheStorageCacheTest::ResponsesAndErrorCallback, | |
387 base::Unretained(this), loop.QuitClosure(), responses, body_handles)); | |
388 loop.Run(); | |
389 return callback_error_ == CACHE_STORAGE_OK; | |
390 } | 409 } |
391 | 410 |
392 bool Delete(const ServiceWorkerFetchRequest& request) { | 411 bool Delete(const ServiceWorkerFetchRequest& request) { |
393 CacheStorageBatchOperation operation; | 412 CacheStorageBatchOperation operation; |
394 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE; | 413 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE; |
395 operation.request = request; | 414 operation.request = request; |
396 | 415 |
397 CacheStorageError error = | 416 CacheStorageError error = |
398 BatchOperation(std::vector<CacheStorageBatchOperation>(1, operation)); | 417 BatchOperation(std::vector<CacheStorageBatchOperation>(1, operation)); |
399 return error == CACHE_STORAGE_OK; | 418 return error == CACHE_STORAGE_OK; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; | 556 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
538 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; | 557 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; |
539 scoped_refptr<MockQuotaManager> mock_quota_manager_; | 558 scoped_refptr<MockQuotaManager> mock_quota_manager_; |
540 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | 559 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; |
541 storage::BlobStorageContext* blob_storage_context_; | 560 storage::BlobStorageContext* blob_storage_context_; |
542 | 561 |
543 scoped_refptr<TestCacheStorageCache> cache_; | 562 scoped_refptr<TestCacheStorageCache> cache_; |
544 | 563 |
545 ServiceWorkerFetchRequest body_request_; | 564 ServiceWorkerFetchRequest body_request_; |
546 ServiceWorkerResponse body_response_; | 565 ServiceWorkerResponse body_response_; |
| 566 ServiceWorkerFetchRequest body_request_with_query_; |
| 567 ServiceWorkerResponse body_response_with_query_; |
547 ServiceWorkerFetchRequest no_body_request_; | 568 ServiceWorkerFetchRequest no_body_request_; |
548 ServiceWorkerResponse no_body_response_; | 569 ServiceWorkerResponse no_body_response_; |
549 scoped_ptr<storage::BlobDataHandle> blob_handle_; | 570 scoped_ptr<storage::BlobDataHandle> blob_handle_; |
550 std::string expected_blob_data_; | 571 std::string expected_blob_data_; |
551 | 572 |
552 CacheStorageError callback_error_ = CACHE_STORAGE_OK; | 573 CacheStorageError callback_error_ = CACHE_STORAGE_OK; |
553 scoped_ptr<ServiceWorkerResponse> callback_response_; | 574 scoped_ptr<ServiceWorkerResponse> callback_response_; |
554 scoped_ptr<storage::BlobDataHandle> callback_response_data_; | 575 scoped_ptr<storage::BlobDataHandle> callback_response_data_; |
555 std::vector<std::string> callback_strings_; | 576 std::vector<std::string> callback_strings_; |
556 bool callback_closed_ = false; | 577 bool callback_closed_ = false; |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
758 body_handles->clear(); | 779 body_handles->clear(); |
759 | 780 |
760 EXPECT_TRUE(Delete(body_request_)); | 781 EXPECT_TRUE(Delete(body_request_)); |
761 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | 782 EXPECT_TRUE(MatchAll(&responses, &body_handles)); |
762 | 783 |
763 ASSERT_EQ(1u, responses->size()); | 784 ASSERT_EQ(1u, responses->size()); |
764 EXPECT_TRUE(ResponseMetadataEqual(no_body_response_, responses->at(0))); | 785 EXPECT_TRUE(ResponseMetadataEqual(no_body_response_, responses->at(0))); |
765 EXPECT_TRUE(body_handles->empty()); | 786 EXPECT_TRUE(body_handles->empty()); |
766 } | 787 } |
767 | 788 |
| 789 TEST_P(CacheStorageCacheTestP, MatchAll_IgnoreSearch) { |
| 790 EXPECT_TRUE(Put(body_request_, body_response_)); |
| 791 EXPECT_TRUE(Put(body_request_with_query_, body_response_with_query_)); |
| 792 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
| 793 |
| 794 scoped_ptr<CacheStorageCache::Responses> responses; |
| 795 scoped_ptr<CacheStorageCache::BlobDataHandles> body_handles; |
| 796 CacheStorageCacheQueryParams match_params; |
| 797 match_params.ignore_search = true; |
| 798 EXPECT_TRUE(MatchAll(body_request_, match_params, &responses, &body_handles)); |
| 799 |
| 800 ASSERT_EQ(2u, responses->size()); |
| 801 ASSERT_EQ(2u, body_handles->size()); |
| 802 |
| 803 // Order of returned responses is not guaranteed. |
| 804 std::set<std::string> matched_set; |
| 805 for (const ServiceWorkerResponse& response : *responses) { |
| 806 if (response.url.spec() == "http://example.com/body.html?query=test") { |
| 807 EXPECT_TRUE(ResponseMetadataEqual(body_response_with_query_, response)); |
| 808 matched_set.insert(response.url.spec()); |
| 809 } else if (response.url.spec() == "http://example.com/body.html") { |
| 810 EXPECT_TRUE(ResponseMetadataEqual(body_response_, response)); |
| 811 matched_set.insert(response.url.spec()); |
| 812 } |
| 813 } |
| 814 EXPECT_EQ(2u, matched_set.size()); |
| 815 } |
| 816 |
768 TEST_P(CacheStorageCacheTestP, Vary) { | 817 TEST_P(CacheStorageCacheTestP, Vary) { |
769 body_request_.headers["vary_foo"] = "foo"; | 818 body_request_.headers["vary_foo"] = "foo"; |
770 body_response_.headers["vary"] = "vary_foo"; | 819 body_response_.headers["vary"] = "vary_foo"; |
771 EXPECT_TRUE(Put(body_request_, body_response_)); | 820 EXPECT_TRUE(Put(body_request_, body_response_)); |
772 EXPECT_TRUE(Match(body_request_)); | 821 EXPECT_TRUE(Match(body_request_)); |
773 | 822 |
774 body_request_.headers["vary_foo"] = "bar"; | 823 body_request_.headers["vary_foo"] = "bar"; |
775 EXPECT_FALSE(Match(body_request_)); | 824 EXPECT_FALSE(Match(body_request_)); |
776 | 825 |
777 body_request_.headers.erase("vary_foo"); | 826 body_request_.headers.erase("vary_foo"); |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1035 EXPECT_EQ(1, sequence_out); | 1084 EXPECT_EQ(1, sequence_out); |
1036 close_loop2->Run(); | 1085 close_loop2->Run(); |
1037 EXPECT_EQ(2, sequence_out); | 1086 EXPECT_EQ(2, sequence_out); |
1038 } | 1087 } |
1039 | 1088 |
1040 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, | 1089 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, |
1041 CacheStorageCacheTestP, | 1090 CacheStorageCacheTestP, |
1042 ::testing::Values(false, true)); | 1091 ::testing::Values(false, true)); |
1043 | 1092 |
1044 } // namespace content | 1093 } // namespace content |
OLD | NEW |