Skip to content

Commit aee357e

Browse files
authored
Update changelog and fix mypy (#747)
* Update changelog for #746 * Resolve mypy error. * Move recorder docs * Update changelog for #745 * mypy * mypy + old python
1 parent 32c4884 commit aee357e

File tree

4 files changed

+114
-113
lines changed

4 files changed

+114
-113
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Responses can now match requests that use `data` with file-like objects.
55
Files will be read as bytes and stored in the request mock. See #736
66
* `RequestsMock.matchers` was added. This property is an alias to `responses.matchers`. See #739
7+
* Removed tests from packaged wheels. See #746
8+
* Improved recorder API to ease use in REPL environments. See #745
79

810
0.25.3
911
------

README.rst

Lines changed: 110 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -61,117 +61,6 @@ Please ensure to update your code according to the guidance.
6161
- 0.20.0
6262
- Use ``responses.mock.assert_all_requests_are_fired``,
6363
``responses.mock.passthru_prefixes``, ``responses.mock.target`` instead.
64-
65-
BETA Features
66-
-------------
67-
Below you can find a list of BETA features. Although we will try to keep the API backwards compatible
68-
with released version, we reserve the right to change these APIs before they are considered stable. Please share your feedback via
69-
`GitHub Issues <https://github.com/getsentry/responses/issues>`_.
70-
71-
Record Responses to files
72-
^^^^^^^^^^^^^^^^^^^^^^^^^
73-
74-
You can perform real requests to the server and ``responses`` will automatically record the output to the
75-
file. Recorded data is stored in `YAML <https://yaml.org>`_ format.
76-
77-
Apply ``@responses._recorder.record(file_path="out.yaml")`` decorator to any function where you perform
78-
requests to record responses to ``out.yaml`` file.
79-
80-
Following code
81-
82-
.. code-block:: python
83-
84-
import requests
85-
from responses import _recorder
86-
87-
88-
def another():
89-
rsp = requests.get("https://httpstat.us/500")
90-
rsp = requests.get("https://httpstat.us/202")
91-
92-
93-
@_recorder.record(file_path="out.yaml")
94-
def test_recorder():
95-
rsp = requests.get("https://httpstat.us/404")
96-
rsp = requests.get("https://httpbin.org/status/wrong")
97-
another()
98-
99-
will produce next output:
100-
101-
.. code-block:: yaml
102-
103-
responses:
104-
- response:
105-
auto_calculate_content_length: false
106-
body: 404 Not Found
107-
content_type: text/plain
108-
method: GET
109-
status: 404
110-
url: https://httpstat.us/404
111-
- response:
112-
auto_calculate_content_length: false
113-
body: Invalid status code
114-
content_type: text/plain
115-
method: GET
116-
status: 400
117-
url: https://httpbin.org/status/wrong
118-
- response:
119-
auto_calculate_content_length: false
120-
body: 500 Internal Server Error
121-
content_type: text/plain
122-
method: GET
123-
status: 500
124-
url: https://httpstat.us/500
125-
- response:
126-
auto_calculate_content_length: false
127-
body: 202 Accepted
128-
content_type: text/plain
129-
method: GET
130-
status: 202
131-
url: https://httpstat.us/202
132-
133-
If you are in the REPL, you can also activete the recorder for all following responses:
134-
135-
.. code-block:: python
136-
137-
import requests
138-
from responses import _recorder
139-
140-
_recorder.recorder.start()
141-
142-
requests.get("https://httpstat.us/500")
143-
144-
_recorder.recorder.dump_to_file("out.yaml")
145-
146-
# you can stop or reset the recorder
147-
_recorder.recorder.stop()
148-
_recorder.recorder.reset()
149-
150-
Replay responses (populate registry) from files
151-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
152-
153-
You can populate your active registry from a ``yaml`` file with recorded responses.
154-
(See `Record Responses to files`_ to understand how to obtain a file).
155-
To do that you need to execute ``responses._add_from_file(file_path="out.yaml")`` within
156-
an activated decorator or a context manager.
157-
158-
The following code example registers a ``patch`` response, then all responses present in
159-
``out.yaml`` file and a ``post`` response at the end.
160-
161-
.. code-block:: python
162-
163-
import responses
164-
165-
166-
@responses.activate
167-
def run():
168-
responses.patch("http://httpbin.org")
169-
responses._add_from_file(file_path="out.yaml")
170-
responses.post("http://httpbin.org/form")
171-
172-
173-
run()
174-
17564
Basics
17665
------
17766

@@ -1418,6 +1307,116 @@ single thread to access it.
14181307
14191308
await run()
14201309
1310+
BETA Features
1311+
-------------
1312+
Below you can find a list of BETA features. Although we will try to keep the API backwards compatible
1313+
with released version, we reserve the right to change these APIs before they are considered stable. Please share your feedback via
1314+
`GitHub Issues <https://github.com/getsentry/responses/issues>`_.
1315+
1316+
Record Responses to files
1317+
^^^^^^^^^^^^^^^^^^^^^^^^^
1318+
1319+
You can perform real requests to the server and ``responses`` will automatically record the output to the
1320+
file. Recorded data is stored in `YAML <https://yaml.org>`_ format.
1321+
1322+
Apply ``@responses._recorder.record(file_path="out.yaml")`` decorator to any function where you perform
1323+
requests to record responses to ``out.yaml`` file.
1324+
1325+
Following code
1326+
1327+
.. code-block:: python
1328+
1329+
import requests
1330+
from responses import _recorder
1331+
1332+
1333+
def another():
1334+
rsp = requests.get("https://httpstat.us/500")
1335+
rsp = requests.get("https://httpstat.us/202")
1336+
1337+
1338+
@_recorder.record(file_path="out.yaml")
1339+
def test_recorder():
1340+
rsp = requests.get("https://httpstat.us/404")
1341+
rsp = requests.get("https://httpbin.org/status/wrong")
1342+
another()
1343+
1344+
will produce next output:
1345+
1346+
.. code-block:: yaml
1347+
1348+
responses:
1349+
- response:
1350+
auto_calculate_content_length: false
1351+
body: 404 Not Found
1352+
content_type: text/plain
1353+
method: GET
1354+
status: 404
1355+
url: https://httpstat.us/404
1356+
- response:
1357+
auto_calculate_content_length: false
1358+
body: Invalid status code
1359+
content_type: text/plain
1360+
method: GET
1361+
status: 400
1362+
url: https://httpbin.org/status/wrong
1363+
- response:
1364+
auto_calculate_content_length: false
1365+
body: 500 Internal Server Error
1366+
content_type: text/plain
1367+
method: GET
1368+
status: 500
1369+
url: https://httpstat.us/500
1370+
- response:
1371+
auto_calculate_content_length: false
1372+
body: 202 Accepted
1373+
content_type: text/plain
1374+
method: GET
1375+
status: 202
1376+
url: https://httpstat.us/202
1377+
1378+
If you are in the REPL, you can also activete the recorder for all following responses:
1379+
1380+
.. code-block:: python
1381+
1382+
import requests
1383+
from responses import _recorder
1384+
1385+
_recorder.recorder.start()
1386+
1387+
requests.get("https://httpstat.us/500")
1388+
1389+
_recorder.recorder.dump_to_file("out.yaml")
1390+
1391+
# you can stop or reset the recorder
1392+
_recorder.recorder.stop()
1393+
_recorder.recorder.reset()
1394+
1395+
Replay responses (populate registry) from files
1396+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1397+
1398+
You can populate your active registry from a ``yaml`` file with recorded responses.
1399+
(See `Record Responses to files`_ to understand how to obtain a file).
1400+
To do that you need to execute ``responses._add_from_file(file_path="out.yaml")`` within
1401+
an activated decorator or a context manager.
1402+
1403+
The following code example registers a ``patch`` response, then all responses present in
1404+
``out.yaml`` file and a ``post`` response at the end.
1405+
1406+
.. code-block:: python
1407+
1408+
import responses
1409+
1410+
1411+
@responses.activate
1412+
def run():
1413+
responses.patch("http://httpbin.org")
1414+
responses._add_from_file(file_path="out.yaml")
1415+
responses.post("http://httpbin.org/form")
1416+
1417+
1418+
run()
1419+
14211420
14221421
Contributing
14231422
------------

responses/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def __getitem__(self, idx: int) -> Call:
249249
...
250250

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

255255
def __getitem__(self, idx: Union[int, slice]) -> Union[Call, List[Call]]:

responses/tests/test_recorder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def dump_to_file(file_path, registered):
101101
with open(file_path, "wb") as file:
102102
_dump(registered, file, tomli_w.dump) # type: ignore[arg-type]
103103

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

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

0 commit comments

Comments
 (0)