diff --git a/CHANGES b/CHANGES index 005ddeaa..3aa996be 100644 --- a/CHANGES +++ b/CHANGES @@ -13,6 +13,7 @@ * 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. +* `delete`, `get`, `head`, `options`, `patch`, `post`, `put` shortcuts are now implemented using `functools.partialmethod`. * Fix `MaxRetryError` exception. Replace exception by `RetryError` according to `requests` implementation. See #572. * Adjust error message when `Retry` is exhausted. See #580. diff --git a/responses/__init__.py b/responses/__init__.py index a83b3e27..2a2c083b 100644 --- a/responses/__init__.py +++ b/responses/__init__.py @@ -2,6 +2,7 @@ import json as json_module import logging from collections import namedtuple +from functools import partialmethod from functools import wraps from http import client from itertools import groupby @@ -760,6 +761,14 @@ def add( response = Response(method=method, url=url, body=body, **kwargs) return self._registry.add(response) + delete = partialmethod(add, DELETE) + get = partialmethod(add, GET) + head = partialmethod(add, HEAD) + options = partialmethod(add, OPTIONS) + patch = partialmethod(add, PATCH) + post = partialmethod(add, POST) + put = partialmethod(add, PUT) + def _add_from_file(self, file_path: "Union[str, bytes, os.PathLike[Any]]") -> None: with open(file_path) as file: data = _toml.load(file) @@ -775,27 +784,6 @@ def _add_from_file(self, file_path: "Union[str, bytes, os.PathLike[Any]]") -> No auto_calculate_content_length=rsp["auto_calculate_content_length"], ) - def delete(self, *args: Any, **kwargs: Any) -> BaseResponse: - return self.add(DELETE, *args, **kwargs) - - def get(self, *args: Any, **kwargs: Any) -> BaseResponse: - return self.add(GET, *args, **kwargs) - - def head(self, *args: Any, **kwargs: Any) -> BaseResponse: - return self.add(HEAD, *args, **kwargs) - - def options(self, *args: Any, **kwargs: Any) -> BaseResponse: - return self.add(OPTIONS, *args, **kwargs) - - def patch(self, *args: Any, **kwargs: Any) -> BaseResponse: - return self.add(PATCH, *args, **kwargs) - - def post(self, *args: Any, **kwargs: Any) -> BaseResponse: - return self.add(POST, *args, **kwargs) - - def put(self, *args: Any, **kwargs: Any) -> BaseResponse: - return self.add(PUT, *args, **kwargs) - def add_passthru(self, prefix: _URLPatternType) -> None: """ Register a URL prefix or regex to passthru any non-matching mock requests to.