Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

pypm install requests-testadapter

How to install requests-testadapter

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install requests-testadapter
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
Windows (64-bit)
Mac OS X (10.5+)
Linux (32-bit)
Linux (64-bit)
0.3.0 Available View build log
Lastest release
version 0.3.0 on Sep 20th, 2013

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('http://mocked.com')
>>> 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('http://github.com', TestAdapter(b'github.com'))
>>> s.mount('http://github.com/about/', TestAdapter(b'github.com/about'))
>>> r = s.get('http://github.com/about/')
>>> r.text
>>> r = s.get('http://github.com')
>>> r.text
>>> r = s.get('http://gittip.com')
>>> 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('http://bitbucket.org')
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/test_testadapter.py

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: setup.py
py26 inst-nodeps: .tox/dist/requests-testadapter-0.1.0.zip
py26 runtests: commands[0]
Ran 2 tests in 0.014s

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

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

py33 inst-nodeps: .tox/dist/requests-testadapter-0.1.0.zip
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.

Subscribe to package updates

Last updated Sep 20th, 2013

Download Stats

Last month:3

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.