Skip to main content

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 of response.json() is set to the given value
  • if type is str, the response.text is set to the given value
  • if type is bytes, the response.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',))

Metadata

Release files for patch-requests 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for patch-requests 0.3.1
File Size Uploaded Provenance Actions
patch-requests-0.3.1.tar.gz 4.2 kB Without provenance Details for patch-requests-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for patch-requests 0.3.1
File Interpreter ABI Platform Actions
patch_requests-0.3.1-py3-none-any.whl Python 3 none any Details for patch_requests-0.3.1-py3-none-any.whl

Total release size: 8.7 kB

Release files / patch-requests-0.3.1.tar.gz

Download URL patch-requests-0.3.1.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f40462ec88501e475ff60a40ed36cf2f3c654c2d0a0b0fbcfa52b6999f31a4cd
BLAKE2b-256 checksum
How to use checksums
4da6e09d09c00bdcc71f0c4d2a8795565b2c842e1c7d3fd2f201e5ccd5d733de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / patch_requests-0.3.1-py3-none-any.whl

Download URL patch_requests-0.3.1-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
17bbec3591bda872c7c7668bc0f198592742cf16e97e8ce3d96add48d8224424
BLAKE2b-256 checksum
How to use checksums
9e86629263f7221e61de296a6afad3413ebd0b5e75cd57c12b029e5f5f5c763d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Security for patch-requests 0.3.1

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page