API Reference

Adapter Module

class requests_staticmock.adapter.Adapter(base_path=None)[source]

A replacement session adapter that responds with the content of static files matching the path of the requested URL

close()[source]

Cleans up adapter specific items.

match_url(request)[source]

Match the request against a file in the adapter directory

Parameters:request (requests.Request) – The request
Returns:Path to the file
Return type:str
register_path(path)[source]

Register a new search path

Parameters:path (str) – The new search path
response_from_fixture(request, fixture_path)[source]
send(request, **kwargs)[source]

Sends PreparedRequest object. Returns Response object.

Parameters:
  • request – The PreparedRequest being sent.
  • stream – (optional) Whether to stream the request content.
  • timeout (float or tuple) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, read timeout) tuple.
  • verify – (optional) Either a boolean, in which case it controls whether we verify the server’s TLS certificate, or a string, in which case it must be a path to a CA bundle to use
  • cert – (optional) Any user-provided SSL certificate to be trusted.
  • proxies – (optional) The proxies dictionary to apply to the request.
class requests_staticmock.adapter.ClassAdapter(cls)[source]

A requests Adapter for a class that has methods matching the URLS, e.g. def _api_v1_test() would be called for session.get(‘api/v1/test’)

send(request, **kwargs)[source]

Sends PreparedRequest object. Returns Response object.

Parameters:
  • request – The PreparedRequest being sent.
  • stream – (optional) Whether to stream the request content.
  • timeout (float or tuple) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, read timeout) tuple.
  • verify – (optional) Either a boolean, in which case it controls whether we verify the server’s TLS certificate, or a string, in which case it must be a path to a CA bundle to use
  • cert – (optional) Any user-provided SSL certificate to be trusted.
  • proxies – (optional) The proxies dictionary to apply to the request.

Context Managers

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

Response Factory

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