| 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 <stack> | 5 #include <stack> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/pickle.h" | 12 #include "base/pickle.h" |
| 13 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "content/browser/appcache/mock_appcache_service.h" | 15 #include "content/browser/appcache/mock_appcache_service.h" |
| 16 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 18 #include "net/base/request_priority.h" | 18 #include "net/base/request_priority.h" |
| 19 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
| 20 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
| 21 #include "net/url_request/url_request_context.h" | 21 #include "net/url_request/url_request_context.h" |
| 22 #include "net/url_request/url_request_error_job.h" | 22 #include "net/url_request/url_request_error_job.h" |
| 23 #include "net/url_request/url_request_job_factory.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "webkit/browser/appcache/appcache_response.h" | 25 #include "webkit/browser/appcache/appcache_response.h" |
| 25 #include "webkit/browser/appcache/appcache_url_request_job.h" | 26 #include "webkit/browser/appcache/appcache_url_request_job.h" |
| 26 | 27 |
| 27 using appcache::AppCacheEntry; | 28 using appcache::AppCacheEntry; |
| 28 using appcache::AppCacheStorage; | 29 using appcache::AppCacheStorage; |
| 29 using appcache::AppCacheResponseInfo; | 30 using appcache::AppCacheResponseInfo; |
| 30 using appcache::AppCacheResponseReader; | 31 using appcache::AppCacheResponseReader; |
| 31 using appcache::AppCacheResponseWriter; | 32 using appcache::AppCacheResponseWriter; |
| 32 using appcache::AppCacheURLRequestJob; | 33 using appcache::AppCacheURLRequestJob; |
| 33 using appcache::HttpResponseInfoIOBuffer; | 34 using appcache::HttpResponseInfoIOBuffer; |
| 34 using appcache::kNoCacheId; | 35 using appcache::kNoCacheId; |
| 35 using net::IOBuffer; | 36 using net::IOBuffer; |
| 36 using net::WrappedIOBuffer; | 37 using net::WrappedIOBuffer; |
| 37 | 38 |
| 38 namespace content { | 39 namespace content { |
| 39 | 40 |
| 40 static const char kHttpBasicHeaders[] = | 41 namespace { |
| 41 "HTTP/1.0 200 OK\0Content-Length: 5\0\0"; | |
| 42 static const char kHttpBasicBody[] = "Hello"; | |
| 43 | 42 |
| 44 static const int kNumBlocks = 4; | 43 const char kHttpBasicHeaders[] = "HTTP/1.0 200 OK\0Content-Length: 5\0\0"; |
| 45 static const int kBlockSize = 1024; | 44 const char kHttpBasicBody[] = "Hello"; |
| 45 |
| 46 const int kNumBlocks = 4; |
| 47 const int kBlockSize = 1024; |
| 48 |
| 49 class MockURLRequestJobFactory : public net::URLRequestJobFactory { |
| 50 public: |
| 51 MockURLRequestJobFactory() : job_(NULL) { |
| 52 } |
| 53 |
| 54 virtual ~MockURLRequestJobFactory() { |
| 55 DCHECK(!job_); |
| 56 } |
| 57 |
| 58 void SetJob(net::URLRequestJob* job) { |
| 59 job_ = job; |
| 60 } |
| 61 |
| 62 bool has_job() const { |
| 63 return job_ != NULL; |
| 64 } |
| 65 |
| 66 // net::URLRequestJobFactory implementation. |
| 67 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
| 68 const std::string& scheme, |
| 69 net::URLRequest* request, |
| 70 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 71 if (job_) { |
| 72 net::URLRequestJob* temp = job_; |
| 73 job_ = NULL; |
| 74 return temp; |
| 75 } else { |
| 76 return new net::URLRequestErrorJob(request, |
| 77 network_delegate, |
| 78 net::ERR_INTERNET_DISCONNECTED); |
| 79 } |
| 80 } |
| 81 |
| 82 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { |
| 83 return scheme == "http"; |
| 84 }; |
| 85 |
| 86 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { |
| 87 return url.SchemeIs("http"); |
| 88 } |
| 89 |
| 90 virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE { |
| 91 return false; |
| 92 } |
| 93 |
| 94 private: |
| 95 mutable net::URLRequestJob* job_; |
| 96 }; |
| 46 | 97 |
| 47 class AppCacheURLRequestJobTest : public testing::Test { | 98 class AppCacheURLRequestJobTest : public testing::Test { |
| 48 public: | 99 public: |
| 49 | 100 |
| 50 // Test Harness ------------------------------------------------------------- | 101 // Test Harness ------------------------------------------------------------- |
| 51 // TODO(michaeln): share this test harness with AppCacheResponseTest | 102 // TODO(michaeln): share this test harness with AppCacheResponseTest |
| 52 | 103 |
| 53 class MockStorageDelegate : public AppCacheStorage::Delegate { | 104 class MockStorageDelegate : public AppCacheStorage::Delegate { |
| 54 public: | 105 public: |
| 55 explicit MockStorageDelegate(AppCacheURLRequestJobTest* test) | 106 explicit MockStorageDelegate(AppCacheURLRequestJobTest* test) |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 | 182 |
| 132 AppCacheURLRequestJobTest* test_; | 183 AppCacheURLRequestJobTest* test_; |
| 133 net::HttpResponseInfo received_info_; | 184 net::HttpResponseInfo received_info_; |
| 134 scoped_refptr<net::IOBuffer> received_data_; | 185 scoped_refptr<net::IOBuffer> received_data_; |
| 135 bool did_receive_headers_; | 186 bool did_receive_headers_; |
| 136 int amount_received_; | 187 int amount_received_; |
| 137 int kill_after_amount_received_; | 188 int kill_after_amount_received_; |
| 138 bool kill_with_io_pending_; | 189 bool kill_with_io_pending_; |
| 139 }; | 190 }; |
| 140 | 191 |
| 141 static net::URLRequestJob* MockHttpJobFactory( | |
| 142 net::URLRequest* request, | |
| 143 net::NetworkDelegate* network_delegate, | |
| 144 const std::string& scheme) { | |
| 145 if (mock_factory_job_) { | |
| 146 net::URLRequestJob* temp = mock_factory_job_; | |
| 147 mock_factory_job_ = NULL; | |
| 148 return temp; | |
| 149 } else { | |
| 150 return new net::URLRequestErrorJob(request, | |
| 151 network_delegate, | |
| 152 net::ERR_INTERNET_DISCONNECTED); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 // Helper callback to run a test on our io_thread. The io_thread is spun up | 192 // Helper callback to run a test on our io_thread. The io_thread is spun up |
| 157 // once and reused for all tests. | 193 // once and reused for all tests. |
| 158 template <class Method> | 194 template <class Method> |
| 159 void MethodWrapper(Method method) { | 195 void MethodWrapper(Method method) { |
| 160 SetUpTest(); | 196 SetUpTest(); |
| 161 (this->*method)(); | 197 (this->*method)(); |
| 162 } | 198 } |
| 163 | 199 |
| 164 static void SetUpTestCase() { | 200 static void SetUpTestCase() { |
| 165 io_thread_.reset(new base::Thread("AppCacheURLRequestJobTest Thread")); | 201 io_thread_.reset(new base::Thread("AppCacheURLRequestJobTest Thread")); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 178 test_finished_event_ .reset(new base::WaitableEvent(false, false)); | 214 test_finished_event_ .reset(new base::WaitableEvent(false, false)); |
| 179 io_thread_->message_loop()->PostTask( | 215 io_thread_->message_loop()->PostTask( |
| 180 FROM_HERE, base::Bind(&AppCacheURLRequestJobTest::MethodWrapper<Method>, | 216 FROM_HERE, base::Bind(&AppCacheURLRequestJobTest::MethodWrapper<Method>, |
| 181 base::Unretained(this), method)); | 217 base::Unretained(this), method)); |
| 182 test_finished_event_->Wait(); | 218 test_finished_event_->Wait(); |
| 183 } | 219 } |
| 184 | 220 |
| 185 void SetUpTest() { | 221 void SetUpTest() { |
| 186 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); | 222 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); |
| 187 DCHECK(task_stack_.empty()); | 223 DCHECK(task_stack_.empty()); |
| 188 orig_http_factory_ = net::URLRequest::Deprecated::RegisterProtocolFactory( | 224 |
| 189 "http", MockHttpJobFactory); | |
| 190 url_request_delegate_.reset(new MockURLRequestDelegate(this)); | |
| 191 storage_delegate_.reset(new MockStorageDelegate(this)); | 225 storage_delegate_.reset(new MockStorageDelegate(this)); |
| 192 service_.reset(new MockAppCacheService()); | 226 service_.reset(new MockAppCacheService()); |
| 193 expected_read_result_ = 0; | 227 expected_read_result_ = 0; |
| 194 expected_write_result_ = 0; | 228 expected_write_result_ = 0; |
| 195 written_response_id_ = 0; | 229 written_response_id_ = 0; |
| 196 reader_deletion_count_down_ = 0; | 230 reader_deletion_count_down_ = 0; |
| 197 writer_deletion_count_down_ = 0; | 231 writer_deletion_count_down_ = 0; |
| 232 |
| 233 url_request_delegate_.reset(new MockURLRequestDelegate(this)); |
| 234 job_factory_.reset(new MockURLRequestJobFactory()); |
| 235 empty_context_.reset(new net::URLRequestContext()); |
| 236 empty_context_->set_job_factory(job_factory_.get()); |
| 198 } | 237 } |
| 199 | 238 |
| 200 void TearDownTest() { | 239 void TearDownTest() { |
| 201 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); | 240 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); |
| 202 net::URLRequest::Deprecated::RegisterProtocolFactory("http", | |
| 203 orig_http_factory_); | |
| 204 orig_http_factory_ = NULL; | |
| 205 request_.reset(); | 241 request_.reset(); |
| 206 url_request_delegate_.reset(); | |
| 207 DCHECK(!mock_factory_job_); | |
| 208 | 242 |
| 209 while (!task_stack_.empty()) | 243 while (!task_stack_.empty()) |
| 210 task_stack_.pop(); | 244 task_stack_.pop(); |
| 211 | 245 |
| 212 reader_.reset(); | 246 reader_.reset(); |
| 213 read_buffer_ = NULL; | 247 read_buffer_ = NULL; |
| 214 read_info_buffer_ = NULL; | 248 read_info_buffer_ = NULL; |
| 215 writer_.reset(); | 249 writer_.reset(); |
| 216 write_buffer_ = NULL; | 250 write_buffer_ = NULL; |
| 217 write_info_buffer_ = NULL; | 251 write_info_buffer_ = NULL; |
| 218 storage_delegate_.reset(); | 252 storage_delegate_.reset(); |
| 219 service_.reset(); | 253 service_.reset(); |
| 254 |
| 255 DCHECK(!job_factory_->has_job()); |
| 256 empty_context_.reset(); |
| 257 job_factory_.reset(); |
| 258 url_request_delegate_.reset(); |
| 220 } | 259 } |
| 221 | 260 |
| 222 void TestFinished() { | 261 void TestFinished() { |
| 223 // We unwind the stack prior to finishing up to let stack | 262 // We unwind the stack prior to finishing up to let stack |
| 224 // based objects get deleted. | 263 // based objects get deleted. |
| 225 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); | 264 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); |
| 226 base::MessageLoop::current()->PostTask( | 265 base::MessageLoop::current()->PostTask( |
| 227 FROM_HERE, | 266 FROM_HERE, |
| 228 base::Bind(&AppCacheURLRequestJobTest::TestFinishedUnwound, | 267 base::Bind(&AppCacheURLRequestJobTest::TestFinishedUnwound, |
| 229 base::Unretained(this))); | 268 base::Unretained(this))); |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 return true; | 417 return true; |
| 379 } | 418 } |
| 380 | 419 |
| 381 // Individual Tests --------------------------------------------------------- | 420 // Individual Tests --------------------------------------------------------- |
| 382 // Some of the individual tests involve multiple async steps. Each test | 421 // Some of the individual tests involve multiple async steps. Each test |
| 383 // is delineated with a section header. | 422 // is delineated with a section header. |
| 384 | 423 |
| 385 // Basic ------------------------------------------------------------------- | 424 // Basic ------------------------------------------------------------------- |
| 386 void Basic() { | 425 void Basic() { |
| 387 AppCacheStorage* storage = service_->storage(); | 426 AppCacheStorage* storage = service_->storage(); |
| 388 net::URLRequest request( | 427 net::URLRequest request(GURL("http://blah/"), net::DEFAULT_PRIORITY, NULL, |
| 389 GURL("http://blah/"), net::DEFAULT_PRIORITY, NULL, &empty_context_); | 428 empty_context_.get()); |
| 390 scoped_refptr<AppCacheURLRequestJob> job; | 429 scoped_refptr<AppCacheURLRequestJob> job; |
| 391 | 430 |
| 392 // Create an instance and see that it looks as expected. | 431 // Create an instance and see that it looks as expected. |
| 393 | 432 |
| 394 job = new AppCacheURLRequestJob( | 433 job = new AppCacheURLRequestJob( |
| 395 &request, NULL, storage, NULL, false); | 434 &request, NULL, storage, NULL, false); |
| 396 EXPECT_TRUE(job->is_waiting()); | 435 EXPECT_TRUE(job->is_waiting()); |
| 397 EXPECT_FALSE(job->is_delivering_appcache_response()); | 436 EXPECT_FALSE(job->is_delivering_appcache_response()); |
| 398 EXPECT_FALSE(job->is_delivering_network_response()); | 437 EXPECT_FALSE(job->is_delivering_network_response()); |
| 399 EXPECT_FALSE(job->is_delivering_error_response()); | 438 EXPECT_FALSE(job->is_delivering_error_response()); |
| 400 EXPECT_FALSE(job->has_been_started()); | 439 EXPECT_FALSE(job->has_been_started()); |
| 401 EXPECT_FALSE(job->has_been_killed()); | 440 EXPECT_FALSE(job->has_been_killed()); |
| 402 EXPECT_EQ(GURL(), job->manifest_url()); | 441 EXPECT_EQ(GURL(), job->manifest_url()); |
| 403 EXPECT_EQ(kNoCacheId, job->cache_id()); | 442 EXPECT_EQ(kNoCacheId, job->cache_id()); |
| 404 EXPECT_FALSE(job->entry().has_response_id()); | 443 EXPECT_FALSE(job->entry().has_response_id()); |
| 405 | 444 |
| 406 TestFinished(); | 445 TestFinished(); |
| 407 } | 446 } |
| 408 | 447 |
| 409 // DeliveryOrders ----------------------------------------------------- | 448 // DeliveryOrders ----------------------------------------------------- |
| 410 void DeliveryOrders() { | 449 void DeliveryOrders() { |
| 411 AppCacheStorage* storage = service_->storage(); | 450 AppCacheStorage* storage = service_->storage(); |
| 412 net::URLRequest request( | 451 net::URLRequest request(GURL("http://blah/"), net::DEFAULT_PRIORITY, NULL, |
| 413 GURL("http://blah/"), net::DEFAULT_PRIORITY, NULL, &empty_context_); | 452 empty_context_.get()); |
| 414 scoped_refptr<AppCacheURLRequestJob> job; | 453 scoped_refptr<AppCacheURLRequestJob> job; |
| 415 | 454 |
| 416 // Create an instance, give it a delivery order and see that | 455 // Create an instance, give it a delivery order and see that |
| 417 // it looks as expected. | 456 // it looks as expected. |
| 418 | 457 |
| 419 job = new AppCacheURLRequestJob(&request, NULL, storage, NULL, false); | 458 job = new AppCacheURLRequestJob(&request, NULL, storage, NULL, false); |
| 420 job->DeliverErrorResponse(); | 459 job->DeliverErrorResponse(); |
| 421 EXPECT_TRUE(job->is_delivering_error_response()); | 460 EXPECT_TRUE(job->is_delivering_error_response()); |
| 422 EXPECT_FALSE(job->has_been_started()); | 461 EXPECT_FALSE(job->has_been_started()); |
| 423 | 462 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 447 | 486 |
| 448 // DeliverNetworkResponse -------------------------------------------------- | 487 // DeliverNetworkResponse -------------------------------------------------- |
| 449 | 488 |
| 450 void DeliverNetworkResponse() { | 489 void DeliverNetworkResponse() { |
| 451 // This test has async steps. | 490 // This test has async steps. |
| 452 PushNextTask( | 491 PushNextTask( |
| 453 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverNetworkResponse, | 492 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverNetworkResponse, |
| 454 base::Unretained(this))); | 493 base::Unretained(this))); |
| 455 | 494 |
| 456 AppCacheStorage* storage = service_->storage(); | 495 AppCacheStorage* storage = service_->storage(); |
| 457 request_ = empty_context_.CreateRequest(GURL("http://blah/"), | 496 request_ = empty_context_->CreateRequest(GURL("http://blah/"), |
| 458 net::DEFAULT_PRIORITY, | 497 net::DEFAULT_PRIORITY, |
| 459 url_request_delegate_.get(), | 498 url_request_delegate_.get(), |
| 460 NULL); | 499 NULL); |
| 461 | 500 |
| 462 // Setup to create an AppCacheURLRequestJob with orders to deliver | 501 // Setup to create an AppCacheURLRequestJob with orders to deliver |
| 463 // a network response. | 502 // a network response. |
| 464 mock_factory_job_ = new AppCacheURLRequestJob( | 503 AppCacheURLRequestJob* mock_job = new AppCacheURLRequestJob( |
| 465 request_.get(), NULL, storage, NULL, false); | 504 request_.get(), NULL, storage, NULL, false); |
| 466 mock_factory_job_->DeliverNetworkResponse(); | 505 job_factory_->SetJob(mock_job); |
| 467 EXPECT_TRUE(mock_factory_job_->is_delivering_network_response()); | 506 mock_job->DeliverNetworkResponse(); |
| 468 EXPECT_FALSE(mock_factory_job_->has_been_started()); | 507 EXPECT_TRUE(mock_job->is_delivering_network_response()); |
| 508 EXPECT_FALSE(mock_job->has_been_started()); |
| 469 | 509 |
| 470 // Start the request. | 510 // Start the request. |
| 471 request_->Start(); | 511 request_->Start(); |
| 472 | 512 |
| 473 // The job should have been picked up. | 513 // The job should have been picked up. |
| 474 EXPECT_FALSE(mock_factory_job_); | 514 EXPECT_FALSE(job_factory_->has_job()); |
| 475 // Completion is async. | 515 // Completion is async. |
| 476 } | 516 } |
| 477 | 517 |
| 478 void VerifyDeliverNetworkResponse() { | 518 void VerifyDeliverNetworkResponse() { |
| 479 EXPECT_EQ(request_->status().error(), | 519 EXPECT_EQ(request_->status().error(), |
| 480 net::ERR_INTERNET_DISCONNECTED); | 520 net::ERR_INTERNET_DISCONNECTED); |
| 481 TestFinished(); | 521 TestFinished(); |
| 482 } | 522 } |
| 483 | 523 |
| 484 // DeliverErrorResponse -------------------------------------------------- | 524 // DeliverErrorResponse -------------------------------------------------- |
| 485 | 525 |
| 486 void DeliverErrorResponse() { | 526 void DeliverErrorResponse() { |
| 487 // This test has async steps. | 527 // This test has async steps. |
| 488 PushNextTask( | 528 PushNextTask( |
| 489 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverErrorResponse, | 529 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverErrorResponse, |
| 490 base::Unretained(this))); | 530 base::Unretained(this))); |
| 491 | 531 |
| 492 AppCacheStorage* storage = service_->storage(); | 532 AppCacheStorage* storage = service_->storage(); |
| 493 request_ = empty_context_.CreateRequest(GURL("http://blah/"), | 533 request_ = empty_context_->CreateRequest(GURL("http://blah/"), |
| 494 net::DEFAULT_PRIORITY, | 534 net::DEFAULT_PRIORITY, |
| 495 url_request_delegate_.get(), | 535 url_request_delegate_.get(), |
| 496 NULL); | 536 NULL); |
| 497 | 537 |
| 498 // Setup to create an AppCacheURLRequestJob with orders to deliver | 538 // Setup to create an AppCacheURLRequestJob with orders to deliver |
| 499 // a network response. | 539 // a network response. |
| 500 mock_factory_job_ = new AppCacheURLRequestJob( | 540 AppCacheURLRequestJob* mock_job = new AppCacheURLRequestJob( |
| 501 request_.get(), NULL, storage, NULL, false); | 541 request_.get(), NULL, storage, NULL, false); |
| 502 mock_factory_job_->DeliverErrorResponse(); | 542 job_factory_->SetJob(mock_job); |
| 503 EXPECT_TRUE(mock_factory_job_->is_delivering_error_response()); | 543 mock_job->DeliverErrorResponse(); |
| 504 EXPECT_FALSE(mock_factory_job_->has_been_started()); | 544 EXPECT_TRUE(mock_job->is_delivering_error_response()); |
| 545 EXPECT_FALSE(mock_job->has_been_started()); |
| 505 | 546 |
| 506 // Start the request. | 547 // Start the request. |
| 507 request_->Start(); | 548 request_->Start(); |
| 508 | 549 |
| 509 // The job should have been picked up. | 550 // The job should have been picked up. |
| 510 EXPECT_FALSE(mock_factory_job_); | 551 EXPECT_FALSE(job_factory_->has_job()); |
| 511 // Completion is async. | 552 // Completion is async. |
| 512 } | 553 } |
| 513 | 554 |
| 514 void VerifyDeliverErrorResponse() { | 555 void VerifyDeliverErrorResponse() { |
| 515 EXPECT_EQ(request_->status().error(), net::ERR_FAILED); | 556 EXPECT_EQ(request_->status().error(), net::ERR_FAILED); |
| 516 TestFinished(); | 557 TestFinished(); |
| 517 } | 558 } |
| 518 | 559 |
| 519 // DeliverSmallAppCachedResponse -------------------------------------- | 560 // DeliverSmallAppCachedResponse -------------------------------------- |
| 520 // "Small" being small enough to read completely in a single | 561 // "Small" being small enough to read completely in a single |
| (...skipping 13 matching lines...) Expand all Loading... |
| 534 base::Unretained(this), false)); | 575 base::Unretained(this), false)); |
| 535 | 576 |
| 536 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); | 577 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); |
| 537 written_response_id_ = writer_->response_id(); | 578 written_response_id_ = writer_->response_id(); |
| 538 WriteBasicResponse(); | 579 WriteBasicResponse(); |
| 539 // Continues async | 580 // Continues async |
| 540 } | 581 } |
| 541 | 582 |
| 542 void RequestAppCachedResource(bool start_after_delivery_orders) { | 583 void RequestAppCachedResource(bool start_after_delivery_orders) { |
| 543 AppCacheStorage* storage = service_->storage(); | 584 AppCacheStorage* storage = service_->storage(); |
| 544 request_ = empty_context_.CreateRequest(GURL("http://blah/"), | 585 request_ = empty_context_->CreateRequest(GURL("http://blah/"), |
| 545 net::DEFAULT_PRIORITY, | 586 net::DEFAULT_PRIORITY, |
| 546 url_request_delegate_.get(), | 587 url_request_delegate_.get(), |
| 547 NULL); | 588 NULL); |
| 548 | 589 |
| 549 // Setup to create an AppCacheURLRequestJob with orders to deliver | 590 // Setup to create an AppCacheURLRequestJob with orders to deliver |
| 550 // a network response. | 591 // a network response. |
| 551 scoped_refptr<AppCacheURLRequestJob> job(new AppCacheURLRequestJob( | 592 scoped_refptr<AppCacheURLRequestJob> job(new AppCacheURLRequestJob( |
| 552 request_.get(), NULL, storage, NULL, false)); | 593 request_.get(), NULL, storage, NULL, false)); |
| 553 | 594 |
| 554 if (start_after_delivery_orders) { | 595 if (start_after_delivery_orders) { |
| 555 job->DeliverAppCachedResponse( | 596 job->DeliverAppCachedResponse( |
| 556 GURL(), 0, 111, | 597 GURL(), 0, 111, |
| 557 AppCacheEntry(AppCacheEntry::EXPLICIT, written_response_id_), | 598 AppCacheEntry(AppCacheEntry::EXPLICIT, written_response_id_), |
| 558 false); | 599 false); |
| 559 EXPECT_TRUE(job->is_delivering_appcache_response()); | 600 EXPECT_TRUE(job->is_delivering_appcache_response()); |
| 560 } | 601 } |
| 561 | 602 |
| 562 // Start the request. | 603 // Start the request. |
| 563 EXPECT_FALSE(job->has_been_started()); | 604 EXPECT_FALSE(job->has_been_started()); |
| 564 mock_factory_job_ = job.get(); | 605 job_factory_->SetJob(job.get()); |
| 565 request_->Start(); | 606 request_->Start(); |
| 566 EXPECT_FALSE(mock_factory_job_); | 607 EXPECT_FALSE(job_factory_->has_job()); |
| 567 EXPECT_TRUE(job->has_been_started()); | 608 EXPECT_TRUE(job->has_been_started()); |
| 568 | 609 |
| 569 if (!start_after_delivery_orders) { | 610 if (!start_after_delivery_orders) { |
| 570 job->DeliverAppCachedResponse( | 611 job->DeliverAppCachedResponse( |
| 571 GURL(), 0, 111, | 612 GURL(), 0, 111, |
| 572 AppCacheEntry(AppCacheEntry::EXPLICIT, written_response_id_), | 613 AppCacheEntry(AppCacheEntry::EXPLICIT, written_response_id_), |
| 573 false); | 614 false); |
| 574 EXPECT_TRUE(job->is_delivering_appcache_response()); | 615 EXPECT_TRUE(job->is_delivering_appcache_response()); |
| 575 } | 616 } |
| 576 | 617 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 PushNextTask(base::Bind( | 690 PushNextTask(base::Bind( |
| 650 &AppCacheURLRequestJobTest::MakeRangeRequest, base::Unretained(this))); | 691 &AppCacheURLRequestJobTest::MakeRangeRequest, base::Unretained(this))); |
| 651 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); | 692 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); |
| 652 written_response_id_ = writer_->response_id(); | 693 written_response_id_ = writer_->response_id(); |
| 653 WriteBasicResponse(); | 694 WriteBasicResponse(); |
| 654 // Continues async | 695 // Continues async |
| 655 } | 696 } |
| 656 | 697 |
| 657 void MakeRangeRequest() { | 698 void MakeRangeRequest() { |
| 658 AppCacheStorage* storage = service_->storage(); | 699 AppCacheStorage* storage = service_->storage(); |
| 659 request_ = empty_context_.CreateRequest(GURL("http://blah/"), | 700 request_ = empty_context_->CreateRequest(GURL("http://blah/"), |
| 660 net::DEFAULT_PRIORITY, | 701 net::DEFAULT_PRIORITY, |
| 661 url_request_delegate_.get(), | 702 url_request_delegate_.get(), |
| 662 NULL); | 703 NULL); |
| 663 | 704 |
| 664 // Request a range, the 3 middle chars out of 'Hello' | 705 // Request a range, the 3 middle chars out of 'Hello' |
| 665 net::HttpRequestHeaders extra_headers; | 706 net::HttpRequestHeaders extra_headers; |
| 666 extra_headers.SetHeader("Range", "bytes= 1-3"); | 707 extra_headers.SetHeader("Range", "bytes= 1-3"); |
| 667 request_->SetExtraRequestHeaders(extra_headers); | 708 request_->SetExtraRequestHeaders(extra_headers); |
| 668 | 709 |
| 669 // Create job with orders to deliver an appcached entry. | 710 // Create job with orders to deliver an appcached entry. |
| 670 scoped_refptr<AppCacheURLRequestJob> job(new AppCacheURLRequestJob( | 711 scoped_refptr<AppCacheURLRequestJob> job(new AppCacheURLRequestJob( |
| 671 request_.get(), NULL, storage, NULL, false)); | 712 request_.get(), NULL, storage, NULL, false)); |
| 672 job->DeliverAppCachedResponse( | 713 job->DeliverAppCachedResponse( |
| 673 GURL(), 0, 111, | 714 GURL(), 0, 111, |
| 674 AppCacheEntry(AppCacheEntry::EXPLICIT, written_response_id_), | 715 AppCacheEntry(AppCacheEntry::EXPLICIT, written_response_id_), |
| 675 false); | 716 false); |
| 676 EXPECT_TRUE(job->is_delivering_appcache_response()); | 717 EXPECT_TRUE(job->is_delivering_appcache_response()); |
| 677 | 718 |
| 678 // Start the request. | 719 // Start the request. |
| 679 EXPECT_FALSE(job->has_been_started()); | 720 EXPECT_FALSE(job->has_been_started()); |
| 680 mock_factory_job_ = job.get(); | 721 job_factory_->SetJob(job.get()); |
| 681 request_->Start(); | 722 request_->Start(); |
| 682 EXPECT_FALSE(mock_factory_job_); | 723 EXPECT_FALSE(job_factory_->has_job()); |
| 683 EXPECT_TRUE(job->has_been_started()); | 724 EXPECT_TRUE(job->has_been_started()); |
| 684 // Completion is async. | 725 // Completion is async. |
| 685 } | 726 } |
| 686 | 727 |
| 687 void VerifyDeliverPartialResponse() { | 728 void VerifyDeliverPartialResponse() { |
| 688 EXPECT_TRUE(request_->status().is_success()); | 729 EXPECT_TRUE(request_->status().is_success()); |
| 689 EXPECT_EQ(3, url_request_delegate_->amount_received_); | 730 EXPECT_EQ(3, url_request_delegate_->amount_received_); |
| 690 EXPECT_EQ(0, memcmp(kHttpBasicBody + 1, | 731 EXPECT_EQ(0, memcmp(kHttpBasicBody + 1, |
| 691 url_request_delegate_->received_data_->data(), | 732 url_request_delegate_->received_data_->data(), |
| 692 3)); | 733 3)); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 769 int expected_read_result_; | 810 int expected_read_result_; |
| 770 int reader_deletion_count_down_; | 811 int reader_deletion_count_down_; |
| 771 | 812 |
| 772 int64 written_response_id_; | 813 int64 written_response_id_; |
| 773 scoped_ptr<AppCacheResponseWriter> writer_; | 814 scoped_ptr<AppCacheResponseWriter> writer_; |
| 774 scoped_refptr<HttpResponseInfoIOBuffer> write_info_buffer_; | 815 scoped_refptr<HttpResponseInfoIOBuffer> write_info_buffer_; |
| 775 scoped_refptr<IOBuffer> write_buffer_; | 816 scoped_refptr<IOBuffer> write_buffer_; |
| 776 int expected_write_result_; | 817 int expected_write_result_; |
| 777 int writer_deletion_count_down_; | 818 int writer_deletion_count_down_; |
| 778 | 819 |
| 779 net::URLRequest::ProtocolFactory* orig_http_factory_; | 820 scoped_ptr<MockURLRequestJobFactory> job_factory_; |
| 780 net::URLRequestContext empty_context_; | 821 scoped_ptr<net::URLRequestContext> empty_context_; |
| 781 scoped_ptr<net::URLRequest> request_; | 822 scoped_ptr<net::URLRequest> request_; |
| 782 scoped_ptr<MockURLRequestDelegate> url_request_delegate_; | 823 scoped_ptr<MockURLRequestDelegate> url_request_delegate_; |
| 783 | 824 |
| 784 static scoped_ptr<base::Thread> io_thread_; | 825 static scoped_ptr<base::Thread> io_thread_; |
| 785 static AppCacheURLRequestJob* mock_factory_job_; | |
| 786 }; | 826 }; |
| 787 | 827 |
| 788 // static | 828 // static |
| 789 scoped_ptr<base::Thread> AppCacheURLRequestJobTest::io_thread_; | 829 scoped_ptr<base::Thread> AppCacheURLRequestJobTest::io_thread_; |
| 790 AppCacheURLRequestJob* AppCacheURLRequestJobTest::mock_factory_job_ = NULL; | |
| 791 | 830 |
| 792 TEST_F(AppCacheURLRequestJobTest, Basic) { | 831 TEST_F(AppCacheURLRequestJobTest, Basic) { |
| 793 RunTestOnIOThread(&AppCacheURLRequestJobTest::Basic); | 832 RunTestOnIOThread(&AppCacheURLRequestJobTest::Basic); |
| 794 } | 833 } |
| 795 | 834 |
| 796 TEST_F(AppCacheURLRequestJobTest, DeliveryOrders) { | 835 TEST_F(AppCacheURLRequestJobTest, DeliveryOrders) { |
| 797 RunTestOnIOThread(&AppCacheURLRequestJobTest::DeliveryOrders); | 836 RunTestOnIOThread(&AppCacheURLRequestJobTest::DeliveryOrders); |
| 798 } | 837 } |
| 799 | 838 |
| 800 TEST_F(AppCacheURLRequestJobTest, DeliverNetworkResponse) { | 839 TEST_F(AppCacheURLRequestJobTest, DeliverNetworkResponse) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 818 } | 857 } |
| 819 | 858 |
| 820 TEST_F(AppCacheURLRequestJobTest, CancelRequest) { | 859 TEST_F(AppCacheURLRequestJobTest, CancelRequest) { |
| 821 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequest); | 860 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequest); |
| 822 } | 861 } |
| 823 | 862 |
| 824 TEST_F(AppCacheURLRequestJobTest, CancelRequestWithIOPending) { | 863 TEST_F(AppCacheURLRequestJobTest, CancelRequestWithIOPending) { |
| 825 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending); | 864 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending); |
| 826 } | 865 } |
| 827 | 866 |
| 867 } // namespace |
| 868 |
| 828 } // namespace content | 869 } // namespace content |
| OLD | NEW |