| 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/appcache/appcache_url_request_job.h" | 5 #include "content/browser/appcache/appcache_url_request_job.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "base/single_thread_task_runner.h" | 24 #include "base/single_thread_task_runner.h" |
| 25 #include "base/synchronization/waitable_event.h" | 25 #include "base/synchronization/waitable_event.h" |
| 26 #include "base/threading/thread.h" | 26 #include "base/threading/thread.h" |
| 27 #include "base/threading/thread_task_runner_handle.h" | 27 #include "base/threading/thread_task_runner_handle.h" |
| 28 #include "content/browser/appcache/appcache_response.h" | 28 #include "content/browser/appcache/appcache_response.h" |
| 29 #include "content/browser/appcache/mock_appcache_service.h" | 29 #include "content/browser/appcache/mock_appcache_service.h" |
| 30 #include "net/base/io_buffer.h" | 30 #include "net/base/io_buffer.h" |
| 31 #include "net/base/net_errors.h" | 31 #include "net/base/net_errors.h" |
| 32 #include "net/base/request_priority.h" | 32 #include "net/base/request_priority.h" |
| 33 #include "net/http/http_response_headers.h" | 33 #include "net/http/http_response_headers.h" |
| 34 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| 34 #include "net/url_request/url_request.h" | 35 #include "net/url_request/url_request.h" |
| 35 #include "net/url_request/url_request_context.h" | 36 #include "net/url_request/url_request_context.h" |
| 36 #include "net/url_request/url_request_error_job.h" | 37 #include "net/url_request/url_request_error_job.h" |
| 37 #include "net/url_request/url_request_job_factory.h" | 38 #include "net/url_request/url_request_job_factory.h" |
| 38 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
| 39 #include "url/gurl.h" | 40 #include "url/gurl.h" |
| 40 | 41 |
| 41 using net::IOBuffer; | 42 using net::IOBuffer; |
| 42 using net::WrappedIOBuffer; | 43 using net::WrappedIOBuffer; |
| 43 | 44 |
| (...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 } | 454 } |
| 454 | 455 |
| 455 // Individual Tests --------------------------------------------------------- | 456 // Individual Tests --------------------------------------------------------- |
| 456 // Some of the individual tests involve multiple async steps. Each test | 457 // Some of the individual tests involve multiple async steps. Each test |
| 457 // is delineated with a section header. | 458 // is delineated with a section header. |
| 458 | 459 |
| 459 // Basic ------------------------------------------------------------------- | 460 // Basic ------------------------------------------------------------------- |
| 460 void Basic() { | 461 void Basic() { |
| 461 AppCacheStorage* storage = service_->storage(); | 462 AppCacheStorage* storage = service_->storage(); |
| 462 request_ = empty_context_->CreateRequest(GURL("http://blah/"), | 463 request_ = empty_context_->CreateRequest(GURL("http://blah/"), |
| 463 net::DEFAULT_PRIORITY, nullptr); | 464 net::DEFAULT_PRIORITY, nullptr, |
| 465 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 464 | 466 |
| 465 // Create an instance and see that it looks as expected. | 467 // Create an instance and see that it looks as expected. |
| 466 | 468 |
| 467 std::unique_ptr<AppCacheURLRequestJob> job( | 469 std::unique_ptr<AppCacheURLRequestJob> job( |
| 468 new AppCacheURLRequestJob(request_.get(), nullptr, storage, nullptr, | 470 new AppCacheURLRequestJob(request_.get(), nullptr, storage, nullptr, |
| 469 false, base::Bind(&ExpectNotRestarted))); | 471 false, base::Bind(&ExpectNotRestarted))); |
| 470 EXPECT_TRUE(job->IsWaiting()); | 472 EXPECT_TRUE(job->IsWaiting()); |
| 471 EXPECT_FALSE(job->IsDeliveringAppCacheResponse()); | 473 EXPECT_FALSE(job->IsDeliveringAppCacheResponse()); |
| 472 EXPECT_FALSE(job->IsDeliveringNetworkResponse()); | 474 EXPECT_FALSE(job->IsDeliveringNetworkResponse()); |
| 473 EXPECT_FALSE(job->IsDeliveringErrorResponse()); | 475 EXPECT_FALSE(job->IsDeliveringErrorResponse()); |
| 474 EXPECT_FALSE(job->IsStarted()); | 476 EXPECT_FALSE(job->IsStarted()); |
| 475 EXPECT_FALSE(job->has_been_killed()); | 477 EXPECT_FALSE(job->has_been_killed()); |
| 476 EXPECT_EQ(GURL(), job->manifest_url()); | 478 EXPECT_EQ(GURL(), job->manifest_url()); |
| 477 EXPECT_EQ(kAppCacheNoCacheId, job->cache_id()); | 479 EXPECT_EQ(kAppCacheNoCacheId, job->cache_id()); |
| 478 EXPECT_FALSE(job->entry().has_response_id()); | 480 EXPECT_FALSE(job->entry().has_response_id()); |
| 479 | 481 |
| 480 TestFinished(); | 482 TestFinished(); |
| 481 } | 483 } |
| 482 | 484 |
| 483 // DeliveryOrders ----------------------------------------------------- | 485 // DeliveryOrders ----------------------------------------------------- |
| 484 void DeliveryOrders() { | 486 void DeliveryOrders() { |
| 485 AppCacheStorage* storage = service_->storage(); | 487 AppCacheStorage* storage = service_->storage(); |
| 486 std::unique_ptr<net::URLRequest> request(empty_context_->CreateRequest( | 488 std::unique_ptr<net::URLRequest> request(empty_context_->CreateRequest( |
| 487 GURL("http://blah/"), net::DEFAULT_PRIORITY, nullptr)); | 489 GURL("http://blah/"), net::DEFAULT_PRIORITY, nullptr, |
| 490 TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 488 | 491 |
| 489 // Create an instance, give it a delivery order and see that | 492 // Create an instance, give it a delivery order and see that |
| 490 // it looks as expected. | 493 // it looks as expected. |
| 491 | 494 |
| 492 std::unique_ptr<AppCacheURLRequestJob> job( | 495 std::unique_ptr<AppCacheURLRequestJob> job( |
| 493 new AppCacheURLRequestJob(request.get(), nullptr, storage, nullptr, | 496 new AppCacheURLRequestJob(request.get(), nullptr, storage, nullptr, |
| 494 false, base::Bind(&ExpectNotRestarted))); | 497 false, base::Bind(&ExpectNotRestarted))); |
| 495 job->DeliverErrorResponse(); | 498 job->DeliverErrorResponse(); |
| 496 EXPECT_TRUE(job->IsDeliveringErrorResponse()); | 499 EXPECT_TRUE(job->IsDeliveringErrorResponse()); |
| 497 EXPECT_FALSE(job->IsStarted()); | 500 EXPECT_FALSE(job->IsStarted()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 523 | 526 |
| 524 // DeliverNetworkResponse -------------------------------------------------- | 527 // DeliverNetworkResponse -------------------------------------------------- |
| 525 | 528 |
| 526 void DeliverNetworkResponse() { | 529 void DeliverNetworkResponse() { |
| 527 // This test has async steps. | 530 // This test has async steps. |
| 528 PushNextTask( | 531 PushNextTask( |
| 529 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverNetworkResponse, | 532 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverNetworkResponse, |
| 530 base::Unretained(this))); | 533 base::Unretained(this))); |
| 531 | 534 |
| 532 AppCacheStorage* storage = service_->storage(); | 535 AppCacheStorage* storage = service_->storage(); |
| 533 request_ = empty_context_->CreateRequest(GURL("http://blah/"), | 536 request_ = empty_context_->CreateRequest( |
| 534 net::DEFAULT_PRIORITY, | 537 GURL("http://blah/"), net::DEFAULT_PRIORITY, |
| 535 url_request_delegate_.get()); | 538 url_request_delegate_.get(), TRAFFIC_ANNOTATION_FOR_TESTS); |
| 536 | 539 |
| 537 // Set up to create an AppCacheURLRequestJob with orders to deliver | 540 // Set up to create an AppCacheURLRequestJob with orders to deliver |
| 538 // a network response. | 541 // a network response. |
| 539 std::unique_ptr<AppCacheURLRequestJob> mock_job(new AppCacheURLRequestJob( | 542 std::unique_ptr<AppCacheURLRequestJob> mock_job(new AppCacheURLRequestJob( |
| 540 request_.get(), nullptr, storage, nullptr, false, | 543 request_.get(), nullptr, storage, nullptr, false, |
| 541 base::Bind(&SetIfCalled, &restart_callback_invoked_))); | 544 base::Bind(&SetIfCalled, &restart_callback_invoked_))); |
| 542 mock_job->DeliverNetworkResponse(); | 545 mock_job->DeliverNetworkResponse(); |
| 543 EXPECT_TRUE(mock_job->IsDeliveringNetworkResponse()); | 546 EXPECT_TRUE(mock_job->IsDeliveringNetworkResponse()); |
| 544 EXPECT_FALSE(mock_job->IsStarted()); | 547 EXPECT_FALSE(mock_job->IsStarted()); |
| 545 job_factory_->SetJob(std::move(mock_job)); | 548 job_factory_->SetJob(std::move(mock_job)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 561 | 564 |
| 562 // DeliverErrorResponse -------------------------------------------------- | 565 // DeliverErrorResponse -------------------------------------------------- |
| 563 | 566 |
| 564 void DeliverErrorResponse() { | 567 void DeliverErrorResponse() { |
| 565 // This test has async steps. | 568 // This test has async steps. |
| 566 PushNextTask( | 569 PushNextTask( |
| 567 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverErrorResponse, | 570 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverErrorResponse, |
| 568 base::Unretained(this))); | 571 base::Unretained(this))); |
| 569 | 572 |
| 570 AppCacheStorage* storage = service_->storage(); | 573 AppCacheStorage* storage = service_->storage(); |
| 571 request_ = empty_context_->CreateRequest(GURL("http://blah/"), | 574 request_ = empty_context_->CreateRequest( |
| 572 net::DEFAULT_PRIORITY, | 575 GURL("http://blah/"), net::DEFAULT_PRIORITY, |
| 573 url_request_delegate_.get()); | 576 url_request_delegate_.get(), TRAFFIC_ANNOTATION_FOR_TESTS); |
| 574 | 577 |
| 575 // Setup to create an AppCacheURLRequestJob with orders to deliver | 578 // Setup to create an AppCacheURLRequestJob with orders to deliver |
| 576 // a network response. | 579 // a network response. |
| 577 std::unique_ptr<AppCacheURLRequestJob> mock_job( | 580 std::unique_ptr<AppCacheURLRequestJob> mock_job( |
| 578 new AppCacheURLRequestJob(request_.get(), nullptr, storage, nullptr, | 581 new AppCacheURLRequestJob(request_.get(), nullptr, storage, nullptr, |
| 579 false, base::Bind(&ExpectNotRestarted))); | 582 false, base::Bind(&ExpectNotRestarted))); |
| 580 mock_job->DeliverErrorResponse(); | 583 mock_job->DeliverErrorResponse(); |
| 581 EXPECT_TRUE(mock_job->IsDeliveringErrorResponse()); | 584 EXPECT_TRUE(mock_job->IsDeliveringErrorResponse()); |
| 582 EXPECT_FALSE(mock_job->IsStarted()); | 585 EXPECT_FALSE(mock_job->IsStarted()); |
| 583 job_factory_->SetJob(std::move(mock_job)); | 586 job_factory_->SetJob(std::move(mock_job)); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 613 base::Unretained(this), false)); | 616 base::Unretained(this), false)); |
| 614 | 617 |
| 615 writer_.reset(service_->storage()->CreateResponseWriter(GURL())); | 618 writer_.reset(service_->storage()->CreateResponseWriter(GURL())); |
| 616 written_response_id_ = writer_->response_id(); | 619 written_response_id_ = writer_->response_id(); |
| 617 WriteBasicResponse(); | 620 WriteBasicResponse(); |
| 618 // Continues async | 621 // Continues async |
| 619 } | 622 } |
| 620 | 623 |
| 621 void RequestAppCachedResource(bool start_after_delivery_orders) { | 624 void RequestAppCachedResource(bool start_after_delivery_orders) { |
| 622 AppCacheStorage* storage = service_->storage(); | 625 AppCacheStorage* storage = service_->storage(); |
| 623 request_ = empty_context_->CreateRequest(GURL("http://blah/"), | 626 request_ = empty_context_->CreateRequest( |
| 624 net::DEFAULT_PRIORITY, | 627 GURL("http://blah/"), net::DEFAULT_PRIORITY, |
| 625 url_request_delegate_.get()); | 628 url_request_delegate_.get(), TRAFFIC_ANNOTATION_FOR_TESTS); |
| 626 | 629 |
| 627 // Setup to create an AppCacheURLRequestJob with orders to deliver | 630 // Setup to create an AppCacheURLRequestJob with orders to deliver |
| 628 // a network response. | 631 // a network response. |
| 629 std::unique_ptr<AppCacheURLRequestJob> job( | 632 std::unique_ptr<AppCacheURLRequestJob> job( |
| 630 new AppCacheURLRequestJob(request_.get(), NULL, storage, NULL, false, | 633 new AppCacheURLRequestJob(request_.get(), NULL, storage, NULL, false, |
| 631 base::Bind(&ExpectNotRestarted))); | 634 base::Bind(&ExpectNotRestarted))); |
| 632 | 635 |
| 633 if (start_after_delivery_orders) { | 636 if (start_after_delivery_orders) { |
| 634 job->DeliverAppCachedResponse( | 637 job->DeliverAppCachedResponse( |
| 635 GURL(), 111, | 638 GURL(), 111, |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 PushNextTask(base::Bind( | 732 PushNextTask(base::Bind( |
| 730 &AppCacheURLRequestJobTest::MakeRangeRequest, base::Unretained(this))); | 733 &AppCacheURLRequestJobTest::MakeRangeRequest, base::Unretained(this))); |
| 731 writer_.reset(service_->storage()->CreateResponseWriter(GURL())); | 734 writer_.reset(service_->storage()->CreateResponseWriter(GURL())); |
| 732 written_response_id_ = writer_->response_id(); | 735 written_response_id_ = writer_->response_id(); |
| 733 WriteBasicResponse(); | 736 WriteBasicResponse(); |
| 734 // Continues async | 737 // Continues async |
| 735 } | 738 } |
| 736 | 739 |
| 737 void MakeRangeRequest() { | 740 void MakeRangeRequest() { |
| 738 AppCacheStorage* storage = service_->storage(); | 741 AppCacheStorage* storage = service_->storage(); |
| 739 request_ = empty_context_->CreateRequest(GURL("http://blah/"), | 742 request_ = empty_context_->CreateRequest( |
| 740 net::DEFAULT_PRIORITY, | 743 GURL("http://blah/"), net::DEFAULT_PRIORITY, |
| 741 url_request_delegate_.get()); | 744 url_request_delegate_.get(), TRAFFIC_ANNOTATION_FOR_TESTS); |
| 742 | 745 |
| 743 // Request a range, the 3 middle chars out of 'Hello' | 746 // Request a range, the 3 middle chars out of 'Hello' |
| 744 net::HttpRequestHeaders extra_headers; | 747 net::HttpRequestHeaders extra_headers; |
| 745 extra_headers.SetHeader("Range", "bytes= 1-3"); | 748 extra_headers.SetHeader("Range", "bytes= 1-3"); |
| 746 request_->SetExtraRequestHeaders(extra_headers); | 749 request_->SetExtraRequestHeaders(extra_headers); |
| 747 | 750 |
| 748 // Create job with orders to deliver an appcached entry. | 751 // Create job with orders to deliver an appcached entry. |
| 749 std::unique_ptr<AppCacheURLRequestJob> job( | 752 std::unique_ptr<AppCacheURLRequestJob> job( |
| 750 new AppCacheURLRequestJob(request_.get(), NULL, storage, NULL, false, | 753 new AppCacheURLRequestJob(request_.get(), NULL, storage, NULL, false, |
| 751 base::Bind(&ExpectNotRestarted))); | 754 base::Bind(&ExpectNotRestarted))); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 896 | 899 |
| 897 TEST_F(AppCacheURLRequestJobTest, CancelRequest) { | 900 TEST_F(AppCacheURLRequestJobTest, CancelRequest) { |
| 898 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequest); | 901 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequest); |
| 899 } | 902 } |
| 900 | 903 |
| 901 TEST_F(AppCacheURLRequestJobTest, CancelRequestWithIOPending) { | 904 TEST_F(AppCacheURLRequestJobTest, CancelRequestWithIOPending) { |
| 902 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending); | 905 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending); |
| 903 } | 906 } |
| 904 | 907 |
| 905 } // namespace content | 908 } // namespace content |
| OLD | NEW |