Skip to content

Commit

Permalink
Fix statics and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil committed Dec 26, 2024
1 parent f7ff231 commit 14f2fcf
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/test_static_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import pytest
from pydantic import ValidationError

from esmerald import Include, get
from esmerald.config import StaticFilesConfig
from esmerald.config.template import TemplateConfig
from esmerald.testclient import create_client


Expand Down Expand Up @@ -117,3 +119,51 @@ def test_static_substring_of_self(tmpdir: Any) -> None:
response = client.get("/static/static_part/static/test.txt")
assert response.status_code == 200
assert response.text == "content"


@pytest.mark.parametrize("redirect_slashes", [True, False])
def test_mixing_static_and_include(tmpdir: Any, redirect_slashes) -> None:
@get("/include")
async def get_include() -> str:
return "include"

path = tmpdir.join("test.txt")
path.write("content")
static_files_config = StaticFilesConfig(path="/static", directory=tmpdir)

with create_client(
[Include("/", routes=[get_include])],
static_files_config=static_files_config,
redirect_slashes=redirect_slashes,
) as client:
response = client.get("/static/test.txt")
assert response.status_code == 200
assert response.text == "content"

response = client.get("/include")
assert response.status_code == 200


@pytest.mark.parametrize("redirect_slashes", [True, False])
def test_mixing_static_and_include_template_config(tmpdir: Any, template_dir, redirect_slashes) -> None:
@get("/include")
async def get_include() -> str:
return "include"

path = tmpdir.join("test.txt")
path.write("content")
static_files_config = StaticFilesConfig(path="/static", directory=tmpdir)
template_config = TemplateConfig(directory=template_dir)

with create_client(
[Include("/", routes=[get_include])],
static_files_config=static_files_config,
redirect_slashes=redirect_slashes,
template_config=template_config
) as client:
response = client.get("/static/test.txt")
assert response.status_code == 200
assert response.text == "content"

response = client.get("/include")
assert response.status_code == 200

0 comments on commit 14f2fcf

Please sign in to comment.