Requests Mock Documentation, Release 1.11.0
(continued from previous page)
>>> adapter.register_uri('GET',
... 'mock://test.com/3',
... text=text_callback,
... headers={'Test2': 'value2'},
... status_code=400)
>>> resp = session.get('mock://test.com/3')
>>> resp.status_code, resp.headers, resp.text
(200, {'Test1': 'value1', 'Test2': 'value2'}, 'response')
4.3 Response Lists
Multiple responses can be provided to be returned in order by specifying the keyword parameters in a list. If the list is
exhausted then the last response will continue to be returned.
>>> adapter.register_uri('GET', 'mock://test.com/4', [{'text': 'resp1', 'status_code
˓→': 300},
... {'text': 'resp2', 'status_code
˓→': 200}])
>>> resp = session.get('mock://test.com/4')
>>> (resp.status_code, resp.text)
(300, 'resp1')
>>> resp = session.get('mock://test.com/4')
>>> (resp.status_code, resp.text)
(200, 'resp2')
>>> resp = session.get('mock://test.com/4')
>>> (resp.status_code, resp.text)
(200, 'resp2')
Callbacks work within response lists in exactly the same way they do normally;
>>> adapter.register_uri('GET', 'mock://test.com/5', [{'text': text_callback}]),
>>> resp = session.get('mock://test.com/5')
>>> resp.status_code, resp.headers, resp.text
(200, {'Test1': 'value1', 'Test2': 'value2'}, 'response')
4.4 Raising Exceptions
When trying to emulate a connection timeout or SSLError you need to be able to throw an exception when a mock is
hit. This can be achieved by passing the exc parameter instead of a body parameter.
>>> adapter.register_uri('GET', 'mock://test.com/6', exc=requests.exceptions.
˓→ConnectTimeout),
>>> session.get('mock://test.com/6')
Traceback (most recent call last):
...
ConnectTimeout:
4.3. Response Lists 19