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"
11 #include "base/bind.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/test/test_simple_task_runner.h"
14 #include "components/domain_reliability/test_util.h"
15 #include "net/base/load_flags.h"
16 #include "net/base/net_errors.h"
17 #include "net/http/http_response_headers.h"
18 #include "net/url_request/test_url_fetcher_factory.h"
19 #include "net/url_request/url_fetcher_delegate.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "net/url_request/url_request_test_util.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace domain_reliability
{
27 const char *kReportJSON
= "{}";
28 const char *kUploadURL
= "https://example/upload";
30 class DomainReliabilityUploaderTest
: public testing::Test
{
32 DomainReliabilityUploaderTest()
33 : network_task_runner_(new base::TestSimpleTaskRunner()),
34 url_request_context_getter_(new net::TestURLRequestContextGetter(
35 network_task_runner_
)),
36 uploader_(DomainReliabilityUploader::Create(
37 &time_
, url_request_context_getter_
)) {
38 uploader_
->set_discard_uploads(false);
41 DomainReliabilityUploader::UploadCallback
MakeUploadCallback(size_t index
) {
42 return base::Bind(&DomainReliabilityUploaderTest::OnUploadComplete
,
43 base::Unretained(this),
47 void OnUploadComplete(size_t index
,
48 const DomainReliabilityUploader::UploadResult
& result
) {
49 EXPECT_FALSE(upload_complete_
[index
]);
50 upload_complete_
[index
] = true;
51 upload_result_
[index
] = result
;
54 void SimulateUpload() {
55 uploader_
->UploadReport(kReportJSON
,
57 MakeUploadCallback(0));
60 void SimulateUploadRequest() {
61 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
64 EXPECT_EQ(kReportJSON
, fetcher
->upload_data());
65 EXPECT_EQ(GURL(kUploadURL
), fetcher
->GetOriginalURL());
66 EXPECT_TRUE(fetcher
->GetLoadFlags() & net::LOAD_DO_NOT_SAVE_COOKIES
);
67 EXPECT_TRUE(fetcher
->GetLoadFlags() & net::LOAD_DO_NOT_SEND_COOKIES
);
69 fetcher
->set_url(GURL(kUploadURL
));
72 void SimulateNoUploadRequest() {
73 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
74 EXPECT_FALSE(fetcher
);
77 void SimulateUploadResponse(int response_code
, const char* headers
) {
78 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
81 fetcher
->set_status(net::URLRequestStatus());
82 fetcher
->set_response_code(response_code
);
83 fetcher
->set_response_headers(new net::HttpResponseHeaders(
84 net::HttpUtil::AssembleRawHeaders(headers
, strlen(headers
))));
85 fetcher
->SetResponseString("");
86 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
89 void SimulateUploadError(int error
) {
90 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
93 net::URLRequestStatus status
;
94 fetcher
->set_status(net::URLRequestStatus::FromError(error
));
95 fetcher
->set_response_code(-1);
96 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
99 scoped_refptr
<base::TestSimpleTaskRunner
> network_task_runner_
;
100 net::TestURLFetcherFactory url_fetcher_factory_
;
101 scoped_refptr
<net::TestURLRequestContextGetter
> url_request_context_getter_
;
103 scoped_ptr
<DomainReliabilityUploader
> uploader_
;
105 // Whether the upload callback was called for a particular collector index.
106 std::map
<size_t, bool> upload_complete_
;
107 // The result of the last completed upload for a particular collector index.
108 std::map
<size_t, DomainReliabilityUploader::UploadResult
> upload_result_
;
111 TEST_F(DomainReliabilityUploaderTest
, Create
) {
112 SimulateNoUploadRequest();
115 TEST_F(DomainReliabilityUploaderTest
, SuccessfulUpload
) {
117 SimulateUploadRequest();
118 EXPECT_FALSE(upload_complete_
[0]);
119 SimulateUploadResponse(200, "HTTP/1.1 200 OK\n\n");
120 EXPECT_TRUE(upload_complete_
[0]);
121 EXPECT_TRUE(upload_result_
[0].is_success());
124 TEST_F(DomainReliabilityUploaderTest
, NetworkFailedUpload
) {
126 SimulateUploadRequest();
127 EXPECT_FALSE(upload_complete_
[0]);
128 SimulateUploadError(net::ERR_CONNECTION_REFUSED
);
129 EXPECT_TRUE(upload_complete_
[0]);
130 EXPECT_TRUE(upload_result_
[0].is_failure());
133 TEST_F(DomainReliabilityUploaderTest
, ServerFailedUpload
) {
135 SimulateUploadRequest();
136 EXPECT_FALSE(upload_complete_
[0]);
137 SimulateUploadResponse(500, "HTTP/1.1 500 Blargh\n\n");
138 EXPECT_TRUE(upload_complete_
[0]);
139 EXPECT_TRUE(upload_result_
[0].is_failure());
142 TEST_F(DomainReliabilityUploaderTest
, RetryAfterUpload
) {
144 SimulateUploadRequest();
145 EXPECT_FALSE(upload_complete_
[0]);
146 SimulateUploadResponse(503, "HTTP/1.1 503 Ugh\nRetry-After: 3600\n\n");
147 EXPECT_TRUE(upload_complete_
[0]);
148 EXPECT_TRUE(upload_result_
[0].is_retry_after());
149 EXPECT_EQ(base::TimeDelta::FromSeconds(3600), upload_result_
[0].retry_after
);
152 TEST_F(DomainReliabilityUploaderTest
, DiscardedUpload
) {
153 uploader_
->set_discard_uploads(true);
155 SimulateNoUploadRequest();
159 } // namespace domain_reliability