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

Update changelog for #746 #747

Merged
merged 7 commits into from
Jan 10, 2025
Merged
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
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Responses can now match requests that use `data` with file-like objects.
Files will be read as bytes and stored in the request mock. See #736
* `RequestsMock.matchers` was added. This property is an alias to `responses.matchers`. See #739
* Removed tests from packaged wheels. See #746
* Improved recorder API to ease use in REPL environments. See #745

0.25.3
------
Expand Down
221 changes: 110 additions & 111 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,117 +61,6 @@ Please ensure to update your code according to the guidance.
- 0.20.0
- Use ``responses.mock.assert_all_requests_are_fired``,
``responses.mock.passthru_prefixes``, ``responses.mock.target`` instead.

BETA Features
-------------
Below you can find a list of BETA features. Although we will try to keep the API backwards compatible
with released version, we reserve the right to change these APIs before they are considered stable. Please share your feedback via
`GitHub Issues <https://github.com/getsentry/responses/issues>`_.

Record Responses to files
^^^^^^^^^^^^^^^^^^^^^^^^^

You can perform real requests to the server and ``responses`` will automatically record the output to the
file. Recorded data is stored in `YAML <https://yaml.org>`_ format.

Apply ``@responses._recorder.record(file_path="out.yaml")`` decorator to any function where you perform
requests to record responses to ``out.yaml`` file.

Following code

.. code-block:: python

import requests
from responses import _recorder


def another():
rsp = requests.get("https://httpstat.us/500")
rsp = requests.get("https://httpstat.us/202")


@_recorder.record(file_path="out.yaml")
def test_recorder():
rsp = requests.get("https://httpstat.us/404")
rsp = requests.get("https://httpbin.org/status/wrong")
another()

will produce next output:

.. code-block:: yaml

responses:
- response:
auto_calculate_content_length: false
body: 404 Not Found
content_type: text/plain
method: GET
status: 404
url: https://httpstat.us/404
- response:
auto_calculate_content_length: false
body: Invalid status code
content_type: text/plain
method: GET
status: 400
url: https://httpbin.org/status/wrong
- response:
auto_calculate_content_length: false
body: 500 Internal Server Error
content_type: text/plain
method: GET
status: 500
url: https://httpstat.us/500
- response:
auto_calculate_content_length: false
body: 202 Accepted
content_type: text/plain
method: GET
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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You can populate your active registry from a ``yaml`` file with recorded responses.
(See `Record Responses to files`_ to understand how to obtain a file).
To do that you need to execute ``responses._add_from_file(file_path="out.yaml")`` within
an activated decorator or a context manager.

The following code example registers a ``patch`` response, then all responses present in
``out.yaml`` file and a ``post`` response at the end.

.. code-block:: python

import responses


@responses.activate
def run():
responses.patch("http://httpbin.org")
responses._add_from_file(file_path="out.yaml")
responses.post("http://httpbin.org/form")


run()

Basics
------

Expand Down Expand Up @@ -1418,6 +1307,116 @@ single thread to access it.

await run()

BETA Features
-------------
Below you can find a list of BETA features. Although we will try to keep the API backwards compatible
with released version, we reserve the right to change these APIs before they are considered stable. Please share your feedback via
`GitHub Issues <https://github.com/getsentry/responses/issues>`_.

Record Responses to files
^^^^^^^^^^^^^^^^^^^^^^^^^

You can perform real requests to the server and ``responses`` will automatically record the output to the
file. Recorded data is stored in `YAML <https://yaml.org>`_ format.

Apply ``@responses._recorder.record(file_path="out.yaml")`` decorator to any function where you perform
requests to record responses to ``out.yaml`` file.

Following code

.. code-block:: python

import requests
from responses import _recorder


def another():
rsp = requests.get("https://httpstat.us/500")
rsp = requests.get("https://httpstat.us/202")


@_recorder.record(file_path="out.yaml")
def test_recorder():
rsp = requests.get("https://httpstat.us/404")
rsp = requests.get("https://httpbin.org/status/wrong")
another()

will produce next output:

.. code-block:: yaml

responses:
- response:
auto_calculate_content_length: false
body: 404 Not Found
content_type: text/plain
method: GET
status: 404
url: https://httpstat.us/404
- response:
auto_calculate_content_length: false
body: Invalid status code
content_type: text/plain
method: GET
status: 400
url: https://httpbin.org/status/wrong
- response:
auto_calculate_content_length: false
body: 500 Internal Server Error
content_type: text/plain
method: GET
status: 500
url: https://httpstat.us/500
- response:
auto_calculate_content_length: false
body: 202 Accepted
content_type: text/plain
method: GET
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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You can populate your active registry from a ``yaml`` file with recorded responses.
(See `Record Responses to files`_ to understand how to obtain a file).
To do that you need to execute ``responses._add_from_file(file_path="out.yaml")`` within
an activated decorator or a context manager.

The following code example registers a ``patch`` response, then all responses present in
``out.yaml`` file and a ``post`` response at the end.

.. code-block:: python

import responses


@responses.activate
def run():
responses.patch("http://httpbin.org")
responses._add_from_file(file_path="out.yaml")
responses.post("http://httpbin.org/form")


run()


Contributing
------------
Expand Down
2 changes: 1 addition & 1 deletion responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def __getitem__(self, idx: int) -> Call:
...

@overload
def __getitem__(self, idx: slice) -> List[Call]:
def __getitem__(self, idx: "slice[int, int, Optional[int]]") -> List[Call]:
...

def __getitem__(self, idx: Union[int, slice]) -> Union[Call, List[Call]]:
Expand Down
2 changes: 1 addition & 1 deletion responses/tests/test_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def dump_to_file(file_path, registered):
with open(file_path, "wb") as file:
_dump(registered, file, tomli_w.dump) # type: ignore[arg-type]

custom_recorder.dump_to_file = dump_to_file # type: ignore[method-assign]
custom_recorder.dump_to_file = dump_to_file # type: ignore[assignment]

url202, url400, url404, url500 = self.prepare_server(httpserver)

Expand Down
Loading