Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Side by Side Diff: net/url_request/url_fetcher_impl_unittest.cc

Issue 12224066: Fix UrlFetcher upload retry handling. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Revert http://crrev.com/178535 Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 240 // Version of URLFetcherTest that does a POST and retries it.
243 // SetUploadDataStream 241 class URLFetcherPostRetryTest : public URLFetcherMockDnsTest {
244 class URLFetcherPostFileTest : public URLFetcherTest {
245 public: 242 public:
246 URLFetcherPostFileTest(); 243 // URLFetcherMockDnsTest:
247
248 // URLFetcherTest:
249 virtual void CreateFetcher(const GURL& url) OVERRIDE; 244 virtual void CreateFetcher(const GURL& url) OVERRIDE;
250 245
251 // URLFetcherDelegate: 246 // URLFetcherDelegate:
252 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; 247 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
253
254 private:
255 base::FilePath path_;
256 }; 248 };
257 249
258 // Version of URLFetcherTest that does a POST instead with empty upload body 250 // Version of URLFetcherTest that does a POST instead with empty upload body
259 class URLFetcherEmptyPostTest : public URLFetcherTest { 251 class URLFetcherEmptyPostTest : public URLFetcherTest {
260 public: 252 public:
261 // URLFetcherTest: 253 // URLFetcherTest:
262 virtual void CreateFetcher(const GURL& url) OVERRIDE; 254 virtual void CreateFetcher(const GURL& url) OVERRIDE;
263 255
264 // URLFetcherDelegate: 256 // URLFetcherDelegate:
265 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; 257 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 fetcher_->Start(); 507 fetcher_->Start();
516 } 508 }
517 509
518 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { 510 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) {
519 std::string data; 511 std::string data;
520 EXPECT_TRUE(source->GetResponseAsString(&data)); 512 EXPECT_TRUE(source->GetResponseAsString(&data));
521 EXPECT_EQ(std::string("bobsyeruncle"), data); 513 EXPECT_EQ(std::string("bobsyeruncle"), data);
522 URLFetcherTest::OnURLFetchComplete(source); 514 URLFetcherTest::OnURLFetchComplete(source);
523 } 515 }
524 516
525 URLFetcherPostFileTest::URLFetcherPostFileTest() { 517 void URLFetcherPostRetryTest::CreateFetcher(const GURL& url) {
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); 518 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this);
535 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( 519 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
536 io_message_loop_proxy(), request_context())); 520 io_message_loop_proxy(), request_context()));
537 scoped_ptr<UploadElementReader> reader(new UploadFileElementReader( 521 fetcher_->SetUploadData("application/x-www-form-urlencoded",
538 base::MessageLoopProxy::current(), path_, 0, kuint64max, base::Time())); 522 "bobsyeruncle");
539 fetcher_->SetUploadDataStream(
540 "application/x-www-form-urlencoded",
541 make_scoped_ptr(UploadDataStream::CreateWithReader(reader.Pass(), 0)));
542 fetcher_->Start();
543 } 523 }
544 524
545 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) { 525 void URLFetcherPostRetryTest::OnURLFetchComplete(const URLFetcher* source) {
546 int64 size = 0; 526 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
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; 527 std::string data;
552 EXPECT_TRUE(source->GetResponseAsString(&data)); 528 EXPECT_TRUE(source->GetResponseAsString(&data));
553 EXPECT_EQ(std::string(&expected[0], size), data); 529 EXPECT_EQ(std::string("bobsyeruncle"), data);
554 URLFetcherTest::OnURLFetchComplete(source); 530 completed_fetcher_.reset(fetcher_);
555 } 531 }
556 532
557 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { 533 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) {
558 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); 534 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this);
559 fetcher_->SetRequestContext(new TestURLRequestContextGetter( 535 fetcher_->SetRequestContext(new TestURLRequestContextGetter(
560 io_message_loop_proxy())); 536 io_message_loop_proxy()));
561 fetcher_->SetUploadData("text/plain", ""); 537 fetcher_->SetUploadData("text/plain", "");
562 fetcher_->Start(); 538 fetcher_->Start();
563 } 539 }
564 540
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
1039 ASSERT_TRUE(completed_fetcher_); 1015 ASSERT_TRUE(completed_fetcher_);
1040 1016
1041 // This time the request succeeded. 1017 // This time the request succeeded.
1042 EXPECT_EQ(OK, completed_fetcher_->GetStatus().error()); 1018 EXPECT_EQ(OK, completed_fetcher_->GetStatus().error());
1043 EXPECT_EQ(200, completed_fetcher_->GetResponseCode()); 1019 EXPECT_EQ(200, completed_fetcher_->GetResponseCode());
1044 } 1020 }
1045 1021
1046 TEST_F(URLFetcherPostTest, Basic) { 1022 TEST_F(URLFetcherPostTest, Basic) {
1047 TestServer test_server(TestServer::TYPE_HTTP, 1023 TestServer test_server(TestServer::TYPE_HTTP,
1048 TestServer::kLocalhost, 1024 TestServer::kLocalhost,
1049 base::FilePath(kDocRoot)); 1025 FilePath(kDocRoot));
1050 ASSERT_TRUE(test_server.Start()); 1026 ASSERT_TRUE(test_server.Start());
1051 1027
1052 CreateFetcher(test_server.GetURL("echo")); 1028 CreateFetcher(test_server.GetURL("echo"));
1053 MessageLoop::current()->Run(); 1029 MessageLoop::current()->Run();
1054 } 1030 }
1055 1031
1056 TEST_F(URLFetcherPostFileTest, Basic) { 1032 TEST_F(URLFetcherPostRetryTest, RetryOnNetworkChangedWithUploadData) {
1057 TestServer test_server(TestServer::TYPE_HTTP, 1033 EXPECT_EQ(0, GetNumFetcherCores());
1058 TestServer::kLocalhost, 1034 EXPECT_FALSE(resolver_.has_pending_requests());
1059 base::FilePath(kDocRoot));
1060 ASSERT_TRUE(test_server.Start());
1061 1035
1062 CreateFetcher(test_server.GetURL("echo")); 1036 GURL url = GURL(
1037 base::StringPrintf("http://example.com:%d/echo",
1038 test_server_->host_port_pair().port()));
1039
1040 // This posts a task to start the fetcher.
1041 CreateFetcher(url);
1042 fetcher_->SetAutomaticallyRetryOnNetworkChanges(3);
1043 fetcher_->Start();
1044 EXPECT_EQ(0, GetNumFetcherCores());
1045 MessageLoop::current()->RunUntilIdle();
1046
1047 // The fetcher is now running, but is pending the host resolve.
1048 EXPECT_EQ(1, GetNumFetcherCores());
1049 EXPECT_TRUE(resolver_.has_pending_requests());
1050 ASSERT_FALSE(completed_fetcher_);
1051
1052 // Make it fail 3 times.
1053 for (int i = 0; i < 3; ++i) {
1054 // A network change notification aborts the connect job.
1055 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
1056 MessageLoop::current()->RunUntilIdle();
1057
1058 // But the fetcher retries automatically.
1059 EXPECT_EQ(1, GetNumFetcherCores());
1060 EXPECT_TRUE(resolver_.has_pending_requests());
1061 ASSERT_FALSE(completed_fetcher_);
1062 }
1063
1064 // Now let it succeed by resolving the pending request.
1065 resolver_.ResolveAllPending();
1063 MessageLoop::current()->Run(); 1066 MessageLoop::current()->Run();
1067
1068 // URLFetcherMockDnsTest::OnURLFetchComplete() will quit the loop.
1069 EXPECT_EQ(0, GetNumFetcherCores());
1070 EXPECT_FALSE(resolver_.has_pending_requests());
1071 ASSERT_TRUE(completed_fetcher_);
1072
1073 // This time the request succeeded.
1074 EXPECT_EQ(OK, completed_fetcher_->GetStatus().error());
1075 EXPECT_EQ(200, completed_fetcher_->GetResponseCode());
1064 } 1076 }
1065 1077
1066 TEST_F(URLFetcherEmptyPostTest, Basic) { 1078 TEST_F(URLFetcherEmptyPostTest, Basic) {
1067 TestServer test_server(TestServer::TYPE_HTTP, 1079 TestServer test_server(TestServer::TYPE_HTTP,
1068 TestServer::kLocalhost, 1080 TestServer::kLocalhost,
1069 base::FilePath(kDocRoot)); 1081 base::FilePath(kDocRoot));
1070 ASSERT_TRUE(test_server.Start()); 1082 ASSERT_TRUE(test_server.Start());
1071 1083
1072 CreateFetcher(test_server.GetURL("echo")); 1084 CreateFetcher(test_server.GetURL("echo"));
1073 MessageLoop::current()->Run(); 1085 MessageLoop::current()->Run();
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
1476 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). 1488 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1477 1489
1478 MessageLoop::current()->RunUntilIdle(); 1490 MessageLoop::current()->RunUntilIdle();
1479 ASSERT_FALSE(file_util::PathExists(file_path_)) 1491 ASSERT_FALSE(file_util::PathExists(file_path_))
1480 << file_path_.value() << " not removed."; 1492 << file_path_.value() << " not removed.";
1481 } 1493 }
1482 1494
1483 } // namespace 1495 } // namespace
1484 1496
1485 } // namespace net 1497 } // namespace net
OLDNEW
« net/url_request/url_fetcher_core.cc ('K') | « net/url_request/url_fetcher_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698