Skip to content

Commit

Permalink
added fix for wrong boolean value in add_callback
Browse files Browse the repository at this point in the history
added tests to catch the behaviour described in getsentry#464
  • Loading branch information
beliaev-maksim committed Jan 12, 2022
1 parent 2ac730d commit 10bc25f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
28 changes: 19 additions & 9 deletions responses/__init__.py
Expand Up @@ -62,6 +62,15 @@
logger = logging.getLogger("responses")


class FalseBool:
# used for backwards compatibility, see
# https://github.com/getsentry/responses/issues/464
def __bool__(self):
return False

__nonzero__ = __bool__


def urlencoded_params_matcher(params):
warn(
"Function is deprecated. Use 'from responses.matchers import urlencoded_params_matcher'",
Expand Down Expand Up @@ -277,14 +286,15 @@ def _should_match_querystring(self, match_querystring_argument):
return False

if match_querystring_argument is not None:
warn(
(
"Argument 'match_querystring' is deprecated. "
"Use 'responses.matchers.query_param_matcher' or "
"'responses.matchers.query_string_matcher'"
),
DeprecationWarning,
)
if not isinstance(match_querystring_argument, FalseBool):
warn(
(
"Argument 'match_querystring' is deprecated. "
"Use 'responses.matchers.query_param_matcher' or "
"'responses.matchers.query_string_matcher'"
),
DeprecationWarning,
)
return match_querystring_argument

return bool(urlparse(self.url).query)
Expand Down Expand Up @@ -659,7 +669,7 @@ def add_callback(
method,
url,
callback,
match_querystring=False,
match_querystring=FalseBool(),
content_type="text/plain",
match=(),
):
Expand Down
39 changes: 38 additions & 1 deletion responses/test_responses.py
Expand Up @@ -559,11 +559,48 @@ def run():
assert_reset()


def test_callback_deprecated_argument():
def test_callback_deprecated_stream_argument():
with pytest.deprecated_call():
CallbackResponse(responses.GET, "url", lambda x: x, stream=False)


def test_callback_deprecated_match_querystring_argument():
with pytest.deprecated_call():
CallbackResponse(responses.GET, "url", lambda x: x, match_querystring=False)


def test_callback_match_querystring_default_false():
"""
Test to ensure that by default 'match_querystring' in 'add_callback' is set to False
and does not raise deprecation
see: https://github.com/getsentry/responses/issues/464 and related PR
"""
body = b"test callback"
status = 200
params = {"hello": "world", "I am": "a big test"}
headers = {"foo": "bar"}
url = "http://example.com/"

def request_callback(_request):
return status, headers, body

@responses.activate
def run():
responses.add_callback(responses.GET, url, request_callback, content_type=None)
resp = requests.get(url, params=params)
assert resp.text == "test callback"
assert resp.status_code == status
assert "foo" in resp.headers

from _pytest.outcomes import Failed

with pytest.raises(Failed):
with pytest.deprecated_call():
run()

assert_reset()


def test_callback_exception_result():
result = Exception()
url = "http://example.com/"
Expand Down

0 comments on commit 10bc25f

Please sign in to comment.