2016-10-24 09:21:06 +01:00
|
|
|
import inspect
|
2021-04-06 20:20:25 +01:00
|
|
|
import logging
|
2016-10-24 09:21:06 +01:00
|
|
|
import os
|
2021-01-28 07:34:51 +00:00
|
|
|
|
2021-04-06 20:20:25 +01:00
|
|
|
from collections import Counter
|
2021-01-19 01:53:14 +00: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
|
|
|
|
|
2021-04-06 20:20:25 +01:00
|
|
|
from sanic import text
|
|
|
|
from sanic.exceptions import FileNotFound
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-01-19 01:53:14 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"file_name",
|
|
|
|
["test.file", "decode me.txt", "python.png", "symlink", "hard_link"],
|
|
|
|
)
|
|
|
|
def test_static_file_pathlib(app, static_file_directory, file_name):
|
|
|
|
file_path = Path(get_file_path(static_file_directory, file_name))
|
|
|
|
app.static("/testing.file", file_path)
|
|
|
|
request, response = app.test_client.get("/testing.file")
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"file_name",
|
|
|
|
[b"test.file", b"decode me.txt", b"python.png"],
|
|
|
|
)
|
|
|
|
def test_static_file_bytes(app, static_file_directory, file_name):
|
2021-01-28 07:34:51 +00:00
|
|
|
bsep = os.path.sep.encode("utf-8")
|
|
|
|
file_path = static_file_directory.encode("utf-8") + bsep + file_name
|
2021-01-19 01:53:14 +00:00
|
|
|
app.static("/testing.file", file_path)
|
|
|
|
request, response = app.test_client.get("/testing.file")
|
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"file_name",
|
2021-03-05 08:26:03 +00:00
|
|
|
[{}, [], object()],
|
2021-01-19 01:53:14 +00:00
|
|
|
)
|
|
|
|
def test_static_file_invalid_path(app, static_file_directory, file_name):
|
2021-02-07 09:38:37 +00:00
|
|
|
app.route("/")(lambda x: x)
|
2021-01-19 01:53:14 +00:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
app.static("/testing.file", file_name)
|
|
|
|
request, response = app.test_client.get("/testing.file")
|
|
|
|
assert response.status == 404
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2021-02-15 19:50:20 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"file_name,expected",
|
|
|
|
[
|
|
|
|
("test.html", "text/html; charset=utf-8"),
|
|
|
|
("decode me.txt", "text/plain; charset=utf-8"),
|
|
|
|
("test.file", "application/octet-stream"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_static_file_content_type_guessed(
|
|
|
|
app, static_file_directory, file_name, expected
|
|
|
|
):
|
|
|
|
app.static(
|
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
)
|
|
|
|
|
|
|
|
request, response = app.test_client.get("/testing.file")
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
|
|
|
assert response.headers["Content-Type"] == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_static_file_content_type_with_charset(app, static_file_directory):
|
|
|
|
app.static(
|
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, "decode me.txt"),
|
|
|
|
content_type="text/plain;charset=ISO-8859-1",
|
|
|
|
)
|
|
|
|
|
|
|
|
request, response = app.test_client.get("/testing.file")
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.headers["Content-Type"] == "text/plain;charset=ISO-8859-1"
|
|
|
|
|
|
|
|
|
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
|
2021-03-21 13:03:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_nested_dir(app, static_file_directory):
|
|
|
|
app.static("/static", static_file_directory)
|
|
|
|
|
|
|
|
request, response = app.test_client.get("/static/nested/dir/foo.txt")
|
|
|
|
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == "foo\n"
|
2021-04-06 20:20:25 +01:00
|
|
|
|
|
|
|
|
Merge in main to current-release (#2254)
* Remove unnecessary import in test_constants.py, which also fixes an error on win (#2180)
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
* Manually reset the buffer when streaming request body (#2183)
* Remove Duplicated Dependencies and PEP 517 Support (#2173)
* Remove duplicated dependencies
* Specify setuptools as the tool for generating distribution (PEP 517)
* Add `isort` to `dev_require`
* manage all dependencies in setup.py
* Execute `make pretty`
* Set usedevelop to true (revert previous change)
* Fix the handling of the end of a chunked request. (#2188)
* Fix the handling of the end of a chunked request.
* Avoid hardcoding final chunk header size.
* Add some unit tests for pipeline body reading
* Decode bytes for json serialization
Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Resolve regressions in exceptions (#2181)
* Update sanic-routing to fix path issues plus lookahead / lookbehind support (#2178)
* Update sanic-routing to fix path issues plus lookahead / lookbehind support
* Update setup.py
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
* style(app,blueprints): add some type hints (#2196)
* style(app,blueprints): add some type hints
* style(app): option is Any
* style(blueprints): url prefix default value is ``""``
* style(app): backward compatible
* style(app): backward compatible
* style(blueprints): defult is None
* style(app): apply code style (black)
* Update some CC config (#2199)
* Update README.rst
* raise exception for `_static_request_handler` unknown exception; add test with custom error (#2195)
Co-authored-by: n.feofanov <n.feofanov@visionlabs.ru>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
* Change dumps to AnyStr (#2193)
* HTTP tests (#2194)
* Fix issues with after request handling in HTTP pipelining (#2201)
* Clean up after a request is complete, before the next pipelined request.
* Limit the size of request body consumed after handler has finished.
* Linter error.
* Add unit test re: bad headers
Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Update CHANGELOG
* Log remote address if available (#2207)
* Log remote address if available
* Add tests
* Fix testing version
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Fixed for handling exceptions of asgi app call. (#2211)
@cansarigol3megawatt Thanks for looking into this and getting the quick turnaround on this. I will :cherries: pick this into the 21.6 branch and get it out a little later tonight.
* Signals Integration (#2160)
* Update some tests
* Resolve #2122 route decorator returning tuple
* Use rc sanic-routing version
* Update unit tests to <:str>
* Minimal working version with some signals implemented
* Add more http signals
* Update ASGI and change listeners to signals
* Allow for dynamic ODE signals
* Allow signals to be stacked
* Begin tests
* Prioritize match_info on keyword argument injection
* WIP on tests
* Compat with signals
* Work through some test coverage
* Passing tests
* Post linting
* Setup proper resets
* coverage reporting
* Fixes from vltr comments
* clear delayed tasks
* Fix bad test
* rm pycache
* uncomment windows tests (#2214)
* Add convenience methods to BP groups (#2209)
* Fix bug where ws exceptions not being logged (#2213)
* Fix bug where ws exceptions not being logged
* Fix t\est
* Style: add type hints (#2217)
* style(routes): add_route argument, return typing
* style(listeners): typing
* style(views): typing as_view
* style(routes): change type hint
* style(listeners): change type hint
* style(routes): change type hint
* add some more types
* Change as_view typing
* Add some cleaner type annotations
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Add default messages to SanicExceptions (#2216)
* Add default messages to SanicExceptions
* Cleaner exception message setting
* Copy Blueprints Implementation (#2184)
* Accept header parsing (#2200)
* Add some tests
* docstring
* Add accept matching
* Add some more tests on matching
* Add matching flags for wildcards
* Add mathing controls to accept
* Limit uvicorn 14 in testing
* Add convenience for annotated handlers (#2225)
* Split HttpProtocol parts into base SanicProtocol and HTTPProtocol subclass (#2229)
* Split HttpProtocol parts into base SanicProtocol and HTTPProtocol subclass.
* lint fixes
* re-black server.py
* Move server.py into its own module (#2230)
* Move server.py into its own module
* Change monkeypatch path on test_logging.py
* Blueprint specific exception handlers (#2208)
* Call abort() on sockets after close() to prevent dangling sockets (#2231)
* Add ability to return Falsey but not-None from handlers (#2236)
* Adds Blueprint Group exception decorator (#2238)
* Add exception decorator
* Added tests
* Fix line too long
* Static DIR and FILE resource types (#2244)
* Explicit static directive for serving file or dir
Co-authored-by: anbuhckr <36891836+anbuhckr@users.noreply.github.com>
Co-authored-by: anbuhckr <miki.suhendra@gmail.com>
* Close HTTP loop when connection task cancelled (#2245)
* Terminate loop when no transport exists
* Add log when closing HTTP loop because of shutdown
* Add unit test
* New websockets (#2158)
* First attempt at new Websockets implementation based on websockets >= 9.0, with sans-i/o features. Requires more work.
* Update sanic/websocket.py
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Update sanic/websocket.py
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Update sanic/websocket.py
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* wip, update websockets code to new Sans/IO API
* Refactored new websockets impl into own modules
Incorporated other suggestions made by team
* Another round of work on the new websockets impl
* Added websocket_timeout support (matching previous/legacy support)
* Lots more comments
* Incorporated suggested changes from previous round of review
* Changed RuntimeError usage to ServerError
* Changed SanicException usage to ServerError
* Removed some redundant asserts
* Change remaining asserts to ServerErrors
* Fixed some timeout handling issues
* Fixed websocket.close() handling, and made it more robust
* Made auto_close task smarter and more error-resilient
* Made fail_connection routine smarter and more error-resilient
* Further new websockets impl fixes
* Update compatibility with Websockets v10
* Track server connection state in a more precise way
* Try to handle the shutdown process more gracefully
* Add a new end_connection() helper, to use as an alterative to close() or fail_connection()
* Kill the auto-close task and keepalive-timeout task when sanic is shutdown
* Deprecate WEBSOCKET_READ_LIMIT and WEBSOCKET_WRITE_LIMIT configs, they are not used in this implementation.
* Change a warning message to debug level
Remove default values for deprecated websocket parameters
* Fix flake8 errors
* Fix a couple of missed failing tests
* remove websocket bench from examples
* Integrate suggestions from code reviews
Use Optional[T] instead of union[T,None]
Fix mypy type logic errors
change "is not None" to truthy checks where appropriate
change "is None" to falsy checks were appropriate
Add more debug logging when debug mode is on
Change to using sanic.logger for debug logging rather than error_logger.
* Fix long line lengths of debug messages
Add some new debug messages when websocket IO is paused and unpaused for flow control
Fix websocket example to use app.static()
* remove unused import in websocket example app
* re-run isort after Flake8 fixes
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
* Account for BP with exception handler but no routes (#2246)
* Don't log "enabled" if auto-reload disabled (#2247)
Fixes #2240
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
* Smarter auto fallback (#2162)
* Smarter auto fallback
* remove config from blueprints
* Add tests for error formatting
* Add check for proper format
* Fix some tests
* Add some tests
* docstring
* Add accept matching
* Add some more tests on matching
* Fix contains bug, earlier return on MediaType eq
* Add matching flags for wildcards
* Add mathing controls to accept
* Cleanup dev cruft
* Add cleanup and resolve OSError relating to test implementation
* Fix test
* Fix some typos
* Some fixes to the new Websockets impl (#2248)
* First attempt at new Websockets implementation based on websockets >= 9.0, with sans-i/o features. Requires more work.
* Update sanic/websocket.py
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Update sanic/websocket.py
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Update sanic/websocket.py
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* wip, update websockets code to new Sans/IO API
* Refactored new websockets impl into own modules
Incorporated other suggestions made by team
* Another round of work on the new websockets impl
* Added websocket_timeout support (matching previous/legacy support)
* Lots more comments
* Incorporated suggested changes from previous round of review
* Changed RuntimeError usage to ServerError
* Changed SanicException usage to ServerError
* Removed some redundant asserts
* Change remaining asserts to ServerErrors
* Fixed some timeout handling issues
* Fixed websocket.close() handling, and made it more robust
* Made auto_close task smarter and more error-resilient
* Made fail_connection routine smarter and more error-resilient
* Further new websockets impl fixes
* Update compatibility with Websockets v10
* Track server connection state in a more precise way
* Try to handle the shutdown process more gracefully
* Add a new end_connection() helper, to use as an alterative to close() or fail_connection()
* Kill the auto-close task and keepalive-timeout task when sanic is shutdown
* Deprecate WEBSOCKET_READ_LIMIT and WEBSOCKET_WRITE_LIMIT configs, they are not used in this implementation.
* Change a warning message to debug level
Remove default values for deprecated websocket parameters
* Fix flake8 errors
* Fix a couple of missed failing tests
* remove websocket bench from examples
* Integrate suggestions from code reviews
Use Optional[T] instead of union[T,None]
Fix mypy type logic errors
change "is not None" to truthy checks where appropriate
change "is None" to falsy checks were appropriate
Add more debug logging when debug mode is on
Change to using sanic.logger for debug logging rather than error_logger.
* Fix long line lengths of debug messages
Add some new debug messages when websocket IO is paused and unpaused for flow control
Fix websocket example to use app.static()
* remove unused import in websocket example app
* re-run isort after Flake8 fixes
* Some fixes to the new Websockets impl
Will throw WebsocketClosed exception instead of ServerException now when attempting to read or write to closed websocket, this makes it easier to catch
The various ws.recv() methods now have the ability to raise CancelledError into your websocket handler
Fix a niche close-socket negotiation bug
Fix bug where http protocol thought the websocket never sent any response.
Allow data to still send in some cases after websocket enters CLOSING state.
Fix some badly formatted and badly placed comments
* allow eof_received to send back data too, if the connection is in CLOSING state
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
* 21.9 release docs (#2218)
* Beging 21.9 release docs
* Add PRs to changelog
* Change deprecation version
* Update logging tests
* Bump version
* Update changelog
* Change dev install command (#2251)
Co-authored-by: Zhiwei <zhi.wei.liang@outlook.com>
Co-authored-by: L. Kärkkäinen <98187+Tronic@users.noreply.github.com>
Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Robert Palmer <robd003@users.noreply.github.com>
Co-authored-by: Ryu JuHeon <saidbysolo@gmail.com>
Co-authored-by: gluhar2006 <49654448+gluhar2006@users.noreply.github.com>
Co-authored-by: n.feofanov <n.feofanov@visionlabs.ru>
Co-authored-by: Néstor Pérez <25409753+prryplatypus@users.noreply.github.com>
Co-authored-by: Can Sarigol <56863826+cansarigol3megawatt@users.noreply.github.com>
Co-authored-by: Zhiwei <chihwei.public@outlook.com>
Co-authored-by: YongChan Cho <h3236516@gmail.com>
Co-authored-by: Zhiwei <zhiwei@sinatra.ai>
Co-authored-by: Ashley Sommer <ashleysommer@gmail.com>
Co-authored-by: anbuhckr <36891836+anbuhckr@users.noreply.github.com>
Co-authored-by: anbuhckr <miki.suhendra@gmail.com>
2021-10-02 19:55:23 +01:00
|
|
|
def test_handle_is_a_directory_error(app, static_file_directory):
|
|
|
|
error_text = "Is a directory. Access denied"
|
|
|
|
app.static("/static", static_file_directory)
|
|
|
|
|
|
|
|
@app.exception(Exception)
|
|
|
|
async def handleStaticDirError(request, exception):
|
|
|
|
if isinstance(exception, IsADirectoryError):
|
|
|
|
return text(error_text, status=403)
|
|
|
|
raise exception
|
|
|
|
|
|
|
|
request, response = app.test_client.get("/static/")
|
|
|
|
|
|
|
|
assert response.status == 403
|
|
|
|
assert response.text == error_text
|
|
|
|
|
|
|
|
|
2021-04-06 20:20:25 +01:00
|
|
|
def test_stack_trace_on_not_found(app, static_file_directory, caplog):
|
|
|
|
app.static("/static", static_file_directory)
|
|
|
|
|
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
_, response = app.test_client.get("/static/non_existing_file.file")
|
|
|
|
|
|
|
|
counter = Counter([r[1] for r in caplog.record_tuples])
|
|
|
|
|
|
|
|
assert response.status == 404
|
|
|
|
assert counter[logging.INFO] == 5
|
2021-07-11 22:35:27 +01:00
|
|
|
assert counter[logging.ERROR] == 0
|
2021-04-06 20:20:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_no_stack_trace_on_not_found(app, static_file_directory, caplog):
|
|
|
|
app.static("/static", static_file_directory)
|
|
|
|
|
|
|
|
@app.exception(FileNotFound)
|
|
|
|
async def file_not_found(request, exception):
|
|
|
|
return text(f"No file: {request.path}", status=404)
|
|
|
|
|
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
_, response = app.test_client.get("/static/non_existing_file.file")
|
|
|
|
|
|
|
|
counter = Counter([r[1] for r in caplog.record_tuples])
|
|
|
|
|
|
|
|
assert response.status == 404
|
|
|
|
assert counter[logging.INFO] == 5
|
|
|
|
assert logging.ERROR not in counter
|
|
|
|
assert response.text == "No file: /static/non_existing_file.file"
|
2021-04-19 22:53:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_multiple_statics(app, static_file_directory):
|
|
|
|
app.static("/file", get_file_path(static_file_directory, "test.file"))
|
|
|
|
app.static("/png", get_file_path(static_file_directory, "python.png"))
|
|
|
|
|
|
|
|
_, response = app.test_client.get("/file")
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(
|
|
|
|
static_file_directory, "test.file"
|
|
|
|
)
|
|
|
|
|
|
|
|
_, response = app.test_client.get("/png")
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(
|
|
|
|
static_file_directory, "python.png"
|
|
|
|
)
|
Merge in main to current-release (#2254)
* Remove unnecessary import in test_constants.py, which also fixes an error on win (#2180)
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
* Manually reset the buffer when streaming request body (#2183)
* Remove Duplicated Dependencies and PEP 517 Support (#2173)
* Remove duplicated dependencies
* Specify setuptools as the tool for generating distribution (PEP 517)
* Add `isort` to `dev_require`
* manage all dependencies in setup.py
* Execute `make pretty`
* Set usedevelop to true (revert previous change)
* Fix the handling of the end of a chunked request. (#2188)
* Fix the handling of the end of a chunked request.
* Avoid hardcoding final chunk header size.
* Add some unit tests for pipeline body reading
* Decode bytes for json serialization
Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Resolve regressions in exceptions (#2181)
* Update sanic-routing to fix path issues plus lookahead / lookbehind support (#2178)
* Update sanic-routing to fix path issues plus lookahead / lookbehind support
* Update setup.py
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
* style(app,blueprints): add some type hints (#2196)
* style(app,blueprints): add some type hints
* style(app): option is Any
* style(blueprints): url prefix default value is ``""``
* style(app): backward compatible
* style(app): backward compatible
* style(blueprints): defult is None
* style(app): apply code style (black)
* Update some CC config (#2199)
* Update README.rst
* raise exception for `_static_request_handler` unknown exception; add test with custom error (#2195)
Co-authored-by: n.feofanov <n.feofanov@visionlabs.ru>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
* Change dumps to AnyStr (#2193)
* HTTP tests (#2194)
* Fix issues with after request handling in HTTP pipelining (#2201)
* Clean up after a request is complete, before the next pipelined request.
* Limit the size of request body consumed after handler has finished.
* Linter error.
* Add unit test re: bad headers
Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Update CHANGELOG
* Log remote address if available (#2207)
* Log remote address if available
* Add tests
* Fix testing version
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Fixed for handling exceptions of asgi app call. (#2211)
@cansarigol3megawatt Thanks for looking into this and getting the quick turnaround on this. I will :cherries: pick this into the 21.6 branch and get it out a little later tonight.
* Signals Integration (#2160)
* Update some tests
* Resolve #2122 route decorator returning tuple
* Use rc sanic-routing version
* Update unit tests to <:str>
* Minimal working version with some signals implemented
* Add more http signals
* Update ASGI and change listeners to signals
* Allow for dynamic ODE signals
* Allow signals to be stacked
* Begin tests
* Prioritize match_info on keyword argument injection
* WIP on tests
* Compat with signals
* Work through some test coverage
* Passing tests
* Post linting
* Setup proper resets
* coverage reporting
* Fixes from vltr comments
* clear delayed tasks
* Fix bad test
* rm pycache
* uncomment windows tests (#2214)
* Add convenience methods to BP groups (#2209)
* Fix bug where ws exceptions not being logged (#2213)
* Fix bug where ws exceptions not being logged
* Fix t\est
* Style: add type hints (#2217)
* style(routes): add_route argument, return typing
* style(listeners): typing
* style(views): typing as_view
* style(routes): change type hint
* style(listeners): change type hint
* style(routes): change type hint
* add some more types
* Change as_view typing
* Add some cleaner type annotations
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Add default messages to SanicExceptions (#2216)
* Add default messages to SanicExceptions
* Cleaner exception message setting
* Copy Blueprints Implementation (#2184)
* Accept header parsing (#2200)
* Add some tests
* docstring
* Add accept matching
* Add some more tests on matching
* Add matching flags for wildcards
* Add mathing controls to accept
* Limit uvicorn 14 in testing
* Add convenience for annotated handlers (#2225)
* Split HttpProtocol parts into base SanicProtocol and HTTPProtocol subclass (#2229)
* Split HttpProtocol parts into base SanicProtocol and HTTPProtocol subclass.
* lint fixes
* re-black server.py
* Move server.py into its own module (#2230)
* Move server.py into its own module
* Change monkeypatch path on test_logging.py
* Blueprint specific exception handlers (#2208)
* Call abort() on sockets after close() to prevent dangling sockets (#2231)
* Add ability to return Falsey but not-None from handlers (#2236)
* Adds Blueprint Group exception decorator (#2238)
* Add exception decorator
* Added tests
* Fix line too long
* Static DIR and FILE resource types (#2244)
* Explicit static directive for serving file or dir
Co-authored-by: anbuhckr <36891836+anbuhckr@users.noreply.github.com>
Co-authored-by: anbuhckr <miki.suhendra@gmail.com>
* Close HTTP loop when connection task cancelled (#2245)
* Terminate loop when no transport exists
* Add log when closing HTTP loop because of shutdown
* Add unit test
* New websockets (#2158)
* First attempt at new Websockets implementation based on websockets >= 9.0, with sans-i/o features. Requires more work.
* Update sanic/websocket.py
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Update sanic/websocket.py
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Update sanic/websocket.py
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* wip, update websockets code to new Sans/IO API
* Refactored new websockets impl into own modules
Incorporated other suggestions made by team
* Another round of work on the new websockets impl
* Added websocket_timeout support (matching previous/legacy support)
* Lots more comments
* Incorporated suggested changes from previous round of review
* Changed RuntimeError usage to ServerError
* Changed SanicException usage to ServerError
* Removed some redundant asserts
* Change remaining asserts to ServerErrors
* Fixed some timeout handling issues
* Fixed websocket.close() handling, and made it more robust
* Made auto_close task smarter and more error-resilient
* Made fail_connection routine smarter and more error-resilient
* Further new websockets impl fixes
* Update compatibility with Websockets v10
* Track server connection state in a more precise way
* Try to handle the shutdown process more gracefully
* Add a new end_connection() helper, to use as an alterative to close() or fail_connection()
* Kill the auto-close task and keepalive-timeout task when sanic is shutdown
* Deprecate WEBSOCKET_READ_LIMIT and WEBSOCKET_WRITE_LIMIT configs, they are not used in this implementation.
* Change a warning message to debug level
Remove default values for deprecated websocket parameters
* Fix flake8 errors
* Fix a couple of missed failing tests
* remove websocket bench from examples
* Integrate suggestions from code reviews
Use Optional[T] instead of union[T,None]
Fix mypy type logic errors
change "is not None" to truthy checks where appropriate
change "is None" to falsy checks were appropriate
Add more debug logging when debug mode is on
Change to using sanic.logger for debug logging rather than error_logger.
* Fix long line lengths of debug messages
Add some new debug messages when websocket IO is paused and unpaused for flow control
Fix websocket example to use app.static()
* remove unused import in websocket example app
* re-run isort after Flake8 fixes
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
* Account for BP with exception handler but no routes (#2246)
* Don't log "enabled" if auto-reload disabled (#2247)
Fixes #2240
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
* Smarter auto fallback (#2162)
* Smarter auto fallback
* remove config from blueprints
* Add tests for error formatting
* Add check for proper format
* Fix some tests
* Add some tests
* docstring
* Add accept matching
* Add some more tests on matching
* Fix contains bug, earlier return on MediaType eq
* Add matching flags for wildcards
* Add mathing controls to accept
* Cleanup dev cruft
* Add cleanup and resolve OSError relating to test implementation
* Fix test
* Fix some typos
* Some fixes to the new Websockets impl (#2248)
* First attempt at new Websockets implementation based on websockets >= 9.0, with sans-i/o features. Requires more work.
* Update sanic/websocket.py
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Update sanic/websocket.py
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* Update sanic/websocket.py
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
* wip, update websockets code to new Sans/IO API
* Refactored new websockets impl into own modules
Incorporated other suggestions made by team
* Another round of work on the new websockets impl
* Added websocket_timeout support (matching previous/legacy support)
* Lots more comments
* Incorporated suggested changes from previous round of review
* Changed RuntimeError usage to ServerError
* Changed SanicException usage to ServerError
* Removed some redundant asserts
* Change remaining asserts to ServerErrors
* Fixed some timeout handling issues
* Fixed websocket.close() handling, and made it more robust
* Made auto_close task smarter and more error-resilient
* Made fail_connection routine smarter and more error-resilient
* Further new websockets impl fixes
* Update compatibility with Websockets v10
* Track server connection state in a more precise way
* Try to handle the shutdown process more gracefully
* Add a new end_connection() helper, to use as an alterative to close() or fail_connection()
* Kill the auto-close task and keepalive-timeout task when sanic is shutdown
* Deprecate WEBSOCKET_READ_LIMIT and WEBSOCKET_WRITE_LIMIT configs, they are not used in this implementation.
* Change a warning message to debug level
Remove default values for deprecated websocket parameters
* Fix flake8 errors
* Fix a couple of missed failing tests
* remove websocket bench from examples
* Integrate suggestions from code reviews
Use Optional[T] instead of union[T,None]
Fix mypy type logic errors
change "is not None" to truthy checks where appropriate
change "is None" to falsy checks were appropriate
Add more debug logging when debug mode is on
Change to using sanic.logger for debug logging rather than error_logger.
* Fix long line lengths of debug messages
Add some new debug messages when websocket IO is paused and unpaused for flow control
Fix websocket example to use app.static()
* remove unused import in websocket example app
* re-run isort after Flake8 fixes
* Some fixes to the new Websockets impl
Will throw WebsocketClosed exception instead of ServerException now when attempting to read or write to closed websocket, this makes it easier to catch
The various ws.recv() methods now have the ability to raise CancelledError into your websocket handler
Fix a niche close-socket negotiation bug
Fix bug where http protocol thought the websocket never sent any response.
Allow data to still send in some cases after websocket enters CLOSING state.
Fix some badly formatted and badly placed comments
* allow eof_received to send back data too, if the connection is in CLOSING state
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
* 21.9 release docs (#2218)
* Beging 21.9 release docs
* Add PRs to changelog
* Change deprecation version
* Update logging tests
* Bump version
* Update changelog
* Change dev install command (#2251)
Co-authored-by: Zhiwei <zhi.wei.liang@outlook.com>
Co-authored-by: L. Kärkkäinen <98187+Tronic@users.noreply.github.com>
Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Robert Palmer <robd003@users.noreply.github.com>
Co-authored-by: Ryu JuHeon <saidbysolo@gmail.com>
Co-authored-by: gluhar2006 <49654448+gluhar2006@users.noreply.github.com>
Co-authored-by: n.feofanov <n.feofanov@visionlabs.ru>
Co-authored-by: Néstor Pérez <25409753+prryplatypus@users.noreply.github.com>
Co-authored-by: Can Sarigol <56863826+cansarigol3megawatt@users.noreply.github.com>
Co-authored-by: Zhiwei <chihwei.public@outlook.com>
Co-authored-by: YongChan Cho <h3236516@gmail.com>
Co-authored-by: Zhiwei <zhiwei@sinatra.ai>
Co-authored-by: Ashley Sommer <ashleysommer@gmail.com>
Co-authored-by: anbuhckr <36891836+anbuhckr@users.noreply.github.com>
Co-authored-by: anbuhckr <miki.suhendra@gmail.com>
2021-10-02 19:55:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_resource_type_default(app, static_file_directory):
|
|
|
|
app.static("/static", static_file_directory)
|
|
|
|
app.static("/file", get_file_path(static_file_directory, "test.file"))
|
|
|
|
|
|
|
|
_, response = app.test_client.get("/static")
|
|
|
|
assert response.status == 404
|
|
|
|
|
|
|
|
_, response = app.test_client.get("/file")
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(
|
|
|
|
static_file_directory, "test.file"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_resource_type_file(app, static_file_directory):
|
|
|
|
app.static(
|
|
|
|
"/file",
|
|
|
|
get_file_path(static_file_directory, "test.file"),
|
|
|
|
resource_type="file",
|
|
|
|
)
|
|
|
|
|
|
|
|
_, response = app.test_client.get("/file")
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(
|
|
|
|
static_file_directory, "test.file"
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
app.static("/static", static_file_directory, resource_type="file")
|
|
|
|
|
|
|
|
|
|
|
|
def test_resource_type_dir(app, static_file_directory):
|
|
|
|
app.static("/static", static_file_directory, resource_type="dir")
|
|
|
|
|
|
|
|
_, response = app.test_client.get("/static/test.file")
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(
|
|
|
|
static_file_directory, "test.file"
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
app.static(
|
|
|
|
"/file",
|
|
|
|
get_file_path(static_file_directory, "test.file"),
|
|
|
|
resource_type="dir",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_resource_type_unknown(app, static_file_directory, caplog):
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
app.static("/static", static_file_directory, resource_type="unknown")
|