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

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

Issue 14578004: Support range uploading of a file. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 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 | Annotate | Revision Log
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"
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 // URLFetcherTest: 246 // URLFetcherTest:
247 virtual void CreateFetcher(const GURL& url) OVERRIDE; 247 virtual void CreateFetcher(const GURL& url) OVERRIDE;
248 248
249 // URLFetcherDelegate: 249 // URLFetcherDelegate:
250 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; 250 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
251 251
252 private: 252 private:
253 base::FilePath path_; 253 base::FilePath path_;
254 }; 254 };
255 255
256 // Version of URLFetcherTest that does a POST of a file using
257 // SetUploadDataStream
258 class URLFetcherPostFileWithRangeTest : public URLFetcherTest {
259 public:
260 URLFetcherPostFileWithRangeTest();
261
262 // URLFetcherTest:
263 virtual void CreateFetcher(const GURL& url) OVERRIDE;
264
265 // URLFetcherDelegate:
266 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
267
268 private:
269 base::FilePath path_;
270 };
271
256 // Version of URLFetcherTest that does a POST instead with empty upload body 272 // Version of URLFetcherTest that does a POST instead with empty upload body
257 class URLFetcherEmptyPostTest : public URLFetcherTest { 273 class URLFetcherEmptyPostTest : public URLFetcherTest {
258 public: 274 public:
259 // URLFetcherTest: 275 // URLFetcherTest:
260 virtual void CreateFetcher(const GURL& url) OVERRIDE; 276 virtual void CreateFetcher(const GURL& url) OVERRIDE;
261 277
262 // URLFetcherDelegate: 278 // URLFetcherDelegate:
263 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; 279 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
264 }; 280 };
265 281
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 ASSERT_EQ(true, file_util::GetFileSize(path_, &size)); 563 ASSERT_EQ(true, file_util::GetFileSize(path_, &size));
548 scoped_ptr<char[]> expected(new char[size]); 564 scoped_ptr<char[]> expected(new char[size]);
549 ASSERT_EQ(size, file_util::ReadFile(path_, expected.get(), size)); 565 ASSERT_EQ(size, file_util::ReadFile(path_, expected.get(), size));
550 566
551 std::string data; 567 std::string data;
552 EXPECT_TRUE(source->GetResponseAsString(&data)); 568 EXPECT_TRUE(source->GetResponseAsString(&data));
553 EXPECT_EQ(std::string(&expected[0], size), data); 569 EXPECT_EQ(std::string(&expected[0], size), data);
554 URLFetcherTest::OnURLFetchComplete(source); 570 URLFetcherTest::OnURLFetchComplete(source);
555 } 571 }
556 572
573 URLFetcherPostFileWithRangeTest::URLFetcherPostFileWithRangeTest() {
574 PathService::Get(base::DIR_SOURCE_ROOT, &path_);
575 path_ = path_.Append(FILE_PATH_LITERAL("net"));
576 path_ = path_.Append(FILE_PATH_LITERAL("data"));
577 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest"));
578 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt"));
579
580 range_offset_ = 30;
581 range_length_ = 100;
582 }
583
584 void URLFetcherPostFileWithRangeTest::CreateFetcher(const GURL& url) {
585 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this);
586 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
587 io_message_loop_proxy(), request_context()));
588 fetcher_->SetUploadFilePathWithRange("application/x-www-form-urlencoded",
589 path_,
590 range_offset_,
591 range_length_,
592 base::MessageLoopProxy::current());
593 fetcher_->Start();
594 }
595
596 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) {
597 std::string expected;
598 ASSERT_TRUE(file_util::ReadFileToString(path_, &expected));
599 ASSERT_LE(range_offset_, expected.size());
600 ASSERT_LE(range_offset_ + range_length_, expected.size());
601 expected = expected.substr(range_offset_, range_length_);
602
603 std::string data;
604 EXPECT_TRUE(source->GetResponseAsString(&data));
605 EXPECT_EQ(expected, data);
606 URLFetcherTest::OnURLFetchComplete(source);
607 }
608
557 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { 609 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) {
558 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); 610 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this);
559 fetcher_->SetRequestContext(new TestURLRequestContextGetter( 611 fetcher_->SetRequestContext(new TestURLRequestContextGetter(
560 io_message_loop_proxy())); 612 io_message_loop_proxy()));
561 fetcher_->SetUploadData("text/plain", std::string()); 613 fetcher_->SetUploadData("text/plain", std::string());
562 fetcher_->Start(); 614 fetcher_->Start();
563 } 615 }
564 616
565 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { 617 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) {
566 EXPECT_TRUE(source->GetStatus().is_success()); 618 EXPECT_TRUE(source->GetStatus().is_success());
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after
1056 TEST_F(URLFetcherPostFileTest, Basic) { 1108 TEST_F(URLFetcherPostFileTest, Basic) {
1057 TestServer test_server(TestServer::TYPE_HTTP, 1109 TestServer test_server(TestServer::TYPE_HTTP,
1058 TestServer::kLocalhost, 1110 TestServer::kLocalhost,
1059 base::FilePath(kDocRoot)); 1111 base::FilePath(kDocRoot));
1060 ASSERT_TRUE(test_server.Start()); 1112 ASSERT_TRUE(test_server.Start());
1061 1113
1062 CreateFetcher(test_server.GetURL("echo")); 1114 CreateFetcher(test_server.GetURL("echo"));
1063 MessageLoop::current()->Run(); 1115 MessageLoop::current()->Run();
1064 } 1116 }
1065 1117
1118 TEST_F(URLFetcherPostFileWithRangeTest, Basic) {
1119 TestServer test_server(TestServer::TYPE_HTTP,
1120 TestServer::kLocalhost,
1121 base::FilePath(kDocRoot));
1122 ASSERT_TRUE(test_server.Start());
1123
1124 CreateFetcher(test_server.GetURL("echo"));
1125 MessageLoop::current()->Run();
1126 }
1127
1066 TEST_F(URLFetcherEmptyPostTest, Basic) { 1128 TEST_F(URLFetcherEmptyPostTest, Basic) {
1067 TestServer test_server(TestServer::TYPE_HTTP, 1129 TestServer test_server(TestServer::TYPE_HTTP,
1068 TestServer::kLocalhost, 1130 TestServer::kLocalhost,
1069 base::FilePath(kDocRoot)); 1131 base::FilePath(kDocRoot));
1070 ASSERT_TRUE(test_server.Start()); 1132 ASSERT_TRUE(test_server.Start());
1071 1133
1072 CreateFetcher(test_server.GetURL("echo")); 1134 CreateFetcher(test_server.GetURL("echo"));
1073 MessageLoop::current()->Run(); 1135 MessageLoop::current()->Run();
1074 } 1136 }
1075 1137
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after
1496 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). 1558 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1497 1559
1498 MessageLoop::current()->RunUntilIdle(); 1560 MessageLoop::current()->RunUntilIdle();
1499 ASSERT_FALSE(file_util::PathExists(file_path_)) 1561 ASSERT_FALSE(file_util::PathExists(file_path_))
1500 << file_path_.value() << " not removed."; 1562 << file_path_.value() << " not removed.";
1501 } 1563 }
1502 1564
1503 } // namespace 1565 } // namespace
1504 1566
1505 } // namespace net 1567 } // namespace net
OLDNEW
« net/url_request/url_fetcher.h ('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