Skip to main content

Requtests

PyPI PyPI - Python Version PyPI - Status PyPI - License Python package

Test helpers for the requests library

Installation

Install the package requtests version 1.2+ from PyPI. The recommended requirements.txt line is requtests~=1.2.

FakeAdapter

Creates an adapter intended for use with request.Session. Returns the passed Response instance when the adapter's send method is called. If the assertions function has been specified, it will be called with the request before returning the response.

The faked adapter can be mounted using the standard mount method on an instance of Session with a suitable URL prefix. Use multiple faked adapters, specifically mounted for some URL:s, to simulate a chain of requests and responses being made.

Example

from requests import Session
from requtests import FakeAdapter, fake_response


class Client:
    def __init__(self):
        self.session = Session()

    def create_user(self, username):
        return self.session.post(
            "https://example.com/users",
            params={"action": "create"},
            json={"username": username},
            headers={"Authorization": "Bearer token"},
        )


def test_create_user():
    user_created_response = fake_response(json={"message": "User created!"}, status_code=201)
    adapter = FakeAdapter(user_created_response, assertions=_create_user_assertions)
    prefix = "https://example.com/users"
    
    client = Client()
    client.session.mount(prefix, adapter)
    actual_response = client.create_user("my_username")

    assert actual_response.status_code == 201
    assert actual_response.json() == {"message": "User created!"}


def _create_user_assertions(prepared_request, **kwargs):
    assert prepared_request.method == "POST"
    assert prepared_request.url == "https://example.com/users?action=create"
    assert prepared_request.headers["Authorization"] == "Bearer token"
    assert prepared_request.body == b'{"username": "my_username"}'

fake_request

Returns a function behaving as requests.request, except that it returns a different response each time it is called. Useful to test e.g. pagination.

fake_delete

fake_get

fake_head

fake_options

fake_patch

fake_post

fake_put

Convenience functions returning partially applied fake_request functions with the HTTP method filled in.

fake_request_with_response

Similar to fake_request, except that it instantiates a single Response object and returns it based on its arguments.

Example

import requests
from requtests import fake_request_with_response


def login(username, password, request_func=requests.request):
    response = request_func("POST", "https://example.com/login", data={"username": username, "password": password})
    response.raise_for_status()
    return response.json()["token"]


def test_login():
    username = "my-username"
    password = "my-password"
    request_func = fake_request_with_response(json={"token": "my-login-token"})
    assert login(username, password, request_func=request_func) == "my-login-token"

fake_response

Returns a requests.Response object with either the return value of its json() method set to a python data structure or its text property set.

ParsedRequest

A test helper object wrapping a PreparedRequest object to make it easier to write assertions. In addition to wrapping the PreparedRequest's body, headers, method, and url properties, it also provides the following convenience properties.

  • endpoint - the URL without any query parameters.
  • query - any query parameters, parsed and decoded.
  • json - the body parsed as JSON.
  • text - the body decoded as a string.

Example

from requtests import ParsedRequest

def _create_user_assertions(prepared_request, **kwargs):
    parsed_request = ParsedRequest(prepared_request)
    assert parsed_request.method == "POST"
    assert parsed_request.url == "https://example.com/users?action=create"
    assert parsed_request.endpoint == "https://example.com/users"
    assert parsed_request.query == {"action": "create"}
    assert parsed_request.headers["Authorization"] == "Bearer token"
    assert parsed_request.body == b'{"username": "my_username"}'
    assert parsed_request.json == {"username": "my_username"}
    assert parsed_request.text == '{"username": "my_username"}'

Metadata

Release files for requtests 1.2.0

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

Source distribution (sdist)

Source distribution for requtests 1.2.0
File Size Uploaded Provenance Actions
requtests-1.2.0.tar.gz 7.9 kB Without provenance Details for requtests-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for requtests 1.2.0
File Interpreter ABI Platform Actions
requtests-1.2.0-py3-none-any.whl Python 3 none any Details for requtests-1.2.0-py3-none-any.whl

Total release size: 14.9 kB

Release files / requtests-1.2.0.tar.gz

Download URL requtests-1.2.0.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9fc47885965165ccd66889948427b60d71d05377b41c0eb535874676b6488426
BLAKE2b-256 checksum
How to use checksums
1cbee7691cefff4bc90222bf84cbe1cd69bc6c7003f3c2ecf97c072395fb26d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.18

Release files / requtests-1.2.0-py3-none-any.whl

Download URL requtests-1.2.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac080b888ffe60d4e3e24cf398e17f266fa66a2bcd958e614dac580d58594aae
BLAKE2b-256 checksum
How to use checksums
c5dc86863605614393b0ba10ee65832b4c76554392fc484183ccfc84eaf27b1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.18

Security for requtests 1.2.0

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