Fix record/replay in test framework

This commit is contained in:
Vincent Jacques
2012-08-23 09:28:47 +02:00
parent bf1e3b588b
commit e4124980fa
2 changed files with 24 additions and 11 deletions
+11 -3
View File
@@ -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 ),
+13 -8
View File
@@ -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"