A static HTTP mock interface for requests
Project description
requests-staticmock
A static HTTP mock interface for requests
Free software: Apache 2 License
Documentation: https://requests-staticmock.readthedocs.org.
Usage
As a context manager for requests Session instances
The requests_staticmock
import requests
import requests_staticmock
session = requests.Session()
with requests_staticmock.mock_session_with_fixtures(session, 'tests/fixtures', 'http://test_context.com'):
# will return a response object with the contents of tests/fixtures/test.json
response = new_session.request('get', 'http://test_context.com/test.json')
As an adapter
You can inject the requests_staticmock adapter into an existing (or new) requests session to mock out a particular URL or domain, e.g.
import requests
from requests_staticmock import Adapter
session = requests.Session()
special_adapter = Adapter('fixtures')
session.mount('http://specialwebsite.com', special_adapter)
session.request('http://normal.com/api/example') # works as normal
session.request('http://specialwebsite.com') # returns static mocks
Class adapter
Instead of using a static asset adapter, you can use an adapter that expects an internal method to respond with a string, e.g.
GET /test/example.xml will call method _test_example_xml(self, request) GET /test/example.xml?query=param will call method _test_example_xml(self, request)
This can be used via requests_staticmock.ClassAdapter or the context manager
import requests
import requests_staticmock
class MyTestClass(requests_staticmock.BaseMockClass):
def _api_v1_idea(self, request):
return "woop woop"
session = requests.Session()
with requests_staticmock.mock_session_with_class(session, MyTestClass, 'http://test_context.com'):
# will return a response object with the contents 'woop woop'
response = new_session.request('get', 'http://test_context.com/test.json')
Features
Allow mocking of HTTP responses via a directory of static fixtures
Support for sub-directories matching URL paths
Credits
This project takes inspiration and ideas from the requests_mock package, maintained by the OpenStack foundation.
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
History
0.3.0 (2017-01-29)
Added a class adapter
0.2.0 (2017-01-28)
Added a context manager for the static mocks
0.1.0 (2017-01-01)
First release on PyPI.
Project details
Release history Release notifications | RSS feed
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
Hashes for requests-staticmock-0.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79402820b895481bf1335cea77ede9e014cddfc322fee9567b8d4b873060f712 |
|
MD5 | 4af2ee5f04abd9bf38aa549812c98d97 |
|
BLAKE2b-256 | b5010715ed4865a255163e0a04d53ac37d36cc2c2c89444b31c5933fb76e88f8 |
Hashes for requests_staticmock-0.4.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3b10abb8a74fda755f0d5d3fec9eee07e8fa743dbcf6e6ff42f24c9e747a1d9 |
|
MD5 | 1bba3ff70149ab1571071e48e497228d |
|
BLAKE2b-256 | 7446dbdbc543635444f08b5a977740778831e1a6debcb9b99ae2867727e67000 |