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

Side by Side Diff: chrome/common/net/url_fetcher_unittest.cc

Issue 6969067: Allow URLFetcher to save results in a file. Have CRX updates use this capability. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase for commit Created 9 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/message_loop_proxy.h" 5 #include "base/message_loop_proxy.h"
6 #include "base/synchronization/waitable_event.h" 6 #include "base/synchronization/waitable_event.h"
7 #include "base/threading/thread.h" 7 #include "base/threading/thread.h"
8 #include "build/build_config.h" 8 #include "build/build_config.h"
9 #include "chrome/common/net/url_fetcher.h" 9 #include "chrome/common/net/url_fetcher.h"
10 #include "net/http/http_response_headers.h" 10 #include "net/http/http_response_headers.h"
11 #include "net/test/test_server.h" 11 #include "net/test/test_server.h"
12 #include "net/url_request/url_request_context_getter.h" 12 #include "net/url_request/url_request_context_getter.h"
13 #include "net/url_request/url_request_test_util.h" 13 #include "net/url_request/url_request_test_util.h"
14 #include "net/url_request/url_request_throttler_manager.h" 14 #include "net/url_request/url_request_throttler_manager.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 16
17 #if defined(USE_NSS) 17 #if defined(USE_NSS)
18 #include "net/ocsp/nss_ocsp.h" 18 #include "net/ocsp/nss_ocsp.h"
19 #endif 19 #endif
20 20
21 using base::Time; 21 using base::Time;
22 using base::TimeDelta; 22 using base::TimeDelta;
23 23
24 // TODO(eroman): Add a regression test for http://crbug.com/40505. 24 // TODO(eroman): Add a regression test for http://crbug.com/40505.
25 25
26 namespace { 26 namespace {
27 27
28 const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data"); 28 const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data");
29 const std::string kTestServerFilePrefix = "files/";
29 30
30 class CurriedTask : public Task { 31 class CurriedTask : public Task {
31 public: 32 public:
32 CurriedTask(Task* task, MessageLoop* target_loop) 33 CurriedTask(Task* task, MessageLoop* target_loop)
33 : task_(task), 34 : task_(task),
34 target_loop_(target_loop) {} 35 target_loop_(target_loop) {}
35 36
36 virtual void Run() { 37 virtual void Run() {
37 target_loop_->PostTask(FROM_HERE, task_); 38 target_loop_->PostTask(FROM_HERE, task_);
38 } 39 }
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 virtual void OnURLFetchComplete(const URLFetcher* source, 299 virtual void OnURLFetchComplete(const URLFetcher* source,
299 const GURL& url, 300 const GURL& url,
300 const net::URLRequestStatus& status, 301 const net::URLRequestStatus& status,
301 int response_code, 302 int response_code,
302 const net::ResponseCookies& cookies, 303 const net::ResponseCookies& cookies,
303 const std::string& data); 304 const std::string& data);
304 private: 305 private:
305 std::string data_; 306 std::string data_;
306 }; 307 };
307 308
309 class URLFetcherTempFileTest : public URLFetcherTest {
310 public:
311 URLFetcherTempFileTest()
312 : take_ownership_of_temp_file_(false) {
313 }
314
315 // URLFetcher::Delegate
316 virtual void OnURLFetchComplete(const URLFetcher* source);
317
318 virtual void CreateFetcher(const GURL& url);
319
320 protected:
321 FilePath expected_file_;
322 FilePath temp_file_;
323
324 // Set by the test. Used in OnURLFetchComplete() to decide if
325 // the URLFetcher should own the temp file, so that we can test
326 // disowning prevents the file from being deleted.
327 bool take_ownership_of_temp_file_;
328 };
329
330 void URLFetcherTempFileTest::CreateFetcher(const GURL& url) {
331 fetcher_ = new URLFetcher(url, URLFetcher::GET, this);
332 fetcher_->set_request_context(new TestURLRequestContextGetter(
333 io_message_loop_proxy()));
334
335 // Use the IO message loop to do the file operations in this test.
336 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy());
337 fetcher_->Start();
338 }
339
340 TEST_F(URLFetcherTempFileTest, SmallGet) {
341 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot));
342 ASSERT_TRUE(test_server.Start());
343
344 // Get a small file.
345 const char* kFileToFetch = "simple.html";
346 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch);
347 CreateFetcher(test_server.GetURL(kTestServerFilePrefix + kFileToFetch));
348
349 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
350
351 ASSERT_TRUE(file_util::PathExists(temp_file_))
352 << temp_file_.value() << " not found.";
353
354 // Deleting the fetcher should remove the file.
355 delete fetcher_;
356
357 MessageLoop::current()->RunAllPending();
358 ASSERT_FALSE(file_util::PathExists(temp_file_))
359 << temp_file_.value() << " not removed.";
360 }
361
362 TEST_F(URLFetcherTempFileTest, LargeGet) {
363 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot));
364 ASSERT_TRUE(test_server.Start());
365
366 // Get a file large enough to require more than one read into
367 // URLFetcher::Core's IOBuffer.
368 const char* kFileToFetch = "animate1.gif";
369 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch);
370 CreateFetcher(test_server.GetURL(kTestServerFilePrefix + kFileToFetch));
371
372 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
373 }
374
375 TEST_F(URLFetcherTempFileTest, CanTakeOwnershipOfFile) {
376 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot));
377 ASSERT_TRUE(test_server.Start());
378
379 // Get a small file.
380 const char* kFileToFetch = "simple.html";
381 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch);
382 CreateFetcher(test_server.GetURL(kTestServerFilePrefix + kFileToFetch));
383
384 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
385
386 ASSERT_TRUE(file_util::PathExists(temp_file_))
387 << temp_file_.value() << " not found.";
388
389 // Deleting the fetcher should remove the file.
390 delete fetcher_;
391
392 MessageLoop::current()->RunAllPending();
393 ASSERT_FALSE(file_util::PathExists(temp_file_))
394 << temp_file_.value() << " not removed.";
395 }
396
308 // Wrapper that lets us call CreateFetcher() on a thread of our choice. We 397 // Wrapper that lets us call CreateFetcher() on a thread of our choice. We
309 // could make URLFetcherTest refcounted and use PostTask(FROM_HERE.. ) to call 398 // could make URLFetcherTest refcounted and use PostTask(FROM_HERE.. ) to call
310 // CreateFetcher() directly, but the ownership of the URLFetcherTest is a bit 399 // CreateFetcher() directly, but the ownership of the URLFetcherTest is a bit
311 // confusing in that case because GTest doesn't know about the refcounting. 400 // confusing in that case because GTest doesn't know about the refcounting.
312 // It's less confusing to just do it this way. 401 // It's less confusing to just do it this way.
313 class FetcherWrapperTask : public Task { 402 class FetcherWrapperTask : public Task {
314 public: 403 public:
315 FetcherWrapperTask(URLFetcherTest* test, const GURL& url) 404 FetcherWrapperTask(URLFetcherTest* test, const GURL& url)
316 : test_(test), url_(url) { } 405 : test_(test), url_(url) { }
317 virtual void Run() { 406 virtual void Run() {
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 delete fetcher_; // Have to delete this here and not in the destructor, 621 delete fetcher_; // Have to delete this here and not in the destructor,
533 // because the destructor won't necessarily run on the 622 // because the destructor won't necessarily run on the
534 // same thread that CreateFetcher() did. 623 // same thread that CreateFetcher() did.
535 624
536 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); 625 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
537 // If the current message loop is not the IO loop, it will be shut down when 626 // If the current message loop is not the IO loop, it will be shut down when
538 // the main loop returns and this thread subsequently goes out of scope. 627 // the main loop returns and this thread subsequently goes out of scope.
539 } 628 }
540 } 629 }
541 630
631 void URLFetcherTempFileTest::OnURLFetchComplete(const URLFetcher* source) {
632 EXPECT_TRUE(source->status().is_success());
633 EXPECT_EQ(source->response_code(), 200);
634
635 EXPECT_TRUE(source->GetResponseAsFilePath(
636 take_ownership_of_temp_file_, &temp_file_));
637
638 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_));
639
640 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
641 }
642
542 TEST_F(URLFetcherTest, SameThreadsTest) { 643 TEST_F(URLFetcherTest, SameThreadsTest) {
543 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); 644 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot));
544 ASSERT_TRUE(test_server.Start()); 645 ASSERT_TRUE(test_server.Start());
545 646
546 // Create the fetcher on the main thread. Since IO will happen on the main 647 // Create the fetcher on the main thread. Since IO will happen on the main
547 // thread, this will test URLFetcher's ability to do everything on one 648 // thread, this will test URLFetcher's ability to do everything on one
548 // thread. 649 // thread.
549 CreateFetcher(test_server.GetURL("defaultresponse")); 650 CreateFetcher(test_server.GetURL("defaultresponse"));
550 651
551 MessageLoop::current()->Run(); 652 MessageLoop::current()->Run();
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 TEST_F(URLFetcherCancelTest, DISABLED_ReleasesContext) { 799 TEST_F(URLFetcherCancelTest, DISABLED_ReleasesContext) {
699 #else 800 #else
700 TEST_F(URLFetcherCancelTest, ReleasesContext) { 801 TEST_F(URLFetcherCancelTest, ReleasesContext) {
701 #endif 802 #endif
702 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); 803 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot));
703 ASSERT_TRUE(test_server.Start()); 804 ASSERT_TRUE(test_server.Start());
704 805
705 GURL url(test_server.GetURL("files/server-unavailable.html")); 806 GURL url(test_server.GetURL("files/server-unavailable.html"));
706 807
707 // Registers an entry for test url. The backoff time is calculated by: 808 // Registers an entry for test url. The backoff time is calculated by:
708 // new_backoff = 2.0 * old_backoff + 0 809 // new_backoff = 2.0 * old_backoff +0
709 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. 810 // The initial backoff is 2 seconds and maximum backoff is 4 seconds.
710 // Maximum retries allowed is set to 2. 811 // Maximum retries allowed is set to 2.
711 net::URLRequestThrottlerManager* manager = 812 net::URLRequestThrottlerManager* manager =
712 net::URLRequestThrottlerManager::GetInstance(); 813 net::URLRequestThrottlerManager::GetInstance();
713 scoped_refptr<net::URLRequestThrottlerEntry> entry( 814 scoped_refptr<net::URLRequestThrottlerEntry> entry(
714 new net::URLRequestThrottlerEntry( 815 new net::URLRequestThrottlerEntry(
715 manager, 200, 3, 2000, 2.0, 0.0, 4000)); 816 manager, 200, 3, 2000, 2.0, 0.0, 4000));
716 manager->OverrideEntryForTests(url, entry); 817 manager->OverrideEntryForTests(url, entry);
717 818
718 // Create a separate thread that will create the URLFetcher. The current 819 // Create a separate thread that will create the URLFetcher. The current
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 FROM_HERE, 884 FROM_HERE,
784 new CurriedTask(new MessageLoop::QuitTask(), MessageLoop::current())); 885 new CurriedTask(new MessageLoop::QuitTask(), MessageLoop::current()));
785 MessageLoop::current()->Run(); 886 MessageLoop::current()->Run();
786 EXPECT_EQ(1, GetNumFetcherCores()); 887 EXPECT_EQ(1, GetNumFetcherCores());
787 URLFetcher::CancelAll(); 888 URLFetcher::CancelAll();
788 EXPECT_EQ(0, GetNumFetcherCores()); 889 EXPECT_EQ(0, GetNumFetcherCores());
789 delete fetcher_; 890 delete fetcher_;
790 } 891 }
791 892
792 } // namespace. 893 } // namespace.
OLDNEW
« chrome/common/net/url_fetcher.cc ('K') | « chrome/common/net/url_fetcher.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698