From e4124980faf031626f4bba6e2fd04dd2d0930cce Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Thu, 23 Aug 2012 09:28:47 +0200 Subject: [PATCH] Fix record/replay in test framework --- github/Requester.py | 14 +++++++++++--- test/Framework.py | 21 +++++++++++++-------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/github/Requester.py b/github/Requester.py index 5d9c8857..be96e597 100644 --- a/github/Requester.py +++ b/github/Requester.py @@ -24,6 +24,14 @@ except ImportError: #pragma no cover: only for Python 2.5 import GithubException class Requester: + __httpConnectionClass = httplib.HTTPConnection + __httpsConnectionClass = httplib.HTTPSConnection + + @classmethod + def injectConnectionClasses( cls, httpConnectionClass, httpsConnectionClass ): + cls.__httpConnectionClass = httpConnectionClass + cls.__httpsConnectionClass = httpsConnectionClass + def __init__( self, login_or_token, password, base_url ): if password is not None: login = login_or_token @@ -40,9 +48,9 @@ class Requester: self.__port = o.port self.__prefix = o.path if o.scheme == "https": - self.__connection_class = httplib.HTTPSConnection + self.__connectionClass = self.__httpsConnectionClass elif o.scheme == "http": - self.__connection_class = httplib.HTTPConnection + self.__connectionClass = self.__httpConnectionClass else: assert( False ) #pragma no cover @@ -71,7 +79,7 @@ class Requester: if self.__authorizationHeader is not None: headers[ "Authorization" ] = self.__authorizationHeader - cnx = self.__connection_class( host = self.__hostname, port = self.__port, strict = True ) + cnx = self.__connectionClass( host = self.__hostname, port = self.__port, strict = True ) cnx.request( verb, self.__completeUrl( url, parameters ), diff --git a/test/Framework.py b/test/Framework.py index fd70f350..f41d1592 100644 --- a/test/Framework.py +++ b/test/Framework.py @@ -77,14 +77,15 @@ class RecordingConnection: class RecordingHttpConnection( RecordingConnection ): _realConnection = httplib.HTTPConnection - def __init__( self, file, host, port, *args, **kwds ): - RecordingConnection.__init__( self, file, "http", host, port, *args, **kwds ) + def __init__( self, file, *args, **kwds ): + RecordingConnection.__init__( self, file, "http", *args, **kwds ) class RecordingHttpsConnection( RecordingConnection ): _realConnection = httplib.HTTPSConnection - def __init__( self, file, host, port, *args, **kwds ): - RecordingConnection.__init__( self, file, "https", host, port, *args, **kwds ) + def __init__( self, file, *args, **kwds ): + print args, kwds + RecordingConnection.__init__( self, file, "https", *args, **kwds ) class ReplayingConnection: def __init__( self, testCase, file, protocol, host, port, *args, **kwds ): @@ -123,15 +124,19 @@ class BasicTestCase( unittest.TestCase ): self.__fileName = "" self.__file = None if self.recordMode: - httplib.HTTPSConnection = lambda *args, **kwds: RecordingHttpsConnection( self.__openFile( "wb" ), *args, **kwds ) - httplib.HTTPConnection = lambda *args, **kwds: RecordingHttpConnection( self.__openFile( "wb" ), *args, **kwds ) + github.Requester.Requester.injectConnectionClasses( + lambda ignored, *args, **kwds: RecordingHttpConnection( self.__openFile( "wb" ), *args, **kwds ), + lambda ignored, *args, **kwds: RecordingHttpsConnection( self.__openFile( "wb" ), *args, **kwds ) + ) import GithubCredentials self.login = GithubCredentials.login self.password = GithubCredentials.password self.oauth_token = GithubCredentials.oauth_token else: - httplib.HTTPSConnection = lambda *args, **kwds: ReplayingHttpsConnection( self, self.__openFile( "r" ), *args, **kwds ) - httplib.HTTPConnection = lambda *args, **kwds: ReplayingHttpConnection( self, self.__openFile( "r" ), *args, **kwds ) + github.Requester.Requester.injectConnectionClasses( + lambda ignored, *args, **kwds: ReplayingHttpConnection( self, self.__openFile( "r" ), *args, **kwds ), + lambda ignored, *args, **kwds: ReplayingHttpsConnection( self, self.__openFile( "r" ), *args, **kwds ) + ) self.login = "login" self.password = "password" self.oauth_token = "oauth_token"