requests-mock/docs/matching.rst
Jamie Lennox 4c97632c53 Split docs for better RTFD viewing
Simplified README, not sure how wise that is.
2014-06-16 22:36:56 +10:00

1.5 KiB

Request Matching

Whilst it is preferable to provide the whole URI to :pyrequests_mock.Adapter.register_uri it is possible to just specify components.

Basic

You can specify a protocol-less path:

>>> adapter.register_uri('GET', '//test.com/5', text='resp')
>>> session.get('mock://test.com/5').text
'resp'

or you can specify just a path:

>>> adapter.register_uri('GET', '/6', text='resp')
>>> session.get('mock://test.com/6').text
'resp'
>>> session.get('mock://another.com/6').text
'resp'

Query Strings

Query strings provided to a register will match so long as at least those provided form part of the request.

>>> adapter.register_uri('GET', '/7?a=1', text='resp')
>>> session.get('mock://test.com/7?a=1&b=2').text
'resp'

>>> session.get('mock://test.com/7?a=3')
Traceback (most recent call last):
   ...
requests_mock.exceptions.NoMockAddress: No mock address: GET mock://test.com/7?a=3

This can be a problem in certain situations, so if you wish to match only the complete query string there is a flag `complete_qs`:

>>> adapter.register_uri('GET', '/8?a=1', complete_qs=True, text='resp')
>>> session.get('mock://test.com/8?a=1&b=2')
Traceback (most recent call last):
   ...
requests_mock.exceptions.NoMockAddress: No mock address: GET mock://test.com/8?a=1&b=2