Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: content/child/throttling_url_loader_unittest.cc

Issue 2914423002: Network service: move URLLoaderThrottle and ThrottlingURLLoader. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/child/throttling_url_loader.cc ('k') | content/common/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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.
4
5 #include "content/child/throttling_url_loader.h"
6 #include "base/logging.h"
7 #include "base/macros.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "content/common/url_loader.mojom.h"
11 #include "content/common/url_loader_factory.mojom.h"
12 #include "content/public/child/url_loader_throttle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace content {
16 namespace {
17
18 class TestURLLoaderFactory : public mojom::URLLoaderFactory {
19 public:
20 TestURLLoaderFactory() : binding_(this) {
21 binding_.Bind(mojo::MakeRequest(&factory_ptr_));
22 }
23
24 mojom::URLLoaderFactoryPtr& factory_ptr() { return factory_ptr_; }
25 mojom::URLLoaderClientPtr& client_ptr() { return client_ptr_; }
26
27 size_t create_loader_and_start_called() const {
28 return create_loader_and_start_called_;
29 }
30
31 void NotifyClientOnReceiveResponse() {
32 client_ptr_->OnReceiveResponse(ResourceResponseHead(), base::nullopt,
33 nullptr);
34 }
35
36 void NotifyClientOnReceiveRedirect() {
37 client_ptr_->OnReceiveRedirect(net::RedirectInfo(), ResourceResponseHead());
38 }
39
40 void NotifyClientOnComplete(int error_code) {
41 ResourceRequestCompletionStatus data;
42 data.error_code = error_code;
43 client_ptr_->OnComplete(data);
44 }
45
46 private:
47 // mojom::URLLoaderFactory implementation.
48 void CreateLoaderAndStart(mojom::URLLoaderAssociatedRequest request,
49 int32_t routing_id,
50 int32_t request_id,
51 uint32_t options,
52 const ResourceRequest& url_request,
53 mojom::URLLoaderClientPtr client) override {
54 create_loader_and_start_called_++;
55
56 client_ptr_ = std::move(client);
57 }
58
59 void SyncLoad(int32_t routing_id,
60 int32_t request_id,
61 const ResourceRequest& request,
62 SyncLoadCallback callback) override {
63 NOTREACHED();
64 }
65
66 size_t create_loader_and_start_called_ = 0;
67
68 mojo::Binding<mojom::URLLoaderFactory> binding_;
69 mojom::URLLoaderFactoryPtr factory_ptr_;
70 mojom::URLLoaderClientPtr client_ptr_;
71 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderFactory);
72 };
73
74 class TestURLLoaderClient : public mojom::URLLoaderClient {
75 public:
76 TestURLLoaderClient() {}
77
78 size_t on_received_response_called() const {
79 return on_received_response_called_;
80 }
81
82 size_t on_received_redirect_called() const {
83 return on_received_redirect_called_;
84 }
85
86 size_t on_complete_called() const { return on_complete_called_; }
87
88 using OnCompleteCallback = base::Callback<void(int error_code)>;
89 void set_on_complete_callback(const OnCompleteCallback& callback) {
90 on_complete_callback_ = callback;
91 }
92
93 private:
94 // mojom::URLLoaderClient implementation:
95 void OnReceiveResponse(
96 const ResourceResponseHead& response_head,
97 const base::Optional<net::SSLInfo>& ssl_info,
98 mojom::DownloadedTempFilePtr downloaded_file) override {
99 on_received_response_called_++;
100 }
101 void OnReceiveRedirect(const net::RedirectInfo& redirect_info,
102 const ResourceResponseHead& response_head) override {
103 on_received_redirect_called_++;
104 }
105 void OnDataDownloaded(int64_t data_len, int64_t encoded_data_len) override {}
106 void OnUploadProgress(int64_t current_position,
107 int64_t total_size,
108 OnUploadProgressCallback ack_callback) override {}
109 void OnReceiveCachedMetadata(const std::vector<uint8_t>& data) override {}
110 void OnTransferSizeUpdated(int32_t transfer_size_diff) override {}
111 void OnStartLoadingResponseBody(
112 mojo::ScopedDataPipeConsumerHandle body) override {}
113 void OnComplete(const ResourceRequestCompletionStatus& status) override {
114 on_complete_called_++;
115 if (on_complete_callback_)
116 on_complete_callback_.Run(status.error_code);
117 }
118
119 size_t on_received_response_called_ = 0;
120 size_t on_received_redirect_called_ = 0;
121 size_t on_complete_called_ = 0;
122
123 OnCompleteCallback on_complete_callback_;
124
125 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderClient);
126 };
127
128 class TestURLLoaderThrottle : public URLLoaderThrottle {
129 public:
130 TestURLLoaderThrottle() {}
131
132 using ThrottleCallback =
133 base::Callback<void(URLLoaderThrottle::Delegate* delegate, bool* defer)>;
134
135 size_t will_start_request_called() const {
136 return will_start_request_called_;
137 }
138 size_t will_redirect_request_called() const {
139 return will_redirect_request_called_;
140 }
141 size_t will_process_response_called() const {
142 return will_process_response_called_;
143 }
144
145 void set_will_start_request_callback(const ThrottleCallback& callback) {
146 will_start_request_callback_ = callback;
147 }
148
149 void set_will_redirect_request_callback(const ThrottleCallback& callback) {
150 will_redirect_request_callback_ = callback;
151 }
152
153 void set_will_process_response_callback(const ThrottleCallback& callback) {
154 will_process_response_callback_ = callback;
155 }
156
157 Delegate* delegate() const { return delegate_; }
158
159 private:
160 // URLLoaderThrottle implementation.
161 void WillStartRequest(const GURL& url,
162 int load_flags,
163 ResourceType resource_type,
164 bool* defer) override {
165 will_start_request_called_++;
166 if (will_start_request_callback_)
167 will_start_request_callback_.Run(delegate_, defer);
168 }
169
170 void WillRedirectRequest(const net::RedirectInfo& redirect_info,
171 bool* defer) override {
172 will_redirect_request_called_++;
173 if (will_redirect_request_callback_)
174 will_redirect_request_callback_.Run(delegate_, defer);
175 }
176
177 void WillProcessResponse(bool* defer) override {
178 will_process_response_called_++;
179 if (will_process_response_callback_)
180 will_process_response_callback_.Run(delegate_, defer);
181 }
182
183 size_t will_start_request_called_ = 0;
184 size_t will_redirect_request_called_ = 0;
185 size_t will_process_response_called_ = 0;
186
187 ThrottleCallback will_start_request_callback_;
188 ThrottleCallback will_redirect_request_callback_;
189 ThrottleCallback will_process_response_callback_;
190
191 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderThrottle);
192 };
193
194 class ThrottlingURLLoaderTest : public testing::Test {
195 public:
196 ThrottlingURLLoaderTest() {}
197
198 protected:
199 // testing::Test implementation.
200 void SetUp() override {
201 auto throttle = base::MakeUnique<TestURLLoaderThrottle>();
202 throttle_ = throttle.get();
203
204 throttles_.push_back(std::move(throttle));
205 }
206
207 void CreateLoaderAndStart() {
208 auto request = base::MakeUnique<ResourceRequest>();
209 request->url = GURL("http://example.org");
210 loader_ = ThrottlingURLLoader::CreateLoaderAndStart(
211 factory_.factory_ptr().get(), std::move(throttles_), 0, 0, 0,
212 std::move(request), &client_);
213 factory_.factory_ptr().FlushForTesting();
214 }
215
216 // Be the first member so it is destroyed last.
217 base::MessageLoop message_loop_;
218
219 std::unique_ptr<ThrottlingURLLoader> loader_;
220 std::vector<std::unique_ptr<URLLoaderThrottle>> throttles_;
221
222 TestURLLoaderFactory factory_;
223 TestURLLoaderClient client_;
224
225 // Owned by |throttles_| or |loader_|.
226 TestURLLoaderThrottle* throttle_ = nullptr;
227
228 DISALLOW_COPY_AND_ASSIGN(ThrottlingURLLoaderTest);
229 };
230
231 TEST_F(ThrottlingURLLoaderTest, CancelBeforeStart) {
232 throttle_->set_will_start_request_callback(
233 base::Bind([](URLLoaderThrottle::Delegate* delegate, bool* defer) {
234 delegate->CancelWithError(net::ERR_ACCESS_DENIED);
235 }));
236
237 base::RunLoop run_loop;
238 client_.set_on_complete_callback(base::Bind(
239 [](const base::Closure& quit_closure, int error) {
240 EXPECT_EQ(net::ERR_ACCESS_DENIED, error);
241 quit_closure.Run();
242 },
243 run_loop.QuitClosure()));
244
245 CreateLoaderAndStart();
246 run_loop.Run();
247
248 EXPECT_EQ(1u, throttle_->will_start_request_called());
249 EXPECT_EQ(0u, throttle_->will_redirect_request_called());
250 EXPECT_EQ(0u, throttle_->will_process_response_called());
251
252 EXPECT_EQ(0u, factory_.create_loader_and_start_called());
253
254 EXPECT_EQ(0u, client_.on_received_response_called());
255 EXPECT_EQ(0u, client_.on_received_redirect_called());
256 EXPECT_EQ(1u, client_.on_complete_called());
257 }
258
259 TEST_F(ThrottlingURLLoaderTest, DeferBeforeStart) {
260 throttle_->set_will_start_request_callback(
261 base::Bind([](URLLoaderThrottle::Delegate* delegate, bool* defer) {
262 *defer = true;
263 }));
264
265 base::RunLoop run_loop;
266 client_.set_on_complete_callback(base::Bind(
267 [](const base::Closure& quit_closure, int error) {
268 EXPECT_EQ(net::OK, error);
269 quit_closure.Run();
270 },
271 run_loop.QuitClosure()));
272
273 CreateLoaderAndStart();
274
275 EXPECT_EQ(1u, throttle_->will_start_request_called());
276 EXPECT_EQ(0u, throttle_->will_redirect_request_called());
277 EXPECT_EQ(0u, throttle_->will_process_response_called());
278
279 EXPECT_EQ(0u, factory_.create_loader_and_start_called());
280
281 EXPECT_EQ(0u, client_.on_received_response_called());
282 EXPECT_EQ(0u, client_.on_received_redirect_called());
283 EXPECT_EQ(0u, client_.on_complete_called());
284
285 throttle_->delegate()->Resume();
286 factory_.factory_ptr().FlushForTesting();
287
288 EXPECT_EQ(1u, factory_.create_loader_and_start_called());
289
290 factory_.NotifyClientOnReceiveResponse();
291 factory_.NotifyClientOnComplete(net::OK);
292
293 run_loop.Run();
294
295 EXPECT_EQ(1u, throttle_->will_start_request_called());
296 EXPECT_EQ(0u, throttle_->will_redirect_request_called());
297 EXPECT_EQ(1u, throttle_->will_process_response_called());
298
299 EXPECT_EQ(1u, client_.on_received_response_called());
300 EXPECT_EQ(0u, client_.on_received_redirect_called());
301 EXPECT_EQ(1u, client_.on_complete_called());
302 }
303
304 TEST_F(ThrottlingURLLoaderTest, CancelBeforeRedirect) {
305 throttle_->set_will_redirect_request_callback(
306 base::Bind([](URLLoaderThrottle::Delegate* delegate, bool* defer) {
307 delegate->CancelWithError(net::ERR_ACCESS_DENIED);
308 }));
309
310 base::RunLoop run_loop;
311 client_.set_on_complete_callback(base::Bind(
312 [](const base::Closure& quit_closure, int error) {
313 EXPECT_EQ(net::ERR_ACCESS_DENIED, error);
314 quit_closure.Run();
315 },
316 run_loop.QuitClosure()));
317
318 CreateLoaderAndStart();
319
320 factory_.NotifyClientOnReceiveRedirect();
321
322 run_loop.Run();
323
324 EXPECT_EQ(1u, throttle_->will_start_request_called());
325 EXPECT_EQ(1u, throttle_->will_redirect_request_called());
326 EXPECT_EQ(0u, throttle_->will_process_response_called());
327
328 EXPECT_EQ(0u, client_.on_received_response_called());
329 EXPECT_EQ(0u, client_.on_received_redirect_called());
330 EXPECT_EQ(1u, client_.on_complete_called());
331 }
332
333 TEST_F(ThrottlingURLLoaderTest, DeferBeforeRedirect) {
334 base::RunLoop run_loop1;
335 throttle_->set_will_redirect_request_callback(base::Bind(
336 [](const base::Closure& quit_closure,
337 URLLoaderThrottle::Delegate* delegate, bool* defer) {
338 *defer = true;
339 quit_closure.Run();
340 },
341 run_loop1.QuitClosure()));
342
343 base::RunLoop run_loop2;
344 client_.set_on_complete_callback(base::Bind(
345 [](const base::Closure& quit_closure, int error) {
346 EXPECT_EQ(net::ERR_UNEXPECTED, error);
347 quit_closure.Run();
348 },
349 run_loop2.QuitClosure()));
350
351 CreateLoaderAndStart();
352
353 factory_.NotifyClientOnReceiveRedirect();
354
355 run_loop1.Run();
356
357 EXPECT_EQ(1u, throttle_->will_start_request_called());
358 EXPECT_EQ(1u, throttle_->will_redirect_request_called());
359 EXPECT_EQ(0u, throttle_->will_process_response_called());
360
361 factory_.NotifyClientOnComplete(net::ERR_UNEXPECTED);
362
363 base::RunLoop run_loop3;
364 run_loop3.RunUntilIdle();
365
366 EXPECT_EQ(0u, client_.on_received_response_called());
367 EXPECT_EQ(0u, client_.on_received_redirect_called());
368 EXPECT_EQ(0u, client_.on_complete_called());
369
370 throttle_->delegate()->Resume();
371 run_loop2.Run();
372
373 EXPECT_EQ(1u, throttle_->will_start_request_called());
374 EXPECT_EQ(1u, throttle_->will_redirect_request_called());
375 EXPECT_EQ(0u, throttle_->will_process_response_called());
376
377 EXPECT_EQ(0u, client_.on_received_response_called());
378 EXPECT_EQ(1u, client_.on_received_redirect_called());
379 EXPECT_EQ(1u, client_.on_complete_called());
380 }
381
382 TEST_F(ThrottlingURLLoaderTest, CancelBeforeResponse) {
383 throttle_->set_will_process_response_callback(
384 base::Bind([](URLLoaderThrottle::Delegate* delegate, bool* defer) {
385 delegate->CancelWithError(net::ERR_ACCESS_DENIED);
386 }));
387
388 base::RunLoop run_loop;
389 client_.set_on_complete_callback(base::Bind(
390 [](const base::Closure& quit_closure, int error) {
391 EXPECT_EQ(net::ERR_ACCESS_DENIED, error);
392 quit_closure.Run();
393 },
394 run_loop.QuitClosure()));
395
396 CreateLoaderAndStart();
397
398 factory_.NotifyClientOnReceiveResponse();
399
400 run_loop.Run();
401
402 EXPECT_EQ(1u, throttle_->will_start_request_called());
403 EXPECT_EQ(0u, throttle_->will_redirect_request_called());
404 EXPECT_EQ(1u, throttle_->will_process_response_called());
405
406 EXPECT_EQ(0u, client_.on_received_response_called());
407 EXPECT_EQ(0u, client_.on_received_redirect_called());
408 EXPECT_EQ(1u, client_.on_complete_called());
409 }
410
411 TEST_F(ThrottlingURLLoaderTest, DeferBeforeResponse) {
412 base::RunLoop run_loop1;
413 throttle_->set_will_process_response_callback(base::Bind(
414 [](const base::Closure& quit_closure,
415 URLLoaderThrottle::Delegate* delegate, bool* defer) {
416 *defer = true;
417 quit_closure.Run();
418 },
419 run_loop1.QuitClosure()));
420
421 base::RunLoop run_loop2;
422 client_.set_on_complete_callback(base::Bind(
423 [](const base::Closure& quit_closure, int error) {
424 EXPECT_EQ(net::ERR_UNEXPECTED, error);
425 quit_closure.Run();
426 },
427 run_loop2.QuitClosure()));
428
429 CreateLoaderAndStart();
430
431 factory_.NotifyClientOnReceiveResponse();
432
433 run_loop1.Run();
434
435 EXPECT_EQ(1u, throttle_->will_start_request_called());
436 EXPECT_EQ(0u, throttle_->will_redirect_request_called());
437 EXPECT_EQ(1u, throttle_->will_process_response_called());
438
439 factory_.NotifyClientOnComplete(net::ERR_UNEXPECTED);
440
441 base::RunLoop run_loop3;
442 run_loop3.RunUntilIdle();
443
444 EXPECT_EQ(0u, client_.on_received_response_called());
445 EXPECT_EQ(0u, client_.on_received_redirect_called());
446 EXPECT_EQ(0u, client_.on_complete_called());
447
448 throttle_->delegate()->Resume();
449 run_loop2.Run();
450
451 EXPECT_EQ(1u, throttle_->will_start_request_called());
452 EXPECT_EQ(0u, throttle_->will_redirect_request_called());
453 EXPECT_EQ(1u, throttle_->will_process_response_called());
454
455 EXPECT_EQ(1u, client_.on_received_response_called());
456 EXPECT_EQ(0u, client_.on_received_redirect_called());
457 EXPECT_EQ(1u, client_.on_complete_called());
458 }
459
460 TEST_F(ThrottlingURLLoaderTest, ResumeNoOpIfNotDeferred) {
461 auto resume_callback =
462 base::Bind([](URLLoaderThrottle::Delegate* delegate, bool* defer) {
463 delegate->Resume();
464 delegate->Resume();
465 });
466 throttle_->set_will_start_request_callback(resume_callback);
467 throttle_->set_will_redirect_request_callback(resume_callback);
468 throttle_->set_will_process_response_callback(resume_callback);
469
470 base::RunLoop run_loop;
471 client_.set_on_complete_callback(base::Bind(
472 [](const base::Closure& quit_closure, int error) {
473 EXPECT_EQ(net::OK, error);
474 quit_closure.Run();
475 },
476 run_loop.QuitClosure()));
477
478 CreateLoaderAndStart();
479 factory_.NotifyClientOnReceiveRedirect();
480 factory_.NotifyClientOnReceiveResponse();
481 factory_.NotifyClientOnComplete(net::OK);
482
483 run_loop.Run();
484
485 EXPECT_EQ(1u, throttle_->will_start_request_called());
486 EXPECT_EQ(1u, throttle_->will_redirect_request_called());
487 EXPECT_EQ(1u, throttle_->will_process_response_called());
488
489 EXPECT_EQ(1u, client_.on_received_response_called());
490 EXPECT_EQ(1u, client_.on_received_redirect_called());
491 EXPECT_EQ(1u, client_.on_complete_called());
492 }
493
494 TEST_F(ThrottlingURLLoaderTest, CancelNoOpIfAlreadyCanceled) {
495 throttle_->set_will_start_request_callback(
496 base::Bind([](URLLoaderThrottle::Delegate* delegate, bool* defer) {
497 delegate->CancelWithError(net::ERR_ACCESS_DENIED);
498 delegate->CancelWithError(net::ERR_UNEXPECTED);
499 }));
500
501 base::RunLoop run_loop;
502 client_.set_on_complete_callback(base::Bind(
503 [](const base::Closure& quit_closure, int error) {
504 EXPECT_EQ(net::ERR_ACCESS_DENIED, error);
505 quit_closure.Run();
506 },
507 run_loop.QuitClosure()));
508
509 CreateLoaderAndStart();
510 throttle_->delegate()->CancelWithError(net::ERR_INVALID_ARGUMENT);
511 run_loop.Run();
512
513 EXPECT_EQ(1u, throttle_->will_start_request_called());
514 EXPECT_EQ(0u, throttle_->will_redirect_request_called());
515 EXPECT_EQ(0u, throttle_->will_process_response_called());
516
517 EXPECT_EQ(0u, factory_.create_loader_and_start_called());
518
519 EXPECT_EQ(0u, client_.on_received_response_called());
520 EXPECT_EQ(0u, client_.on_received_redirect_called());
521 EXPECT_EQ(1u, client_.on_complete_called());
522 }
523
524 TEST_F(ThrottlingURLLoaderTest, ResumeNoOpIfAlreadyCanceled) {
525 throttle_->set_will_process_response_callback(
526 base::Bind([](URLLoaderThrottle::Delegate* delegate, bool* defer) {
527 delegate->CancelWithError(net::ERR_ACCESS_DENIED);
528 delegate->Resume();
529 }));
530
531 base::RunLoop run_loop1;
532 client_.set_on_complete_callback(base::Bind(
533 [](const base::Closure& quit_closure, int error) {
534 EXPECT_EQ(net::ERR_ACCESS_DENIED, error);
535 quit_closure.Run();
536 },
537 run_loop1.QuitClosure()));
538
539 CreateLoaderAndStart();
540
541 factory_.NotifyClientOnReceiveResponse();
542
543 run_loop1.Run();
544
545 throttle_->delegate()->Resume();
546
547 base::RunLoop run_loop2;
548 run_loop2.RunUntilIdle();
549
550 EXPECT_EQ(1u, throttle_->will_start_request_called());
551 EXPECT_EQ(0u, throttle_->will_redirect_request_called());
552 EXPECT_EQ(1u, throttle_->will_process_response_called());
553
554 EXPECT_EQ(0u, client_.on_received_response_called());
555 EXPECT_EQ(0u, client_.on_received_redirect_called());
556 EXPECT_EQ(1u, client_.on_complete_called());
557 }
558
559 } // namespace
560 } // namespace content
OLDNEW
« no previous file with comments | « content/child/throttling_url_loader.cc ('k') | content/common/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698