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 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 uint64 range_offset_; | |
271 uint64 range_length_; | |
272 }; | |
273 | |
256 // Version of URLFetcherTest that does a POST instead with empty upload body | 274 // Version of URLFetcherTest that does a POST instead with empty upload body |
257 class URLFetcherEmptyPostTest : public URLFetcherTest { | 275 class URLFetcherEmptyPostTest : public URLFetcherTest { |
258 public: | 276 public: |
259 // URLFetcherTest: | 277 // URLFetcherTest: |
260 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 278 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
261 | 279 |
262 // URLFetcherDelegate: | 280 // URLFetcherDelegate: |
263 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 281 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
264 }; | 282 }; |
265 | 283 |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
531 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest")); | 549 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest")); |
532 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); | 550 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
533 } | 551 } |
534 | 552 |
535 void URLFetcherPostFileTest::CreateFetcher(const GURL& url) { | 553 void URLFetcherPostFileTest::CreateFetcher(const GURL& url) { |
536 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 554 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
537 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 555 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
538 io_message_loop_proxy(), request_context())); | 556 io_message_loop_proxy(), request_context())); |
539 fetcher_->SetUploadFilePath("application/x-www-form-urlencoded", | 557 fetcher_->SetUploadFilePath("application/x-www-form-urlencoded", |
540 path_, | 558 path_, |
559 0, | |
560 kuint64max, | |
541 base::MessageLoopProxy::current()); | 561 base::MessageLoopProxy::current()); |
542 fetcher_->Start(); | 562 fetcher_->Start(); |
543 } | 563 } |
544 | 564 |
545 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) { | 565 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) { |
546 int64 size = 0; | 566 int64 size = 0; |
547 ASSERT_EQ(true, file_util::GetFileSize(path_, &size)); | 567 ASSERT_EQ(true, file_util::GetFileSize(path_, &size)); |
548 scoped_ptr<char[]> expected(new char[size]); | 568 scoped_ptr<char[]> expected(new char[size]); |
549 ASSERT_EQ(size, file_util::ReadFile(path_, expected.get(), size)); | 569 ASSERT_EQ(size, file_util::ReadFile(path_, expected.get(), size)); |
550 | 570 |
551 std::string data; | 571 std::string data; |
552 EXPECT_TRUE(source->GetResponseAsString(&data)); | 572 EXPECT_TRUE(source->GetResponseAsString(&data)); |
553 EXPECT_EQ(std::string(&expected[0], size), data); | 573 EXPECT_EQ(std::string(&expected[0], size), data); |
554 URLFetcherTest::OnURLFetchComplete(source); | 574 URLFetcherTest::OnURLFetchComplete(source); |
555 } | 575 } |
556 | 576 |
577 URLFetcherPostFileWithRangeTest::URLFetcherPostFileWithRangeTest() { | |
578 PathService::Get(base::DIR_SOURCE_ROOT, &path_); | |
579 path_ = path_.Append(FILE_PATH_LITERAL("net")); | |
580 path_ = path_.Append(FILE_PATH_LITERAL("data")); | |
581 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest")); | |
582 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); | |
583 | |
584 range_offset_ = 30; | |
585 range_length_ = 100; | |
mmenke
2013/05/01 15:37:01
I think it's a little weird to make these variable
mmenke
2013/05/01 15:37:01
Also, these should be set in initializer lists.
hidehiko
2013/05/01 17:38:12
I see. So how about this? Merged two test fixtures
| |
586 } | |
587 | |
588 void URLFetcherPostFileWithRangeTest::CreateFetcher(const GURL& url) { | |
589 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | |
590 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
591 io_message_loop_proxy(), request_context())); | |
592 fetcher_->SetUploadFilePath("application/x-www-form-urlencoded", | |
593 path_, | |
594 range_offset_, | |
595 range_length_, | |
596 base::MessageLoopProxy::current()); | |
597 fetcher_->Start(); | |
598 } | |
599 | |
600 void URLFetcherPostFileWithRangeTest::OnURLFetchComplete( | |
601 const URLFetcher* source) { | |
602 std::string expected; | |
603 ASSERT_TRUE(file_util::ReadFileToString(path_, &expected)); | |
604 ASSERT_LE(range_offset_, expected.size()); | |
605 ASSERT_LE(range_offset_ + range_length_, expected.size()); | |
606 expected = expected.substr(range_offset_, range_length_); | |
607 | |
608 std::string data; | |
609 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
610 EXPECT_EQ(expected, data); | |
611 URLFetcherTest::OnURLFetchComplete(source); | |
612 } | |
613 | |
557 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { | 614 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { |
558 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 615 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
559 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 616 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
560 io_message_loop_proxy())); | 617 io_message_loop_proxy())); |
561 fetcher_->SetUploadData("text/plain", std::string()); | 618 fetcher_->SetUploadData("text/plain", std::string()); |
562 fetcher_->Start(); | 619 fetcher_->Start(); |
563 } | 620 } |
564 | 621 |
565 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { | 622 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { |
566 EXPECT_TRUE(source->GetStatus().is_success()); | 623 EXPECT_TRUE(source->GetStatus().is_success()); |
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1056 TEST_F(URLFetcherPostFileTest, Basic) { | 1113 TEST_F(URLFetcherPostFileTest, Basic) { |
1057 TestServer test_server(TestServer::TYPE_HTTP, | 1114 TestServer test_server(TestServer::TYPE_HTTP, |
1058 TestServer::kLocalhost, | 1115 TestServer::kLocalhost, |
1059 base::FilePath(kDocRoot)); | 1116 base::FilePath(kDocRoot)); |
1060 ASSERT_TRUE(test_server.Start()); | 1117 ASSERT_TRUE(test_server.Start()); |
1061 | 1118 |
1062 CreateFetcher(test_server.GetURL("echo")); | 1119 CreateFetcher(test_server.GetURL("echo")); |
1063 MessageLoop::current()->Run(); | 1120 MessageLoop::current()->Run(); |
1064 } | 1121 } |
1065 | 1122 |
1123 TEST_F(URLFetcherPostFileWithRangeTest, Basic) { | |
1124 TestServer test_server(TestServer::TYPE_HTTP, | |
1125 TestServer::kLocalhost, | |
1126 base::FilePath(kDocRoot)); | |
1127 ASSERT_TRUE(test_server.Start()); | |
1128 | |
1129 CreateFetcher(test_server.GetURL("echo")); | |
1130 MessageLoop::current()->Run(); | |
1131 } | |
1132 | |
1066 TEST_F(URLFetcherEmptyPostTest, Basic) { | 1133 TEST_F(URLFetcherEmptyPostTest, Basic) { |
1067 TestServer test_server(TestServer::TYPE_HTTP, | 1134 TestServer test_server(TestServer::TYPE_HTTP, |
1068 TestServer::kLocalhost, | 1135 TestServer::kLocalhost, |
1069 base::FilePath(kDocRoot)); | 1136 base::FilePath(kDocRoot)); |
1070 ASSERT_TRUE(test_server.Start()); | 1137 ASSERT_TRUE(test_server.Start()); |
1071 | 1138 |
1072 CreateFetcher(test_server.GetURL("echo")); | 1139 CreateFetcher(test_server.GetURL("echo")); |
1073 MessageLoop::current()->Run(); | 1140 MessageLoop::current()->Run(); |
1074 } | 1141 } |
1075 | 1142 |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1496 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1563 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1497 | 1564 |
1498 MessageLoop::current()->RunUntilIdle(); | 1565 MessageLoop::current()->RunUntilIdle(); |
1499 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1566 ASSERT_FALSE(file_util::PathExists(file_path_)) |
1500 << file_path_.value() << " not removed."; | 1567 << file_path_.value() << " not removed."; |
1501 } | 1568 } |
1502 | 1569 |
1503 } // namespace | 1570 } // namespace |
1504 | 1571 |
1505 } // namespace net | 1572 } // namespace net |
OLD | NEW |