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.
5 #include "components/domain_reliability/uploader.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"
21 namespace domain_reliability
{
24 class DomainReliabilityUploaderTest
: public testing::Test
{
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);
35 DomainReliabilityUploader::UploadCallback
MakeUploadCallback(size_t index
) {
36 return base::Bind(&DomainReliabilityUploaderTest::OnUploadComplete
,
37 base::Unretained(this),
41 void OnUploadComplete(size_t index
, bool success
) {
42 EXPECT_FALSE(upload_complete_
[index
]);
43 upload_complete_
[index
] = true;
44 upload_successful_
[index
] = success
;
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_
;
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_
;
58 TEST_F(DomainReliabilityUploaderTest
, Create
) {
59 net::TestURLFetcher
* fetcher
;
61 fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
62 EXPECT_FALSE(fetcher
);
65 TEST_F(DomainReliabilityUploaderTest
, SuccessfulUpload
) {
66 net::TestURLFetcher
* fetcher
;
68 std::string report_json
= "{}";
69 GURL upload_url
= GURL("https://example/upload");
70 uploader_
->UploadReport(report_json
, upload_url
, MakeUploadCallback(0));
72 fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
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
);
79 fetcher
->set_url(upload_url
);
80 fetcher
->set_status(net::URLRequestStatus());
81 fetcher
->set_response_code(200);
82 fetcher
->SetResponseString("");
84 EXPECT_FALSE(upload_complete_
[0]);
85 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
86 EXPECT_TRUE(upload_complete_
[0]);
87 EXPECT_TRUE(upload_successful_
[0]);
90 TEST_F(DomainReliabilityUploaderTest
, FailedUpload
) {
91 net::TestURLFetcher
* fetcher
;
93 std::string report_json
= "{}";
94 GURL upload_url
= GURL("https://example/upload");
95 uploader_
->UploadReport(report_json
, upload_url
, MakeUploadCallback(0));
97 fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
99 EXPECT_EQ(report_json
, fetcher
->upload_data());
100 EXPECT_EQ(upload_url
, fetcher
->GetOriginalURL());
102 fetcher
->set_url(upload_url
);
103 fetcher
->set_status(net::URLRequestStatus());
104 fetcher
->set_response_code(500);
105 fetcher
->SetResponseString("");
107 EXPECT_FALSE(upload_complete_
[0]);
108 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
109 EXPECT_TRUE(upload_complete_
[0]);
110 EXPECT_FALSE(upload_successful_
[0]);
113 TEST_F(DomainReliabilityUploaderTest
, DiscardedUpload
) {
114 net::TestURLFetcher
* fetcher
;
116 uploader_
->set_discard_uploads(true);
118 std::string report_json
= "{}";
119 GURL upload_url
= GURL("https://example/upload");
120 uploader_
->UploadReport(report_json
, upload_url
, MakeUploadCallback(0));
122 fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
123 EXPECT_FALSE(fetcher
);
127 } // namespace domain_reliability