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_impl.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 "content/public/common/url_fetcher_delegate.h" |
13 #include "crypto/nss_util.h" | 13 #include "crypto/nss_util.h" |
14 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
15 #include "net/test/test_server.h" | 15 #include "net/test/test_server.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 }; | 57 }; |
58 | 58 |
59 } // namespace | 59 } // namespace |
60 | 60 |
61 class URLFetcherTest : public testing::Test, | 61 class URLFetcherTest : public testing::Test, |
62 public content::URLFetcherDelegate { | 62 public content::URLFetcherDelegate { |
63 public: | 63 public: |
64 URLFetcherTest() : fetcher_(NULL) { } | 64 URLFetcherTest() : fetcher_(NULL) { } |
65 | 65 |
66 static int GetNumFetcherCores() { | 66 static int GetNumFetcherCores() { |
67 return URLFetcher::GetNumFetcherCores(); | 67 return URLFetcherImpl::GetNumFetcherCores(); |
68 } | 68 } |
69 | 69 |
70 // 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. |
71 virtual void CreateFetcher(const GURL& url); | 71 virtual void CreateFetcher(const GURL& url); |
72 | 72 |
73 // content::URLFetcherDelegate | 73 // content::URLFetcherDelegate |
74 virtual void OnURLFetchComplete(const content::URLFetcher* source); | 74 virtual void OnURLFetchComplete(const content::URLFetcher* source); |
75 | 75 |
76 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { | 76 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { |
77 return io_message_loop_proxy_; | 77 return io_message_loop_proxy_; |
(...skipping 17 matching lines...) Expand all Loading... |
95 #endif | 95 #endif |
96 } | 96 } |
97 | 97 |
98 // URLFetcher is designed to run on the main UI thread, but in our tests | 98 // URLFetcher is designed to run on the main UI thread, but in our tests |
99 // we assume that the current thread is the IO thread where the URLFetcher | 99 // we assume that the current thread is the IO thread where the URLFetcher |
100 // dispatches its requests to. When we wish to simulate being used from | 100 // dispatches its requests to. When we wish to simulate being used from |
101 // a UI thread, we dispatch a worker thread to do so. | 101 // a UI thread, we dispatch a worker thread to do so. |
102 MessageLoopForIO io_loop_; | 102 MessageLoopForIO io_loop_; |
103 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 103 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
104 | 104 |
105 URLFetcher* fetcher_; | 105 URLFetcherImpl* fetcher_; |
106 }; | 106 }; |
107 | 107 |
108 void URLFetcherTest::CreateFetcher(const GURL& url) { | 108 void URLFetcherTest::CreateFetcher(const GURL& url) { |
109 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 109 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
110 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 110 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
111 io_message_loop_proxy())); | 111 io_message_loop_proxy())); |
112 fetcher_->Start(); | 112 fetcher_->Start(); |
113 } | 113 } |
114 | 114 |
115 void URLFetcherTest::OnURLFetchComplete(const content::URLFetcher* source) { | 115 void URLFetcherTest::OnURLFetchComplete(const content::URLFetcher* source) { |
116 EXPECT_TRUE(source->GetStatus().is_success()); | 116 EXPECT_TRUE(source->GetStatus().is_success()); |
117 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK | 117 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK |
118 | 118 |
119 std::string data; | 119 std::string data; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 FilePath expected_file_; | 263 FilePath expected_file_; |
264 FilePath temp_file_; | 264 FilePath temp_file_; |
265 | 265 |
266 // Set by the test. Used in OnURLFetchComplete() to decide if | 266 // Set by the test. Used in OnURLFetchComplete() to decide if |
267 // 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 |
268 // disowning prevents the file from being deleted. | 268 // disowning prevents the file from being deleted. |
269 bool take_ownership_of_temp_file_; | 269 bool take_ownership_of_temp_file_; |
270 }; | 270 }; |
271 | 271 |
272 void URLFetcherTempFileTest::CreateFetcher(const GURL& url) { | 272 void URLFetcherTempFileTest::CreateFetcher(const GURL& url) { |
273 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 273 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
274 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 274 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
275 io_message_loop_proxy())); | 275 io_message_loop_proxy())); |
276 | 276 |
277 // Use the IO message loop to do the file operations in this test. | 277 // Use the IO message loop to do the file operations in this test. |
278 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); | 278 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); |
279 fetcher_->Start(); | 279 fetcher_->Start(); |
280 } | 280 } |
281 | 281 |
282 TEST_F(URLFetcherTempFileTest, SmallGet) { | 282 TEST_F(URLFetcherTempFileTest, SmallGet) { |
283 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 283 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 virtual void Run() { | 338 virtual void Run() { |
339 test_->CreateFetcher(url_); | 339 test_->CreateFetcher(url_); |
340 } | 340 } |
341 | 341 |
342 private: | 342 private: |
343 URLFetcherTest* test_; | 343 URLFetcherTest* test_; |
344 GURL url_; | 344 GURL url_; |
345 }; | 345 }; |
346 | 346 |
347 void URLFetcherPostTest::CreateFetcher(const GURL& url) { | 347 void URLFetcherPostTest::CreateFetcher(const GURL& url) { |
348 fetcher_ = new URLFetcher(url, URLFetcher::POST, this); | 348 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this); |
349 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 349 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
350 io_message_loop_proxy())); | 350 io_message_loop_proxy())); |
351 fetcher_->SetUploadData("application/x-www-form-urlencoded", | 351 fetcher_->SetUploadData("application/x-www-form-urlencoded", |
352 "bobsyeruncle"); | 352 "bobsyeruncle"); |
353 fetcher_->Start(); | 353 fetcher_->Start(); |
354 } | 354 } |
355 | 355 |
356 void URLFetcherPostTest::OnURLFetchComplete(const content::URLFetcher* source) { | 356 void URLFetcherPostTest::OnURLFetchComplete(const content::URLFetcher* source) { |
357 std::string data; | 357 std::string data; |
358 EXPECT_TRUE(source->GetResponseAsString(&data)); | 358 EXPECT_TRUE(source->GetResponseAsString(&data)); |
359 EXPECT_EQ(std::string("bobsyeruncle"), data); | 359 EXPECT_EQ(std::string("bobsyeruncle"), data); |
360 URLFetcherTest::OnURLFetchComplete(source); | 360 URLFetcherTest::OnURLFetchComplete(source); |
361 } | 361 } |
362 | 362 |
363 void URLFetcherHeadersTest::OnURLFetchComplete(const content::URLFetcher* source
) { | 363 void URLFetcherHeadersTest::OnURLFetchComplete( |
| 364 const content::URLFetcher* source) { |
364 std::string header; | 365 std::string header; |
365 EXPECT_TRUE(source->GetResponseHeaders()->GetNormalizedHeader("cache-control", | 366 EXPECT_TRUE(source->GetResponseHeaders()->GetNormalizedHeader("cache-control", |
366 &header)); | 367 &header)); |
367 EXPECT_EQ("private", header); | 368 EXPECT_EQ("private", header); |
368 URLFetcherTest::OnURLFetchComplete(source); | 369 URLFetcherTest::OnURLFetchComplete(source); |
369 } | 370 } |
370 | 371 |
371 void URLFetcherSocketAddressTest::OnURLFetchComplete( | 372 void URLFetcherSocketAddressTest::OnURLFetchComplete( |
372 const content::URLFetcher* source) { | 373 const content::URLFetcher* source) { |
373 EXPECT_EQ("127.0.0.1", source->GetSocketAddress().host()); | 374 EXPECT_EQ("127.0.0.1", source->GetSocketAddress().host()); |
374 EXPECT_EQ(expected_port_, source->GetSocketAddress().port()); | 375 EXPECT_EQ(expected_port_, source->GetSocketAddress().port()); |
375 URLFetcherTest::OnURLFetchComplete(source); | 376 URLFetcherTest::OnURLFetchComplete(source); |
376 } | 377 } |
377 | 378 |
378 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { | 379 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { |
379 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 380 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
380 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 381 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
381 io_message_loop_proxy())); | 382 io_message_loop_proxy())); |
382 start_time_ = Time::Now(); | 383 start_time_ = Time::Now(); |
383 fetcher_->SetMaxRetries(11); | 384 fetcher_->SetMaxRetries(11); |
384 fetcher_->Start(); | 385 fetcher_->Start(); |
385 } | 386 } |
386 | 387 |
387 void URLFetcherProtectTest::OnURLFetchComplete( | 388 void URLFetcherProtectTest::OnURLFetchComplete( |
388 const content::URLFetcher* source) { | 389 const content::URLFetcher* source) { |
389 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); | 390 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); |
(...skipping 17 matching lines...) Expand all Loading... |
407 } else { | 408 } else { |
408 // We have already sent 20 requests continuously. And we expect that | 409 // We have already sent 20 requests continuously. And we expect that |
409 // it takes more than 1 second due to the overload protection settings. | 410 // it takes more than 1 second due to the overload protection settings. |
410 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 411 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
411 URLFetcherTest::OnURLFetchComplete(source); | 412 URLFetcherTest::OnURLFetchComplete(source); |
412 } | 413 } |
413 } | 414 } |
414 } | 415 } |
415 | 416 |
416 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { | 417 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { |
417 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 418 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
418 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 419 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
419 io_message_loop_proxy())); | 420 io_message_loop_proxy())); |
420 fetcher_->SetAutomaticallyRetryOn5xx(false); | 421 fetcher_->SetAutomaticallyRetryOn5xx(false); |
421 start_time_ = Time::Now(); | 422 start_time_ = Time::Now(); |
422 fetcher_->SetMaxRetries(11); | 423 fetcher_->SetMaxRetries(11); |
423 fetcher_->Start(); | 424 fetcher_->Start(); |
424 } | 425 } |
425 | 426 |
426 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( | 427 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( |
427 const content::URLFetcher* source) { | 428 const content::URLFetcher* source) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 std::string data; | 471 std::string data; |
471 EXPECT_TRUE(source->GetResponseAsString(&data)); | 472 EXPECT_TRUE(source->GetResponseAsString(&data)); |
472 EXPECT_TRUE(data.empty()); | 473 EXPECT_TRUE(data.empty()); |
473 | 474 |
474 // The rest is the same as URLFetcherTest::OnURLFetchComplete. | 475 // The rest is the same as URLFetcherTest::OnURLFetchComplete. |
475 delete fetcher_; | 476 delete fetcher_; |
476 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 477 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
477 } | 478 } |
478 | 479 |
479 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { | 480 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { |
480 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 481 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
481 CancelTestURLRequestContextGetter* context_getter = | 482 CancelTestURLRequestContextGetter* context_getter = |
482 new CancelTestURLRequestContextGetter(io_message_loop_proxy()); | 483 new CancelTestURLRequestContextGetter(io_message_loop_proxy()); |
483 fetcher_->SetRequestContext(context_getter); | 484 fetcher_->SetRequestContext(context_getter); |
484 fetcher_->SetMaxRetries(2); | 485 fetcher_->SetMaxRetries(2); |
485 fetcher_->Start(); | 486 fetcher_->Start(); |
486 // We need to wait for the creation of the net::URLRequestContext, since we | 487 // We need to wait for the creation of the net::URLRequestContext, since we |
487 // rely on it being destroyed as a signal to end the test. | 488 // rely on it being destroyed as a signal to end the test. |
488 context_getter->WaitForContextCreation(); | 489 context_getter->WaitForContextCreation(); |
489 CancelRequest(); | 490 CancelRequest(); |
490 } | 491 } |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
770 // Create the fetcher on the main thread. Since IO will happen on the main | 771 // Create the fetcher on the main thread. Since IO will happen on the main |
771 // thread, this will test URLFetcher's ability to do everything on one | 772 // thread, this will test URLFetcher's ability to do everything on one |
772 // thread. | 773 // thread. |
773 CreateFetcher(test_server.GetURL("defaultresponse")); | 774 CreateFetcher(test_server.GetURL("defaultresponse")); |
774 | 775 |
775 MessageLoop::current()->Run(); | 776 MessageLoop::current()->Run(); |
776 } | 777 } |
777 | 778 |
778 void CancelAllOnIO() { | 779 void CancelAllOnIO() { |
779 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores()); | 780 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores()); |
780 URLFetcher::CancelAll(); | 781 URLFetcherImpl::CancelAll(); |
781 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores()); | 782 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores()); |
782 } | 783 } |
783 | 784 |
784 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. | 785 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. |
785 TEST_F(URLFetcherTest, CancelAll) { | 786 TEST_F(URLFetcherTest, CancelAll) { |
786 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 787 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
787 ASSERT_TRUE(test_server.Start()); | 788 ASSERT_TRUE(test_server.Start()); |
788 EXPECT_EQ(0, GetNumFetcherCores()); | 789 EXPECT_EQ(0, GetNumFetcherCores()); |
789 | 790 |
790 CreateFetcher(test_server.GetURL("defaultresponse")); | 791 CreateFetcher(test_server.GetURL("defaultresponse")); |
791 io_message_loop_proxy()->PostTaskAndReply( | 792 io_message_loop_proxy()->PostTaskAndReply( |
792 FROM_HERE, | 793 FROM_HERE, |
793 base::Bind(&CancelAllOnIO), | 794 base::Bind(&CancelAllOnIO), |
794 base::Bind(&MessageLoop::Quit, | 795 base::Bind(&MessageLoop::Quit, |
795 base::Unretained(MessageLoop::current()))); | 796 base::Unretained(MessageLoop::current()))); |
796 MessageLoop::current()->Run(); | 797 MessageLoop::current()->Run(); |
797 EXPECT_EQ(0, GetNumFetcherCores()); | 798 EXPECT_EQ(0, GetNumFetcherCores()); |
798 delete fetcher_; | 799 delete fetcher_; |
799 } | 800 } |
800 | 801 |
801 } // namespace. | 802 } // namespace. |
OLD | NEW |