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

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: Fix tests 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_->set_file_message_loop_proxy(
337 io_message_loop_proxy());
338
339 fetcher_->SaveResponseToTemporaryFile();
340 fetcher_->Start();
341 }
342
343 TEST_F(URLFetcherTempFileTest, SmallGet) {
344 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot));
345 ASSERT_TRUE(test_server.Start());
346
347 // Get a small file.
348 const char* kFileToFetch = "simple.html";
349 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch);
350 CreateFetcher(test_server.GetURL(kTestServerFilePrefix + kFileToFetch));
351
352 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
353
354 ASSERT_TRUE(file_util::PathExists(temp_file_))
355 << temp_file_.value() << " not found.";
356
357 // Deleting the fetcher should remove the file.
358 delete fetcher_;
359
360 MessageLoop::current()->RunAllPending();
361 ASSERT_FALSE(file_util::PathExists(temp_file_))
362 << temp_file_.value() << " not removed.";
363 }
364
365 TEST_F(URLFetcherTempFileTest, LargeGet) {
366 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot));
367 ASSERT_TRUE(test_server.Start());
368
369 // Get a file large enough to require more than one read into
370 // URLFetcher::Core's IOBuffer.
371 const char* kFileToFetch = "animate1.gif";
372 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch);
373 CreateFetcher(test_server.GetURL(kTestServerFilePrefix + kFileToFetch));
374
375 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
376 }
377
378 TEST_F(URLFetcherTempFileTest, CanTakeOwnershipOfFile) {
379 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot));
380 ASSERT_TRUE(test_server.Start());
381
382 // Get a small file.
383 const char* kFileToFetch = "simple.html";
384 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch);
385 CreateFetcher(test_server.GetURL(kTestServerFilePrefix + kFileToFetch));
386
387 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
388
389 ASSERT_TRUE(file_util::PathExists(temp_file_))
390 << temp_file_.value() << " not found.";
391
392 // Deleting the fetcher should remove the file.
393 delete fetcher_;
394
395 MessageLoop::current()->RunAllPending();
396 ASSERT_FALSE(file_util::PathExists(temp_file_))
397 << temp_file_.value() << " not removed.";
398 }
399
308 // Wrapper that lets us call CreateFetcher() on a thread of our choice. We 400 // 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 401 // could make URLFetcherTest refcounted and use PostTask(FROM_HERE.. ) to call
310 // CreateFetcher() directly, but the ownership of the URLFetcherTest is a bit 402 // CreateFetcher() directly, but the ownership of the URLFetcherTest is a bit
311 // confusing in that case because GTest doesn't know about the refcounting. 403 // confusing in that case because GTest doesn't know about the refcounting.
312 // It's less confusing to just do it this way. 404 // It's less confusing to just do it this way.
313 class FetcherWrapperTask : public Task { 405 class FetcherWrapperTask : public Task {
314 public: 406 public:
315 FetcherWrapperTask(URLFetcherTest* test, const GURL& url) 407 FetcherWrapperTask(URLFetcherTest* test, const GURL& url)
316 : test_(test), url_(url) { } 408 : test_(test), url_(url) { }
317 virtual void Run() { 409 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, 624 delete fetcher_; // Have to delete this here and not in the destructor,
533 // because the destructor won't necessarily run on the 625 // because the destructor won't necessarily run on the
534 // same thread that CreateFetcher() did. 626 // same thread that CreateFetcher() did.
535 627
536 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); 628 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 629 // 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. 630 // the main loop returns and this thread subsequently goes out of scope.
539 } 631 }
540 } 632 }
541 633
634 void URLFetcherTempFileTest::OnURLFetchComplete(const URLFetcher* source) {
635 EXPECT_TRUE(source->status().is_success());
636 EXPECT_EQ(source->response_code(), 200);
637
638 EXPECT_TRUE(source->GetResponseAsFilePath(
639 take_ownership_of_temp_file_, &temp_file_));
640
641 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_));
642
643 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
644 }
645
542 TEST_F(URLFetcherTest, SameThreadsTest) { 646 TEST_F(URLFetcherTest, SameThreadsTest) {
543 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); 647 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot));
544 ASSERT_TRUE(test_server.Start()); 648 ASSERT_TRUE(test_server.Start());
545 649
546 // Create the fetcher on the main thread. Since IO will happen on the main 650 // 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 651 // thread, this will test URLFetcher's ability to do everything on one
548 // thread. 652 // thread.
549 CreateFetcher(test_server.GetURL("defaultresponse")); 653 CreateFetcher(test_server.GetURL("defaultresponse"));
550 654
551 MessageLoop::current()->Run(); 655 MessageLoop::current()->Run();
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 TEST_F(URLFetcherCancelTest, DISABLED_ReleasesContext) { 802 TEST_F(URLFetcherCancelTest, DISABLED_ReleasesContext) {
699 #else 803 #else
700 TEST_F(URLFetcherCancelTest, ReleasesContext) { 804 TEST_F(URLFetcherCancelTest, ReleasesContext) {
701 #endif 805 #endif
702 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); 806 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot));
703 ASSERT_TRUE(test_server.Start()); 807 ASSERT_TRUE(test_server.Start());
704 808
705 GURL url(test_server.GetURL("files/server-unavailable.html")); 809 GURL url(test_server.GetURL("files/server-unavailable.html"));
706 810
707 // Registers an entry for test url. The backoff time is calculated by: 811 // Registers an entry for test url. The backoff time is calculated by:
708 // new_backoff = 2.0 * old_backoff + 0 812 // new_backoff = 2.0 * old_backoff +0
709 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. 813 // The initial backoff is 2 seconds and maximum backoff is 4 seconds.
710 // Maximum retries allowed is set to 2. 814 // Maximum retries allowed is set to 2.
711 net::URLRequestThrottlerManager* manager = 815 net::URLRequestThrottlerManager* manager =
712 net::URLRequestThrottlerManager::GetInstance(); 816 net::URLRequestThrottlerManager::GetInstance();
713 scoped_refptr<net::URLRequestThrottlerEntry> entry( 817 scoped_refptr<net::URLRequestThrottlerEntry> entry(
714 new net::URLRequestThrottlerEntry( 818 new net::URLRequestThrottlerEntry(
715 manager, 200, 3, 2000, 2.0, 0.0, 4000)); 819 manager, 200, 3, 2000, 2.0, 0.0, 4000));
716 manager->OverrideEntryForTests(url, entry); 820 manager->OverrideEntryForTests(url, entry);
717 821
718 // Create a separate thread that will create the URLFetcher. The current 822 // 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, 887 FROM_HERE,
784 new CurriedTask(new MessageLoop::QuitTask(), MessageLoop::current())); 888 new CurriedTask(new MessageLoop::QuitTask(), MessageLoop::current()));
785 MessageLoop::current()->Run(); 889 MessageLoop::current()->Run();
786 EXPECT_EQ(1, GetNumFetcherCores()); 890 EXPECT_EQ(1, GetNumFetcherCores());
787 URLFetcher::CancelAll(); 891 URLFetcher::CancelAll();
788 EXPECT_EQ(0, GetNumFetcherCores()); 892 EXPECT_EQ(0, GetNumFetcherCores());
789 delete fetcher_; 893 delete fetcher_;
790 } 894 }
791 895
792 } // namespace. 896 } // namespace.
OLDNEW
« chrome/common/net/url_fetcher.h ('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