| 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" | |
| 22 #include "net/http/http_response_headers.h" | 20 #include "net/http/http_response_headers.h" |
| 23 #include "net/test/test_server.h" | 21 #include "net/test/test_server.h" |
| 24 #include "net/url_request/url_fetcher_delegate.h" | 22 #include "net/url_request/url_fetcher_delegate.h" |
| 25 #include "net/url_request/url_request_context_getter.h" | 23 #include "net/url_request/url_request_context_getter.h" |
| 26 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
| 27 #include "net/url_request/url_request_throttler_manager.h" | 25 #include "net/url_request/url_request_throttler_manager.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 27 |
| 30 #if defined(USE_NSS) || defined(OS_IOS) | 28 #if defined(USE_NSS) || defined(OS_IOS) |
| 31 #include "net/ocsp/nss_ocsp.h" | 29 #include "net/ocsp/nss_ocsp.h" |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 // Version of URLFetcherTest that does a POST instead | 230 // Version of URLFetcherTest that does a POST instead |
| 233 class URLFetcherPostTest : public URLFetcherTest { | 231 class URLFetcherPostTest : public URLFetcherTest { |
| 234 public: | 232 public: |
| 235 // URLFetcherTest: | 233 // URLFetcherTest: |
| 236 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 234 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 237 | 235 |
| 238 // URLFetcherDelegate: | 236 // URLFetcherDelegate: |
| 239 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 237 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| 240 }; | 238 }; |
| 241 | 239 |
| 242 // Version of URLFetcherTest that does a POST of a file using | |
| 243 // SetUploadDataStream | |
| 244 class URLFetcherPostFileTest : public URLFetcherTest { | |
| 245 public: | |
| 246 URLFetcherPostFileTest(); | |
| 247 | |
| 248 // URLFetcherTest: | |
| 249 virtual void CreateFetcher(const GURL& url) OVERRIDE; | |
| 250 | |
| 251 // URLFetcherDelegate: | |
| 252 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | |
| 253 | |
| 254 private: | |
| 255 base::FilePath path_; | |
| 256 }; | |
| 257 | |
| 258 // Version of URLFetcherTest that does a POST instead with empty upload body | 240 // Version of URLFetcherTest that does a POST instead with empty upload body |
| 259 class URLFetcherEmptyPostTest : public URLFetcherTest { | 241 class URLFetcherEmptyPostTest : public URLFetcherTest { |
| 260 public: | 242 public: |
| 261 // URLFetcherTest: | 243 // URLFetcherTest: |
| 262 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 244 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 263 | 245 |
| 264 // URLFetcherDelegate: | 246 // URLFetcherDelegate: |
| 265 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 247 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| 266 }; | 248 }; |
| 267 | 249 |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 fetcher_->Start(); | 497 fetcher_->Start(); |
| 516 } | 498 } |
| 517 | 499 |
| 518 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { | 500 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { |
| 519 std::string data; | 501 std::string data; |
| 520 EXPECT_TRUE(source->GetResponseAsString(&data)); | 502 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 521 EXPECT_EQ(std::string("bobsyeruncle"), data); | 503 EXPECT_EQ(std::string("bobsyeruncle"), data); |
| 522 URLFetcherTest::OnURLFetchComplete(source); | 504 URLFetcherTest::OnURLFetchComplete(source); |
| 523 } | 505 } |
| 524 | 506 |
| 525 URLFetcherPostFileTest::URLFetcherPostFileTest() { | |
| 526 PathService::Get(base::DIR_SOURCE_ROOT, &path_); | |
| 527 path_ = path_.Append(FILE_PATH_LITERAL("net")); | |
| 528 path_ = path_.Append(FILE_PATH_LITERAL("data")); | |
| 529 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest")); | |
| 530 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); | |
| 531 } | |
| 532 | |
| 533 void URLFetcherPostFileTest::CreateFetcher(const GURL& url) { | |
| 534 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | |
| 535 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 536 io_message_loop_proxy(), request_context())); | |
| 537 scoped_ptr<UploadElementReader> reader(new UploadFileElementReader( | |
| 538 base::MessageLoopProxy::current(), path_, 0, kuint64max, base::Time())); | |
| 539 fetcher_->SetUploadDataStream( | |
| 540 "application/x-www-form-urlencoded", | |
| 541 make_scoped_ptr(UploadDataStream::CreateWithReader(reader.Pass(), 0))); | |
| 542 fetcher_->Start(); | |
| 543 } | |
| 544 | |
| 545 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) { | |
| 546 int64 size = 0; | |
| 547 ASSERT_EQ(true, file_util::GetFileSize(path_, &size)); | |
| 548 scoped_array<char> expected(new char[size]); | |
| 549 ASSERT_EQ(size, file_util::ReadFile(path_, expected.get(), size)); | |
| 550 | |
| 551 std::string data; | |
| 552 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
| 553 EXPECT_EQ(std::string(&expected[0], size), data); | |
| 554 URLFetcherTest::OnURLFetchComplete(source); | |
| 555 } | |
| 556 | |
| 557 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { | 507 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { |
| 558 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 508 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| 559 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 509 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 560 io_message_loop_proxy())); | 510 io_message_loop_proxy())); |
| 561 fetcher_->SetUploadData("text/plain", ""); | 511 fetcher_->SetUploadData("text/plain", ""); |
| 562 fetcher_->Start(); | 512 fetcher_->Start(); |
| 563 } | 513 } |
| 564 | 514 |
| 565 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { | 515 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { |
| 566 EXPECT_TRUE(source->GetStatus().is_success()); | 516 EXPECT_TRUE(source->GetStatus().is_success()); |
| (...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1046 TEST_F(URLFetcherPostTest, Basic) { | 996 TEST_F(URLFetcherPostTest, Basic) { |
| 1047 TestServer test_server(TestServer::TYPE_HTTP, | 997 TestServer test_server(TestServer::TYPE_HTTP, |
| 1048 TestServer::kLocalhost, | 998 TestServer::kLocalhost, |
| 1049 base::FilePath(kDocRoot)); | 999 base::FilePath(kDocRoot)); |
| 1050 ASSERT_TRUE(test_server.Start()); | 1000 ASSERT_TRUE(test_server.Start()); |
| 1051 | 1001 |
| 1052 CreateFetcher(test_server.GetURL("echo")); | 1002 CreateFetcher(test_server.GetURL("echo")); |
| 1053 MessageLoop::current()->Run(); | 1003 MessageLoop::current()->Run(); |
| 1054 } | 1004 } |
| 1055 | 1005 |
| 1056 TEST_F(URLFetcherPostFileTest, Basic) { | |
| 1057 TestServer test_server(TestServer::TYPE_HTTP, | |
| 1058 TestServer::kLocalhost, | |
| 1059 base::FilePath(kDocRoot)); | |
| 1060 ASSERT_TRUE(test_server.Start()); | |
| 1061 | |
| 1062 CreateFetcher(test_server.GetURL("echo")); | |
| 1063 MessageLoop::current()->Run(); | |
| 1064 } | |
| 1065 | |
| 1066 TEST_F(URLFetcherEmptyPostTest, Basic) { | 1006 TEST_F(URLFetcherEmptyPostTest, Basic) { |
| 1067 TestServer test_server(TestServer::TYPE_HTTP, | 1007 TestServer test_server(TestServer::TYPE_HTTP, |
| 1068 TestServer::kLocalhost, | 1008 TestServer::kLocalhost, |
| 1069 base::FilePath(kDocRoot)); | 1009 base::FilePath(kDocRoot)); |
| 1070 ASSERT_TRUE(test_server.Start()); | 1010 ASSERT_TRUE(test_server.Start()); |
| 1071 | 1011 |
| 1072 CreateFetcher(test_server.GetURL("echo")); | 1012 CreateFetcher(test_server.GetURL("echo")); |
| 1073 MessageLoop::current()->Run(); | 1013 MessageLoop::current()->Run(); |
| 1074 } | 1014 } |
| 1075 | 1015 |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1476 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1416 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1477 | 1417 |
| 1478 MessageLoop::current()->RunUntilIdle(); | 1418 MessageLoop::current()->RunUntilIdle(); |
| 1479 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1419 ASSERT_FALSE(file_util::PathExists(file_path_)) |
| 1480 << file_path_.value() << " not removed."; | 1420 << file_path_.value() << " not removed."; |
| 1481 } | 1421 } |
| 1482 | 1422 |
| 1483 } // namespace | 1423 } // namespace |
| 1484 | 1424 |
| 1485 } // namespace net | 1425 } // namespace net |
| OLD | NEW |