Skip to content

Commit 1f58042

Browse files
committed
Update README with code examples
1 parent f6feaa0 commit 1f58042

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

README.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,84 @@ Test helpers for the [requests](https://docs.python-requests.org) library
1111
## Installation
1212

1313
Install the package `requtests` version `1.0+` from PyPI.
14-
The recommended `requirements.txt` line is `requtests~=1.0`.
14+
The recommended `requirements.txt` line is `requtests~=1.0`.
15+
16+
### `FakeAdapter`
17+
18+
Creates an adapter intended for use with `request.Session`.
19+
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.
20+
21+
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.
22+
23+
#### Example
24+
25+
```python3
26+
from requtests import FakeAdapter, fake_response
27+
from requests import Session
28+
29+
30+
class Client:
31+
def __init__(self):
32+
self.session = Session()
33+
34+
def create_user(self, username):
35+
return self.session.post(
36+
"https://example.com/users",
37+
params={"action": "create"},
38+
json={"username": username},
39+
headers={"Authorization": "Bearer token"},
40+
)
41+
42+
43+
def test_create_user():
44+
user_created_response = fake_response(json={"message": "User created!"}, status_code=201)
45+
adapter = FakeAdapter(user_created_response, assertions=_create_user_assertions)
46+
prefix = "https://example.com/users"
47+
48+
client = Client()
49+
client.session.mount(prefix, adapter)
50+
actual_response = client.create_user("my_username")
51+
52+
assert actual_response.status_code == 201
53+
assert actual_response.json() == {"message": "User created!"}
54+
55+
56+
def _create_user_assertions(prepared_request, **kwargs):
57+
assert prepared_request.method == "POST"
58+
assert prepared_request.url == "https://example.com/users?action=create"
59+
assert prepared_request.headers["Authorization"] == "Bearer token"
60+
assert prepared_request.body == b'{"username": "my_username"}'
61+
```
62+
63+
### `fake_request`
64+
65+
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.
66+
67+
### `fake_request_with_response`
68+
69+
Similar to `fake_request`, except that it instantiates a single `Response` object and returns it based on its arguments.
70+
71+
#### Example
72+
73+
```python3
74+
import requests
75+
from requtests import fake_request_with_response
76+
77+
78+
def login(username, password, request_func=requests.request):
79+
response = request_func("POST", "https://example.com/login", data={"username": username, "password": password})
80+
response.raise_for_status()
81+
return response.json()["token"]
82+
83+
84+
def test_login():
85+
username = "my-username"
86+
password = "my-password"
87+
request_func = fake_request_with_response(json={"token": "my-login-token"})
88+
assert login(username, password, request_func=request_func) == "my-login-token"
89+
90+
```
91+
92+
### `fake_response`
93+
94+
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.

0 commit comments

Comments
 (0)