Skip to main content

Provides an adapter for mocking HTTP requests for unit test purposes.

Project description

Currently a very basic module that provides an adapter for requests that mocks network activity for unit test purposes.

How to use

This code assumes your HTTP client is written in a way that enables passing a custom Session object. When that’s the case, all you have to do is to mount the adapter to answer for a specific prefix:

>>> import requests
>>> from requests_testadapter import TestAdapter
>>> s = requests.Session()
>>> s.mount('http://', TestAdapter(b'Mock!', status=404))
>>> r = s.get('')
>>> r.status_code
>>> r.text

You can also specify a headers dictionary as a keyword argument to TestAdapter.


In requests 1.2.0 and older, the order of mounted adapters is unpredictable. To make sure you can mount arbitrary paths with their own respective adapters, use TestSession which always matches the longest prefix:

>>> from requests_testadapter import TestAdapter, TestSession
>>> s = TestSession()
>>> s.mount('http://git', TestAdapter(b'git'))
>>> s.mount('http://github', TestAdapter(b'github'))
>>> s.mount('', TestAdapter(b''))
>>> s.mount('', TestAdapter(b''))
>>> r = s.get('')
>>> r.text
>>> r = s.get('')
>>> r.text
>>> r = s.get('')
>>> r.text

TestSession doesn’t connect the default handlers for HTTP and HTTPS so you will be notified if your requests unintentionally try to reach external websites in your unit tests:

>>> r = s.get('')
Traceback (most recent call last):
requests.exceptions.InvalidSchema: No connection adapters were found for

How do I run the tests?

The easiest way would be to extract the source tarball and run:

$ python test/

If you have all compatible Python implementations available on your system, you can run tests on all of them by using tox:

$ pip install tox
$ tox
GLOB sdist-make:
py26 inst-nodeps: .tox/dist/
py26 runtests: commands[0]
Ran 2 tests in 0.014s

py27 inst-nodeps: .tox/dist/
py27 runtests: commands[0]
Ran 2 tests in 0.014s

py32 inst-nodeps: .tox/dist/
py32 runtests: commands[0]
Ran 2 tests in 0.024s

py33 inst-nodeps: .tox/dist/
py33 runtests: commands[0]
Ran 2 tests in 0.017s

_______________________________ summary ______________________________
  py26: commands succeeded
  py27: commands succeeded
  py32: commands succeeded
  py33: commands succeeded
  congratulations :)

Change Log


  • fixed a compatibility problem with requests 1.2.1+


  • introduced TestSession to make adapter order predictable in requests 1.2 and older

  • TestAdapter doesn’t prefetch response.content anymore if stream=True is passed to a request. This lets the user read response.raw herself.


  • initial published version


Glued together by Łukasz Langa. Additional fixes by Luke Sneeringer.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

requests-testadapter-0.3.0.tar.gz (5.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page