From eb5c94f8b5638efa0e56c575695565533f1cd3fe Mon Sep 17 00:00:00 2001 From: Nicco Kunzmann Date: Thu, 26 Dec 2024 14:59:52 +0000 Subject: [PATCH] test and document dumping to file --- README.rst | 16 ++++++++++++++++ responses/_recorder.py | 8 ++++++-- responses/tests/test_recorder.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index da473bfc..001a45cc 100644 --- a/README.rst +++ b/README.rst @@ -130,6 +130,22 @@ will produce next output: status: 202 url: https://httpstat.us/202 +If you are in the REPL, you can also activete the recorder for all following responses: + +.. code-block:: python + + import requests + from responses import _recorder + + _recorder.recorder.start() + + requests.get("https://httpstat.us/500") + + _recorder.recorder.dump_to_file("out.yaml") + + # you can stop or reset the recorder + _recorder.recorder.stop() + _recorder.recorder.reset() Replay responses (populate registry) from files ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/responses/_recorder.py b/responses/_recorder.py index 64533e29..151e47aa 100644 --- a/responses/_recorder.py +++ b/responses/_recorder.py @@ -9,6 +9,7 @@ from typing import Callable from typing import Dict from typing import List + from typing import Optional from typing import Type from typing import Union from responses import FirstMatchRegistry @@ -122,10 +123,13 @@ def wrapper(*args: "Any", **kwargs: "Any") -> "Any": # type: ignore[misc] def dump_to_file( self, - *, file_path: "Union[str, bytes, os.PathLike[Any]]", - registered: "List[BaseResponse]", + *, + registered: "Optional[List[BaseResponse]]" = None, ) -> None: + """Dump the recorded responses to a file.""" + if registered is None: + registered = self.get_registry().registered with open(file_path, "w") as file: _dump(registered, file, yaml.dump) diff --git a/responses/tests/test_recorder.py b/responses/tests/test_recorder.py index 9adbca7a..abe03bb8 100644 --- a/responses/tests/test_recorder.py +++ b/responses/tests/test_recorder.py @@ -152,6 +152,35 @@ def prepare_server(self, httpserver): url400 = httpserver.url_for("/status/wrong") return url202, url400, url404, url500 + def test_use_recorder_without_decorator(self, httpserver): + """I want to be able to record in the REPL.""" + url202, url400, url404, url500 = self.prepare_server(httpserver) + + _recorder.recorder.start() + + def another(): + requests.get(url500) + requests.put(url202) + + def run(): + requests.get(url404) + requests.get(url400) + another() + + run() + + _recorder.recorder.stop() + _recorder.recorder.dump_to_file(self.out_file) + + with open(self.out_file) as file: + data = yaml.safe_load(file) + assert data == get_data(httpserver.host, httpserver.port) + + # Now, we test that the recorder is properly reset + assert _recorder.recorder.get_registry().registered + _recorder.recorder.reset() + assert not _recorder.recorder.get_registry().registered + class TestReplay: def setup_method(self):