|
5 | 5 | from io import BufferedReader
|
6 | 6 | from io import BytesIO
|
7 | 7 | from typing import Any
|
| 8 | +from typing import List |
8 | 9 | from typing import Optional
|
9 | 10 | from unittest.mock import Mock
|
10 | 11 | from unittest.mock import patch
|
|
20 | 21 |
|
21 | 22 | import responses
|
22 | 23 | from responses import BaseResponse
|
| 24 | +from responses import Call |
23 | 25 | from responses import CallbackResponse
|
24 | 26 | from responses import PassthroughResponse
|
25 | 27 | from responses import Response
|
@@ -2035,6 +2037,37 @@ def run():
|
2035 | 2037 | assert_reset()
|
2036 | 2038 |
|
2037 | 2039 |
|
| 2040 | +def test_response_calls_indexing_and_slicing(): |
| 2041 | + @responses.activate |
| 2042 | + def run(): |
| 2043 | + responses.add(responses.GET, "http://www.example.com") |
| 2044 | + responses.add(responses.GET, "http://www.example.com/1") |
| 2045 | + responses.add(responses.GET, "http://www.example.com/2") |
| 2046 | + |
| 2047 | + requests.get("http://www.example.com") |
| 2048 | + requests.get("http://www.example.com/1") |
| 2049 | + requests.get("http://www.example.com/2") |
| 2050 | + requests.get("http://www.example.com/1") |
| 2051 | + requests.get("http://www.example.com") |
| 2052 | + |
| 2053 | + # Use of a type hints here ensures mypy knows the difference between index and slice. |
| 2054 | + individual_call: Call = responses.calls[0] |
| 2055 | + call_slice: List[Call] = responses.calls[1:-1] |
| 2056 | + |
| 2057 | + assert individual_call.request.url == "http://www.example.com" |
| 2058 | + |
| 2059 | + assert call_slice == [ |
| 2060 | + responses.calls[1], |
| 2061 | + responses.calls[2], |
| 2062 | + responses.calls[3], |
| 2063 | + ] |
| 2064 | + assert [c.request.url for c in call_slice] == [ |
| 2065 | + "http://www.example.com/1", |
| 2066 | + "http://www.example.com/2", |
| 2067 | + "http://www.example.com/1", |
| 2068 | + ] |
| 2069 | + |
| 2070 | + |
2038 | 2071 | def test_response_calls_and_registry_calls_are_equal():
|
2039 | 2072 | @responses.activate
|
2040 | 2073 | def run():
|
|
0 commit comments