Http Methods handling to look more like requests

Allow users to specify methods via the same HTTP method based format
that requests does. It makes them look more synchronous.
This commit is contained in:
Jamie Lennox 2014-07-10 14:23:38 +10:00
parent b51a166db9
commit 33474a296e
3 changed files with 104 additions and 0 deletions

View File

@ -47,6 +47,35 @@ If the position of the mock is likely to conflict with other arguments you can p
>>> test_kw_function()
'resp'
Methods
=======
The mocker object can be used with a similar interface to requests itself. Mocker objects can be called with simply the
.. doctest::
>>> with requests_mock.Mocker() as mock:
... mock.get('http://test.com', text='resp')
... requests.get('http://test.com').text
...
'resp'
The functions exist for the common HTTP method:
- :py:meth:`~requests_mock.MockerCore.delete`
- :py:meth:`~requests_mock.MockerCore.get`
- :py:meth:`~requests_mock.MockerCore.head`
- :py:meth:`~requests_mock.MockerCore.options`
- :py:meth:`~requests_mock.MockerCore.patch`
- :py:meth:`~requests_mock.MockerCore.post`
- :py:meth:`~requests_mock.MockerCore.put`
As well as the base:
- :py:meth:`~requests_mock.MockerCore.request`
Real HTTP Requests
==================

View File

@ -85,6 +85,30 @@ class MockerCore(object):
raise AttributeError(name)
def request(self, *args, **kwargs):
return self.register_uri(*args, **kwargs)
def get(self, *args, **kwargs):
return self.request('GET', *args, **kwargs)
def options(self, *args, **kwargs):
return self.request('OPTIONS', *args, **kwargs)
def head(self, *args, **kwargs):
return self.request('HEAD', *args, **kwargs)
def post(self, *args, **kwargs):
return self.request('POST', *args, **kwargs)
def put(self, *args, **kwargs):
return self.request('PUT', *args, **kwargs)
def patch(self, *args, **kwargs):
return self.request('PATCH', *args, **kwargs)
def delete(self, *args, **kwargs):
return self.request('DELETE', *args, **kwargs)
class Mocker(MockerCore):
"""The standard entry point for mock Adapter loading.

View File

@ -79,3 +79,54 @@ class MockerTests(base.TestCase):
self.assertMockStopped()
inner()
self.assertMockStopped()
class MockerHttpMethodsTests(base.TestCase):
URL = 'http://test.com/path'
TEXT = 'resp'
def assertResponse(self, resp):
self.assertEqual(self.TEXT, resp.text)
@requests_mock.Mocker()
def test_mocker_request(self, m):
method = 'XXX'
m.request(method, self.URL, text=self.TEXT)
resp = requests.request(method, self.URL)
self.assertResponse(resp)
@requests_mock.Mocker()
def test_mocker_get(self, m):
m.get(self.URL, text=self.TEXT)
self.assertResponse(requests.get(self.URL))
@requests_mock.Mocker()
def test_mocker_options(self, m):
m.options(self.URL, text=self.TEXT)
self.assertResponse(requests.options(self.URL))
@requests_mock.Mocker()
def test_mocker_head(self, m):
m.head(self.URL, text=self.TEXT)
self.assertResponse(requests.head(self.URL))
@requests_mock.Mocker()
def test_mocker_post(self, m):
m.post(self.URL, text=self.TEXT)
self.assertResponse(requests.post(self.URL))
@requests_mock.Mocker()
def test_mocker_put(self, m):
m.put(self.URL, text=self.TEXT)
self.assertResponse(requests.put(self.URL))
@requests_mock.Mocker()
def test_mocker_patch(self, m):
m.patch(self.URL, text=self.TEXT)
self.assertResponse(requests.patch(self.URL))
@requests_mock.Mocker()
def test_mocker_delete(self, m):
m.delete(self.URL, text=self.TEXT)
self.assertResponse(requests.delete(self.URL))