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