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

Side by Side Diff: net/proxy/proxy_service_unittest.cc

Issue 149525: Remove the concept of threading from ProxyService, and move it into the Proxy... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Add missing header for mac Created 11 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « net/proxy/proxy_service.cc ('k') | net/proxy/single_threaded_proxy_resolver.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 "base/compiler_specific.h" 5 #include "base/compiler_specific.h"
6 #include "googleurl/src/gurl.h" 6 #include "googleurl/src/gurl.h"
7 #include "net/base/net_errors.h" 7 #include "net/base/net_errors.h"
8 #include "net/proxy/single_threaded_proxy_resolver.h"
8 #include "net/proxy/proxy_config_service.h" 9 #include "net/proxy/proxy_config_service.h"
9 #include "net/proxy/proxy_resolver.h" 10 #include "net/proxy/proxy_resolver.h"
10 #include "net/proxy/proxy_script_fetcher.h" 11 #include "net/proxy/proxy_script_fetcher.h"
11 #include "net/proxy/proxy_service.h" 12 #include "net/proxy/proxy_service.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace { 15 namespace {
15 16
16 class MockProxyConfigService: public net::ProxyConfigService { 17 class MockProxyConfigService: public net::ProxyConfigService {
17 public: 18 public:
18 MockProxyConfigService() {} // Direct connect. 19 MockProxyConfigService() {} // Direct connect.
19 explicit MockProxyConfigService(const net::ProxyConfig& pc) : config(pc) {} 20 explicit MockProxyConfigService(const net::ProxyConfig& pc) : config(pc) {}
20 explicit MockProxyConfigService(const std::string& pac_url) { 21 explicit MockProxyConfigService(const std::string& pac_url) {
21 config.pac_url = GURL(pac_url); 22 config.pac_url = GURL(pac_url);
22 } 23 }
23 24
24 virtual int GetProxyConfig(net::ProxyConfig* results) { 25 virtual int GetProxyConfig(net::ProxyConfig* results) {
25 *results = config; 26 *results = config;
26 return net::OK; 27 return net::OK;
27 } 28 }
28 29
29 net::ProxyConfig config; 30 net::ProxyConfig config;
30 }; 31 };
31 32
32 class MockProxyResolver : public net::ProxyResolver { 33 class SyncMockProxyResolver : public net::ProxyResolver {
33 public: 34 public:
34 MockProxyResolver() : net::ProxyResolver(true), 35 SyncMockProxyResolver() : net::ProxyResolver(false /*expects_pac_bytes*/),
35 fail_get_proxy_for_url(false) { 36 fail_get_proxy_for_url(false) {
36 } 37 }
37 38
39 // ProxyResolver implementation:
38 virtual int GetProxyForURL(const GURL& query_url, 40 virtual int GetProxyForURL(const GURL& query_url,
39 const GURL& pac_url, 41 net::ProxyInfo* results,
40 net::ProxyInfo* results) { 42 net::CompletionCallback* callback,
43 RequestHandle* request) {
41 if (fail_get_proxy_for_url) 44 if (fail_get_proxy_for_url)
42 return net::ERR_FAILED; 45 return net::ERR_FAILED;
43 if (GURL(query_url).host() == info_predicate_query_host) { 46 if (GURL(query_url).host() == info_predicate_query_host) {
44 results->Use(info); 47 results->Use(info);
45 } else { 48 } else {
46 results->UseDirect(); 49 results->UseDirect();
47 } 50 }
48 return net::OK; 51 return net::OK;
49 } 52 }
50 53
54 virtual void CancelRequest(RequestHandle request) {
55 NOTREACHED();
56 }
57
58 virtual void SetPacScriptByUrlInternal(const GURL& pac_url) {}
59
60
51 net::ProxyInfo info; 61 net::ProxyInfo info;
52 62
53 // info is only returned if query_url in GetProxyForURL matches this: 63 // info is only returned if query_url in GetProxyForURL matches this:
54 std::string info_predicate_query_host; 64 std::string info_predicate_query_host;
55 65
56 // If true, then GetProxyForURL will fail, which simulates failure to 66 // If true, then GetProxyForURL will fail, which simulates failure to
57 // download or execute the PAC file. 67 // download or execute the PAC file.
58 bool fail_get_proxy_for_url; 68 bool fail_get_proxy_for_url;
59 }; 69 };
60 70
71 class MockProxyResolver : public net::SingleThreadedProxyResolver {
72 public:
73 MockProxyResolver()
74 : net::SingleThreadedProxyResolver(new SyncMockProxyResolver) {
75 x = reinterpret_cast<SyncMockProxyResolver*>(resolver());
76 }
77
78 // TODO(eroman): cleanup.
79 SyncMockProxyResolver* x;
80 };
81
61 // ResultFuture is a handle to get at the result from 82 // ResultFuture is a handle to get at the result from
62 // ProxyService::ResolveProxyForURL() that ran on another thread. 83 // ProxyService::ResolveProxyForURL() that ran on another thread.
63 class ResultFuture : public base::RefCountedThreadSafe<ResultFuture> { 84 class ResultFuture : public base::RefCountedThreadSafe<ResultFuture> {
64 public: 85 public:
65 // |service| is the ProxyService to issue requests on, and |io_message_loop| 86 // |service| is the ProxyService to issue requests on, and |io_message_loop|
66 // is the message loop where ProxyService lives. 87 // is the message loop where ProxyService lives.
67 ResultFuture(MessageLoop* io_message_loop, 88 ResultFuture(MessageLoop* io_message_loop,
68 net::ProxyService* service) 89 net::ProxyService* service)
69 : io_message_loop_(io_message_loop), 90 : io_message_loop_(io_message_loop),
70 service_(service), 91 service_(service),
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 scoped_refptr<ResultFuture> result; 340 scoped_refptr<ResultFuture> result;
320 service_.ResetConfigService(&result, new_proxy_config_service); 341 service_.ResetConfigService(&result, new_proxy_config_service);
321 return result->GetResultCode(); 342 return result->GetResultCode();
322 } 343 }
323 344
324 private: 345 private:
325 ProxyServiceWithFutures service_; 346 ProxyServiceWithFutures service_;
326 }; 347 };
327 348
328 // A ProxyResolver which can be set to block upon reaching GetProxyForURL. 349 // A ProxyResolver which can be set to block upon reaching GetProxyForURL.
329 class BlockableProxyResolver : public net::ProxyResolver { 350 class SyncBlockableProxyResolver : public net::ProxyResolver {
330 public: 351 public:
331 BlockableProxyResolver() : net::ProxyResolver(true), 352 SyncBlockableProxyResolver()
332 should_block_(false), 353 : net::ProxyResolver(false /*expects_pac_bytes*/),
333 unblocked_(true, true), 354 should_block_(false),
334 blocked_(true, false) { 355 unblocked_(true, true),
356 blocked_(true, false) {
335 } 357 }
336 358
337 void Block() { 359 void Block() {
338 should_block_ = true; 360 should_block_ = true;
339 unblocked_.Reset(); 361 unblocked_.Reset();
340 } 362 }
341 363
342 void Unblock() { 364 void Unblock() {
343 should_block_ = false; 365 should_block_ = false;
344 blocked_.Reset(); 366 blocked_.Reset();
345 unblocked_.Signal(); 367 unblocked_.Signal();
346 } 368 }
347 369
348 void WaitUntilBlocked() { 370 void WaitUntilBlocked() {
349 blocked_.Wait(); 371 blocked_.Wait();
350 } 372 }
351 373
352 // net::ProxyResolver implementation: 374 // net::ProxyResolver implementation:
353 virtual int GetProxyForURL(const GURL& query_url, 375 virtual int GetProxyForURL(const GURL& query_url,
354 const GURL& pac_url, 376 net::ProxyInfo* results,
355 net::ProxyInfo* results) { 377 net::CompletionCallback* callback,
378 RequestHandle* request) {
356 if (should_block_) { 379 if (should_block_) {
357 blocked_.Signal(); 380 blocked_.Signal();
358 unblocked_.Wait(); 381 unblocked_.Wait();
359 } 382 }
360 383
361 results->UseNamedProxy(query_url.host()); 384 results->UseNamedProxy(query_url.host());
362 return net::OK; 385 return net::OK;
363 } 386 }
364 387
388 virtual void CancelRequest(RequestHandle request) {
389 NOTREACHED();
390 }
391
392 virtual void SetPacScriptByUrlInternal(const GURL& pac_url) {}
393
365 private: 394 private:
366 bool should_block_; 395 bool should_block_;
367 base::WaitableEvent unblocked_; 396 base::WaitableEvent unblocked_;
368 base::WaitableEvent blocked_; 397 base::WaitableEvent blocked_;
369 }; 398 };
370 399
400 class BlockableProxyResolver : public net::SingleThreadedProxyResolver {
401 public:
402 BlockableProxyResolver()
403 : net::SingleThreadedProxyResolver(new SyncBlockableProxyResolver) {
404 x = reinterpret_cast<SyncBlockableProxyResolver*>(resolver());
405 }
406
407 // TODO(eroman): cleanup.
408 SyncBlockableProxyResolver* x;
409 };
410
371 // A mock ProxyResolverWithoutFetch which concatenates the query's host with 411 // A mock ProxyResolverWithoutFetch which concatenates the query's host with
372 // the last download PAC contents. This way the result describes what the last 412 // the last download PAC contents. This way the result describes what the last
373 // downloaded PAC script's contents were, in addition to the query url itself. 413 // downloaded PAC script's contents were, in addition to the query url itself.
374 class MockProxyResolverWithoutFetch : public net::ProxyResolver { 414 class SyncMockProxyResolverWithoutFetch : public net::ProxyResolver {
375 public: 415 public:
376 MockProxyResolverWithoutFetch() : net::ProxyResolver(false), 416 SyncMockProxyResolverWithoutFetch()
377 last_pac_contents_("NONE") {} 417 : net::ProxyResolver(true /*expects_pac_bytes*/),
418 last_pac_contents_("NONE") {}
378 419
379 // net::ProxyResolver implementation: 420 // net::ProxyResolver implementation:
380 virtual int GetProxyForURL(const GURL& query_url, 421 virtual int GetProxyForURL(const GURL& query_url,
381 const GURL& pac_url, 422 net::ProxyInfo* results,
382 net::ProxyInfo* results) { 423 net::CompletionCallback* callback,
424 RequestHandle* request) {
383 results->UseNamedProxy(last_pac_contents_ + "." + query_url.host()); 425 results->UseNamedProxy(last_pac_contents_ + "." + query_url.host());
384 return net::OK; 426 return net::OK;
385 } 427 }
386 428
387 virtual void SetPacScript(const std::string& bytes) { 429 virtual void SetPacScriptByDataInternal(const std::string& bytes) {
388 last_pac_contents_ = bytes; 430 last_pac_contents_ = bytes;
389 } 431 }
390 432
433 virtual void CancelRequest(RequestHandle request) {
434 NOTREACHED();
435 }
436
391 private: 437 private:
392 std::string last_pac_contents_; 438 std::string last_pac_contents_;
393 }; 439 };
394 440
441 class MockProxyResolverWithoutFetch : public net::SingleThreadedProxyResolver {
442 public:
443 MockProxyResolverWithoutFetch()
444 : net::SingleThreadedProxyResolver(
445 new SyncMockProxyResolverWithoutFetch) {
446 x = reinterpret_cast<SyncMockProxyResolverWithoutFetch*>(resolver());
447 }
448
449 // TODO(eroman): cleanup.
450 SyncMockProxyResolverWithoutFetch* x;
451 };
452
453
395 } // namespace 454 } // namespace
396 455
397 // A mock ProxyScriptFetcher. No result will be returned to the fetch client 456 // A mock ProxyScriptFetcher. No result will be returned to the fetch client
398 // until we call NotifyFetchCompletion() to set the results. 457 // until we call NotifyFetchCompletion() to set the results.
399 class MockProxyScriptFetcher : public net::ProxyScriptFetcher { 458 class MockProxyScriptFetcher : public net::ProxyScriptFetcher {
400 public: 459 public:
401 MockProxyScriptFetcher() : pending_request_loop_(NULL), 460 MockProxyScriptFetcher() : pending_request_loop_(NULL),
402 pending_request_callback_(NULL), pending_request_bytes_(NULL) {} 461 pending_request_callback_(NULL), pending_request_bytes_(NULL) {}
403 462
404 // net::ProxyScriptFetcher implementation. 463 // net::ProxyScriptFetcher implementation.
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 int rv = service.ResolveProxy(url, &info); 517 int rv = service.ResolveProxy(url, &info);
459 EXPECT_EQ(rv, net::OK); 518 EXPECT_EQ(rv, net::OK);
460 EXPECT_TRUE(info.is_direct()); 519 EXPECT_TRUE(info.is_direct());
461 } 520 }
462 521
463 TEST(ProxyServiceTest, PAC) { 522 TEST(ProxyServiceTest, PAC) {
464 MockProxyConfigService* config_service = 523 MockProxyConfigService* config_service =
465 new MockProxyConfigService("http://foopy/proxy.pac"); 524 new MockProxyConfigService("http://foopy/proxy.pac");
466 525
467 MockProxyResolver* resolver = new MockProxyResolver; 526 MockProxyResolver* resolver = new MockProxyResolver;
468 resolver->info.UseNamedProxy("foopy"); 527 resolver->x->info.UseNamedProxy("foopy");
469 resolver->info_predicate_query_host = "www.google.com"; 528 resolver->x->info_predicate_query_host = "www.google.com";
470 529
471 SyncProxyService service(config_service, resolver); 530 SyncProxyService service(config_service, resolver);
472 531
473 GURL url("http://www.google.com/"); 532 GURL url("http://www.google.com/");
474 533
475 net::ProxyInfo info; 534 net::ProxyInfo info;
476 int rv = service.ResolveProxy(url, &info); 535 int rv = service.ResolveProxy(url, &info);
477 EXPECT_EQ(rv, net::OK); 536 EXPECT_EQ(rv, net::OK);
478 EXPECT_FALSE(info.is_direct()); 537 EXPECT_FALSE(info.is_direct());
479 EXPECT_EQ("foopy:80", info.proxy_server().ToURI()); 538 EXPECT_EQ("foopy:80", info.proxy_server().ToURI());
480 } 539 }
481 540
482 TEST(ProxyServiceTest, PAC_FailoverToDirect) { 541 TEST(ProxyServiceTest, PAC_FailoverToDirect) {
483 MockProxyConfigService* config_service = 542 MockProxyConfigService* config_service =
484 new MockProxyConfigService("http://foopy/proxy.pac"); 543 new MockProxyConfigService("http://foopy/proxy.pac");
485 544
486 MockProxyResolver* resolver = new MockProxyResolver; 545 MockProxyResolver* resolver = new MockProxyResolver;
487 resolver->info.UseNamedProxy("foopy:8080"); 546 resolver->x->info.UseNamedProxy("foopy:8080");
488 resolver->info_predicate_query_host = "www.google.com"; 547 resolver->x->info_predicate_query_host = "www.google.com";
489 548
490 SyncProxyService service(config_service, resolver); 549 SyncProxyService service(config_service, resolver);
491 550
492 GURL url("http://www.google.com/"); 551 GURL url("http://www.google.com/");
493 552
494 net::ProxyInfo info; 553 net::ProxyInfo info;
495 int rv = service.ResolveProxy(url, &info); 554 int rv = service.ResolveProxy(url, &info);
496 EXPECT_EQ(rv, net::OK); 555 EXPECT_EQ(rv, net::OK);
497 EXPECT_FALSE(info.is_direct()); 556 EXPECT_FALSE(info.is_direct());
498 EXPECT_EQ("foopy:8080", info.proxy_server().ToURI()); 557 EXPECT_EQ("foopy:8080", info.proxy_server().ToURI());
499 558
500 // Now, imagine that connecting to foopy:8080 fails. 559 // Now, imagine that connecting to foopy:8080 fails.
501 rv = service.ReconsiderProxyAfterError(url, &info); 560 rv = service.ReconsiderProxyAfterError(url, &info);
502 EXPECT_EQ(rv, net::OK); 561 EXPECT_EQ(rv, net::OK);
503 EXPECT_TRUE(info.is_direct()); 562 EXPECT_TRUE(info.is_direct());
504 } 563 }
505 564
506 TEST(ProxyServiceTest, PAC_FailsToDownload) { 565 TEST(ProxyServiceTest, PAC_FailsToDownload) {
507 // Test what happens when we fail to download the PAC URL. 566 // Test what happens when we fail to download the PAC URL.
508 567
509 MockProxyConfigService* config_service = 568 MockProxyConfigService* config_service =
510 new MockProxyConfigService("http://foopy/proxy.pac"); 569 new MockProxyConfigService("http://foopy/proxy.pac");
511 570
512 MockProxyResolver* resolver = new MockProxyResolver; 571 MockProxyResolver* resolver = new MockProxyResolver;
513 resolver->info.UseNamedProxy("foopy:8080"); 572 resolver->x->info.UseNamedProxy("foopy:8080");
514 resolver->info_predicate_query_host = "www.google.com"; 573 resolver->x->info_predicate_query_host = "www.google.com";
515 resolver->fail_get_proxy_for_url = true; 574 resolver->x->fail_get_proxy_for_url = true;
516 575
517 SyncProxyService service(config_service, resolver); 576 SyncProxyService service(config_service, resolver);
518 577
519 // The first resolve fails in the MockProxyResolver. 578 // The first resolve fails in the MockProxyResolver.
520 GURL url("http://www.google.com/"); 579 GURL url("http://www.google.com/");
521 net::ProxyInfo info; 580 net::ProxyInfo info;
522 int rv = service.ResolveProxy(url, &info); 581 int rv = service.ResolveProxy(url, &info);
523 EXPECT_EQ(rv, net::ERR_FAILED); 582 EXPECT_EQ(rv, net::ERR_FAILED);
524 583
525 // The second resolve request will automatically select direct connect, 584 // The second resolve request will automatically select direct connect,
526 // because it has cached the configuration as being bad. 585 // because it has cached the configuration as being bad.
527 rv = service.ResolveProxy(url, &info); 586 rv = service.ResolveProxy(url, &info);
528 EXPECT_EQ(rv, net::OK); 587 EXPECT_EQ(rv, net::OK);
529 EXPECT_TRUE(info.is_direct()); 588 EXPECT_TRUE(info.is_direct());
530 589
531 resolver->fail_get_proxy_for_url = false; 590 resolver->x->fail_get_proxy_for_url = false;
532 resolver->info.UseNamedProxy("foopy_valid:8080"); 591 resolver->x->info.UseNamedProxy("foopy_valid:8080");
533 592
534 // But, if that fails, then we should give the proxy config another shot 593 // But, if that fails, then we should give the proxy config another shot
535 // since we have never tried it with this URL before. 594 // since we have never tried it with this URL before.
536 rv = service.ReconsiderProxyAfterError(url, &info); 595 rv = service.ReconsiderProxyAfterError(url, &info);
537 EXPECT_EQ(rv, net::OK); 596 EXPECT_EQ(rv, net::OK);
538 EXPECT_FALSE(info.is_direct()); 597 EXPECT_FALSE(info.is_direct());
539 EXPECT_EQ("foopy_valid:8080", info.proxy_server().ToURI()); 598 EXPECT_EQ("foopy_valid:8080", info.proxy_server().ToURI());
540 } 599 }
541 600
542 TEST(ProxyServiceTest, ProxyFallback) { 601 TEST(ProxyServiceTest, ProxyFallback) {
543 // Test what happens when we specify multiple proxy servers and some of them 602 // Test what happens when we specify multiple proxy servers and some of them
544 // are bad. 603 // are bad.
545 604
546 MockProxyConfigService* config_service = 605 MockProxyConfigService* config_service =
547 new MockProxyConfigService("http://foopy/proxy.pac"); 606 new MockProxyConfigService("http://foopy/proxy.pac");
548 607
549 MockProxyResolver* resolver = new MockProxyResolver; 608 MockProxyResolver* resolver = new MockProxyResolver;
550 resolver->info.UseNamedProxy("foopy1:8080;foopy2:9090"); 609 resolver->x->info.UseNamedProxy("foopy1:8080;foopy2:9090");
551 resolver->info_predicate_query_host = "www.google.com"; 610 resolver->x->info_predicate_query_host = "www.google.com";
552 resolver->fail_get_proxy_for_url = false; 611 resolver->x->fail_get_proxy_for_url = false;
553 612
554 SyncProxyService service(config_service, resolver); 613 SyncProxyService service(config_service, resolver);
555 614
556 GURL url("http://www.google.com/"); 615 GURL url("http://www.google.com/");
557 616
558 // Get the proxy information. 617 // Get the proxy information.
559 net::ProxyInfo info; 618 net::ProxyInfo info;
560 int rv = service.ResolveProxy(url, &info); 619 int rv = service.ResolveProxy(url, &info);
561 EXPECT_EQ(rv, net::OK); 620 EXPECT_EQ(rv, net::OK);
562 EXPECT_FALSE(info.is_direct()); 621 EXPECT_FALSE(info.is_direct());
563 622
564 // The first item is valid. 623 // The first item is valid.
565 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); 624 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
566 625
567 // Fake an error on the proxy. 626 // Fake an error on the proxy.
568 rv = service.ReconsiderProxyAfterError(url, &info); 627 rv = service.ReconsiderProxyAfterError(url, &info);
569 EXPECT_EQ(rv, net::OK); 628 EXPECT_EQ(rv, net::OK);
570 629
571 // The second proxy should be specified. 630 // The second proxy should be specified.
572 EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI()); 631 EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI());
573 632
574 // Create a new resolver that returns 3 proxies. The second one is already 633 // Create a new resolver that returns 3 proxies. The second one is already
575 // known to be bad. 634 // known to be bad.
576 config_service->config.pac_url = GURL("http://foopy/proxy.pac"); 635 config_service->config.pac_url = GURL("http://foopy/proxy.pac");
577 resolver->info.UseNamedProxy("foopy3:7070;foopy1:8080;foopy2:9090"); 636 resolver->x->info.UseNamedProxy("foopy3:7070;foopy1:8080;foopy2:9090");
578 resolver->info_predicate_query_host = "www.google.com"; 637 resolver->x->info_predicate_query_host = "www.google.com";
579 resolver->fail_get_proxy_for_url = false; 638 resolver->x->fail_get_proxy_for_url = false;
580 639
581 rv = service.ResolveProxy(url, &info); 640 rv = service.ResolveProxy(url, &info);
582 EXPECT_EQ(rv, net::OK); 641 EXPECT_EQ(rv, net::OK);
583 EXPECT_FALSE(info.is_direct()); 642 EXPECT_FALSE(info.is_direct());
584 EXPECT_EQ("foopy3:7070", info.proxy_server().ToURI()); 643 EXPECT_EQ("foopy3:7070", info.proxy_server().ToURI());
585 644
586 // We fake another error. It should now try the third one. 645 // We fake another error. It should now try the third one.
587 rv = service.ReconsiderProxyAfterError(url, &info); 646 rv = service.ReconsiderProxyAfterError(url, &info);
588 EXPECT_EQ(rv, net::OK); 647 EXPECT_EQ(rv, net::OK);
589 EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI()); 648 EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI());
(...skipping 10 matching lines...) Expand all
600 // TODO(nsylvain): Test that the proxy can be retried after the delay. 659 // TODO(nsylvain): Test that the proxy can be retried after the delay.
601 } 660 }
602 661
603 TEST(ProxyServiceTest, ProxyFallback_NewSettings) { 662 TEST(ProxyServiceTest, ProxyFallback_NewSettings) {
604 // Test proxy failover when new settings are available. 663 // Test proxy failover when new settings are available.
605 664
606 MockProxyConfigService* config_service = 665 MockProxyConfigService* config_service =
607 new MockProxyConfigService("http://foopy/proxy.pac"); 666 new MockProxyConfigService("http://foopy/proxy.pac");
608 667
609 MockProxyResolver* resolver = new MockProxyResolver; 668 MockProxyResolver* resolver = new MockProxyResolver;
610 resolver->info.UseNamedProxy("foopy1:8080;foopy2:9090"); 669 resolver->x->info.UseNamedProxy("foopy1:8080;foopy2:9090");
611 resolver->info_predicate_query_host = "www.google.com"; 670 resolver->x->info_predicate_query_host = "www.google.com";
612 resolver->fail_get_proxy_for_url = false; 671 resolver->x->fail_get_proxy_for_url = false;
613 672
614 SyncProxyService service(config_service, resolver); 673 SyncProxyService service(config_service, resolver);
615 674
616 GURL url("http://www.google.com/"); 675 GURL url("http://www.google.com/");
617 676
618 // Get the proxy information. 677 // Get the proxy information.
619 net::ProxyInfo info; 678 net::ProxyInfo info;
620 int rv = service.ResolveProxy(url, &info); 679 int rv = service.ResolveProxy(url, &info);
621 EXPECT_EQ(rv, net::OK); 680 EXPECT_EQ(rv, net::OK);
622 EXPECT_FALSE(info.is_direct()); 681 EXPECT_FALSE(info.is_direct());
(...skipping 26 matching lines...) Expand all
649 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); 708 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
650 } 709 }
651 710
652 TEST(ProxyServiceTest, ProxyFallback_BadConfig) { 711 TEST(ProxyServiceTest, ProxyFallback_BadConfig) {
653 // Test proxy failover when the configuration is bad. 712 // Test proxy failover when the configuration is bad.
654 713
655 MockProxyConfigService* config_service = 714 MockProxyConfigService* config_service =
656 new MockProxyConfigService("http://foopy/proxy.pac"); 715 new MockProxyConfigService("http://foopy/proxy.pac");
657 716
658 MockProxyResolver* resolver = new MockProxyResolver; 717 MockProxyResolver* resolver = new MockProxyResolver;
659 resolver->info.UseNamedProxy("foopy1:8080;foopy2:9090"); 718 resolver->x->info.UseNamedProxy("foopy1:8080;foopy2:9090");
660 resolver->info_predicate_query_host = "www.google.com"; 719 resolver->x->info_predicate_query_host = "www.google.com";
661 resolver->fail_get_proxy_for_url = false; 720 resolver->x->fail_get_proxy_for_url = false;
662 721
663 SyncProxyService service(config_service, resolver); 722 SyncProxyService service(config_service, resolver);
664 723
665 GURL url("http://www.google.com/"); 724 GURL url("http://www.google.com/");
666 725
667 // Get the proxy information. 726 // Get the proxy information.
668 net::ProxyInfo info; 727 net::ProxyInfo info;
669 int rv = service.ResolveProxy(url, &info); 728 int rv = service.ResolveProxy(url, &info);
670 EXPECT_EQ(rv, net::OK); 729 EXPECT_EQ(rv, net::OK);
671 EXPECT_FALSE(info.is_direct()); 730 EXPECT_FALSE(info.is_direct());
672 731
673 // The first item is valid. 732 // The first item is valid.
674 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); 733 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
675 734
676 // Fake a proxy error. 735 // Fake a proxy error.
677 rv = service.ReconsiderProxyAfterError(url, &info); 736 rv = service.ReconsiderProxyAfterError(url, &info);
678 EXPECT_EQ(rv, net::OK); 737 EXPECT_EQ(rv, net::OK);
679 738
680 // The first proxy is ignored, and the second one is selected. 739 // The first proxy is ignored, and the second one is selected.
681 EXPECT_FALSE(info.is_direct()); 740 EXPECT_FALSE(info.is_direct());
682 EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI()); 741 EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI());
683 742
684 // Fake a PAC failure. 743 // Fake a PAC failure.
685 net::ProxyInfo info2; 744 net::ProxyInfo info2;
686 resolver->fail_get_proxy_for_url = true; 745 resolver->x->fail_get_proxy_for_url = true;
687 rv = service.ResolveProxy(url, &info2); 746 rv = service.ResolveProxy(url, &info2);
688 EXPECT_EQ(rv, net::ERR_FAILED); 747 EXPECT_EQ(rv, net::ERR_FAILED);
689 748
690 // No proxy servers are returned. It's a direct connection. 749 // No proxy servers are returned. It's a direct connection.
691 EXPECT_TRUE(info2.is_direct()); 750 EXPECT_TRUE(info2.is_direct());
692 751
693 // The PAC is now fixed and will return a proxy server. 752 // The PAC is now fixed and will return a proxy server.
694 // It should also clear the list of bad proxies. 753 // It should also clear the list of bad proxies.
695 resolver->fail_get_proxy_for_url = false; 754 resolver->x->fail_get_proxy_for_url = false;
696 755
697 // Try to resolve, it will still return "direct" because we have no reason 756 // Try to resolve, it will still return "direct" because we have no reason
698 // to check the config since everything works. 757 // to check the config since everything works.
699 net::ProxyInfo info3; 758 net::ProxyInfo info3;
700 rv = service.ResolveProxy(url, &info3); 759 rv = service.ResolveProxy(url, &info3);
701 EXPECT_EQ(rv, net::OK); 760 EXPECT_EQ(rv, net::OK);
702 EXPECT_TRUE(info3.is_direct()); 761 EXPECT_TRUE(info3.is_direct());
703 762
704 // But if the direct connection fails, we check if the ProxyInfo tried to 763 // But if the direct connection fails, we check if the ProxyInfo tried to
705 // resolve the proxy before, and if not (like in this case), we give the 764 // resolve the proxy before, and if not (like in this case), we give the
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
992 // Test cancellation of a queued request. 1051 // Test cancellation of a queued request.
993 TEST(ProxyServiceTest, CancelQueuedRequest) { 1052 TEST(ProxyServiceTest, CancelQueuedRequest) {
994 MockProxyConfigService* config_service = 1053 MockProxyConfigService* config_service =
995 new MockProxyConfigService("http://foopy/proxy.pac"); 1054 new MockProxyConfigService("http://foopy/proxy.pac");
996 1055
997 BlockableProxyResolver* resolver = new BlockableProxyResolver; 1056 BlockableProxyResolver* resolver = new BlockableProxyResolver;
998 1057
999 ProxyServiceWithFutures service(config_service, resolver); 1058 ProxyServiceWithFutures service(config_service, resolver);
1000 1059
1001 // Cause requests to pile up, by having them block in the PAC thread. 1060 // Cause requests to pile up, by having them block in the PAC thread.
1002 resolver->Block(); 1061 resolver->x->Block();
1003 1062
1004 // Start 3 requests. 1063 // Start 3 requests.
1005 scoped_refptr<ResultFuture> result1; 1064 scoped_refptr<ResultFuture> result1;
1006 service.ResolveProxy(&result1, GURL("http://request1")); 1065 service.ResolveProxy(&result1, GURL("http://request1"));
1007 1066
1008 scoped_refptr<ResultFuture> result2; 1067 scoped_refptr<ResultFuture> result2;
1009 service.ResolveProxy(&result2, GURL("http://request2")); 1068 service.ResolveProxy(&result2, GURL("http://request2"));
1010 1069
1011 scoped_refptr<ResultFuture> result3; 1070 scoped_refptr<ResultFuture> result3;
1012 service.ResolveProxy(&result3, GURL("http://request3")); 1071 service.ResolveProxy(&result3, GURL("http://request3"));
1013 1072
1014 // Wait until the first request has become blocked in the PAC thread. 1073 // Wait until the first request has become blocked in the PAC thread.
1015 resolver->WaitUntilBlocked(); 1074 resolver->x->WaitUntilBlocked();
1016 1075
1017 // Cancel the second request 1076 // Cancel the second request
1018 result2->Cancel(); 1077 result2->Cancel();
1019 1078
1020 // Unblock the PAC thread. 1079 // Unblock the PAC thread.
1021 resolver->Unblock(); 1080 resolver->x->Unblock();
1022 1081
1023 // Wait for the final request to complete. 1082 // Wait for the final request to complete.
1024 result3->WaitUntilCompleted(); 1083 result3->WaitUntilCompleted();
1025 1084
1026 // Verify that requests ran as expected. 1085 // Verify that requests ran as expected.
1027 1086
1028 EXPECT_TRUE(result1->IsCompleted()); 1087 EXPECT_TRUE(result1->IsCompleted());
1029 EXPECT_EQ(net::OK, result1->GetResultCode()); 1088 EXPECT_EQ(net::OK, result1->GetResultCode());
1030 EXPECT_EQ("request1:80", result1->GetProxyInfo().proxy_server().ToURI()); 1089 EXPECT_EQ("request1:80", result1->GetProxyInfo().proxy_server().ToURI());
1031 1090
1032 EXPECT_FALSE(result2->IsCompleted()); // Cancelled. 1091 EXPECT_FALSE(result2->IsCompleted()); // Cancelled.
1033 1092
1034 EXPECT_TRUE(result3->IsCompleted()); 1093 EXPECT_TRUE(result3->IsCompleted());
1035 EXPECT_EQ(net::OK, result3->GetResultCode()); 1094 EXPECT_EQ(net::OK, result3->GetResultCode());
1036 EXPECT_EQ("request3:80", result3->GetProxyInfo().proxy_server().ToURI()); 1095 EXPECT_EQ("request3:80", result3->GetProxyInfo().proxy_server().ToURI());
1037 } 1096 }
1038 1097
1039 // Test cancellation of an in-progress request. 1098 // Test cancellation of an in-progress request.
1040 TEST(ProxyServiceTest, CancelInprogressRequest) { 1099 TEST(ProxyServiceTest, CancelInprogressRequest) {
1041 MockProxyConfigService* config_service = 1100 MockProxyConfigService* config_service =
1042 new MockProxyConfigService("http://foopy/proxy.pac"); 1101 new MockProxyConfigService("http://foopy/proxy.pac");
1043 1102
1044 BlockableProxyResolver* resolver = new BlockableProxyResolver; 1103 BlockableProxyResolver* resolver = new BlockableProxyResolver;
1045 1104
1046 ProxyServiceWithFutures service(config_service, resolver); 1105 ProxyServiceWithFutures service(config_service, resolver);
1047 1106
1048 // Cause requests to pile up, by having them block in the PAC thread. 1107 // Cause requests to pile up, by having them block in the PAC thread.
1049 resolver->Block(); 1108 resolver->x->Block();
1050 1109
1051 // Start 3 requests. 1110 // Start 3 requests.
1052 scoped_refptr<ResultFuture> result1; 1111 scoped_refptr<ResultFuture> result1;
1053 service.ResolveProxy(&result1, GURL("http://request1")); 1112 service.ResolveProxy(&result1, GURL("http://request1"));
1054 1113
1055 scoped_refptr<ResultFuture> result2; 1114 scoped_refptr<ResultFuture> result2;
1056 service.ResolveProxy(&result2, GURL("http://request2")); 1115 service.ResolveProxy(&result2, GURL("http://request2"));
1057 1116
1058 scoped_refptr<ResultFuture> result3; 1117 scoped_refptr<ResultFuture> result3;
1059 service.ResolveProxy(&result3, GURL("http://request3")); 1118 service.ResolveProxy(&result3, GURL("http://request3"));
1060 1119
1061 // Wait until the first request has become blocked in the PAC thread. 1120 // Wait until the first request has become blocked in the PAC thread.
1062 resolver->WaitUntilBlocked(); 1121 resolver->x->WaitUntilBlocked();
1063 1122
1064 // Cancel the first request 1123 // Cancel the first request
1065 result1->Cancel(); 1124 result1->Cancel();
1066 1125
1067 // Unblock the PAC thread. 1126 // Unblock the PAC thread.
1068 resolver->Unblock(); 1127 resolver->x->Unblock();
1069 1128
1070 // Wait for the final request to complete. 1129 // Wait for the final request to complete.
1071 result3->WaitUntilCompleted(); 1130 result3->WaitUntilCompleted();
1072 1131
1073 // Verify that requests ran as expected. 1132 // Verify that requests ran as expected.
1074 1133
1075 EXPECT_FALSE(result1->IsCompleted()); // Cancelled. 1134 EXPECT_FALSE(result1->IsCompleted()); // Cancelled.
1076 1135
1077 EXPECT_TRUE(result2->IsCompleted()); 1136 EXPECT_TRUE(result2->IsCompleted());
1078 EXPECT_EQ(net::OK, result2->GetResultCode()); 1137 EXPECT_EQ(net::OK, result2->GetResultCode());
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1185 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); 1244 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
1186 1245
1187 net::ProxyConfig config2; 1246 net::ProxyConfig config2;
1188 config2.proxy_rules.ParseFromString("foopy2:8080"); 1247 config2.proxy_rules.ParseFromString("foopy2:8080");
1189 config2.auto_detect = false; 1248 config2.auto_detect = false;
1190 int result = service->ResetConfigService(new MockProxyConfigService(config2)); 1249 int result = service->ResetConfigService(new MockProxyConfigService(config2));
1191 DCHECK(result == 0); 1250 DCHECK(result == 0);
1192 service->ResolveProxy(GURL("http://request2"), &info); 1251 service->ResolveProxy(GURL("http://request2"), &info);
1193 EXPECT_EQ("foopy2:8080", info.proxy_server().ToURI()); 1252 EXPECT_EQ("foopy2:8080", info.proxy_server().ToURI());
1194 } 1253 }
OLDNEW
« no previous file with comments | « net/proxy/proxy_service.cc ('k') | net/proxy/single_threaded_proxy_resolver.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698