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