From 10bc25f569b060e75ea6748d6a9dedd3761f75a0 Mon Sep 17 00:00:00 2001 From: Maksim Beliaev Date: Wed, 12 Jan 2022 13:08:24 +0100 Subject: [PATCH] added fix for wrong boolean value in add_callback added tests to catch the behaviour described in #464 --- responses/__init__.py | 28 +++++++++++++++++--------- responses/test_responses.py | 39 ++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/responses/__init__.py b/responses/__init__.py index e364ad87..550b442c 100644 --- a/responses/__init__.py +++ b/responses/__init__.py @@ -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'", @@ -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) @@ -659,7 +669,7 @@ def add_callback( method, url, callback, - match_querystring=False, + match_querystring=FalseBool(), content_type="text/plain", match=(), ): diff --git a/responses/test_responses.py b/responses/test_responses.py index 05c7eeb3..c658961f 100644 --- a/responses/test_responses.py +++ b/responses/test_responses.py @@ -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/"