forked from mtusi2026/mtusi2026lab4
Собрать стенд lab4 с вложениями в приватном S3-bucket.
Метаданные остаются в PostgreSQL, байты — в RustFS, чтобы скачивание пережило остановку одной реплики API.
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Проверка лимита размера на самом API, мимо балансировщика. Токен не печатает."""
|
||||
import http.client
|
||||
import importlib
|
||||
import json
|
||||
import time
|
||||
import uuid
|
||||
import urllib.request
|
||||
|
||||
urlerr = importlib.import_module("urllib.error")
|
||||
|
||||
BASE = "http://127.0.0.1:8080"
|
||||
|
||||
|
||||
def call(method, path, body=None, headers=None, timeout=5):
|
||||
req = urllib.request.Request(BASE + path, data=body, headers=headers or {}, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=timeout) as response:
|
||||
return response.status, response.read()
|
||||
except urlerr.HTTPError as exc:
|
||||
return exc.code, exc.read()
|
||||
|
||||
|
||||
def main():
|
||||
username = "u_sz_" + uuid.uuid4().hex[:12]
|
||||
password = "T3st-" + uuid.uuid4().hex
|
||||
status, raw = call(
|
||||
"POST", "/api/v1/users",
|
||||
json.dumps({"username": username, "display_name": "Size", "password": password}).encode(),
|
||||
{"Content-Type": "application/json"},
|
||||
)
|
||||
if status != 201:
|
||||
raise SystemExit("register %s" % status)
|
||||
user = json.loads(raw)
|
||||
import base64
|
||||
token = base64.b64encode((username + ":" + password).encode()).decode()
|
||||
status, raw = call("POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + token})
|
||||
if status != 201:
|
||||
raise SystemExit("login %s" % status)
|
||||
bearer = json.loads(raw)["token"]
|
||||
auth = {"Authorization": "Bearer " + bearer, "Content-Type": "application/json"}
|
||||
status, raw = call(
|
||||
"POST", "/api/v1/chats",
|
||||
json.dumps({"title": "limit", "member_ids": [user["id"]]}).encode(),
|
||||
auth,
|
||||
)
|
||||
if status != 201:
|
||||
raise SystemExit("chat %s" % status)
|
||||
chat = json.loads(raw)["id"]
|
||||
# Заголовок больше лимита, тело из одного байта: API должен ответить до чтения остальных байт.
|
||||
started = time.monotonic()
|
||||
conn = http.client.HTTPConnection("127.0.0.1", 8080, timeout=3)
|
||||
conn.request(
|
||||
"POST",
|
||||
"/api/v1/chats/" + chat + "/attachments?filename=big.png",
|
||||
body=b"x",
|
||||
headers={
|
||||
"Authorization": "Bearer " + bearer,
|
||||
"Content-Type": "image/png",
|
||||
"Content-Length": "10485761",
|
||||
},
|
||||
)
|
||||
response = conn.getresponse()
|
||||
status = response.status
|
||||
elapsed = time.monotonic() - started
|
||||
conn.close()
|
||||
if status != 413:
|
||||
raise SystemExit("expected 413, got %s" % status)
|
||||
if elapsed > 2:
|
||||
raise SystemExit("limit check took too long: %.3f" % elapsed)
|
||||
print("oversized Content-Length -> %s in %.3fs" % (status, elapsed))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user