Skip to main content

httmock

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

Installation

pip install httmock

Or, if you are a Gentoo user:

emerge dev-python/httmock

Usage

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

@urlmatch(netloc=r'(.*\.)?google\.com$')
def google_mock(url, request):
    return 'Feeling lucky, punk?'

with HTTMock(google_mock):
    r = requests.get('http://google.com/')
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

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

@all_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('https://api.github.com/users/whatever')

print r.json().get('message')
print r.cookies['foo']

Release files for httmock 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for httmock 1.4.0
File Size Uploaded
httmock-1.4.0.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for httmock 1.4.0
File Interpreter ABI Platform
httmock-1.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.5 kB

Release files / httmock-1.4.0.tar.gz

Download URL httmock-1.4.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
44eaf4bb59cc64cd6f5d8bf8700b46aa3097cc5651b9bc85c527dfbc71792f41
BLAKE2b-256 checksum
How to use checksums
e035d881561984239dcf507625ec86f9653ba5526213ed94138a9ea6b0305169
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.2

Release files / httmock-1.4.0-py3-none-any.whl

Download URL httmock-1.4.0-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
13e6c63f135a928e15d386af789a2890efb03e0e280f29bdc9961f3f0dc34cb9
BLAKE2b-256 checksum
How to use checksums
ce99f950e23335affb58ae116aaf32565258a732b2b570aa961764df2ac0540d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

1.4.0 This release

2 release files

1.3.0

1 release file

1.2.6

1 release file

1.2.5

1 release file

1.2.4

1 release file

1.2.3

1 release file

1.2.2

1 release file

1.2.1

1 release file

1.2

1 release file

1.1.1

1 release file

1.1

1 release file

1.0.7

1 release file

1.0.6

1 release file

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page