A utility library for mocking out the `urllib3` Python library.
Project description
A utility library for mocking out the urllib3 Python library.
This is an adaptation of the responses library.
Response body as string
from urllib3_mock import Responses
import requests
responses = Responses('requests.packages.urllib3')
@responses.activate
def test_my_api():
responses.add('GET', '/api/1/foobar',
body='{"error": "not found"}', status=404,
content_type='application/json')
resp = requests.get('http://twitter.com/api/1/foobar')
assert resp.json() == {"error": "not found"}
assert len(responses.calls) == 1
assert responses.calls[0].request.url == '/api/1/foobar'
assert responses.calls[0].request.host == 'twitter.com'
assert responses.calls[0].request.scheme == 'http'
Request callback
import json
from urllib3_mock import Responses
import requests
responses = Responses('requests.packages.urllib3')
@responses.activate
def test_calc_api():
def request_callback(request):
payload = json.loads(request.body)
resp_body = {'value': sum(payload['numbers'])}
headers = {'request-id': '728d329e-0e86-11e4-a748-0c84dc037c13'}
return (200, headers, json.dumps(resp_body))
responses.add_callback('POST', '/sum',
callback=request_callback,
content_type='application/json')
resp = requests.post(
'http://calc.com/sum',
json.dumps({'numbers': [1, 2, 3]}),
headers={'content-type': 'application/json'},
)
assert resp.json() == {'value': 6}
assert len(responses.calls) == 1
assert responses.calls[0].request.url == '/sum'
assert responses.calls[0].request.host == 'calc.com'
assert (
responses.calls[0].response.headers['request-id'] ==
'728d329e-0e86-11e4-a748-0c84dc037c13'
)
Instead of passing a string URL into responses.add or responses.add_callback you can also supply a compiled regular expression.
import re
from urllib3_mock import Responses
import requests
responses = Responses('requests.packages.urllib3')
# Instead of
responses.add('GET', '/api/1/foobar',
body='{"error": "not found"}', status=404,
content_type='application/json')
# You can do the following
url_re = re.compile(r'/api/\d+/foobar')
responses.add('GET', url_re,
body='{"error": "not found"}', status=404,
content_type='application/json')
A response can also throw an exception as follows.
from urllib3_mock import Responses
from requests.packages.urllib3.exceptions import HTTPError
exception = HTTPError('Something went wrong')
responses = Responses('requests.packages.urllib3')
responses.add('GET', '/api/1/foobar',
body=exception)
# All calls to 'http://twitter.com/api/1/foobar' will throw exception.
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
urllib3-mock-0.3.2.tar.gz
(9.9 kB
view details)
Built Distribution
File details
Details for the file urllib3-mock-0.3.2.tar.gz
.
File metadata
- Download URL: urllib3-mock-0.3.2.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 691be6a9a93b7cb3a8f631cda64aabef148511d059fcf935f2865aad34eeee04 |
|
MD5 | c276daee606226d22be2b7422231b049 |
|
BLAKE2b-256 | f87c00db7bb68e0eae9be5a7d4bdec39896bd065d6359fe890839f01adb1cf35 |
Provenance
File details
Details for the file urllib3_mock-0.3.2-py2.py3-none-any.whl
.
File metadata
- Download URL: urllib3_mock-0.3.2-py2.py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40522cad41cbdad21ef72b6add471d33218f56a87aa6423834d27c140579b568 |
|
MD5 | 81388d9ddbde970e4d4fdaba11c1807d |
|
BLAKE2b-256 | c0c4b783557caedd912d86bad3778301126eb6ebde20f29090a9e6753013e205 |