~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: Vincent Ladeuil
  • Date: 2007-03-01 21:26:57 UTC
  • mto: (2323.7.1 redirection)
  • mto: This revision was merged to the branch mainline in revision 2390.
  • Revision ID: v.ladeuil+lp@free.fr-20070301212657-bclmihdfnxgfy0sw
Take Aaron's review comments into account.

* bzrlib/transport/http/_urllib2_wrappers.py:
(HTTPRedirectHandler.http_error_301): Fix bug #88780.

* bzrlib/transport/http/__init__.py:
(HttpTransportBase._get): Fix doc string.

* bzrlib/transport/__init__.py:
(do_catching_redirections): Raise TooManyRedirections instead of
requiring an exception as a parameter.

* bzrlib/tests/test_http.py:
(TestHTTPRedirections.setUp): Simplified.
(TestHTTPRedirections.test_read_redirected_bundle_from_url): New test.
(TestDoCatchRedirections): New tests.

* bzrlib/tests/HTTPTestUtil.py:
(TestCaseWithRedirectedWebserver): New class factored out from
test_http.TestHTTPRedirections.

* bzrlib/errors.py:
(TooManyRedirections): New exception.

* bzrlib/bzrdir.py:
(BzrDir.open_from_transport.redirected): Catch TooManyRedirections.

* bzrlib/bundle/__init__.py:
(read_bundle_from_url.redirected_transport): Catch TooManyRedirections.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import threading
27
27
 
28
28
import bzrlib
29
 
from bzrlib import errors
30
 
from bzrlib import osutils
 
29
from bzrlib import (
 
30
    errors,
 
31
    osutils,
 
32
    urlutils,
 
33
    )
31
34
from bzrlib.tests import (
32
35
    TestCase,
33
36
    TestSkipped,
46
49
    InvalidStatusRequestHandler,
47
50
    NoRangeRequestHandler,
48
51
    SingleRangeRequestHandler,
 
52
    TestCaseWithRedirectedWebserver,
49
53
    TestCaseWithTwoWebservers,
50
54
    TestCaseWithWebserver,
51
55
    WallRequestHandler,
52
56
    )
53
57
from bzrlib.transport import (
 
58
    do_catching_redirections,
54
59
    get_transport,
55
60
    Transport,
56
61
    )
883
888
    """Test redirection between http servers.
884
889
 
885
890
    This MUST be used by daughter classes that also inherit from
886
 
    TestCaseWithTwoWebservers.
 
891
    TestCaseWithRedirectedWebserver.
887
892
 
888
893
    We can't inherit directly from TestCaseWithTwoWebservers or the
889
894
    test framework will try to create an instance which cannot
900
905
 
901
906
    def setUp(self):
902
907
        super(TestHTTPRedirections, self).setUp()
903
 
        # The redirections will point to the new server
904
 
        new_server = self.get_readonly_server()
905
 
        # The requests to the old server will be redirected
906
 
        old_server = self.get_secondary_server()
907
 
 
908
 
        self.build_tree_contents([('a', '0123456789')],)
909
 
 
910
 
        self.old_transport = self._transport(old_server.get_url())
 
908
        self.build_tree_contents([('a', '0123456789'),
 
909
                                  ('bundle',
 
910
                                  '# Bazaar revision bundle v0.9\n#\n')
 
911
                                  ],)
 
912
 
 
913
        self.old_transport = self._transport(self.old_server.get_url())
911
914
 
912
915
    def test_redirected(self):
913
916
        self.assertRaises(errors.RedirectRequested, self.old_transport.get, 'a')
 
917
        t = self._transport(self.new_server.get_url())
 
918
        self.assertEqual('0123456789', t.get('a').read())
 
919
 
 
920
    def test_read_redirected_bundle_from_url(self):
 
921
        from bzrlib.bundle import read_bundle_from_url
 
922
        url = self.old_transport.abspath('bundle')
 
923
        bundle = read_bundle_from_url(url)
 
924
        # If read_bundle_from_url was successful we get an empty bundle
 
925
        self.assertEqual([], bundle.revisions)
914
926
 
915
927
 
916
928
class TestHTTPRedirections_urllib(TestHTTPRedirections,
917
 
                                  TestCaseWithTwoWebservers):
 
929
                                  TestCaseWithRedirectedWebserver):
918
930
    """Tests redirections for urllib implementation"""
919
931
 
920
932
    _transport = HttpTransport_urllib
923
935
 
924
936
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
925
937
                                  TestHTTPRedirections,
926
 
                                  TestCaseWithTwoWebservers):
 
938
                                  TestCaseWithRedirectedWebserver):
927
939
    """Tests redirections for pycurl implementation"""
 
940
 
 
941
 
 
942
class TestDoCatchRedirections(TestCaseWithRedirectedWebserver):
 
943
    """Test transport.do_catching_redirections.
 
944
 
 
945
    We arbitrarily choose to use urllib transports
 
946
    """
 
947
 
 
948
    _transport = HttpTransport_urllib
 
949
 
 
950
    def setUp(self):
 
951
        super(TestDoCatchRedirections, self).setUp()
 
952
        self.build_tree_contents([('a', '0123456789'),],)
 
953
 
 
954
        self.old_transport = self._transport(self.old_server.get_url())
 
955
 
 
956
    def get_a(self, transport):
 
957
        return transport.get('a')
 
958
 
 
959
    def test_no_redirection(self):
 
960
        t = self._transport(self.new_server.get_url())
 
961
 
 
962
        # We use None for redirected so that we fail if redirected
 
963
        self.assertEquals('0123456789',
 
964
                          do_catching_redirections(self.get_a, t, None).read())
 
965
 
 
966
    def test_one_redirection(self):
 
967
        self.redirections = 0
 
968
 
 
969
        def redirected(transport, exception, redirection_notice):
 
970
            self.redirections += 1
 
971
            dir, file = urlutils.split(exception.target)
 
972
            return self._transport(dir)
 
973
 
 
974
        self.assertEquals('0123456789',
 
975
                          do_catching_redirections(self.get_a,
 
976
                                                   self.old_transport,
 
977
                                                   redirected
 
978
                                                   ).read())
 
979
        self.assertEquals(1, self.redirections)
 
980
 
 
981
    def test_redirection_loop(self):
 
982
 
 
983
        def redirected(transport, exception, redirection_notice):
 
984
            # By using the redirected url as a base dir for the
 
985
            # *old* transport, we create a loop: a => a/a =>
 
986
            # a/a/a
 
987
            return self.old_transport.clone(exception.target)
 
988
 
 
989
        self.assertRaises(errors.TooManyRedirections, do_catching_redirections,
 
990
                          self.get_a, self.old_transport, redirected)