| 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 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 // Version of URLFetcherTest that does a POST instead | 231 // Version of URLFetcherTest that does a POST instead |
| 232 class URLFetcherPostTest : public URLFetcherTest { | 232 class URLFetcherPostTest : public URLFetcherTest { |
| 233 public: | 233 public: |
| 234 // URLFetcherTest: | 234 // URLFetcherTest: |
| 235 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 235 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 236 | 236 |
| 237 // URLFetcherDelegate: | 237 // URLFetcherDelegate: |
| 238 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 238 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| 239 }; | 239 }; |
| 240 | 240 |
| 241 // Version of URLFetcherTest that does a POST instead |
| 242 class URLFetcherPostRetryTest : public URLFetcherMockDnsTest { |
| 243 public: |
| 244 // URLFetcherMockDnsTest: |
| 245 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 246 |
| 247 // URLFetcherDelegate: |
| 248 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| 249 }; |
| 250 |
| 241 // Version of URLFetcherTest that does a POST of a file using | 251 // Version of URLFetcherTest that does a POST of a file using |
| 242 // SetUploadDataStream | 252 // SetUploadDataStream |
| 243 class URLFetcherPostFileTest : public URLFetcherTest { | 253 class URLFetcherPostFileTest : public URLFetcherTest { |
| 244 public: | 254 public: |
| 245 URLFetcherPostFileTest(); | 255 URLFetcherPostFileTest(); |
| 246 | 256 |
| 247 // URLFetcherTest: | 257 // URLFetcherTest: |
| 248 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 258 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 249 | 259 |
| 250 // URLFetcherDelegate: | 260 // URLFetcherDelegate: |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 fetcher_->Start(); | 524 fetcher_->Start(); |
| 515 } | 525 } |
| 516 | 526 |
| 517 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { | 527 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { |
| 518 std::string data; | 528 std::string data; |
| 519 EXPECT_TRUE(source->GetResponseAsString(&data)); | 529 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 520 EXPECT_EQ(std::string("bobsyeruncle"), data); | 530 EXPECT_EQ(std::string("bobsyeruncle"), data); |
| 521 URLFetcherTest::OnURLFetchComplete(source); | 531 URLFetcherTest::OnURLFetchComplete(source); |
| 522 } | 532 } |
| 523 | 533 |
| 534 void URLFetcherPostRetryTest::CreateFetcher(const GURL& url) { |
| 535 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| 536 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
| 537 io_message_loop_proxy(), request_context())); |
| 538 fetcher_->SetUploadData("application/x-www-form-urlencoded", |
| 539 "bobsyeruncle"); |
| 540 } |
| 541 |
| 542 void URLFetcherPostRetryTest::OnURLFetchComplete(const URLFetcher* source) { |
| 543 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 544 std::string data; |
| 545 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 546 EXPECT_EQ(std::string("bobsyeruncle"), data); |
| 547 completed_fetcher_.reset(fetcher_); |
| 548 } |
| 549 |
| 524 URLFetcherPostFileTest::URLFetcherPostFileTest() { | 550 URLFetcherPostFileTest::URLFetcherPostFileTest() { |
| 525 PathService::Get(base::DIR_SOURCE_ROOT, &path_); | 551 PathService::Get(base::DIR_SOURCE_ROOT, &path_); |
| 526 path_ = path_.Append(FILE_PATH_LITERAL("net")); | 552 path_ = path_.Append(FILE_PATH_LITERAL("net")); |
| 527 path_ = path_.Append(FILE_PATH_LITERAL("data")); | 553 path_ = path_.Append(FILE_PATH_LITERAL("data")); |
| 528 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest")); | 554 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 529 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); | 555 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 530 } | 556 } |
| 531 | 557 |
| 532 void URLFetcherPostFileTest::CreateFetcher(const GURL& url) { | 558 void URLFetcherPostFileTest::CreateFetcher(const GURL& url) { |
| 533 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 559 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| (...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1045 TEST_F(URLFetcherPostTest, Basic) { | 1071 TEST_F(URLFetcherPostTest, Basic) { |
| 1046 TestServer test_server(TestServer::TYPE_HTTP, | 1072 TestServer test_server(TestServer::TYPE_HTTP, |
| 1047 TestServer::kLocalhost, | 1073 TestServer::kLocalhost, |
| 1048 FilePath(kDocRoot)); | 1074 FilePath(kDocRoot)); |
| 1049 ASSERT_TRUE(test_server.Start()); | 1075 ASSERT_TRUE(test_server.Start()); |
| 1050 | 1076 |
| 1051 CreateFetcher(test_server.GetURL("echo")); | 1077 CreateFetcher(test_server.GetURL("echo")); |
| 1052 MessageLoop::current()->Run(); | 1078 MessageLoop::current()->Run(); |
| 1053 } | 1079 } |
| 1054 | 1080 |
| 1081 TEST_F(URLFetcherPostRetryTest, RetryOnNetworkChangedWithUploadData) { |
| 1082 EXPECT_EQ(0, GetNumFetcherCores()); |
| 1083 EXPECT_FALSE(resolver_.has_pending_requests()); |
| 1084 |
| 1085 GURL url = GURL( |
| 1086 base::StringPrintf("http://example.com:%d/echo", |
| 1087 test_server_->host_port_pair().port())); |
| 1088 |
| 1089 // This posts a task to start the fetcher. |
| 1090 CreateFetcher(url); |
| 1091 fetcher_->SetAutomaticallyRetryOnNetworkChanges(3); |
| 1092 fetcher_->Start(); |
| 1093 EXPECT_EQ(0, GetNumFetcherCores()); |
| 1094 MessageLoop::current()->RunUntilIdle(); |
| 1095 |
| 1096 // The fetcher is now running, but is pending the host resolve. |
| 1097 EXPECT_EQ(1, GetNumFetcherCores()); |
| 1098 EXPECT_TRUE(resolver_.has_pending_requests()); |
| 1099 ASSERT_FALSE(completed_fetcher_); |
| 1100 |
| 1101 // Make it fail 3 times. |
| 1102 for (int i = 0; i < 3; ++i) { |
| 1103 // A network change notification aborts the connect job. |
| 1104 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 1105 MessageLoop::current()->RunUntilIdle(); |
| 1106 |
| 1107 // But the fetcher retries automatically. |
| 1108 EXPECT_EQ(1, GetNumFetcherCores()); |
| 1109 EXPECT_TRUE(resolver_.has_pending_requests()); |
| 1110 ASSERT_FALSE(completed_fetcher_); |
| 1111 } |
| 1112 |
| 1113 // Now let it succeed by resolving the pending request. |
| 1114 resolver_.ResolveAllPending(); |
| 1115 MessageLoop::current()->Run(); |
| 1116 |
| 1117 // URLFetcherMockDnsTest::OnURLFetchComplete() will quit the loop. |
| 1118 EXPECT_EQ(0, GetNumFetcherCores()); |
| 1119 EXPECT_FALSE(resolver_.has_pending_requests()); |
| 1120 ASSERT_TRUE(completed_fetcher_); |
| 1121 |
| 1122 // This time the request succeeded. |
| 1123 EXPECT_EQ(OK, completed_fetcher_->GetStatus().error()); |
| 1124 EXPECT_EQ(200, completed_fetcher_->GetResponseCode()); |
| 1125 } |
| 1126 |
| 1055 TEST_F(URLFetcherPostFileTest, Basic) { | 1127 TEST_F(URLFetcherPostFileTest, Basic) { |
| 1056 TestServer test_server(TestServer::TYPE_HTTP, | 1128 TestServer test_server(TestServer::TYPE_HTTP, |
| 1057 TestServer::kLocalhost, | 1129 TestServer::kLocalhost, |
| 1058 FilePath(kDocRoot)); | 1130 FilePath(kDocRoot)); |
| 1059 ASSERT_TRUE(test_server.Start()); | 1131 ASSERT_TRUE(test_server.Start()); |
| 1060 | 1132 |
| 1061 CreateFetcher(test_server.GetURL("echo")); | 1133 CreateFetcher(test_server.GetURL("echo")); |
| 1062 MessageLoop::current()->Run(); | 1134 MessageLoop::current()->Run(); |
| 1063 } | 1135 } |
| 1064 | 1136 |
| (...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1475 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1547 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1476 | 1548 |
| 1477 MessageLoop::current()->RunUntilIdle(); | 1549 MessageLoop::current()->RunUntilIdle(); |
| 1478 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1550 ASSERT_FALSE(file_util::PathExists(file_path_)) |
| 1479 << file_path_.value() << " not removed."; | 1551 << file_path_.value() << " not removed."; |
| 1480 } | 1552 } |
| 1481 | 1553 |
| 1482 } // namespace | 1554 } // namespace |
| 1483 | 1555 |
| 1484 } // namespace net | 1556 } // namespace net |
| OLD | NEW |