OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/common/net/url_fetcher.h" | 5 #include "content/common/net/url_fetcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "content/public/common/url_fetcher_delegate.h" |
12 #include "crypto/nss_util.h" | 13 #include "crypto/nss_util.h" |
13 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
14 #include "net/test/test_server.h" | 15 #include "net/test/test_server.h" |
15 #include "net/url_request/url_request_context_getter.h" | 16 #include "net/url_request/url_request_context_getter.h" |
16 #include "net/url_request/url_request_test_util.h" | 17 #include "net/url_request/url_request_test_util.h" |
17 #include "net/url_request/url_request_throttler_manager.h" | 18 #include "net/url_request/url_request_throttler_manager.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 #if defined(USE_NSS) | 21 #if defined(USE_NSS) |
21 #include "net/ocsp/nss_ocsp.h" | 22 #include "net/ocsp/nss_ocsp.h" |
(...skipping 28 matching lines...) Expand all Loading... |
50 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 51 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
51 | 52 |
52 private: | 53 private: |
53 virtual ~TestURLRequestContextGetter() {} | 54 virtual ~TestURLRequestContextGetter() {} |
54 | 55 |
55 scoped_refptr<net::URLRequestContext> context_; | 56 scoped_refptr<net::URLRequestContext> context_; |
56 }; | 57 }; |
57 | 58 |
58 } // namespace | 59 } // namespace |
59 | 60 |
60 class URLFetcherTest : public testing::Test, public URLFetcher::Delegate { | 61 class URLFetcherTest : public testing::Test, |
| 62 public content::URLFetcherDelegate { |
61 public: | 63 public: |
62 URLFetcherTest() : fetcher_(NULL) { } | 64 URLFetcherTest() : fetcher_(NULL) { } |
63 | 65 |
64 static int GetNumFetcherCores() { | 66 static int GetNumFetcherCores() { |
65 return URLFetcher::GetNumFetcherCores(); | 67 return URLFetcher::GetNumFetcherCores(); |
66 } | 68 } |
67 | 69 |
68 // Creates a URLFetcher, using the program's main thread to do IO. | 70 // Creates a URLFetcher, using the program's main thread to do IO. |
69 virtual void CreateFetcher(const GURL& url); | 71 virtual void CreateFetcher(const GURL& url); |
70 | 72 |
71 // URLFetcher::Delegate | 73 // content::URLFetcherDelegate |
72 virtual void OnURLFetchComplete(const URLFetcher* source, | 74 virtual void OnURLFetchComplete(const URLFetcher* source); |
73 const GURL& url, | |
74 const net::URLRequestStatus& status, | |
75 int response_code, | |
76 const net::ResponseCookies& cookies, | |
77 const std::string& data); | |
78 | 75 |
79 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { | 76 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { |
80 return io_message_loop_proxy_; | 77 return io_message_loop_proxy_; |
81 } | 78 } |
82 | 79 |
83 protected: | 80 protected: |
84 virtual void SetUp() { | 81 virtual void SetUp() { |
85 testing::Test::SetUp(); | 82 testing::Test::SetUp(); |
86 | 83 |
87 io_message_loop_proxy_ = base::MessageLoopProxy::current(); | 84 io_message_loop_proxy_ = base::MessageLoopProxy::current(); |
(...skipping 20 matching lines...) Expand all Loading... |
108 URLFetcher* fetcher_; | 105 URLFetcher* fetcher_; |
109 }; | 106 }; |
110 | 107 |
111 void URLFetcherTest::CreateFetcher(const GURL& url) { | 108 void URLFetcherTest::CreateFetcher(const GURL& url) { |
112 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 109 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); |
113 fetcher_->set_request_context(new TestURLRequestContextGetter( | 110 fetcher_->set_request_context(new TestURLRequestContextGetter( |
114 io_message_loop_proxy())); | 111 io_message_loop_proxy())); |
115 fetcher_->Start(); | 112 fetcher_->Start(); |
116 } | 113 } |
117 | 114 |
118 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source, | 115 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source) { |
119 const GURL& url, | 116 EXPECT_TRUE(source->status().is_success()); |
120 const net::URLRequestStatus& status, | 117 EXPECT_EQ(200, source->response_code()); // HTTP OK |
121 int response_code, | 118 |
122 const net::ResponseCookies& cookies, | 119 std::string data; |
123 const std::string& data) { | 120 EXPECT_TRUE(source->GetResponseAsString(&data)); |
124 EXPECT_TRUE(status.is_success()); | |
125 EXPECT_EQ(200, response_code); // HTTP OK | |
126 EXPECT_FALSE(data.empty()); | 121 EXPECT_FALSE(data.empty()); |
127 | 122 |
128 delete fetcher_; // Have to delete this here and not in the destructor, | 123 delete fetcher_; // Have to delete this here and not in the destructor, |
129 // because the destructor won't necessarily run on the | 124 // because the destructor won't necessarily run on the |
130 // same thread that CreateFetcher() did. | 125 // same thread that CreateFetcher() did. |
131 | 126 |
132 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 127 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
133 // If the current message loop is not the IO loop, it will be shut down when | 128 // If the current message loop is not the IO loop, it will be shut down when |
134 // the main loop returns and this thread subsequently goes out of scope. | 129 // the main loop returns and this thread subsequently goes out of scope. |
135 } | 130 } |
136 | 131 |
137 namespace { | 132 namespace { |
138 | 133 |
139 // Version of URLFetcherTest that does a POST instead | 134 // Version of URLFetcherTest that does a POST instead |
140 class URLFetcherPostTest : public URLFetcherTest { | 135 class URLFetcherPostTest : public URLFetcherTest { |
141 public: | 136 public: |
142 virtual void CreateFetcher(const GURL& url); | 137 virtual void CreateFetcher(const GURL& url); |
143 | 138 |
144 // URLFetcher::Delegate | 139 // content::URLFetcherDelegate |
145 virtual void OnURLFetchComplete(const URLFetcher* source, | 140 virtual void OnURLFetchComplete(const URLFetcher* source); |
146 const GURL& url, | |
147 const net::URLRequestStatus& status, | |
148 int response_code, | |
149 const net::ResponseCookies& cookies, | |
150 const std::string& data); | |
151 }; | 141 }; |
152 | 142 |
153 // Version of URLFetcherTest that tests headers. | 143 // Version of URLFetcherTest that tests headers. |
154 class URLFetcherHeadersTest : public URLFetcherTest { | 144 class URLFetcherHeadersTest : public URLFetcherTest { |
155 public: | 145 public: |
156 // URLFetcher::Delegate | 146 // content::URLFetcherDelegate |
157 virtual void OnURLFetchComplete(const URLFetcher* source, | 147 virtual void OnURLFetchComplete(const URLFetcher* source); |
158 const GURL& url, | |
159 const net::URLRequestStatus& status, | |
160 int response_code, | |
161 const net::ResponseCookies& cookies, | |
162 const std::string& data); | |
163 }; | 148 }; |
164 | 149 |
165 // Version of URLFetcherTest that tests SocketAddress. | 150 // Version of URLFetcherTest that tests SocketAddress. |
166 class URLFetcherSocketAddressTest : public URLFetcherTest { | 151 class URLFetcherSocketAddressTest : public URLFetcherTest { |
167 public: | 152 public: |
168 // URLFetcher::Delegate | 153 // content::URLFetcherDelegate |
169 virtual void OnURLFetchComplete(const URLFetcher* source, | 154 virtual void OnURLFetchComplete(const URLFetcher* source); |
170 const GURL& url, | |
171 const net::URLRequestStatus& status, | |
172 int response_code, | |
173 const net::ResponseCookies& cookies, | |
174 const std::string& data); | |
175 protected: | 155 protected: |
176 std::string expected_host_; | 156 std::string expected_host_; |
177 uint16 expected_port_; | 157 uint16 expected_port_; |
178 }; | 158 }; |
179 | 159 |
180 // Version of URLFetcherTest that tests overload protection. | 160 // Version of URLFetcherTest that tests overload protection. |
181 class URLFetcherProtectTest : public URLFetcherTest { | 161 class URLFetcherProtectTest : public URLFetcherTest { |
182 public: | 162 public: |
183 virtual void CreateFetcher(const GURL& url); | 163 virtual void CreateFetcher(const GURL& url); |
184 // URLFetcher::Delegate | 164 // content::URLFetcherDelegate |
185 virtual void OnURLFetchComplete(const URLFetcher* source, | 165 virtual void OnURLFetchComplete(const URLFetcher* source); |
186 const GURL& url, | |
187 const net::URLRequestStatus& status, | |
188 int response_code, | |
189 const net::ResponseCookies& cookies, | |
190 const std::string& data); | |
191 private: | 166 private: |
192 Time start_time_; | 167 Time start_time_; |
193 }; | 168 }; |
194 | 169 |
195 // Version of URLFetcherTest that tests overload protection, when responses | 170 // Version of URLFetcherTest that tests overload protection, when responses |
196 // passed through. | 171 // passed through. |
197 class URLFetcherProtectTestPassedThrough : public URLFetcherTest { | 172 class URLFetcherProtectTestPassedThrough : public URLFetcherTest { |
198 public: | 173 public: |
199 virtual void CreateFetcher(const GURL& url); | 174 virtual void CreateFetcher(const GURL& url); |
200 // URLFetcher::Delegate | 175 // content::URLFetcherDelegate |
201 virtual void OnURLFetchComplete(const URLFetcher* source, | 176 virtual void OnURLFetchComplete(const URLFetcher* source); |
202 const GURL& url, | |
203 const net::URLRequestStatus& status, | |
204 int response_code, | |
205 const net::ResponseCookies& cookies, | |
206 const std::string& data); | |
207 private: | 177 private: |
208 Time start_time_; | 178 Time start_time_; |
209 }; | 179 }; |
210 | 180 |
211 // Version of URLFetcherTest that tests bad HTTPS requests. | 181 // Version of URLFetcherTest that tests bad HTTPS requests. |
212 class URLFetcherBadHTTPSTest : public URLFetcherTest { | 182 class URLFetcherBadHTTPSTest : public URLFetcherTest { |
213 public: | 183 public: |
214 URLFetcherBadHTTPSTest(); | 184 URLFetcherBadHTTPSTest(); |
215 | 185 |
216 // URLFetcher::Delegate | 186 // content::URLFetcherDelegate |
217 virtual void OnURLFetchComplete(const URLFetcher* source, | 187 virtual void OnURLFetchComplete(const URLFetcher* source); |
218 const GURL& url, | |
219 const net::URLRequestStatus& status, | |
220 int response_code, | |
221 const net::ResponseCookies& cookies, | |
222 const std::string& data); | |
223 | 188 |
224 private: | 189 private: |
225 FilePath cert_dir_; | 190 FilePath cert_dir_; |
226 }; | 191 }; |
227 | 192 |
228 // Version of URLFetcherTest that tests request cancellation on shutdown. | 193 // Version of URLFetcherTest that tests request cancellation on shutdown. |
229 class URLFetcherCancelTest : public URLFetcherTest { | 194 class URLFetcherCancelTest : public URLFetcherTest { |
230 public: | 195 public: |
231 virtual void CreateFetcher(const GURL& url); | 196 virtual void CreateFetcher(const GURL& url); |
232 // URLFetcher::Delegate | 197 // content::URLFetcherDelegate |
233 virtual void OnURLFetchComplete(const URLFetcher* source, | 198 virtual void OnURLFetchComplete(const URLFetcher* source); |
234 const GURL& url, | |
235 const net::URLRequestStatus& status, | |
236 int response_code, | |
237 const net::ResponseCookies& cookies, | |
238 const std::string& data); | |
239 | 199 |
240 void CancelRequest(); | 200 void CancelRequest(); |
241 }; | 201 }; |
242 | 202 |
243 // Version of TestURLRequestContext that posts a Quit task to the IO | 203 // Version of TestURLRequestContext that posts a Quit task to the IO |
244 // thread once it is deleted. | 204 // thread once it is deleted. |
245 class CancelTestURLRequestContext : public TestURLRequestContext { | 205 class CancelTestURLRequestContext : public TestURLRequestContext { |
246 virtual ~CancelTestURLRequestContext() { | 206 virtual ~CancelTestURLRequestContext() { |
247 // The d'tor should execute in the IO thread. Post the quit task to the | 207 // The d'tor should execute in the IO thread. Post the quit task to the |
248 // current thread. | 208 // current thread. |
(...skipping 26 matching lines...) Expand all Loading... |
275 ~CancelTestURLRequestContextGetter() {} | 235 ~CancelTestURLRequestContextGetter() {} |
276 | 236 |
277 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 237 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
278 base::WaitableEvent context_created_; | 238 base::WaitableEvent context_created_; |
279 scoped_refptr<net::URLRequestContext> context_; | 239 scoped_refptr<net::URLRequestContext> context_; |
280 }; | 240 }; |
281 | 241 |
282 // Version of URLFetcherTest that tests retying the same request twice. | 242 // Version of URLFetcherTest that tests retying the same request twice. |
283 class URLFetcherMultipleAttemptTest : public URLFetcherTest { | 243 class URLFetcherMultipleAttemptTest : public URLFetcherTest { |
284 public: | 244 public: |
285 // URLFetcher::Delegate | 245 // content::URLFetcherDelegate |
286 virtual void OnURLFetchComplete(const URLFetcher* source, | 246 virtual void OnURLFetchComplete(const URLFetcher* source); |
287 const GURL& url, | |
288 const net::URLRequestStatus& status, | |
289 int response_code, | |
290 const net::ResponseCookies& cookies, | |
291 const std::string& data); | |
292 private: | 247 private: |
293 std::string data_; | 248 std::string data_; |
294 }; | 249 }; |
295 | 250 |
296 class URLFetcherTempFileTest : public URLFetcherTest { | 251 class URLFetcherTempFileTest : public URLFetcherTest { |
297 public: | 252 public: |
298 URLFetcherTempFileTest() | 253 URLFetcherTempFileTest() |
299 : take_ownership_of_temp_file_(false) { | 254 : take_ownership_of_temp_file_(false) { |
300 } | 255 } |
301 | 256 |
302 // URLFetcher::Delegate | 257 // content::URLFetcherDelegate |
303 virtual void OnURLFetchComplete(const URLFetcher* source); | 258 virtual void OnURLFetchComplete(const URLFetcher* source); |
304 | 259 |
305 // This obsolete signature should not be used, but must be present | |
306 // to make clang happy. | |
307 virtual void OnURLFetchComplete(const URLFetcher* source, | |
308 const GURL& url, | |
309 const net::URLRequestStatus& status, | |
310 int response_code, | |
311 const net::ResponseCookies& cookies, | |
312 const std::string& data); | |
313 | |
314 virtual void CreateFetcher(const GURL& url); | 260 virtual void CreateFetcher(const GURL& url); |
315 | 261 |
316 protected: | 262 protected: |
317 FilePath expected_file_; | 263 FilePath expected_file_; |
318 FilePath temp_file_; | 264 FilePath temp_file_; |
319 | 265 |
320 // Set by the test. Used in OnURLFetchComplete() to decide if | 266 // Set by the test. Used in OnURLFetchComplete() to decide if |
321 // the URLFetcher should own the temp file, so that we can test | 267 // the URLFetcher should own the temp file, so that we can test |
322 // disowning prevents the file from being deleted. | 268 // disowning prevents the file from being deleted. |
323 bool take_ownership_of_temp_file_; | 269 bool take_ownership_of_temp_file_; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 | 346 |
401 void URLFetcherPostTest::CreateFetcher(const GURL& url) { | 347 void URLFetcherPostTest::CreateFetcher(const GURL& url) { |
402 fetcher_ = new URLFetcher(url, URLFetcher::POST, this); | 348 fetcher_ = new URLFetcher(url, URLFetcher::POST, this); |
403 fetcher_->set_request_context(new TestURLRequestContextGetter( | 349 fetcher_->set_request_context(new TestURLRequestContextGetter( |
404 io_message_loop_proxy())); | 350 io_message_loop_proxy())); |
405 fetcher_->set_upload_data("application/x-www-form-urlencoded", | 351 fetcher_->set_upload_data("application/x-www-form-urlencoded", |
406 "bobsyeruncle"); | 352 "bobsyeruncle"); |
407 fetcher_->Start(); | 353 fetcher_->Start(); |
408 } | 354 } |
409 | 355 |
410 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source, | 356 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { |
411 const GURL& url, | 357 std::string data; |
412 const net::URLRequestStatus& status, | 358 EXPECT_TRUE(source->GetResponseAsString(&data)); |
413 int response_code, | |
414 const net::ResponseCookies& cookies, | |
415 const std::string& data) { | |
416 EXPECT_EQ(std::string("bobsyeruncle"), data); | 359 EXPECT_EQ(std::string("bobsyeruncle"), data); |
417 URLFetcherTest::OnURLFetchComplete(source, url, status, response_code, | 360 URLFetcherTest::OnURLFetchComplete(source); |
418 cookies, data); | |
419 } | 361 } |
420 | 362 |
421 void URLFetcherHeadersTest::OnURLFetchComplete( | 363 void URLFetcherHeadersTest::OnURLFetchComplete(const URLFetcher* source) { |
422 const URLFetcher* source, | |
423 const GURL& url, | |
424 const net::URLRequestStatus& status, | |
425 int response_code, | |
426 const net::ResponseCookies& cookies, | |
427 const std::string& data) { | |
428 std::string header; | 364 std::string header; |
429 EXPECT_TRUE(source->response_headers()->GetNormalizedHeader("cache-control", | 365 EXPECT_TRUE(source->response_headers()->GetNormalizedHeader("cache-control", |
430 &header)); | 366 &header)); |
431 EXPECT_EQ("private", header); | 367 EXPECT_EQ("private", header); |
432 URLFetcherTest::OnURLFetchComplete(source, url, status, response_code, | 368 URLFetcherTest::OnURLFetchComplete(source); |
433 cookies, data); | |
434 } | 369 } |
435 | 370 |
436 void URLFetcherSocketAddressTest::OnURLFetchComplete( | 371 void URLFetcherSocketAddressTest::OnURLFetchComplete(const URLFetcher* source) { |
437 const URLFetcher* source, | |
438 const GURL& url, | |
439 const net::URLRequestStatus& status, | |
440 int response_code, | |
441 const net::ResponseCookies& cookies, | |
442 const std::string& data) { | |
443 EXPECT_EQ("127.0.0.1", source->socket_address().host()); | 372 EXPECT_EQ("127.0.0.1", source->socket_address().host()); |
444 EXPECT_EQ(expected_port_, source->socket_address().port()); | 373 EXPECT_EQ(expected_port_, source->socket_address().port()); |
445 URLFetcherTest::OnURLFetchComplete(source, url, status, response_code, | 374 URLFetcherTest::OnURLFetchComplete(source); |
446 cookies, data); | |
447 } | 375 } |
448 | 376 |
449 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { | 377 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { |
450 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 378 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); |
451 fetcher_->set_request_context(new TestURLRequestContextGetter( | 379 fetcher_->set_request_context(new TestURLRequestContextGetter( |
452 io_message_loop_proxy())); | 380 io_message_loop_proxy())); |
453 start_time_ = Time::Now(); | 381 start_time_ = Time::Now(); |
454 fetcher_->set_max_retries(11); | 382 fetcher_->set_max_retries(11); |
455 fetcher_->Start(); | 383 fetcher_->Start(); |
456 } | 384 } |
457 | 385 |
458 void URLFetcherProtectTest::OnURLFetchComplete( | 386 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source) { |
459 const URLFetcher* source, | |
460 const GURL& url, | |
461 const net::URLRequestStatus& status, | |
462 int response_code, | |
463 const net::ResponseCookies& cookies, | |
464 const std::string& data) { | |
465 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); | 387 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); |
466 if (response_code >= 500) { | 388 if (source->response_code() >= 500) { |
467 // Now running ServerUnavailable test. | 389 // Now running ServerUnavailable test. |
468 // It takes more than 1 second to finish all 11 requests. | 390 // It takes more than 1 second to finish all 11 requests. |
469 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 391 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
470 EXPECT_TRUE(status.is_success()); | 392 EXPECT_TRUE(source->status().is_success()); |
| 393 std::string data; |
| 394 EXPECT_TRUE(source->GetResponseAsString(&data)); |
471 EXPECT_FALSE(data.empty()); | 395 EXPECT_FALSE(data.empty()); |
472 delete fetcher_; | 396 delete fetcher_; |
473 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 397 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
474 } else { | 398 } else { |
475 // Now running Overload test. | 399 // Now running Overload test. |
476 static int count = 0; | 400 static int count = 0; |
477 count++; | 401 count++; |
478 if (count < 20) { | 402 if (count < 20) { |
479 fetcher_->StartWithRequestContextGetter(new TestURLRequestContextGetter( | 403 fetcher_->StartWithRequestContextGetter(new TestURLRequestContextGetter( |
480 io_message_loop_proxy())); | 404 io_message_loop_proxy())); |
481 } else { | 405 } else { |
482 // We have already sent 20 requests continuously. And we expect that | 406 // We have already sent 20 requests continuously. And we expect that |
483 // it takes more than 1 second due to the overload protection settings. | 407 // it takes more than 1 second due to the overload protection settings. |
484 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 408 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
485 URLFetcherTest::OnURLFetchComplete(source, url, status, response_code, | 409 URLFetcherTest::OnURLFetchComplete(source); |
486 cookies, data); | |
487 } | 410 } |
488 } | 411 } |
489 } | 412 } |
490 | 413 |
491 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { | 414 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { |
492 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 415 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); |
493 fetcher_->set_request_context(new TestURLRequestContextGetter( | 416 fetcher_->set_request_context(new TestURLRequestContextGetter( |
494 io_message_loop_proxy())); | 417 io_message_loop_proxy())); |
495 fetcher_->set_automatically_retry_on_5xx(false); | 418 fetcher_->set_automatically_retry_on_5xx(false); |
496 start_time_ = Time::Now(); | 419 start_time_ = Time::Now(); |
497 fetcher_->set_max_retries(11); | 420 fetcher_->set_max_retries(11); |
498 fetcher_->Start(); | 421 fetcher_->Start(); |
499 } | 422 } |
500 | 423 |
501 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( | 424 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( |
502 const URLFetcher* source, | 425 const URLFetcher* source) { |
503 const GURL& url, | |
504 const net::URLRequestStatus& status, | |
505 int response_code, | |
506 const net::ResponseCookies& cookies, | |
507 const std::string& data) { | |
508 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000); | 426 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000); |
509 if (response_code >= 500) { | 427 if (source->response_code() >= 500) { |
510 // Now running ServerUnavailable test. | 428 // Now running ServerUnavailable test. |
511 // It should get here on the first attempt, so almost immediately and | 429 // It should get here on the first attempt, so almost immediately and |
512 // *not* to attempt to execute all 11 requests (2.5 minutes). | 430 // *not* to attempt to execute all 11 requests (2.5 minutes). |
513 EXPECT_TRUE(Time::Now() - start_time_ < one_minute); | 431 EXPECT_TRUE(Time::Now() - start_time_ < one_minute); |
514 EXPECT_TRUE(status.is_success()); | 432 EXPECT_TRUE(source->status().is_success()); |
515 // Check that suggested back off time is bigger than 0. | 433 // Check that suggested back off time is bigger than 0. |
516 EXPECT_GT(fetcher_->backoff_delay().InMicroseconds(), 0); | 434 EXPECT_GT(fetcher_->backoff_delay().InMicroseconds(), 0); |
| 435 std::string data; |
| 436 EXPECT_TRUE(source->GetResponseAsString(&data)); |
517 EXPECT_FALSE(data.empty()); | 437 EXPECT_FALSE(data.empty()); |
518 } else { | 438 } else { |
519 // We should not get here! | 439 // We should not get here! |
520 ADD_FAILURE(); | 440 ADD_FAILURE(); |
521 } | 441 } |
522 | 442 |
523 delete fetcher_; | 443 delete fetcher_; |
524 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 444 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
525 } | 445 } |
526 | 446 |
527 | 447 |
528 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() { | 448 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() { |
529 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); | 449 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); |
530 cert_dir_ = cert_dir_.AppendASCII("chrome"); | 450 cert_dir_ = cert_dir_.AppendASCII("chrome"); |
531 cert_dir_ = cert_dir_.AppendASCII("test"); | 451 cert_dir_ = cert_dir_.AppendASCII("test"); |
532 cert_dir_ = cert_dir_.AppendASCII("data"); | 452 cert_dir_ = cert_dir_.AppendASCII("data"); |
533 cert_dir_ = cert_dir_.AppendASCII("ssl"); | 453 cert_dir_ = cert_dir_.AppendASCII("ssl"); |
534 cert_dir_ = cert_dir_.AppendASCII("certificates"); | 454 cert_dir_ = cert_dir_.AppendASCII("certificates"); |
535 } | 455 } |
536 | 456 |
537 // The "server certificate expired" error should result in automatic | 457 // The "server certificate expired" error should result in automatic |
538 // cancellation of the request by | 458 // cancellation of the request by |
539 // net::URLRequest::Delegate::OnSSLCertificateError. | 459 // net::URLRequest::Delegate::OnSSLCertificateError. |
540 void URLFetcherBadHTTPSTest::OnURLFetchComplete( | 460 void URLFetcherBadHTTPSTest::OnURLFetchComplete(const URLFetcher* source) { |
541 const URLFetcher* source, | |
542 const GURL& url, | |
543 const net::URLRequestStatus& status, | |
544 int response_code, | |
545 const net::ResponseCookies& cookies, | |
546 const std::string& data) { | |
547 // This part is different from URLFetcherTest::OnURLFetchComplete | 461 // This part is different from URLFetcherTest::OnURLFetchComplete |
548 // because this test expects the request to be cancelled. | 462 // because this test expects the request to be cancelled. |
549 EXPECT_EQ(net::URLRequestStatus::CANCELED, status.status()); | 463 EXPECT_EQ(net::URLRequestStatus::CANCELED, source->status().status()); |
550 EXPECT_EQ(net::ERR_ABORTED, status.error()); | 464 EXPECT_EQ(net::ERR_ABORTED, source->status().error()); |
551 EXPECT_EQ(-1, response_code); | 465 EXPECT_EQ(-1, source->response_code()); |
552 EXPECT_TRUE(cookies.empty()); | 466 EXPECT_TRUE(source->cookies().empty()); |
| 467 std::string data; |
| 468 EXPECT_TRUE(source->GetResponseAsString(&data)); |
553 EXPECT_TRUE(data.empty()); | 469 EXPECT_TRUE(data.empty()); |
554 | 470 |
555 // The rest is the same as URLFetcherTest::OnURLFetchComplete. | 471 // The rest is the same as URLFetcherTest::OnURLFetchComplete. |
556 delete fetcher_; | 472 delete fetcher_; |
557 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 473 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
558 } | 474 } |
559 | 475 |
560 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { | 476 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { |
561 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 477 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); |
562 CancelTestURLRequestContextGetter* context_getter = | 478 CancelTestURLRequestContextGetter* context_getter = |
563 new CancelTestURLRequestContextGetter(io_message_loop_proxy()); | 479 new CancelTestURLRequestContextGetter(io_message_loop_proxy()); |
564 fetcher_->set_request_context(context_getter); | 480 fetcher_->set_request_context(context_getter); |
565 fetcher_->set_max_retries(2); | 481 fetcher_->set_max_retries(2); |
566 fetcher_->Start(); | 482 fetcher_->Start(); |
567 // We need to wait for the creation of the net::URLRequestContext, since we | 483 // We need to wait for the creation of the net::URLRequestContext, since we |
568 // rely on it being destroyed as a signal to end the test. | 484 // rely on it being destroyed as a signal to end the test. |
569 context_getter->WaitForContextCreation(); | 485 context_getter->WaitForContextCreation(); |
570 CancelRequest(); | 486 CancelRequest(); |
571 } | 487 } |
572 | 488 |
573 void URLFetcherCancelTest::OnURLFetchComplete( | 489 void URLFetcherCancelTest::OnURLFetchComplete(const URLFetcher* source) { |
574 const URLFetcher* source, | |
575 const GURL& url, | |
576 const net::URLRequestStatus& status, | |
577 int response_code, | |
578 const net::ResponseCookies& cookies, | |
579 const std::string& data) { | |
580 // We should have cancelled the request before completion. | 490 // We should have cancelled the request before completion. |
581 ADD_FAILURE(); | 491 ADD_FAILURE(); |
582 delete fetcher_; | 492 delete fetcher_; |
583 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 493 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
584 } | 494 } |
585 | 495 |
586 void URLFetcherCancelTest::CancelRequest() { | 496 void URLFetcherCancelTest::CancelRequest() { |
587 delete fetcher_; | 497 delete fetcher_; |
588 // The URLFetcher's test context will post a Quit task once it is | 498 // The URLFetcher's test context will post a Quit task once it is |
589 // deleted. So if this test simply hangs, it means cancellation | 499 // deleted. So if this test simply hangs, it means cancellation |
590 // did not work. | 500 // did not work. |
591 } | 501 } |
592 | 502 |
593 void URLFetcherMultipleAttemptTest::OnURLFetchComplete( | 503 void URLFetcherMultipleAttemptTest::OnURLFetchComplete( |
594 const URLFetcher* source, | 504 const URLFetcher* source) { |
595 const GURL& url, | 505 EXPECT_TRUE(source->status().is_success()); |
596 const net::URLRequestStatus& status, | 506 EXPECT_EQ(200, source->response_code()); // HTTP OK |
597 int response_code, | 507 std::string data; |
598 const net::ResponseCookies& cookies, | 508 EXPECT_TRUE(source->GetResponseAsString(&data)); |
599 const std::string& data) { | |
600 EXPECT_TRUE(status.is_success()); | |
601 EXPECT_EQ(200, response_code); // HTTP OK | |
602 EXPECT_FALSE(data.empty()); | 509 EXPECT_FALSE(data.empty()); |
603 if (!data.empty() && data_.empty()) { | 510 if (!data.empty() && data_.empty()) { |
604 data_ = data; | 511 data_ = data; |
605 fetcher_->StartWithRequestContextGetter( | 512 fetcher_->StartWithRequestContextGetter( |
606 new TestURLRequestContextGetter(io_message_loop_proxy())); | 513 new TestURLRequestContextGetter(io_message_loop_proxy())); |
607 } else { | 514 } else { |
608 EXPECT_EQ(data, data_); | 515 EXPECT_EQ(data, data_); |
609 delete fetcher_; // Have to delete this here and not in the destructor, | 516 delete fetcher_; // Have to delete this here and not in the destructor, |
610 // because the destructor won't necessarily run on the | 517 // because the destructor won't necessarily run on the |
611 // same thread that CreateFetcher() did. | 518 // same thread that CreateFetcher() did. |
(...skipping 11 matching lines...) Expand all Loading... |
623 EXPECT_TRUE(source->GetResponseAsFilePath( | 530 EXPECT_TRUE(source->GetResponseAsFilePath( |
624 take_ownership_of_temp_file_, &temp_file_)); | 531 take_ownership_of_temp_file_, &temp_file_)); |
625 | 532 |
626 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_)); | 533 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_)); |
627 | 534 |
628 delete fetcher_; | 535 delete fetcher_; |
629 | 536 |
630 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 537 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
631 } | 538 } |
632 | 539 |
633 void URLFetcherTempFileTest::OnURLFetchComplete( | |
634 const URLFetcher* source, | |
635 const GURL& url, | |
636 const net::URLRequestStatus& status, | |
637 int response_code, | |
638 const net::ResponseCookies& cookies, | |
639 const std::string& data) { | |
640 NOTREACHED(); | |
641 } | |
642 | |
643 | |
644 TEST_F(URLFetcherTest, SameThreadsTest) { | 540 TEST_F(URLFetcherTest, SameThreadsTest) { |
645 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 541 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
646 ASSERT_TRUE(test_server.Start()); | 542 ASSERT_TRUE(test_server.Start()); |
647 | 543 |
648 // Create the fetcher on the main thread. Since IO will happen on the main | 544 // Create the fetcher on the main thread. Since IO will happen on the main |
649 // thread, this will test URLFetcher's ability to do everything on one | 545 // thread, this will test URLFetcher's ability to do everything on one |
650 // thread. | 546 // thread. |
651 CreateFetcher(test_server.GetURL("defaultresponse")); | 547 CreateFetcher(test_server.GetURL("defaultresponse")); |
652 | 548 |
653 MessageLoop::current()->Run(); | 549 MessageLoop::current()->Run(); |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
891 FROM_HERE, | 787 FROM_HERE, |
892 base::Bind(&CancelAllOnIO), | 788 base::Bind(&CancelAllOnIO), |
893 base::Bind(&MessageLoop::Quit, | 789 base::Bind(&MessageLoop::Quit, |
894 base::Unretained(MessageLoop::current()))); | 790 base::Unretained(MessageLoop::current()))); |
895 MessageLoop::current()->Run(); | 791 MessageLoop::current()->Run(); |
896 EXPECT_EQ(0, GetNumFetcherCores()); | 792 EXPECT_EQ(0, GetNumFetcherCores()); |
897 delete fetcher_; | 793 delete fetcher_; |
898 } | 794 } |
899 | 795 |
900 } // namespace. | 796 } // namespace. |
OLD | NEW |