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