/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-12-13 16:23:52 UTC
  • mfrom: (2182.1.1 Aaron's integration)
  • Revision ID: pqm@pqm.ubuntu.com-20061213162352-0de9ae3d8d38f95d
Respect proxy environment settings (Vincent Ladeuil, #74759)

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# implementation; at the moment we have urllib and pycurl.
19
19
 
20
20
# TODO: Should be renamed to bzrlib.transport.http.tests?
 
21
# TODO: What about renaming to bzrlib.tests.transport.http ?
21
22
 
 
23
import os
22
24
import select
23
25
import socket
24
26
import threading
38
40
from bzrlib.tests.HTTPTestUtil import (
39
41
    BadProtocolRequestHandler,
40
42
    BadStatusRequestHandler,
 
43
    FakeProxyRequestHandler,
41
44
    ForbiddenRequestHandler,
42
45
    InvalidStatusRequestHandler,
43
46
    NoRangeRequestHandler,
44
47
    SingleRangeRequestHandler,
 
48
    TestCaseWithTwoWebservers,
45
49
    TestCaseWithWebserver,
46
50
    WallRequestHandler,
47
51
    )
532
536
 
533
537
 
534
538
class TestRangeRequestServer(object):
535
 
    """Test the http connections.
 
539
    """Tests readv requests against server.
536
540
 
537
541
    This MUST be used by daughter classes that also inherit from
538
542
    TestCaseWithWebserver.
546
550
        TestCaseWithWebserver.setUp(self)
547
551
        self.build_tree_contents([('a', '0123456789')],)
548
552
 
549
 
    """Tests readv requests against server"""
550
 
 
551
553
    def test_readv(self):
552
554
        server = self.get_readonly_server()
553
555
        t = self._transport(server.get_url())
621
623
    """Tests range requests refusing server for pycurl implementation"""
622
624
 
623
625
 
 
626
class TestProxyHttpServer(object):
 
627
    """Tests proxy server.
 
628
 
 
629
    This MUST be used by daughter classes that also inherit from
 
630
    TestCaseWithTwoWebservers.
 
631
 
 
632
    We can't inherit directly from TestCaseWithTwoWebservers or
 
633
    the test framework will try to create an instance which
 
634
    cannot run, its implementation being incomplete.
 
635
 
 
636
    Be aware that we do not setup a real proxy here. Instead, we
 
637
    check that the *connection* goes through the proxy by serving
 
638
    different content (the faked proxy server append '-proxied'
 
639
    to the file names).
 
640
    """
 
641
 
 
642
    # FIXME: We don't have an https server available, so we don't
 
643
    # test https connections.
 
644
 
 
645
    def setUp(self):
 
646
        TestCaseWithTwoWebservers.setUp(self)
 
647
        self.build_tree_contents([('foo', 'contents of foo\n'),
 
648
                                  ('foo-proxied', 'proxied contents of foo\n')])
 
649
        # Let's setup some attributes for tests
 
650
        self.server = self.get_readonly_server()
 
651
        self.no_proxy_host = 'localhost:%d' % self.server.port
 
652
        # The secondary server is the proxy
 
653
        self.proxy = self.get_secondary_server()
 
654
        self.proxy_url = self.proxy.get_url()
 
655
        self._old_env = {}
 
656
 
 
657
    def create_transport_secondary_server(self):
 
658
        """Creates an http server that will serve files with
 
659
        '-proxied' appended to their names.
 
660
        """
 
661
        return HttpServer(FakeProxyRequestHandler)
 
662
 
 
663
    def _set_and_capture_env_var(self, name, new_value):
 
664
        """Set an environment variable, and reset it when finished."""
 
665
        self._old_env[name] = osutils.set_or_unset_env(name, new_value)
 
666
 
 
667
    def _install_env(self, env):
 
668
        for name, value in env.iteritems():
 
669
            self._set_and_capture_env_var(name, value)
 
670
 
 
671
    def _restore_env(self):
 
672
        for name, value in self._old_env.iteritems():
 
673
            osutils.set_or_unset_env(name, value)
 
674
 
 
675
    def proxied_in_env(self, env):
 
676
        self._install_env(env)
 
677
        url = self.server.get_url()
 
678
        t = self._transport(url)
 
679
        try:
 
680
            self.assertEqual(t.get('foo').read(), 'proxied contents of foo\n')
 
681
        finally:
 
682
            self._restore_env()
 
683
 
 
684
    def not_proxied_in_env(self, env):
 
685
        self._install_env(env)
 
686
        url = self.server.get_url()
 
687
        t = self._transport(url)
 
688
        try:
 
689
            self.assertEqual(t.get('foo').read(), 'contents of foo\n')
 
690
        finally:
 
691
            self._restore_env()
 
692
 
 
693
    def test_http_proxy(self):
 
694
        self.proxied_in_env({'http_proxy': self.proxy_url})
 
695
 
 
696
    def test_HTTP_PROXY(self):
 
697
        self.proxied_in_env({'HTTP_PROXY': self.proxy_url})
 
698
 
 
699
    def test_all_proxy(self):
 
700
        self.proxied_in_env({'all_proxy': self.proxy_url})
 
701
 
 
702
    def test_ALL_PROXY(self):
 
703
        self.proxied_in_env({'ALL_PROXY': self.proxy_url})
 
704
 
 
705
    def test_http_proxy_with_no_proxy(self):
 
706
        self.not_proxied_in_env({'http_proxy': self.proxy_url,
 
707
                                 'no_proxy': self.no_proxy_host})
 
708
 
 
709
    def test_HTTP_PROXY_with_NO_PROXY(self):
 
710
        self.not_proxied_in_env({'HTTP_PROXY': self.proxy_url,
 
711
                                 'NO_PROXY': self.no_proxy_host})
 
712
 
 
713
    def test_all_proxy_with_no_proxy(self):
 
714
        self.not_proxied_in_env({'all_proxy': self.proxy_url,
 
715
                                 'no_proxy': self.no_proxy_host})
 
716
 
 
717
    def test_ALL_PROXY_with_NO_PROXY(self):
 
718
        self.not_proxied_in_env({'ALL_PROXY': self.proxy_url,
 
719
                                 'NO_PROXY': self.no_proxy_host})
 
720
 
 
721
 
 
722
class TestProxyHttpServer_urllib(TestProxyHttpServer,
 
723
                                 TestCaseWithTwoWebservers):
 
724
    """Tests proxy server for urllib implementation"""
 
725
 
 
726
    _transport = HttpTransport_urllib
 
727
 
 
728
 
 
729
class TestProxyHttpServer_pycurl(TestWithTransport_pycurl,
 
730
                                 TestProxyHttpServer,
 
731
                                 TestCaseWithTwoWebservers):
 
732
    """Tests proxy server for pycurl implementation"""
 
733
 
 
734
    def setUp(self):
 
735
        TestProxyHttpServer.setUp(self)
 
736
        # Oh my ! pycurl does not check for the port as part of
 
737
        # no_proxy :-( So we just test the host part
 
738
        self.no_proxy_host = 'localhost'
 
739
 
 
740
    def test_HTTP_PROXY(self):
 
741
        # pycurl do not check HTTP_PROXY for security reasons
 
742
        # (for use in a CGI context that we do not care
 
743
        # about. Should we ?)
 
744
        raise TestSkipped()
 
745
 
 
746
    def test_HTTP_PROXY_with_NO_PROXY(self):
 
747
        raise TestSkipped()