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.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.5.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | fdba5377cd0c894d145bd900b067f2f2c4fdf956192c26e44944c7da9dcdb863 |
|
MD5 | c55a8d94e061ac3adfc3bbc4d5c0b1c5 |
|
BLAKE2b-256 | 51639967df7cc3e84e98fd5e3067b4725b790951ee208c035a009ed68827713a |
Hashes for requests_staticmock-0.5.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60c376e1f18203596660a3a0a28b62d3d8ea787076b55c7fe007c3b41d0a19d1 |
|
MD5 | 4a3abca74f09df2d9a763f1e163ac23a |
|
BLAKE2b-256 | 5e6018be84f4f89c6abfbe4033ca56a6ed76d92986d9a29836f885aa0290b68e |