| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/url_request/url_fetcher_impl.h" | 5 #include "net/url_request/url_fetcher_impl.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <limits> | 11 #include <limits> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 | 14 |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 17 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
| 18 #include "base/files/scoped_temp_dir.h" | 18 #include "base/files/scoped_temp_dir.h" |
| 19 #include "base/location.h" | 19 #include "base/location.h" |
| 20 #include "base/macros.h" | 20 #include "base/macros.h" |
| 21 #include "base/memory/ptr_util.h" | 21 #include "base/memory/ptr_util.h" |
| 22 #include "base/message_loop/message_loop.h" |
| 22 #include "base/path_service.h" | 23 #include "base/path_service.h" |
| 23 #include "base/run_loop.h" | 24 #include "base/run_loop.h" |
| 24 #include "base/single_thread_task_runner.h" | 25 #include "base/single_thread_task_runner.h" |
| 25 #include "base/strings/string_util.h" | 26 #include "base/strings/string_util.h" |
| 26 #include "base/strings/stringprintf.h" | 27 #include "base/strings/stringprintf.h" |
| 27 #include "base/synchronization/waitable_event.h" | 28 #include "base/synchronization/waitable_event.h" |
| 28 #include "base/task_scheduler/post_task.h" | 29 #include "base/task_scheduler/post_task.h" |
| 29 #include "base/test/scoped_task_scheduler.h" | 30 #include "base/test/scoped_task_scheduler.h" |
| 30 #include "base/test/test_timeouts.h" | 31 #include "base/test/test_timeouts.h" |
| 31 #include "base/threading/platform_thread.h" | 32 #include "base/threading/platform_thread.h" |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 | 307 |
| 307 base::Closure on_destruction_callback_; | 308 base::Closure on_destruction_callback_; |
| 308 | 309 |
| 309 DISALLOW_COPY_AND_ASSIGN(FetcherTestURLRequestContextGetter); | 310 DISALLOW_COPY_AND_ASSIGN(FetcherTestURLRequestContextGetter); |
| 310 }; | 311 }; |
| 311 | 312 |
| 312 } // namespace | 313 } // namespace |
| 313 | 314 |
| 314 class URLFetcherTest : public testing::Test { | 315 class URLFetcherTest : public testing::Test { |
| 315 public: | 316 public: |
| 316 URLFetcherTest() : num_upload_streams_created_(0) {} | 317 URLFetcherTest() |
| 318 : scoped_task_scheduler_(base::MessageLoop::current()), |
| 319 num_upload_streams_created_(0) {} |
| 317 | 320 |
| 318 static int GetNumFetcherCores() { | 321 static int GetNumFetcherCores() { |
| 319 return URLFetcherImpl::GetNumFetcherCores(); | 322 return URLFetcherImpl::GetNumFetcherCores(); |
| 320 } | 323 } |
| 321 | 324 |
| 322 // Creates a URLRequestContextGetter with a URLRequestContext that lives on | 325 // Creates a URLRequestContextGetter with a URLRequestContext that lives on |
| 323 // the current thread. | 326 // the current thread. |
| 324 scoped_refptr<FetcherTestURLRequestContextGetter> | 327 scoped_refptr<FetcherTestURLRequestContextGetter> |
| 325 CreateSameThreadContextGetter() { | 328 CreateSameThreadContextGetter() { |
| 326 return scoped_refptr<FetcherTestURLRequestContextGetter>( | 329 return scoped_refptr<FetcherTestURLRequestContextGetter>( |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 #endif | 447 #endif |
| 445 } | 448 } |
| 446 | 449 |
| 447 // Initializes |test_server_| without starting it. Allows subclasses to use | 450 // Initializes |test_server_| without starting it. Allows subclasses to use |
| 448 // their own server configuration. | 451 // their own server configuration. |
| 449 virtual void SetUpServer() { | 452 virtual void SetUpServer() { |
| 450 test_server_.reset(new EmbeddedTestServer); | 453 test_server_.reset(new EmbeddedTestServer); |
| 451 test_server_->AddDefaultHandlers(base::FilePath(kDocRoot)); | 454 test_server_->AddDefaultHandlers(base::FilePath(kDocRoot)); |
| 452 } | 455 } |
| 453 | 456 |
| 457 base::test::ScopedTaskScheduler scoped_task_scheduler_; |
| 458 |
| 454 // Network thread for cross-thread tests. Most threads just use the main | 459 // Network thread for cross-thread tests. Most threads just use the main |
| 455 // thread for network activity. | 460 // thread for network activity. |
| 456 std::unique_ptr<base::Thread> network_thread_; | 461 std::unique_ptr<base::Thread> network_thread_; |
| 457 | 462 |
| 458 std::unique_ptr<EmbeddedTestServer> test_server_; | 463 std::unique_ptr<EmbeddedTestServer> test_server_; |
| 459 GURL hanging_url_; | 464 GURL hanging_url_; |
| 460 | 465 |
| 461 size_t num_upload_streams_created_; | 466 size_t num_upload_streams_created_; |
| 462 }; | 467 }; |
| 463 | 468 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 518 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
| 514 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 519 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
| 515 std::string data; | 520 std::string data; |
| 516 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 521 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
| 517 EXPECT_EQ(kDefaultResponseBody, data); | 522 EXPECT_EQ(kDefaultResponseBody, data); |
| 518 } | 523 } |
| 519 | 524 |
| 520 // Create the fetcher from a sequenced (not single-threaded) task. Verify that | 525 // Create the fetcher from a sequenced (not single-threaded) task. Verify that |
| 521 // the expected response is received. | 526 // the expected response is received. |
| 522 TEST_F(URLFetcherTest, SequencedTaskTest) { | 527 TEST_F(URLFetcherTest, SequencedTaskTest) { |
| 523 base::test::ScopedTaskScheduler scoped_task_scheduler( | |
| 524 base::MessageLoop::current()); | |
| 525 auto sequenced_task_runner = | 528 auto sequenced_task_runner = |
| 526 base::CreateSequencedTaskRunnerWithTraits(base::TaskTraits()); | 529 base::CreateSequencedTaskRunnerWithTraits(base::TaskTraits()); |
| 527 | 530 |
| 528 auto delegate = base::MakeUnique<WaitingURLFetcherDelegate>(); | 531 auto delegate = base::MakeUnique<WaitingURLFetcherDelegate>(); |
| 529 sequenced_task_runner->PostTask( | 532 sequenced_task_runner->PostTask( |
| 530 FROM_HERE, base::Bind(&WaitingURLFetcherDelegate::CreateFetcher, | 533 FROM_HERE, base::Bind(&WaitingURLFetcherDelegate::CreateFetcher, |
| 531 base::Unretained(delegate.get()), | 534 base::Unretained(delegate.get()), |
| 532 test_server_->GetURL(kDefaultResponsePath), | 535 test_server_->GetURL(kDefaultResponsePath), |
| 533 URLFetcher::GET, CreateCrossThreadContextGetter())); | 536 URLFetcher::GET, CreateCrossThreadContextGetter())); |
| 534 base::RunLoop().RunUntilIdle(); | 537 base::RunLoop().RunUntilIdle(); |
| (...skipping 1033 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1568 EXPECT_EQ(-1, delegate.fetcher()->GetResponseCode()); | 1571 EXPECT_EQ(-1, delegate.fetcher()->GetResponseCode()); |
| 1569 EXPECT_FALSE(delegate.fetcher()->GetResponseHeaders()); | 1572 EXPECT_FALSE(delegate.fetcher()->GetResponseHeaders()); |
| 1570 std::string data; | 1573 std::string data; |
| 1571 EXPECT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 1574 EXPECT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
| 1572 EXPECT_TRUE(data.empty()); | 1575 EXPECT_TRUE(data.empty()); |
| 1573 } | 1576 } |
| 1574 | 1577 |
| 1575 } // namespace | 1578 } // namespace |
| 1576 | 1579 |
| 1577 } // namespace net | 1580 } // namespace net |
| OLD | NEW |