~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-02-09 19:53:30 UTC
  • mfrom: (2278.1.1 Aaron's integration)
  • Revision ID: pqm@pqm.ubuntu.com-20070209195330-312ec52588462782
Fix proxy authentication on urllib (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
636
636
    """Tests range requests refusing server for pycurl implementation"""
637
637
 
638
638
 
 
639
class TestHttpProxyWhiteBox(TestCase):
 
640
    """Whitebox test proxy http authorization."""
 
641
 
 
642
    def setUp(self):
 
643
        TestCase.setUp(self)
 
644
        self._old_env = {}
 
645
 
 
646
    def tearDown(self):
 
647
        self._restore_env()
 
648
 
 
649
    def _set_and_capture_env_var(self, name, new_value):
 
650
        """Set an environment variable, and reset it when finished."""
 
651
        self._old_env[name] = osutils.set_or_unset_env(name, new_value)
 
652
 
 
653
    def _install_env(self, env):
 
654
        for name, value in env.iteritems():
 
655
            self._set_and_capture_env_var(name, value)
 
656
 
 
657
    def _restore_env(self):
 
658
        for name, value in self._old_env.iteritems():
 
659
            osutils.set_or_unset_env(name, value)
 
660
 
 
661
    def _proxied_request(self):
 
662
        from bzrlib.transport.http._urllib2_wrappers import (
 
663
            ProxyHandler,
 
664
            Request,
 
665
            )
 
666
 
 
667
        handler = ProxyHandler()
 
668
        request = Request('GET','http://baz/buzzle')
 
669
        handler.set_proxy(request, 'http')
 
670
        return request
 
671
 
 
672
    def test_empty_user(self):
 
673
        self._install_env({'http_proxy': 'http://bar.com'})
 
674
        request = self._proxied_request()
 
675
        self.assertFalse(request.headers.has_key('Proxy-authorization'))
 
676
 
 
677
    def test_empty_pass(self):
 
678
        self._install_env({'http_proxy': 'http://joe@bar.com'})
 
679
        request = self._proxied_request()
 
680
        self.assertEqual('Basic ' + 'joe:'.encode('base64').strip(),
 
681
                         request.headers['Proxy-authorization'])
 
682
    def test_user_pass(self):
 
683
        self._install_env({'http_proxy': 'http://joe:foo@bar.com'})
 
684
        request = self._proxied_request()
 
685
        self.assertEqual('Basic ' + 'joe:foo'.encode('base64').strip(),
 
686
                         request.headers['Proxy-authorization'])
 
687
 
 
688
 
 
689
 
639
690
class TestProxyHttpServer(object):
640
691
    """Tests proxy server.
641
692
 
655
706
    # FIXME: We don't have an https server available, so we don't
656
707
    # test https connections.
657
708
 
 
709
    # FIXME: Once the test suite is better fitted to test
 
710
    # authorization schemes, test proxy authorizations too (see
 
711
    # bug #83954).
 
712
 
658
713
    def setUp(self):
659
714
        TestCaseWithTwoWebservers.setUp(self)
660
715
        self.build_tree_contents([('foo', 'contents of foo\n'),