~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: v.ladeuil+lp at free
  • Date: 2006-12-11 13:24:55 UTC
  • mto: (2182.1.1 Aaron's integration)
  • mto: This revision was merged to the branch mainline in revision 2183.
  • Revision ID: v.ladeuil+lp@free.fr-20061211132455-e8k81db8ktfwvrlz
Tests for proxies, covering  #74759.

* bzrlib/transport/http/_urllib2_wrappers.py:
(ProxyHandler.proxy_bypass): Matches against the modified regexp,
not the original domain.

* bzrlib/tests/test_http.py:
(TestProxyHttpServer, TestProxyHttpServer_urllib,
TestProxyHttpServer_pycurl): New classes for proxy tests.

* bzrlib/tests/HttpServer.py:
(HttpServer._http_start): Give access to the port used by the
server socket.
(HttpServer.setUp, HttpServer.tearDown): Handles the 'no_proxy'
env var too.

* bzrlib/tests/HTTPTestUtil.py:
(TestCaseWithTwoWebservers): New class for tests needing two
related web servers.
(FakeProxyRequestHandler): New class to fake a proxy http server.

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
 
 
637
    def setUp(self):
 
638
        TestCaseWithTwoWebservers.setUp(self)
 
639
        self.build_tree_contents([('foo', 'contents of foo\n'),
 
640
                                  ('foo-proxied', 'proxied contents of foo\n')])
 
641
        # Create the server now. Otherwise, it will created when
 
642
        # test methods are invoked. At that point the test
 
643
        # methods may have set env vars and the HttpServer
 
644
        # creation will erase them.
 
645
        server = self.get_readonly_server()
 
646
        # The secondary server is the proxy
 
647
 
 
648
        # NOTE: We do not setup a real proxy, instead we check
 
649
        # that the connection goes thru the proxy by serving
 
650
        # different content
 
651
        proxy = self.get_secondary_server()
 
652
        proxy_url = proxy.get_url()
 
653
        # All tests will rely on the secondary server being set
 
654
        # as the proxy
 
655
        os.environ['http_proxy'] = proxy_url
 
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 test_http_proxy(self):
 
664
        server = self.get_readonly_server()
 
665
        url = server.get_url()
 
666
        t = self._transport(url)
 
667
        self.assertEqual(t.get('foo').read(),
 
668
                         'proxied contents of foo\n')
 
669
 
 
670
    def test_http_no_proxy(self):
 
671
        server = self.get_readonly_server()
 
672
        url = server.get_url()
 
673
        os.environ['no_proxy'] = 'localhost:%d' % server.port
 
674
        t = self._transport(url)
 
675
        try:
 
676
            self.assertEqual(t.get('foo').read(),
 
677
                             'contents of foo\n')
 
678
        finally:
 
679
            del os.environ['no_proxy']
 
680
 
 
681
 
 
682
class TestProxyHttpServer_urllib(TestProxyHttpServer,
 
683
                                 TestCaseWithTwoWebservers):
 
684
    """Tests proxy server for urllib implementation"""
 
685
 
 
686
    _transport = HttpTransport_urllib
 
687
 
 
688
 
 
689
class TestProxyHttpServer_pycurl(TestWithTransport_pycurl,
 
690
                                 TestProxyHttpServer,
 
691
                                 TestCaseWithTwoWebservers):
 
692
    """Tests proxy server for pycurl implementation"""
 
693
 
 
694
    def test_http_no_proxy(self):
 
695
        server = self.get_readonly_server()
 
696
        url = server.get_url()
 
697
        # Oh my ! pycurl do not check the port part :-( So we
 
698
        # just test the host part
 
699
        os.environ['no_proxy'] = 'localhost'
 
700
        t = self._transport(url)
 
701
        try:
 
702
            self.assertEqual(t.get('foo').read(),
 
703
                             'contents of foo\n')
 
704
        finally:
 
705
            del os.environ['no_proxy']