This commit is contained in:
82
test_app.py
Normal file
82
test_app.py
Normal file
@@ -0,0 +1,82 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app import app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
def test_health_check(client):
|
||||
response = client.get("/health")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"status": "ok"}
|
||||
|
||||
|
||||
def test_pdf_generation(client):
|
||||
request_data = {"html": "<h1>Hello, World!</h1>", "filename": "testfile"}
|
||||
response = client.post("/pdf", json=request_data)
|
||||
assert response.status_code == 200
|
||||
assert response.headers["Content-Type"] == "application/pdf"
|
||||
assert (
|
||||
response.headers["Content-Disposition"] == 'attachment; filename="testfile.pdf"'
|
||||
)
|
||||
|
||||
|
||||
def test_pdf_generation_default_filename(client):
|
||||
request_data = {"html": "<h1>Hello, World!</h1>", "filename": None}
|
||||
response = client.post("/pdf", json=request_data)
|
||||
assert response.status_code == 200
|
||||
assert response.headers["Content-Type"] == "application/pdf"
|
||||
assert (
|
||||
response.headers["Content-Disposition"] == 'attachment; filename="hinpdof.pdf"'
|
||||
)
|
||||
|
||||
|
||||
def test_pdf_generation_invalid_html(client):
|
||||
request_data = {"html": "", "filename": "testfile"}
|
||||
response = client.post("/pdf", json=request_data)
|
||||
assert response.status_code == 422 # Unprocessable Entity due to invalid input
|
||||
|
||||
|
||||
def test_pdf_generation_missing_html(client):
|
||||
request_data = {"filename": "testfile"}
|
||||
response = client.post("/pdf", json=request_data)
|
||||
assert (
|
||||
response.status_code == 422
|
||||
) # Unprocessable Entity due to missing required field
|
||||
|
||||
|
||||
def test_pdf_generation_large_html(client):
|
||||
large_html = "<h1>" + "Hello, World! " * 1000 + "</h1>"
|
||||
request_data = {"html": large_html, "filename": "largefile"}
|
||||
response = client.post("/pdf", json=request_data)
|
||||
assert response.status_code == 200
|
||||
assert response.headers["Content-Type"] == "application/pdf"
|
||||
assert (
|
||||
response.headers["Content-Disposition"]
|
||||
== 'attachment; filename="largefile.pdf"'
|
||||
)
|
||||
|
||||
|
||||
def test_pdf_generation_invalid_filename(client):
|
||||
request_data = {"html": "<h1>Hello, World!</h1>", "filename": "invalid/filename"}
|
||||
response = client.post("/pdf", json=request_data)
|
||||
assert response.status_code == 200
|
||||
assert response.headers["Content-Type"] == "application/pdf"
|
||||
assert (
|
||||
response.headers["Content-Disposition"]
|
||||
== 'attachment; filename="invalid_filename.pdf"'
|
||||
)
|
||||
|
||||
|
||||
def test_pdf_generation_missing_filename(client):
|
||||
request_data = {"html": "<h1>Hello, World!</h1>"}
|
||||
response = client.post("/pdf", json=request_data)
|
||||
assert response.status_code == 200
|
||||
assert response.headers["Content-Type"] == "application/pdf"
|
||||
assert (
|
||||
response.headers["Content-Disposition"] == 'attachment; filename="hinpdof.pdf"'
|
||||
)
|
||||
Reference in New Issue
Block a user