Need help with httmock?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

patrys
422 Stars 52 Forks Other 102 Commits 15 Opened issues

Description

A mocking library for requests

Services available

!
?

Need anything else?

Contributors list

# 3,748
Django
GraphQL
graphql...
paypal
28 commits
# 183,543
Shell
Go
HTML
subcomm...
4 commits
# 94,277
GraphQL
Java
Shell
HTTP
3 commits
# 20,439
Google
python-...
python-...
google-...
3 commits
# 282,359
HTML
CSS
Shell
HTTP
2 commits
# 294,205
Elixir
HTML
CSS
Shell
2 commits
# 186,642
C++
C
Twitter
Flask
2 commits
# 427,144
CSS
gtk
HTML
Postgre...
2 commits
# 314,239
CSS
Shell
HTML
paypal
1 commit
# 212,345
TeX
natural...
languag...
SQL
1 commit
# 92,513
HTML
python3
task-ma...
Shell
1 commit
# 461,294
C
Shell
C++
digital...
1 commit
# 161,668
faceboo...
rabbitm...
celery
Django
1 commit
# 191,142
HTTP
HTML
Maven
Gradle
1 commit
# 203,162
C
Shell
bitmap
SQLite
1 commit
# 38,433
XML
hashtab...
Django
Flask
1 commit
# 117,203
Firefox
phantom...
Git
pypy
1 commit
# 53,440
Haskell
Shell
elm
configu...
1 commit
# 8,981
servo
Django
mpd
afl
1 commit
# 605,096
Python
HTTP
1 commit

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']

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.