Skip to content

Commit

Permalink
remove OriginalResponseShim (#585)
Browse files Browse the repository at this point in the history
Replace this shim class with HTTPResponse
  • Loading branch information
beliaev-maksim committed Sep 12, 2022
1 parent d4ace43 commit caca43c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -9,6 +9,7 @@
* Fix type annotation for `CallList`
* Add `passthrough` argument to `BaseResponse` object. See #557
* Fix `registries` leak. See #563
* `OriginalResponseShim` is removed. See #585
* Add support for the `loose` version of `json_params_matcher` via named argument `strict_match`. See #551
* Add lists support as JSON objects in `json_params_matcher`. See #559
* Added project links to pypi listing.
Expand Down
67 changes: 28 additions & 39 deletions responses/__init__.py
Expand Up @@ -484,6 +484,32 @@ def matches(self, request: "PreparedRequest") -> Tuple[bool, str]:
return True, ""


def _form_response(
body: Union[BufferedReader, BytesIO],
headers: Optional[Mapping[str, str]],
status: int,
) -> HTTPResponse:
# The requests library's cookie handling depends on the response object
# having an original response object with the headers as the `msg`, so
# we give it what it needs.
data = BytesIO()
data.close()

orig_response = HTTPResponse(
body=data, # required to avoid "ValueError: Unable to determine whether fp is closed."
msg=headers,
preload_content=False,
)
return HTTPResponse(
status=status,
reason=client.responses.get(status, None),
body=body,
headers=headers,
original_response=orig_response,
preload_content=False,
)


class Response(BaseResponse):
def __init__(
self,
Expand Down Expand Up @@ -545,14 +571,7 @@ def get_response(self, request: "PreparedRequest") -> HTTPResponse:
content_length = len(body.getvalue())
headers["Content-Length"] = str(content_length)

return HTTPResponse(
status=status,
reason=client.responses.get(status, None),
body=body,
headers=headers,
original_response=OriginalResponseShim(headers),
preload_content=False,
)
return _form_response(body, headers, status)

def __repr__(self) -> str:
return (
Expand Down Expand Up @@ -614,44 +633,14 @@ def get_response(self, request: "PreparedRequest") -> HTTPResponse:
body = _handle_body(body)
headers.extend(r_headers)

return HTTPResponse(
status=status,
reason=client.responses.get(status, None),
body=body,
headers=headers,
original_response=OriginalResponseShim(headers),
preload_content=False,
)
return _form_response(body, headers, status)


class PassthroughResponse(BaseResponse):
def __init__(self, *args: Any, **kwargs: Any):
super().__init__(*args, passthrough=True, **kwargs)


class OriginalResponseShim(object):
"""
Shim for compatibility with older versions of urllib3
requests cookie handling depends on responses having a property chain of
`response._original_response.msg` which contains the response headers [1]
Using HTTPResponse() for this purpose causes compatibility errors with
urllib3<1.23.0. To avoid adding more dependencies we can use this shim.
[1]: https://github.com/psf/requests/blob/75bdc998e2d/requests/cookies.py#L125
"""

def __init__(self, headers: Any) -> None:
self.msg: Any = headers

def isclosed(self) -> bool:
return True

def close(self) -> None:
return


class RequestsMock(object):
DELETE: Literal["DELETE"] = "DELETE"
GET: Literal["GET"] = "GET"
Expand Down

0 comments on commit caca43c

Please sign in to comment.