Package: requests-staticmock
requests-staticmockA static HTTP mock interface for testing classes that leverage Pythonrequestswithnomonkey patching!Free software: Apache 2 LicenseDocumentation:https://requests-staticmock.readthedocs.org.UsageAs a context manager for requests Session instancesTherequests_staticmockimportrequestsimportrequests_staticmocksession=requests.Session()withrequests_staticmock.mock_session_with_fixtures(session,'tests/fixtures','http://test_context.com'):# will return a response object with the contents of tests/fixtures/test.jsonresponse=new_session.request('get','http://test_context.com/test.json')As an adapterYou can inject therequests_staticmockadapter into an existing (or new) requests session to mock out a particular URL
or domain, e.g.importrequestsfromrequests_staticmockimportAdaptersession=requests.Session()special_adapter=Adapter('fixtures')session.mount('http://specialwebsite.com',special_adapter)session.request('http://normal.com/api/example')# works as normalsession.request('http://specialwebsite.com')# returns static mocksClass adapterInstead 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.xmlwill call method_test_example_xml(self, request)GET/test/example.xml?query=paramwill call method_test_example_xml(self, request)This can be used viarequests_staticmock.ClassAdapteror the context managerimportrequestsimportrequests_staticmockclassMyTestClass(requests_staticmock.BaseMockClass):def_api_v1_idea(self,request):return"woop woop"session=requests.Session()withrequests_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')Class adapter with unpacked requestsThe class adapter supports unpacking of the following components, just add these keyword arguments
to your callback methods and the class adapter will match them to the arguments.method- The HTTP verb, e.g. GETurl- The full URLparams- The dict with the request parametersheaders- The request headersbody- The request body textimportrequestsimportrequests_staticmockclass_session=Session()classTestMockClass(BaseMockClass):def_api_v1_idea(self,method,params,headers):ifparams['special']=='value':return'yes'def_api_v1_brillo(self,url,body):ifjson.loads(body)['special']=='value':return'yes'a=ClassAdapter(TestMockClass)session=requests.Session()withrequests_staticmock.mock_session_with_class(session,MyTestClass,'http://test_context.com'):response=new_session.request('get','http://test_context.com/api/v1/idea')FeaturesAllow mocking of HTTP responses via a directory of static fixturesSupport for sub-directories matching URL pathsCreditsThis project takes inspiration and ideas from therequests_mockpackage, maintained by the OpenStack foundation.This package was created withCookiecutterand theaudreyr/cookiecutter-pypackageproject template.History1.4.0 (2017-09-01)Class adapter correctly maps - character to _ as - is invalid method name in Python1.3.0 (2017-09-01)Add a property in MockClass for the adapter instance, helps when you want to respond
with static fixture data1.2.0 (2017-05-10)Add support for case-insensitive file matching1.1.0 (2017-05-10)Add support for query params being part of the file path0.8.0 (2017-02-02)Add support for streaming requests and iter_content/iter_lines0.7.0 (2017-01-29)Add support version unpacking, class adapters now support a range of keyword arguments,
provided in no particular order.0.6.0 (2017-01-29)Add support for the class adapter methods to return either a string or
a response objectMoved to Py.Test0.3.0 (2017-01-29)Added a class adapter0.2.0 (2017-01-28)Added a context manager for the static mocks0.1.0 (2017-01-01)First release on PyPI.