forked from username/flaskpaste
add HEAD method for paste endpoints
This commit is contained in:
@@ -244,9 +244,9 @@ def create_paste():
|
||||
return _json_response(response_data, 201)
|
||||
|
||||
|
||||
@bp.route("/<paste_id>", methods=["GET"])
|
||||
@bp.route("/<paste_id>", 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("/<paste_id>/raw", methods=["GET"])
|
||||
@bp.route("/<paste_id>/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)
|
||||
|
||||
|
||||
@@ -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 /<id>/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 /<id> endpoint."""
|
||||
|
||||
Reference in New Issue
Block a user