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:
```python
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:
```python
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:
```python
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']
```
Raw data
{
"_id": null,
"home_page": "https://github.com/patrys/httmock",
"name": "httmock",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "requests,testing,mock",
"author": "Patryk Zawadzki",
"author_email": "patrys@room-303.com",
"download_url": "https://files.pythonhosted.org/packages/e0/35/d881561984239dcf507625ec86f9653ba5526213ed94138a9ea6b0305169/httmock-1.4.0.tar.gz",
"platform": "",
"description": "httmock\n=======\n\nA mocking library for `requests` for Python 2.7 and 3.4+.\n\nInstallation\n------------\n\n pip install httmock\n\nOr, if you are a Gentoo user:\n\n emerge dev-python/httmock\n\nUsage\n-----\nYou can use it to mock third-party APIs and test libraries that use `requests` internally, conditionally using mocked replies with the `urlmatch` decorator:\n\n```python\nfrom httmock import urlmatch, HTTMock\nimport requests\n\n@urlmatch(netloc=r'(.*\\.)?google\\.com$')\ndef google_mock(url, request):\n return 'Feeling lucky, punk?'\n\nwith HTTMock(google_mock):\n r = requests.get('http://google.com/')\nprint r.content # 'Feeling lucky, punk?'\n```\n\nThe `all_requests` decorator doesn't conditionally block real requests. If you return a dictionary, it will map to the `requests.Response` object returned:\n\n```python\nfrom httmock import all_requests, HTTMock\nimport requests\n\n@all_requests\ndef response_content(url, request):\n\treturn {'status_code': 200,\n\t 'content': 'Oh hai'}\n\nwith HTTMock(response_content):\n\tr = requests.get('https://foo_bar')\n\nprint r.status_code\nprint r.content\n```\n\nIf 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:\n\n```python\nfrom httmock import all_requests, response, HTTMock\nimport requests\n\n@all_requests\ndef response_content(url, request):\n\theaders = {'content-type': 'application/json',\n\t 'Set-Cookie': 'foo=bar;'}\n\tcontent = {'message': 'API rate limit exceeded'}\n\treturn response(403, content, headers, None, 5, request)\n\nwith HTTMock(response_content):\n\tr = requests.get('https://api.github.com/users/whatever')\n\nprint r.json().get('message')\nprint r.cookies['foo']\n```\n\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "A mocking library for requests.",
"version": "1.4.0",
"split_keywords": [
"requests",
"testing",
"mock"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "6d8626f92868cd85f1bd4b7f2d84ac33",
"sha256": "13e6c63f135a928e15d386af789a2890efb03e0e280f29bdc9961f3f0dc34cb9"
},
"downloads": -1,
"filename": "httmock-1.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6d8626f92868cd85f1bd4b7f2d84ac33",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 4754,
"upload_time": "2020-10-28T12:01:29",
"upload_time_iso_8601": "2020-10-28T12:01:29.100916Z",
"url": "https://files.pythonhosted.org/packages/ce/99/f950e23335affb58ae116aaf32565258a732b2b570aa961764df2ac0540d/httmock-1.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "b6bf4aafb6f6772d4dfcfce92c81a14c",
"sha256": "44eaf4bb59cc64cd6f5d8bf8700b46aa3097cc5651b9bc85c527dfbc71792f41"
},
"downloads": -1,
"filename": "httmock-1.4.0.tar.gz",
"has_sig": false,
"md5_digest": "b6bf4aafb6f6772d4dfcfce92c81a14c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4795,
"upload_time": "2020-10-28T12:01:30",
"upload_time_iso_8601": "2020-10-28T12:01:30.325959Z",
"url": "https://files.pythonhosted.org/packages/e0/35/d881561984239dcf507625ec86f9653ba5526213ed94138a9ea6b0305169/httmock-1.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2020-10-28 12:01:30",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "patrys",
"github_project": "httmock",
"travis_ci": true,
"coveralls": true,
"github_actions": false,
"lcname": "httmock"
}