~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-04-21 09:26:30 UTC
  • mto: (2420.1.21 bzr.http.auth)
  • mto: This revision was merged to the branch mainline in revision 2463.
  • Revision ID: v.ladeuil+lp@free.fr-20070421092630-ty7a90wrx4v2x5k8
Refactor proxy and auth test classes. Tests failing for digest auth.

* bzrlib/transport/http/_urllib2_wrappers.py:
(AbstractAuthHandler.auth_required,
AbstractAuthHandler.auth_successful): Add response parameter as an
authentication scheme may need additional info provided only
there.

* bzrlib/tests/test_http.py:
(TestAuth, TestHTTPAuth, TestProxyAuth, TestHTTPBasicAuth,
TestHTTPProxyBasicAuth): Refactoring.
(TestHTTPDigestAuth, TestHTTPProxyDigestAuth): New classes for
digest authentication tests.

* bzrlib/tests/HttpServer.py:
(TestingHTTPRequestHandler.translate_path): Handles request
proxying based on a server attribute.
(HttpServer): Add proxy_requests attribute.

* bzrlib/tests/HTTPTestUtil.py:
(ProxyServer): Replaces FakeProxyRequestHandler now that
HttpServer handles proxying based on a server attribute.
(AuthRequestHandler, BasicAuthRequestHandler, AuthServer,
HTTPAuthServer, ProxyAuthServer, HTTPBasicAuthServer,
ProxyBasicAuthServer): Refactoring Auth servers and request
handlers.
(HTTPDigestAuthServer, ProxyDigestAuthServer): New classes for
digest authentication.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
from bzrlib.tests.HTTPTestUtil import (
48
48
    BadProtocolRequestHandler,
49
49
    BadStatusRequestHandler,
50
 
    BasicAuthHTTPServer,
51
 
    FakeProxyRequestHandler,
52
50
    ForbiddenRequestHandler,
 
51
    HTTPBasicAuthServer,
 
52
    HTTPDigestAuthServer,
53
53
    HTTPServerRedirecting,
54
54
    InvalidStatusRequestHandler,
55
55
    NoRangeRequestHandler,
56
 
    ProxyBasicAuthHTTPServer,
 
56
    ProxyBasicAuthServer,
 
57
    ProxyDigestAuthServer,
 
58
    ProxyServer,
57
59
    SingleRangeRequestHandler,
58
60
    TestCaseWithRedirectedWebserver,
59
61
    TestCaseWithTwoWebservers,
788
790
        """Creates an http server that will serve files with
789
791
        '-proxied' appended to their names.
790
792
        """
791
 
        return HttpServer(FakeProxyRequestHandler)
 
793
        return ProxyServer()
792
794
 
793
795
    def _install_env(self, env):
794
796
        for name, value in env.iteritems():
1133
1135
                          self.get_a, self.old_transport, redirected)
1134
1136
 
1135
1137
 
1136
 
class TestHTTPAuth(object):
 
1138
class TestAuth(object):
1137
1139
    """Test some authentication scheme specified by daughter class.
1138
1140
 
1139
1141
    This MUST be used by daughter classes that also inherit from
1216
1218
        self.assertEqual(1, self.server.auth_required_errors)
1217
1219
 
1218
1220
 
1219
 
class TestHTTPBasicAuth(TestHTTPAuth, TestCaseWithWebserver):
1220
 
    """Test basic http authentication scheme"""
1221
 
 
1222
 
    _transport = HttpTransport_urllib
 
1221
class TestHTTPAuth(TestAuth):
 
1222
    """Test HTTP authentication schemes.
 
1223
 
 
1224
    Daughter classes MUST inherit from TestCaseWithWebserver too.
 
