requests_mock package

Subpackages

requests_mock.adapter module

class requests_mock.adapter.Adapter[source]

Bases: requests.adapters.BaseAdapter, requests_mock.adapter._RequestHistoryTracker

A fake adapter than can return predefined responses.

add_matcher(matcher)[source]

Register a custom matcher.

A matcher is a callable that takes a requests.Request and returns a requests.Response if it matches or None if not.

Parameters:matcher (callable) – The matcher to execute.
close()[source]
register_uri(method, url, response_list=None, **kwargs)[source]

Register a new URI match and fake response.

Parameters:
  • method (str) – The HTTP method to match.
  • url (str) – The URL to match.
send(request, **kwargs)[source]

requests_mock.exceptions module

exception requests_mock.exceptions.InvalidRequest[source]

Bases: requests_mock.exceptions.MockException

This call cannot be made under a mocked environment

exception requests_mock.exceptions.MockException[source]

Bases: exceptions.Exception

Base Exception for library

exception requests_mock.exceptions.NoMockAddress(request)[source]

Bases: requests_mock.exceptions.MockException

The requested URL was not mocked

requests_mock.fixture module

class requests_mock.contrib.fixture.Fixture(**kwargs)[source]

Bases: fixtures.fixture.Fixture, requests_mock.mocker.MockerCore

setUp()[source]

Module contents

class requests_mock.Adapter[source]

Bases: requests.adapters.BaseAdapter, requests_mock.adapter._RequestHistoryTracker

A fake adapter than can return predefined responses.

add_matcher(matcher)[source]

Register a custom matcher.

A matcher is a callable that takes a requests.Request and returns a requests.Response if it matches or None if not.

Parameters:matcher (callable) – The matcher to execute.
close()[source]
register_uri(method, url, response_list=None, **kwargs)[source]

Register a new URI match and fake response.

Parameters:
  • method (str) – The HTTP method to match.
  • url (str) – The URL to match.
send(request, **kwargs)[source]
requests_mock.create_response(request, **kwargs)[source]
Parameters:
  • status_code (int) – The status code to return upon a successful match. Defaults to 200.
  • raw (HTTPResponse) – A HTTPResponse object to return upon a successful match.
  • body (io.IOBase) – An IO object with a read() method that can return a body on successful match.
  • content (bytes) – A byte string to return upon a successful match.
  • text (unicode) – A text string to return upon a successful match.
  • json (object) – A python object to be converted to a JSON string and returned upon a successful match.
  • headers (dict) – A dictionary object containing headers that are returned upon a successful match.
  • cookies (CookieJar) – A cookie jar with cookies to set on the response.
class requests_mock.CookieJar(policy=None)[source]

Bases: requests.cookies.RequestsCookieJar

set(name, value, **kwargs)[source]

Add a cookie to the Jar.

Parameters:
  • name (str) – cookie name/key.
  • value (str) – cookie value.
  • version (int) – Integer or None. Netscape cookies have version 0. RFC 2965 and RFC 2109 cookies have a version cookie-attribute of 1. However, note that cookielib may ‘downgrade’ RFC 2109 cookies to Netscape cookies, in which case version is 0.
  • port (str) – String representing a port or a set of ports (eg. ‘80’, or ‘80,8080’),
  • domain (str) – The domain the cookie should apply to.
  • path (str) – Cookie path (a string, eg. ‘/acme/rocket_launchers’).
  • secure (bool) – True if cookie should only be returned over a secure connection.
  • expires (int) – Integer expiry date in seconds since epoch or None.
  • discard (bool) – True if this is a session cookie.
  • comment (str) – String comment from the server explaining the function of this cookie.
  • comment_url (str) – URL linking to a comment from the server explaining the function of this cookie.
requests_mock.mock

alias of Mocker

class requests_mock.Mocker(**kwargs)[source]

Bases: requests_mock.mocker.MockerCore

The standard entry point for mock Adapter loading.

TEST_PREFIX = 'test'
copy()[source]

Returns an exact copy of current mock

decorate_callable(func)[source]

Decorates a callable

Parameters:func (callable) – callable to decorate
decorate_class(klass)[source]

Decorates methods in a class with request_mock

Method will be decorated only if it name begins with TEST_PREFIX

Parameters:klass (object) – class which methods will be decorated
class requests_mock.MockerCore(**kwargs)[source]

Bases: object

A wrapper around common mocking functions.

Automate the process of mocking the requests library. This will keep the same general options available and prevent repeating code.

delete(*args, **kwargs)[source]
get(*args, **kwargs)[source]
head(*args, **kwargs)[source]
options(*args, **kwargs)[source]
patch(*args, **kwargs)[source]
post(*args, **kwargs)[source]
put(*args, **kwargs)[source]
request(*args, **kwargs)[source]
start()[source]

Start mocking requests.

Install the adapter and the wrappers required to intercept requests.

stop()[source]

Stop mocking requests.

This should have no impact if mocking has not been started.

exception requests_mock.MockException[source]

Bases: exceptions.Exception

Base Exception for library

exception requests_mock.NoMockAddress(request)[source]

Bases: requests_mock.exceptions.MockException

The requested URL was not mocked