OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import "package:crypto/crypto.dart"; | 5 import "package:crypto/crypto.dart"; |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 import "package:path/path.dart"; | 7 import "package:path/path.dart"; |
8 import "dart:async"; | 8 import "dart:async"; |
9 import "dart:io"; | 9 import "dart:io"; |
10 import 'dart:convert'; | 10 import 'dart:convert'; |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 } | 400 } |
401 | 401 |
402 test(false); | 402 test(false); |
403 test(true); | 403 test(true); |
404 } | 404 } |
405 }); | 405 }); |
406 }); | 406 }); |
407 }); | 407 }); |
408 } | 408 } |
409 | 409 |
410 int testProxyIPV6DoneCount = 0; | |
411 void testProxyIPV6() { | |
412 setupProxyServer(ipV6: true).then((proxyServer) { | |
413 setupServer(1, directRequestPaths: ["/4"]).then((server) { | |
414 setupServer(1, directRequestPaths: ["/4"], secure: true).then((secureServer) { | |
415 HttpClient client = new HttpClient(); | |
416 | |
417 List<String> proxy = ["PROXY [::1]:${proxyServer.port}"]; | |
418 client.findProxy = (Uri uri) { | |
419 // Pick the proxy configuration based on the request path. | |
420 int index = int.parse(uri.path.substring(1)); | |
421 return proxy[index]; | |
422 }; | |
423 | |
424 for (int i = 0; i < proxy.length; i++) { | |
425 test(bool secure) { | |
426 String url = secure | |
427 ? "https://localhost:${secureServer.port}/$i" | |
428 : "http://localhost:${server.port}/$i"; | |
429 | |
430 client.postUrl(Uri.parse(url)) | |
431 .then((HttpClientRequest clientRequest) { | |
432 String content = "$i$i$i"; | |
433 clientRequest.write(content); | |
434 return clientRequest.close(); | |
435 }) | |
436 .then((HttpClientResponse response) { | |
437 response.listen((_) {}, onDone: () { | |
438 testProxyIPV6DoneCount++; | |
439 if (testProxyIPV6DoneCount == proxy.length * 2) { | |
440 Expect.equals(proxy.length, server.requestCount); | |
441 Expect.equals(proxy.length, secureServer.requestCount); | |
442 proxyServer.shutdown(); | |
443 server.shutdown(); | |
444 secureServer.shutdown(); | |
445 client.close(); | |
446 } | |
447 }); | |
448 }); | |
449 } | |
450 | |
451 test(false); | |
452 test(true); | |
453 } | |
454 }); | |
455 }); | |
456 }); | |
457 } | |
458 | 410 |
459 int testProxyChainDoneCount = 0; | 411 int testProxyChainDoneCount = 0; |
460 void testProxyChain() { | 412 void testProxyChain() { |
461 // Setup two proxy servers having the first using the second as its proxy. | 413 // Setup two proxy servers having the first using the second as its proxy. |
462 setupProxyServer().then((proxyServer1) { | 414 setupProxyServer().then((proxyServer1) { |
463 setupProxyServer().then((proxyServer2) { | 415 setupProxyServer().then((proxyServer2) { |
464 proxyServer1.client.findProxy = (_) => "PROXY localhost:${proxyServer2.port}"; | 416 proxyServer1.client.findProxy = (_) => "PROXY localhost:${proxyServer2.port}"; |
465 | 417 |
466 setupServer(2, directRequestPaths: ["/4"]).then((server) { | 418 setupServer(2, directRequestPaths: ["/4"]).then((server) { |
467 HttpClient client = new HttpClient(); | 419 HttpClient client = new HttpClient(); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 client.close(); | 463 client.close(); |
512 } | 464 } |
513 }); | 465 }); |
514 }); | 466 }); |
515 } | 467 } |
516 }); | 468 }); |
517 }); | 469 }); |
518 }); | 470 }); |
519 } | 471 } |
520 | 472 |
521 int testProxyFromEnviromentDoneCount = 0; | |
522 void testProxyFromEnviroment() { | |
523 setupProxyServer().then((proxyServer) { | |
524 setupServer(1).then((server) { | |
525 setupServer(1, secure: true).then((secureServer) { | |
526 HttpClient client = new HttpClient(); | |
527 | |
528 client.findProxy = (Uri uri) { | |
529 return HttpClient.findProxyFromEnvironment( | |
530 uri, | |
531 environment: {"http_proxy": "localhost:${proxyServer.port}", | |
532 "https_proxy": "localhost:${proxyServer.port}"}); | |
533 }; | |
534 | |
535 const int loopCount = 5; | |
536 for (int i = 0; i < loopCount; i++) { | |
537 test(bool secure) { | |
538 String url = secure | |
539 ? "https://localhost:${secureServer.port}/$i" | |
540 : "http://localhost:${server.port}/$i"; | |
541 | |
542 client.postUrl(Uri.parse(url)) | |
543 .then((HttpClientRequest clientRequest) { | |
544 String content = "$i$i$i"; | |
545 clientRequest.write(content); | |
546 return clientRequest.close(); | |
547 }) | |
548 .then((HttpClientResponse response) { | |
549 response.listen((_) {}, onDone: () { | |
550 testProxyFromEnviromentDoneCount++; | |
551 if (testProxyFromEnviromentDoneCount == loopCount * 2) { | |
552 Expect.equals(loopCount, server.requestCount); | |
553 Expect.equals(loopCount, secureServer.requestCount); | |
554 proxyServer.shutdown(); | |
555 server.shutdown(); | |
556 secureServer.shutdown(); | |
557 client.close(); | |
558 } | |
559 }); | |
560 }); | |
561 } | |
562 | |
563 test(false); | |
564 test(true); | |
565 } | |
566 }); | |
567 }); | |
568 }); | |
569 } | |
570 | |
571 | |
572 int testProxyAuthenticateCount = 0; | |
573 Future testProxyAuthenticate(bool useDigestAuthentication) { | |
574 testProxyAuthenticateCount = 0; | |
575 var completer = new Completer(); | |
576 | |
577 setupProxyServer().then((proxyServer) { | |
578 setupServer(1).then((server) { | |
579 setupServer(1, secure: true).then((secureServer) { | |
580 HttpClient client = new HttpClient(); | |
581 | |
582 Completer step1 = new Completer(); | |
583 Completer step2 = new Completer(); | |
584 | |
585 if (useDigestAuthentication) { | |
586 proxyServer.useDigestAuthentication("dart", "password"); | |
587 } else { | |
588 proxyServer.useBasicAuthentication("dart", "password"); | |
589 } | |
590 | |
591 // Test with no authentication. | |
592 client.findProxy = (Uri uri) { | |
593 return "PROXY localhost:${proxyServer.port}"; | |
594 }; | |
595 | |
596 const int loopCount = 2; | |
597 for (int i = 0; i < loopCount; i++) { | |
598 test(bool secure) { | |
599 String url = secure | |
600 ? "https://localhost:${secureServer.port}/$i" | |
601 : "http://localhost:${server.port}/$i"; | |
602 | |
603 client.postUrl(Uri.parse(url)) | |
604 .then((HttpClientRequest clientRequest) { | |
605 String content = "$i$i$i"; | |
606 clientRequest.write(content); | |
607 return clientRequest.close(); | |
608 }) | |
609 .then((HttpClientResponse response) { | |
610 Expect.fail("No response expected"); | |
611 }). | |
612 catchError((e) { | |
613 testProxyAuthenticateCount++; | |
614 if (testProxyAuthenticateCount == loopCount * 2) { | |
615 Expect.equals(0, server.requestCount); | |
616 Expect.equals(0, secureServer.requestCount); | |
617 step1.complete(null); | |
618 } | |
619 }); | |
620 } | |
621 | |
622 test(false); | |
623 test(true); | |
624 } | |
625 step1.future.then((_) { | |
626 testProxyAuthenticateCount = 0; | |
627 if (useDigestAuthentication) { | |
628 client.findProxy = (Uri uri) => "PROXY localhost:${proxyServer.port}"; | |
629 client.addProxyCredentials( | |
630 "localhost", | |
631 proxyServer.port, | |
632 "test", | |
633 new HttpClientDigestCredentials("dart", "password")); | |
634 } else { | |
635 client.findProxy = (Uri uri) { | |
636 return "PROXY dart:password@localhost:${proxyServer.port}"; | |
637 }; | |
638 } | |
639 | |
640 for (int i = 0; i < loopCount; i++) { | |
641 test(bool secure) { | |
642 var path = useDigestAuthentication ? "A" : "$i"; | |
643 String url = secure | |
644 ? "https://localhost:${secureServer.port}/$path" | |
645 : "http://localhost:${server.port}/$path"; | |
646 | |
647 client.postUrl(Uri.parse(url)) | |
648 .then((HttpClientRequest clientRequest) { | |
649 String content = "$i$i$i"; | |
650 clientRequest.write(content); | |
651 return clientRequest.close(); | |
652 }) | |
653 .then((HttpClientResponse response) { | |
654 response.listen((_) {}, onDone: () { | |
655 testProxyAuthenticateCount++; | |
656 Expect.equals(HttpStatus.OK, response.statusCode); | |
657 if (testProxyAuthenticateCount == loopCount * 2) { | |
658 Expect.equals(loopCount, server.requestCount); | |
659 Expect.equals(loopCount, secureServer.requestCount); | |
660 step2.complete(null); | |
661 } | |
662 }); | |
663 }); | |
664 } | |
665 | |
666 test(false); | |
667 test(true); | |
668 } | |
669 }); | |
670 | |
671 step2.future.then((_) { | |
672 testProxyAuthenticateCount = 0; | |
673 client.findProxy = (Uri uri) { | |
674 return "PROXY localhost:${proxyServer.port}"; | |
675 }; | |
676 | |
677 client.authenticateProxy = (host, port, scheme, realm) { | |
678 client.addProxyCredentials( | |
679 "localhost", | |
680 proxyServer.port, | |
681 "realm", | |
682 new HttpClientBasicCredentials("dart", "password")); | |
683 return new Future.value(true); | |
684 }; | |
685 | |
686 for (int i = 0; i < loopCount; i++) { | |
687 test(bool secure) { | |
688 String url = secure | |
689 ? "https://localhost:${secureServer.port}/A" | |
690 : "http://localhost:${server.port}/A"; | |
691 | |
692 client.postUrl(Uri.parse(url)) | |
693 .then((HttpClientRequest clientRequest) { | |
694 String content = "$i$i$i"; | |
695 clientRequest.write(content); | |
696 return clientRequest.close(); | |
697 }) | |
698 .then((HttpClientResponse response) { | |
699 response.listen((_) {}, onDone: () { | |
700 testProxyAuthenticateCount++; | |
701 Expect.equals(HttpStatus.OK, response.statusCode); | |
702 if (testProxyAuthenticateCount == loopCount * 2) { | |
703 Expect.equals(loopCount * 2, server.requestCount); | |
704 Expect.equals(loopCount * 2, secureServer.requestCount); | |
705 proxyServer.shutdown(); | |
706 server.shutdown(); | |
707 secureServer.shutdown(); | |
708 client.close(); | |
709 completer.complete(null); | |
710 } | |
711 }); | |
712 }); | |
713 } | |
714 test(false); | |
715 test(true); | |
716 } | |
717 }); | |
718 | |
719 }); | |
720 }); | |
721 }); | |
722 | |
723 return completer.future; | |
724 } | |
725 | |
726 int testRealProxyDoneCount = 0; | |
727 void testRealProxy() { | |
728 setupServer(1).then((server) { | |
729 HttpClient client = new HttpClient(); | |
730 client.addProxyCredentials( | |
731 "localhost", | |
732 8080, | |
733 "test", | |
734 new HttpClientBasicCredentials("dart", "password")); | |
735 | |
736 List<String> proxy = | |
737 ["PROXY localhost:8080", | |
738 "PROXY localhost:8080; PROXY hede.hule.hest:8080", | |
739 "PROXY hede.hule.hest:8080; PROXY localhost:8080", | |
740 "PROXY localhost:8080; DIRECT"]; | |
741 | |
742 client.findProxy = (Uri uri) { | |
743 // Pick the proxy configuration based on the request path. | |
744 int index = int.parse(uri.path.substring(1)); | |
745 return proxy[index]; | |
746 }; | |
747 | |
748 for (int i = 0; i < proxy.length; i++) { | |
749 client.getUrl(Uri.parse("http://localhost:${server.port}/$i")) | |
750 .then((HttpClientRequest clientRequest) { | |
751 String content = "$i$i$i"; | |
752 clientRequest.contentLength = content.length; | |
753 clientRequest.write(content); | |
754 return clientRequest.close(); | |
755 }) | |
756 .then((HttpClientResponse response) { | |
757 response.listen((_) {}, onDone: () { | |
758 if (++testRealProxyDoneCount == proxy.length) { | |
759 Expect.equals(proxy.length, server.requestCount); | |
760 server.shutdown(); | |
761 client.close(); | |
762 } | |
763 }); | |
764 }); | |
765 } | |
766 }); | |
767 } | |
768 | |
769 int testRealProxyAuthDoneCount = 0; | |
770 void testRealProxyAuth() { | |
771 setupServer(1).then((server) { | |
772 HttpClient client = new HttpClient(); | |
773 | |
774 List<String> proxy = | |
775 ["PROXY dart:password@localhost:8080", | |
776 "PROXY dart:password@localhost:8080; PROXY hede.hule.hest:8080", | |
777 "PROXY hede.hule.hest:8080; PROXY dart:password@localhost:8080", | |
778 "PROXY dart:password@localhost:8080; DIRECT"]; | |
779 | |
780 client.findProxy = (Uri uri) { | |
781 // Pick the proxy configuration based on the request path. | |
782 int index = int.parse(uri.path.substring(1)); | |
783 return proxy[index]; | |
784 }; | |
785 | |
786 for (int i = 0; i < proxy.length; i++) { | |
787 client.getUrl(Uri.parse("http://localhost:${server.port}/$i")) | |
788 .then((HttpClientRequest clientRequest) { | |
789 String content = "$i$i$i"; | |
790 clientRequest.contentLength = content.length; | |
791 clientRequest.write(content); | |
792 return clientRequest.close(); | |
793 }) | |
794 .then((HttpClientResponse response) { | |
795 response.listen((_) {}, onDone: () { | |
796 if (++testRealProxyAuthDoneCount == proxy.length) { | |
797 Expect.equals(proxy.length, server.requestCount); | |
798 server.shutdown(); | |
799 client.close(); | |
800 } | |
801 }); | |
802 }); | |
803 } | |
804 }); | |
805 } | |
806 | |
807 main() { | 473 main() { |
808 testInvalidProxy(); | 474 testInvalidProxy(); |
809 testDirectProxy(); | 475 testDirectProxy(); |
810 testProxy(); | 476 testProxy(); |
811 // testProxyIPV6(); // TODO(24074): Move failing tests to separate files. | |
812 testProxyChain(); | 477 testProxyChain(); |
813 // TODO(24074): Move failing tests to separate files. | |
814 // testProxyFromEnviroment(); | |
815 // The two invocations of uses the same global variable for state - | |
816 // run one after the other. | |
817 // TODO(24074): Move failing tests to separate files. | |
818 // testProxyAuthenticate(false) | |
819 // .then((_) => testProxyAuthenticate(true)); | |
820 | |
821 // This test is not normally run. It can be used for locally testing | |
822 // with a real proxy server (e.g. Apache). | |
823 // testRealProxy(); | |
824 // testRealProxyAuth(); | |
825 } | 478 } |
OLD | NEW |