Chromium Code Reviews| 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 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 374 public: | 374 public: |
| 375 // URLFetcherDelegate | 375 // URLFetcherDelegate |
| 376 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 376 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| 377 private: | 377 private: |
| 378 std::string data_; | 378 std::string data_; |
| 379 }; | 379 }; |
| 380 | 380 |
| 381 class URLFetcherFileTest : public URLFetcherTest { | 381 class URLFetcherFileTest : public URLFetcherTest { |
| 382 public: | 382 public: |
| 383 URLFetcherFileTest() : take_ownership_of_file_(false), | 383 URLFetcherFileTest() : take_ownership_of_file_(false), |
| 384 expected_file_error_(base::PLATFORM_FILE_OK) {} | 384 expected_file_error_(base::PLATFORM_FILE_OK) { |
| 385 #if defined(OS_ANDROID) | |
| 386 PathService::Get(base::DIR_SOURCE_ROOT, &document_root_); | |
| 387 document_root_= document_root_.AppendASCII(kDocRoot); | |
| 388 #endif | |
| 389 } | |
| 385 | 390 |
| 386 void CreateFetcherForFile(const GURL& url, const FilePath& file_path); | 391 void CreateFetcherForFile(const GURL& url, const FilePath& file_path); |
| 387 void CreateFetcherForTempFile(const GURL& url); | 392 void CreateFetcherForTempFile(const GURL& url); |
| 388 | 393 |
| 394 FilePath GetDocumentRoot(const TestServer& test_server) { | |
|
wtc
2012/10/18 22:25:01
My proposal is to remove the document_root member
| |
| 395 #if defined(OS_ANDROID) | |
| 396 return document_root_; | |
| 397 #else | |
| 398 return test_server.document_root(); | |
| 399 #endif | |
| 400 } | |
| 401 | |
| 389 // URLFetcherDelegate | 402 // URLFetcherDelegate |
| 390 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 403 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| 391 | 404 |
| 392 protected: | 405 protected: |
| 406 #if defined(OS_ANDROID) | |
| 407 FilePath document_root_; | |
| 408 #endif | |
| 393 FilePath expected_file_; | 409 FilePath expected_file_; |
| 394 FilePath file_path_; | 410 FilePath file_path_; |
| 395 | 411 |
| 396 // Set by the test. Used in OnURLFetchComplete() to decide if | 412 // Set by the test. Used in OnURLFetchComplete() to decide if |
| 397 // the URLFetcher should own the temp file, so that we can test | 413 // the URLFetcher should own the temp file, so that we can test |
| 398 // disowning prevents the file from being deleted. | 414 // disowning prevents the file from being deleted. |
| 399 bool take_ownership_of_file_; | 415 bool take_ownership_of_file_; |
| 400 | 416 |
| 401 // Expected file error code for the test. | 417 // Expected file error code for the test. |
| 402 // PLATFORM_FILE_OK when expecting success. | 418 // PLATFORM_FILE_OK when expecting success. |
| (...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1080 TestServer test_server(TestServer::TYPE_HTTP, | 1096 TestServer test_server(TestServer::TYPE_HTTP, |
| 1081 TestServer::kLocalhost, | 1097 TestServer::kLocalhost, |
| 1082 FilePath(kDocRoot)); | 1098 FilePath(kDocRoot)); |
| 1083 ASSERT_TRUE(test_server.Start()); | 1099 ASSERT_TRUE(test_server.Start()); |
| 1084 | 1100 |
| 1085 ScopedTempDir temp_dir; | 1101 ScopedTempDir temp_dir; |
| 1086 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1102 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1087 | 1103 |
| 1088 // Get a small file. | 1104 // Get a small file. |
| 1089 static const char kFileToFetch[] = "simple.html"; | 1105 static const char kFileToFetch[] = "simple.html"; |
| 1090 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1106 expected_file_ = GetDocumentRoot(test_server).AppendASCII(kFileToFetch); |
| 1091 CreateFetcherForFile( | 1107 CreateFetcherForFile( |
| 1092 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1108 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 1093 temp_dir.path().AppendASCII(kFileToFetch)); | 1109 temp_dir.path().AppendASCII(kFileToFetch)); |
| 1094 | 1110 |
| 1095 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1111 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1096 | 1112 |
| 1097 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1113 ASSERT_FALSE(file_util::PathExists(file_path_)) |
| 1098 << file_path_.value() << " not removed."; | 1114 << file_path_.value() << " not removed."; |
| 1099 } | 1115 } |
| 1100 | 1116 |
| 1101 TEST_F(URLFetcherFileTest, LargeGet) { | 1117 TEST_F(URLFetcherFileTest, LargeGet) { |
| 1102 TestServer test_server(TestServer::TYPE_HTTP, | 1118 TestServer test_server(TestServer::TYPE_HTTP, |
| 1103 TestServer::kLocalhost, | 1119 TestServer::kLocalhost, |
| 1104 FilePath(kDocRoot)); | 1120 FilePath(kDocRoot)); |
| 1105 ASSERT_TRUE(test_server.Start()); | 1121 ASSERT_TRUE(test_server.Start()); |
| 1106 | 1122 |
| 1107 ScopedTempDir temp_dir; | 1123 ScopedTempDir temp_dir; |
| 1108 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1124 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1109 | 1125 |
| 1110 // Get a file large enough to require more than one read into | 1126 // Get a file large enough to require more than one read into |
| 1111 // URLFetcher::Core's IOBuffer. | 1127 // URLFetcher::Core's IOBuffer. |
| 1112 static const char kFileToFetch[] = "animate1.gif"; | 1128 static const char kFileToFetch[] = "animate1.gif"; |
| 1113 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1129 expected_file_ = GetDocumentRoot(test_server).AppendASCII(kFileToFetch); |
| 1114 CreateFetcherForFile( | 1130 CreateFetcherForFile( |
| 1115 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1131 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 1116 temp_dir.path().AppendASCII(kFileToFetch)); | 1132 temp_dir.path().AppendASCII(kFileToFetch)); |
| 1117 | 1133 |
| 1118 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1134 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1119 } | 1135 } |
| 1120 | 1136 |
| 1121 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfFile) { | 1137 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfFile) { |
| 1122 TestServer test_server(TestServer::TYPE_HTTP, | 1138 TestServer test_server(TestServer::TYPE_HTTP, |
| 1123 TestServer::kLocalhost, | 1139 TestServer::kLocalhost, |
| 1124 FilePath(kDocRoot)); | 1140 FilePath(kDocRoot)); |
| 1125 ASSERT_TRUE(test_server.Start()); | 1141 ASSERT_TRUE(test_server.Start()); |
| 1126 | 1142 |
| 1127 ScopedTempDir temp_dir; | 1143 ScopedTempDir temp_dir; |
| 1128 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1144 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1129 | 1145 |
| 1130 // Get a small file. | 1146 // Get a small file. |
| 1131 static const char kFileToFetch[] = "simple.html"; | 1147 static const char kFileToFetch[] = "simple.html"; |
| 1132 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1148 expected_file_ = GetDocumentRoot(test_server).AppendASCII(kFileToFetch); |
| 1133 CreateFetcherForFile( | 1149 CreateFetcherForFile( |
| 1134 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1150 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 1135 temp_dir.path().AppendASCII(kFileToFetch)); | 1151 temp_dir.path().AppendASCII(kFileToFetch)); |
| 1136 | 1152 |
| 1137 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1153 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1138 | 1154 |
| 1139 MessageLoop::current()->RunAllPending(); | 1155 MessageLoop::current()->RunAllPending(); |
| 1140 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1156 ASSERT_FALSE(file_util::PathExists(file_path_)) |
| 1141 << file_path_.value() << " not removed."; | 1157 << file_path_.value() << " not removed."; |
| 1142 } | 1158 } |
| 1143 | 1159 |
| 1144 | 1160 |
| 1145 TEST_F(URLFetcherFileTest, OverwriteExistingFile) { | 1161 TEST_F(URLFetcherFileTest, OverwriteExistingFile) { |
| 1146 TestServer test_server(TestServer::TYPE_HTTP, | 1162 TestServer test_server(TestServer::TYPE_HTTP, |
| 1147 TestServer::kLocalhost, | 1163 TestServer::kLocalhost, |
| 1148 FilePath(kDocRoot)); | 1164 FilePath(kDocRoot)); |
| 1149 ASSERT_TRUE(test_server.Start()); | 1165 ASSERT_TRUE(test_server.Start()); |
| 1150 | 1166 |
| 1151 ScopedTempDir temp_dir; | 1167 ScopedTempDir temp_dir; |
| 1152 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1168 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1153 | 1169 |
| 1154 // Create a file before trying to fetch. | 1170 // Create a file before trying to fetch. |
| 1155 static const char kFileToFetch[] = "simple.html"; | 1171 static const char kFileToFetch[] = "simple.html"; |
| 1156 static const char kData[] = "abcdefghijklmnopqrstuvwxyz"; | 1172 static const char kData[] = "abcdefghijklmnopqrstuvwxyz"; |
| 1157 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); | 1173 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); |
| 1158 const int data_size = arraysize(kData); | 1174 const int data_size = arraysize(kData); |
| 1159 ASSERT_EQ(file_util::WriteFile(file_path_, kData, data_size), data_size); | 1175 ASSERT_EQ(file_util::WriteFile(file_path_, kData, data_size), data_size); |
| 1160 ASSERT_TRUE(file_util::PathExists(file_path_)); | 1176 ASSERT_TRUE(file_util::PathExists(file_path_)); |
| 1161 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1177 expected_file_ = GetDocumentRoot(test_server).AppendASCII(kFileToFetch); |
| 1162 ASSERT_FALSE(file_util::ContentsEqual(file_path_, expected_file_)); | 1178 ASSERT_FALSE(file_util::ContentsEqual(file_path_, expected_file_)); |
| 1163 | 1179 |
| 1164 // Get a small file. | 1180 // Get a small file. |
| 1165 CreateFetcherForFile( | 1181 CreateFetcherForFile( |
| 1166 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1182 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 1167 file_path_); | 1183 file_path_); |
| 1168 | 1184 |
| 1169 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1185 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1170 } | 1186 } |
| 1171 | 1187 |
| 1172 TEST_F(URLFetcherFileTest, TryToOverwriteDirectory) { | 1188 TEST_F(URLFetcherFileTest, TryToOverwriteDirectory) { |
| 1173 TestServer test_server(TestServer::TYPE_HTTP, | 1189 TestServer test_server(TestServer::TYPE_HTTP, |
| 1174 TestServer::kLocalhost, | 1190 TestServer::kLocalhost, |
| 1175 FilePath(kDocRoot)); | 1191 FilePath(kDocRoot)); |
| 1176 ASSERT_TRUE(test_server.Start()); | 1192 ASSERT_TRUE(test_server.Start()); |
| 1177 | 1193 |
| 1178 ScopedTempDir temp_dir; | 1194 ScopedTempDir temp_dir; |
| 1179 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1195 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1180 | 1196 |
| 1181 // Create a directory before trying to fetch. | 1197 // Create a directory before trying to fetch. |
| 1182 static const char kFileToFetch[] = "simple.html"; | 1198 static const char kFileToFetch[] = "simple.html"; |
| 1183 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); | 1199 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); |
| 1184 ASSERT_TRUE(file_util::CreateDirectory(file_path_)); | 1200 ASSERT_TRUE(file_util::CreateDirectory(file_path_)); |
| 1185 ASSERT_TRUE(file_util::PathExists(file_path_)); | 1201 ASSERT_TRUE(file_util::PathExists(file_path_)); |
| 1186 | 1202 |
| 1187 // Get a small file. | 1203 // Get a small file. |
| 1188 expected_file_error_ = base::PLATFORM_FILE_ERROR_ACCESS_DENIED; | 1204 expected_file_error_ = base::PLATFORM_FILE_ERROR_ACCESS_DENIED; |
| 1189 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1205 expected_file_ = GetDocumentRoot(test_server).AppendASCII(kFileToFetch); |
| 1190 CreateFetcherForFile( | 1206 CreateFetcherForFile( |
| 1191 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1207 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 1192 file_path_); | 1208 file_path_); |
| 1193 | 1209 |
| 1194 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1210 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1195 | 1211 |
| 1196 MessageLoop::current()->RunAllPending(); | 1212 MessageLoop::current()->RunAllPending(); |
| 1197 } | 1213 } |
| 1198 | 1214 |
| 1199 TEST_F(URLFetcherFileTest, SmallGetToTempFile) { | 1215 TEST_F(URLFetcherFileTest, SmallGetToTempFile) { |
| 1200 TestServer test_server(TestServer::TYPE_HTTP, | 1216 TestServer test_server(TestServer::TYPE_HTTP, |
| 1201 TestServer::kLocalhost, | 1217 TestServer::kLocalhost, |
| 1202 FilePath(kDocRoot)); | 1218 FilePath(kDocRoot)); |
| 1203 ASSERT_TRUE(test_server.Start()); | 1219 ASSERT_TRUE(test_server.Start()); |
| 1204 | 1220 |
| 1205 // Get a small file. | 1221 // Get a small file. |
| 1206 static const char kFileToFetch[] = "simple.html"; | 1222 static const char kFileToFetch[] = "simple.html"; |
| 1207 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1223 expected_file_ = GetDocumentRoot(test_server).AppendASCII(kFileToFetch); |
| 1208 CreateFetcherForTempFile( | 1224 CreateFetcherForTempFile( |
| 1209 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); | 1225 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 1210 | 1226 |
| 1211 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1227 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1212 | 1228 |
| 1213 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1229 ASSERT_FALSE(file_util::PathExists(file_path_)) |
| 1214 << file_path_.value() << " not removed."; | 1230 << file_path_.value() << " not removed."; |
| 1215 } | 1231 } |
| 1216 | 1232 |
| 1217 TEST_F(URLFetcherFileTest, LargeGetToTempFile) { | 1233 TEST_F(URLFetcherFileTest, LargeGetToTempFile) { |
| 1218 TestServer test_server(TestServer::TYPE_HTTP, | 1234 TestServer test_server(TestServer::TYPE_HTTP, |
| 1219 TestServer::kLocalhost, | 1235 TestServer::kLocalhost, |
| 1220 FilePath(kDocRoot)); | 1236 FilePath(kDocRoot)); |
| 1221 ASSERT_TRUE(test_server.Start()); | 1237 ASSERT_TRUE(test_server.Start()); |
| 1222 | 1238 |
| 1223 // Get a file large enough to require more than one read into | 1239 // Get a file large enough to require more than one read into |
| 1224 // URLFetcher::Core's IOBuffer. | 1240 // URLFetcher::Core's IOBuffer. |
| 1225 static const char kFileToFetch[] = "animate1.gif"; | 1241 static const char kFileToFetch[] = "animate1.gif"; |
| 1226 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1242 expected_file_ = GetDocumentRoot(test_server).AppendASCII(kFileToFetch); |
| 1227 CreateFetcherForTempFile(test_server.GetURL( | 1243 CreateFetcherForTempFile(test_server.GetURL( |
| 1228 std::string(kTestServerFilePrefix) + kFileToFetch)); | 1244 std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 1229 | 1245 |
| 1230 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1246 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1231 } | 1247 } |
| 1232 | 1248 |
| 1233 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfTempFile) { | 1249 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfTempFile) { |
| 1234 TestServer test_server(TestServer::TYPE_HTTP, | 1250 TestServer test_server(TestServer::TYPE_HTTP, |
| 1235 TestServer::kLocalhost, | 1251 TestServer::kLocalhost, |
| 1236 FilePath(kDocRoot)); | 1252 FilePath(kDocRoot)); |
| 1237 ASSERT_TRUE(test_server.Start()); | 1253 ASSERT_TRUE(test_server.Start()); |
| 1238 | 1254 |
| 1239 // Get a small file. | 1255 // Get a small file. |
| 1240 static const char kFileToFetch[] = "simple.html"; | 1256 static const char kFileToFetch[] = "simple.html"; |
| 1241 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1257 expected_file_ = GetDocumentRoot(test_server).AppendASCII(kFileToFetch); |
| 1242 CreateFetcherForTempFile(test_server.GetURL( | 1258 CreateFetcherForTempFile(test_server.GetURL( |
| 1243 std::string(kTestServerFilePrefix) + kFileToFetch)); | 1259 std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 1244 | 1260 |
| 1245 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1261 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1246 | 1262 |
| 1247 MessageLoop::current()->RunAllPending(); | 1263 MessageLoop::current()->RunAllPending(); |
| 1248 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1264 ASSERT_FALSE(file_util::PathExists(file_path_)) |
| 1249 << file_path_.value() << " not removed."; | 1265 << file_path_.value() << " not removed."; |
| 1250 } | 1266 } |
| 1251 | 1267 |
| 1252 } // namespace | 1268 } // namespace |
| 1253 | 1269 |
| 1254 } // namespace net | 1270 } // namespace net |
| OLD | NEW |