diff --git a/app/api/routes.py b/app/api/routes.py index bdd6881..bd0b30e 100644 --- a/app/api/routes.py +++ b/app/api/routes.py @@ -244,9 +244,9 @@ def create_paste(): return _json_response(response_data, 201) -@bp.route("/", methods=["GET"]) +@bp.route("/", methods=["GET", "HEAD"]) def get_paste(paste_id: str): - """Retrieve paste metadata by ID.""" + """Retrieve paste metadata by ID. HEAD returns headers only.""" if not _is_valid_paste_id(paste_id): return _json_response({"error": "Invalid paste ID"}, 400) @@ -275,9 +275,9 @@ def get_paste(paste_id: str): }) -@bp.route("//raw", methods=["GET"]) +@bp.route("//raw", methods=["GET", "HEAD"]) def get_paste_raw(paste_id: str): - """Retrieve raw paste content with correct MIME type.""" + """Retrieve raw paste content with correct MIME type. HEAD returns headers only.""" if not _is_valid_paste_id(paste_id): return _json_response({"error": "Invalid paste ID"}, 400) diff --git a/tests/test_api.py b/tests/test_api.py index 967abe4..5628d94 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -153,6 +153,21 @@ class TestGetPaste: response = client.get("/abc") assert response.status_code == 400 + def test_head_paste_metadata(self, client, sample_text): + """HEAD returns headers without body.""" + create = client.post("/", data=sample_text, content_type="text/plain") + paste_id = json.loads(create.data)["id"] + + response = client.head(f"/{paste_id}") + assert response.status_code == 200 + assert response.content_type == "application/json" + assert response.data == b"" # No body for HEAD + + def test_head_paste_not_found(self, client): + """HEAD on nonexistent paste returns 404.""" + response = client.head("/abcd12345678") + assert response.status_code == 404 + class TestGetPasteRaw: """Tests for GET //raw endpoint.""" @@ -194,6 +209,16 @@ class TestGetPasteRaw: response = client.get(f"/{paste_id}/raw") assert response.headers.get("Content-Disposition") == "inline" + def test_head_paste_raw(self, client, sample_text): + """HEAD on raw returns content-type without body.""" + create = client.post("/", data=sample_text, content_type="text/plain") + paste_id = json.loads(create.data)["id"] + + response = client.head(f"/{paste_id}/raw") + assert response.status_code == 200 + assert response.content_type == "text/plain; charset=utf-8" + assert response.data == b"" # No body for HEAD + class TestDeletePaste: """Tests for DELETE / endpoint."""