2016-10-24 09:21:06 +01:00
|
|
|
import inspect
|
|
|
|
import os
|
2022-07-31 11:26:11 +01:00
|
|
|
import sys
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2022-07-31 11:26:11 +01:00
|
|
|
from pathlib import Path
|
2018-11-20 04:28:00 +00:00
|
|
|
from time import gmtime, strftime
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2016-12-25 02:16:19 +00:00
|
|
|
import pytest
|
|
|
|
|
2022-07-31 11:26:11 +01:00
|
|
|
from sanic.app import Sanic
|
|
|
|
|
2016-12-25 02:16:19 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2016-12-25 02:16:19 +00:00
|
|
|
def static_file_directory():
|
|
|
|
"""The static directory to serve"""
|
2016-10-24 09:21:06 +01:00
|
|
|
current_file = inspect.getfile(inspect.currentframe())
|
2016-12-25 02:16:19 +00:00
|
|
|
current_directory = os.path.dirname(os.path.abspath(current_file))
|
2018-12-30 11:18:06 +00:00
|
|
|
static_directory = os.path.join(current_directory, "static")
|
2016-12-25 02:16:19 +00:00
|
|
|
return static_directory
|
|
|
|
|
|
|
|
|
2022-07-31 11:26:11 +01:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def double_dotted_directory_file(static_file_directory: str):
|
|
|
|
"""Generate double dotted directory and its files"""
|
|
|
|
if sys.platform == "win32":
|
|
|
|
raise Exception("Windows doesn't support double dotted directories")
|
|
|
|
|
|
|
|
file_path = Path(static_file_directory) / "dotted.." / "dot.txt"
|
|
|
|
double_dotted_dir = file_path.parent
|
|
|
|
Path.mkdir(double_dotted_dir, exist_ok=True)
|
|
|
|
with open(file_path, "w") as f:
|
|
|
|
f.write("DOT\n")
|
|
|
|
yield file_path
|
|
|
|
Path.unlink(file_path)
|
|
|
|
Path.rmdir(double_dotted_dir)
|
|
|
|
|
|
|
|
|
2017-02-01 15:00:57 +00:00
|
|
|
def get_file_path(static_file_directory, file_name):
|
|
|
|
return os.path.join(static_file_directory, file_name)
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2016-12-25 02:16:19 +00:00
|
|
|
|
2017-02-01 15:00:57 +00:00
|
|
|
def get_file_content(static_file_directory, file_name):
|
2016-12-25 02:16:19 +00:00
|
|
|
"""The content of the static file to check"""
|
2018-12-30 11:18:06 +00:00
|
|
|
with open(get_file_path(static_file_directory, file_name), "rb") as file:
|
2016-12-25 02:16:19 +00:00
|
|
|
return file.read()
|
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2018-11-20 04:28:00 +00:00
|
|
|
def large_file(static_file_directory):
|
2018-12-30 11:18:06 +00:00
|
|
|
large_file_path = os.path.join(static_file_directory, "large.file")
|
2018-11-20 04:28:00 +00:00
|
|
|
|
|
|
|
size = 2 * 1024 * 1024
|
2018-12-30 11:18:06 +00:00
|
|
|
with open(large_file_path, "w") as f:
|
|
|
|
f.write("a" * size)
|
2018-11-20 04:28:00 +00:00
|
|
|
|
|
|
|
yield large_file_path
|
|
|
|
|
|
|
|
os.remove(large_file_path)
|
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.fixture(autouse=True, scope="module")
|
2018-11-20 04:28:00 +00:00
|
|
|
def symlink(static_file_directory):
|
2018-12-30 11:18:06 +00:00
|
|
|
src = os.path.abspath(
|
|
|
|
os.path.join(os.path.dirname(static_file_directory), "conftest.py")
|
|
|
|
)
|
|
|
|
symlink = "symlink"
|
2018-11-20 04:28:00 +00:00
|
|
|
dist = os.path.join(static_file_directory, symlink)
|
|
|
|
os.symlink(src, dist)
|
|
|
|
yield symlink
|
|
|
|
os.remove(dist)
|
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.fixture(autouse=True, scope="module")
|
2018-11-20 04:28:00 +00:00
|
|
|
def hard_link(static_file_directory):
|
2018-12-30 11:18:06 +00:00
|
|
|
src = os.path.abspath(
|
|
|
|
os.path.join(os.path.dirname(static_file_directory), "conftest.py")
|
|
|
|
)
|
|
|
|
hard_link = "hard_link"
|
2018-11-20 04:28:00 +00:00
|
|
|
dist = os.path.join(static_file_directory, hard_link)
|
|
|
|
os.link(src, dist)
|
|
|
|
yield hard_link
|
|
|
|
os.remove(dist)
|
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"file_name",
|
|
|
|
["test.file", "decode me.txt", "python.png", "symlink", "hard_link"],
|
|
|
|
)
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_static_file(app, static_file_directory, file_name):
|
2017-02-01 15:00:57 +00:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file", get_file_path(static_file_directory, file_name)
|
|
|
|
)
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/testing.file")
|
2016-10-24 09:21:06 +01:00
|
|
|
assert response.status == 200
|
2017-02-01 15:00:57 +00:00
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2016-12-25 02:16:19 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.html"])
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_static_file_content_type(app, static_file_directory, file_name):
|
2018-07-21 06:31:15 +01:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
2018-07-21 06:31:15 +01:00
|
|
|
get_file_path(static_file_directory, file_name),
|
2018-12-30 11:18:06 +00:00
|
|
|
content_type="text/html; charset=utf-8",
|
2018-07-21 06:31:15 +01:00
|
|
|
)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/testing.file")
|
2018-07-21 06:31:15 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.headers["Content-Type"] == "text/html; charset=utf-8"
|
2018-07-21 06:31:15 +01:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"file_name", ["test.file", "decode me.txt", "symlink", "hard_link"]
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize("base_uri", ["/static", "", "/dir"])
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_static_directory(app, file_name, base_uri, static_file_directory):
|
2017-02-01 15:00:57 +00:00
|
|
|
app.static(base_uri, static_file_directory)
|
2016-12-25 02:16:19 +00:00
|
|
|
|
2020-06-05 15:14:18 +01:00
|
|
|
request, response = app.test_client.get(uri=f"{base_uri}/{file_name}")
|
2016-12-25 02:16:19 +00:00
|
|
|
assert response.status == 200
|
2017-02-01 15:00:57 +00:00
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2017-02-09 01:59:34 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_static_head_request(app, file_name, static_file_directory):
|
2017-02-09 01:37:32 +00:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.head("/testing.file")
|
2017-01-31 01:04:51 +00:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert "Accept-Ranges" in response.headers
|
|
|
|
assert "Content-Length" in response.headers
|
|
|
|
assert int(response.headers["Content-Length"]) == len(
|
|
|
|
get_file_content(static_file_directory, file_name)
|
|
|
|
)
|
2017-02-09 01:59:34 +00:00
|
|
|
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_static_content_range_correct(app, file_name, static_file_directory):
|
2017-02-09 01:37:32 +00:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
headers = {"Range": "bytes=12-19"}
|
|
|
|
request, response = app.test_client.get("/testing.file", headers=headers)
|
2018-11-07 13:36:56 +00:00
|
|
|
assert response.status == 206
|
2018-12-30 11:18:06 +00:00
|
|
|
assert "Content-Length" in response.headers
|
|
|
|
assert "Content-Range" in response.headers
|
|
|
|
static_content = bytes(get_file_content(static_file_directory, file_name))[
|
|
|
|
12:20
|
|
|
|
]
|
|
|
|
assert int(response.headers["Content-Length"]) == len(static_content)
|
2017-02-09 01:59:34 +00:00
|
|
|
assert response.body == static_content
|
|
|
|
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_static_content_range_front(app, file_name, static_file_directory):
|
2017-02-09 01:37:32 +00:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
headers = {"Range": "bytes=12-"}
|
|
|
|
request, response = app.test_client.get("/testing.file", headers=headers)
|
2018-11-07 13:36:56 +00:00
|
|
|
assert response.status == 206
|
2018-12-30 11:18:06 +00:00
|
|
|
assert "Content-Length" in response.headers
|
|
|
|
assert "Content-Range" in response.headers
|
|
|
|
static_content = bytes(get_file_content(static_file_directory, file_name))[
|
|
|
|
12:
|
|
|
|
]
|
|
|
|
assert int(response.headers["Content-Length"]) == len(static_content)
|
2017-02-09 01:59:34 +00:00
|
|
|
assert response.body == static_content
|
|
|
|
|
2017-02-09 01:37:32 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_static_content_range_back(app, file_name, static_file_directory):
|
2017-02-09 01:37:32 +00:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2017-02-09 01:59:34 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
headers = {"Range": "bytes=-12"}
|
|
|
|
request, response = app.test_client.get("/testing.file", headers=headers)
|
2018-11-07 13:36:56 +00:00
|
|
|
assert response.status == 206
|
2018-12-30 11:18:06 +00:00
|
|
|
assert "Content-Length" in response.headers
|
|
|
|
assert "Content-Range" in response.headers
|
|
|
|
static_content = bytes(get_file_content(static_file_directory, file_name))[
|
|
|
|
-12:
|
|
|
|
]
|
|
|
|
assert int(response.headers["Content-Length"]) == len(static_content)
|
2017-02-09 01:59:34 +00:00
|
|
|
assert response.body == static_content
|
|
|
|
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("use_modified_since", [True, False])
|
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
|
|
|
def test_static_content_range_empty(
|
|
|
|
app, file_name, static_file_directory, use_modified_since
|
|
|
|
):
|
2017-02-09 01:37:32 +00:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
2018-11-20 04:28:00 +00:00
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
2018-12-30 11:18:06 +00:00
|
|
|
use_modified_since=use_modified_since,
|
2018-11-20 04:28:00 +00:00
|
|
|
)
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/testing.file")
|
2017-01-31 01:04:51 +00:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert "Content-Length" in response.headers
|
|
|
|
assert "Content-Range" not in response.headers
|
|
|
|
assert int(response.headers["Content-Length"]) == len(
|
|
|
|
get_file_content(static_file_directory, file_name)
|
|
|
|
)
|
2017-02-09 01:59:34 +00:00
|
|
|
assert response.body == bytes(
|
2018-12-30 11:18:06 +00:00
|
|
|
get_file_content(static_file_directory, file_name)
|
|
|
|
)
|
2017-02-09 01:59:34 +00:00
|
|
|
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_static_content_range_error(app, file_name, static_file_directory):
|
2017-02-09 01:37:32 +00:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
headers = {"Range": "bytes=1-0"}
|
|
|
|
request, response = app.test_client.get("/testing.file", headers=headers)
|
2017-01-31 01:04:51 +00:00
|
|
|
assert response.status == 416
|
2018-12-30 11:18:06 +00:00
|
|
|
assert "Content-Length" in response.headers
|
|
|
|
assert "Content-Range" in response.headers
|
|
|
|
assert response.headers["Content-Range"] == "bytes */%s" % (
|
|
|
|
len(get_file_content(static_file_directory, file_name)),
|
|
|
|
)
|
2017-09-27 09:24:43 +01:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
|
|
|
def test_static_content_range_invalid_unit(
|
|
|
|
app, file_name, static_file_directory
|
|
|
|
):
|
2018-12-22 15:21:45 +00:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
unit = "bit"
|
2020-02-25 20:01:13 +00:00
|
|
|
headers = {"Range": f"{unit}=1-0"}
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/testing.file", headers=headers)
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
assert response.status == 416
|
2020-01-20 14:58:14 +00:00
|
|
|
assert f"{unit} is not a valid Range Type" in response.text
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
|
|
|
def test_static_content_range_invalid_start(
|
|
|
|
app, file_name, static_file_directory
|
|
|
|
):
|
2018-12-22 15:21:45 +00:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
start = "start"
|
2020-02-25 20:01:13 +00:00
|
|
|
headers = {"Range": f"bytes={start}-0"}
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/testing.file", headers=headers)
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
assert response.status == 416
|
2020-01-20 14:58:14 +00:00
|
|
|
assert f"'{start}' is invalid for Content Range" in response.text
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
|
|
|
def test_static_content_range_invalid_end(
|
|
|
|
app, file_name, static_file_directory
|
|
|
|
):
|
2018-12-22 15:21:45 +00:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
end = "end"
|
2020-02-25 20:01:13 +00:00
|
|
|
headers = {"Range": f"bytes=1-{end}"}
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/testing.file", headers=headers)
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
assert response.status == 416
|
2020-01-20 14:58:14 +00:00
|
|
|
assert f"'{end}' is invalid for Content Range" in response.text
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
|
|
|
def test_static_content_range_invalid_parameters(
|
|
|
|
app, file_name, static_file_directory
|
|
|
|
):
|
2018-12-22 15:21:45 +00:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
headers = {"Range": "bytes=-"}
|
|
|
|
request, response = app.test_client.get("/testing.file", headers=headers)
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
assert response.status == 416
|
2020-01-20 14:58:14 +00:00
|
|
|
assert "Invalid for Content Range parameters" in response.text
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"file_name", ["test.file", "decode me.txt", "python.png"]
|
|
|
|
)
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_static_file_specified_host(app, static_file_directory, file_name):
|
2017-09-27 09:24:43 +01:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
2017-09-27 09:24:43 +01:00
|
|
|
get_file_path(static_file_directory, file_name),
|
2018-12-30 11:18:06 +00:00
|
|
|
host="www.example.com",
|
2017-09-27 09:24:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
headers = {"Host": "www.example.com"}
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/testing.file", headers=headers)
|
2017-09-27 09:24:43 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/testing.file")
|
2017-09-27 09:24:43 +01:00
|
|
|
assert response.status == 404
|
2018-11-20 04:28:00 +00:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("use_modified_since", [True, False])
|
|
|
|
@pytest.mark.parametrize("stream_large_files", [True, 1024])
|
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "large.file"])
|
|
|
|
def test_static_stream_large_file(
|
|
|
|
app,
|
|
|
|
static_file_directory,
|
|
|
|
file_name,
|
|
|
|
use_modified_since,
|
|
|
|
stream_large_files,
|
|
|
|
large_file,
|
|
|
|
):
|
2018-11-20 04:28:00 +00:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
2018-11-20 04:28:00 +00:00
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_modified_since=use_modified_since,
|
2018-12-30 11:18:06 +00:00
|
|
|
stream_large_files=stream_large_files,
|
2018-11-20 04:28:00 +00:00
|
|
|
)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/testing.file")
|
2018-11-20 04:28:00 +00:00
|
|
|
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"file_name", ["test.file", "decode me.txt", "python.png"]
|
|
|
|
)
|
2018-11-20 04:28:00 +00:00
|
|
|
def test_use_modified_since(app, static_file_directory, file_name):
|
|
|
|
|
|
|
|
file_stat = os.stat(get_file_path(static_file_directory, file_name))
|
2018-12-30 11:18:06 +00:00
|
|
|
modified_since = strftime(
|
|
|
|
"%a, %d %b %Y %H:%M:%S GMT", gmtime(file_stat.st_mtime)
|
|
|
|
)
|
2018-11-20 04:28:00 +00:00
|
|
|
|
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
2018-11-20 04:28:00 +00:00
|
|
|
get_file_path(static_file_directory, file_name),
|
2018-12-30 11:18:06 +00:00
|
|
|
use_modified_since=True,
|
2018-11-20 04:28:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
request, response = app.test_client.get(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file", headers={"If-Modified-Since": modified_since}
|
|
|
|
)
|
2018-11-20 04:28:00 +00:00
|
|
|
|
|
|
|
assert response.status == 304
|
|
|
|
|
|
|
|
|
|
|
|
def test_file_not_found(app, static_file_directory):
|
2018-12-30 11:18:06 +00:00
|
|
|
app.static("/static", static_file_directory)
|
2018-11-20 04:28:00 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/static/not_found")
|
2018-11-20 04:28:00 +00:00
|
|
|
|
|
|
|
assert response.status == 404
|
2020-01-20 14:58:14 +00:00
|
|
|
assert "File not found" in response.text
|
2018-11-20 04:28:00 +00:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("static_name", ["_static_name", "static"])
|
|
|
|
@pytest.mark.parametrize("file_name", ["test.html"])
|
2018-11-20 04:28:00 +00:00
|
|
|
def test_static_name(app, static_file_directory, static_name, file_name):
|
2018-12-30 11:18:06 +00:00
|
|
|
app.static("/static", static_file_directory, name=static_name)
|
2018-11-20 04:28:00 +00:00
|
|
|
|
2020-02-25 20:01:13 +00:00
|
|
|
request, response = app.test_client.get(f"/static/{file_name}")
|
2018-11-20 04:28:00 +00:00
|
|
|
|
|
|
|
assert response.status == 200
|
2022-07-31 11:26:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.platform == "win32",
|
|
|
|
reason="Windows does not support double dotted directories",
|
|
|
|
)
|
|
|
|
def test_dotted_dir_ok(
|
|
|
|
app: Sanic, static_file_directory: str, double_dotted_directory_file: Path
|
|
|
|
):
|
|
|
|
app.static("/foo", static_file_directory)
|
|
|
|
url = (
|
|
|
|
"/foo"
|
|
|
|
+ str(double_dotted_directory_file)[len(static_file_directory) :]
|
|
|
|
)
|
|
|
|
_, response = app.test_client.get(url)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == b"DOT\n"
|
|
|
|
|
|
|
|
|
|
|
|
def test_breakout(app: Sanic, static_file_directory: str):
|
|
|
|
app.static("/foo", static_file_directory)
|
|
|
|
|
|
|
|
_, response = app.test_client.get("/foo/..%2Fstatic/test.file")
|
|
|
|
assert response.status == 400
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.platform != "win32", reason="Block backslash on Windows only"
|
|
|
|
)
|
|
|
|
def test_double_backslash_prohibited_on_win32(
|
|
|
|
app: Sanic, static_file_directory: str
|
|
|
|
):
|
|
|
|
app.static("/foo", static_file_directory)
|
|
|
|
|
|
|
|
_, response = app.test_client.get("/foo/static/..\\static/test.file")
|
|
|
|
assert response.status == 400
|
|
|
|
_, response = app.test_client.get("/foo/static\\../static/test.file")
|
|
|
|
assert response.status == 400
|