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 "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"
20 namespace domain_reliability
{
22 class DomainReliabilityUploaderTest
: public testing::Test
{
24 DomainReliabilityUploaderTest()
25 : test_browser_thread_bundle_(
26 content::TestBrowserThreadBundle::IO_MAINLOOP
),
27 url_request_context_getter_(new net::TestURLRequestContextGetter(
28 base::MessageLoopProxy::current())),
29 uploader_(DomainReliabilityUploader::Create(
30 url_request_context_getter_
)) {}
32 DomainReliabilityUploader::UploadCallback
MakeUploadCallback(int index
) {
33 return base::Bind(&DomainReliabilityUploaderTest::OnUploadComplete
,
34 base::Unretained(this),
38 void OnUploadComplete(int index
, bool success
) {
39 EXPECT_FALSE(upload_complete_
[index
]);
40 upload_complete_
[index
] = true;
41 upload_successful_
[index
] = success
;
44 content::TestBrowserThreadBundle test_browser_thread_bundle_
;
45 net::TestURLFetcherFactory url_fetcher_factory_
;
46 scoped_refptr
<net::TestURLRequestContextGetter
> url_request_context_getter_
;
47 scoped_ptr
<DomainReliabilityUploader
> uploader_
;
49 std::map
<int, bool> upload_complete_
;
50 std::map
<int, bool> upload_successful_
;
53 TEST_F(DomainReliabilityUploaderTest
, Create
) {
54 net::TestURLFetcher
* fetcher
;
56 fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
57 EXPECT_FALSE(fetcher
);
60 TEST_F(DomainReliabilityUploaderTest
, SuccessfulUpload
) {
61 net::TestURLFetcher
* fetcher
;
63 std::string report_json
= "{}";
64 GURL upload_url
= GURL("https://test.example/upload");
65 uploader_
->UploadReport(report_json
, upload_url
, MakeUploadCallback(0));
67 fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
69 EXPECT_EQ(report_json
, fetcher
->upload_data());
70 EXPECT_EQ(upload_url
, fetcher
->GetOriginalURL());
72 fetcher
->set_url(upload_url
);
73 fetcher
->set_status(net::URLRequestStatus());
74 fetcher
->set_response_code(200);
75 fetcher
->SetResponseString("");
77 EXPECT_FALSE(upload_complete_
[0]);
78 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
79 EXPECT_TRUE(upload_complete_
[0]);
80 EXPECT_TRUE(upload_successful_
[0]);
83 TEST_F(DomainReliabilityUploaderTest
, FailedUpload
) {
84 net::TestURLFetcher
* fetcher
;
86 std::string report_json
= "{}";
87 GURL upload_url
= GURL("https://test.example/upload");
88 uploader_
->UploadReport(report_json
, upload_url
, MakeUploadCallback(0));
90 fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
92 EXPECT_EQ(report_json
, fetcher
->upload_data());
93 EXPECT_EQ(upload_url
, fetcher
->GetOriginalURL());
95 fetcher
->set_url(upload_url
);
96 fetcher
->set_status(net::URLRequestStatus());
97 fetcher
->set_response_code(500);
98 fetcher
->SetResponseString("");
100 EXPECT_FALSE(upload_complete_
[0]);
101 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
102 EXPECT_TRUE(upload_complete_
[0]);
103 EXPECT_FALSE(upload_successful_
[0]);
106 } // namespace domain_reliability