Simple patching of `requests` calls
Project description
patch-requests
Simple patching of requests
calls.
The package contains a patch_requests
context manager, which can be used to
mock one or multiple calls to requests.FOO()
methods. Also calls to
requests.Session().FOO()
are mocked automatically.
The patch_requests
context manager takes a list of expected HTTP methods to
be called, as well as their expected status codes and returned data. A single
expected call is a tuple of ("method", ("status_code (int)", "returned_data (either dict, str or bytes)"))
. E.g.:
with patch_requests([('get', (200, {'ids': [1, 3, 4]}))]) as cm:
assert requests.get('http://whatever').json() == {'ids': [1, 3, 4]}
Some assumptions are made based on the type of given "returned data":
- if type is
dict
, the return value ofresponse.json()
is set to the given value - if type is
str
, theresponse.text
is set to the given value - if type is
bytes
, theresponse.content
is set to the given value.
Upon exiting the patch_requests
context, it is possible to access the mocks of
each requests
calls with cm.mocks
, which is a dictionary with method names as keys.
For example:
with patch_requests([('get', (200, {'ids': [1, 3, 4]}))]) as cm:
assert requests.get('http://whatever').json() == {'ids': [1, 3, 4]}
assert cm.mocks['get'].call_args_list[0][0][0] == 'http://whatever'
A full example from tests.py:
import unittest
import requests
from patch_requests import patch_requests
class TestPatcher(unittest.TestCase):
def test_multiple_requests(self):
with patch_requests([
('get', (200, {1: 1})),
('post', (201, {2: 2})),
('GET', (404, '<html><p><br/>')),
('patch', (500, b'\\')),
]) as p:
response = requests.get('http://example.com')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {1: 1})
s = requests.Session()
response = s.post('http://www.example.com')
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json(), {2: 2})
response = s.get('http://')
self.assertEqual(response.status_code, 404)
self.assertEqual(response.text, '<html><p><br/>')
s.close()
response = requests.patch('')
self.assertEqual(response.status_code, 500)
self.assertEqual(response.content, b'\\')
self.assertEqual(
p.mocks['get'].call_args_list[0][0], ('http://example.com',))
self.assertEqual(
p.mocks['post'].call_args_list[0][0], ('http://www.example.com',))
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
File details
Details for the file patch-requests-0.3.1.tar.gz
.
File metadata
- Download URL: patch-requests-0.3.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f40462ec88501e475ff60a40ed36cf2f3c654c2d0a0b0fbcfa52b6999f31a4cd |
|
MD5 | 4a90796c6c5c144340f4a9eea8cd8d5a |
|
BLAKE2b-256 | 4da6e09d09c00bdcc71f0c4d2a8795565b2c842e1c7d3fd2f201e5ccd5d733de |
File details
Details for the file patch_requests-0.3.1-py3-none-any.whl
.
File metadata
- Download URL: patch_requests-0.3.1-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17bbec3591bda872c7c7668bc0f198592742cf16e97e8ce3d96add48d8224424 |
|
MD5 | 8b2f9c01c43f523f656dc8bee3574443 |
|
BLAKE2b-256 | 9e86629263f7221e61de296a6afad3413ebd0b5e75cd57c12b029e5f5f5c763d |