Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(343)

Side by Side Diff: net/url_request/url_fetcher_impl_unittest.cc

Issue 1094553002: Revert "Speculative revert by sheriff" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/tools/player_x11/x11_video_renderer.cc ('k') | net/url_request/url_request_test_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 scoped_ptr<UploadElementReader>( 193 scoped_ptr<UploadElementReader>(
194 new UploadOwnedBytesElementReader(&buffer)), 194 new UploadOwnedBytesElementReader(&buffer)),
195 0); 195 0);
196 } 196 }
197 197
198 // Number of streams created by CreateUploadStream. 198 // Number of streams created by CreateUploadStream.
199 size_t num_upload_streams_created() const { 199 size_t num_upload_streams_created() const {
200 return num_upload_streams_created_; 200 return num_upload_streams_created_;
201 } 201 }
202 202
203 // Downloads |file_to_fetch| and checks the contents when done. If
204 // |save_to_temporary_file| is true, saves it to a temporary file, and
205 // |requested_out_path| is ignored. Otherwise, saves it to
206 // |requested_out_path|. Takes ownership of the file if |take_ownership| is
207 // true. Deletes file when done.
208 void SaveFileTest(const char* file_to_fetch,
209 bool save_to_temporary_file,
210 const base::FilePath& requested_out_path,
211 bool take_ownership) {
212 scoped_ptr<WaitingURLFetcherDelegate> delegate(
213 new WaitingURLFetcherDelegate());
214 delegate->CreateFetcherWithContext(
215 test_server_->GetURL(std::string(kTestServerFilePrefix) +
216 file_to_fetch),
217 URLFetcher::GET, request_context());
218 if (save_to_temporary_file) {
219 delegate->fetcher()->SaveResponseToTemporaryFile(
220 scoped_refptr<base::MessageLoopProxy>(
221 base::MessageLoopProxy::current()));
222 } else {
223 delegate->fetcher()->SaveResponseToFileAtPath(
224 requested_out_path, scoped_refptr<base::MessageLoopProxy>(
225 base::MessageLoopProxy::current()));
226 }
227 delegate->StartFetcherAndWait();
228
229 EXPECT_TRUE(delegate->fetcher()->GetStatus().is_success());
230 EXPECT_EQ(200, delegate->fetcher()->GetResponseCode());
231
232 base::FilePath out_path;
233 EXPECT_TRUE(
234 delegate->fetcher()->GetResponseAsFilePath(take_ownership, &out_path));
235 if (!save_to_temporary_file) {
236 EXPECT_EQ(requested_out_path, out_path);
237 }
238
239 EXPECT_TRUE(base::ContentsEqual(
240 test_server_->GetDocumentRoot().AppendASCII(file_to_fetch), out_path));
241
242 // Delete the delegate and run the message loop to give the fetcher's
243 // destructor a chance to delete the file.
244 delegate.reset();
245 base::RunLoop().RunUntilIdle();
246
247 // File should only exist if |take_ownership| was true.
248 EXPECT_EQ(take_ownership, base::PathExists(out_path));
249
250 // Cleanup.
251 if (base::PathExists(out_path))
252 base::DeleteFile(out_path, false);
253 }
254
203 // Returns a URL that hangs on DNS resolution. Only hangs when using the 255 // Returns a URL that hangs on DNS resolution. Only hangs when using the
204 // request context returned by request_context(). 256 // request context returned by request_context().
205 const GURL& hanging_url() const { return hanging_url_; } 257 const GURL& hanging_url() const { return hanging_url_; }
206 258
207 MockHostResolver* resolver() { return &resolver_; } 259 MockHostResolver* resolver() { return &resolver_; }
208 260
209 // testing::Test: 261 // testing::Test:
210 void SetUp() override { 262 void SetUp() override {
211 SetUpServer(); 263 SetUpServer();
212 ASSERT_TRUE(test_server_->Start()); 264 ASSERT_TRUE(test_server_->Start());
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 // Version of URLFetcherTest that tests retying the same request twice. 509 // Version of URLFetcherTest that tests retying the same request twice.
458 class URLFetcherMultipleAttemptTest : public URLFetcherTest { 510 class URLFetcherMultipleAttemptTest : public URLFetcherTest {
459 public: 511 public:
460 // URLFetcherDelegate: 512 // URLFetcherDelegate:
461 void OnURLFetchComplete(const URLFetcher* source) override; 513 void OnURLFetchComplete(const URLFetcher* source) override;
462 514
463 private: 515 private:
464 std::string data_; 516 std::string data_;
465 }; 517 };
466 518
467 class URLFetcherFileTest : public URLFetcherTest {
468 public:
469 URLFetcherFileTest() : take_ownership_of_file_(false),
470 expected_file_error_(OK) {}
471
472 void CreateFetcherForFile(const GURL& url, const base::FilePath& file_path);
473 void CreateFetcherForTempFile(const GURL& url);
474
475 // URLFetcherDelegate:
476 void OnURLFetchComplete(const URLFetcher* source) override;
477
478 protected:
479 base::FilePath expected_file_;
480 base::FilePath file_path_;
481
482 // Set by the test. Used in OnURLFetchComplete() to decide if
483 // the URLFetcher should own the temp file, so that we can test
484 // disowning prevents the file from being deleted.
485 bool take_ownership_of_file_;
486
487 // Expected file error code for the test. OK when expecting success.
488 int expected_file_error_;
489 };
490
491 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { 519 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) {
492 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); 520 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
493 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( 521 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
494 io_message_loop_proxy().get(), request_context())); 522 io_message_loop_proxy().get(), request_context()));
495 fetcher_->Start(); 523 fetcher_->Start();
496 } 524 }
497 525
498 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( 526 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress(
499 const URLFetcher* source, int64 progress, int64 total) { 527 const URLFetcher* source, int64 progress, int64 total) {
500 // Increasing between 0 and total. 528 // Increasing between 0 and total.
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
630 data_ = data; 658 data_ = data;
631 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( 659 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
632 io_message_loop_proxy().get(), request_context())); 660 io_message_loop_proxy().get(), request_context()));
633 fetcher_->Start(); 661 fetcher_->Start();
634 } else { 662 } else {
635 EXPECT_EQ(data, data_); 663 EXPECT_EQ(data, data_);
636 CleanupAfterFetchComplete(); 664 CleanupAfterFetchComplete();
637 } 665 }
638 } 666 }
639 667
640 void URLFetcherFileTest::CreateFetcherForFile(const GURL& url,
641 const base::FilePath& file_path) {
642 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
643 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
644 io_message_loop_proxy().get(), request_context()));
645
646 // Use the IO message loop to do the file operations in this test.
647 fetcher_->SaveResponseToFileAtPath(file_path, io_message_loop_proxy());
648 fetcher_->Start();
649 }
650
651 void URLFetcherFileTest::CreateFetcherForTempFile(const GURL& url) {
652 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
653 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
654 io_message_loop_proxy().get(), request_context()));
655
656 // Use the IO message loop to do the file operations in this test.
657 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy());
658 fetcher_->Start();
659 }
660
661 void URLFetcherFileTest::OnURLFetchComplete(const URLFetcher* source) {
662 if (expected_file_error_ == OK) {
663 EXPECT_TRUE(source->GetStatus().is_success());
664 EXPECT_EQ(OK, source->GetStatus().error());
665 EXPECT_EQ(200, source->GetResponseCode());
666
667 EXPECT_TRUE(source->GetResponseAsFilePath(
668 take_ownership_of_file_, &file_path_));
669
670 EXPECT_TRUE(base::ContentsEqual(expected_file_, file_path_));
671 } else {
672 EXPECT_FALSE(source->GetStatus().is_success());
673 EXPECT_EQ(expected_file_error_, source->GetStatus().error());
674 }
675 CleanupAfterFetchComplete();
676 }
677
678 // Create the fetcher on the main thread. Since network IO will happen on the 668 // Create the fetcher on the main thread. Since network IO will happen on the
679 // main thread, this will test URLFetcher's ability to do everything on one 669 // main thread, this will test URLFetcher's ability to do everything on one
680 // thread. 670 // thread.
681 TEST_F(URLFetcherTest, SameThreadTest) { 671 TEST_F(URLFetcherTest, SameThreadTest) {
682 WaitingURLFetcherDelegate delegate; 672 WaitingURLFetcherDelegate delegate;
683 delegate.CreateFetcherWithContext(test_server_->GetURL(kDefaultResponsePath), 673 delegate.CreateFetcherWithContext(test_server_->GetURL(kDefaultResponsePath),
684 URLFetcher::GET, request_context()); 674 URLFetcher::GET, request_context());
685 delegate.StartFetcherAndWait(); 675 delegate.StartFetcherAndWait();
686 676
687 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); 677 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success());
(...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after
1195 1185
1196 TEST_F(URLFetcherMultipleAttemptTest, SameData) { 1186 TEST_F(URLFetcherMultipleAttemptTest, SameData) {
1197 // Create the fetcher on the main thread. Since IO will happen on the main 1187 // Create the fetcher on the main thread. Since IO will happen on the main
1198 // thread, this will test URLFetcher's ability to do everything on one 1188 // thread, this will test URLFetcher's ability to do everything on one
1199 // thread. 1189 // thread.
1200 CreateFetcher(test_server_->GetURL(kDefaultResponsePath)); 1190 CreateFetcher(test_server_->GetURL(kDefaultResponsePath));
1201 1191
1202 base::MessageLoop::current()->Run(); 1192 base::MessageLoop::current()->Run();
1203 } 1193 }
1204 1194
1205 TEST_F(URLFetcherFileTest, SmallGet) { 1195 // Get a small file.
1196 TEST_F(URLFetcherTest, FileTestSmallGet) {
1197 const char kFileToFetch[] = "simple.html";
1198
1206 base::ScopedTempDir temp_dir; 1199 base::ScopedTempDir temp_dir;
1207 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 1200 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1208 1201 base::FilePath out_path = temp_dir.path().AppendASCII(kFileToFetch);
1209 // Get a small file. 1202 SaveFileTest(kFileToFetch, false, out_path, false);
1210 static const char kFileToFetch[] = "simple.html";
1211 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch);
1212 CreateFetcherForFile(
1213 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1214 temp_dir.path().AppendASCII(kFileToFetch));
1215
1216 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1217
1218 ASSERT_FALSE(base::PathExists(file_path_))
1219 << file_path_.value() << " not removed.";
1220 } 1203 }
1221 1204
1222 TEST_F(URLFetcherFileTest, LargeGet) { 1205 // Get a file large enough to require more than one read into URLFetcher::Core's
1206 // IOBuffer.
1207 TEST_F(URLFetcherTest, FileTestLargeGet) {
1208 const char kFileToFetch[] = "animate1.gif";
1209
1223 base::ScopedTempDir temp_dir; 1210 base::ScopedTempDir temp_dir;
1224 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 1211 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1225 1212 base::FilePath out_path = temp_dir.path().AppendASCII(kFileToFetch);
1226 // Get a file large enough to require more than one read into 1213 SaveFileTest(kFileToFetch, false, out_path, false);
1227 // URLFetcher::Core's IOBuffer.
1228 static const char kFileToFetch[] = "animate1.gif";
1229 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch);
1230 CreateFetcherForFile(
1231 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1232 temp_dir.path().AppendASCII(kFileToFetch));
1233
1234 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1235 } 1214 }
1236 1215
1237 TEST_F(URLFetcherFileTest, SavedOutputFileOwnerhisp) { 1216 // If the caller takes the ownership of the output file, the file should persist
1238 // If the caller takes the ownership of the output file, the file should 1217 // even after URLFetcher is gone.
1239 // persist even after URLFetcher is gone. If not, the file must be deleted. 1218 TEST_F(URLFetcherTest, FileTestTakeOwnership) {
1240 const bool kTake[] = {false, true}; 1219 const char kFileToFetch[] = "simple.html";
1241 for (size_t i = 0; i < arraysize(kTake); ++i) {
1242 take_ownership_of_file_ = kTake[i];
1243 base::ScopedTempDir temp_dir;
1244 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1245 1220
1246 // Get a small file. 1221 base::ScopedTempDir temp_dir;
1247 static const char kFileToFetch[] = "simple.html"; 1222 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1248 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch); 1223 base::FilePath out_path = temp_dir.path().AppendASCII(kFileToFetch);
1249 CreateFetcherForFile( 1224 SaveFileTest(kFileToFetch, false, out_path, true);
1250 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1251 temp_dir.path().AppendASCII(kFileToFetch));
1252
1253 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1254
1255 base::MessageLoop::current()->RunUntilIdle();
1256 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) <<
1257 "FilePath: " << file_path_.value();
1258 }
1259 } 1225 }
1260 1226
1261 TEST_F(URLFetcherFileTest, OverwriteExistingFile) { 1227 // Test that an existing file can be overwritten be a fetcher.
1228 TEST_F(URLFetcherTest, FileTestOverwriteExisting) {
1262 base::ScopedTempDir temp_dir; 1229 base::ScopedTempDir temp_dir;
1263 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 1230 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1264 1231
1265 // Create a file before trying to fetch. 1232 // Create a file before trying to fetch.
1266 static const char kFileToFetch[] = "simple.html"; 1233 const char kFileToFetch[] = "simple.html";
1267 std::string data(10000, '?'); // Meant to be larger than simple.html. 1234 std::string data(10000, '?'); // Meant to be larger than simple.html.
1268 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); 1235 base::FilePath out_path = temp_dir.path().AppendASCII(kFileToFetch);
1269 ASSERT_EQ(static_cast<int>(data.size()), 1236 ASSERT_EQ(static_cast<int>(data.size()),
1270 base::WriteFile(file_path_, data.data(), data.size())); 1237 base::WriteFile(out_path, data.data(), data.size()));
1271 ASSERT_TRUE(base::PathExists(file_path_)); 1238 ASSERT_TRUE(base::PathExists(out_path));
1272 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch);
1273 ASSERT_FALSE(base::ContentsEqual(file_path_, expected_file_));
1274 1239
1275 // Get a small file. 1240 SaveFileTest(kFileToFetch, false, out_path, true);
1276 CreateFetcherForFile(
1277 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1278 file_path_);
1279
1280 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1281 } 1241 }
1282 1242
1283 TEST_F(URLFetcherFileTest, TryToOverwriteDirectory) { 1243 // Test trying to overwrite a directory with a file when using a fetcher fails.
1244 TEST_F(URLFetcherTest, FileTestTryToOverwriteDirectory) {
1284 base::ScopedTempDir temp_dir; 1245 base::ScopedTempDir temp_dir;
1285 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 1246 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1286 1247
1287 // Create a directory before trying to fetch. 1248 // Create a directory before trying to fetch.
1288 static const char kFileToFetch[] = "simple.html"; 1249 static const char kFileToFetch[] = "simple.html";
1289 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); 1250 base::FilePath out_path = temp_dir.path().AppendASCII(kFileToFetch);
1290 ASSERT_TRUE(base::CreateDirectory(file_path_)); 1251 ASSERT_TRUE(base::CreateDirectory(out_path));
1291 ASSERT_TRUE(base::PathExists(file_path_)); 1252 ASSERT_TRUE(base::PathExists(out_path));
1292 1253
1293 // Get a small file. 1254 WaitingURLFetcherDelegate delegate;
1294 expected_file_error_ = ERR_ACCESS_DENIED; 1255 delegate.CreateFetcherWithContext(
1295 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch);
1296 CreateFetcherForFile(
1297 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), 1256 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1298 file_path_); 1257 URLFetcher::GET, request_context());
1258 delegate.fetcher()->SaveResponseToFileAtPath(
1259 out_path,
1260 scoped_refptr<base::MessageLoopProxy>(base::MessageLoopProxy::current()));
1261 delegate.StartFetcherAndWait();
1299 1262
1300 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). 1263 EXPECT_FALSE(delegate.fetcher()->GetStatus().is_success());
1301 1264 EXPECT_EQ(ERR_ACCESS_DENIED, delegate.fetcher()->GetStatus().error());
1302 base::MessageLoop::current()->RunUntilIdle();
1303 } 1265 }
1304 1266
1305 TEST_F(URLFetcherFileTest, SmallGetToTempFile) { 1267 // Get a small file and save it to a temp file.
1306 // Get a small file. 1268 TEST_F(URLFetcherTest, TempFileTestSmallGet) {
1307 static const char kFileToFetch[] = "simple.html"; 1269 SaveFileTest("simple.html", true, base::FilePath(), false);
1308 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch);
1309 CreateFetcherForTempFile(
1310 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch));
1311
1312 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1313
1314 ASSERT_FALSE(base::PathExists(file_path_))
1315 << file_path_.value() << " not removed.";
1316 } 1270 }
1317 1271
1318 TEST_F(URLFetcherFileTest, LargeGetToTempFile) { 1272 // Get a file large enough to require more than one read into URLFetcher::Core's
1319 // Get a file large enough to require more than one read into 1273 // IOBuffer and save it to a temp file.
1320 // URLFetcher::Core's IOBuffer. 1274 TEST_F(URLFetcherTest, TempFileTestLargeGet) {
1321 static const char kFileToFetch[] = "animate1.gif"; 1275 SaveFileTest("animate1.gif", true, base::FilePath(), false);
1322 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch);
1323 CreateFetcherForTempFile(
1324 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch));
1325
1326 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1327 } 1276 }
1328 1277
1329 TEST_F(URLFetcherFileTest, SavedOutputTempFileOwnerhisp) { 1278 // If the caller takes the ownership of the temp file, check that the file
1330 // If the caller takes the ownership of the temp file, the file should persist 1279 // persists even after URLFetcher is gone.
1331 // even after URLFetcher is gone. If not, the file must be deleted. 1280 TEST_F(URLFetcherTest, TempFileTestTakeOwnership) {
1332 const bool kTake[] = {false, true}; 1281 SaveFileTest("simple.html", true, base::FilePath(), true);
1333 for (size_t i = 0; i < arraysize(kTake); ++i) {
1334 take_ownership_of_file_ = kTake[i];
1335
1336 // Get a small file.
1337 static const char kFileToFetch[] = "simple.html";
1338 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch);
1339 CreateFetcherForTempFile(test_server_->GetURL(
1340 std::string(kTestServerFilePrefix) + kFileToFetch));
1341
1342 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1343
1344 base::MessageLoop::current()->RunUntilIdle();
1345 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) <<
1346 "FilePath: " << file_path_.value();
1347 }
1348 } 1282 }
1349 1283
1350 } // namespace 1284 } // namespace
1351 1285
1352 } // namespace net 1286 } // namespace net
OLDNEW
« no previous file with comments | « media/tools/player_x11/x11_video_renderer.cc ('k') | net/url_request/url_request_test_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698