Chromium Code Reviews| 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) |
| 31 #include "net/ocsp/nss_ocsp.h" | 35 #include "net/ocsp/nss_ocsp.h" |
| 32 #endif | 36 #endif |
| 33 | 37 |
| 34 namespace net { | 38 namespace net { |
| 35 | 39 |
| 36 using base::Time; | 40 using base::Time; |
| 37 using base::TimeDelta; | 41 using base::TimeDelta; |
| 38 | 42 |
| 39 // TODO(eroman): Add a regression test for http://crbug.com/40505. | 43 // TODO(eroman): Add a regression test for http://crbug.com/40505. |
| 40 | 44 |
| 41 namespace { | 45 namespace { |
| 42 | 46 |
| 43 // TODO(akalin): Move all the test data to somewhere under net/. | 47 // TODO(akalin): Move all the test data to somewhere under net/. |
| 44 const base::FilePath::CharType kDocRoot[] = | 48 const base::FilePath::CharType kDocRoot[] = |
| 45 FILE_PATH_LITERAL("net/data/url_fetcher_impl_unittest"); | 49 FILE_PATH_LITERAL("net/data/url_fetcher_impl_unittest"); |
| 46 const char kTestServerFilePrefix[] = "files/"; | 50 const char kTestServerFilePrefix[] = "files/"; |
| 47 | 51 |
| 52 scoped_ptr<UploadDataStream> CreateUploadStream(const base::FilePath& path) { | |
| 53 ScopedVector<UploadElementReader> readers; | |
| 54 const std::string str("bobsyeruncle\n"); | |
| 55 std::vector<char> buffer(str.begin(), str.end()); | |
| 56 readers.push_back(new UploadOwnedBytesElementReader(&buffer)); | |
| 57 readers.push_back( | |
| 58 new UploadFileElementReader(base::MessageLoopProxy::current().get(), path, | |
| 59 0, kuint64max, base::Time())); | |
| 60 return make_scoped_ptr<UploadDataStream>( | |
| 61 new ElementsUploadDataStream(readers.Pass(), 0)).Pass(); | |
|
mmenke
2015/01/09 17:42:25
Do we get anything from having multiple elements h
hirono
2015/01/15 08:14:27
No, let me simplify it.
| |
| 62 } | |
| 63 | |
| 48 class ThrottlingTestURLRequestContext : public TestURLRequestContext { | 64 class ThrottlingTestURLRequestContext : public TestURLRequestContext { |
| 49 public: | 65 public: |
| 50 ThrottlingTestURLRequestContext() : TestURLRequestContext(true) { | 66 ThrottlingTestURLRequestContext() : TestURLRequestContext(true) { |
| 51 set_throttler_manager(&throttler_manager_); | 67 set_throttler_manager(&throttler_manager_); |
| 52 Init(); | 68 Init(); |
| 53 DCHECK(throttler_manager() != NULL); | 69 DCHECK(throttler_manager() != NULL); |
| 54 } | 70 } |
| 55 | 71 |
| 56 private: | 72 private: |
| 57 URLRequestThrottlerManager throttler_manager_; | 73 URLRequestThrottlerManager throttler_manager_; |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 252 | 268 |
| 253 // URLFetcherDelegate: | 269 // URLFetcherDelegate: |
| 254 void OnURLFetchComplete(const URLFetcher* source) override; | 270 void OnURLFetchComplete(const URLFetcher* source) override; |
| 255 | 271 |
| 256 private: | 272 private: |
| 257 base::FilePath path_; | 273 base::FilePath path_; |
| 258 uint64 range_offset_; | 274 uint64 range_offset_; |
| 259 uint64 range_length_; | 275 uint64 range_length_; |
| 260 }; | 276 }; |
| 261 | 277 |
| 278 class URLFetcherPostStreamTest : public URLFetcherTest { | |
| 279 public: | |
| 280 URLFetcherPostStreamTest(); | |
| 281 | |
| 282 // URLFetcherTest: | |
| 283 void CreateFetcher(const GURL& url) override; | |
| 284 | |
| 285 // URLFetcherDelegate: | |
| 286 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 287 | |
| 288 private: | |
| 289 base::FilePath path_; | |
| 290 }; | |
| 291 | |
| 262 // Version of URLFetcherTest that does a POST instead with empty upload body | 292 // Version of URLFetcherTest that does a POST instead with empty upload body |
| 263 class URLFetcherEmptyPostTest : public URLFetcherTest { | 293 class URLFetcherEmptyPostTest : public URLFetcherTest { |
| 264 public: | 294 public: |
| 265 // URLFetcherTest: | 295 // URLFetcherTest: |
| 266 void CreateFetcher(const GURL& url) override; | 296 void CreateFetcher(const GURL& url) override; |
| 267 | 297 |
| 268 // URLFetcherDelegate: | 298 // URLFetcherDelegate: |
| 269 void OnURLFetchComplete(const URLFetcher* source) override; | 299 void OnURLFetchComplete(const URLFetcher* source) override; |
| 270 }; | 300 }; |
| 271 | 301 |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 564 ASSERT_LE(range_offset_, expected.size()); | 594 ASSERT_LE(range_offset_, expected.size()); |
| 565 uint64 expected_size = | 595 uint64 expected_size = |
| 566 std::min(range_length_, expected.size() - range_offset_); | 596 std::min(range_length_, expected.size() - range_offset_); |
| 567 | 597 |
| 568 std::string data; | 598 std::string data; |
| 569 EXPECT_TRUE(source->GetResponseAsString(&data)); | 599 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 570 EXPECT_EQ(expected.substr(range_offset_, expected_size), data); | 600 EXPECT_EQ(expected.substr(range_offset_, expected_size), data); |
| 571 URLFetcherTest::OnURLFetchComplete(source); | 601 URLFetcherTest::OnURLFetchComplete(source); |
| 572 } | 602 } |
| 573 | 603 |
| 604 URLFetcherPostStreamTest::URLFetcherPostStreamTest() { | |
| 605 PathService::Get(base::DIR_SOURCE_ROOT, &path_); | |
| 606 path_ = path_.Append(FILE_PATH_LITERAL("net")); | |
| 607 path_ = path_.Append(FILE_PATH_LITERAL("data")); | |
| 608 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest")); | |
| 609 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); | |
| 610 } | |
| 611 | |
| 612 void URLFetcherPostStreamTest::CreateFetcher(const GURL& url) { | |
|
mmenke
2015/01/09 17:42:25
We really should make sure the test actually retri
hirono
2015/01/15 08:14:27
I added status parameter to the "echo" URL. Done.
| |
| 613 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | |
| 614 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 615 io_message_loop_proxy().get(), request_context())); | |
| 616 fetcher_->SetUploadStreamFactory("text/plain", | |
| 617 base::Bind(CreateUploadStream, path_)); | |
| 618 fetcher_->Start(); | |
| 619 } | |
| 620 | |
| 621 void URLFetcherPostStreamTest::OnURLFetchComplete(const URLFetcher* source) { | |
| 622 std::string data; | |
| 623 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
| 624 | |
| 625 std::string expected; | |
| 626 ASSERT_TRUE(base::ReadFileToString(path_, &expected)); | |
| 627 EXPECT_EQ(std::string("bobsyeruncle\n") + expected, data); | |
| 628 | |
| 629 URLFetcherTest::OnURLFetchComplete(source); | |
| 630 } | |
| 631 | |
| 574 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { | 632 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { |
| 575 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 633 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| 576 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 634 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 577 io_message_loop_proxy())); | 635 io_message_loop_proxy())); |
| 578 fetcher_->SetUploadData("text/plain", std::string()); | 636 fetcher_->SetUploadData("text/plain", std::string()); |
| 579 fetcher_->Start(); | 637 fetcher_->Start(); |
| 580 } | 638 } |
| 581 | 639 |
| 582 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { | 640 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { |
| 583 EXPECT_TRUE(source->GetStatus().is_success()); | 641 EXPECT_TRUE(source->GetStatus().is_success()); |
| (...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1078 SpawnedTestServer::kLocalhost, | 1136 SpawnedTestServer::kLocalhost, |
| 1079 base::FilePath(kDocRoot)); | 1137 base::FilePath(kDocRoot)); |
| 1080 ASSERT_TRUE(test_server.Start()); | 1138 ASSERT_TRUE(test_server.Start()); |
| 1081 | 1139 |
| 1082 SetUploadRange(30, 100); | 1140 SetUploadRange(30, 100); |
| 1083 | 1141 |
| 1084 CreateFetcher(test_server.GetURL("echo")); | 1142 CreateFetcher(test_server.GetURL("echo")); |
| 1085 base::MessageLoop::current()->Run(); | 1143 base::MessageLoop::current()->Run(); |
| 1086 } | 1144 } |
| 1087 | 1145 |
| 1146 TEST_F(URLFetcherPostStreamTest, Basic) { | |
| 1147 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1148 SpawnedTestServer::kLocalhost, | |
| 1149 base::FilePath(kDocRoot)); | |
| 1150 ASSERT_TRUE(test_server.Start()); | |
| 1151 | |
| 1152 CreateFetcher(test_server.GetURL("echo")); | |
| 1153 base::MessageLoop::current()->Run(); | |
| 1154 } | |
| 1155 | |
| 1088 TEST_F(URLFetcherEmptyPostTest, Basic) { | 1156 TEST_F(URLFetcherEmptyPostTest, Basic) { |
| 1089 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | 1157 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
| 1090 SpawnedTestServer::kLocalhost, | 1158 SpawnedTestServer::kLocalhost, |
| 1091 base::FilePath(kDocRoot)); | 1159 base::FilePath(kDocRoot)); |
| 1092 ASSERT_TRUE(test_server.Start()); | 1160 ASSERT_TRUE(test_server.Start()); |
| 1093 | 1161 |
| 1094 CreateFetcher(test_server.GetURL("echo")); | 1162 CreateFetcher(test_server.GetURL("echo")); |
| 1095 base::MessageLoop::current()->Run(); | 1163 base::MessageLoop::current()->Run(); |
| 1096 } | 1164 } |
| 1097 | 1165 |
| (...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1534 | 1602 |
| 1535 base::MessageLoop::current()->RunUntilIdle(); | 1603 base::MessageLoop::current()->RunUntilIdle(); |
| 1536 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) << | 1604 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) << |
| 1537 "FilePath: " << file_path_.value(); | 1605 "FilePath: " << file_path_.value(); |
| 1538 } | 1606 } |
| 1539 } | 1607 } |
| 1540 | 1608 |
| 1541 } // namespace | 1609 } // namespace |
| 1542 | 1610 |
| 1543 } // namespace net | 1611 } // namespace net |
| OLD | NEW |