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/api/v1/idea')
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.6.0 (2017-01-29)
Add support for the class adapter methods to return either a string or a response object
Moved to Py.Test
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.6.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a155d57bdd0ad60c8dfd95b58a015b8b4ccdc7ef42c59c3ee35a617e14e09cdb |
|
MD5 | cbcc4a5e24b333664ed638e56cc1eaa5 |
|
BLAKE2b-256 | 14f34dd3d3fd45ecaec22dc41d8f00d528ebaf6d097686ad077c762cbf34c6cc |
Hashes for requests_staticmock-0.6.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec7b75efdbd7af0ffb19062bd631e250218c8c953829a5a392e12fcbcc548829 |
|
MD5 | 8fdb39ffb825fb3e3cc31ab76ca22ac4 |
|
BLAKE2b-256 | 1e751e674f6a8f6abf93b584c34cc5f218d97d5ef913855c4d5285badb382ced |