A library for mocking requests in Tornado HTTP client
Project description
## tornado-httpclient-mock
A library for mocking requests in Tornado HTTP client
[![Build Status](https://travis-ci.org/SuminAndrew/tornado-httpclient-mock.svg?branch=master)](https://travis-ci.org/SuminAndrew/tornado-httpclient-mock)
[![codecov](https://codecov.io/gh/SuminAndrew/tornado-httpclient-mock/branch/master/graph/badge.svg)](https://codecov.io/gh/SuminAndrew/tornado-httpclient-mock)
This library can be used to patch `AsyncHTTPClient.fetch_impl` method to stub requests to external resources.
`tornado_mock.httpclient.patch_http_client` does this patching on `AsyncHTTPClient` instance.
After that you can use `tornado_mock.httpclient.set_stub` method to setup an appropriate responses to the requests.
```python
from tornado import gen
from tornado.httpclient import AsyncHTTPClient
from tornado.testing import AsyncHTTPTestCase
from tornado.web import Application, RequestHandler
from tornado_mock.httpclient import get_response_stub, patch_http_client, set_stub
class TestHandler(RequestHandler):
@gen.coroutine
def get(self, *args, **kwargs):
response1 = yield self.application.http_client.fetch('http://example.com/path?query')
self.write(response1.body)
response2 = yield self.application.http_client.fetch('http://test.com/path')
self.finish(response2.body)
class HTTPClientMockTest(AsyncHTTPTestCase):
def get_app(self):
app = Application([('/test', TestHandler)])
self.app_http_client = app.http_client = AsyncHTTPClient(force_instance=True)
return app
def test_mock(self):
patch_http_client(self.app_http_client)
def get_response(request):
return get_response_stub(request, buffer='STUB2')
set_stub(self.app_http_client, 'http://example.com/path', response_body='STUB1')
set_stub(self.app_http_client, 'http://test.com/path', response_function=get_response)
self.assertEqual(self.fetch('/test').body, b'STUB1STUB2')
```
For more information see docstrings in [httpclient.py](tornado_mock/httpclient.py).
A library for mocking requests in Tornado HTTP client
[![Build Status](https://travis-ci.org/SuminAndrew/tornado-httpclient-mock.svg?branch=master)](https://travis-ci.org/SuminAndrew/tornado-httpclient-mock)
[![codecov](https://codecov.io/gh/SuminAndrew/tornado-httpclient-mock/branch/master/graph/badge.svg)](https://codecov.io/gh/SuminAndrew/tornado-httpclient-mock)
This library can be used to patch `AsyncHTTPClient.fetch_impl` method to stub requests to external resources.
`tornado_mock.httpclient.patch_http_client` does this patching on `AsyncHTTPClient` instance.
After that you can use `tornado_mock.httpclient.set_stub` method to setup an appropriate responses to the requests.
```python
from tornado import gen
from tornado.httpclient import AsyncHTTPClient
from tornado.testing import AsyncHTTPTestCase
from tornado.web import Application, RequestHandler
from tornado_mock.httpclient import get_response_stub, patch_http_client, set_stub
class TestHandler(RequestHandler):
@gen.coroutine
def get(self, *args, **kwargs):
response1 = yield self.application.http_client.fetch('http://example.com/path?query')
self.write(response1.body)
response2 = yield self.application.http_client.fetch('http://test.com/path')
self.finish(response2.body)
class HTTPClientMockTest(AsyncHTTPTestCase):
def get_app(self):
app = Application([('/test', TestHandler)])
self.app_http_client = app.http_client = AsyncHTTPClient(force_instance=True)
return app
def test_mock(self):
patch_http_client(self.app_http_client)
def get_response(request):
return get_response_stub(request, buffer='STUB2')
set_stub(self.app_http_client, 'http://example.com/path', response_body='STUB1')
set_stub(self.app_http_client, 'http://test.com/path', response_function=get_response)
self.assertEqual(self.fetch('/test').body, b'STUB1STUB2')
```
For more information see docstrings in [httpclient.py](tornado_mock/httpclient.py).
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for tornado-httpclient-mock-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b64908390f3db832f7fd1d31bce363646da4f7a2fd41b0150bbe6fd5a01322dd |
|
MD5 | c126c07687d15163f0011d6a9fdddb58 |
|
BLAKE2b-256 | 8781abf6fc1f4cb06bf3b3b2f6ab3eb41233ab5c13e71a8f8ff986a6c13b689f |
Close
Hashes for tornado_httpclient_mock-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24dd0837ac4f640a46f67a4a4d295bd66e57d1965add31d33fbd5b98ed238681 |
|
MD5 | 8c2ab097fd2e2e32a72fd7cbb117ede5 |
|
BLAKE2b-256 | 365fbbf765fb361196fd9bf916f10ab0204cff5d404bda009f7868fc1481afbe |