| 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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; | 560 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
| 542 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; | 561 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; |
| 543 scoped_refptr<MockQuotaManager> mock_quota_manager_; | 562 scoped_refptr<MockQuotaManager> mock_quota_manager_; |
| 544 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | 563 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; |
| 545 storage::BlobStorageContext* blob_storage_context_; | 564 storage::BlobStorageContext* blob_storage_context_; |
| 546 | 565 |
| 547 scoped_refptr<TestCacheStorageCache> cache_; | 566 scoped_refptr<TestCacheStorageCache> cache_; |
| 548 | 567 |
| 549 ServiceWorkerFetchRequest body_request_; | 568 ServiceWorkerFetchRequest body_request_; |
| 550 ServiceWorkerResponse body_response_; | 569 ServiceWorkerResponse body_response_; |
| 570 ServiceWorkerFetchRequest body_request_with_query_; |
| 571 ServiceWorkerResponse body_response_with_query_; |
| 551 ServiceWorkerFetchRequest no_body_request_; | 572 ServiceWorkerFetchRequest no_body_request_; |
| 552 ServiceWorkerResponse no_body_response_; | 573 ServiceWorkerResponse no_body_response_; |
| 553 scoped_ptr<storage::BlobDataHandle> blob_handle_; | 574 scoped_ptr<storage::BlobDataHandle> blob_handle_; |
| 554 std::string expected_blob_data_; | 575 std::string expected_blob_data_; |
| 555 | 576 |
| 556 CacheStorageError callback_error_ = CACHE_STORAGE_OK; | 577 CacheStorageError callback_error_ = CACHE_STORAGE_OK; |
| 557 scoped_ptr<ServiceWorkerResponse> callback_response_; | 578 scoped_ptr<ServiceWorkerResponse> callback_response_; |
| 558 scoped_ptr<storage::BlobDataHandle> callback_response_data_; | 579 scoped_ptr<storage::BlobDataHandle> callback_response_data_; |
| 559 std::vector<std::string> callback_strings_; | 580 std::vector<std::string> callback_strings_; |
| 560 bool callback_closed_ = false; | 581 bool callback_closed_ = false; |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 762 body_handles->clear(); | 783 body_handles->clear(); |
| 763 | 784 |
| 764 EXPECT_TRUE(Delete(body_request_)); | 785 EXPECT_TRUE(Delete(body_request_)); |
| 765 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | 786 EXPECT_TRUE(MatchAll(&responses, &body_handles)); |
| 766 | 787 |
| 767 ASSERT_EQ(1u, responses->size()); | 788 ASSERT_EQ(1u, responses->size()); |
| 768 EXPECT_TRUE(ResponseMetadataEqual(no_body_response_, responses->at(0))); | 789 EXPECT_TRUE(ResponseMetadataEqual(no_body_response_, responses->at(0))); |
| 769 EXPECT_TRUE(body_handles->empty()); | 790 EXPECT_TRUE(body_handles->empty()); |
| 770 } | 791 } |
| 771 | 792 |
| 793 TEST_P(CacheStorageCacheTestP, MatchAll_IgnoreSearch) { |
| 794 EXPECT_TRUE(Put(body_request_, body_response_)); |
| 795 EXPECT_TRUE(Put(body_request_with_query_, body_response_with_query_)); |
| 796 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
| 797 |
| 798 scoped_ptr<CacheStorageCache::Responses> responses; |
| 799 scoped_ptr<CacheStorageCache::BlobDataHandles> body_handles; |
| 800 CacheStorageCacheQueryParams match_params; |
| 801 match_params.ignore_search = true; |
| 802 EXPECT_TRUE(MatchAll(body_request_, match_params, &responses, &body_handles)); |
| 803 |
| 804 ASSERT_EQ(2u, responses->size()); |
| 805 ASSERT_EQ(2u, body_handles->size()); |
| 806 |
| 807 // Order of returned responses is not guaranteed. |
| 808 std::set<std::string> matched_set; |
| 809 for (const ServiceWorkerResponse& response : *responses) { |
| 810 if (response.url.spec() == "http://example.com/body.html?query=test") { |
| 811 EXPECT_TRUE(ResponseMetadataEqual(body_response_with_query_, response)); |
| 812 matched_set.insert(response.url.spec()); |
| 813 } else if (response.url.spec() == "http://example.com/body.html") { |
| 814 EXPECT_TRUE(ResponseMetadataEqual(body_response_, response)); |
| 815 matched_set.insert(response.url.spec()); |
| 816 } |
| 817 } |
| 818 EXPECT_EQ(2u, matched_set.size()); |
| 819 } |
| 820 |
| 772 TEST_P(CacheStorageCacheTestP, Vary) { | 821 TEST_P(CacheStorageCacheTestP, Vary) { |
| 773 body_request_.headers["vary_foo"] = "foo"; | 822 body_request_.headers["vary_foo"] = "foo"; |
| 774 body_response_.headers["vary"] = "vary_foo"; | 823 body_response_.headers["vary"] = "vary_foo"; |
| 775 EXPECT_TRUE(Put(body_request_, body_response_)); | 824 EXPECT_TRUE(Put(body_request_, body_response_)); |
| 776 EXPECT_TRUE(Match(body_request_)); | 825 EXPECT_TRUE(Match(body_request_)); |
| 777 | 826 |
| 778 body_request_.headers["vary_foo"] = "bar"; | 827 body_request_.headers["vary_foo"] = "bar"; |
| 779 EXPECT_FALSE(Match(body_request_)); | 828 EXPECT_FALSE(Match(body_request_)); |
| 780 | 829 |
| 781 body_request_.headers.erase("vary_foo"); | 830 body_request_.headers.erase("vary_foo"); |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1045 EXPECT_EQ(1, sequence_out); | 1094 EXPECT_EQ(1, sequence_out); |
| 1046 close_loop2->Run(); | 1095 close_loop2->Run(); |
| 1047 EXPECT_EQ(2, sequence_out); | 1096 EXPECT_EQ(2, sequence_out); |
| 1048 } | 1097 } |
| 1049 | 1098 |
| 1050 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, | 1099 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, |
| 1051 CacheStorageCacheTestP, | 1100 CacheStorageCacheTestP, |
| 1052 ::testing::Values(false, true)); | 1101 ::testing::Values(false, true)); |
| 1053 | 1102 |
| 1054 } // namespace content | 1103 } // namespace content |
| OLD | NEW |