flymock
Easy to mock external HTTP request in Tornado.
Installation
$ pip install --upgrade flymock
Create mock data
Make a directory in your tests package:
$ mkdir __mock__
Use the hostname as the config filename, assume the url is http://example.com/demo, the config filename should be example.com.yaml, the config see below:
- path: /demo # path of the request to match
method: GET # method of the request to match
headers: # Response headers
Content-Type: application/json
body: Hello world # Response body
code: 200 # Response status code
- path: /file
body_type: file # Use a file content as the response
body: demo.json # Filename(same path of the config file)
code: 202
- path: /json
body: # If body is an object, that will response JSON content.
code: 2
Usage
import os
from tornado import httpclient
from tornado import testing
from flymock import FlyPatcher
class DemoTestCase(testing.AsyncTestCase):
def setUp(self):
super(DemoTestCase, self).setUp()
path = os.path.join(os.path.dirname(__file__), "__mock__")
self.patcher = FlyPatcher(path)
self.http_client = httpclient.AsyncHTTPClient()
self.patcher.start()
def tearDown(self):
super(DemoTestCase, self).tearDown()
self.patcher.stop()
@testing.gen_test
def test_mocked(self):
resp = yield self.http_client.fetch("http://example.com/demo")
self.assertEqual(resp.code, 200)
Adjust response dynamic:
patcher = FlyPatcher("/path/to/__mock__")
def hook(response):
response.patch_json({"a": 1})
with patcher.dynamic_hook(hook):
# code goes here
pass
# shortcut to adjust JSON
with patcher.patch_json({"a": 1}):
# code goes here
pass
Metadata
Release files for flymock 0.0.6
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 |
|---|---|---|---|---|
| flymock-0.0.6.tar.gz | 11.3 kB | Without provenance | Details for flymock-0.0.6.tar.gz |
Release files / flymock-0.0.6.tar.gz
| Download URL | flymock-0.0.6.tar.gz |
|---|---|
| Size | 11.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fbe8f9ea6b1ae4af1e5fb887203bed3fe4047e5b346795ecb460a27123a9cdc8
|
|
BLAKE2b-256 checksum How to use checksums |
bc6a40c2e56ef8a3c2a771503cc0986d30d81fe470a498ed63b2827575b42546
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3
|
Security for flymock 0.0.6
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.