Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method_or_response= as optional keyword parameter for add() method. #737

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ Response Parameters
The following attributes can be passed to a Response mock:

method (``str``)
The HTTP method (GET, POST, etc).
The HTTP method (GET, POST, etc). You may also use `method_or_response=`
as a keyword parameter in place of `method`, in which case all attributes
must be passed in as keyword parameters. This is to match the parameter
name on the `remove()`, `replace()`, and `upsert ()` methods.

url (``str`` or ``compiled regular expression``)
The full resource URL.
Expand Down
24 changes: 20 additions & 4 deletions responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ def add(
url: "Optional[_URLPatternType]" = None,
body: "_Body" = "",
adding_headers: "_HeaderSet" = None,
method_or_response: "_HTTPMethodOrResponse" = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't adding a method parameter to upsert() get the parameter name alignment you're looking for? We would also need to possibly update replace() and remove(). I think adding a shorter and easier parameter name to those methods is better than expanding add(). The .add() method is by far the most frequently used method of the set and its conventions will be the ones that folks remember.

*args: Any,
**kwargs: Any,
) -> BaseResponse:
Expand Down Expand Up @@ -808,9 +809,24 @@ def add(
>>> headers={'X-Header': 'foo'},
>>> )

Use the keyword argument method_or_response in place of method:

>>> responses.add(
>>> method_or_response='GET',
>>> url='http://example.com',
>>> )

"""
if isinstance(method, BaseResponse):
return self._registry.add(method)
# must have only one of method or method_or_response
assert (method is not None and method_or_response is None) or (
method is None and method_or_response is not None
), "Only one of `method` or `method_or_response` should be used."

# for backwards compatibility, method takes priority over method_or_response
actual_method = method if method is not None else method_or_response

if isinstance(actual_method, BaseResponse):
return self._registry.add(actual_method)

if adding_headers is not None:
kwargs.setdefault("headers", adding_headers)
Expand All @@ -827,8 +843,8 @@ def add(
)

assert url is not None
assert isinstance(method, str)
response = Response(method=method, url=url, body=body, **kwargs)
assert isinstance(actual_method, str)
response = Response(method=actual_method, url=url, body=body, **kwargs)
return self._registry.add(response)

delete = partialmethod(add, DELETE)
Expand Down
35 changes: 35 additions & 0 deletions responses/tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,41 @@ def run():
assert_reset()


def test_response_using_method_or_response():
@responses.activate
def run():
responses.add(
responses.GET, "http://example.net/rest/path/varname", body="return value"
)
resp = requests.get("http://example.net/rest/path/varname")
assert_response(resp, "return value")

responses.add(
method_or_response=responses.GET,
url="http://example.net/rest/path/varname",
body="return value",
)
resp = requests.get("http://example.net/rest/path/varname")
assert_response(resp, "return value")

with pytest.raises(AssertionError) as exc:
responses.add(
method=responses.GET,
method_or_response=responses.GET,
url="http://example.net/rest/path/varname",
body="return value",
)
resp = requests.get("http://example.net/rest/path/varname")
assert_response(resp, "return value")
assert (
exc.value.args[0]
== "Only one of `method` or `method_or_response` should be used."
)

run()
assert_reset()


def test_response_encoded():
@responses.activate
def run():
Expand Down