A mocking library for requests.
Project description
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']
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file httmock-1.4.0.tar.gz.
File metadata
- Download URL: httmock-1.4.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44eaf4bb59cc64cd6f5d8bf8700b46aa3097cc5651b9bc85c527dfbc71792f41
|
|
| MD5 |
b6bf4aafb6f6772d4dfcfce92c81a14c
|
|
| BLAKE2b-256 |
e035d881561984239dcf507625ec86f9653ba5526213ed94138a9ea6b0305169
|
File details
Details for the file httmock-1.4.0-py3-none-any.whl.
File metadata
- Download URL: httmock-1.4.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13e6c63f135a928e15d386af789a2890efb03e0e280f29bdc9961f3f0dc34cb9
|
|
| MD5 |
6d8626f92868cd85f1bd4b7f2d84ac33
|
|
| BLAKE2b-256 |
ce99f950e23335affb58ae116aaf32565258a732b2b570aa961764df2ac0540d
|