Test helpers for the requests library.
Project description
Requtests
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"}'
Project details
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 requtests-1.2.0.tar.gz
.
File metadata
- Download URL: requtests-1.2.0.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fc47885965165ccd66889948427b60d71d05377b41c0eb535874676b6488426 |
|
MD5 | 985dd5138475b568dbec97cce0a73b70 |
|
BLAKE2b-256 | 1cbee7691cefff4bc90222bf84cbe1cd69bc6c7003f3c2ecf97c072395fb26d4 |
File details
Details for the file requtests-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: requtests-1.2.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac080b888ffe60d4e3e24cf398e17f266fa66a2bcd958e614dac580d58594aae |
|
MD5 | b870e63682c35a95c08552041c458b58 |
|
BLAKE2b-256 | c5dc86863605614393b0ba10ee65832b4c76554392fc484183ccfc84eaf27b1d |