| 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 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 | 240 // Version of URLFetcherTest that does a POST of a file using |
| 241 // SetUploadDataStream | 241 // SetUploadDataStream |
| 242 class URLFetcherPostFileTest : public URLFetcherTest { | 242 class URLFetcherPostFileTest : public URLFetcherTest { |
| 243 public: | 243 public: |
| 244 URLFetcherPostFileTest(); | 244 URLFetcherPostFileTest(); |
| 245 | 245 |
| 246 void SetUploadRange(uint64 range_offset, uint64 range_length) { |
| 247 range_offset_ = range_offset; |
| 248 range_length_ = range_length; |
| 249 } |
| 250 |
| 246 // URLFetcherTest: | 251 // URLFetcherTest: |
| 247 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 252 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 248 | 253 |
| 249 // URLFetcherDelegate: | 254 // URLFetcherDelegate: |
| 250 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 255 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| 251 | 256 |
| 252 private: | 257 private: |
| 253 base::FilePath path_; | 258 base::FilePath path_; |
| 259 uint64 range_offset_; |
| 260 uint64 range_length_; |
| 254 }; | 261 }; |
| 255 | 262 |
| 256 // Version of URLFetcherTest that does a POST instead with empty upload body | 263 // Version of URLFetcherTest that does a POST instead with empty upload body |
| 257 class URLFetcherEmptyPostTest : public URLFetcherTest { | 264 class URLFetcherEmptyPostTest : public URLFetcherTest { |
| 258 public: | 265 public: |
| 259 // URLFetcherTest: | 266 // URLFetcherTest: |
| 260 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 267 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 261 | 268 |
| 262 // URLFetcherDelegate: | 269 // URLFetcherDelegate: |
| 263 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 270 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 fetcher_->Start(); | 524 fetcher_->Start(); |
| 518 } | 525 } |
| 519 | 526 |
| 520 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { | 527 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { |
| 521 std::string data; | 528 std::string data; |
| 522 EXPECT_TRUE(source->GetResponseAsString(&data)); | 529 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 523 EXPECT_EQ(std::string("bobsyeruncle"), data); | 530 EXPECT_EQ(std::string("bobsyeruncle"), data); |
| 524 URLFetcherTest::OnURLFetchComplete(source); | 531 URLFetcherTest::OnURLFetchComplete(source); |
| 525 } | 532 } |
| 526 | 533 |
| 527 URLFetcherPostFileTest::URLFetcherPostFileTest() { | 534 URLFetcherPostFileTest::URLFetcherPostFileTest() |
| 535 : range_offset_(0), |
| 536 range_length_(kuint64max) { |
| 528 PathService::Get(base::DIR_SOURCE_ROOT, &path_); | 537 PathService::Get(base::DIR_SOURCE_ROOT, &path_); |
| 529 path_ = path_.Append(FILE_PATH_LITERAL("net")); | 538 path_ = path_.Append(FILE_PATH_LITERAL("net")); |
| 530 path_ = path_.Append(FILE_PATH_LITERAL("data")); | 539 path_ = path_.Append(FILE_PATH_LITERAL("data")); |
| 531 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest")); | 540 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 532 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); | 541 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 533 } | 542 } |
| 534 | 543 |
| 535 void URLFetcherPostFileTest::CreateFetcher(const GURL& url) { | 544 void URLFetcherPostFileTest::CreateFetcher(const GURL& url) { |
| 536 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 545 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| 537 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 546 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
| 538 io_message_loop_proxy(), request_context())); | 547 io_message_loop_proxy(), request_context())); |
| 539 fetcher_->SetUploadFilePath("application/x-www-form-urlencoded", | 548 fetcher_->SetUploadFilePath("application/x-www-form-urlencoded", |
| 540 path_, | 549 path_, |
| 550 range_offset_, |
| 551 range_length_, |
| 541 base::MessageLoopProxy::current()); | 552 base::MessageLoopProxy::current()); |
| 542 fetcher_->Start(); | 553 fetcher_->Start(); |
| 543 } | 554 } |
| 544 | 555 |
| 545 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) { | 556 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) { |
| 546 int64 size = 0; | 557 std::string expected; |
| 547 ASSERT_EQ(true, file_util::GetFileSize(path_, &size)); | 558 ASSERT_TRUE(file_util::ReadFileToString(path_, &expected)); |
| 548 scoped_ptr<char[]> expected(new char[size]); | 559 ASSERT_LE(range_offset_, expected.size()); |
| 549 ASSERT_EQ(size, file_util::ReadFile(path_, expected.get(), size)); | 560 size_t expected_size = std::min(static_cast<size_t>(range_length_), |
| 561 expected.size() - range_offset_); |
| 550 | 562 |
| 551 std::string data; | 563 std::string data; |
| 552 EXPECT_TRUE(source->GetResponseAsString(&data)); | 564 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 553 EXPECT_EQ(std::string(&expected[0], size), data); | 565 EXPECT_EQ(expected.substr(range_offset_, expected_size), data); |
| 554 URLFetcherTest::OnURLFetchComplete(source); | 566 URLFetcherTest::OnURLFetchComplete(source); |
| 555 } | 567 } |
| 556 | 568 |
| 557 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { | 569 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { |
| 558 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 570 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| 559 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 571 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 560 io_message_loop_proxy())); | 572 io_message_loop_proxy())); |
| 561 fetcher_->SetUploadData("text/plain", std::string()); | 573 fetcher_->SetUploadData("text/plain", std::string()); |
| 562 fetcher_->Start(); | 574 fetcher_->Start(); |
| 563 } | 575 } |
| (...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1056 TEST_F(URLFetcherPostFileTest, Basic) { | 1068 TEST_F(URLFetcherPostFileTest, Basic) { |
| 1057 TestServer test_server(TestServer::TYPE_HTTP, | 1069 TestServer test_server(TestServer::TYPE_HTTP, |
| 1058 TestServer::kLocalhost, | 1070 TestServer::kLocalhost, |
| 1059 base::FilePath(kDocRoot)); | 1071 base::FilePath(kDocRoot)); |
| 1060 ASSERT_TRUE(test_server.Start()); | 1072 ASSERT_TRUE(test_server.Start()); |
| 1061 | 1073 |
| 1062 CreateFetcher(test_server.GetURL("echo")); | 1074 CreateFetcher(test_server.GetURL("echo")); |
| 1063 MessageLoop::current()->Run(); | 1075 MessageLoop::current()->Run(); |
| 1064 } | 1076 } |
| 1065 | 1077 |
| 1078 TEST_F(URLFetcherPostFileTest, Range) { |
| 1079 TestServer test_server(TestServer::TYPE_HTTP, |
| 1080 TestServer::kLocalhost, |
| 1081 base::FilePath(kDocRoot)); |
| 1082 ASSERT_TRUE(test_server.Start()); |
| 1083 |
| 1084 SetUploadRange(30, 100); |
| 1085 |
| 1086 CreateFetcher(test_server.GetURL("echo")); |
| 1087 MessageLoop::current()->Run(); |
| 1088 } |
| 1089 |
| 1066 TEST_F(URLFetcherEmptyPostTest, Basic) { | 1090 TEST_F(URLFetcherEmptyPostTest, Basic) { |
| 1067 TestServer test_server(TestServer::TYPE_HTTP, | 1091 TestServer test_server(TestServer::TYPE_HTTP, |
| 1068 TestServer::kLocalhost, | 1092 TestServer::kLocalhost, |
| 1069 base::FilePath(kDocRoot)); | 1093 base::FilePath(kDocRoot)); |
| 1070 ASSERT_TRUE(test_server.Start()); | 1094 ASSERT_TRUE(test_server.Start()); |
| 1071 | 1095 |
| 1072 CreateFetcher(test_server.GetURL("echo")); | 1096 CreateFetcher(test_server.GetURL("echo")); |
| 1073 MessageLoop::current()->Run(); | 1097 MessageLoop::current()->Run(); |
| 1074 } | 1098 } |
| 1075 | 1099 |
| (...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1496 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1520 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1497 | 1521 |
| 1498 MessageLoop::current()->RunUntilIdle(); | 1522 MessageLoop::current()->RunUntilIdle(); |
| 1499 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1523 ASSERT_FALSE(file_util::PathExists(file_path_)) |
| 1500 << file_path_.value() << " not removed."; | 1524 << file_path_.value() << " not removed."; |
| 1501 } | 1525 } |
| 1502 | 1526 |
| 1503 } // namespace | 1527 } // namespace |
| 1504 | 1528 |
| 1505 } // namespace net | 1529 } // namespace net |
| OLD | NEW |