A library for mocking requests in Tornado HTTP client
[](https://travis-ci.org/SuminAndrew/tornado-httpclient-mock)
[](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).
Metadata
Release files for tornado-httpclient-mock 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tornado-httpclient-mock-0.2.3.tar.gz | 4.5 kB | Without provenance | Details for tornado-httpclient-mock-0.2.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tornado_httpclient_mock-0.2.3-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for tornado_httpclient_mock-0.2.3-py2.py3-none-any.whl |
Total release size: 11.2 kB
Release files / tornado-httpclient-mock-0.2.3.tar.gz
| Download URL | tornado-httpclient-mock-0.2.3.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
46ceecdf46a60ed6dad3ae74453cb28892ac814fcf6e1852cc2d1aca67d079a4
|
|
BLAKE2b-256 checksum How to use checksums |
2b70884e2d3565874c8e075dd763258481379173f9bccf90967af6f14e7d66e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/2.7.12
|
Release files / tornado_httpclient_mock-0.2.3-py2.py3-none-any.whl
| Download URL | tornado_httpclient_mock-0.2.3-py2.py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
79ccebe5ddfbd62749b45657204de1adcbba0492561841778a3f658ffa467d14
|
|
BLAKE2b-256 checksum How to use checksums |
48beb95d6bb2f352eff644f498c33b6dd132e2ca7e3cc30d94cd2e749d3e89ee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/2.7.12
|
Security for tornado-httpclient-mock 0.2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.