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