| 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/service_worker/service_worker_cache.h" | 5 #include "content/browser/service_worker/service_worker_cache.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 for (int i = 0; i < 100; ++i) | 98 for (int i = 0; i < 100; ++i) |
| 99 expected_blob_data_ += kTestData; | 99 expected_blob_data_ += kTestData; |
| 100 | 100 |
| 101 scoped_refptr<storage::BlobData> blob_data( | 101 scoped_refptr<storage::BlobData> blob_data( |
| 102 new storage::BlobData("blob-id:myblob")); | 102 new storage::BlobData("blob-id:myblob")); |
| 103 blob_data->AppendData(expected_blob_data_); | 103 blob_data->AppendData(expected_blob_data_); |
| 104 | 104 |
| 105 blob_handle_ = | 105 blob_handle_ = |
| 106 blob_storage_context->context()->AddFinishedBlob(blob_data.get()); | 106 blob_storage_context->context()->AddFinishedBlob(blob_data.get()); |
| 107 | 107 |
| 108 body_response_ = ServiceWorkerResponse(GURL("http://example.com/body.html"), | 108 body_response_ = |
| 109 200, | 109 ServiceWorkerResponse(GURL("http://example.com/body.html"), |
| 110 "OK", | 110 200, |
| 111 headers, | 111 "OK", |
| 112 blob_handle_->uuid()); | 112 blink::WebServiceWorkerResponseTypeDefault, |
| 113 headers, |
| 114 blob_handle_->uuid()); |
| 113 | 115 |
| 114 no_body_response_ = ServiceWorkerResponse( | 116 no_body_response_ = |
| 115 GURL("http://example.com/no_body.html"), 200, "OK", headers, ""); | 117 ServiceWorkerResponse(GURL("http://example.com/no_body.html"), |
| 118 200, |
| 119 "OK", |
| 120 blink::WebServiceWorkerResponseTypeDefault, |
| 121 headers, |
| 122 ""); |
| 116 } | 123 } |
| 117 | 124 |
| 118 scoped_ptr<ServiceWorkerFetchRequest> CopyFetchRequest( | 125 scoped_ptr<ServiceWorkerFetchRequest> CopyFetchRequest( |
| 119 const ServiceWorkerFetchRequest& request) { | 126 const ServiceWorkerFetchRequest& request) { |
| 120 return make_scoped_ptr(new ServiceWorkerFetchRequest(request.url, | 127 return make_scoped_ptr(new ServiceWorkerFetchRequest(request.url, |
| 121 request.method, | 128 request.method, |
| 122 request.headers, | 129 request.headers, |
| 123 request.referrer, | 130 request.referrer, |
| 124 request.is_reload)); | 131 request.is_reload)); |
| 125 } | 132 } |
| 126 | 133 |
| 127 scoped_ptr<ServiceWorkerResponse> CopyFetchResponse( | 134 scoped_ptr<ServiceWorkerResponse> CopyFetchResponse( |
| 128 const ServiceWorkerResponse& response) { | 135 const ServiceWorkerResponse& response) { |
| 129 return make_scoped_ptr(new ServiceWorkerResponse(response.url, | 136 scoped_ptr<ServiceWorkerResponse> sw_response( |
| 130 response.status_code, | 137 new ServiceWorkerResponse(response.url, |
| 131 response.status_text, | 138 response.status_code, |
| 132 response.headers, | 139 response.status_text, |
| 133 response.blob_uuid)); | 140 response.response_type, |
| 141 response.headers, |
| 142 response.blob_uuid)); |
| 143 return sw_response.Pass(); |
| 134 } | 144 } |
| 135 | 145 |
| 136 bool Put(const ServiceWorkerFetchRequest& request, | 146 bool Put(const ServiceWorkerFetchRequest& request, |
| 137 const ServiceWorkerResponse& response) { | 147 const ServiceWorkerResponse& response) { |
| 138 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 148 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
| 139 | 149 |
| 140 cache_->Put(CopyFetchRequest(request), | 150 cache_->Put(CopyFetchRequest(request), |
| 141 CopyFetchResponse(response), | 151 CopyFetchResponse(response), |
| 142 base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback, | 152 base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback, |
| 143 base::Unretained(this), | 153 base::Unretained(this), |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 for (int i = 0, max = callback_strings_.size(); i < max; ++i) | 242 for (int i = 0, max = callback_strings_.size(); i < max; ++i) |
| 233 found_set.insert(callback_strings_[i]); | 243 found_set.insert(callback_strings_[i]); |
| 234 | 244 |
| 235 for (int i = 0, max = expected_keys.size(); i < max; ++i) { | 245 for (int i = 0, max = expected_keys.size(); i < max; ++i) { |
| 236 if (found_set.find(expected_keys[i]) == found_set.end()) | 246 if (found_set.find(expected_keys[i]) == found_set.end()) |
| 237 return false; | 247 return false; |
| 238 } | 248 } |
| 239 return true; | 249 return true; |
| 240 } | 250 } |
| 241 | 251 |
| 252 bool TestResponseType(blink::WebServiceWorkerResponseType response_type) { |
| 253 body_response_.response_type = response_type; |
| 254 EXPECT_TRUE(Put(body_request_, body_response_)); |
| 255 EXPECT_TRUE(Match(body_request_)); |
| 256 EXPECT_TRUE(Delete(body_request_)); |
| 257 return response_type == callback_response_->response_type; |
| 258 } |
| 259 |
| 242 virtual bool MemoryOnly() { return false; } | 260 virtual bool MemoryOnly() { return false; } |
| 243 | 261 |
| 244 protected: | 262 protected: |
| 245 TestBrowserContext browser_context_; | 263 TestBrowserContext browser_context_; |
| 246 TestBrowserThreadBundle browser_thread_bundle_; | 264 TestBrowserThreadBundle browser_thread_bundle_; |
| 247 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; | 265 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
| 248 storage::BlobStorageContext* blob_storage_context_; | 266 storage::BlobStorageContext* blob_storage_context_; |
| 249 | 267 |
| 250 base::ScopedTempDir temp_dir_; | 268 base::ScopedTempDir temp_dir_; |
| 251 scoped_refptr<ServiceWorkerCache> cache_; | 269 scoped_refptr<ServiceWorkerCache> cache_; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 280 EXPECT_TRUE(Put(body_request_, body_response_)); | 298 EXPECT_TRUE(Put(body_request_, body_response_)); |
| 281 EXPECT_TRUE(callback_response_); | 299 EXPECT_TRUE(callback_response_); |
| 282 EXPECT_STREQ(body_response_.url.spec().c_str(), | 300 EXPECT_STREQ(body_response_.url.spec().c_str(), |
| 283 callback_response_->url.spec().c_str()); | 301 callback_response_->url.spec().c_str()); |
| 284 EXPECT_TRUE(callback_response_data_); | 302 EXPECT_TRUE(callback_response_data_); |
| 285 std::string response_body; | 303 std::string response_body; |
| 286 CopyBody(callback_response_data_.get(), &response_body); | 304 CopyBody(callback_response_data_.get(), &response_body); |
| 287 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); | 305 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); |
| 288 } | 306 } |
| 289 | 307 |
| 308 TEST_P(ServiceWorkerCacheTestP, PutResponseType) { |
| 309 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeBasic)); |
| 310 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeCORS)); |
| 311 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeDefault)); |
| 312 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeError)); |
| 313 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeOpaque)); |
| 314 } |
| 315 |
| 290 TEST_F(ServiceWorkerCacheTest, PutBodyDropBlobRef) { | 316 TEST_F(ServiceWorkerCacheTest, PutBodyDropBlobRef) { |
| 291 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 317 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
| 292 cache_->Put(CopyFetchRequest(body_request_), | 318 cache_->Put(CopyFetchRequest(body_request_), |
| 293 CopyFetchResponse(body_response_), | 319 CopyFetchResponse(body_response_), |
| 294 base::Bind(&ServiceWorkerCacheTestP::ResponseAndErrorCallback, | 320 base::Bind(&ServiceWorkerCacheTestP::ResponseAndErrorCallback, |
| 295 base::Unretained(this), | 321 base::Unretained(this), |
| 296 base::Unretained(loop.get()))); | 322 base::Unretained(loop.get()))); |
| 297 // The handle should be held by the cache now so the deref here should be | 323 // The handle should be held by the cache now so the deref here should be |
| 298 // okay. | 324 // okay. |
| 299 blob_handle_.reset(); | 325 blob_handle_.reset(); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 ASSERT_TRUE(Put(body_request_, body_response_)); | 484 ASSERT_TRUE(Put(body_request_, body_response_)); |
| 459 ASSERT_TRUE(Match(body_request_)); | 485 ASSERT_TRUE(Match(body_request_)); |
| 460 ASSERT_TRUE(Delete(body_request_)); | 486 ASSERT_TRUE(Delete(body_request_)); |
| 461 } | 487 } |
| 462 } | 488 } |
| 463 #endif // OS_WIN | 489 #endif // OS_WIN |
| 464 | 490 |
| 465 TEST_F(ServiceWorkerCacheTest, CaselessServiceWorkerResponseHeaders) { | 491 TEST_F(ServiceWorkerCacheTest, CaselessServiceWorkerResponseHeaders) { |
| 466 // ServiceWorkerCache depends on ServiceWorkerResponse having caseless | 492 // ServiceWorkerCache depends on ServiceWorkerResponse having caseless |
| 467 // headers so that it can quickly lookup vary headers. | 493 // headers so that it can quickly lookup vary headers. |
| 468 ServiceWorkerResponse response( | 494 ServiceWorkerResponse response(GURL("http://www.example.com"), |
| 469 GURL("http://www.example.com"), 200, "OK", ServiceWorkerHeaderMap(), ""); | 495 200, |
| 496 "OK", |
| 497 blink::WebServiceWorkerResponseTypeDefault, |
| 498 ServiceWorkerHeaderMap(), |
| 499 ""); |
| 470 response.headers["content-type"] = "foo"; | 500 response.headers["content-type"] = "foo"; |
| 471 response.headers["Content-Type"] = "bar"; | 501 response.headers["Content-Type"] = "bar"; |
| 472 EXPECT_EQ("bar", response.headers["content-type"]); | 502 EXPECT_EQ("bar", response.headers["content-type"]); |
| 473 } | 503 } |
| 474 | 504 |
| 475 TEST_F(ServiceWorkerCacheTest, CaselessServiceWorkerFetchRequestHeaders) { | 505 TEST_F(ServiceWorkerCacheTest, CaselessServiceWorkerFetchRequestHeaders) { |
| 476 // ServiceWorkerCache depends on ServiceWorkerFetchRequest having caseless | 506 // ServiceWorkerCache depends on ServiceWorkerFetchRequest having caseless |
| 477 // headers so that it can quickly lookup vary headers. | 507 // headers so that it can quickly lookup vary headers. |
| 478 ServiceWorkerFetchRequest request(GURL("http://www.example.com"), | 508 ServiceWorkerFetchRequest request(GURL("http://www.example.com"), |
| 479 "GET", | 509 "GET", |
| 480 ServiceWorkerHeaderMap(), | 510 ServiceWorkerHeaderMap(), |
| 481 GURL(""), | 511 GURL(""), |
| 482 false); | 512 false); |
| 483 request.headers["content-type"] = "foo"; | 513 request.headers["content-type"] = "foo"; |
| 484 request.headers["Content-Type"] = "bar"; | 514 request.headers["Content-Type"] = "bar"; |
| 485 EXPECT_EQ("bar", request.headers["content-type"]); | 515 EXPECT_EQ("bar", request.headers["content-type"]); |
| 486 } | 516 } |
| 487 | 517 |
| 488 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, | 518 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, |
| 489 ServiceWorkerCacheTestP, | 519 ServiceWorkerCacheTestP, |
| 490 ::testing::Values(false, true)); | 520 ::testing::Values(false, true)); |
| 491 | 521 |
| 492 } // namespace content | 522 } // namespace content |
| OLD | NEW |