Make castv2 performance test work.
[chromium-blink-merge.git] / components / domain_reliability / uploader_unittest.cc
blob05911861bda59413ac2ededff13f879e52af9db3
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"
7 #include <algorithm>
8 #include <cstring>
9 #include <map>
11 #include "base/bind.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/test/test_simple_task_runner.h"
15 #include "components/domain_reliability/test_util.h"
16 #include "net/base/load_flags.h"
17 #include "net/base/net_errors.h"
18 #include "net/http/http_response_headers.h"
19 #include "net/url_request/test_url_fetcher_factory.h"
20 #include "net/url_request/url_fetcher_delegate.h"
21 #include "net/url_request/url_request_context_getter.h"
22 #include "net/url_request/url_request_test_util.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 namespace domain_reliability {
26 namespace {
28 const char *kReportJSON = "{}";
29 const char *kUploadURL = "https://example/upload";
31 class DomainReliabilityUploaderTest : public testing::Test {
32 protected:
33 DomainReliabilityUploaderTest()
34 : network_task_runner_(new base::TestSimpleTaskRunner()),
35 url_request_context_getter_(new net::TestURLRequestContextGetter(
36 network_task_runner_)),
37 uploader_(DomainReliabilityUploader::Create(
38 &time_, url_request_context_getter_)) {
39 uploader_->set_discard_uploads(false);
42 DomainReliabilityUploader::UploadCallback MakeUploadCallback(size_t index) {
43 return base::Bind(&DomainReliabilityUploaderTest::OnUploadComplete,
44 base::Unretained(this),
45 index);
48 void OnUploadComplete(size_t index,
49 const DomainReliabilityUploader::UploadResult& result) {
50 EXPECT_FALSE(upload_complete_[index]);
51 upload_complete_[index] = true;
52 upload_result_[index] = result;
55 void SimulateUpload() {
56 uploader_->UploadReport(kReportJSON,
57 GURL(kUploadURL),
58 MakeUploadCallback(0));
61 void SimulateUploadRequest() {
62 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
63 EXPECT_TRUE(fetcher);
65 EXPECT_EQ(kReportJSON, fetcher->upload_data());
66 EXPECT_EQ(GURL(kUploadURL), fetcher->GetOriginalURL());
67 EXPECT_TRUE(fetcher->GetLoadFlags() & net::LOAD_DO_NOT_SAVE_COOKIES);
68 EXPECT_TRUE(fetcher->GetLoadFlags() & net::LOAD_DO_NOT_SEND_COOKIES);
70 fetcher->set_url(GURL(kUploadURL));
73 void SimulateNoUploadRequest() {
74 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
75 EXPECT_FALSE(fetcher);
78 void SimulateUploadResponse(int response_code, const char* headers) {
79 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
80 EXPECT_TRUE(fetcher);
82 fetcher->set_status(net::URLRequestStatus());
83 fetcher->set_response_code(response_code);
84 fetcher->set_response_headers(new net::HttpResponseHeaders(
85 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers))));
86 fetcher->SetResponseString("");
87 fetcher->delegate()->OnURLFetchComplete(fetcher);
90 void SimulateUploadError(int error) {
91 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
92 EXPECT_TRUE(fetcher);
94 net::URLRequestStatus status;
95 status.set_status(net::URLRequestStatus::FAILED);
96 status.set_error(error);
97 fetcher->set_status(status);
98 fetcher->set_response_code(-1);
99 fetcher->delegate()->OnURLFetchComplete(fetcher);
102 scoped_refptr<base::TestSimpleTaskRunner> network_task_runner_;
103 net::TestURLFetcherFactory url_fetcher_factory_;
104 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
105 MockTime time_;
106 scoped_ptr<DomainReliabilityUploader> uploader_;
108 // Whether the upload callback was called for a particular collector index.
109 std::map<size_t, bool> upload_complete_;
110 // The result of the last completed upload for a particular collector index.
111 std::map<size_t, DomainReliabilityUploader::UploadResult> upload_result_;
114 TEST_F(DomainReliabilityUploaderTest, Create) {
115 SimulateNoUploadRequest();
118 TEST_F(DomainReliabilityUploaderTest, SuccessfulUpload) {
119 SimulateUpload();
120 SimulateUploadRequest();
121 EXPECT_FALSE(upload_complete_[0]);
122 SimulateUploadResponse(200, "HTTP/1.1 200 OK\n\n");
123 EXPECT_TRUE(upload_complete_[0]);
124 EXPECT_TRUE(upload_result_[0].is_success());
127 TEST_F(DomainReliabilityUploaderTest, NetworkFailedUpload) {
128 SimulateUpload();
129 SimulateUploadRequest();
130 EXPECT_FALSE(upload_complete_[0]);
131 SimulateUploadError(net::ERR_CONNECTION_REFUSED);
132 EXPECT_TRUE(upload_complete_[0]);
133 EXPECT_TRUE(upload_result_[0].is_failure());
136 TEST_F(DomainReliabilityUploaderTest, ServerFailedUpload) {
137 SimulateUpload();
138 SimulateUploadRequest();
139 EXPECT_FALSE(upload_complete_[0]);
140 SimulateUploadResponse(500, "HTTP/1.1 500 Blargh\n\n");
141 EXPECT_TRUE(upload_complete_[0]);
142 EXPECT_TRUE(upload_result_[0].is_failure());
145 TEST_F(DomainReliabilityUploaderTest, RetryAfterUpload) {
146 SimulateUpload();
147 SimulateUploadRequest();
148 EXPECT_FALSE(upload_complete_[0]);
149 SimulateUploadResponse(503, "HTTP/1.1 503 Ugh\nRetry-After: 3600\n\n");
150 EXPECT_TRUE(upload_complete_[0]);
151 EXPECT_TRUE(upload_result_[0].is_retry_after());
152 EXPECT_EQ(base::TimeDelta::FromSeconds(3600), upload_result_[0].retry_after);
155 TEST_F(DomainReliabilityUploaderTest, DiscardedUpload) {
156 uploader_->set_discard_uploads(true);
157 SimulateUpload();
158 SimulateNoUploadRequest();
161 } // namespace
162 } // namespace domain_reliability