-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathTestApi.py
More file actions
52 lines (39 loc) · 1.37 KB
/
TestApi.py
File metadata and controls
52 lines (39 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Program to test the REST API
# Generated by chatGPT
BASE_URL = "http://localhost/api/users"
import requests
def print_response(r):
print(f"Status: {r.status_code}")
print("Response:", r.text)
print("-" * 40)
def post_user(name, email):
return requests.post(BASE_URL, json={"name": name, "email": email})
print("Creating 5 users:")
for i in range(5):
r = post_user(f"User{i+1}", f"user{i+1}@example.com")
print_response(r)
print("Fetching all users:")
r = requests.get(BASE_URL)
print_response(r)
print("Fetching user with id=1:")
print_response(requests.get(f"{BASE_URL}/1"))
print("Fetching non-existent user with id=99:")
print_response(requests.get(f"{BASE_URL}/99"))
print("Testing POST with missing 'email':")
bad_user = {"name": "BadUser"}
print_response(requests.post(BASE_URL, json=bad_user))
print("Creating 6 more users to test DB full condition:")
for i in range(6):
r = post_user(f"ExtraUser{i+1}", f"extrauser{i+1}@example.com")
print_response(r)
# Delete every second user: 1, 3, 5, 7, 9
print("Deleting every second user (1, 3, 5, 7, 9):")
for i in range(1, 11, 2):
r = requests.delete(f"{BASE_URL}/{i}")
print(f"DELETE /users/{i}")
print_response(r)
# Try adding 5 new users again
print("Adding 5 new users after deletions:")
for i in range(5):
r = post_user(f"NewUser{i+1}", f"newuser{i+1}@example.com")
print_response(r)