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)
| File | Size | Uploaded | |
|---|---|---|---|
| httmock-1.4.0.tar.gz | 4.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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
|