[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 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 "components/domain_reliability/test_util.h" |
| 13 | #include "content/public/test/test_browser_thread_bundle.h" |
| 14 | #include "net/url_request/test_url_fetcher_factory.h" |
| 15 | #include "net/url_request/url_fetcher_delegate.h" |
| 16 | #include "net/url_request/url_request_context_getter.h" |
| 17 | #include "net/url_request/url_request_test_util.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
| 20 | namespace domain_reliability { |
[email protected] | 563dc476 | 2014-05-11 00:43:49 | [diff] [blame^] | 21 | namespace { |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 22 | |
| 23 | class DomainReliabilityUploaderTest : public testing::Test { |
| 24 | protected: |
| 25 | DomainReliabilityUploaderTest() |
| 26 | : test_browser_thread_bundle_( |
| 27 | content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 28 | url_request_context_getter_(new net::TestURLRequestContextGetter( |
| 29 | base::MessageLoopProxy::current())), |
| 30 | uploader_(DomainReliabilityUploader::Create( |
| 31 | url_request_context_getter_)) {} |
| 32 | |
[email protected] | 84d2a49 | 2014-05-09 22:18:50 | [diff] [blame] | 33 | DomainReliabilityUploader::UploadCallback MakeUploadCallback(size_t index) { |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 34 | return base::Bind(&DomainReliabilityUploaderTest::OnUploadComplete, |
| 35 | base::Unretained(this), |
| 36 | index); |
| 37 | } |
| 38 | |
[email protected] | 84d2a49 | 2014-05-09 22:18:50 | [diff] [blame] | 39 | void OnUploadComplete(size_t index, bool success) { |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 40 | EXPECT_FALSE(upload_complete_[index]); |
| 41 | upload_complete_[index] = true; |
| 42 | upload_successful_[index] = success; |
| 43 | } |
| 44 | |
| 45 | content::TestBrowserThreadBundle test_browser_thread_bundle_; |
| 46 | net::TestURLFetcherFactory url_fetcher_factory_; |
| 47 | scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; |
| 48 | scoped_ptr<DomainReliabilityUploader> uploader_; |
| 49 | |
[email protected] | 84d2a49 | 2014-05-09 22:18:50 | [diff] [blame] | 50 | // Whether the upload callback was called for a particular collector index. |
| 51 | std::map<size_t, bool> upload_complete_; |
| 52 | // Whether the upload to a particular collector was successful. |
| 53 | std::map<size_t, bool> upload_successful_; |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | TEST_F(DomainReliabilityUploaderTest, Create) { |
| 57 | net::TestURLFetcher* fetcher; |
| 58 | |
| 59 | fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 60 | EXPECT_FALSE(fetcher); |
| 61 | } |
| 62 | |
| 63 | TEST_F(DomainReliabilityUploaderTest, SuccessfulUpload) { |
| 64 | net::TestURLFetcher* fetcher; |
| 65 | |
| 66 | std::string report_json = "{}"; |
| 67 | GURL upload_url = GURL("https://test.example/upload"); |
| 68 | uploader_->UploadReport(report_json, upload_url, MakeUploadCallback(0)); |
| 69 | |
| 70 | fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 71 | EXPECT_TRUE(fetcher); |
| 72 | EXPECT_EQ(report_json, fetcher->upload_data()); |
| 73 | EXPECT_EQ(upload_url, fetcher->GetOriginalURL()); |
| 74 | |
| 75 | fetcher->set_url(upload_url); |
| 76 | fetcher->set_status(net::URLRequestStatus()); |
| 77 | fetcher->set_response_code(200); |
| 78 | fetcher->SetResponseString(""); |
| 79 | |
| 80 | EXPECT_FALSE(upload_complete_[0]); |
| 81 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 82 | EXPECT_TRUE(upload_complete_[0]); |
| 83 | EXPECT_TRUE(upload_successful_[0]); |
| 84 | } |
| 85 | |
| 86 | TEST_F(DomainReliabilityUploaderTest, FailedUpload) { |
| 87 | net::TestURLFetcher* fetcher; |
| 88 | |
| 89 | std::string report_json = "{}"; |
| 90 | GURL upload_url = GURL("https://test.example/upload"); |
| 91 | uploader_->UploadReport(report_json, upload_url, MakeUploadCallback(0)); |
| 92 | |
| 93 | fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 94 | EXPECT_TRUE(fetcher); |
| 95 | EXPECT_EQ(report_json, fetcher->upload_data()); |
| 96 | EXPECT_EQ(upload_url, fetcher->GetOriginalURL()); |
| 97 | |
| 98 | fetcher->set_url(upload_url); |
| 99 | fetcher->set_status(net::URLRequestStatus()); |
| 100 | fetcher->set_response_code(500); |
| 101 | fetcher->SetResponseString(""); |
| 102 | |
| 103 | EXPECT_FALSE(upload_complete_[0]); |
| 104 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 105 | EXPECT_TRUE(upload_complete_[0]); |
| 106 | EXPECT_FALSE(upload_successful_[0]); |
| 107 | } |
| 108 | |
[email protected] | 563dc476 | 2014-05-11 00:43:49 | [diff] [blame^] | 109 | } // namespace |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 110 | } // namespace domain_reliability |