| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/message_loop/message_loop_proxy.h" | 13 #include "base/message_loop/message_loop_proxy.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 17 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "crypto/nss_util.h" | 19 #include "crypto/nss_util.h" |
| 20 #include "net/base/elements_upload_data_stream.h" |
| 20 #include "net/base/network_change_notifier.h" | 21 #include "net/base/network_change_notifier.h" |
| 22 #include "net/base/upload_bytes_element_reader.h" |
| 23 #include "net/base/upload_element_reader.h" |
| 24 #include "net/base/upload_file_element_reader.h" |
| 21 #include "net/dns/mock_host_resolver.h" | 25 #include "net/dns/mock_host_resolver.h" |
| 22 #include "net/http/http_response_headers.h" | 26 #include "net/http/http_response_headers.h" |
| 23 #include "net/test/spawned_test_server/spawned_test_server.h" | 27 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 24 #include "net/url_request/url_fetcher_delegate.h" | 28 #include "net/url_request/url_fetcher_delegate.h" |
| 25 #include "net/url_request/url_request_context_getter.h" | 29 #include "net/url_request/url_request_context_getter.h" |
| 26 #include "net/url_request/url_request_test_util.h" | 30 #include "net/url_request/url_request_test_util.h" |
| 27 #include "net/url_request/url_request_throttler_manager.h" | 31 #include "net/url_request/url_request_throttler_manager.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 33 |
| 30 #if defined(USE_NSS) || defined(OS_IOS) | 34 #if defined(USE_NSS) || defined(OS_IOS) |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 | 256 |
| 253 // URLFetcherDelegate: | 257 // URLFetcherDelegate: |
| 254 void OnURLFetchComplete(const URLFetcher* source) override; | 258 void OnURLFetchComplete(const URLFetcher* source) override; |
| 255 | 259 |
| 256 private: | 260 private: |
| 257 base::FilePath path_; | 261 base::FilePath path_; |
| 258 uint64 range_offset_; | 262 uint64 range_offset_; |
| 259 uint64 range_length_; | 263 uint64 range_length_; |
| 260 }; | 264 }; |
| 261 | 265 |
| 266 class URLFetcherPostStreamTest : public URLFetcherTest { |
| 267 public: |
| 268 URLFetcherPostStreamTest(); |
| 269 |
| 270 // URLFetcherTest: |
| 271 void CreateFetcher(const GURL& url) override; |
| 272 |
| 273 // URLFetcherDelegate: |
| 274 void OnURLFetchComplete(const URLFetcher* source) override; |
| 275 |
| 276 private: |
| 277 base::FilePath path_; |
| 278 }; |
| 279 |
| 262 // Version of URLFetcherTest that does a POST instead with empty upload body | 280 // Version of URLFetcherTest that does a POST instead with empty upload body |
| 263 class URLFetcherEmptyPostTest : public URLFetcherTest { | 281 class URLFetcherEmptyPostTest : public URLFetcherTest { |
| 264 public: | 282 public: |
| 265 // URLFetcherTest: | 283 // URLFetcherTest: |
| 266 void CreateFetcher(const GURL& url) override; | 284 void CreateFetcher(const GURL& url) override; |
| 267 | 285 |
| 268 // URLFetcherDelegate: | 286 // URLFetcherDelegate: |
| 269 void OnURLFetchComplete(const URLFetcher* source) override; | 287 void OnURLFetchComplete(const URLFetcher* source) override; |
| 270 }; | 288 }; |
| 271 | 289 |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 ASSERT_LE(range_offset_, expected.size()); | 582 ASSERT_LE(range_offset_, expected.size()); |
| 565 uint64 expected_size = | 583 uint64 expected_size = |
| 566 std::min(range_length_, expected.size() - range_offset_); | 584 std::min(range_length_, expected.size() - range_offset_); |
| 567 | 585 |
| 568 std::string data; | 586 std::string data; |
| 569 EXPECT_TRUE(source->GetResponseAsString(&data)); | 587 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 570 EXPECT_EQ(expected.substr(range_offset_, expected_size), data); | 588 EXPECT_EQ(expected.substr(range_offset_, expected_size), data); |
| 571 URLFetcherTest::OnURLFetchComplete(source); | 589 URLFetcherTest::OnURLFetchComplete(source); |
| 572 } | 590 } |
| 573 | 591 |
| 592 URLFetcherPostStreamTest::URLFetcherPostStreamTest() { |
| 593 PathService::Get(base::DIR_SOURCE_ROOT, &path_); |
| 594 path_ = path_.Append(FILE_PATH_LITERAL("net")); |
| 595 path_ = path_.Append(FILE_PATH_LITERAL("data")); |
| 596 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 597 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 598 } |
| 599 |
| 600 void URLFetcherPostStreamTest::CreateFetcher(const GURL& url) { |
| 601 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| 602 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
| 603 io_message_loop_proxy().get(), request_context())); |
| 604 |
| 605 ScopedVector<UploadElementReader> readers; |
| 606 const std::string str("bobsyeruncle\n"); |
| 607 std::vector<char> buffer(str.begin(), str.end()); |
| 608 readers.push_back(new UploadOwnedBytesElementReader(&buffer)); |
| 609 readers.push_back( |
| 610 new UploadFileElementReader(base::MessageLoopProxy::current().get(), |
| 611 path_, 0, kuint64max, base::Time())); |
| 612 scoped_ptr<ElementsUploadDataStream> stream( |
| 613 new ElementsUploadDataStream(readers.Pass(), 0)); |
| 614 |
| 615 fetcher_->SetUploadStream("text/plain", stream.Pass()); |
| 616 fetcher_->Start(); |
| 617 } |
| 618 |
| 619 void URLFetcherPostStreamTest::OnURLFetchComplete(const URLFetcher* source) { |
| 620 std::string data; |
| 621 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 622 |
| 623 std::string expected; |
| 624 ASSERT_TRUE(base::ReadFileToString(path_, &expected)); |
| 625 EXPECT_EQ(std::string("bobsyeruncle\n") + expected, data); |
| 626 |
| 627 URLFetcherTest::OnURLFetchComplete(source); |
| 628 } |
| 629 |
| 574 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { | 630 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { |
| 575 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 631 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| 576 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 632 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 577 io_message_loop_proxy())); | 633 io_message_loop_proxy())); |
| 578 fetcher_->SetUploadData("text/plain", std::string()); | 634 fetcher_->SetUploadData("text/plain", std::string()); |
| 579 fetcher_->Start(); | 635 fetcher_->Start(); |
| 580 } | 636 } |
| 581 | 637 |
| 582 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { | 638 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { |
| 583 EXPECT_TRUE(source->GetStatus().is_success()); | 639 EXPECT_TRUE(source->GetStatus().is_success()); |
| (...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1078 SpawnedTestServer::kLocalhost, | 1134 SpawnedTestServer::kLocalhost, |
| 1079 base::FilePath(kDocRoot)); | 1135 base::FilePath(kDocRoot)); |
| 1080 ASSERT_TRUE(test_server.Start()); | 1136 ASSERT_TRUE(test_server.Start()); |
| 1081 | 1137 |
| 1082 SetUploadRange(30, 100); | 1138 SetUploadRange(30, 100); |
| 1083 | 1139 |
| 1084 CreateFetcher(test_server.GetURL("echo")); | 1140 CreateFetcher(test_server.GetURL("echo")); |
| 1085 base::MessageLoop::current()->Run(); | 1141 base::MessageLoop::current()->Run(); |
| 1086 } | 1142 } |
| 1087 | 1143 |
| 1144 TEST_F(URLFetcherPostStreamTest, Basic) { |
| 1145 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
| 1146 SpawnedTestServer::kLocalhost, |
| 1147 base::FilePath(kDocRoot)); |
| 1148 ASSERT_TRUE(test_server.Start()); |
| 1149 |
| 1150 CreateFetcher(test_server.GetURL("echo")); |
| 1151 base::MessageLoop::current()->Run(); |
| 1152 } |
| 1153 |
| 1088 TEST_F(URLFetcherEmptyPostTest, Basic) { | 1154 TEST_F(URLFetcherEmptyPostTest, Basic) { |
| 1089 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | 1155 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
| 1090 SpawnedTestServer::kLocalhost, | 1156 SpawnedTestServer::kLocalhost, |
| 1091 base::FilePath(kDocRoot)); | 1157 base::FilePath(kDocRoot)); |
| 1092 ASSERT_TRUE(test_server.Start()); | 1158 ASSERT_TRUE(test_server.Start()); |
| 1093 | 1159 |
| 1094 CreateFetcher(test_server.GetURL("echo")); | 1160 CreateFetcher(test_server.GetURL("echo")); |
| 1095 base::MessageLoop::current()->Run(); | 1161 base::MessageLoop::current()->Run(); |
| 1096 } | 1162 } |
| 1097 | 1163 |
| (...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1534 | 1600 |
| 1535 base::MessageLoop::current()->RunUntilIdle(); | 1601 base::MessageLoop::current()->RunUntilIdle(); |
| 1536 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) << | 1602 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) << |
| 1537 "FilePath: " << file_path_.value(); | 1603 "FilePath: " << file_path_.value(); |
| 1538 } | 1604 } |
| 1539 } | 1605 } |
| 1540 | 1606 |
| 1541 } // namespace | 1607 } // namespace |
| 1542 | 1608 |
| 1543 } // namespace net | 1609 } // namespace net |
| OLD | NEW |