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" |
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/rand_util.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" |
17 #include "crypto/nss_util.h" | 18 #include "crypto/nss_util.h" |
18 #include "net/base/network_change_notifier.h" | 19 #include "net/base/network_change_notifier.h" |
19 #include "net/dns/mock_host_resolver.h" | 20 #include "net/dns/mock_host_resolver.h" |
20 #include "net/http/http_response_headers.h" | 21 #include "net/http/http_response_headers.h" |
21 #include "net/test/test_server.h" | 22 #include "net/test/test_server.h" |
22 #include "net/url_request/url_fetcher_delegate.h" | 23 #include "net/url_request/url_fetcher_delegate.h" |
(...skipping 213 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 const base::FilePath& path() { return path_; } | |
mmenke
2013/05/01 18:10:22
nit: "... path() const { ...", if you want to kee
hidehiko
2013/05/01 18:30:04
Just removed.
| |
248 | |
249 void SetUploadRange(uint64 range_offset, uint64 range_length) { | |
250 range_offset_ = range_offset; | |
251 range_length_ = range_length; | |
252 } | |
253 | |
246 // URLFetcherTest: | 254 // URLFetcherTest: |
247 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 255 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
248 | 256 |
249 // URLFetcherDelegate: | 257 // URLFetcherDelegate: |
250 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 258 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
251 | 259 |
252 private: | 260 private: |
253 base::FilePath path_; | 261 base::FilePath path_; |
262 uint64 range_offset_; | |
263 uint64 range_length_; | |
254 }; | 264 }; |
255 | 265 |
256 // Version of URLFetcherTest that does a POST instead with empty upload body | 266 // Version of URLFetcherTest that does a POST instead with empty upload body |
257 class URLFetcherEmptyPostTest : public URLFetcherTest { | 267 class URLFetcherEmptyPostTest : public URLFetcherTest { |
258 public: | 268 public: |
259 // URLFetcherTest: | 269 // URLFetcherTest: |
260 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 270 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
261 | 271 |
262 // URLFetcherDelegate: | 272 // URLFetcherDelegate: |
263 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 273 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
517 fetcher_->Start(); | 527 fetcher_->Start(); |
518 } | 528 } |
519 | 529 |
520 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { | 530 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { |
521 std::string data; | 531 std::string data; |
522 EXPECT_TRUE(source->GetResponseAsString(&data)); | 532 EXPECT_TRUE(source->GetResponseAsString(&data)); |
523 EXPECT_EQ(std::string("bobsyeruncle"), data); | 533 EXPECT_EQ(std::string("bobsyeruncle"), data); |
524 URLFetcherTest::OnURLFetchComplete(source); | 534 URLFetcherTest::OnURLFetchComplete(source); |
525 } | 535 } |
526 | 536 |
527 URLFetcherPostFileTest::URLFetcherPostFileTest() { | 537 URLFetcherPostFileTest::URLFetcherPostFileTest() |
538 : range_offset_(0), | |
539 range_length_(kuint64max) { | |
528 PathService::Get(base::DIR_SOURCE_ROOT, &path_); | 540 PathService::Get(base::DIR_SOURCE_ROOT, &path_); |
529 path_ = path_.Append(FILE_PATH_LITERAL("net")); | 541 path_ = path_.Append(FILE_PATH_LITERAL("net")); |
530 path_ = path_.Append(FILE_PATH_LITERAL("data")); | 542 path_ = path_.Append(FILE_PATH_LITERAL("data")); |
531 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest")); | 543 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest")); |
532 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); | 544 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
533 } | 545 } |
534 | 546 |
535 void URLFetcherPostFileTest::CreateFetcher(const GURL& url) { | 547 void URLFetcherPostFileTest::CreateFetcher(const GURL& url) { |
536 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 548 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
537 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 549 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
538 io_message_loop_proxy(), request_context())); | 550 io_message_loop_proxy(), request_context())); |
539 fetcher_->SetUploadFilePath("application/x-www-form-urlencoded", | 551 fetcher_->SetUploadFilePath("application/x-www-form-urlencoded", |
540 path_, | 552 path_, |
553 range_offset_, | |
554 range_length_, | |
541 base::MessageLoopProxy::current()); | 555 base::MessageLoopProxy::current()); |
542 fetcher_->Start(); | 556 fetcher_->Start(); |
543 } | 557 } |
544 | 558 |
545 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) { | 559 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) { |
546 int64 size = 0; | 560 std::string expected; |
547 ASSERT_EQ(true, file_util::GetFileSize(path_, &size)); | 561 ASSERT_TRUE(file_util::ReadFileToString(path_, &expected)); |
548 scoped_ptr<char[]> expected(new char[size]); | 562 ASSERT_LE(range_offset_, expected.size()); |
549 ASSERT_EQ(size, file_util::ReadFile(path_, expected.get(), size)); | 563 size_t expected_size = std::min(static_cast<size_t>(range_length_), |
564 expected.size() - range_offset_); | |
550 | 565 |
551 std::string data; | 566 std::string data; |
552 EXPECT_TRUE(source->GetResponseAsString(&data)); | 567 EXPECT_TRUE(source->GetResponseAsString(&data)); |
553 EXPECT_EQ(std::string(&expected[0], size), data); | 568 EXPECT_EQ(expected.substr(range_offset_, expected_size), data); |
554 URLFetcherTest::OnURLFetchComplete(source); | 569 URLFetcherTest::OnURLFetchComplete(source); |
555 } | 570 } |
556 | 571 |
557 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { | 572 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { |
558 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 573 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
559 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 574 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
560 io_message_loop_proxy())); | 575 io_message_loop_proxy())); |
561 fetcher_->SetUploadData("text/plain", std::string()); | 576 fetcher_->SetUploadData("text/plain", std::string()); |
562 fetcher_->Start(); | 577 fetcher_->Start(); |
563 } | 578 } |
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1056 TEST_F(URLFetcherPostFileTest, Basic) { | 1071 TEST_F(URLFetcherPostFileTest, Basic) { |
1057 TestServer test_server(TestServer::TYPE_HTTP, | 1072 TestServer test_server(TestServer::TYPE_HTTP, |
1058 TestServer::kLocalhost, | 1073 TestServer::kLocalhost, |
1059 base::FilePath(kDocRoot)); | 1074 base::FilePath(kDocRoot)); |
1060 ASSERT_TRUE(test_server.Start()); | 1075 ASSERT_TRUE(test_server.Start()); |
1061 | 1076 |
1062 CreateFetcher(test_server.GetURL("echo")); | 1077 CreateFetcher(test_server.GetURL("echo")); |
1063 MessageLoop::current()->Run(); | 1078 MessageLoop::current()->Run(); |
1064 } | 1079 } |
1065 | 1080 |
1081 TEST_F(URLFetcherPostFileTest, Range) { | |
1082 TestServer test_server(TestServer::TYPE_HTTP, | |
1083 TestServer::kLocalhost, | |
1084 base::FilePath(kDocRoot)); | |
1085 ASSERT_TRUE(test_server.Start()); | |
1086 | |
1087 int64 size; | |
1088 ASSERT_TRUE(file_util::GetFileSize(path(), &size)); | |
1089 uint64 range_offset = base::RandGenerator(size); | |
1090 uint64 range_length = base::RandGenerator(size - range_offset); | |
mmenke
2013/05/01 18:10:22
We generally avoid deliberate randomization in tes
hidehiko
2013/05/01 18:30:04
I see. Sorry that I misunderstood that I should se
mmenke
2013/05/01 18:34:48
Oh, I'm the one who should have been clearer. Sor
| |
1091 SetUploadRange(range_offset, range_length); | |
1092 | |
1093 CreateFetcher(test_server.GetURL("echo")); | |
1094 MessageLoop::current()->Run(); | |
1095 } | |
1096 | |
1066 TEST_F(URLFetcherEmptyPostTest, Basic) { | 1097 TEST_F(URLFetcherEmptyPostTest, Basic) { |
1067 TestServer test_server(TestServer::TYPE_HTTP, | 1098 TestServer test_server(TestServer::TYPE_HTTP, |
1068 TestServer::kLocalhost, | 1099 TestServer::kLocalhost, |
1069 base::FilePath(kDocRoot)); | 1100 base::FilePath(kDocRoot)); |
1070 ASSERT_TRUE(test_server.Start()); | 1101 ASSERT_TRUE(test_server.Start()); |
1071 | 1102 |
1072 CreateFetcher(test_server.GetURL("echo")); | 1103 CreateFetcher(test_server.GetURL("echo")); |
1073 MessageLoop::current()->Run(); | 1104 MessageLoop::current()->Run(); |
1074 } | 1105 } |
1075 | 1106 |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1496 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1527 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1497 | 1528 |
1498 MessageLoop::current()->RunUntilIdle(); | 1529 MessageLoop::current()->RunUntilIdle(); |
1499 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1530 ASSERT_FALSE(file_util::PathExists(file_path_)) |
1500 << file_path_.value() << " not removed."; | 1531 << file_path_.value() << " not removed."; |
1501 } | 1532 } |
1502 | 1533 |
1503 } // namespace | 1534 } // namespace |
1504 | 1535 |
1505 } // namespace net | 1536 } // namespace net |
OLD | NEW |