@@ -484,6 +484,32 @@ def matches(self, request: "PreparedRequest") -> Tuple[bool, str]:
484484 return True , ""
485485
486486
487+ def _form_response (
488+ body : Union [BufferedReader , BytesIO ],
489+ headers : Optional [Mapping [str , str ]],
490+ status : int ,
491+ ) -> HTTPResponse :
492+ # The requests library's cookie handling depends on the response object
493+ # having an original response object with the headers as the `msg`, so
494+ # we give it what it needs.
495+ data = BytesIO ()
496+ data .close ()
497+
498+ orig_response = HTTPResponse (
499+ body = data , # required to avoid "ValueError: Unable to determine whether fp is closed."
500+ msg = headers ,
501+ preload_content = False ,
502+ )
503+ return HTTPResponse (
504+ status = status ,
505+ reason = client .responses .get (status , None ),
506+ body = body ,
507+ headers = headers ,
508+ original_response = orig_response ,
509+ preload_content = False ,
510+ )
511+
512+
487513class Response (BaseResponse ):
488514 def __init__ (
489515 self ,
@@ -545,14 +571,7 @@ def get_response(self, request: "PreparedRequest") -> HTTPResponse:
545571 content_length = len (body .getvalue ())
546572 headers ["Content-Length" ] = str (content_length )
547573
548- return HTTPResponse (
549- status = status ,
550- reason = client .responses .get (status , None ),
551- body = body ,
552- headers = headers ,
553- original_response = OriginalResponseShim (headers ),
554- preload_content = False ,
555- )
574+ return _form_response (body , headers , status )
556575
557576 def __repr__ (self ) -> str :
558577 return (
@@ -614,44 +633,14 @@ def get_response(self, request: "PreparedRequest") -> HTTPResponse:
614633 body = _handle_body (body )
615634 headers .extend (r_headers )
616635
617- return HTTPResponse (
618- status = status ,
619- reason = client .responses .get (status , None ),
620- body = body ,
621- headers = headers ,
622- original_response = OriginalResponseShim (headers ),
623- preload_content = False ,
624- )
636+ return _form_response (body , headers , status )
625637
626638
627639class PassthroughResponse (BaseResponse ):
628640 def __init__ (self , * args : Any , ** kwargs : Any ):
629641 super ().__init__ (* args , passthrough = True , ** kwargs )
630642
631643
632- class OriginalResponseShim (object ):
633- """
634- Shim for compatibility with older versions of urllib3
635-
636- requests cookie handling depends on responses having a property chain of
637- `response._original_response.msg` which contains the response headers [1]
638-
639- Using HTTPResponse() for this purpose causes compatibility errors with
640- urllib3<1.23.0. To avoid adding more dependencies we can use this shim.
641-
642- [1]: https://github.com/psf/requests/blob/75bdc998e2d/requests/cookies.py#L125
643- """
644-
645- def __init__ (self , headers : Any ) -> None :
646- self .msg : Any = headers
647-
648- def isclosed (self ) -> bool :
649- return True
650-
651- def close (self ) -> None :
652- return
653-
654-
655644class RequestsMock (object ):
656645 DELETE : Literal ["DELETE" ] = "DELETE"
657646 GET : Literal ["GET" ] = "GET"
0 commit comments