1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/domain_reliability/uploader.h"
#include <algorithm>
#include <cstring>
#include <map>
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/test/test_simple_task_runner.h"
#include "components/domain_reliability/test_util.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace domain_reliability {
namespace {
const char *kReportJSON = "{}";
const char *kUploadURL = "https://example/upload";
class DomainReliabilityUploaderTest : public testing::Test {
protected:
DomainReliabilityUploaderTest()
: network_task_runner_(new base::TestSimpleTaskRunner()),
url_request_context_getter_(new net::TestURLRequestContextGetter(
network_task_runner_)),
uploader_(DomainReliabilityUploader::Create(
&time_, url_request_context_getter_)) {
uploader_->set_discard_uploads(false);
}
DomainReliabilityUploader::UploadCallback MakeUploadCallback(size_t index) {
return base::Bind(&DomainReliabilityUploaderTest::OnUploadComplete,
base::Unretained(this),
index);
}
void OnUploadComplete(size_t index,
const DomainReliabilityUploader::UploadResult& result) {
EXPECT_FALSE(upload_complete_[index]);
upload_complete_[index] = true;
upload_result_[index] = result;
}
void SimulateUpload() {
uploader_->UploadReport(kReportJSON,
GURL(kUploadURL),
MakeUploadCallback(0));
}
void SimulateUploadRequest() {
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
EXPECT_TRUE(fetcher);
EXPECT_EQ(kReportJSON, fetcher->upload_data());
EXPECT_EQ(GURL(kUploadURL), fetcher->GetOriginalURL());
EXPECT_TRUE(fetcher->GetLoadFlags() & net::LOAD_DO_NOT_SAVE_COOKIES);
EXPECT_TRUE(fetcher->GetLoadFlags() & net::LOAD_DO_NOT_SEND_COOKIES);
fetcher->set_url(GURL(kUploadURL));
}
void SimulateNoUploadRequest() {
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
EXPECT_FALSE(fetcher);
}
void SimulateUploadResponse(int response_code, const char* headers) {
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
EXPECT_TRUE(fetcher);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(response_code);
fetcher->set_response_headers(new net::HttpResponseHeaders(
net::HttpUtil::AssembleRawHeaders(headers, strlen(headers))));
fetcher->SetResponseString("");
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
void SimulateUploadError(int error) {
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
EXPECT_TRUE(fetcher);
net::URLRequestStatus status;
fetcher->set_status(net::URLRequestStatus::FromError(error));
fetcher->set_response_code(-1);
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
scoped_refptr<base::TestSimpleTaskRunner> network_task_runner_;
net::TestURLFetcherFactory url_fetcher_factory_;
scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
MockTime time_;
scoped_ptr<DomainReliabilityUploader> uploader_;
// Whether the upload callback was called for a particular collector index.
std::map<size_t, bool> upload_complete_;
// The result of the last completed upload for a particular collector index.
std::map<size_t, DomainReliabilityUploader::UploadResult> upload_result_;
};
TEST_F(DomainReliabilityUploaderTest, Create) {
SimulateNoUploadRequest();
}
TEST_F(DomainReliabilityUploaderTest, SuccessfulUpload) {
SimulateUpload();
SimulateUploadRequest();
EXPECT_FALSE(upload_complete_[0]);
SimulateUploadResponse(200, "HTTP/1.1 200 OK\n\n");
EXPECT_TRUE(upload_complete_[0]);
EXPECT_TRUE(upload_result_[0].is_success());
}
TEST_F(DomainReliabilityUploaderTest, NetworkFailedUpload) {
SimulateUpload();
SimulateUploadRequest();
EXPECT_FALSE(upload_complete_[0]);
SimulateUploadError(net::ERR_CONNECTION_REFUSED);
EXPECT_TRUE(upload_complete_[0]);
EXPECT_TRUE(upload_result_[0].is_failure());
}
TEST_F(DomainReliabilityUploaderTest, ServerFailedUpload) {
SimulateUpload();
SimulateUploadRequest();
EXPECT_FALSE(upload_complete_[0]);
SimulateUploadResponse(500, "HTTP/1.1 500 Blargh\n\n");
EXPECT_TRUE(upload_complete_[0]);
EXPECT_TRUE(upload_result_[0].is_failure());
}
TEST_F(DomainReliabilityUploaderTest, RetryAfterUpload) {
SimulateUpload();
SimulateUploadRequest();
EXPECT_FALSE(upload_complete_[0]);
SimulateUploadResponse(503, "HTTP/1.1 503 Ugh\nRetry-After: 3600\n\n");
EXPECT_TRUE(upload_complete_[0]);
EXPECT_TRUE(upload_result_[0].is_retry_after());
EXPECT_EQ(base::TimeDelta::FromSeconds(3600), upload_result_[0].retry_after);
}
TEST_F(DomainReliabilityUploaderTest, DiscardedUpload) {
uploader_->set_discard_uploads(true);
SimulateUpload();
SimulateNoUploadRequest();
}
} // namespace
} // namespace domain_reliability
|