From bbd1d8ee373b456698f7166364e4f629113ac859 Mon Sep 17 00:00:00 2001 From: southfox Date: Mon, 3 Apr 2023 15:16:46 +0800 Subject: [PATCH] [tests] add index tests --- app/main.py | 21 ++++++++++++++++++++- tests/test_main.py | 18 +++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 377f026..7915e3b 100644 --- a/app/main.py +++ b/app/main.py @@ -50,8 +50,27 @@ class ActivityPubResponse(JSONResponse): 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("/") -async def index(): +async def index( + request: Request +): + if is_ap_requested(request): + return ActivityPubResponse(ME) + return ME @app.post("/inbox") diff --git a/tests/test_main.py b/tests/test_main.py index dd3ad30..5fcb45a 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,10 +1,26 @@ #!/usr/bin/env python3 +import pytest from fastapi.testclient import TestClient 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): response = client.get("/") 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