Files
mtusi2026lab3/tests/client.py
T
2026-11-09 19:51:17 +03:00

37 lines
1.2 KiB
Python

import requests
import time
def login():
response = requests.post("http://localhost:8080/api/auth/login", json={"login": "user", "password": "password"})
return response.json()["token"]
def get_messages(token):
headers = {"Authorization": f"Bearer {token}"}
response = requests.get("http://localhost:8080/api/messages", headers=headers)
return response.json()
def post_message(token, message):
headers = {"Authorization": f"Bearer {token}", "Idempotency-Key": str(uuid.uuid4())}
response = requests.post("http://localhost:8080/api/messages", headers=headers, json={"recipient": "user", "body": message})
return response.json()
def test_idempotency(token):
message = "Hello, World!"
response1 = post_message(token, message)
response2 = post_message(token, message)
return response1 == response2
def test_health():
response = requests.get("http://localhost:8080/health")
return response.json() == {"status": "ok", "instance": "api1"}
def main():
token = login()
messages = get_messages(token)
print(messages)
assert test_idempotency(token)
assert test_health()
if __name__ == "__main__":
main()