1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "components/domain_reliability/uploader.h" 6 7 #include <map> 8 9 #include "base/bind.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "base/message_loop/message_loop_proxy.h" 12 #include "base/test/test_simple_task_runner.h" 13 #include "components/domain_reliability/test_util.h" 14 #include "net/base/load_flags.h" 15 #include "net/url_request/test_url_fetcher_factory.h" 16 #include "net/url_request/url_fetcher_delegate.h" 17 #include "net/url_request/url_request_context_getter.h" 18 #include "net/url_request/url_request_test_util.h" 19 #include "testing/gtest/include/gtest/gtest.h" 20 21 namespace domain_reliability { 22 namespace { 23 24 class DomainReliabilityUploaderTest : public testing::Test { 25 protected: 26 DomainReliabilityUploaderTest() 27 : network_task_runner_(new base::TestSimpleTaskRunner()), 28 url_request_context_getter_(new net::TestURLRequestContextGetter( 29 network_task_runner_)), 30 uploader_(DomainReliabilityUploader::Create( 31 url_request_context_getter_)) { 32 uploader_->set_discard_uploads(false); 33 } 34 35 DomainReliabilityUploader::UploadCallback MakeUploadCallback(size_t index) { 36 return base::Bind(&DomainReliabilityUploaderTest::OnUploadComplete, 37 base::Unretained(this), 38 index); 39 } 40 41 void OnUploadComplete(size_t index, bool success) { 42 EXPECT_FALSE(upload_complete_[index]); 43 upload_complete_[index] = true; 44 upload_successful_[index] = success; 45 } 46 47 scoped_refptr<base::TestSimpleTaskRunner> network_task_runner_; 48 net::TestURLFetcherFactory url_fetcher_factory_; 49 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; 50 scoped_ptr<DomainReliabilityUploader> uploader_; 51 52 // Whether the upload callback was called for a particular collector index. 53 std::map<size_t, bool> upload_complete_; 54 // Whether the upload to a particular collector was successful. 55 std::map<size_t, bool> upload_successful_; 56 }; 57 58 TEST_F(DomainReliabilityUploaderTest, Create) { 59 net::TestURLFetcher* fetcher; 60 61 fetcher = url_fetcher_factory_.GetFetcherByID(0); 62 EXPECT_FALSE(fetcher); 63 } 64 65 TEST_F(DomainReliabilityUploaderTest, SuccessfulUpload) { 66 net::TestURLFetcher* fetcher; 67 68 std::string report_json = "{}"; 69 GURL upload_url = GURL("https://example/upload"); 70 uploader_->UploadReport(report_json, upload_url, MakeUploadCallback(0)); 71 72 fetcher = url_fetcher_factory_.GetFetcherByID(0); 73 EXPECT_TRUE(fetcher); 74 EXPECT_EQ(report_json, fetcher->upload_data()); 75 EXPECT_EQ(upload_url, fetcher->GetOriginalURL()); 76 EXPECT_TRUE(fetcher->GetLoadFlags() & net::LOAD_DO_NOT_SAVE_COOKIES); 77 EXPECT_TRUE(fetcher->GetLoadFlags() & net::LOAD_DO_NOT_SEND_COOKIES); 78 79 fetcher->set_url(upload_url); 80 fetcher->set_status(net::URLRequestStatus()); 81 fetcher->set_response_code(200); 82 fetcher->SetResponseString(""); 83 84 EXPECT_FALSE(upload_complete_[0]); 85 fetcher->delegate()->OnURLFetchComplete(fetcher); 86 EXPECT_TRUE(upload_complete_[0]); 87 EXPECT_TRUE(upload_successful_[0]); 88 } 89 90 TEST_F(DomainReliabilityUploaderTest, FailedUpload) { 91 net::TestURLFetcher* fetcher; 92 93 std::string report_json = "{}"; 94 GURL upload_url = GURL("https://example/upload"); 95 uploader_->UploadReport(report_json, upload_url, MakeUploadCallback(0)); 96 97 fetcher = url_fetcher_factory_.GetFetcherByID(0); 98 EXPECT_TRUE(fetcher); 99 EXPECT_EQ(report_json, fetcher->upload_data()); 100 EXPECT_EQ(upload_url, fetcher->GetOriginalURL()); 101 102 fetcher->set_url(upload_url); 103 fetcher->set_status(net::URLRequestStatus()); 104 fetcher->set_response_code(500); 105 fetcher->SetResponseString(""); 106 107 EXPECT_FALSE(upload_complete_[0]); 108 fetcher->delegate()->OnURLFetchComplete(fetcher); 109 EXPECT_TRUE(upload_complete_[0]); 110 EXPECT_FALSE(upload_successful_[0]); 111 } 112 113 TEST_F(DomainReliabilityUploaderTest, DiscardedUpload) { 114 net::TestURLFetcher* fetcher; 115 116 uploader_->set_discard_uploads(true); 117 118 std::string report_json = "{}"; 119 GURL upload_url = GURL("https://example/upload"); 120 uploader_->UploadReport(report_json, upload_url, MakeUploadCallback(0)); 121 122 fetcher = url_fetcher_factory_.GetFetcherByID(0); 123 EXPECT_FALSE(fetcher); 124 } 125 126 } // namespace 127 } // namespace domain_reliability 128