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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
|
// Copyright (c) 2011 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 "net/proxy/proxy_service.h"
#include <vector>
#include "base/format_macros.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "googleurl/src/gurl.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"
#include "net/base/net_log_unittest.h"
#include "net/base/test_completion_callback.h"
#include "net/proxy/mock_proxy_resolver.h"
#include "net/proxy/proxy_config_service.h"
#include "net/proxy/proxy_resolver.h"
#include "net/proxy/proxy_script_fetcher.h"
#include "testing/gtest/include/gtest/gtest.h"
// TODO(eroman): Write a test which exercises
// ProxyService::SuspendAllPendingRequests().
namespace net {
namespace {
class MockProxyConfigService: public ProxyConfigService {
public:
explicit MockProxyConfigService(const ProxyConfig& config)
: availability_(CONFIG_VALID),
config_(config) {
}
explicit MockProxyConfigService(const std::string& pac_url)
: availability_(CONFIG_VALID),
config_(ProxyConfig::CreateFromCustomPacURL(GURL(pac_url))) {
}
virtual void AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
virtual void RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
virtual ConfigAvailability GetLatestProxyConfig(ProxyConfig* results) {
if (availability_ == CONFIG_VALID)
*results = config_;
return availability_;
}
void SetConfig(const ProxyConfig& config) {
availability_ = CONFIG_VALID;
config_ = config;
FOR_EACH_OBSERVER(Observer, observers_,
OnProxyConfigChanged(config_, availability_));
}
private:
ConfigAvailability availability_;
ProxyConfig config_;
ObserverList<Observer, true> observers_;
};
} // namespace
// A mock ProxyScriptFetcher. No result will be returned to the fetch client
// until we call NotifyFetchCompletion() to set the results.
class MockProxyScriptFetcher : public ProxyScriptFetcher {
public:
MockProxyScriptFetcher()
: pending_request_callback_(NULL), pending_request_text_(NULL) {
}
// ProxyScriptFetcher implementation.
virtual int Fetch(const GURL& url,
string16* text,
CompletionCallback* callback) {
DCHECK(!has_pending_request());
// Save the caller's information, and have them wait.
pending_request_url_ = url;
pending_request_callback_ = callback;
pending_request_text_ = text;
return ERR_IO_PENDING;
}
void NotifyFetchCompletion(int result, const std::string& ascii_text) {
DCHECK(has_pending_request());
*pending_request_text_ = ASCIIToUTF16(ascii_text);
CompletionCallback* callback = pending_request_callback_;
pending_request_callback_ = NULL;
callback->Run(result);
}
virtual void Cancel() {}
virtual URLRequestContext* GetRequestContext() { return NULL; }
const GURL& pending_request_url() const {
return pending_request_url_;
}
bool has_pending_request() const {
return pending_request_callback_ != NULL;
}
private:
GURL pending_request_url_;
CompletionCallback* pending_request_callback_;
string16* pending_request_text_;
};
TEST(ProxyServiceTest, Direct) {
MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver;
scoped_refptr<ProxyService> service(
new ProxyService(new MockProxyConfigService(
ProxyConfig::CreateDirect()), resolver, NULL));
GURL url("http://www.google.com/");
ProxyInfo info;
TestCompletionCallback callback;
CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
int rv = service->ResolveProxy(url, &info, &callback, NULL, log.bound());
EXPECT_EQ(OK, rv);
EXPECT_TRUE(resolver->pending_requests().empty());
EXPECT_TRUE(info.is_direct());
// Check the NetLog was filled correctly.
CapturingNetLog::EntryList entries;
log.GetEntries(&entries);
EXPECT_EQ(3u, entries.size());
EXPECT_TRUE(LogContainsBeginEvent(
entries, 0, NetLog::TYPE_PROXY_SERVICE));
EXPECT_TRUE(LogContainsEvent(
entries, 1, NetLog::TYPE_PROXY_SERVICE_RESOLVED_PROXY_LIST,
NetLog::PHASE_NONE));
EXPECT_TRUE(LogContainsEndEvent(
entries, 2, NetLog::TYPE_PROXY_SERVICE));
}
TEST(ProxyServiceTest, PAC) {
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
GURL url("http://www.google.com/");
ProxyInfo info;
TestCompletionCallback callback;
CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
int rv = service->ResolveProxy(url, &info, &callback, NULL, log.bound());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(GURL("http://foopy/proxy.pac"),
resolver->pending_set_pac_script_request()->script_data()->url());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(url, resolver->pending_requests()[0]->url());
// Set the result in proxy resolver.
resolver->pending_requests()[0]->results()->UseNamedProxy("foopy");
resolver->pending_requests()[0]->CompleteNow(OK);
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foopy:80", info.proxy_server().ToURI());
// Check the NetLog was filled correctly.
CapturingNetLog::EntryList entries;
log.GetEntries(&entries);
EXPECT_EQ(5u, entries.size());
EXPECT_TRUE(LogContainsBeginEvent(
entries, 0, NetLog::TYPE_PROXY_SERVICE));
EXPECT_TRUE(LogContainsBeginEvent(
entries, 1, NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC));
EXPECT_TRUE(LogContainsEndEvent(
entries, 2, NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC));
EXPECT_TRUE(LogContainsEndEvent(
entries, 4, NetLog::TYPE_PROXY_SERVICE));
}
// Test that the proxy resolver does not see the URL's username/password
// or its reference section.
TEST(ProxyServiceTest, PAC_NoIdentityOrHash) {
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
GURL url("http://username:password@www.google.com/?ref#hash#hash");
ProxyInfo info;
TestCompletionCallback callback;
int rv = service->ResolveProxy(url, &info, &callback, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(GURL("http://foopy/proxy.pac"),
resolver->pending_set_pac_script_request()->script_data()->url());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
// The URL should have been simplified, stripping the username/password/hash.
EXPECT_EQ(GURL("http://www.google.com/?ref"),
resolver->pending_requests()[0]->url());
// We end here without ever completing the request -- destruction of
// ProxyService will cancel the outstanding request.
}
TEST(ProxyServiceTest, PAC_FailoverWithoutDirect) {
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
GURL url("http://www.google.com/");
ProxyInfo info;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(url, &info, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(GURL("http://foopy/proxy.pac"),
resolver->pending_set_pac_script_request()->script_data()->url());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(url, resolver->pending_requests()[0]->url());
// Set the result in proxy resolver.
resolver->pending_requests()[0]->results()->UseNamedProxy("foopy:8080");
resolver->pending_requests()[0]->CompleteNow(OK);
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foopy:8080", info.proxy_server().ToURI());
// Now, imagine that connecting to foopy:8080 fails: there is nothing
// left to fallback to, since our proxy list was NOT terminated by
// DIRECT.
TestCompletionCallback callback2;
rv = service->ReconsiderProxyAfterError(url, &info, &callback2, NULL,
BoundNetLog());
// ReconsiderProxyAfterError returns error indicating nothing left.
EXPECT_EQ(ERR_FAILED, rv);
EXPECT_TRUE(info.is_empty());
}
// The proxy list could potentially contain the DIRECT fallback choice
// in a location other than the very end of the list, and could even
// specify it multiple times.
//
// This is not a typical usage, but we will obey it.
// (If we wanted to disallow this type of input, the right place to
// enforce it would be in parsing the PAC result string).
//
// This test will use the PAC result string:
//
// "DIRECT ; PROXY foobar:10 ; DIRECT ; PROXY foobar:20"
//
// For which we expect it to try DIRECT, then foobar:10, then DIRECT again,
// then foobar:20, and then give up and error.
//
// The important check of this test is to make sure that DIRECT is not somehow
// cached as being a bad proxy.
TEST(ProxyServiceTest, PAC_FailoverAfterDirect) {
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
GURL url("http://www.google.com/");
ProxyInfo info;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(url, &info, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(GURL("http://foopy/proxy.pac"),
resolver->pending_set_pac_script_request()->script_data()->url());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(url, resolver->pending_requests()[0]->url());
// Set the result in proxy resolver.
resolver->pending_requests()[0]->results()->UsePacString(
"DIRECT ; PROXY foobar:10 ; DIRECT ; PROXY foobar:20");
resolver->pending_requests()[0]->CompleteNow(OK);
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_TRUE(info.is_direct());
// Fallback 1.
TestCompletionCallback callback2;
rv = service->ReconsiderProxyAfterError(url, &info, &callback2, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foobar:10", info.proxy_server().ToURI());
// Fallback 2.
TestCompletionCallback callback3;
rv = service->ReconsiderProxyAfterError(url, &info, &callback3, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_TRUE(info.is_direct());
// Fallback 3.
TestCompletionCallback callback4;
rv = service->ReconsiderProxyAfterError(url, &info, &callback4, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foobar:20", info.proxy_server().ToURI());
// Fallback 4 -- Nothing to fall back to!
TestCompletionCallback callback5;
rv = service->ReconsiderProxyAfterError(url, &info, &callback5, NULL,
BoundNetLog());
EXPECT_EQ(ERR_FAILED, rv);
EXPECT_TRUE(info.is_empty());
}
TEST(ProxyServiceTest, ProxyResolverFails) {
// Test what happens when the ProxyResolver fails. The download and setting
// of the PAC script have already succeeded, so this corresponds with a
// javascript runtime error while calling FindProxyForURL().
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
// Start first resolve request.
GURL url("http://www.google.com/");
ProxyInfo info;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(url, &info, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(GURL("http://foopy/proxy.pac"),
resolver->pending_set_pac_script_request()->script_data()->url());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(url, resolver->pending_requests()[0]->url());
// Fail the first resolve request in MockAsyncProxyResolver.
resolver->pending_requests()[0]->CompleteNow(ERR_FAILED);
// Although the proxy resolver failed the request, ProxyService implicitly
// falls-back to DIRECT.
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_TRUE(info.is_direct());
// The second resolve request will try to run through the proxy resolver,
// regardless of whether the first request failed in it.
TestCompletionCallback callback2;
rv = service->ResolveProxy(url, &info, &callback2, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(url, resolver->pending_requests()[0]->url());
// This time we will have the resolver succeed (perhaps the PAC script has
// a dependency on the current time).
resolver->pending_requests()[0]->results()->UseNamedProxy("foopy_valid:8080");
resolver->pending_requests()[0]->CompleteNow(OK);
EXPECT_EQ(OK, callback2.WaitForResult());
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foopy_valid:8080", info.proxy_server().ToURI());
}
TEST(ProxyServiceTest, ProxyFallback) {
// Test what happens when we specify multiple proxy servers and some of them
// are bad.
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
GURL url("http://www.google.com/");
// Get the proxy information.
ProxyInfo info;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(url, &info, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(GURL("http://foopy/proxy.pac"),
resolver->pending_set_pac_script_request()->script_data()->url());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(url, resolver->pending_requests()[0]->url());
// Set the result in proxy resolver.
resolver->pending_requests()[0]->results()->UseNamedProxy(
"foopy1:8080;foopy2:9090");
resolver->pending_requests()[0]->CompleteNow(OK);
// The first item is valid.
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
// Fake an error on the proxy.
TestCompletionCallback callback2;
rv = service->ReconsiderProxyAfterError(url, &info, &callback2, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
// The second proxy should be specified.
EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI());
TestCompletionCallback callback3;
rv = service->ResolveProxy(url, &info, &callback3, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(url, resolver->pending_requests()[0]->url());
// Set the result in proxy resolver -- the second result is already known
// to be bad, so we will not try to use it initially.
resolver->pending_requests()[0]->results()->UseNamedProxy(
"foopy3:7070;foopy1:8080;foopy2:9090");
resolver->pending_requests()[0]->CompleteNow(OK);
EXPECT_EQ(OK, callback3.WaitForResult());
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foopy3:7070", info.proxy_server().ToURI());
// We fake another error. It should now try the third one.
TestCompletionCallback callback4;
rv = service->ReconsiderProxyAfterError(url, &info, &callback4, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI());
// We fake another error. At this point we have tried all of the
// proxy servers we thought were valid; next we try the proxy server
// that was in our bad proxies map (foopy1:8080).
TestCompletionCallback callback5;
rv = service->ReconsiderProxyAfterError(url, &info, &callback5, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
// Fake another error, the last proxy is gone, the list should now be empty,
// so there is nothing left to try.
TestCompletionCallback callback6;
rv = service->ReconsiderProxyAfterError(url, &info, &callback6, NULL,
BoundNetLog());
EXPECT_EQ(ERR_FAILED, rv);
EXPECT_FALSE(info.is_direct());
EXPECT_TRUE(info.is_empty());
// TODO(nsylvain): Test that the proxy can be retried after the delay.
}
// This test is similar to ProxyFallback, but this time we have an explicit
// fallback choice to DIRECT.
TEST(ProxyServiceTest, ProxyFallbackToDirect) {
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
GURL url("http://www.google.com/");
// Get the proxy information.
ProxyInfo info;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(url, &info, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(GURL("http://foopy/proxy.pac"),
resolver->pending_set_pac_script_request()->script_data()->url());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(url, resolver->pending_requests()[0]->url());
// Set the result in proxy resolver.
resolver->pending_requests()[0]->results()->UsePacString(
"PROXY foopy1:8080; PROXY foopy2:9090; DIRECT");
resolver->pending_requests()[0]->CompleteNow(OK);
// Get the first result.
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
// Fake an error on the proxy.
TestCompletionCallback callback2;
rv = service->ReconsiderProxyAfterError(url, &info, &callback2, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
// Now we get back the second proxy.
EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI());
// Fake an error on this proxy as well.
TestCompletionCallback callback3;
rv = service->ReconsiderProxyAfterError(url, &info, &callback3, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
// Finally, we get back DIRECT.
EXPECT_TRUE(info.is_direct());
// Now we tell the proxy service that even DIRECT failed.
TestCompletionCallback callback4;
rv = service->ReconsiderProxyAfterError(url, &info, &callback4, NULL,
BoundNetLog());
// There was nothing left to try after DIRECT, so we are out of
// choices.
EXPECT_EQ(ERR_FAILED, rv);
}
TEST(ProxyServiceTest, ProxyFallback_NewSettings) {
// Test proxy failover when new settings are available.
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
GURL url("http://www.google.com/");
// Get the proxy information.
ProxyInfo info;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(url, &info, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(GURL("http://foopy/proxy.pac"),
resolver->pending_set_pac_script_request()->script_data()->url());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(url, resolver->pending_requests()[0]->url());
// Set the result in proxy resolver.
resolver->pending_requests()[0]->results()->UseNamedProxy(
"foopy1:8080;foopy2:9090");
resolver->pending_requests()[0]->CompleteNow(OK);
// The first item is valid.
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
// Fake an error on the proxy, and also a new configuration on the proxy.
config_service->SetConfig(
ProxyConfig::CreateFromCustomPacURL(GURL("http://foopy-new/proxy.pac")));
TestCompletionCallback callback2;
rv = service->ReconsiderProxyAfterError(url, &info, &callback2, NULL,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(GURL("http://foopy-new/proxy.pac"),
resolver->pending_set_pac_script_request()->script_data()->url());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(url, resolver->pending_requests()[0]->url());
resolver->pending_requests()[0]->results()->UseNamedProxy(
"foopy1:8080;foopy2:9090");
resolver->pending_requests()[0]->CompleteNow(OK);
// The first proxy is still there since the configuration changed.
EXPECT_EQ(OK, callback2.WaitForResult());
EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
// We fake another error. It should now ignore the first one.
TestCompletionCallback callback3;
rv = service->ReconsiderProxyAfterError(url, &info, &callback3, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI());
// We simulate a new configuration.
config_service->SetConfig(
ProxyConfig::CreateFromCustomPacURL(
GURL("http://foopy-new2/proxy.pac")));
// We fake another error. It should go back to the first proxy.
TestCompletionCallback callback4;
rv = service->ReconsiderProxyAfterError(url, &info, &callback4, NULL,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(GURL("http://foopy-new2/proxy.pac"),
resolver->pending_set_pac_script_request()->script_data()->url());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(url, resolver->pending_requests()[0]->url());
resolver->pending_requests()[0]->results()->UseNamedProxy(
"foopy1:8080;foopy2:9090");
resolver->pending_requests()[0]->CompleteNow(OK);
EXPECT_EQ(OK, callback4.WaitForResult());
EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
}
TEST(ProxyServiceTest, ProxyFallback_BadConfig) {
// Test proxy failover when the configuration is bad.
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
GURL url("http://www.google.com/");
// Get the proxy information.
ProxyInfo info;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(url, &info, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(GURL("http://foopy/proxy.pac"),
resolver->pending_set_pac_script_request()->script_data()->url());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(url, resolver->pending_requests()[0]->url());
resolver->pending_requests()[0]->results()->UseNamedProxy(
"foopy1:8080;foopy2:9090");
resolver->pending_requests()[0]->CompleteNow(OK);
// The first item is valid.
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
// Fake a proxy error.
TestCompletionCallback callback2;
rv = service->ReconsiderProxyAfterError(url, &info, &callback2, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
// The first proxy is ignored, and the second one is selected.
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI());
// Fake a PAC failure.
ProxyInfo info2;
TestCompletionCallback callback3;
rv = service->ResolveProxy(url, &info2, &callback3, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(url, resolver->pending_requests()[0]->url());
// This simulates a javascript runtime error in the PAC script.
resolver->pending_requests()[0]->CompleteNow(ERR_FAILED);
// Although the resolver failed, the ProxyService will implicitly fall-back
// to a DIRECT connection.
EXPECT_EQ(OK, callback3.WaitForResult());
EXPECT_TRUE(info2.is_direct());
EXPECT_FALSE(info2.is_empty());
// The PAC script will work properly next time and successfully return a
// proxy list. Since we have not marked the configuration as bad, it should
// "just work" the next time we call it.
ProxyInfo info3;
TestCompletionCallback callback4;
rv = service->ReconsiderProxyAfterError(url, &info3, &callback4, NULL,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(url, resolver->pending_requests()[0]->url());
resolver->pending_requests()[0]->results()->UseNamedProxy(
"foopy1:8080;foopy2:9090");
resolver->pending_requests()[0]->CompleteNow(OK);
// The first proxy is not there since the it was added to the bad proxies
// list by the earlier ReconsiderProxyAfterError().
EXPECT_EQ(OK, callback4.WaitForResult());
EXPECT_FALSE(info3.is_direct());
EXPECT_EQ("foopy1:8080", info3.proxy_server().ToURI());
}
TEST(ProxyServiceTest, ProxyBypassList) {
// Test that the proxy bypass rules are consulted.
TestCompletionCallback callback[2];
ProxyInfo info[2];
ProxyConfig config;
config.proxy_rules().ParseFromString("foopy1:8080;foopy2:9090");
config.set_auto_detect(false);
config.proxy_rules().bypass_rules.ParseFromString("*.org");
scoped_refptr<ProxyService> service(new ProxyService(
new MockProxyConfigService(config), new MockAsyncProxyResolver, NULL));
int rv;
GURL url1("http://www.webkit.org");
GURL url2("http://www.webkit.com");
// Request for a .org domain should bypass proxy.
rv = service->ResolveProxy(url1, &info[0], &callback[0], NULL, BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_TRUE(info[0].is_direct());
// Request for a .com domain hits the proxy.
rv = service->ResolveProxy(url2, &info[1], &callback[1], NULL, BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_EQ("foopy1:8080", info[1].proxy_server().ToURI());
}
TEST(ProxyServiceTest, PerProtocolProxyTests) {
ProxyConfig config;
config.proxy_rules().ParseFromString("http=foopy1:8080;https=foopy2:8080");
config.set_auto_detect(false);
{
scoped_refptr<ProxyService> service(new ProxyService(
new MockProxyConfigService(config), new MockAsyncProxyResolver, NULL));
GURL test_url("http://www.msn.com");
ProxyInfo info;
TestCompletionCallback callback;
int rv = service->ResolveProxy(test_url, &info, &callback, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
}
{
scoped_refptr<ProxyService> service(new ProxyService(
new MockProxyConfigService(config), new MockAsyncProxyResolver, NULL));
GURL test_url("ftp://ftp.google.com");
ProxyInfo info;
TestCompletionCallback callback;
int rv = service->ResolveProxy(test_url, &info, &callback, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_TRUE(info.is_direct());
EXPECT_EQ("direct://", info.proxy_server().ToURI());
}
{
scoped_refptr<ProxyService> service(new ProxyService(
new MockProxyConfigService(config), new MockAsyncProxyResolver, NULL));
GURL test_url("https://webbranch.techcu.com");
ProxyInfo info;
TestCompletionCallback callback;
int rv = service->ResolveProxy(test_url, &info, &callback, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foopy2:8080", info.proxy_server().ToURI());
}
{
config.proxy_rules().ParseFromString("foopy1:8080");
scoped_refptr<ProxyService> service(new ProxyService(
new MockProxyConfigService(config), new MockAsyncProxyResolver, NULL));
GURL test_url("http://www.microsoft.com");
ProxyInfo info;
TestCompletionCallback callback;
int rv = service->ResolveProxy(test_url, &info, &callback, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
}
}
// If only HTTP and a SOCKS proxy are specified, check if ftp/https queries
// fall back to the SOCKS proxy.
TEST(ProxyServiceTest, DefaultProxyFallbackToSOCKS) {
ProxyConfig config;
config.proxy_rules().ParseFromString("http=foopy1:8080;socks=foopy2:1080");
config.set_auto_detect(false);
EXPECT_EQ(ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
config.proxy_rules().type);
{
scoped_refptr<ProxyService> service(new ProxyService(
new MockProxyConfigService(config), new MockAsyncProxyResolver, NULL));
GURL test_url("http://www.msn.com");
ProxyInfo info;
TestCompletionCallback callback;
int rv = service->ResolveProxy(test_url, &info, &callback, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
}
{
scoped_refptr<ProxyService> service(new ProxyService(
new MockProxyConfigService(config), new MockAsyncProxyResolver, NULL));
GURL test_url("ftp://ftp.google.com");
ProxyInfo info;
TestCompletionCallback callback;
int rv = service->ResolveProxy(test_url, &info, &callback, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("socks4://foopy2:1080", info.proxy_server().ToURI());
}
{
scoped_refptr<ProxyService> service(new ProxyService(
new MockProxyConfigService(config), new MockAsyncProxyResolver, NULL));
GURL test_url("https://webbranch.techcu.com");
ProxyInfo info;
TestCompletionCallback callback;
int rv = service->ResolveProxy(test_url, &info, &callback, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("socks4://foopy2:1080", info.proxy_server().ToURI());
}
{
scoped_refptr<ProxyService> service(new ProxyService(
new MockProxyConfigService(config), new MockAsyncProxyResolver, NULL));
GURL test_url("unknown://www.microsoft.com");
ProxyInfo info;
TestCompletionCallback callback;
int rv = service->ResolveProxy(test_url, &info, &callback, NULL,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_FALSE(info.is_direct());
EXPECT_EQ("socks4://foopy2:1080", info.proxy_server().ToURI());
}
}
// Test cancellation of an in-progress request.
TEST(ProxyServiceTest, CancelInProgressRequest) {
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
// Start 3 requests.
ProxyInfo info1;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(
GURL("http://request1"), &info1, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
// Nothing has been sent to the proxy resolver yet, since the proxy
// resolver has not been configured yet.
ASSERT_EQ(0u, resolver->pending_requests().size());
// Successfully initialize the PAC script.
EXPECT_EQ(GURL("http://foopy/proxy.pac"),
resolver->pending_set_pac_script_request()->script_data()->url());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(GURL("http://request1"), resolver->pending_requests()[0]->url());
ProxyInfo info2;
TestCompletionCallback callback2;
ProxyService::PacRequest* request2;
rv = service->ResolveProxy(
GURL("http://request2"), &info2, &callback2, &request2, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
ASSERT_EQ(2u, resolver->pending_requests().size());
EXPECT_EQ(GURL("http://request2"), resolver->pending_requests()[1]->url());
ProxyInfo info3;
TestCompletionCallback callback3;
rv = service->ResolveProxy(
GURL("http://request3"), &info3, &callback3, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
ASSERT_EQ(3u, resolver->pending_requests().size());
EXPECT_EQ(GURL("http://request3"), resolver->pending_requests()[2]->url());
// Cancel the second request
service->CancelPacRequest(request2);
ASSERT_EQ(2u, resolver->pending_requests().size());
EXPECT_EQ(GURL("http://request1"), resolver->pending_requests()[0]->url());
EXPECT_EQ(GURL("http://request3"), resolver->pending_requests()[1]->url());
// Complete the two un-cancelled requests.
// We complete the last one first, just to mix it up a bit.
resolver->pending_requests()[1]->results()->UseNamedProxy("request3:80");
resolver->pending_requests()[1]->CompleteNow(OK);
resolver->pending_requests()[0]->results()->UseNamedProxy("request1:80");
resolver->pending_requests()[0]->CompleteNow(OK);
// Complete and verify that requests ran as expected.
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_EQ("request1:80", info1.proxy_server().ToURI());
EXPECT_FALSE(callback2.have_result()); // Cancelled.
ASSERT_EQ(1u, resolver->cancelled_requests().size());
EXPECT_EQ(GURL("http://request2"), resolver->cancelled_requests()[0]->url());
EXPECT_EQ(OK, callback3.WaitForResult());
EXPECT_EQ("request3:80", info3.proxy_server().ToURI());
}
// Test the initial PAC download for resolver that expects bytes.
TEST(ProxyServiceTest, InitialPACScriptDownload) {
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolverExpectsBytes* resolver =
new MockAsyncProxyResolverExpectsBytes;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
MockProxyScriptFetcher* fetcher = new MockProxyScriptFetcher;
service->SetProxyScriptFetcher(fetcher);
// Start 3 requests.
ProxyInfo info1;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(
GURL("http://request1"), &info1, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
// The first request should have triggered download of PAC script.
EXPECT_TRUE(fetcher->has_pending_request());
EXPECT_EQ(GURL("http://foopy/proxy.pac"), fetcher->pending_request_url());
ProxyInfo info2;
TestCompletionCallback callback2;
rv = service->ResolveProxy(
GURL("http://request2"), &info2, &callback2, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
ProxyInfo info3;
TestCompletionCallback callback3;
rv = service->ResolveProxy(
GURL("http://request3"), &info3, &callback3, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
// Nothing has been sent to the resolver yet.
EXPECT_TRUE(resolver->pending_requests().empty());
// At this point the ProxyService should be waiting for the
// ProxyScriptFetcher to invoke its completion callback, notifying it of
// PAC script download completion.
fetcher->NotifyFetchCompletion(OK, "pac-v1");
// Now that the PAC script is downloaded, it will have been sent to the proxy
// resolver.
EXPECT_EQ(ASCIIToUTF16("pac-v1"),
resolver->pending_set_pac_script_request()->script_data()->utf16());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(3u, resolver->pending_requests().size());
EXPECT_EQ(GURL("http://request1"), resolver->pending_requests()[0]->url());
EXPECT_EQ(GURL("http://request2"), resolver->pending_requests()[1]->url());
EXPECT_EQ(GURL("http://request3"), resolver->pending_requests()[2]->url());
// Complete all the requests (in some order).
// Note that as we complete requests, they shift up in |pending_requests()|.
resolver->pending_requests()[2]->results()->UseNamedProxy("request3:80");
resolver->pending_requests()[2]->CompleteNow(OK);
resolver->pending_requests()[0]->results()->UseNamedProxy("request1:80");
resolver->pending_requests()[0]->CompleteNow(OK);
resolver->pending_requests()[0]->results()->UseNamedProxy("request2:80");
resolver->pending_requests()[0]->CompleteNow(OK);
// Complete and verify that requests ran as expected.
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_EQ("request1:80", info1.proxy_server().ToURI());
EXPECT_EQ(OK, callback2.WaitForResult());
EXPECT_EQ("request2:80", info2.proxy_server().ToURI());
EXPECT_EQ(OK, callback3.WaitForResult());
EXPECT_EQ("request3:80", info3.proxy_server().ToURI());
}
// Test changing the ProxyScriptFetcher while PAC download is in progress.
TEST(ProxyServiceTest, ChangeScriptFetcherWhilePACDownloadInProgress) {
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolverExpectsBytes* resolver =
new MockAsyncProxyResolverExpectsBytes;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
MockProxyScriptFetcher* fetcher = new MockProxyScriptFetcher;
service->SetProxyScriptFetcher(fetcher);
// Start 2 requests.
ProxyInfo info1;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(
GURL("http://request1"), &info1, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
// The first request should have triggered download of PAC script.
EXPECT_TRUE(fetcher->has_pending_request());
EXPECT_EQ(GURL("http://foopy/proxy.pac"), fetcher->pending_request_url());
ProxyInfo info2;
TestCompletionCallback callback2;
rv = service->ResolveProxy(
GURL("http://request2"), &info2, &callback2, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
// At this point the ProxyService should be waiting for the
// ProxyScriptFetcher to invoke its completion callback, notifying it of
// PAC script download completion.
// We now change out the ProxyService's script fetcher. We should restart
// the initialization with the new fetcher.
fetcher = new MockProxyScriptFetcher;
service->SetProxyScriptFetcher(fetcher);
// Nothing has been sent to the resolver yet.
EXPECT_TRUE(resolver->pending_requests().empty());
fetcher->NotifyFetchCompletion(OK, "pac-v1");
// Now that the PAC script is downloaded, it will have been sent to the proxy
// resolver.
EXPECT_EQ(ASCIIToUTF16("pac-v1"),
resolver->pending_set_pac_script_request()->script_data()->utf16());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(2u, resolver->pending_requests().size());
EXPECT_EQ(GURL("http://request1"), resolver->pending_requests()[0]->url());
EXPECT_EQ(GURL("http://request2"), resolver->pending_requests()[1]->url());
}
// Test cancellation of a request, while the PAC script is being fetched.
TEST(ProxyServiceTest, CancelWhilePACFetching) {
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolverExpectsBytes* resolver =
new MockAsyncProxyResolverExpectsBytes;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
MockProxyScriptFetcher* fetcher = new MockProxyScriptFetcher;
service->SetProxyScriptFetcher(fetcher);
// Start 3 requests.
ProxyInfo info1;
TestCompletionCallback callback1;
ProxyService::PacRequest* request1;
CapturingBoundNetLog log1(CapturingNetLog::kUnbounded);
int rv = service->ResolveProxy(
GURL("http://request1"), &info1, &callback1, &request1, log1.bound());
EXPECT_EQ(ERR_IO_PENDING, rv);
// The first request should have triggered download of PAC script.
EXPECT_TRUE(fetcher->has_pending_request());
EXPECT_EQ(GURL("http://foopy/proxy.pac"), fetcher->pending_request_url());
ProxyInfo info2;
TestCompletionCallback callback2;
ProxyService::PacRequest* request2;
rv = service->ResolveProxy(
GURL("http://request2"), &info2, &callback2, &request2, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
ProxyInfo info3;
TestCompletionCallback callback3;
rv = service->ResolveProxy(
GURL("http://request3"), &info3, &callback3, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
// Nothing has been sent to the resolver yet.
EXPECT_TRUE(resolver->pending_requests().empty());
// Cancel the first 2 requests.
service->CancelPacRequest(request1);
service->CancelPacRequest(request2);
// At this point the ProxyService should be waiting for the
// ProxyScriptFetcher to invoke its completion callback, notifying it of
// PAC script download completion.
fetcher->NotifyFetchCompletion(OK, "pac-v1");
// Now that the PAC script is downloaded, it will have been sent to the
// proxy resolver.
EXPECT_EQ(ASCIIToUTF16("pac-v1"),
resolver->pending_set_pac_script_request()->script_data()->utf16());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(GURL("http://request3"), resolver->pending_requests()[0]->url());
// Complete all the requests.
resolver->pending_requests()[0]->results()->UseNamedProxy("request3:80");
resolver->pending_requests()[0]->CompleteNow(OK);
EXPECT_EQ(OK, callback3.WaitForResult());
EXPECT_EQ("request3:80", info3.proxy_server().ToURI());
EXPECT_TRUE(resolver->cancelled_requests().empty());
EXPECT_FALSE(callback1.have_result()); // Cancelled.
EXPECT_FALSE(callback2.have_result()); // Cancelled.
CapturingNetLog::EntryList entries1;
log1.GetEntries(&entries1);
// Check the NetLog for request 1 (which was cancelled) got filled properly.
EXPECT_EQ(4u, entries1.size());
EXPECT_TRUE(LogContainsBeginEvent(
entries1, 0, NetLog::TYPE_PROXY_SERVICE));
EXPECT_TRUE(LogContainsBeginEvent(
entries1, 1, NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC));
// Note that TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC is never completed before
// the cancellation occured.
EXPECT_TRUE(LogContainsEvent(
entries1, 2, NetLog::TYPE_CANCELLED, NetLog::PHASE_NONE));
EXPECT_TRUE(LogContainsEndEvent(
entries1, 3, NetLog::TYPE_PROXY_SERVICE));
}
// Test that if auto-detect fails, we fall-back to the custom pac.
TEST(ProxyServiceTest, FallbackFromAutodetectToCustomPac) {
ProxyConfig config;
config.set_auto_detect(true);
config.set_pac_url(GURL("http://foopy/proxy.pac"));
config.proxy_rules().ParseFromString("http=foopy:80"); // Won't be used.
MockProxyConfigService* config_service = new MockProxyConfigService(config);
MockAsyncProxyResolverExpectsBytes* resolver =
new MockAsyncProxyResolverExpectsBytes;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
MockProxyScriptFetcher* fetcher = new MockProxyScriptFetcher;
service->SetProxyScriptFetcher(fetcher);
// Start 2 requests.
ProxyInfo info1;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(
GURL("http://request1"), &info1, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
ProxyInfo info2;
TestCompletionCallback callback2;
ProxyService::PacRequest* request2;
rv = service->ResolveProxy(
GURL("http://request2"), &info2, &callback2, &request2, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
// Check that nothing has been sent to the proxy resolver yet.
ASSERT_EQ(0u, resolver->pending_requests().size());
// It should be trying to auto-detect first -- FAIL the autodetect during
// the script download.
EXPECT_TRUE(fetcher->has_pending_request());
EXPECT_EQ(GURL("http://wpad/wpad.dat"), fetcher->pending_request_url());
fetcher->NotifyFetchCompletion(ERR_FAILED, "");
// Next it should be trying the custom PAC url.
EXPECT_TRUE(fetcher->has_pending_request());
EXPECT_EQ(GURL("http://foopy/proxy.pac"), fetcher->pending_request_url());
fetcher->NotifyFetchCompletion(OK, "custom-pac-script");
EXPECT_EQ(ASCIIToUTF16("custom-pac-script"),
resolver->pending_set_pac_script_request()->script_data()->utf16());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
// Now finally, the pending requests should have been sent to the resolver
// (which was initialized with custom PAC script).
ASSERT_EQ(2u, resolver->pending_requests().size());
EXPECT_EQ(GURL("http://request1"), resolver->pending_requests()[0]->url());
EXPECT_EQ(GURL("http://request2"), resolver->pending_requests()[1]->url());
// Complete the pending requests.
resolver->pending_requests()[1]->results()->UseNamedProxy("request2:80");
resolver->pending_requests()[1]->CompleteNow(OK);
resolver->pending_requests()[0]->results()->UseNamedProxy("request1:80");
resolver->pending_requests()[0]->CompleteNow(OK);
// Verify that requests ran as expected.
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_EQ("request1:80", info1.proxy_server().ToURI());
EXPECT_EQ(OK, callback2.WaitForResult());
EXPECT_EQ("request2:80", info2.proxy_server().ToURI());
}
// This is the same test as FallbackFromAutodetectToCustomPac, except
// the auto-detect script fails parsing rather than downloading.
TEST(ProxyServiceTest, FallbackFromAutodetectToCustomPac2) {
ProxyConfig config;
config.set_auto_detect(true);
config.set_pac_url(GURL("http://foopy/proxy.pac"));
config.proxy_rules().ParseFromString("http=foopy:80"); // Won't be used.
MockProxyConfigService* config_service = new MockProxyConfigService(config);
MockAsyncProxyResolverExpectsBytes* resolver =
new MockAsyncProxyResolverExpectsBytes;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
MockProxyScriptFetcher* fetcher = new MockProxyScriptFetcher;
service->SetProxyScriptFetcher(fetcher);
// Start 2 requests.
ProxyInfo info1;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(
GURL("http://request1"), &info1, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
ProxyInfo info2;
TestCompletionCallback callback2;
ProxyService::PacRequest* request2;
rv = service->ResolveProxy(
GURL("http://request2"), &info2, &callback2, &request2, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
// Check that nothing has been sent to the proxy resolver yet.
ASSERT_EQ(0u, resolver->pending_requests().size());
// It should be trying to auto-detect first -- succeed the download.
EXPECT_TRUE(fetcher->has_pending_request());
EXPECT_EQ(GURL("http://wpad/wpad.dat"), fetcher->pending_request_url());
fetcher->NotifyFetchCompletion(OK, "invalid-script-contents");
// Simulate a parse error.
EXPECT_EQ(ASCIIToUTF16("invalid-script-contents"),
resolver->pending_set_pac_script_request()->script_data()->utf16());
resolver->pending_set_pac_script_request()->CompleteNow(
ERR_PAC_SCRIPT_FAILED);
// Next it should be trying the custom PAC url.
EXPECT_TRUE(fetcher->has_pending_request());
EXPECT_EQ(GURL("http://foopy/proxy.pac"), fetcher->pending_request_url());
fetcher->NotifyFetchCompletion(OK, "custom-pac-script");
EXPECT_EQ(ASCIIToUTF16("custom-pac-script"),
resolver->pending_set_pac_script_request()->script_data()->utf16());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
// Now finally, the pending requests should have been sent to the resolver
// (which was initialized with custom PAC script).
ASSERT_EQ(2u, resolver->pending_requests().size());
EXPECT_EQ(GURL("http://request1"), resolver->pending_requests()[0]->url());
EXPECT_EQ(GURL("http://request2"), resolver->pending_requests()[1]->url());
// Complete the pending requests.
resolver->pending_requests()[1]->results()->UseNamedProxy("request2:80");
resolver->pending_requests()[1]->CompleteNow(OK);
resolver->pending_requests()[0]->results()->UseNamedProxy("request1:80");
resolver->pending_requests()[0]->CompleteNow(OK);
// Verify that requests ran as expected.
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_EQ("request1:80", info1.proxy_server().ToURI());
EXPECT_EQ(OK, callback2.WaitForResult());
EXPECT_EQ("request2:80", info2.proxy_server().ToURI());
}
// Test that if all of auto-detect, a custom PAC script, and manual settings
// are given, then we will try them in that order.
TEST(ProxyServiceTest, FallbackFromAutodetectToCustomToManual) {
ProxyConfig config;
config.set_auto_detect(true);
config.set_pac_url(GURL("http://foopy/proxy.pac"));
config.proxy_rules().ParseFromString("http=foopy:80");
MockProxyConfigService* config_service = new MockProxyConfigService(config);
MockAsyncProxyResolverExpectsBytes* resolver =
new MockAsyncProxyResolverExpectsBytes;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
MockProxyScriptFetcher* fetcher = new MockProxyScriptFetcher;
service->SetProxyScriptFetcher(fetcher);
// Start 2 requests.
ProxyInfo info1;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(
GURL("http://request1"), &info1, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
ProxyInfo info2;
TestCompletionCallback callback2;
ProxyService::PacRequest* request2;
rv = service->ResolveProxy(
GURL("http://request2"), &info2, &callback2, &request2, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
// Check that nothing has been sent to the proxy resolver yet.
ASSERT_EQ(0u, resolver->pending_requests().size());
// It should be trying to auto-detect first -- fail the download.
EXPECT_TRUE(fetcher->has_pending_request());
EXPECT_EQ(GURL("http://wpad/wpad.dat"), fetcher->pending_request_url());
fetcher->NotifyFetchCompletion(ERR_FAILED, "");
// Next it should be trying the custom PAC url -- fail the download.
EXPECT_TRUE(fetcher->has_pending_request());
EXPECT_EQ(GURL("http://foopy/proxy.pac"), fetcher->pending_request_url());
fetcher->NotifyFetchCompletion(ERR_FAILED, "");
// Since we never managed to initialize a ProxyResolver, nothing should have
// been sent to it.
ASSERT_EQ(0u, resolver->pending_requests().size());
// Verify that requests ran as expected -- they should have fallen back to
// the manual proxy configuration for HTTP urls.
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_EQ("foopy:80", info1.proxy_server().ToURI());
EXPECT_EQ(OK, callback2.WaitForResult());
EXPECT_EQ("foopy:80", info2.proxy_server().ToURI());
}
// Test that the bypass rules are NOT applied when using autodetect.
TEST(ProxyServiceTest, BypassDoesntApplyToPac) {
ProxyConfig config;
config.set_auto_detect(true);
config.set_pac_url(GURL("http://foopy/proxy.pac"));
config.proxy_rules().ParseFromString("http=foopy:80"); // Not used.
config.proxy_rules().bypass_rules.ParseFromString("www.google.com");
MockProxyConfigService* config_service = new MockProxyConfigService(config);
MockAsyncProxyResolverExpectsBytes* resolver =
new MockAsyncProxyResolverExpectsBytes;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
MockProxyScriptFetcher* fetcher = new MockProxyScriptFetcher;
service->SetProxyScriptFetcher(fetcher);
// Start 1 requests.
ProxyInfo info1;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(
GURL("http://www.google.com"), &info1, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
// Check that nothing has been sent to the proxy resolver yet.
ASSERT_EQ(0u, resolver->pending_requests().size());
// It should be trying to auto-detect first -- succeed the download.
EXPECT_TRUE(fetcher->has_pending_request());
EXPECT_EQ(GURL("http://wpad/wpad.dat"), fetcher->pending_request_url());
fetcher->NotifyFetchCompletion(OK, "auto-detect");
EXPECT_EQ(ASCIIToUTF16("auto-detect"),
resolver->pending_set_pac_script_request()->script_data()->utf16());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(GURL("http://www.google.com"),
resolver->pending_requests()[0]->url());
// Complete the pending request.
resolver->pending_requests()[0]->results()->UseNamedProxy("request1:80");
resolver->pending_requests()[0]->CompleteNow(OK);
// Verify that request ran as expected.
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_EQ("request1:80", info1.proxy_server().ToURI());
// Start another request, it should pickup the bypass item.
ProxyInfo info2;
TestCompletionCallback callback2;
rv = service->ResolveProxy(
GURL("http://www.google.com"), &info2, &callback2, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(GURL("http://www.google.com"),
resolver->pending_requests()[0]->url());
// Complete the pending request.
resolver->pending_requests()[0]->results()->UseNamedProxy("request2:80");
resolver->pending_requests()[0]->CompleteNow(OK);
EXPECT_EQ(OK, callback2.WaitForResult());
EXPECT_EQ("request2:80", info2.proxy_server().ToURI());
}
// Delete the ProxyService while InitProxyResolver has an outstanding
// request to the script fetcher. When run under valgrind, should not
// have any memory errors (used to be that the ProxyScriptFetcher was
// being deleted prior to the InitProxyResolver).
TEST(ProxyServiceTest, DeleteWhileInitProxyResolverHasOutstandingFetch) {
ProxyConfig config =
ProxyConfig::CreateFromCustomPacURL(GURL("http://foopy/proxy.pac"));
MockProxyConfigService* config_service = new MockProxyConfigService(config);
MockAsyncProxyResolverExpectsBytes* resolver =
new MockAsyncProxyResolverExpectsBytes;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
MockProxyScriptFetcher* fetcher = new MockProxyScriptFetcher;
service->SetProxyScriptFetcher(fetcher);
// Start 1 request.
ProxyInfo info1;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(
GURL("http://www.google.com"), &info1, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
// Check that nothing has been sent to the proxy resolver yet.
ASSERT_EQ(0u, resolver->pending_requests().size());
// InitProxyResolver should have issued a request to the ProxyScriptFetcher
// and be waiting on that to complete.
EXPECT_TRUE(fetcher->has_pending_request());
EXPECT_EQ(GURL("http://foopy/proxy.pac"), fetcher->pending_request_url());
// Delete the ProxyService
service = NULL;
}
// Delete the ProxyService while InitProxyResolver has an outstanding
// request to the proxy resolver. When run under valgrind, should not
// have any memory errors (used to be that the ProxyResolver was
// being deleted prior to the InitProxyResolver).
TEST(ProxyServiceTest, DeleteWhileInitProxyResolverHasOutstandingSet) {
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
GURL url("http://www.google.com/");
ProxyInfo info;
TestCompletionCallback callback;
int rv = service->ResolveProxy(url, &info, &callback, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(GURL("http://foopy/proxy.pac"),
resolver->pending_set_pac_script_request()->script_data()->url());
// Delete the ProxyService.
service = NULL;
}
TEST(ProxyServiceTest, ResetProxyConfigService) {
ProxyConfig config1;
config1.proxy_rules().ParseFromString("foopy1:8080");
config1.set_auto_detect(false);
scoped_refptr<ProxyService> service(new ProxyService(
new MockProxyConfigService(config1),
new MockAsyncProxyResolverExpectsBytes, NULL));
ProxyInfo info;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(
GURL("http://request1"), &info, &callback1, NULL, BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI());
ProxyConfig config2;
config2.proxy_rules().ParseFromString("foopy2:8080");
config2.set_auto_detect(false);
service->ResetConfigService(new MockProxyConfigService(config2));
TestCompletionCallback callback2;
rv = service->ResolveProxy(
GURL("http://request2"), &info, &callback2, NULL, BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_EQ("foopy2:8080", info.proxy_server().ToURI());
}
// Test that when going from a configuration that required PAC to one
// that does NOT, we unset the variable |should_use_proxy_resolver_|.
TEST(ProxyServiceTest, UpdateConfigFromPACToDirect) {
ProxyConfig config = ProxyConfig::CreateAutoDetect();
MockProxyConfigService* config_service = new MockProxyConfigService(config);
MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver;
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, NULL));
// Start 1 request.
ProxyInfo info1;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(
GURL("http://www.google.com"), &info1, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
// Check that nothing has been sent to the proxy resolver yet.
ASSERT_EQ(0u, resolver->pending_requests().size());
// Successfully set the autodetect script.
EXPECT_EQ(ProxyResolverScriptData::TYPE_AUTO_DETECT,
resolver->pending_set_pac_script_request()->script_data()->type());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
// Complete the pending request.
ASSERT_EQ(1u, resolver->pending_requests().size());
resolver->pending_requests()[0]->results()->UseNamedProxy("request1:80");
resolver->pending_requests()[0]->CompleteNow(OK);
// Verify that request ran as expected.
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_EQ("request1:80", info1.proxy_server().ToURI());
// Force the ProxyService to pull down a new proxy configuration.
// (Even though the configuration isn't old/bad).
//
// This new configuration no longer has auto_detect set, so
// requests should complete synchronously now as direct-connect.
config_service->SetConfig(ProxyConfig::CreateDirect());
// Start another request -- the effective configuration has changed.
ProxyInfo info2;
TestCompletionCallback callback2;
rv = service->ResolveProxy(
GURL("http://www.google.com"), &info2, &callback2, NULL, BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_TRUE(info2.is_direct());
}
TEST(ProxyServiceTest, NetworkChangeTriggersPacRefetch) {
MockProxyConfigService* config_service =
new MockProxyConfigService("http://foopy/proxy.pac");
MockAsyncProxyResolverExpectsBytes* resolver =
new MockAsyncProxyResolverExpectsBytes;
CapturingNetLog log(CapturingNetLog::kUnbounded);
scoped_refptr<ProxyService> service(
new ProxyService(config_service, resolver, &log));
MockProxyScriptFetcher* fetcher = new MockProxyScriptFetcher;
service->SetProxyScriptFetcher(fetcher);
// Disable the "wait after IP address changes" hack, so this unit-test can
// complete quickly.
service->set_stall_proxy_auto_config_delay(base::TimeDelta());
// Start 1 request.
ProxyInfo info1;
TestCompletionCallback callback1;
int rv = service->ResolveProxy(
GURL("http://request1"), &info1, &callback1, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
// The first request should have triggered initial download of PAC script.
EXPECT_TRUE(fetcher->has_pending_request());
EXPECT_EQ(GURL("http://foopy/proxy.pac"), fetcher->pending_request_url());
// Nothing has been sent to the resolver yet.
EXPECT_TRUE(resolver->pending_requests().empty());
// At this point the ProxyService should be waiting for the
// ProxyScriptFetcher to invoke its completion callback, notifying it of
// PAC script download completion.
fetcher->NotifyFetchCompletion(OK, "pac-v1");
// Now that the PAC script is downloaded, the request will have been sent to
// the proxy resolver.
EXPECT_EQ(ASCIIToUTF16("pac-v1"),
resolver->pending_set_pac_script_request()->script_data()->utf16());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(GURL("http://request1"), resolver->pending_requests()[0]->url());
// Complete the pending request.
resolver->pending_requests()[0]->results()->UseNamedProxy("request1:80");
resolver->pending_requests()[0]->CompleteNow(OK);
// Wait for completion callback, and verify that the request ran as expected.
EXPECT_EQ(OK, callback1.WaitForResult());
EXPECT_EQ("request1:80", info1.proxy_server().ToURI());
// Now simluate a change in the network. The ProxyConfigService is still
// going to return the same PAC URL as before, but this URL needs to be
// refetched on the new network.
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
MessageLoop::current()->RunAllPending(); // Notification happens async.
// Start a second request.
ProxyInfo info2;
TestCompletionCallback callback2;
rv = service->ResolveProxy(
GURL("http://request2"), &info2, &callback2, NULL, BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
// This second request should have triggered the re-download of the PAC
// script (since we marked the network as having changed).
EXPECT_TRUE(fetcher->has_pending_request());
EXPECT_EQ(GURL("http://foopy/proxy.pac"), fetcher->pending_request_url());
// Nothing has been sent to the resolver yet.
EXPECT_TRUE(resolver->pending_requests().empty());
// Simulate the PAC script fetch as having completed (this time with
// different data).
fetcher->NotifyFetchCompletion(OK, "pac-v2");
// Now that the PAC script is downloaded, the second request will have been
// sent to the proxy resolver.
EXPECT_EQ(ASCIIToUTF16("pac-v2"),
resolver->pending_set_pac_script_request()->script_data()->utf16());
resolver->pending_set_pac_script_request()->CompleteNow(OK);
ASSERT_EQ(1u, resolver->pending_requests().size());
EXPECT_EQ(GURL("http://request2"), resolver->pending_requests()[0]->url());
// Complete the pending second request.
resolver->pending_requests()[0]->results()->UseNamedProxy("request2:80");
resolver->pending_requests()[0]->CompleteNow(OK);
// Wait for completion callback, and verify that the request ran as expected.
EXPECT_EQ(OK, callback2.WaitForResult());
EXPECT_EQ("request2:80", info2.proxy_server().ToURI());
// Check that the expected events were outputted to the log stream.
// In particular, PROXY_CONFIG_CHANGED should have only been emitted once
// (for the initial setup), and NOT a second time when the IP address
// changed.
CapturingNetLog::EntryList entries;
log.GetEntries(&entries);
EXPECT_TRUE(LogContainsEntryWithType(entries, 0,
NetLog::TYPE_PROXY_CONFIG_CHANGED));
ASSERT_EQ(13u, entries.size());
for (size_t i = 1; i < entries.size(); ++i)
EXPECT_NE(NetLog::TYPE_PROXY_CONFIG_CHANGED, entries[i].type);
}
} // namespace net
|