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