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

Side by Side Diff: content/browser/cache_storage/cache_storage_cache_unittest.cc

Issue 2516353002: Introduce url_list to the Response scheme of CacheStorage. (Closed)
Patch Set: incorporated falken's comment Created 4 years 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
OLDNEW
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 9
10 #include <memory> 10 #include <memory>
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 } 197 }
198 198
199 bool ResponseMetadataEqual(const ServiceWorkerResponse& expected, 199 bool ResponseMetadataEqual(const ServiceWorkerResponse& expected,
200 const ServiceWorkerResponse& actual) { 200 const ServiceWorkerResponse& actual) {
201 EXPECT_EQ(expected.status_code, actual.status_code); 201 EXPECT_EQ(expected.status_code, actual.status_code);
202 if (expected.status_code != actual.status_code) 202 if (expected.status_code != actual.status_code)
203 return false; 203 return false;
204 EXPECT_EQ(expected.status_text, actual.status_text); 204 EXPECT_EQ(expected.status_text, actual.status_text);
205 if (expected.status_text != actual.status_text) 205 if (expected.status_text != actual.status_text)
206 return false; 206 return false;
207 EXPECT_EQ(expected.url, actual.url); 207 EXPECT_EQ(expected.url_list.size(), actual.url_list.size());
208 if (expected.url != actual.url) 208 if (expected.url_list.size() != actual.url_list.size())
209 return false; 209 return false;
210 for (size_t i = 0; i < expected.url_list.size(); ++i) {
211 EXPECT_EQ(expected.url_list[i], actual.url_list[i]);
212 if (expected.url_list[i] != actual.url_list[i])
213 return false;
214 }
210 EXPECT_EQ(expected.blob_size, actual.blob_size); 215 EXPECT_EQ(expected.blob_size, actual.blob_size);
211 if (expected.blob_size != actual.blob_size) 216 if (expected.blob_size != actual.blob_size)
212 return false; 217 return false;
213 218
214 if (expected.blob_size == 0) { 219 if (expected.blob_size == 0) {
215 EXPECT_STREQ("", actual.blob_uuid.c_str()); 220 EXPECT_STREQ("", actual.blob_uuid.c_str());
216 if (!actual.blob_uuid.empty()) 221 if (!actual.blob_uuid.empty())
217 return false; 222 return false;
218 } else { 223 } else {
219 EXPECT_STRNE("", actual.blob_uuid.c_str()); 224 EXPECT_STRNE("", actual.blob_uuid.c_str());
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 for (int i = 0; i < 100; ++i) 400 for (int i = 0; i < 100; ++i)
396 expected_blob_data_ += kTestData; 401 expected_blob_data_ += kTestData;
397 402
398 std::unique_ptr<storage::BlobDataBuilder> blob_data( 403 std::unique_ptr<storage::BlobDataBuilder> blob_data(
399 new storage::BlobDataBuilder("blob-id:myblob")); 404 new storage::BlobDataBuilder("blob-id:myblob"));
400 blob_data->AppendData(expected_blob_data_); 405 blob_data->AppendData(expected_blob_data_);
401 406
402 blob_handle_ = 407 blob_handle_ =
403 blob_storage_context->context()->AddFinishedBlob(blob_data.get()); 408 blob_storage_context->context()->AddFinishedBlob(blob_data.get());
404 409
405 body_response_ = ServiceWorkerResponse( 410 std::unique_ptr<std::vector<GURL>> body_response_url_list(
406 GURL("http://example.com/body.html"), 200, "OK", 411 base::MakeUnique<std::vector<GURL>>());
falken 2016/12/05 04:32:42 = base::MakeUnique (and all occurrences below)
horo 2016/12/05 04:58:32 Ah, sorry. I didn't save this file correctly :(. D
407 blink::WebServiceWorkerResponseTypeDefault, headers, 412 body_response_url_list->push_back(GURL("http://example.com/body.html"));
falken 2016/12/05 04:32:42 (optional) nit: was actually thinking CreateRespon
horo 2016/12/05 04:58:32 Done.
408 blob_handle_->uuid(), expected_blob_data_.size(), GURL(), 413 body_response_ = CreateResponse(
414 std::move(body_response_url_list),
415 base::MakeUnique<ServiceWorkerHeaderMap>(headers), blob_handle_->uuid(),
416 expected_blob_data_.size(),
417 base::MakeUnique<
418 ServiceWorkerHeaderList>() /* cors_exposed_header_names */);
419
420 std::unique_ptr<std::vector<GURL>> body_response_with_query_url_list(
421 base::MakeUnique<std::vector<GURL>>());
422 body_response_with_query_url_list->push_back(
423 GURL("http://example.com/body.html?query=test"));
424 std::unique_ptr<ServiceWorkerHeaderList> cors_exposed_header_names(
425 base::MakeUnique<ServiceWorkerHeaderList>());
426 cors_exposed_header_names->push_back("a");
427 body_response_with_query_ = CreateResponse(
428 std::move(body_response_with_query_url_list),
429 base::MakeUnique<ServiceWorkerHeaderMap>(headers), blob_handle_->uuid(),
430 expected_blob_data_.size(),
431 std::move(cors_exposed_header_names) /* cors_exposed_header_names */);
432
433 std::unique_ptr<std::vector<GURL>> no_body_response_url_list(
434 base::MakeUnique<std::vector<GURL>>());
435 no_body_response_url_list->push_back(
436 GURL("http://example.com/no_body.html"));
437 no_body_response_ = CreateResponse(
438 std::move(no_body_response_url_list),
439 base::MakeUnique<ServiceWorkerHeaderMap>(headers), "", 0,
440 base::MakeUnique<
441 ServiceWorkerHeaderList>() /* cors_exposed_header_names */);
442 }
443
444 static ServiceWorkerResponse CreateResponse(
445 std::unique_ptr<std::vector<GURL>> url_list,
446 std::unique_ptr<ServiceWorkerHeaderMap> headers,
447 const std::string& blob_uuid,
448 uint64_t blob_size,
449 std::unique_ptr<ServiceWorkerHeaderList> cors_exposed_header_names) {
450 return ServiceWorkerResponse(
451 std::move(url_list), 200, "OK",
452 blink::WebServiceWorkerResponseTypeDefault, std::move(headers),
453 blob_uuid, blob_size, GURL() /* stream_url */,
409 blink::WebServiceWorkerResponseErrorUnknown, base::Time::Now(), 454 blink::WebServiceWorkerResponseErrorUnknown, base::Time::Now(),
410 false /* is_in_cache_storage */, 455 false /* is_in_cache_storage */,
411 std::string() /* cache_storage_cache_name */, 456 std::string() /* cache_storage_cache_name */,
412 ServiceWorkerHeaderList() /* cors_exposed_header_names */); 457 std::move(cors_exposed_header_names));
413
414 body_response_with_query_ = ServiceWorkerResponse(
415 GURL("http://example.com/body.html?query=test"), 200, "OK",
416 blink::WebServiceWorkerResponseTypeDefault, headers,
417 blob_handle_->uuid(), expected_blob_data_.size(), GURL(),
418 blink::WebServiceWorkerResponseErrorUnknown, base::Time::Now(),
419 false /* is_in_cache_storage */,
420 std::string() /* cache_storage_cache_name */,
421 {"a"} /* cors_exposed_header_names */);
422
423 no_body_response_ = ServiceWorkerResponse(
424 GURL("http://example.com/no_body.html"), 200, "OK",
425 blink::WebServiceWorkerResponseTypeDefault, headers, "", 0, GURL(),
426 blink::WebServiceWorkerResponseErrorUnknown, base::Time::Now(),
427 false /* is_in_cache_storage */,
428 std::string() /* cache_storage_cache_name */,
429 ServiceWorkerHeaderList() /* cors_exposed_header_names */);
430 } 458 }
431 459
432 std::unique_ptr<ServiceWorkerFetchRequest> CopyFetchRequest( 460 std::unique_ptr<ServiceWorkerFetchRequest> CopyFetchRequest(
433 const ServiceWorkerFetchRequest& request) { 461 const ServiceWorkerFetchRequest& request) {
434 return base::MakeUnique<ServiceWorkerFetchRequest>( 462 return base::MakeUnique<ServiceWorkerFetchRequest>(
435 request.url, request.method, request.headers, request.referrer, 463 request.url, request.method, request.headers, request.referrer,
436 request.is_reload); 464 request.is_reload);
437 } 465 }
438 466
439 CacheStorageError BatchOperation( 467 CacheStorageError BatchOperation(
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 TEST_P(CacheStorageCacheTestP, PutBody) { 741 TEST_P(CacheStorageCacheTestP, PutBody) {
714 EXPECT_TRUE(Put(body_request_, body_response_)); 742 EXPECT_TRUE(Put(body_request_, body_response_));
715 } 743 }
716 744
717 TEST_P(CacheStorageCacheTestP, PutBody_Multiple) { 745 TEST_P(CacheStorageCacheTestP, PutBody_Multiple) {
718 CacheStorageBatchOperation operation1; 746 CacheStorageBatchOperation operation1;
719 operation1.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; 747 operation1.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT;
720 operation1.request = body_request_; 748 operation1.request = body_request_;
721 operation1.request.url = GURL("http://example.com/1"); 749 operation1.request.url = GURL("http://example.com/1");
722 operation1.response = body_response_; 750 operation1.response = body_response_;
723 operation1.response.url = GURL("http://example.com/1"); 751 operation1.response.url_list.push_back(GURL("http://example.com/1"));
724 752
725 CacheStorageBatchOperation operation2; 753 CacheStorageBatchOperation operation2;
726 operation2.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; 754 operation2.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT;
727 operation2.request = body_request_; 755 operation2.request = body_request_;
728 operation2.request.url = GURL("http://example.com/2"); 756 operation2.request.url = GURL("http://example.com/2");
729 operation2.response = body_response_; 757 operation2.response = body_response_;
730 operation2.response.url = GURL("http://example.com/2"); 758 operation2.response.url_list.push_back(GURL("http://example.com/2"));
731 759
732 CacheStorageBatchOperation operation3; 760 CacheStorageBatchOperation operation3;
733 operation3.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; 761 operation3.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT;
734 operation3.request = body_request_; 762 operation3.request = body_request_;
735 operation3.request.url = GURL("http://example.com/3"); 763 operation3.request.url = GURL("http://example.com/3");
736 operation3.response = body_response_; 764 operation3.response = body_response_;
737 operation3.response.url = GURL("http://example.com/3"); 765 operation3.response.url_list.push_back(GURL("http://example.com/3"));
738 766
739 std::vector<CacheStorageBatchOperation> operations; 767 std::vector<CacheStorageBatchOperation> operations;
740 operations.push_back(operation1); 768 operations.push_back(operation1);
741 operations.push_back(operation2); 769 operations.push_back(operation2);
742 operations.push_back(operation3); 770 operations.push_back(operation3);
743 771
744 EXPECT_EQ(CACHE_STORAGE_OK, BatchOperation(operations)); 772 EXPECT_EQ(CACHE_STORAGE_OK, BatchOperation(operations));
745 EXPECT_TRUE(Match(operation1.request)); 773 EXPECT_TRUE(Match(operation1.request));
746 EXPECT_TRUE(Match(operation2.request)); 774 EXPECT_TRUE(Match(operation2.request));
747 EXPECT_TRUE(Match(operation3.request)); 775 EXPECT_TRUE(Match(operation3.request));
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 EXPECT_FALSE(Keys()); 837 EXPECT_FALSE(Keys());
810 EXPECT_EQ(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, callback_error_); 838 EXPECT_EQ(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, callback_error_);
811 } 839 }
812 840
813 // TODO(nhiroki): Add a test for the case where one of PUT operations fails. 841 // TODO(nhiroki): Add a test for the case where one of PUT operations fails.
814 // Currently there is no handy way to fail only one operation in a batch. 842 // Currently there is no handy way to fail only one operation in a batch.
815 // This could be easily achieved after adding some security checks in the 843 // This could be easily achieved after adding some security checks in the
816 // browser side (http://crbug.com/425505). 844 // browser side (http://crbug.com/425505).
817 845
818 TEST_P(CacheStorageCacheTestP, ResponseURLDiffersFromRequestURL) { 846 TEST_P(CacheStorageCacheTestP, ResponseURLDiffersFromRequestURL) {
819 no_body_response_.url = GURL("http://example.com/foobar"); 847 no_body_response_.url_list.clear();
848 no_body_response_.url_list.push_back(GURL("http://example.com/foobar"));
820 EXPECT_STRNE("http://example.com/foobar", 849 EXPECT_STRNE("http://example.com/foobar",
821 no_body_request_.url.spec().c_str()); 850 no_body_request_.url.spec().c_str());
822 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); 851 EXPECT_TRUE(Put(no_body_request_, no_body_response_));
823 EXPECT_TRUE(Match(no_body_request_)); 852 EXPECT_TRUE(Match(no_body_request_));
853 ASSERT_EQ(1u, callback_response_->url_list.size());
824 EXPECT_STREQ("http://example.com/foobar", 854 EXPECT_STREQ("http://example.com/foobar",
825 callback_response_->url.spec().c_str()); 855 callback_response_->url_list[0].spec().c_str());
826 } 856 }
827 857
828 TEST_P(CacheStorageCacheTestP, ResponseURLEmpty) { 858 TEST_P(CacheStorageCacheTestP, ResponseURLEmpty) {
829 no_body_response_.url = GURL(); 859 no_body_response_.url_list.clear();
830 EXPECT_STRNE("", no_body_request_.url.spec().c_str()); 860 EXPECT_STRNE("", no_body_request_.url.spec().c_str());
831 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); 861 EXPECT_TRUE(Put(no_body_request_, no_body_response_));
832 EXPECT_TRUE(Match(no_body_request_)); 862 EXPECT_TRUE(Match(no_body_request_));
833 EXPECT_STREQ("", callback_response_->url.spec().c_str()); 863 EXPECT_EQ(0u, callback_response_->url_list.size());
834 } 864 }
835 865
836 TEST_F(CacheStorageCacheTest, PutBodyDropBlobRef) { 866 TEST_F(CacheStorageCacheTest, PutBodyDropBlobRef) {
837 CacheStorageBatchOperation operation; 867 CacheStorageBatchOperation operation;
838 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; 868 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT;
839 operation.request = body_request_; 869 operation.request = body_request_;
840 operation.response = body_response_; 870 operation.response = body_response_;
841 871
842 std::unique_ptr<base::RunLoop> loop(new base::RunLoop()); 872 std::unique_ptr<base::RunLoop> loop(new base::RunLoop());
843 cache_->BatchOperation( 873 cache_->BatchOperation(
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
1133 CacheStorageCacheQueryParams match_params; 1163 CacheStorageCacheQueryParams match_params;
1134 match_params.ignore_search = true; 1164 match_params.ignore_search = true;
1135 EXPECT_TRUE(MatchAll(body_request_, match_params, &responses, &body_handles)); 1165 EXPECT_TRUE(MatchAll(body_request_, match_params, &responses, &body_handles));
1136 1166
1137 ASSERT_EQ(2u, responses->size()); 1167 ASSERT_EQ(2u, responses->size());
1138 ASSERT_EQ(2u, body_handles->size()); 1168 ASSERT_EQ(2u, body_handles->size());
1139 1169
1140 // Order of returned responses is not guaranteed. 1170 // Order of returned responses is not guaranteed.
1141 std::set<std::string> matched_set; 1171 std::set<std::string> matched_set;
1142 for (const ServiceWorkerResponse& response : *responses) { 1172 for (const ServiceWorkerResponse& response : *responses) {
1143 if (response.url.spec() == "http://example.com/body.html?query=test") { 1173 ASSERT_EQ(1u, response.url_list.size());
1174 if (response.url_list[0].spec() ==
1175 "http://example.com/body.html?query=test") {
1144 EXPECT_TRUE(ResponseMetadataEqual(SetCacheName(body_response_with_query_), 1176 EXPECT_TRUE(ResponseMetadataEqual(SetCacheName(body_response_with_query_),
1145 response)); 1177 response));
1146 matched_set.insert(response.url.spec()); 1178 matched_set.insert(response.url_list[0].spec());
1147 } else if (response.url.spec() == "http://example.com/body.html") { 1179 } else if (response.url_list[0].spec() == "http://example.com/body.html") {
1148 EXPECT_TRUE( 1180 EXPECT_TRUE(
1149 ResponseMetadataEqual(SetCacheName(body_response_), response)); 1181 ResponseMetadataEqual(SetCacheName(body_response_), response));
1150 matched_set.insert(response.url.spec()); 1182 matched_set.insert(response.url_list[0].spec());
1151 } 1183 }
1152 } 1184 }
1153 EXPECT_EQ(2u, matched_set.size()); 1185 EXPECT_EQ(2u, matched_set.size());
1154 } 1186 }
1155 1187
1156 TEST_P(CacheStorageCacheTestP, MatchAll_Head) { 1188 TEST_P(CacheStorageCacheTestP, MatchAll_Head) {
1157 EXPECT_TRUE(Put(body_request_, body_response_)); 1189 EXPECT_TRUE(Put(body_request_, body_response_));
1158 1190
1159 std::unique_ptr<CacheStorageCache::Responses> responses; 1191 std::unique_ptr<CacheStorageCache::Responses> responses;
1160 std::unique_ptr<CacheStorageCache::BlobDataHandles> body_handles; 1192 std::unique_ptr<CacheStorageCache::BlobDataHandles> body_handles;
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
1484 memset(buffer->data(), 0, kSize); 1516 memset(buffer->data(), 0, kSize);
1485 EXPECT_FALSE(WriteSideData(GURL("http://www.example.com/not_exist"), 1517 EXPECT_FALSE(WriteSideData(GURL("http://www.example.com/not_exist"),
1486 base::Time::Now(), buffer, kSize)); 1518 base::Time::Now(), buffer, kSize));
1487 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); 1519 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_);
1488 } 1520 }
1489 1521
1490 TEST_F(CacheStorageCacheTest, CaselessServiceWorkerResponseHeaders) { 1522 TEST_F(CacheStorageCacheTest, CaselessServiceWorkerResponseHeaders) {
1491 // CacheStorageCache depends on ServiceWorkerResponse having caseless 1523 // CacheStorageCache depends on ServiceWorkerResponse having caseless
1492 // headers so that it can quickly lookup vary headers. 1524 // headers so that it can quickly lookup vary headers.
1493 ServiceWorkerResponse response( 1525 ServiceWorkerResponse response(
1494 GURL("http://www.example.com"), 200, "OK", 1526 base::MakeUnique<std::vector<GURL>>(), 200, "OK",
1495 blink::WebServiceWorkerResponseTypeDefault, ServiceWorkerHeaderMap(), "", 1527 blink::WebServiceWorkerResponseTypeDefault,
1496 0, GURL(), blink::WebServiceWorkerResponseErrorUnknown, base::Time(), 1528 base::MakeUnique<ServiceWorkerHeaderMap>(), "", 0, GURL(),
1529 blink::WebServiceWorkerResponseErrorUnknown, base::Time(),
1497 false /* is_in_cache_storage */, 1530 false /* is_in_cache_storage */,
1498 std::string() /* cache_storage_cache_name */, 1531 std::string() /* cache_storage_cache_name */,
1499 ServiceWorkerHeaderList() /* cors_exposed_header_names */); 1532 base::MakeUnique<
1533 ServiceWorkerHeaderList>() /* cors_exposed_header_names */);
1500 response.headers["content-type"] = "foo"; 1534 response.headers["content-type"] = "foo";
1501 response.headers["Content-Type"] = "bar"; 1535 response.headers["Content-Type"] = "bar";
1502 EXPECT_EQ("bar", response.headers["content-type"]); 1536 EXPECT_EQ("bar", response.headers["content-type"]);
1503 } 1537 }
1504 1538
1505 TEST_F(CacheStorageCacheTest, CaselessServiceWorkerFetchRequestHeaders) { 1539 TEST_F(CacheStorageCacheTest, CaselessServiceWorkerFetchRequestHeaders) {
1506 // CacheStorageCache depends on ServiceWorkerFetchRequest having caseless 1540 // CacheStorageCache depends on ServiceWorkerFetchRequest having caseless
1507 // headers so that it can quickly lookup vary headers. 1541 // headers so that it can quickly lookup vary headers.
1508 ServiceWorkerFetchRequest request(GURL("http://www.example.com"), "GET", 1542 ServiceWorkerFetchRequest request(GURL("http://www.example.com"), "GET",
1509 ServiceWorkerHeaderMap(), Referrer(), 1543 ServiceWorkerHeaderMap(), Referrer(),
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
1624 EXPECT_EQ(1, sequence_out); 1658 EXPECT_EQ(1, sequence_out);
1625 close_loop2->Run(); 1659 close_loop2->Run();
1626 EXPECT_EQ(2, sequence_out); 1660 EXPECT_EQ(2, sequence_out);
1627 } 1661 }
1628 1662
1629 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, 1663 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest,
1630 CacheStorageCacheTestP, 1664 CacheStorageCacheTestP,
1631 ::testing::Values(false, true)); 1665 ::testing::Values(false, true));
1632 1666
1633 } // namespace content 1667 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698