| 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" |
| (...skipping 1069 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 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.document_root().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 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.document_root().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 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.document_root().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()->RunAllPending(); | 1139 MessageLoop::current()->RunAllPending(); |
| 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 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.document_root().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 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; |
| 1189 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1189 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); |
| 1190 CreateFetcherForFile( | 1190 CreateFetcherForFile( |
| 1191 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1191 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 1192 file_path_); | 1192 file_path_); |
| 1193 | 1193 |
| 1194 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1194 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1195 | 1195 |
| 1196 MessageLoop::current()->RunAllPending(); | 1196 MessageLoop::current()->RunAllPending(); |
| 1197 } | 1197 } |
| 1198 | 1198 |
| 1199 TEST_F(URLFetcherFileTest, SmallGetToTempFile) { | 1199 TEST_F(URLFetcherFileTest, SmallGetToTempFile) { |
| 1200 TestServer test_server(TestServer::TYPE_HTTP, | 1200 TestServer test_server(TestServer::TYPE_HTTP, |
| 1201 TestServer::kLocalhost, | 1201 TestServer::kLocalhost, |
| 1202 FilePath(kDocRoot)); | 1202 FilePath(kDocRoot)); |
| 1203 ASSERT_TRUE(test_server.Start()); | 1203 ASSERT_TRUE(test_server.Start()); |
| 1204 | 1204 |
| 1205 // Get a small file. | 1205 // Get a small file. |
| 1206 static const char kFileToFetch[] = "simple.html"; | 1206 static const char kFileToFetch[] = "simple.html"; |
| 1207 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1207 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); |
| 1208 CreateFetcherForTempFile( | 1208 CreateFetcherForTempFile( |
| 1209 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); | 1209 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 1210 | 1210 |
| 1211 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1211 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1212 | 1212 |
| 1213 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1213 ASSERT_FALSE(file_util::PathExists(file_path_)) |
| 1214 << file_path_.value() << " not removed."; | 1214 << file_path_.value() << " not removed."; |
| 1215 } | 1215 } |
| 1216 | 1216 |
| 1217 TEST_F(URLFetcherFileTest, LargeGetToTempFile) { | 1217 TEST_F(URLFetcherFileTest, LargeGetToTempFile) { |
| 1218 TestServer test_server(TestServer::TYPE_HTTP, | 1218 TestServer test_server(TestServer::TYPE_HTTP, |
| 1219 TestServer::kLocalhost, | 1219 TestServer::kLocalhost, |
| 1220 FilePath(kDocRoot)); | 1220 FilePath(kDocRoot)); |
| 1221 ASSERT_TRUE(test_server.Start()); | 1221 ASSERT_TRUE(test_server.Start()); |
| 1222 | 1222 |
| 1223 // Get a file large enough to require more than one read into | 1223 // Get a file large enough to require more than one read into |
| 1224 // URLFetcher::Core's IOBuffer. | 1224 // URLFetcher::Core's IOBuffer. |
| 1225 static const char kFileToFetch[] = "animate1.gif"; | 1225 static const char kFileToFetch[] = "animate1.gif"; |
| 1226 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1226 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); |
| 1227 CreateFetcherForTempFile(test_server.GetURL( | 1227 CreateFetcherForTempFile(test_server.GetURL( |
| 1228 std::string(kTestServerFilePrefix) + kFileToFetch)); | 1228 std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 1229 | 1229 |
| 1230 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1230 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1231 } | 1231 } |
| 1232 | 1232 |
| 1233 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfTempFile) { | 1233 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfTempFile) { |
| 1234 TestServer test_server(TestServer::TYPE_HTTP, | 1234 TestServer test_server(TestServer::TYPE_HTTP, |
| 1235 TestServer::kLocalhost, | 1235 TestServer::kLocalhost, |
| 1236 FilePath(kDocRoot)); | 1236 FilePath(kDocRoot)); |
| 1237 ASSERT_TRUE(test_server.Start()); | 1237 ASSERT_TRUE(test_server.Start()); |
| 1238 | 1238 |
| 1239 // Get a small file. | 1239 // Get a small file. |
| 1240 static const char kFileToFetch[] = "simple.html"; | 1240 static const char kFileToFetch[] = "simple.html"; |
| 1241 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1241 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); |
| 1242 CreateFetcherForTempFile(test_server.GetURL( | 1242 CreateFetcherForTempFile(test_server.GetURL( |
| 1243 std::string(kTestServerFilePrefix) + kFileToFetch)); | 1243 std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 1244 | 1244 |
| 1245 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1245 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1246 | 1246 |
| 1247 MessageLoop::current()->RunAllPending(); | 1247 MessageLoop::current()->RunAllPending(); |
| 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 |