9595"""
9696
9797
98- def readLine (file_ ):
99- line = file_ .readline ()
100- if isinstance (line , bytes ):
101- line = line .decode ("utf-8" )
102- return line .strip ()
103-
104-
10598class FakeHttpResponse :
10699 def __init__ (self , status , headers , output ):
107100 self .status = status
@@ -229,7 +222,6 @@ def setOpenFile(func):
229222
230223 def __init__ (self , protocol , host , port , * args , ** kwds ):
231224 self .__file = self .__openFile ("r" )
232- self .__line = 0
233225 self .__protocol = protocol
234226 self .__host = host
235227 self .__port = port
@@ -263,29 +255,25 @@ def request(
263255 self .__stream = stream
264256 self .__cnx .request (verb , url , input , headers , stream = stream )
265257
266- def __readLine (self ) -> str :
267- self .__line += 1
268- return readLine (self .__file )
269-
270- def __replayDataMismatchFilePos (self ) -> str :
271- return f"Replay data mismatch in { self .__file .name } :{ self .__line } "
258+ def __replayDataMismatchLine (self ) -> str :
259+ return f"Replay data mismatch in { self .__file } "
272260
273261 def __readNextRequest (self , verb , url , input , headers ):
274262 fixAuthorizationHeader (headers )
275- assert self .__protocol == self .__readLine (), self .__replayDataMismatchFilePos ()
276- assert verb == self .__readLine (), self .__replayDataMismatchFilePos ()
277- assert self .__host == self .__readLine (), self .__replayDataMismatchFilePos ()
278- assert str (self .__port ) == self .__readLine (), self .__replayDataMismatchFilePos ()
279- assert self .__splitUrl (url ) == self .__splitUrl (self .__readLine ()), self .__replayDataMismatchFilePos ()
280- assert headers == eval (self .__readLine ()), self .__replayDataMismatchFilePos ()
281- expectedInput = self .__readLine ()
263+ assert self .__protocol == self .__file . readline (), self .__replayDataMismatchLine ()
264+ assert verb == self .__file . readline (), self .__replayDataMismatchLine ()
265+ assert self .__host == self .__file . readline (), self .__replayDataMismatchLine ()
266+ assert str (self .__port ) == self .__file . readline (), self .__replayDataMismatchLine ()
267+ assert self .__splitUrl (url ) == self .__splitUrl (self .__file . readline ()), self .__replayDataMismatchLine ()
268+ assert headers == eval (self .__file . readline ()), self .__replayDataMismatchLine ()
269+ expectedInput = self .__file . readline ()
282270 if isinstance (input , str ):
283271 trInput = input .replace ("\n " , "" ).replace ("\r " , "" )
284272 if input .startswith ("{" ):
285- assert expectedInput .startswith ("{" ), self .__replayDataMismatchFilePos ()
286- assert json .loads (trInput ) == json .loads (expectedInput ), self .__replayDataMismatchFilePos ()
273+ assert expectedInput .startswith ("{" ), self .__replayDataMismatchLine ()
274+ assert json .loads (trInput ) == json .loads (expectedInput ), self .__replayDataMismatchLine ()
287275 else :
288- assert trInput == expectedInput , self .__replayDataMismatchFilePos ()
276+ assert trInput == expectedInput , self .__replayDataMismatchLine ()
289277 else :
290278 # for non-string input (e.g. upload asset), let it pass.
291279 pass
@@ -301,19 +289,19 @@ def __splitUrl(self, url):
301289 def __request_callback (self , request , uri , response_headers ):
302290 self .__readNextRequest (self .__cnx .verb , self .__cnx .url , self .__cnx .input , self .__cnx .headers )
303291
304- status = int (self .__readLine ())
305- self .response_headers = CaseInsensitiveDict (eval (self .__readLine ()))
292+ status = int (self .__file . readline ())
293+ self .response_headers = CaseInsensitiveDict (eval (self .__file . readline ()))
306294 if self .__stream :
307295 output = BytesIO ()
308296 while True :
309- line = self .__readLine ()
297+ line = self .__file . readline ()
310298 if not line :
311299 break
312300 output .write (base64 .b64decode (line ))
313301 output = output .getvalue ()
314302 else :
315- output = bytearray (self .__readLine (), "utf-8" )
316- self .__readLine ()
303+ output = bytearray (self .__file . readline (), "utf-8" )
304+ self .__file . readline ()
317305
318306 # make a copy of the headers and remove the ones that interfere with the response handling
319307 adding_headers = CaseInsensitiveDict (self .response_headers )
@@ -351,6 +339,35 @@ def __init__(self, *args, **kwds):
351339 super ().__init__ ("https" , * args , ** kwds )
352340
353341
342+ class ReplayDataFile :
343+ @staticmethod
344+ def open (filename : str , mode : str , encoding : str ) -> ReplayDataFile :
345+ file = open (filename , mode , encoding = encoding )
346+ return ReplayDataFile (filename , file )
347+
348+ def __init__ (self , filename : str , file ):
349+ self .__filename = filename
350+ self .__file = file
351+ self .__line = 0
352+
353+ def __repr__ (self ) -> str :
354+ return f"{ self .__filename } :{ self .__line } "
355+
356+ def readline (self ) -> str :
357+ self .__line += 1
358+ line = self .__file .readline ()
359+ if isinstance (line , bytes ):
360+ line = line .decode ("utf-8" )
361+ return line .strip ()
362+
363+ @property
364+ def line_number (self ):
365+ return self .__line
366+
367+ def close (self ):
368+ self .__file .close ()
369+
370+
354371class BasicTestCase (unittest .TestCase ):
355372 recordMode = False
356373 replayDataFolder = os .path .join (os .path .dirname (__file__ ), "ReplayData" )
@@ -471,15 +488,15 @@ def __openFile(self, mode):
471488 if fileName != self .__fileName :
472489 self .__closeReplayFileIfNeeded ()
473490 self .__fileName = fileName
474- self .__file = open (self .__fileName , mode , encoding = "utf-8" )
491+ self .__file = ReplayDataFile . open (self .__fileName , mode , encoding = "utf-8" )
475492 return self .__file
476493
477494 def __closeReplayFileIfNeeded (self , silent = False ):
478495 if self .__file is not None :
479496 if (
480497 not self .recordMode and not silent
481498 ): # pragma no branch (Branch useful only when recording new tests, not used during automated tests)
482- self .assertEqual (readLine ( self .__file ), "" , self .__fileName )
499+ self .assertEqual (self .__file . readline ( ), "" , self .__file )
483500 self .__file .close ()
484501
485502 def assertListKeyEqual (self , elements , key , expectedKeys ):
0 commit comments