Commits

Olemis Lang  committed 5631584

BH RPC : Generic RPC transport solution by reusing inherited urllib2 capabilities

  • Participants
  • Parent commits 5f09b1d

Comments (0)

Files changed (1)

File trunk/tracrpc/tests/__init__.py

     # RPC functional testers for HTTP digest auth
     #--------------
 
-    class XmlRpcDigestTester(functional.HttpAuthTester, XmlRpcTester):
-        """XML-RPC over HTTP Digest authentication.
+    class AdvancedXmlRpcTester(functional.HttpAuthTester, XmlRpcTester):
+        """Abstract XML-RPC tester supporting custom urllib2 handlers.
 
         Based on
         http://trac-hacks.org/wiki/XmlRpcPlugin#UsingDigestAuthenticationinpython
         http://bytes.com/topic/python/answers/509382-solution-xml-rpc-over-proxy
         """
-        url_auth_handlers = urllib2.HTTPDigestAuthHandler
-
-        class _DigestTransportMixin(object):
-            def __init__(self, username, pw, realm, verbose = None, 
+        class _TransportMixin(object):
+            def __init__(self, username, pw, build_opener, verbose = None,
                          use_datetime=0):
-                self.__username = username
-                self.__pw = pw
-                self.__realm = realm
+                self._username = username
+                self._pw = pw
                 self.verbose = verbose
                 self._use_datetime = use_datetime
+                # FIXME: Tester ?
+                self.build_opener = build_opener
 
             # scheme_prefix overridden in subclasses
             scheme_prefix = None
                 request.add_header("User-Agent", self.user_agent)
                 request.add_header("Content-Type", "text/xml") # Important
 
-                # setup digest authentication
-                authhandler = urllib2.HTTPDigestAuthHandler()
-                authhandler.add_password(self.__realm, url, self.__username,
-                                         self.__pw)
-                opener = urllib2.build_opener(authhandler)
-
-                # TODO: Add support for other features e.g. HTTP proxy ?
-                #proxy_handler=urllib2.ProxyHandler()
-                #opener=urllib2.build_opener(proxy_handler)
+                # implicit handlers setup e.g. digest authentication
+                opener = self.build_opener(url, self._username, self._pw)
                 f=opener.open(request)
                 return(self.parse_response(f))            
 
-        class SafeTransport(_DigestTransportMixin, xmlrpclib.SafeTransport):
+        class SafeTransport(_TransportMixin, xmlrpclib.SafeTransport):
             scheme_prefix = 'https://'
 
-        class Transport(_DigestTransportMixin, xmlrpclib.Transport):
+        class Transport(_TransportMixin, xmlrpclib.Transport):
             scheme_prefix = 'http://'
 
         def transport(self, rpc_url, user):
             else:
                 raise ValueError("Unsupported URL scheme %s" % (rpc_url,))
             # TODO: Real parameterization
-            return cls(user, user, 'bloodhound', verbose=None, use_datetime=0)
+            return cls(user, user, self.build_opener, verbose=None,
+                       use_datetime=0)
+
+
+    class XmlRpcDigestTester(AdvancedXmlRpcTester):
+        """XML-RPC over HTTP Digest authentication.
+
+        Based on
+        http://trac-hacks.org/wiki/XmlRpcPlugin#UsingDigestAuthenticationinpython
+        http://bytes.com/topic/python/answers/509382-solution-xml-rpc-over-proxy
+        """
+        url_auth_handlers = urllib2.HTTPDigestAuthHandler
 
 
     class DefaultRpcTester(XmlRpcDigestTester,