[tests] add index tests

This commit is contained in:
SouthFox 2023-04-03 15:16:46 +08:00
parent 5a4c0405a6
commit bbd1d8ee37
2 changed files with 37 additions and 2 deletions

View file

@ -50,8 +50,27 @@ class ActivityPubResponse(JSONResponse):
media_type = "application/activity+json" media_type = "application/activity+json"
def is_ap_requested(req: Request) -> bool:
accept_value = req.headers.get("accept")
if not accept_value:
return False
for i in {
"application/activity+json",
"application/ld+json",
}:
if accept_value.startswith(i):
return True
return False
@app.get("/") @app.get("/")
async def index(): async def index(
request: Request
):
if is_ap_requested(request):
return ActivityPubResponse(ME)
return ME return ME
@app.post("/inbox") @app.post("/inbox")

View file

@ -1,10 +1,26 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import pytest
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from app import activitypub as ap
from app.config import AP_CONTENT_TYPE
_ACCEPTED_AP_HEADERS = [
"application/activity+json",
"application/activity+json; charset=utf-8",
"application/ld+json",
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
]
def test_index__html(client: TestClient): def test_index__html(client: TestClient):
response = client.get("/") response = client.get("/")
assert response.status_code == 200 assert response.status_code == 200
# assert response.headers["content-type"].startswith("text/html") assert response.headers["content-type"] == "application/json"
@pytest.mark.parametrize("accept", _ACCEPTED_AP_HEADERS)
def test_index__ap(db: Session, client: TestClient, accept: str):
response = client.get("/", headers={"Accept": accept})
assert response.status_code == 200
assert response.headers["content-type"] == AP_CONTENT_TYPE
assert response.json() == ap.ME