1225
    """
 
1226
 
1223
1227
    _auth_header = 'Authorization'
1224
1228
 
1225
1229
    def setUp(self):
1226
1230
        TestCaseWithWebserver.setUp(self)
1227
1231
        self.server = self.get_readonly_server()
1228
 
        TestHTTPAuth.setUp(self)
1229
 
 
1230
 
    def create_transport_readonly_server(self):
1231
 
        return BasicAuthHTTPServer()
 
1232
        TestAuth.setUp(self)
1232
1233
 
1233
1234
    def get_user_transport(self, user, password=None):
1234
1235
        return self._transport(self.get_user_url(user, password))
1235
1236
 
1236
1237
 
1237
 
class TestHTTPProxyBasicAuth(TestHTTPAuth, TestCaseWithTwoWebservers):
1238
 
    """Test basic http authentication scheme"""
 
1238
class TestProxyAuth(TestAuth):
 
1239
    """Test proxy authentication schemes.
1239
1240
 
1240
 
    _transport = HttpTransport_urllib
 
1241
    Daughter classes MUST also inherit from TestCaseWithWebserver.
 
1242
    """
1241
1243
    _auth_header = 'Proxy-authorization'
1242
1244
 
1243
1245
    def setUp(self):
1244
 
        TestCaseWithTwoWebservers.setUp(self)
 
1246
        TestCaseWithWebserver.setUp(self)
1245
1247
        self.server = self.get_readonly_server()
1246
1248
        self._old_env = {}
1247
1249
        self.addCleanup(self._restore_env)
1248
 
        TestHTTPAuth.setUp(self)
 
1250
        TestAuth.setUp(self)
1249
1251
        # Override the contents to avoid false positives
1250
1252
        self.build_tree_contents([('a', 'not proxied contents of a\n'),
1251
1253
                                  ('b', 'not proxied contents of b\n'),
1253
1255
                                  ('b-proxied', 'contents of b\n'),
1254
1256
                                  ])
1255
1257
 
1256
 
    def create_transport_readonly_server(self):
1257
 
        return ProxyBasicAuthHTTPServer()
1258
 
 
1259
 
    def _install_env(self, env):
1260
 
        for name, value in env.iteritems():
1261
 
            self._old_env[name] = osutils.set_or_unset_env(name, value)
1262
 
 
1263
 
    def _restore_env(self):
1264
 
        for name, value in self._old_env.iteritems():
1265
 
            osutils.set_or_unset_env(name, value)
1266
 
 
1267
1258
    def get_user_transport(self, user, password=None):
1268
1259
        self._install_env({'all_proxy': self.get_user_url(user, password)})
1269
1260
        return self._transport(self.server.get_url())
 
1261
 
 
1262
    def _install_env(self, env):
 
1263
        for name, value in env.iteritems():
 
1264
            self._old_env[name] = osutils.set_or_unset_env(name, value)
 
1265
 
 
1266
    def _restore_env(self):
 
1267
        for name, value in self._old_env.iteritems():
 
1268
            osutils.set_or_unset_env(name, value)
 
1269
 
 
1270
 
 
1271
class TestHTTPBasicAuth(TestHTTPAuth, TestCaseWithWebserver):
 
1272
    """Test http basic authentication scheme"""
 
1273
 
 
1274
    _transport = HttpTransport_urllib
 
1275
 
 
1276
    def create_transport_readonly_server(self):
 
1277
        return HTTPBasicAuthServer()
 
1278
 
 
1279
 
 
1280
class TestHTTPProxyBasicAuth(TestProxyAuth, TestCaseWithWebserver):
 
1281
    """Test proxy basic authentication scheme"""
 
1282
 
 
1283
    _transport = HttpTransport_urllib
 
1284
 
 
1285
    def create_transport_readonly_server(self):
 
1286
        return ProxyBasicAuthServer()
 
1287
 
 
1288
 
 
1289
class TestHTTPDigestAuth(TestHTTPAuth, TestCaseWithWebserver):
 
1290
    """Test http digest authentication scheme"""
 
1291
 
 
1292
    _transport = HttpTransport_urllib
 
1293
 
 
1294
    def create_transport_readonly_server(self):
 
1295
        return HTTPDigestAuthServer()
 
1296
 
 
1297
 
 
1298
class TestHTTPProxyDigestAuth(TestProxyAuth, TestCaseWithWebserver):
 
1299
    """Test proxy digest authentication scheme"""
 
1300
 
 
1301
    _transport = HttpTransport_urllib
 
1302
 
 
1303
    def create_transport_readonly_server(self):
 
1304
        return ProxyDigestAuthServer()
 
1305