[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" |
[email protected] | b5c2b74 | 2014-06-14 22:21:42 | [diff] [blame^] | 12 | #include "base/test/test_simple_task_runner.h" |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 13 | #include "components/domain_reliability/test_util.h" |
[email protected] | d759912 | 2014-05-24 03:37:23 | [diff] [blame] | 14 | #include "net/base/load_flags.h" |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 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 { |
[email protected] | 563dc476 | 2014-05-11 00:43:49 | [diff] [blame] | 22 | namespace { |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 23 | |
| 24 | class DomainReliabilityUploaderTest : public testing::Test { |
| 25 | protected: |
| 26 | DomainReliabilityUploaderTest() |
[email protected] | b5c2b74 | 2014-06-14 22:21:42 | [diff] [blame^] | 27 | : network_task_runner_(new base::TestSimpleTaskRunner()), |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 28 | url_request_context_getter_(new net::TestURLRequestContextGetter( |
[email protected] | b5c2b74 | 2014-06-14 22:21:42 | [diff] [blame^] | 29 | network_task_runner_)), |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 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 | |
[email protected] | b5c2b74 | 2014-06-14 22:21:42 | [diff] [blame^] | 45 | scoped_refptr<base::TestSimpleTaskRunner> network_task_runner_; |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 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 = "{}"; |
[email protected] | 7dbbf45 | 2014-05-16 18:31:51 | [diff] [blame] | 67 | GURL upload_url = GURL("https://example/upload"); |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 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()); |
[email protected] | 7dbbf45 | 2014-05-16 18:31:51 | [diff] [blame] | 74 | EXPECT_TRUE(fetcher->GetLoadFlags() & net::LOAD_DO_NOT_SAVE_COOKIES); |
| 75 | EXPECT_TRUE(fetcher->GetLoadFlags() & net::LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 76 | |
| 77 | fetcher->set_url(upload_url); |
| 78 | fetcher->set_status(net::URLRequestStatus()); |
| 79 | fetcher->set_response_code(200); |
| 80 | fetcher->SetResponseString(""); |
| 81 | |
| 82 | EXPECT_FALSE(upload_complete_[0]); |
| 83 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 84 | EXPECT_TRUE(upload_complete_[0]); |
| 85 | EXPECT_TRUE(upload_successful_[0]); |
| 86 | } |
| 87 | |
| 88 | TEST_F(DomainReliabilityUploaderTest, FailedUpload) { |
| 89 | net::TestURLFetcher* fetcher; |
| 90 | |
| 91 | std::string report_json = "{}"; |
[email protected] | 7dbbf45 | 2014-05-16 18:31:51 | [diff] [blame] | 92 | GURL upload_url = GURL("https://example/upload"); |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 93 | uploader_->UploadReport(report_json, upload_url, MakeUploadCallback(0)); |
| 94 | |
| 95 | fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 96 | EXPECT_TRUE(fetcher); |
| 97 | EXPECT_EQ(report_json, fetcher->upload_data()); |
| 98 | EXPECT_EQ(upload_url, fetcher->GetOriginalURL()); |
| 99 | |
| 100 | fetcher->set_url(upload_url); |
| 101 | fetcher->set_status(net::URLRequestStatus()); |
| 102 | fetcher->set_response_code(500); |
| 103 | fetcher->SetResponseString(""); |
| 104 | |
| 105 | EXPECT_FALSE(upload_complete_[0]); |
| 106 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 107 | EXPECT_TRUE(upload_complete_[0]); |
| 108 | EXPECT_FALSE(upload_successful_[0]); |
| 109 | } |
| 110 | |
[email protected] | 563dc476 | 2014-05-11 00:43:49 | [diff] [blame] | 111 | } // namespace |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 112 | } // namespace domain_reliability |