summaryrefslogtreecommitdiffstats
path: root/contrib/python/responses
diff options
context:
space:
mode:
authorrobot-piglet <[email protected]>2024-06-21 09:28:26 +0300
committerrobot-piglet <[email protected]>2024-06-21 09:36:40 +0300
commit0cb3f820fac6a243bcb7e4c4388700898660bfd0 (patch)
tree056f1b8bc5f72039fa422aac0af13bab0e966aa7 /contrib/python/responses
parent08049311fe5c42a97e8bb47a73fb6cd143c0bdb1 (diff)
Intermediate changes
Diffstat (limited to 'contrib/python/responses')
-rw-r--r--contrib/python/responses/py3/.dist-info/METADATA2
-rw-r--r--contrib/python/responses/py3/responses/__init__.py8
-rw-r--r--contrib/python/responses/py3/responses/matchers.py86
-rw-r--r--contrib/python/responses/py3/ya.make2
4 files changed, 34 insertions, 64 deletions
diff --git a/contrib/python/responses/py3/.dist-info/METADATA b/contrib/python/responses/py3/.dist-info/METADATA
index c658b290658..c12dde8ca85 100644
--- a/contrib/python/responses/py3/.dist-info/METADATA
+++ b/contrib/python/responses/py3/.dist-info/METADATA
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: responses
-Version: 0.25.0
+Version: 0.25.2
Summary: A utility library for mocking out the `requests` Python library.
Home-page: https://github.com/getsentry/responses
Author: David Cramer
diff --git a/contrib/python/responses/py3/responses/__init__.py b/contrib/python/responses/py3/responses/__init__.py
index d990091fa6d..8c12345a60e 100644
--- a/contrib/python/responses/py3/responses/__init__.py
+++ b/contrib/python/responses/py3/responses/__init__.py
@@ -532,6 +532,7 @@ def _form_response(
body: Union[BufferedReader, BytesIO],
headers: Optional[Mapping[str, str]],
status: int,
+ request_method: Optional[str],
) -> HTTPResponse:
"""
Function to generate `urllib3.response.HTTPResponse` object.
@@ -566,6 +567,7 @@ def _form_response(
headers=headers,
original_response=orig_response, # type: ignore[arg-type] # See comment above
preload_content=False,
+ request_method=request_method,
)
@@ -632,7 +634,7 @@ class Response(BaseResponse):
content_length = len(body.getvalue())
headers["Content-Length"] = str(content_length)
- return _form_response(body, headers, status)
+ return _form_response(body, headers, status, request.method)
def __repr__(self) -> str:
return (
@@ -695,7 +697,7 @@ class CallbackResponse(BaseResponse):
body = _handle_body(body)
headers.extend(r_headers)
- return _form_response(body, headers, status)
+ return _form_response(body, headers, status, request.method)
class PassthroughResponse(BaseResponse):
@@ -1104,7 +1106,7 @@ class RequestsMock:
response = self._real_send(adapter, request, **kwargs) # type: ignore
else:
try:
- response = adapter.build_response( # type: ignore[no-untyped-call]
+ response = adapter.build_response( # type: ignore[assignment]
request, match.get_response(request)
)
except BaseException as response:
diff --git a/contrib/python/responses/py3/responses/matchers.py b/contrib/python/responses/py3/responses/matchers.py
index 20af1be693b..40f3a83dde1 100644
--- a/contrib/python/responses/py3/responses/matchers.py
+++ b/contrib/python/responses/py3/responses/matchers.py
@@ -18,45 +18,6 @@ from requests import PreparedRequest
from urllib3.util.url import parse_url
-def _create_key_val_str(input_dict: Union[Mapping[Any, Any], Any]) -> str:
- """
- Returns string of format {'key': val, 'key2': val2}
- Function is called recursively for nested dictionaries
-
- :param input_dict: dictionary to transform
- :return: (str) reformatted string
- """
-
- def list_to_str(input_list: List[str]) -> str:
- """
- Convert all list items to string.
- Function is called recursively for nested lists
- """
- converted_list = []
- for item in sorted(input_list, key=lambda x: str(x)):
- if isinstance(item, dict):
- item = _create_key_val_str(item)
- elif isinstance(item, list):
- item = list_to_str(item)
-
- converted_list.append(str(item))
- list_str = ", ".join(converted_list)
- return "[" + list_str + "]"
-
- items_list = []
- for key in sorted(input_dict.keys(), key=lambda x: str(x)):
- val = input_dict[key]
- if isinstance(val, dict):
- val = _create_key_val_str(val)
- elif isinstance(val, list):
- val = list_to_str(input_list=val)
-
- items_list.append(f"{key}: {val}")
-
- key_val_str = "{{{}}}".format(", ".join(items_list))
- return key_val_str
-
-
def _filter_dict_recursively(
dict1: Mapping[Any, Any], dict2: Mapping[Any, Any]
) -> Mapping[Any, Any]:
@@ -70,6 +31,21 @@ def _filter_dict_recursively(
return filtered_dict
+def body_matcher(params: str, *, allow_blank: bool = False) -> Callable[..., Any]:
+ def match(request: PreparedRequest) -> Tuple[bool, str]:
+ reason = ""
+ if isinstance(request.body, bytes):
+ request_body = request.body.decode("utf-8")
+ else:
+ request_body = str(request.body)
+ valid = True if request_body == params else False
+ if not valid:
+ reason = f"request.body doesn't match {params} doesn't match {request_body}"
+ return valid, reason
+
+ return match
+
+
def urlencoded_params_matcher(
params: Optional[Mapping[str, str]], *, allow_blank: bool = False
) -> Callable[..., Any]:
@@ -91,8 +67,8 @@ def urlencoded_params_matcher(
params_dict = params or {}
valid = params is None if request_body is None else params_dict == qsl_body
if not valid:
- reason = "request.body doesn't match: {} doesn't match {}".format(
- _create_key_val_str(qsl_body), _create_key_val_str(params_dict)
+ reason = (
+ f"request.body doesn't match: {qsl_body} doesn't match {params_dict}"
)
return valid, reason
@@ -146,13 +122,7 @@ def json_params_matcher(
valid = params is None if request_body is None else json_params == json_body
if not valid:
- if isinstance(json_body, dict) and isinstance(json_params, dict):
- reason = "request.body doesn't match: {} doesn't match {}".format(
- _create_key_val_str(json_body), _create_key_val_str(json_params)
- )
- else:
- reason = f"request.body doesn't match: {json_body} doesn't match {json_params}"
-
+ reason = f"request.body doesn't match: {json_body} doesn't match {json_params}"
if not strict_match:
reason += (
"\nNote: You use non-strict parameters check, "
@@ -234,10 +204,7 @@ def query_param_matcher(
valid = sorted(params_dict.items()) == sorted(request_params_dict.items())
if not valid:
- reason = "Parameters do not match. {} doesn't match {}".format(
- _create_key_val_str(request_params_dict),
- _create_key_val_str(params_dict),
- )
+ reason = f"Parameters do not match. {request_params_dict} doesn't match {params_dict}"
if not strict_match:
reason += (
"\nYou can use `strict_match=True` to do a strict parameters check."
@@ -267,9 +234,9 @@ def query_string_matcher(query: Optional[str]) -> Callable[..., Any]:
valid = not query if request_query is None else request_qsl == matcher_qsl
if not valid:
- reason = "Query string doesn't match. {} doesn't match {}".format(
- _create_key_val_str(dict(request_qsl)),
- _create_key_val_str(dict(matcher_qsl)),
+ reason = (
+ "Query string doesn't match. "
+ f"{dict(request_qsl)} doesn't match {dict(matcher_qsl)}"
)
return valid, reason
@@ -299,8 +266,8 @@ def request_kwargs_matcher(kwargs: Optional[Mapping[str, Any]]) -> Callable[...,
)
if not valid:
- reason = "Arguments don't match: {} doesn't match {}".format(
- _create_key_val_str(request_kwargs), _create_key_val_str(kwargs_dict)
+ reason = (
+ f"Arguments don't match: {request_kwargs} doesn't match {kwargs_dict}"
)
return valid, reason
@@ -436,8 +403,9 @@ def header_matcher(
valid = _compare_with_regex(request_headers)
if not valid:
- return False, "Headers do not match: {} doesn't match {}".format(
- _create_key_val_str(request_headers), _create_key_val_str(headers)
+ return (
+ False,
+ f"Headers do not match: {request_headers} doesn't match {headers}",
)
return valid, ""
diff --git a/contrib/python/responses/py3/ya.make b/contrib/python/responses/py3/ya.make
index ffa133acdfd..04114979d90 100644
--- a/contrib/python/responses/py3/ya.make
+++ b/contrib/python/responses/py3/ya.make
@@ -2,7 +2,7 @@
PY3_LIBRARY()
-VERSION(0.25.0)
+VERSION(0.25.2)
LICENSE(Apache-2.0)