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 <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" | |
|
mmenke
2013/02/28 16:53:23
Neither of these is needed.
mattm
2013/02/28 22:13:05
Done.
| |
| 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 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 // Version of URLFetcherTest that does a POST instead | 232 // Version of URLFetcherTest that does a POST instead |
| 231 class URLFetcherPostTest : public URLFetcherTest { | 233 class URLFetcherPostTest : public URLFetcherTest { |
| 232 public: | 234 public: |
| 233 // URLFetcherTest: | 235 // URLFetcherTest: |
| 234 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 236 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 235 | 237 |
| 236 // URLFetcherDelegate: | 238 // URLFetcherDelegate: |
| 237 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 239 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| 238 }; | 240 }; |
| 239 | 241 |
| 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 | |
| 240 // Version of URLFetcherTest that does a POST instead with empty upload body | 258 // Version of URLFetcherTest that does a POST instead with empty upload body |
| 241 class URLFetcherEmptyPostTest : public URLFetcherTest { | 259 class URLFetcherEmptyPostTest : public URLFetcherTest { |
| 242 public: | 260 public: |
| 243 // URLFetcherTest: | 261 // URLFetcherTest: |
| 244 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 262 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 245 | 263 |
| 246 // URLFetcherDelegate: | 264 // URLFetcherDelegate: |
| 247 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 265 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| 248 }; | 266 }; |
| 249 | 267 |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 497 fetcher_->Start(); | 515 fetcher_->Start(); |
| 498 } | 516 } |
| 499 | 517 |
| 500 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { | 518 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { |
| 501 std::string data; | 519 std::string data; |
| 502 EXPECT_TRUE(source->GetResponseAsString(&data)); | 520 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 503 EXPECT_EQ(std::string("bobsyeruncle"), data); | 521 EXPECT_EQ(std::string("bobsyeruncle"), data); |
| 504 URLFetcherTest::OnURLFetchComplete(source); | 522 URLFetcherTest::OnURLFetchComplete(source); |
| 505 } | 523 } |
| 506 | 524 |
| 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 fetcher_->SetUploadFilePath("application/x-www-form-urlencoded", | |
| 538 path_, | |
| 539 base::MessageLoopProxy::current()); | |
| 540 fetcher_->Start(); | |
| 541 } | |
| 542 | |
| 543 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) { | |
| 544 int64 size = 0; | |
| 545 ASSERT_EQ(true, file_util::GetFileSize(path_, &size)); | |
| 546 scoped_array<char> expected(new char[size]); | |
| 547 ASSERT_EQ(size, file_util::ReadFile(path_, expected.get(), size)); | |
| 548 | |
| 549 std::string data; | |
| 550 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
| 551 EXPECT_EQ(std::string(&expected[0], size), data); | |
| 552 URLFetcherTest::OnURLFetchComplete(source); | |
| 553 } | |
| 554 | |
| 507 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { | 555 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { |
| 508 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 556 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| 509 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 557 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 510 io_message_loop_proxy())); | 558 io_message_loop_proxy())); |
| 511 fetcher_->SetUploadData("text/plain", ""); | 559 fetcher_->SetUploadData("text/plain", ""); |
| 512 fetcher_->Start(); | 560 fetcher_->Start(); |
| 513 } | 561 } |
| 514 | 562 |
| 515 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { | 563 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { |
| 516 EXPECT_TRUE(source->GetStatus().is_success()); | 564 EXPECT_TRUE(source->GetStatus().is_success()); |
| (...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 996 TEST_F(URLFetcherPostTest, Basic) { | 1044 TEST_F(URLFetcherPostTest, Basic) { |
| 997 TestServer test_server(TestServer::TYPE_HTTP, | 1045 TestServer test_server(TestServer::TYPE_HTTP, |
| 998 TestServer::kLocalhost, | 1046 TestServer::kLocalhost, |
| 999 base::FilePath(kDocRoot)); | 1047 base::FilePath(kDocRoot)); |
| 1000 ASSERT_TRUE(test_server.Start()); | 1048 ASSERT_TRUE(test_server.Start()); |
| 1001 | 1049 |
| 1002 CreateFetcher(test_server.GetURL("echo")); | 1050 CreateFetcher(test_server.GetURL("echo")); |
| 1003 MessageLoop::current()->Run(); | 1051 MessageLoop::current()->Run(); |
| 1004 } | 1052 } |
| 1005 | 1053 |
| 1054 TEST_F(URLFetcherPostFileTest, Basic) { | |
| 1055 TestServer test_server(TestServer::TYPE_HTTP, | |
| 1056 TestServer::kLocalhost, | |
| 1057 base::FilePath(kDocRoot)); | |
| 1058 ASSERT_TRUE(test_server.Start()); | |
| 1059 | |
| 1060 CreateFetcher(test_server.GetURL("echo")); | |
| 1061 MessageLoop::current()->Run(); | |
| 1062 } | |
| 1063 | |
| 1006 TEST_F(URLFetcherEmptyPostTest, Basic) { | 1064 TEST_F(URLFetcherEmptyPostTest, Basic) { |
| 1007 TestServer test_server(TestServer::TYPE_HTTP, | 1065 TestServer test_server(TestServer::TYPE_HTTP, |
| 1008 TestServer::kLocalhost, | 1066 TestServer::kLocalhost, |
| 1009 base::FilePath(kDocRoot)); | 1067 base::FilePath(kDocRoot)); |
| 1010 ASSERT_TRUE(test_server.Start()); | 1068 ASSERT_TRUE(test_server.Start()); |
| 1011 | 1069 |
| 1012 CreateFetcher(test_server.GetURL("echo")); | 1070 CreateFetcher(test_server.GetURL("echo")); |
| 1013 MessageLoop::current()->Run(); | 1071 MessageLoop::current()->Run(); |
| 1014 } | 1072 } |
| 1015 | 1073 |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1416 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1474 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1417 | 1475 |
| 1418 MessageLoop::current()->RunUntilIdle(); | 1476 MessageLoop::current()->RunUntilIdle(); |
| 1419 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1477 ASSERT_FALSE(file_util::PathExists(file_path_)) |
| 1420 << file_path_.value() << " not removed."; | 1478 << file_path_.value() << " not removed."; |
| 1421 } | 1479 } |
| 1422 | 1480 |
| 1423 } // namespace | 1481 } // namespace |
| 1424 | 1482 |
| 1425 } // namespace net | 1483 } // namespace net |
| OLD | NEW |