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 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 EXPECT_TRUE(Delete(body_request_)); | 430 EXPECT_TRUE(Delete(body_request_)); |
413 EXPECT_TRUE(Keys()); | 431 EXPECT_TRUE(Keys()); |
414 EXPECT_EQ(1u, callback_strings_.size()); | 432 EXPECT_EQ(1u, callback_strings_.size()); |
415 std::vector<std::string> expected_key; | 433 std::vector<std::string> expected_key; |
416 expected_key.push_back(no_body_request_.url.spec()); | 434 expected_key.push_back(no_body_request_.url.spec()); |
417 EXPECT_TRUE(VerifyKeys(expected_key)); | 435 EXPECT_TRUE(VerifyKeys(expected_key)); |
418 } | 436 } |
419 | 437 |
420 // TODO(jkarlin): Once SimpleCache is working bug-free on Windows reenable these | 438 // TODO(jkarlin): Once SimpleCache is working bug-free on Windows reenable these |
421 // tests. In the meanwhile we know that Windows operations will be a little | 439 // tests. In the meanwhile we know that Windows operations will be a little |
422 // flaky (though not crashy). See https://crbug.com/409109 | 440 // flaky (though not crashy). See https://crbug.com/409109 and |
| 441 // https://crbug.com/416940. |
423 #ifndef OS_WIN | 442 #ifndef OS_WIN |
424 TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) { | 443 TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) { |
425 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 444 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
426 EXPECT_TRUE(Match(no_body_request_)); | 445 EXPECT_TRUE(Match(no_body_request_)); |
427 EXPECT_TRUE(Delete(no_body_request_)); | 446 EXPECT_TRUE(Delete(no_body_request_)); |
428 EXPECT_FALSE(Match(no_body_request_)); | 447 EXPECT_FALSE(Match(no_body_request_)); |
429 EXPECT_FALSE(Delete(no_body_request_)); | 448 EXPECT_FALSE(Delete(no_body_request_)); |
430 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 449 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
431 EXPECT_TRUE(Match(no_body_request_)); | 450 EXPECT_TRUE(Match(no_body_request_)); |
432 EXPECT_TRUE(Delete(no_body_request_)); | 451 EXPECT_TRUE(Delete(no_body_request_)); |
(...skipping 20 matching lines...) Expand all Loading... |
453 } | 472 } |
454 | 473 |
455 TEST_P(ServiceWorkerCacheTestP, QuickStressBody) { | 474 TEST_P(ServiceWorkerCacheTestP, QuickStressBody) { |
456 for (int i = 0; i < 100; ++i) { | 475 for (int i = 0; i < 100; ++i) { |
457 ASSERT_FALSE(Match(body_request_)); | 476 ASSERT_FALSE(Match(body_request_)); |
458 ASSERT_TRUE(Put(body_request_, body_response_)); | 477 ASSERT_TRUE(Put(body_request_, body_response_)); |
459 ASSERT_TRUE(Match(body_request_)); | 478 ASSERT_TRUE(Match(body_request_)); |
460 ASSERT_TRUE(Delete(body_request_)); | 479 ASSERT_TRUE(Delete(body_request_)); |
461 } | 480 } |
462 } | 481 } |
| 482 |
| 483 TEST_P(ServiceWorkerCacheTestP, PutResponseType) { |
| 484 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeBasic)); |
| 485 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeCORS)); |
| 486 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeDefault)); |
| 487 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeError)); |
| 488 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeOpaque)); |
| 489 } |
463 #endif // OS_WIN | 490 #endif // OS_WIN |
464 | 491 |
465 TEST_F(ServiceWorkerCacheTest, CaselessServiceWorkerResponseHeaders) { | 492 TEST_F(ServiceWorkerCacheTest, CaselessServiceWorkerResponseHeaders) { |
466 // ServiceWorkerCache depends on ServiceWorkerResponse having caseless | 493 // ServiceWorkerCache depends on ServiceWorkerResponse having caseless |
467 // headers so that it can quickly lookup vary headers. | 494 // headers so that it can quickly lookup vary headers. |
468 ServiceWorkerResponse response( | 495 ServiceWorkerResponse response(GURL("http://www.example.com"), |
469 GURL("http://www.example.com"), 200, "OK", ServiceWorkerHeaderMap(), ""); | 496 200, |
| 497 "OK", |
| 498 blink::WebServiceWorkerResponseTypeDefault, |
| 499 ServiceWorkerHeaderMap(), |
| 500 ""); |
470 response.headers["content-type"] = "foo"; | 501 response.headers["content-type"] = "foo"; |
471 response.headers["Content-Type"] = "bar"; | 502 response.headers["Content-Type"] = "bar"; |
472 EXPECT_EQ("bar", response.headers["content-type"]); | 503 EXPECT_EQ("bar", response.headers["content-type"]); |
473 } | 504 } |
474 | 505 |
475 TEST_F(ServiceWorkerCacheTest, CaselessServiceWorkerFetchRequestHeaders) { | 506 TEST_F(ServiceWorkerCacheTest, CaselessServiceWorkerFetchRequestHeaders) { |
476 // ServiceWorkerCache depends on ServiceWorkerFetchRequest having caseless | 507 // ServiceWorkerCache depends on ServiceWorkerFetchRequest having caseless |
477 // headers so that it can quickly lookup vary headers. | 508 // headers so that it can quickly lookup vary headers. |
478 ServiceWorkerFetchRequest request(GURL("http://www.example.com"), | 509 ServiceWorkerFetchRequest request(GURL("http://www.example.com"), |
479 "GET", | 510 "GET", |
480 ServiceWorkerHeaderMap(), | 511 ServiceWorkerHeaderMap(), |
481 GURL(""), | 512 GURL(""), |
482 false); | 513 false); |
483 request.headers["content-type"] = "foo"; | 514 request.headers["content-type"] = "foo"; |
484 request.headers["Content-Type"] = "bar"; | 515 request.headers["Content-Type"] = "bar"; |
485 EXPECT_EQ("bar", request.headers["content-type"]); | 516 EXPECT_EQ("bar", request.headers["content-type"]); |
486 } | 517 } |
487 | 518 |
488 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, | 519 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, |
489 ServiceWorkerCacheTestP, | 520 ServiceWorkerCacheTestP, |
490 ::testing::Values(false, true)); | 521 ::testing::Values(false, true)); |
491 | 522 |
492 } // namespace content | 523 } // namespace content |
OLD | NEW |