OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/domain_reliability/uploader.h" | 5 #include "components/domain_reliability/uploader.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "net/url_request/url_request_test_util.h" | 26 #include "net/url_request/url_request_test_util.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
28 | 28 |
29 namespace domain_reliability { | 29 namespace domain_reliability { |
30 namespace { | 30 namespace { |
31 | 31 |
32 const char kUploadURL[] = "https://example/upload"; | 32 const char kUploadURL[] = "https://example/upload"; |
33 | 33 |
34 struct MockUploadResult { | 34 struct MockUploadResult { |
35 int net_error; | 35 int net_error; |
36 int response_code; | |
37 scoped_refptr<net::HttpResponseHeaders> response_headers; | 36 scoped_refptr<net::HttpResponseHeaders> response_headers; |
38 }; | 37 }; |
39 | 38 |
40 class UploadMockURLRequestJob : public net::URLRequestJob { | 39 class UploadMockURLRequestJob : public net::URLRequestJob { |
41 public: | 40 public: |
42 UploadMockURLRequestJob(net::URLRequest* request, | 41 UploadMockURLRequestJob(net::URLRequest* request, |
43 net::NetworkDelegate* network_delegate, | 42 net::NetworkDelegate* network_delegate, |
44 MockUploadResult result) | 43 MockUploadResult result) |
45 : net::URLRequestJob(request, network_delegate), | 44 : net::URLRequestJob(request, network_delegate), |
46 upload_stream_(nullptr), | 45 upload_stream_(nullptr), |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 | 87 |
89 upload_data_ = std::string(upload_buffer_->data(), upload_buffer_->size()); | 88 upload_data_ = std::string(upload_buffer_->data(), upload_buffer_->size()); |
90 upload_buffer_ = nullptr; | 89 upload_buffer_ = nullptr; |
91 | 90 |
92 if (result_.net_error == net::OK) | 91 if (result_.net_error == net::OK) |
93 NotifyHeadersComplete(); | 92 NotifyHeadersComplete(); |
94 else if (result_.net_error != net::ERR_IO_PENDING) | 93 else if (result_.net_error != net::ERR_IO_PENDING) |
95 NotifyStartError(net::URLRequestStatus::FromError(result_.net_error)); | 94 NotifyStartError(net::URLRequestStatus::FromError(result_.net_error)); |
96 } | 95 } |
97 | 96 |
98 int GetResponseCode() const override { | |
99 return result_.response_code; | |
100 } | |
101 | |
102 void GetResponseInfo(net::HttpResponseInfo* info) override { | 97 void GetResponseInfo(net::HttpResponseInfo* info) override { |
103 info->headers = result_.response_headers; | 98 info->headers = result_.response_headers; |
104 } | 99 } |
105 | 100 |
106 net::UploadDataStream* upload_stream_; | 101 net::UploadDataStream* upload_stream_; |
107 scoped_refptr<net::IOBufferWithSize> upload_buffer_; | 102 scoped_refptr<net::IOBufferWithSize> upload_buffer_; |
108 std::string upload_data_; | 103 std::string upload_data_; |
109 MockUploadResult result_; | 104 MockUploadResult result_; |
110 }; | 105 }; |
111 | 106 |
(...skipping 16 matching lines...) Expand all Loading... |
128 DomainReliabilityUploader::GetURLRequestUploadDepth(*request); | 123 DomainReliabilityUploader::GetURLRequestUploadDepth(*request); |
129 | 124 |
130 ++request_count_; | 125 ++request_count_; |
131 | 126 |
132 return new UploadMockURLRequestJob(request, delegate, result); | 127 return new UploadMockURLRequestJob(request, delegate, result); |
133 } | 128 } |
134 | 129 |
135 void ExpectRequestAndReturnError(int net_error) { | 130 void ExpectRequestAndReturnError(int net_error) { |
136 MockUploadResult result; | 131 MockUploadResult result; |
137 result.net_error = net_error; | 132 result.net_error = net_error; |
138 result.response_code = -1; | |
139 results_.push_back(result); | 133 results_.push_back(result); |
140 } | 134 } |
141 | 135 |
142 void ExpectRequestAndReturnResponseCode(int response_code) { | 136 void ExpectRequestAndReturnResponseHeaders(const char* headers) { |
143 MockUploadResult result; | 137 MockUploadResult result; |
144 result.net_error = net::OK; | 138 result.net_error = net::OK; |
145 result.response_code = response_code; | |
146 results_.push_back(result); | |
147 } | |
148 | |
149 void ExpectRequestAndReturnResponseCodeAndHeaders( | |
150 int response_code, | |
151 const char* headers) { | |
152 MockUploadResult result; | |
153 result.net_error = net::OK; | |
154 result.response_code = response_code; | |
155 result.response_headers = new net::HttpResponseHeaders( | 139 result.response_headers = new net::HttpResponseHeaders( |
156 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers))); | 140 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers))); |
157 results_.push_back(result); | 141 results_.push_back(result); |
158 } | 142 } |
159 | 143 |
160 int request_count() const { return request_count_; } | 144 int request_count() const { return request_count_; } |
161 int last_upload_depth() const { return last_upload_depth_; } | 145 int last_upload_depth() const { return last_upload_depth_; } |
162 | 146 |
163 private: | 147 private: |
164 mutable std::list<MockUploadResult> results_; | 148 mutable std::list<MockUploadResult> results_; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 UploadInterceptor* interceptor_; | 203 UploadInterceptor* interceptor_; |
220 MockTime time_; | 204 MockTime time_; |
221 std::unique_ptr<DomainReliabilityUploader> uploader_; | 205 std::unique_ptr<DomainReliabilityUploader> uploader_; |
222 }; | 206 }; |
223 | 207 |
224 TEST_F(DomainReliabilityUploaderTest, Null) { | 208 TEST_F(DomainReliabilityUploaderTest, Null) { |
225 uploader()->Shutdown(); | 209 uploader()->Shutdown(); |
226 } | 210 } |
227 | 211 |
228 TEST_F(DomainReliabilityUploaderTest, SuccessfulUpload) { | 212 TEST_F(DomainReliabilityUploaderTest, SuccessfulUpload) { |
229 interceptor()->ExpectRequestAndReturnResponseCode(200); | 213 interceptor()->ExpectRequestAndReturnResponseHeaders("HTTP/1.1 200\r\n\r\n"); |
230 | 214 |
231 TestUploadCallback c; | 215 TestUploadCallback c; |
232 uploader()->UploadReport("{}", 0, GURL(kUploadURL), c.callback()); | 216 uploader()->UploadReport("{}", 0, GURL(kUploadURL), c.callback()); |
233 base::RunLoop().RunUntilIdle(); | 217 base::RunLoop().RunUntilIdle(); |
234 EXPECT_EQ(1u, c.called_count()); | 218 EXPECT_EQ(1u, c.called_count()); |
235 EXPECT_TRUE(c.last_result().is_success()); | 219 EXPECT_TRUE(c.last_result().is_success()); |
236 | 220 |
237 uploader()->Shutdown(); | 221 uploader()->Shutdown(); |
238 } | 222 } |
239 | 223 |
240 TEST_F(DomainReliabilityUploaderTest, NetworkErrorUpload) { | 224 TEST_F(DomainReliabilityUploaderTest, NetworkErrorUpload) { |
241 interceptor()->ExpectRequestAndReturnError(net::ERR_CONNECTION_REFUSED); | 225 interceptor()->ExpectRequestAndReturnError(net::ERR_CONNECTION_REFUSED); |
242 | 226 |
243 TestUploadCallback c; | 227 TestUploadCallback c; |
244 uploader()->UploadReport("{}", 0, GURL(kUploadURL), c.callback()); | 228 uploader()->UploadReport("{}", 0, GURL(kUploadURL), c.callback()); |
245 base::RunLoop().RunUntilIdle(); | 229 base::RunLoop().RunUntilIdle(); |
246 EXPECT_EQ(1u, c.called_count()); | 230 EXPECT_EQ(1u, c.called_count()); |
247 EXPECT_TRUE(c.last_result().is_failure()); | 231 EXPECT_TRUE(c.last_result().is_failure()); |
248 | 232 |
249 uploader()->Shutdown(); | 233 uploader()->Shutdown(); |
250 } | 234 } |
251 | 235 |
252 TEST_F(DomainReliabilityUploaderTest, ServerErrorUpload) { | 236 TEST_F(DomainReliabilityUploaderTest, ServerErrorUpload) { |
253 interceptor()->ExpectRequestAndReturnResponseCode(500); | 237 interceptor()->ExpectRequestAndReturnResponseHeaders("HTTP/1.1 500\r\n\r\n"); |
254 | 238 |
255 TestUploadCallback c; | 239 TestUploadCallback c; |
256 uploader()->UploadReport("{}", 0, GURL(kUploadURL), c.callback()); | 240 uploader()->UploadReport("{}", 0, GURL(kUploadURL), c.callback()); |
257 base::RunLoop().RunUntilIdle(); | 241 base::RunLoop().RunUntilIdle(); |
258 EXPECT_EQ(1u, c.called_count()); | 242 EXPECT_EQ(1u, c.called_count()); |
259 EXPECT_TRUE(c.last_result().is_failure()); | 243 EXPECT_TRUE(c.last_result().is_failure()); |
260 | 244 |
261 uploader()->Shutdown(); | 245 uploader()->Shutdown(); |
262 } | 246 } |
263 | 247 |
264 TEST_F(DomainReliabilityUploaderTest, RetryAfterUpload) { | 248 TEST_F(DomainReliabilityUploaderTest, RetryAfterUpload) { |
265 interceptor()->ExpectRequestAndReturnResponseCodeAndHeaders( | 249 interceptor()->ExpectRequestAndReturnResponseHeaders( |
266 503, | |
267 "HTTP/1.1 503 Ugh\nRetry-After: 3600\n\n"); | 250 "HTTP/1.1 503 Ugh\nRetry-After: 3600\n\n"); |
268 | 251 |
269 TestUploadCallback c; | 252 TestUploadCallback c; |
270 uploader()->UploadReport("{}", 0, GURL(kUploadURL), c.callback()); | 253 uploader()->UploadReport("{}", 0, GURL(kUploadURL), c.callback()); |
271 base::RunLoop().RunUntilIdle(); | 254 base::RunLoop().RunUntilIdle(); |
272 EXPECT_EQ(1u, c.called_count()); | 255 EXPECT_EQ(1u, c.called_count()); |
273 EXPECT_TRUE(c.last_result().is_retry_after()); | 256 EXPECT_TRUE(c.last_result().is_retry_after()); |
274 | 257 |
275 uploader()->Shutdown(); | 258 uploader()->Shutdown(); |
276 } | 259 } |
277 | 260 |
278 TEST_F(DomainReliabilityUploaderTest, UploadDepth1) { | 261 TEST_F(DomainReliabilityUploaderTest, UploadDepth1) { |
279 interceptor()->ExpectRequestAndReturnResponseCode(200); | 262 interceptor()->ExpectRequestAndReturnResponseHeaders("HTTP/1.1 200\r\n\r\n"); |
280 | 263 |
281 TestUploadCallback c; | 264 TestUploadCallback c; |
282 uploader()->UploadReport("{}", 0, GURL(kUploadURL), c.callback()); | 265 uploader()->UploadReport("{}", 0, GURL(kUploadURL), c.callback()); |
283 base::RunLoop().RunUntilIdle(); | 266 base::RunLoop().RunUntilIdle(); |
284 EXPECT_EQ(1u, c.called_count()); | 267 EXPECT_EQ(1u, c.called_count()); |
285 | 268 |
286 EXPECT_EQ(1, interceptor()->last_upload_depth()); | 269 EXPECT_EQ(1, interceptor()->last_upload_depth()); |
287 | 270 |
288 uploader()->Shutdown(); | 271 uploader()->Shutdown(); |
289 } | 272 } |
290 | 273 |
291 TEST_F(DomainReliabilityUploaderTest, UploadDepth2) { | 274 TEST_F(DomainReliabilityUploaderTest, UploadDepth2) { |
292 interceptor()->ExpectRequestAndReturnResponseCode(200); | 275 interceptor()->ExpectRequestAndReturnResponseHeaders("HTTP/1.1 200\r\n\r\n"); |
293 | 276 |
294 TestUploadCallback c; | 277 TestUploadCallback c; |
295 uploader()->UploadReport("{}", 1, GURL(kUploadURL), c.callback()); | 278 uploader()->UploadReport("{}", 1, GURL(kUploadURL), c.callback()); |
296 base::RunLoop().RunUntilIdle(); | 279 base::RunLoop().RunUntilIdle(); |
297 EXPECT_EQ(1u, c.called_count()); | 280 EXPECT_EQ(1u, c.called_count()); |
298 | 281 |
299 EXPECT_EQ(2, interceptor()->last_upload_depth()); | 282 EXPECT_EQ(2, interceptor()->last_upload_depth()); |
300 | 283 |
301 uploader()->Shutdown(); | 284 uploader()->Shutdown(); |
302 } | 285 } |
(...skipping 19 matching lines...) Expand all Loading... |
322 | 305 |
323 TestUploadCallback c; | 306 TestUploadCallback c; |
324 uploader()->UploadReport("{}", 1, GURL(kUploadURL), c.callback()); | 307 uploader()->UploadReport("{}", 1, GURL(kUploadURL), c.callback()); |
325 base::RunLoop().RunUntilIdle(); | 308 base::RunLoop().RunUntilIdle(); |
326 EXPECT_EQ(1u, c.called_count()); | 309 EXPECT_EQ(1u, c.called_count()); |
327 EXPECT_EQ(0, interceptor()->request_count()); | 310 EXPECT_EQ(0, interceptor()->request_count()); |
328 } | 311 } |
329 | 312 |
330 } // namespace | 313 } // namespace |
331 } // namespace domain_reliability | 314 } // namespace domain_reliability |
OLD | NEW |