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 "content/common/net/url_fetcher_impl.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/scoped_temp_dir.h" |
9 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
10 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
11 #include "build/build_config.h" | 12 #include "build/build_config.h" |
12 #include "content/public/common/url_fetcher_delegate.h" | 13 #include "content/public/common/url_fetcher_delegate.h" |
13 #include "crypto/nss_util.h" | 14 #include "crypto/nss_util.h" |
14 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
15 #include "net/test/test_server.h" | 16 #include "net/test/test_server.h" |
16 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
17 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
18 #include "net/url_request/url_request_throttler_manager.h" | 19 #include "net/url_request/url_request_throttler_manager.h" |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 | 250 |
250 // Version of URLFetcherTest that tests retying the same request twice. | 251 // Version of URLFetcherTest that tests retying the same request twice. |
251 class URLFetcherMultipleAttemptTest : public URLFetcherTest { | 252 class URLFetcherMultipleAttemptTest : public URLFetcherTest { |
252 public: | 253 public: |
253 // content::URLFetcherDelegate | 254 // content::URLFetcherDelegate |
254 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; | 255 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
255 private: | 256 private: |
256 std::string data_; | 257 std::string data_; |
257 }; | 258 }; |
258 | 259 |
259 class URLFetcherTempFileTest : public URLFetcherTest { | 260 class URLFetcherFileTest : public URLFetcherTest { |
260 public: | 261 public: |
261 URLFetcherTempFileTest() | 262 URLFetcherFileTest() : take_ownership_of_file_(false), |
262 : take_ownership_of_temp_file_(false) { | 263 expected_file_error_(base::PLATFORM_FILE_OK) {} |
263 } | |
264 | 264 |
265 // URLFetcherTest override. | 265 void CreateFetcherForFile(const GURL& url, const FilePath& file_path); |
266 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 266 void CreateFetcherForTempFile(const GURL& url); |
267 | 267 |
268 // content::URLFetcherDelegate | 268 // content::URLFetcherDelegate |
269 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; | 269 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
270 | 270 |
271 protected: | 271 protected: |
272 FilePath expected_file_; | 272 FilePath expected_file_; |
273 FilePath temp_file_; | 273 FilePath file_path_; |
274 | 274 |
275 // Set by the test. Used in OnURLFetchComplete() to decide if | 275 // Set by the test. Used in OnURLFetchComplete() to decide if |
276 // the URLFetcher should own the temp file, so that we can test | 276 // the URLFetcher should own the temp file, so that we can test |
277 // disowning prevents the file from being deleted. | 277 // disowning prevents the file from being deleted. |
278 bool take_ownership_of_temp_file_; | 278 bool take_ownership_of_file_; |
| 279 |
| 280 // Expected file error code for the test. |
| 281 // PLATFORM_FILE_OK when expecting success. |
| 282 base::PlatformFileError expected_file_error_; |
279 }; | 283 }; |
280 | 284 |
281 void URLFetcherPostTest::CreateFetcher(const GURL& url) { | 285 void URLFetcherPostTest::CreateFetcher(const GURL& url) { |
282 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this); | 286 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this); |
283 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 287 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
284 io_message_loop_proxy())); | 288 io_message_loop_proxy())); |
285 fetcher_->SetUploadData("application/x-www-form-urlencoded", | 289 fetcher_->SetUploadData("application/x-www-form-urlencoded", |
286 "bobsyeruncle"); | 290 "bobsyeruncle"); |
287 fetcher_->Start(); | 291 fetcher_->Start(); |
288 } | 292 } |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 delete fetcher_; // Have to delete this here and not in the destructor, | 503 delete fetcher_; // Have to delete this here and not in the destructor, |
500 // because the destructor won't necessarily run on the | 504 // because the destructor won't necessarily run on the |
501 // same thread that CreateFetcher() did. | 505 // same thread that CreateFetcher() did. |
502 | 506 |
503 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 507 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
504 // If the current message loop is not the IO loop, it will be shut down when | 508 // If the current message loop is not the IO loop, it will be shut down when |
505 // the main loop returns and this thread subsequently goes out of scope. | 509 // the main loop returns and this thread subsequently goes out of scope. |
506 } | 510 } |
507 } | 511 } |
508 | 512 |
509 void URLFetcherTempFileTest::CreateFetcher(const GURL& url) { | 513 void URLFetcherFileTest::CreateFetcherForFile(const GURL& url, |
| 514 const FilePath& file_path) { |
510 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); | 515 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
511 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 516 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
512 io_message_loop_proxy())); | 517 io_message_loop_proxy())); |
| 518 |
| 519 // Use the IO message loop to do the file operations in this test. |
| 520 fetcher_->SaveResponseToFileAtPath(file_path, io_message_loop_proxy()); |
| 521 fetcher_->Start(); |
| 522 } |
| 523 |
| 524 void URLFetcherFileTest::CreateFetcherForTempFile(const GURL& url) { |
| 525 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
| 526 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 527 io_message_loop_proxy())); |
513 | 528 |
514 // Use the IO message loop to do the file operations in this test. | 529 // Use the IO message loop to do the file operations in this test. |
515 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); | 530 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); |
516 fetcher_->Start(); | 531 fetcher_->Start(); |
517 } | 532 } |
518 | 533 |
519 void URLFetcherTempFileTest::OnURLFetchComplete( | 534 void URLFetcherFileTest::OnURLFetchComplete(const content::URLFetcher* source) { |
520 const content::URLFetcher* source) { | 535 if (expected_file_error_ == base::PLATFORM_FILE_OK) { |
521 EXPECT_TRUE(source->GetStatus().is_success()); | 536 EXPECT_TRUE(source->GetStatus().is_success()); |
522 EXPECT_EQ(source->GetResponseCode(), 200); | 537 EXPECT_EQ(source->GetResponseCode(), 200); |
523 | 538 |
524 EXPECT_TRUE(source->GetResponseAsFilePath( | 539 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
525 take_ownership_of_temp_file_, &temp_file_)); | 540 EXPECT_FALSE(fetcher_->FileErrorOccurred(&error_code)); |
526 | 541 |
527 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_)); | 542 EXPECT_TRUE(source->GetResponseAsFilePath( |
| 543 take_ownership_of_file_, &file_path_)); |
528 | 544 |
| 545 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, file_path_)); |
| 546 } else { |
| 547 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
| 548 EXPECT_TRUE(fetcher_->FileErrorOccurred(&error_code)); |
| 549 EXPECT_EQ(expected_file_error_, error_code); |
| 550 } |
529 delete fetcher_; | 551 delete fetcher_; |
530 | 552 |
531 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 553 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
532 } | 554 } |
533 | 555 |
534 TEST_F(URLFetcherTest, SameThreadsTest) { | 556 TEST_F(URLFetcherTest, SameThreadsTest) { |
535 net::TestServer test_server(net::TestServer::TYPE_HTTP, | 557 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
536 net::TestServer::kLocalhost, | 558 net::TestServer::kLocalhost, |
537 FilePath(kDocRoot)); | 559 FilePath(kDocRoot)); |
538 ASSERT_TRUE(test_server.Start()); | 560 ASSERT_TRUE(test_server.Start()); |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
842 ASSERT_TRUE(test_server.Start()); | 864 ASSERT_TRUE(test_server.Start()); |
843 | 865 |
844 // Create the fetcher on the main thread. Since IO will happen on the main | 866 // Create the fetcher on the main thread. Since IO will happen on the main |
845 // thread, this will test URLFetcher's ability to do everything on one | 867 // thread, this will test URLFetcher's ability to do everything on one |
846 // thread. | 868 // thread. |
847 CreateFetcher(test_server.GetURL("defaultresponse")); | 869 CreateFetcher(test_server.GetURL("defaultresponse")); |
848 | 870 |
849 MessageLoop::current()->Run(); | 871 MessageLoop::current()->Run(); |
850 } | 872 } |
851 | 873 |
852 TEST_F(URLFetcherTempFileTest, SmallGet) { | 874 TEST_F(URLFetcherFileTest, SmallGet) { |
| 875 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 876 net::TestServer::kLocalhost, |
| 877 FilePath(kDocRoot)); |
| 878 ASSERT_TRUE(test_server.Start()); |
| 879 |
| 880 ScopedTempDir temp_dir; |
| 881 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 882 |
| 883 // Get a small file. |
| 884 static const char kFileToFetch[] = "simple.html"; |
| 885 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
| 886 CreateFetcherForFile( |
| 887 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 888 temp_dir.path().AppendASCII(kFileToFetch)); |
| 889 |
| 890 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 891 |
| 892 ASSERT_FALSE(file_util::PathExists(file_path_)) |
| 893 << file_path_.value() << " not removed."; |
| 894 } |
| 895 |
| 896 TEST_F(URLFetcherFileTest, LargeGet) { |
| 897 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 898 net::TestServer::kLocalhost, |
| 899 FilePath(kDocRoot)); |
| 900 ASSERT_TRUE(test_server.Start()); |
| 901 |
| 902 ScopedTempDir temp_dir; |
| 903 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 904 |
| 905 // Get a file large enough to require more than one read into |
| 906 // URLFetcher::Core's IOBuffer. |
| 907 static const char kFileToFetch[] = "animate1.gif"; |
| 908 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
| 909 CreateFetcherForFile( |
| 910 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 911 temp_dir.path().AppendASCII(kFileToFetch)); |
| 912 |
| 913 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 914 } |
| 915 |
| 916 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfFile) { |
| 917 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 918 net::TestServer::kLocalhost, |
| 919 FilePath(kDocRoot)); |
| 920 ASSERT_TRUE(test_server.Start()); |
| 921 |
| 922 ScopedTempDir temp_dir; |
| 923 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 924 |
| 925 // Get a small file. |
| 926 static const char kFileToFetch[] = "simple.html"; |
| 927 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
| 928 CreateFetcherForFile( |
| 929 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 930 temp_dir.path().AppendASCII(kFileToFetch)); |
| 931 |
| 932 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 933 |
| 934 MessageLoop::current()->RunAllPending(); |
| 935 ASSERT_FALSE(file_util::PathExists(file_path_)) |
| 936 << file_path_.value() << " not removed."; |
| 937 } |
| 938 |
| 939 |
| 940 TEST_F(URLFetcherFileTest, OverwriteExistingFile) { |
| 941 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 942 net::TestServer::kLocalhost, |
| 943 FilePath(kDocRoot)); |
| 944 ASSERT_TRUE(test_server.Start()); |
| 945 |
| 946 ScopedTempDir temp_dir; |
| 947 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 948 |
| 949 // Create a file before trying to fetch. |
| 950 static const char kFileToFetch[] = "simple.html"; |
| 951 static const char kData[] = "abcdefghijklmnopqrstuvwxyz"; |
| 952 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); |
| 953 const int data_size = arraysize(kData); |
| 954 ASSERT_EQ(file_util::WriteFile(file_path_, kData, data_size), data_size); |
| 955 ASSERT_TRUE(file_util::PathExists(file_path_)); |
| 956 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
| 957 ASSERT_FALSE(file_util::ContentsEqual(file_path_, expected_file_)); |
| 958 |
| 959 // Get a small file. |
| 960 CreateFetcherForFile( |
| 961 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 962 file_path_); |
| 963 |
| 964 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 965 } |
| 966 |
| 967 TEST_F(URLFetcherFileTest, TryToOverwriteDirectory) { |
| 968 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 969 net::TestServer::kLocalhost, |
| 970 FilePath(kDocRoot)); |
| 971 ASSERT_TRUE(test_server.Start()); |
| 972 |
| 973 ScopedTempDir temp_dir; |
| 974 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 975 |
| 976 // Create a directory before trying to fetch. |
| 977 static const char kFileToFetch[] = "simple.html"; |
| 978 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); |
| 979 ASSERT_TRUE(file_util::CreateDirectory(file_path_)); |
| 980 ASSERT_TRUE(file_util::PathExists(file_path_)); |
| 981 |
| 982 // Get a small file. |
| 983 expected_file_error_ = base::PLATFORM_FILE_ERROR_ACCESS_DENIED; |
| 984 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
| 985 CreateFetcherForFile( |
| 986 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 987 file_path_); |
| 988 |
| 989 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 990 |
| 991 MessageLoop::current()->RunAllPending(); |
| 992 } |
| 993 |
| 994 TEST_F(URLFetcherFileTest, SmallGetToTempFile) { |
853 net::TestServer test_server(net::TestServer::TYPE_HTTP, | 995 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
854 net::TestServer::kLocalhost, | 996 net::TestServer::kLocalhost, |
855 FilePath(kDocRoot)); | 997 FilePath(kDocRoot)); |
856 ASSERT_TRUE(test_server.Start()); | 998 ASSERT_TRUE(test_server.Start()); |
857 | 999 |
858 // Get a small file. | 1000 // Get a small file. |
859 static const char kFileToFetch[] = "simple.html"; | 1001 static const char kFileToFetch[] = "simple.html"; |
860 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1002 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
861 CreateFetcher( | 1003 CreateFetcherForTempFile( |
862 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); | 1004 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); |
863 | 1005 |
864 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1006 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
865 | 1007 |
866 ASSERT_FALSE(file_util::PathExists(temp_file_)) | 1008 ASSERT_FALSE(file_util::PathExists(file_path_)) |
867 << temp_file_.value() << " not removed."; | 1009 << file_path_.value() << " not removed."; |
868 } | 1010 } |
869 | 1011 |
870 TEST_F(URLFetcherTempFileTest, LargeGet) { | 1012 TEST_F(URLFetcherFileTest, LargeGetToTempFile) { |
871 net::TestServer test_server(net::TestServer::TYPE_HTTP, | 1013 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
872 net::TestServer::kLocalhost, | 1014 net::TestServer::kLocalhost, |
873 FilePath(kDocRoot)); | 1015 FilePath(kDocRoot)); |
874 ASSERT_TRUE(test_server.Start()); | 1016 ASSERT_TRUE(test_server.Start()); |
875 | 1017 |
876 // Get a file large enough to require more than one read into | 1018 // Get a file large enough to require more than one read into |
877 // URLFetcher::Core's IOBuffer. | 1019 // URLFetcher::Core's IOBuffer. |
878 static const char kFileToFetch[] = "animate1.gif"; | 1020 static const char kFileToFetch[] = "animate1.gif"; |
879 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1021 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
880 CreateFetcher(test_server.GetURL( | 1022 CreateFetcherForTempFile(test_server.GetURL( |
881 std::string(kTestServerFilePrefix) + kFileToFetch)); | 1023 std::string(kTestServerFilePrefix) + kFileToFetch)); |
882 | 1024 |
883 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1025 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
884 } | 1026 } |
885 | 1027 |
886 TEST_F(URLFetcherTempFileTest, CanTakeOwnershipOfFile) { | 1028 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfTempFile) { |
887 net::TestServer test_server(net::TestServer::TYPE_HTTP, | 1029 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
888 net::TestServer::kLocalhost, | 1030 net::TestServer::kLocalhost, |
889 FilePath(kDocRoot)); | 1031 FilePath(kDocRoot)); |
890 ASSERT_TRUE(test_server.Start()); | 1032 ASSERT_TRUE(test_server.Start()); |
891 | 1033 |
892 // Get a small file. | 1034 // Get a small file. |
893 static const char kFileToFetch[] = "simple.html"; | 1035 static const char kFileToFetch[] = "simple.html"; |
894 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1036 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
895 CreateFetcher(test_server.GetURL( | 1037 CreateFetcherForTempFile(test_server.GetURL( |
896 std::string(kTestServerFilePrefix) + kFileToFetch)); | 1038 std::string(kTestServerFilePrefix) + kFileToFetch)); |
897 | 1039 |
898 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1040 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
899 | 1041 |
900 MessageLoop::current()->RunAllPending(); | 1042 MessageLoop::current()->RunAllPending(); |
901 ASSERT_FALSE(file_util::PathExists(temp_file_)) | 1043 ASSERT_FALSE(file_util::PathExists(file_path_)) |
902 << temp_file_.value() << " not removed."; | 1044 << file_path_.value() << " not removed."; |
903 } | 1045 } |
904 | 1046 |
905 } // namespace. | 1047 } // namespace. |
OLD | NEW |