| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/message_loop_proxy.h" | 12 #include "base/message_loop_proxy.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "crypto/nss_util.h" | 17 #include "crypto/nss_util.h" |
| 18 #include "net/base/mock_host_resolver.h" | 18 #include "net/base/mock_host_resolver.h" |
| 19 #include "net/base/network_change_notifier.h" | 19 #include "net/base/network_change_notifier.h" |
| 20 #include "net/base/upload_data_stream.h" |
| 21 #include "net/base/upload_file_element_reader.h" |
| 20 #include "net/http/http_response_headers.h" | 22 #include "net/http/http_response_headers.h" |
| 21 #include "net/test/test_server.h" | 23 #include "net/test/test_server.h" |
| 22 #include "net/url_request/url_fetcher_delegate.h" | 24 #include "net/url_request/url_fetcher_delegate.h" |
| 23 #include "net/url_request/url_request_context_getter.h" | 25 #include "net/url_request/url_request_context_getter.h" |
| 24 #include "net/url_request/url_request_test_util.h" | 26 #include "net/url_request/url_request_test_util.h" |
| 25 #include "net/url_request/url_request_throttler_manager.h" | 27 #include "net/url_request/url_request_throttler_manager.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 29 |
| 28 #if defined(USE_NSS) || defined(OS_IOS) | 30 #if defined(USE_NSS) || defined(OS_IOS) |
| 29 #include "net/ocsp/nss_ocsp.h" | 31 #include "net/ocsp/nss_ocsp.h" |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 // Version of URLFetcherTest that does a POST instead | 251 // Version of URLFetcherTest that does a POST instead |
| 250 class URLFetcherPostTest : public URLFetcherTest { | 252 class URLFetcherPostTest : public URLFetcherTest { |
| 251 public: | 253 public: |
| 252 // URLFetcherTest: | 254 // URLFetcherTest: |
| 253 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 255 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 254 | 256 |
| 255 // URLFetcherDelegate: | 257 // URLFetcherDelegate: |
| 256 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 258 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| 257 }; | 259 }; |
| 258 | 260 |
| 261 // Version of URLFetcherTest that does a POST of a file using |
| 262 // SetUploadDataStream |
| 263 class URLFetcherPostFileTest : public URLFetcherTest { |
| 264 public: |
| 265 URLFetcherPostFileTest(); |
| 266 |
| 267 // URLFetcherTest: |
| 268 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 269 |
| 270 // URLFetcherDelegate: |
| 271 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| 272 |
| 273 private: |
| 274 FilePath path_; |
| 275 }; |
| 276 |
| 259 // Version of URLFetcherTest that does a POST instead with empty upload body | 277 // Version of URLFetcherTest that does a POST instead with empty upload body |
| 260 class URLFetcherEmptyPostTest : public URLFetcherTest { | 278 class URLFetcherEmptyPostTest : public URLFetcherTest { |
| 261 public: | 279 public: |
| 262 // URLFetcherTest: | 280 // URLFetcherTest: |
| 263 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 281 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 264 | 282 |
| 265 // URLFetcherDelegate: | 283 // URLFetcherDelegate: |
| 266 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 284 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| 267 }; | 285 }; |
| 268 | 286 |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 fetcher_->Start(); | 534 fetcher_->Start(); |
| 517 } | 535 } |
| 518 | 536 |
| 519 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { | 537 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { |
| 520 std::string data; | 538 std::string data; |
| 521 EXPECT_TRUE(source->GetResponseAsString(&data)); | 539 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 522 EXPECT_EQ(std::string("bobsyeruncle"), data); | 540 EXPECT_EQ(std::string("bobsyeruncle"), data); |
| 523 URLFetcherTest::OnURLFetchComplete(source); | 541 URLFetcherTest::OnURLFetchComplete(source); |
| 524 } | 542 } |
| 525 | 543 |
| 544 URLFetcherPostFileTest::URLFetcherPostFileTest() { |
| 545 PathService::Get(base::DIR_SOURCE_ROOT, &path_); |
| 546 path_ = path_.Append(FILE_PATH_LITERAL("net")); |
| 547 path_ = path_.Append(FILE_PATH_LITERAL("data")); |
| 548 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 549 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 550 } |
| 551 |
| 552 void URLFetcherPostFileTest::CreateFetcher(const GURL& url) { |
| 553 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| 554 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
| 555 io_message_loop_proxy(), request_context())); |
| 556 scoped_ptr<UploadElementReader> reader(new UploadFileElementReader( |
| 557 base::MessageLoopProxy::current(), path_, 0, kuint64max, base::Time())); |
| 558 fetcher_->SetUploadDataStream( |
| 559 "application/x-www-form-urlencoded", |
| 560 make_scoped_ptr(UploadDataStream::CreateWithReader(reader.Pass(), 0))); |
| 561 fetcher_->Start(); |
| 562 } |
| 563 |
| 564 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) { |
| 565 int64 size = 0; |
| 566 ASSERT_EQ(true, file_util::GetFileSize(path_, &size)); |
| 567 scoped_array<char> expected(new char[size]); |
| 568 ASSERT_EQ(size, file_util::ReadFile(path_, expected.get(), size)); |
| 569 |
| 570 std::string data; |
| 571 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 572 EXPECT_EQ(std::string(&expected[0], size), data); |
| 573 URLFetcherTest::OnURLFetchComplete(source); |
| 574 } |
| 575 |
| 526 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { | 576 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { |
| 527 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 577 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| 528 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 578 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 529 io_message_loop_proxy())); | 579 io_message_loop_proxy())); |
| 530 fetcher_->SetUploadData("text/plain", ""); | 580 fetcher_->SetUploadData("text/plain", ""); |
| 531 fetcher_->Start(); | 581 fetcher_->Start(); |
| 532 } | 582 } |
| 533 | 583 |
| 534 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { | 584 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { |
| 535 EXPECT_TRUE(source->GetStatus().is_success()); | 585 EXPECT_TRUE(source->GetStatus().is_success()); |
| (...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1116 #endif | 1166 #endif |
| 1117 TestServer test_server(TestServer::TYPE_HTTP, | 1167 TestServer test_server(TestServer::TYPE_HTTP, |
| 1118 TestServer::kLocalhost, | 1168 TestServer::kLocalhost, |
| 1119 FilePath(kDocRoot)); | 1169 FilePath(kDocRoot)); |
| 1120 ASSERT_TRUE(test_server.Start()); | 1170 ASSERT_TRUE(test_server.Start()); |
| 1121 | 1171 |
| 1122 CreateFetcher(test_server.GetURL("echo")); | 1172 CreateFetcher(test_server.GetURL("echo")); |
| 1123 MessageLoop::current()->Run(); | 1173 MessageLoop::current()->Run(); |
| 1124 } | 1174 } |
| 1125 | 1175 |
| 1176 TEST_F(URLFetcherPostFileTest, Basic) { |
| 1177 TestServer test_server(TestServer::TYPE_HTTP, |
| 1178 TestServer::kLocalhost, |
| 1179 FilePath(kDocRoot)); |
| 1180 ASSERT_TRUE(test_server.Start()); |
| 1181 |
| 1182 CreateFetcher(test_server.GetURL("echo")); |
| 1183 MessageLoop::current()->Run(); |
| 1184 } |
| 1185 |
| 1126 TEST_F(URLFetcherEmptyPostTest, Basic) { | 1186 TEST_F(URLFetcherEmptyPostTest, Basic) { |
| 1127 TestServer test_server(TestServer::TYPE_HTTP, | 1187 TestServer test_server(TestServer::TYPE_HTTP, |
| 1128 TestServer::kLocalhost, | 1188 TestServer::kLocalhost, |
| 1129 FilePath(kDocRoot)); | 1189 FilePath(kDocRoot)); |
| 1130 ASSERT_TRUE(test_server.Start()); | 1190 ASSERT_TRUE(test_server.Start()); |
| 1131 | 1191 |
| 1132 CreateFetcher(test_server.GetURL("echo")); | 1192 CreateFetcher(test_server.GetURL("echo")); |
| 1133 MessageLoop::current()->Run(); | 1193 MessageLoop::current()->Run(); |
| 1134 } | 1194 } |
| 1135 | 1195 |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1551 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1611 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1552 | 1612 |
| 1553 MessageLoop::current()->RunUntilIdle(); | 1613 MessageLoop::current()->RunUntilIdle(); |
| 1554 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1614 ASSERT_FALSE(file_util::PathExists(file_path_)) |
| 1555 << file_path_.value() << " not removed."; | 1615 << file_path_.value() << " not removed."; |
| 1556 } | 1616 } |
| 1557 | 1617 |
| 1558 } // namespace | 1618 } // namespace |
| 1559 | 1619 |
| 1560 } // namespace net | 1620 } // namespace net |
| OLD | NEW |