Skip to main content

A mocking library for requests.

Project description

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
Description: httmock

A mocking library for `requests` for Python 2.7 and 3.4+.


pip install httmock

Or, if you are a Gentoo user:

emerge dev-python/httmock

You can use it to mock third-party APIs and test libraries that use `requests` internally, conditionally using mocked replies with the `urlmatch` decorator:

from httmock import urlmatch, HTTMock
import requests

def google_mock(url, request):
return 'Feeling lucky, punk?'

with HTTMock(google_mock):
r = requests.get('')
print r.content # 'Feeling lucky, punk?'

The `all_requests` decorator doesn't conditionally block real requests. If you return a dictionary, it will map to the `requests.Response` object returned:

from httmock import all_requests, HTTMock
import requests

def response_content(url, request):
return {'status_code': 200,
'content': 'Oh hai'}

with HTTMock(response_content):
r = requests.get('https://foo_bar')

print r.status_code
print r.content

If you pass in `Set-Cookie` headers, `requests.Response.cookies` will contain the values. You can also use `response` method directly instead of returning a dict:

from httmock import all_requests, response, HTTMock
import requests

def response_content(url, request):
headers = {'content-type': 'application/json',
'Set-Cookie': 'foo=bar;'}
content = {'message': 'API rate limit exceeded'}
return response(403, content, headers, None, 5, request)

with HTTMock(response_content):
r = requests.get('')

print r.json().get('message')
print r.cookies['foo']
Keywords: requests,testing,mock
Platform: UNKNOWN
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 3
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Testing
Classifier: Operating System :: OS Independent

Project details

Download files

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

Files for httmock, version 1.3.0
Filename, size File type Python version Upload date Hashes
Filename, size httmock-1.3.0.tar.gz (4.4 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page