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 <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/message_loop/message_loop_proxy.h" | 13 #include "base/message_loop/message_loop_proxy.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
17 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
19 #include "crypto/nss_util.h" | 19 #include "crypto/nss_util.h" |
| 20 #include "net/base/elements_upload_data_stream.h" |
20 #include "net/base/network_change_notifier.h" | 21 #include "net/base/network_change_notifier.h" |
| 22 #include "net/base/upload_bytes_element_reader.h" |
| 23 #include "net/base/upload_element_reader.h" |
| 24 #include "net/base/upload_file_element_reader.h" |
21 #include "net/dns/mock_host_resolver.h" | 25 #include "net/dns/mock_host_resolver.h" |
22 #include "net/http/http_response_headers.h" | 26 #include "net/http/http_response_headers.h" |
23 #include "net/test/spawned_test_server/spawned_test_server.h" | 27 #include "net/test/spawned_test_server/spawned_test_server.h" |
24 #include "net/url_request/url_fetcher_delegate.h" | 28 #include "net/url_request/url_fetcher_delegate.h" |
25 #include "net/url_request/url_request_context_getter.h" | 29 #include "net/url_request/url_request_context_getter.h" |
26 #include "net/url_request/url_request_test_util.h" | 30 #include "net/url_request/url_request_test_util.h" |
27 #include "net/url_request/url_request_throttler_manager.h" | 31 #include "net/url_request/url_request_throttler_manager.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
29 | 33 |
30 #if defined(USE_NSS) || defined(OS_IOS) | 34 #if defined(USE_NSS) || defined(OS_IOS) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 ~ThrottlingTestURLRequestContextGetter() override {} | 78 ~ThrottlingTestURLRequestContextGetter() override {} |
75 | 79 |
76 TestURLRequestContext* const context_; | 80 TestURLRequestContext* const context_; |
77 }; | 81 }; |
78 | 82 |
79 } // namespace | 83 } // namespace |
80 | 84 |
81 class URLFetcherTest : public testing::Test, | 85 class URLFetcherTest : public testing::Test, |
82 public URLFetcherDelegate { | 86 public URLFetcherDelegate { |
83 public: | 87 public: |
84 URLFetcherTest() : fetcher_(NULL) {} | 88 URLFetcherTest() : fetcher_(NULL), expected_status_code_(200) {} |
85 | 89 |
86 static int GetNumFetcherCores() { | 90 static int GetNumFetcherCores() { |
87 return URLFetcherImpl::GetNumFetcherCores(); | 91 return URLFetcherImpl::GetNumFetcherCores(); |
88 } | 92 } |
89 | 93 |
90 // Creates a URLFetcher, using the program's main thread to do IO. | 94 // Creates a URLFetcher, using the program's main thread to do IO. |
91 virtual void CreateFetcher(const GURL& url); | 95 virtual void CreateFetcher(const GURL& url); |
92 | 96 |
93 // URLFetcherDelegate: | 97 // URLFetcherDelegate: |
94 // Subclasses that override this should either call this function or | 98 // Subclasses that override this should either call this function or |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 } | 132 } |
129 | 133 |
130 // URLFetcher is designed to run on the main UI thread, but in our tests | 134 // URLFetcher is designed to run on the main UI thread, but in our tests |
131 // we assume that the current thread is the IO thread where the URLFetcher | 135 // we assume that the current thread is the IO thread where the URLFetcher |
132 // dispatches its requests to. When we wish to simulate being used from | 136 // dispatches its requests to. When we wish to simulate being used from |
133 // a UI thread, we dispatch a worker thread to do so. | 137 // a UI thread, we dispatch a worker thread to do so. |
134 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 138 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
135 | 139 |
136 URLFetcherImpl* fetcher_; | 140 URLFetcherImpl* fetcher_; |
137 scoped_ptr<TestURLRequestContext> context_; | 141 scoped_ptr<TestURLRequestContext> context_; |
| 142 int expected_status_code_; |
138 }; | 143 }; |
139 | 144 |
140 // A test fixture that uses a MockHostResolver, so that name resolutions can | 145 // A test fixture that uses a MockHostResolver, so that name resolutions can |
141 // be manipulated by the tests to keep connections in the resolving state. | 146 // be manipulated by the tests to keep connections in the resolving state. |
142 class URLFetcherMockDnsTest : public URLFetcherTest { | 147 class URLFetcherMockDnsTest : public URLFetcherTest { |
143 public: | 148 public: |
144 // testing::Test: | 149 // testing::Test: |
145 void SetUp() override; | 150 void SetUp() override; |
146 | 151 |
147 // URLFetcherTest: | 152 // URLFetcherTest: |
(...skipping 11 matching lines...) Expand all Loading... |
159 | 164 |
160 void URLFetcherTest::CreateFetcher(const GURL& url) { | 165 void URLFetcherTest::CreateFetcher(const GURL& url) { |
161 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 166 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
162 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 167 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
163 io_message_loop_proxy().get(), request_context())); | 168 io_message_loop_proxy().get(), request_context())); |
164 fetcher_->Start(); | 169 fetcher_->Start(); |
165 } | 170 } |
166 | 171 |
167 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source) { | 172 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source) { |
168 EXPECT_TRUE(source->GetStatus().is_success()); | 173 EXPECT_TRUE(source->GetStatus().is_success()); |
169 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK | 174 EXPECT_EQ(expected_status_code_, source->GetResponseCode()); // HTTP OK |
170 | 175 |
171 std::string data; | 176 std::string data; |
172 EXPECT_TRUE(source->GetResponseAsString(&data)); | 177 EXPECT_TRUE(source->GetResponseAsString(&data)); |
173 EXPECT_FALSE(data.empty()); | 178 EXPECT_FALSE(data.empty()); |
174 | 179 |
175 CleanupAfterFetchComplete(); | 180 CleanupAfterFetchComplete(); |
176 } | 181 } |
177 | 182 |
178 void URLFetcherTest::CleanupAfterFetchComplete() { | 183 void URLFetcherTest::CleanupAfterFetchComplete() { |
179 delete fetcher_; // Have to delete this here and not in the destructor, | 184 delete fetcher_; // Have to delete this here and not in the destructor, |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 | 257 |
253 // URLFetcherDelegate: | 258 // URLFetcherDelegate: |
254 void OnURLFetchComplete(const URLFetcher* source) override; | 259 void OnURLFetchComplete(const URLFetcher* source) override; |
255 | 260 |
256 private: | 261 private: |
257 base::FilePath path_; | 262 base::FilePath path_; |
258 uint64 range_offset_; | 263 uint64 range_offset_; |
259 uint64 range_length_; | 264 uint64 range_length_; |
260 }; | 265 }; |
261 | 266 |
| 267 class URLFetcherSetUploadFactoryTest : public URLFetcherTest { |
| 268 public: |
| 269 URLFetcherSetUploadFactoryTest() : create_stream_count_(0) {} |
| 270 |
| 271 // URLFetcherTest: |
| 272 void CreateFetcher(const GURL& url) override; |
| 273 |
| 274 // URLFetcherDelegate: |
| 275 void OnURLFetchComplete(const URLFetcher* source) override; |
| 276 |
| 277 // Callback passed to URLFetcher to create upload stream. |
| 278 scoped_ptr<UploadDataStream> CreateUploadStream() { |
| 279 ++create_stream_count_; |
| 280 const std::string str("bobsyeruncle\n"); |
| 281 std::vector<char> buffer(str.begin(), str.end()); |
| 282 return ElementsUploadDataStream::CreateWithReader( |
| 283 scoped_ptr<UploadElementReader>( |
| 284 new UploadOwnedBytesElementReader(&buffer)), |
| 285 0); |
| 286 } |
| 287 |
| 288 size_t create_stream_count() const { return create_stream_count_; } |
| 289 |
| 290 private: |
| 291 // Count of calling CreateStream. |
| 292 size_t create_stream_count_; |
| 293 }; |
| 294 |
262 // Version of URLFetcherTest that does a POST instead with empty upload body | 295 // Version of URLFetcherTest that does a POST instead with empty upload body |
263 class URLFetcherEmptyPostTest : public URLFetcherTest { | 296 class URLFetcherEmptyPostTest : public URLFetcherTest { |
264 public: | 297 public: |
265 // URLFetcherTest: | 298 // URLFetcherTest: |
266 void CreateFetcher(const GURL& url) override; | 299 void CreateFetcher(const GURL& url) override; |
267 | 300 |
268 // URLFetcherDelegate: | 301 // URLFetcherDelegate: |
269 void OnURLFetchComplete(const URLFetcher* source) override; | 302 void OnURLFetchComplete(const URLFetcher* source) override; |
270 }; | 303 }; |
271 | 304 |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 ASSERT_LE(range_offset_, expected.size()); | 597 ASSERT_LE(range_offset_, expected.size()); |
565 uint64 expected_size = | 598 uint64 expected_size = |
566 std::min(range_length_, expected.size() - range_offset_); | 599 std::min(range_length_, expected.size() - range_offset_); |
567 | 600 |
568 std::string data; | 601 std::string data; |
569 EXPECT_TRUE(source->GetResponseAsString(&data)); | 602 EXPECT_TRUE(source->GetResponseAsString(&data)); |
570 EXPECT_EQ(expected.substr(range_offset_, expected_size), data); | 603 EXPECT_EQ(expected.substr(range_offset_, expected_size), data); |
571 URLFetcherTest::OnURLFetchComplete(source); | 604 URLFetcherTest::OnURLFetchComplete(source); |
572 } | 605 } |
573 | 606 |
| 607 void URLFetcherSetUploadFactoryTest::CreateFetcher(const GURL& url) { |
| 608 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| 609 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
| 610 io_message_loop_proxy().get(), request_context())); |
| 611 fetcher_->SetUploadStreamFactory( |
| 612 "text/plain", |
| 613 base::Bind(&URLFetcherSetUploadFactoryTest::CreateUploadStream, |
| 614 base::Unretained(this))); |
| 615 fetcher_->SetAutomaticallyRetryOn5xx(true); |
| 616 fetcher_->SetMaxRetriesOn5xx(1); |
| 617 fetcher_->Start(); |
| 618 } |
| 619 |
| 620 void URLFetcherSetUploadFactoryTest::OnURLFetchComplete( |
| 621 const URLFetcher* source) { |
| 622 std::string data; |
| 623 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 624 EXPECT_EQ("bobsyeruncle\n", data); |
| 625 URLFetcherTest::OnURLFetchComplete(source); |
| 626 } |
| 627 |
574 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { | 628 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { |
575 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 629 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
576 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 630 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
577 io_message_loop_proxy())); | 631 io_message_loop_proxy())); |
578 fetcher_->SetUploadData("text/plain", std::string()); | 632 fetcher_->SetUploadData("text/plain", std::string()); |
579 fetcher_->Start(); | 633 fetcher_->Start(); |
580 } | 634 } |
581 | 635 |
582 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { | 636 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { |
583 EXPECT_TRUE(source->GetStatus().is_success()); | 637 EXPECT_TRUE(source->GetStatus().is_success()); |
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1078 SpawnedTestServer::kLocalhost, | 1132 SpawnedTestServer::kLocalhost, |
1079 base::FilePath(kDocRoot)); | 1133 base::FilePath(kDocRoot)); |
1080 ASSERT_TRUE(test_server.Start()); | 1134 ASSERT_TRUE(test_server.Start()); |
1081 | 1135 |
1082 SetUploadRange(30, 100); | 1136 SetUploadRange(30, 100); |
1083 | 1137 |
1084 CreateFetcher(test_server.GetURL("echo")); | 1138 CreateFetcher(test_server.GetURL("echo")); |
1085 base::MessageLoop::current()->Run(); | 1139 base::MessageLoop::current()->Run(); |
1086 } | 1140 } |
1087 | 1141 |
| 1142 TEST_F(URLFetcherSetUploadFactoryTest, Basic) { |
| 1143 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
| 1144 SpawnedTestServer::kLocalhost, |
| 1145 base::FilePath(kDocRoot)); |
| 1146 ASSERT_TRUE(test_server.Start()); |
| 1147 |
| 1148 CreateFetcher(test_server.GetURL("echo")); |
| 1149 base::MessageLoop::current()->Run(); |
| 1150 ASSERT_EQ(1u, create_stream_count()); |
| 1151 } |
| 1152 |
| 1153 TEST_F(URLFetcherSetUploadFactoryTest, Retry) { |
| 1154 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
| 1155 SpawnedTestServer::kLocalhost, |
| 1156 base::FilePath(kDocRoot)); |
| 1157 ASSERT_TRUE(test_server.Start()); |
| 1158 expected_status_code_ = 500; |
| 1159 CreateFetcher(test_server.GetURL("echo?status=500")); |
| 1160 base::MessageLoop::current()->Run(); |
| 1161 ASSERT_EQ(2u, create_stream_count()); |
| 1162 } |
| 1163 |
1088 TEST_F(URLFetcherEmptyPostTest, Basic) { | 1164 TEST_F(URLFetcherEmptyPostTest, Basic) { |
1089 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | 1165 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
1090 SpawnedTestServer::kLocalhost, | 1166 SpawnedTestServer::kLocalhost, |
1091 base::FilePath(kDocRoot)); | 1167 base::FilePath(kDocRoot)); |
1092 ASSERT_TRUE(test_server.Start()); | 1168 ASSERT_TRUE(test_server.Start()); |
1093 | 1169 |
1094 CreateFetcher(test_server.GetURL("echo")); | 1170 CreateFetcher(test_server.GetURL("echo")); |
1095 base::MessageLoop::current()->Run(); | 1171 base::MessageLoop::current()->Run(); |
1096 } | 1172 } |
1097 | 1173 |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1534 | 1610 |
1535 base::MessageLoop::current()->RunUntilIdle(); | 1611 base::MessageLoop::current()->RunUntilIdle(); |
1536 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) << | 1612 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) << |
1537 "FilePath: " << file_path_.value(); | 1613 "FilePath: " << file_path_.value(); |
1538 } | 1614 } |
1539 } | 1615 } |
1540 | 1616 |
1541 } // namespace | 1617 } // namespace |
1542 | 1618 |
1543 } // namespace net | 1619 } // namespace net |
OLD | NEW |