diff --git a/tests/test_responder.py b/tests/test_responder.py index 2c479f7..7048d1b 100644 --- a/tests/test_responder.py +++ b/tests/test_responder.py @@ -2,8 +2,10 @@ import concurrent import pytest import yaml +import random import responder import requests +import string import io from starlette.responses import PlainTextResponse @@ -603,3 +605,78 @@ def test_allowed_hosts(): api._session = None r = api.session(base_url="http://tenant2.;").get(api.url_for(get)) assert r.status_code == 200 + + +def create_asset(static_dir, name=None, parent_dir=None): + if name is None: + name = random.choices(string.ascii_letters, k=6) + # :3 + ext = random.choices(string.ascii_letters, k=2) + name = f"{name}.{ext}" + + if parent_dir is None: + parent_dir = static_dir + else: + parent_dir = static_dir.mkdir(parent_dir) + + asset = parent_dir.join(name) + asset.write("body { color: blue; }") + return asset + + +def test_staticfiles(tmpdir): + static_dir = tmpdir.mkdir("static") + + asset1 = create_asset(static_dir) + parent_dir = "css" + asset2 = create_asset(static_dir, name="asset2", parent_dir=parent_dir) + + api = responder.API(static_dir=str(static_dir)) + session = api.session() + + static_route = api.static_route + + # ok + r = session.get(f"{static_route}/{asset1.basename}") + assert r.status_code == api.status_codes.HTTP_200 + + r = session.get(f"{static_route}/{parent_dir}/{asset2.basename}") + assert r.status_code == api.status_codes.HTTP_200 + + # Asset not found + r = session.get(f"{static_route}/not_found.css") + assert r.status_code == api.status_codes.HTTP_404 + + # Not found on dir listing + r = session.get(f"{static_route}") + assert r.status_code == api.status_codes.HTTP_404 + + r = session.get(f"{static_route}/{parent_dir}") + assert r.status_code == api.status_codes.HTTP_404 + + +def test_staticfiles_custom_route(tmpdir): + static_dir = tmpdir.mkdir("static") + static_route = "custom/static/route/" + + asset = create_asset(static_dir) + + api = responder.API(static_dir=str(static_dir), static_route=static_route) + session = api.session() + + # Check + assert api.static_route == "/custom/static/route" + + static_route = api.static_route + + # ok + r = session.get(f"{static_route}/{asset.basename}") + assert r.status_code == api.status_codes.HTTP_200 + + # Asset not found + r = session.get(f"{static_route}/not_found.css") + assert r.status_code == api.status_codes.HTTP_404 + + # Not found on dir listing + r = session.get(f"{static_route}") + assert r.status_code == api.status_codes.HTTP_404