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/message_loop_proxy.h" | 12 #include "base/message_loop_proxy.h" |
12 #include "base/scoped_temp_dir.h" | |
13 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
16 #include "crypto/nss_util.h" | 16 #include "crypto/nss_util.h" |
17 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
18 #include "net/test/test_server.h" | 18 #include "net/test/test_server.h" |
19 #include "net/url_request/url_fetcher_delegate.h" | 19 #include "net/url_request/url_fetcher_delegate.h" |
20 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
21 #include "net/url_request/url_request_test_util.h" | 21 #include "net/url_request/url_request_test_util.h" |
22 #include "net/url_request/url_request_throttler_manager.h" | 22 #include "net/url_request/url_request_throttler_manager.h" |
(...skipping 1052 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1075 | 1075 |
1076 MessageLoop::current()->Run(); | 1076 MessageLoop::current()->Run(); |
1077 } | 1077 } |
1078 | 1078 |
1079 TEST_F(URLFetcherFileTest, SmallGet) { | 1079 TEST_F(URLFetcherFileTest, SmallGet) { |
1080 TestServer test_server(TestServer::TYPE_HTTP, | 1080 TestServer test_server(TestServer::TYPE_HTTP, |
1081 TestServer::kLocalhost, | 1081 TestServer::kLocalhost, |
1082 FilePath(kDocRoot)); | 1082 FilePath(kDocRoot)); |
1083 ASSERT_TRUE(test_server.Start()); | 1083 ASSERT_TRUE(test_server.Start()); |
1084 | 1084 |
1085 ScopedTempDir temp_dir; | 1085 base::ScopedTempDir temp_dir; |
1086 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1086 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
1087 | 1087 |
1088 // Get a small file. | 1088 // Get a small file. |
1089 static const char kFileToFetch[] = "simple.html"; | 1089 static const char kFileToFetch[] = "simple.html"; |
1090 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | 1090 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); |
1091 CreateFetcherForFile( | 1091 CreateFetcherForFile( |
1092 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1092 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
1093 temp_dir.path().AppendASCII(kFileToFetch)); | 1093 temp_dir.path().AppendASCII(kFileToFetch)); |
1094 | 1094 |
1095 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1095 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1096 | 1096 |
1097 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1097 ASSERT_FALSE(file_util::PathExists(file_path_)) |
1098 << file_path_.value() << " not removed."; | 1098 << file_path_.value() << " not removed."; |
1099 } | 1099 } |
1100 | 1100 |
1101 TEST_F(URLFetcherFileTest, LargeGet) { | 1101 TEST_F(URLFetcherFileTest, LargeGet) { |
1102 TestServer test_server(TestServer::TYPE_HTTP, | 1102 TestServer test_server(TestServer::TYPE_HTTP, |
1103 TestServer::kLocalhost, | 1103 TestServer::kLocalhost, |
1104 FilePath(kDocRoot)); | 1104 FilePath(kDocRoot)); |
1105 ASSERT_TRUE(test_server.Start()); | 1105 ASSERT_TRUE(test_server.Start()); |
1106 | 1106 |
1107 ScopedTempDir temp_dir; | 1107 base::ScopedTempDir temp_dir; |
1108 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1108 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
1109 | 1109 |
1110 // Get a file large enough to require more than one read into | 1110 // Get a file large enough to require more than one read into |
1111 // URLFetcher::Core's IOBuffer. | 1111 // URLFetcher::Core's IOBuffer. |
1112 static const char kFileToFetch[] = "animate1.gif"; | 1112 static const char kFileToFetch[] = "animate1.gif"; |
1113 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | 1113 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); |
1114 CreateFetcherForFile( | 1114 CreateFetcherForFile( |
1115 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1115 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
1116 temp_dir.path().AppendASCII(kFileToFetch)); | 1116 temp_dir.path().AppendASCII(kFileToFetch)); |
1117 | 1117 |
1118 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1118 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1119 } | 1119 } |
1120 | 1120 |
1121 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfFile) { | 1121 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfFile) { |
1122 TestServer test_server(TestServer::TYPE_HTTP, | 1122 TestServer test_server(TestServer::TYPE_HTTP, |
1123 TestServer::kLocalhost, | 1123 TestServer::kLocalhost, |
1124 FilePath(kDocRoot)); | 1124 FilePath(kDocRoot)); |
1125 ASSERT_TRUE(test_server.Start()); | 1125 ASSERT_TRUE(test_server.Start()); |
1126 | 1126 |
1127 ScopedTempDir temp_dir; | 1127 base::ScopedTempDir temp_dir; |
1128 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1128 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
1129 | 1129 |
1130 // Get a small file. | 1130 // Get a small file. |
1131 static const char kFileToFetch[] = "simple.html"; | 1131 static const char kFileToFetch[] = "simple.html"; |
1132 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | 1132 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); |
1133 CreateFetcherForFile( | 1133 CreateFetcherForFile( |
1134 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1134 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
1135 temp_dir.path().AppendASCII(kFileToFetch)); | 1135 temp_dir.path().AppendASCII(kFileToFetch)); |
1136 | 1136 |
1137 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1137 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1138 | 1138 |
1139 MessageLoop::current()->RunUntilIdle(); | 1139 MessageLoop::current()->RunUntilIdle(); |
1140 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1140 ASSERT_FALSE(file_util::PathExists(file_path_)) |
1141 << file_path_.value() << " not removed."; | 1141 << file_path_.value() << " not removed."; |
1142 } | 1142 } |
1143 | 1143 |
1144 | 1144 |
1145 TEST_F(URLFetcherFileTest, OverwriteExistingFile) { | 1145 TEST_F(URLFetcherFileTest, OverwriteExistingFile) { |
1146 TestServer test_server(TestServer::TYPE_HTTP, | 1146 TestServer test_server(TestServer::TYPE_HTTP, |
1147 TestServer::kLocalhost, | 1147 TestServer::kLocalhost, |
1148 FilePath(kDocRoot)); | 1148 FilePath(kDocRoot)); |
1149 ASSERT_TRUE(test_server.Start()); | 1149 ASSERT_TRUE(test_server.Start()); |
1150 | 1150 |
1151 ScopedTempDir temp_dir; | 1151 base::ScopedTempDir temp_dir; |
1152 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1152 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
1153 | 1153 |
1154 // Create a file before trying to fetch. | 1154 // Create a file before trying to fetch. |
1155 static const char kFileToFetch[] = "simple.html"; | 1155 static const char kFileToFetch[] = "simple.html"; |
1156 static const char kData[] = "abcdefghijklmnopqrstuvwxyz"; | 1156 static const char kData[] = "abcdefghijklmnopqrstuvwxyz"; |
1157 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); | 1157 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); |
1158 const int data_size = arraysize(kData); | 1158 const int data_size = arraysize(kData); |
1159 ASSERT_EQ(file_util::WriteFile(file_path_, kData, data_size), data_size); | 1159 ASSERT_EQ(file_util::WriteFile(file_path_, kData, data_size), data_size); |
1160 ASSERT_TRUE(file_util::PathExists(file_path_)); | 1160 ASSERT_TRUE(file_util::PathExists(file_path_)); |
1161 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | 1161 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); |
1162 ASSERT_FALSE(file_util::ContentsEqual(file_path_, expected_file_)); | 1162 ASSERT_FALSE(file_util::ContentsEqual(file_path_, expected_file_)); |
1163 | 1163 |
1164 // Get a small file. | 1164 // Get a small file. |
1165 CreateFetcherForFile( | 1165 CreateFetcherForFile( |
1166 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1166 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
1167 file_path_); | 1167 file_path_); |
1168 | 1168 |
1169 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1169 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1170 } | 1170 } |
1171 | 1171 |
1172 TEST_F(URLFetcherFileTest, TryToOverwriteDirectory) { | 1172 TEST_F(URLFetcherFileTest, TryToOverwriteDirectory) { |
1173 TestServer test_server(TestServer::TYPE_HTTP, | 1173 TestServer test_server(TestServer::TYPE_HTTP, |
1174 TestServer::kLocalhost, | 1174 TestServer::kLocalhost, |
1175 FilePath(kDocRoot)); | 1175 FilePath(kDocRoot)); |
1176 ASSERT_TRUE(test_server.Start()); | 1176 ASSERT_TRUE(test_server.Start()); |
1177 | 1177 |
1178 ScopedTempDir temp_dir; | 1178 base::ScopedTempDir temp_dir; |
1179 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1179 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
1180 | 1180 |
1181 // Create a directory before trying to fetch. | 1181 // Create a directory before trying to fetch. |
1182 static const char kFileToFetch[] = "simple.html"; | 1182 static const char kFileToFetch[] = "simple.html"; |
1183 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); | 1183 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); |
1184 ASSERT_TRUE(file_util::CreateDirectory(file_path_)); | 1184 ASSERT_TRUE(file_util::CreateDirectory(file_path_)); |
1185 ASSERT_TRUE(file_util::PathExists(file_path_)); | 1185 ASSERT_TRUE(file_util::PathExists(file_path_)); |
1186 | 1186 |
1187 // Get a small file. | 1187 // Get a small file. |
1188 expected_file_error_ = base::PLATFORM_FILE_ERROR_ACCESS_DENIED; | 1188 expected_file_error_ = base::PLATFORM_FILE_ERROR_ACCESS_DENIED; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1245 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1245 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1246 | 1246 |
1247 MessageLoop::current()->RunUntilIdle(); | 1247 MessageLoop::current()->RunUntilIdle(); |
1248 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1248 ASSERT_FALSE(file_util::PathExists(file_path_)) |
1249 << file_path_.value() << " not removed."; | 1249 << file_path_.value() << " not removed."; |
1250 } | 1250 } |
1251 | 1251 |
1252 } // namespace | 1252 } // namespace |
1253 | 1253 |
1254 } // namespace net | 1254 } // namespace net |
OLD | NEW |