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.
Metadata
Release files for urllib3-mock 0.3.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| urllib3-mock-0.3.3.tar.gz | 10.4 kB | Without provenance | Details for urllib3-mock-0.3.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| urllib3_mock-0.3.3-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for urllib3_mock-0.3.3-py2.py3-none-any.whl |
Total release size: 16.7 kB
Release files / urllib3-mock-0.3.3.tar.gz
| Download URL | urllib3-mock-0.3.3.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b210037029ac96beac4f3e7b54f466c394b060525ea5a824803d5f5ed14558f1
|
|
BLAKE2b-256 checksum How to use checksums |
b0236a338cfb7c922e455725c3a4cd2df59f05294f0406f9670e20e115b331e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / urllib3_mock-0.3.3-py2.py3-none-any.whl
| Download URL | urllib3_mock-0.3.3-py2.py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
702c90042920d771c9902b7b5b542551cc57f259078f4eada47ab4e8cdd11f1a
|
|
BLAKE2b-256 checksum How to use checksums |
64ec4c723737b2c7733b6b7257d6990aa813144e2bb96e75ceaf2791ee815277
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for urllib3-mock 0.3.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.