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/path_service.h" | 22 #include "base/path_service.h" |
23 #include "base/run_loop.h" | 23 #include "base/run_loop.h" |
24 #include "base/single_thread_task_runner.h" | 24 #include "base/single_thread_task_runner.h" |
25 #include "base/strings/string_util.h" | 25 #include "base/strings/string_util.h" |
26 #include "base/strings/stringprintf.h" | 26 #include "base/strings/stringprintf.h" |
27 #include "base/synchronization/waitable_event.h" | 27 #include "base/synchronization/waitable_event.h" |
| 28 #include "base/task_scheduler/post_task.h" |
| 29 #include "base/test/scoped_task_scheduler.h" |
28 #include "base/test/test_timeouts.h" | 30 #include "base/test/test_timeouts.h" |
29 #include "base/threading/platform_thread.h" | 31 #include "base/threading/platform_thread.h" |
30 #include "base/threading/thread.h" | 32 #include "base/threading/thread.h" |
31 #include "base/threading/thread_task_runner_handle.h" | 33 #include "base/threading/thread_task_runner_handle.h" |
32 #include "build/build_config.h" | 34 #include "build/build_config.h" |
33 #include "crypto/nss_util.h" | 35 #include "crypto/nss_util.h" |
34 #include "net/base/elements_upload_data_stream.h" | 36 #include "net/base/elements_upload_data_stream.h" |
35 #include "net/base/network_change_notifier.h" | 37 #include "net/base/network_change_notifier.h" |
36 #include "net/base/upload_bytes_element_reader.h" | 38 #include "net/base/upload_bytes_element_reader.h" |
37 #include "net/base/upload_element_reader.h" | 39 #include "net/base/upload_element_reader.h" |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 URLFetcher::GET, CreateCrossThreadContextGetter()); | 510 URLFetcher::GET, CreateCrossThreadContextGetter()); |
509 delegate.StartFetcherAndWait(); | 511 delegate.StartFetcherAndWait(); |
510 | 512 |
511 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 513 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
512 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 514 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
513 std::string data; | 515 std::string data; |
514 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 516 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
515 EXPECT_EQ(kDefaultResponseBody, data); | 517 EXPECT_EQ(kDefaultResponseBody, data); |
516 } | 518 } |
517 | 519 |
| 520 // Create the fetcher from a sequenced (not single-threaded) task. Verify that |
| 521 // the expected response is received. |
| 522 TEST_F(URLFetcherTest, SequencedTaskTest) { |
| 523 base::test::ScopedTaskScheduler scoped_task_scheduler( |
| 524 base::MessageLoop::current()); |
| 525 auto sequenced_task_runner = |
| 526 base::CreateSequencedTaskRunnerWithTraits(base::TaskTraits()); |
| 527 |
| 528 auto delegate = base::MakeUnique<WaitingURLFetcherDelegate>(); |
| 529 sequenced_task_runner->PostTask( |
| 530 FROM_HERE, base::Bind(&WaitingURLFetcherDelegate::CreateFetcher, |
| 531 base::Unretained(delegate.get()), |
| 532 test_server_->GetURL(kDefaultResponsePath), |
| 533 URLFetcher::GET, CreateCrossThreadContextGetter())); |
| 534 base::RunLoop().RunUntilIdle(); |
| 535 delegate->StartFetcherAndWait(); |
| 536 |
| 537 EXPECT_TRUE(delegate->fetcher()->GetStatus().is_success()); |
| 538 EXPECT_EQ(200, delegate->fetcher()->GetResponseCode()); |
| 539 std::string data; |
| 540 ASSERT_TRUE(delegate->fetcher()->GetResponseAsString(&data)); |
| 541 EXPECT_EQ(kDefaultResponseBody, data); |
| 542 |
| 543 sequenced_task_runner->DeleteSoon(FROM_HERE, delegate.release()); |
| 544 base::RunLoop().RunUntilIdle(); |
| 545 } |
| 546 |
518 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. | 547 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. |
519 TEST_F(URLFetcherTest, CancelAll) { | 548 TEST_F(URLFetcherTest, CancelAll) { |
520 EXPECT_EQ(0, GetNumFetcherCores()); | 549 EXPECT_EQ(0, GetNumFetcherCores()); |
521 | 550 |
522 scoped_refptr<FetcherTestURLRequestContextGetter> context_getter( | 551 scoped_refptr<FetcherTestURLRequestContextGetter> context_getter( |
523 CreateSameThreadContextGetter()); | 552 CreateSameThreadContextGetter()); |
524 // Force context creation. | 553 // Force context creation. |
525 context_getter->GetURLRequestContext(); | 554 context_getter->GetURLRequestContext(); |
526 MockHostResolver* mock_resolver = context_getter->context()->mock_resolver(); | 555 MockHostResolver* mock_resolver = context_getter->context()->mock_resolver(); |
527 | 556 |
(...skipping 1011 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1539 EXPECT_EQ(-1, delegate.fetcher()->GetResponseCode()); | 1568 EXPECT_EQ(-1, delegate.fetcher()->GetResponseCode()); |
1540 EXPECT_FALSE(delegate.fetcher()->GetResponseHeaders()); | 1569 EXPECT_FALSE(delegate.fetcher()->GetResponseHeaders()); |
1541 std::string data; | 1570 std::string data; |
1542 EXPECT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 1571 EXPECT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
1543 EXPECT_TRUE(data.empty()); | 1572 EXPECT_TRUE(data.empty()); |
1544 } | 1573 } |
1545 | 1574 |
1546 } // namespace | 1575 } // namespace |
1547 | 1576 |
1548 } // namespace net | 1577 } // namespace net |
OLD | NEW |