requests-staticmock

https://img.shields.io/pypi/v/requests-staticmock.svg https://img.shields.io/travis/tonybaloney/requests-staticmock.svg Documentation Status https://coveralls.io/repos/github/tonybaloney/requests-staticmock/badge.svg?branch=master Updates Python 3

A static HTTP mock interface for testing classes that leverage Python requests with no monkey patching!

Usage

As a context manager for requests Session instances

requests_staticmock.context.mock_session_with_fixtures(*args, **kwds)[source]

Context Manager

Mock the responses with a particular session to any files found within a static path

Parameters:
  • session (requests.Session) – The requests session object
  • path (str) – The path to the fixtures
  • url (str or list) – The base URL to mock, e.g. http://mock.com, http:// supports a single URL or a list

Example

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

requests_staticmock.context.mock_session_with_class(*args, **kwds)[source]

Context Manager

Mock the responses with a particular session to any private methods for the URLs

Parameters:
  • session (requests.Session) – The requests session object
  • cls (object) – The class instance with private methods for URLs
  • url (str or list) – The base URL to mock, e.g. http://mock.com, http:// supports a single URL or a list

Example

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')

Class adapter with unpacked requests

The 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. GET
  • url - The full URL
  • params - The dict with the request parameters
  • headers - The request headers
  • body - The request body text
import requests
import requests_staticmock

class_session = Session()
class TestMockClass(BaseMockClass):
    def _api_v1_idea(self, method, params, headers):
        if params['special'] == 'value':
            return 'yes'
    def _api_v1_brillo(self, url, body):
        if json.loads(body)['special'] == 'value':
            return 'yes'

a = ClassAdapter(TestMockClass)

session = requests.Session()
with requests_staticmock.mock_session_with_class(session, MyTestClass, 'http://test_context.com'):
    response = new_session.request('get', 'http://test_context.com/api/v1/idea')

See StaticResponseFactory for simple ways of returning good and bad responses.

class requests_staticmock.responses.StaticResponseFactory[source]

Static factory for producing internal instances of requests Response objects

static BadResponse(body, request, status_code=None, headers=None)[source]

Construct a Bad HTTP response (defined in DEFAULT_BAD_RESPONSE_CODE)

Parameters:
  • body (str) – The body of the response
  • request (requests.Request) – The HTTP request
  • status_code (int) – The return status code, defaults to DEFAULT_GOOD_STATUS_CODE if not specified
  • headers (dict) – Response headers, defaults to DEFAULT_RESPONSE_HEADERS if not specified
Return type:

requests.Response

Returns:

a Response object

static GoodResponse(body, request, status_code=None, headers=None)[source]

Construct a Good HTTP response (defined in DEFAULT_GOOD_RESPONSE_CODE)

Parameters:
  • body (str) – The body of the response
  • request (requests.Request) – The HTTP request
  • status_code (int) – The return status code, defaults to DEFAULT_GOOD_STATUS_CODE if not specified
  • headers (dict) – Response headers, defaults to DEFAULT_RESPONSE_HEADERS if not specified
Return type:

requests.Response

Returns:

a Response object

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. I redesigned this based on the abstractions within the requests project instead of using the patching pattern used in requests_mock. I find the responses more native, easier to work with and also the ability to load static files much easier.

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Contents: