2017-09-06 12:17:52 +01:00
|
|
|
import inspect
|
|
|
|
import os
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2021-02-07 09:38:37 +00:00
|
|
|
from sanic import Sanic
|
2017-09-06 12:17:52 +01:00
|
|
|
from sanic.blueprints import Blueprint
|
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2017-09-06 12:17:52 +01:00
|
|
|
def static_file_directory():
|
|
|
|
"""The static directory to serve"""
|
|
|
|
current_file = inspect.getfile(inspect.currentframe())
|
|
|
|
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")
|
2017-09-06 12:17:52 +01:00
|
|
|
return static_directory
|
|
|
|
|
|
|
|
|
|
|
|
def get_file_path(static_file_directory, file_name):
|
|
|
|
return os.path.join(static_file_directory, file_name)
|
|
|
|
|
|
|
|
|
|
|
|
def get_file_content(static_file_directory, file_name):
|
|
|
|
"""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:
|
2017-09-06 12:17:52 +01:00
|
|
|
return file.read()
|
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize(
|
2021-02-07 09:38:37 +00:00
|
|
|
"file_name",
|
|
|
|
[
|
|
|
|
"test.file",
|
|
|
|
"decode me.txt",
|
|
|
|
"python.png",
|
|
|
|
],
|
2018-12-30 11:18:06 +00:00
|
|
|
)
|
2021-02-07 09:38:37 +00:00
|
|
|
def test_static_file(static_file_directory, file_name):
|
|
|
|
app = Sanic("qq")
|
2017-09-06 12:17:52 +01:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file", get_file_path(static_file_directory, file_name)
|
|
|
|
)
|
2017-09-06 12:17:52 +01:00
|
|
|
app.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing2.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
name="testing_file",
|
|
|
|
)
|
2017-09-06 12:17:52 +01:00
|
|
|
|
2021-02-07 09:38:37 +00:00
|
|
|
app.router.finalize()
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for("static")
|
|
|
|
uri2 = app.url_for("static", filename="any")
|
|
|
|
uri3 = app.url_for("static", name="static", filename="any")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert uri == "/testing.file"
|
2017-09-06 12:17:52 +01:00
|
|
|
assert uri == uri2
|
|
|
|
assert uri2 == uri3
|
|
|
|
|
2021-02-07 09:38:37 +00:00
|
|
|
app.router.reset()
|
|
|
|
|
2017-09-06 12:17:52 +01:00
|
|
|
request, response = app.test_client.get(uri)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
|
|
|
|
2021-02-07 09:38:37 +00:00
|
|
|
app.router.reset()
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
bp = Blueprint("test_bp_static", url_prefix="/bp")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
bp.static("/testing.file", get_file_path(static_file_directory, file_name))
|
|
|
|
bp.static(
|
|
|
|
"/testing2.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
name="testing_file",
|
|
|
|
)
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
2021-02-07 09:38:37 +00:00
|
|
|
uris = [
|
|
|
|
app.url_for("static", name="test_bp_static.static"),
|
|
|
|
app.url_for("static", name="test_bp_static.static", filename="any"),
|
|
|
|
app.url_for("test_bp_static.static"),
|
|
|
|
app.url_for("test_bp_static.static", filename="any"),
|
|
|
|
]
|
2017-09-06 12:17:52 +01:00
|
|
|
|
2021-02-07 09:38:37 +00:00
|
|
|
assert all(uri == "/bp/testing.file" for uri in uris)
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get(uri)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
|
|
|
|
|
|
|
# test for other parameters
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for("static", _external=True, _server="http://localhost")
|
|
|
|
assert uri == "http://localhost/testing.file"
|
2017-09-06 12:17:52 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for(
|
|
|
|
"static",
|
|
|
|
name="test_bp_static.static",
|
|
|
|
_external=True,
|
|
|
|
_server="http://localhost",
|
|
|
|
)
|
|
|
|
assert uri == "http://localhost/bp/testing.file"
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
# test for defined name
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for("static", name="testing_file")
|
|
|
|
assert uri == "/testing2.file"
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get(uri)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for("static", name="test_bp_static.testing_file")
|
|
|
|
assert uri == "/bp/testing2.file"
|
|
|
|
assert uri == app.url_for(
|
|
|
|
"static", name="test_bp_static.testing_file", filename="any"
|
|
|
|
)
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get(uri)
|
|
|
|
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"])
|
|
|
|
@pytest.mark.parametrize("base_uri", ["/static", "", "/dir"])
|
2021-02-07 09:38:37 +00:00
|
|
|
def test_static_directory(file_name, base_uri, static_file_directory):
|
|
|
|
app = Sanic("base")
|
|
|
|
|
2017-09-06 12:17:52 +01:00
|
|
|
app.static(base_uri, static_file_directory)
|
2018-12-30 11:18:06 +00:00
|
|
|
base_uri2 = base_uri + "/2"
|
|
|
|
app.static(base_uri2, static_file_directory, name="uploads")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for("static", name="static", filename=file_name)
|
2020-02-25 20:01:13 +00:00
|
|
|
assert uri == f"{base_uri}/{file_name}"
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get(uri)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
uri2 = app.url_for("static", name="static", filename="/" + file_name)
|
|
|
|
uri3 = app.url_for("static", filename=file_name)
|
|
|
|
uri4 = app.url_for("static", filename="/" + file_name)
|
|
|
|
uri5 = app.url_for("static", name="uploads", filename=file_name)
|
|
|
|
uri6 = app.url_for("static", name="uploads", filename="/" + file_name)
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
assert uri == uri2
|
|
|
|
assert uri2 == uri3
|
|
|
|
assert uri3 == uri4
|
|
|
|
|
2020-02-25 20:01:13 +00:00
|
|
|
assert uri5 == f"{base_uri2}/{file_name}"
|
2017-09-06 12:17:52 +01:00
|
|
|
assert uri5 == uri6
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
bp = Blueprint("test_bp_static", url_prefix="/bp")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
bp.static(base_uri, static_file_directory)
|
2018-12-30 11:18:06 +00:00
|
|
|
bp.static(base_uri2, static_file_directory, name="uploads")
|
2021-02-07 09:38:37 +00:00
|
|
|
|
|
|
|
app.router.reset()
|
2017-09-06 12:17:52 +01:00
|
|
|
app.blueprint(bp)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for(
|
|
|
|
"static", name="test_bp_static.static", filename=file_name
|
|
|
|
)
|
|
|
|
uri2 = app.url_for(
|
|
|
|
"static", name="test_bp_static.static", filename="/" + file_name
|
|
|
|
)
|
|
|
|
|
|
|
|
uri4 = app.url_for(
|
|
|
|
"static", name="test_bp_static.uploads", filename=file_name
|
|
|
|
)
|
|
|
|
uri5 = app.url_for(
|
|
|
|
"static", name="test_bp_static.uploads", filename="/" + file_name
|
|
|
|
)
|
|
|
|
|
2020-02-25 20:01:13 +00:00
|
|
|
assert uri == f"/bp{base_uri}/{file_name}"
|
2017-09-06 12:17:52 +01:00
|
|
|
assert uri == uri2
|
|
|
|
|
2020-02-25 20:01:13 +00:00
|
|
|
assert uri4 == f"/bp{base_uri2}/{file_name}"
|
2017-09-06 12:17:52 +01:00
|
|
|
assert uri4 == uri5
|
|
|
|
|
|
|
|
request, response = app.test_client.get(uri)
|
|
|
|
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"])
|
2021-02-07 09:38:37 +00:00
|
|
|
def test_static_head_request(file_name, static_file_directory):
|
|
|
|
app = Sanic("base")
|
2017-09-06 12:17:52 +01: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,
|
|
|
|
)
|
|
|
|
|
|
|
|
bp = Blueprint("test_bp_static", url_prefix="/bp")
|
|
|
|
bp.static(
|
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2017-09-06 12:17:52 +01:00
|
|
|
app.blueprint(bp)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for("static")
|
|
|
|
assert uri == "/testing.file"
|
|
|
|
assert uri == app.url_for("static", name="static")
|
|
|
|
assert uri == app.url_for("static", name="static", filename="any")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.head(uri)
|
|
|
|
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-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
# blueprint
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for("static", name="test_bp_static.static")
|
|
|
|
assert uri == "/bp/testing.file"
|
|
|
|
assert uri == app.url_for(
|
|
|
|
"static", name="test_bp_static.static", filename="any"
|
|
|
|
)
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.head(uri)
|
|
|
|
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-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
2021-02-07 09:38:37 +00:00
|
|
|
def test_static_content_range_correct(file_name, static_file_directory):
|
|
|
|
app = Sanic("base")
|
2017-09-06 12:17:52 +01: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,
|
|
|
|
)
|
|
|
|
|
|
|
|
bp = Blueprint("test_bp_static", url_prefix="/bp")
|
|
|
|
bp.static(
|
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2017-09-06 12:17:52 +01:00
|
|
|
app.blueprint(bp)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
headers = {"Range": "bytes=12-19"}
|
|
|
|
uri = app.url_for("static")
|
|
|
|
assert uri == "/testing.file"
|
|
|
|
assert uri == app.url_for("static", name="static")
|
|
|
|
assert uri == app.url_for("static", name="static", filename="any")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get(uri, 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-09-06 12:17:52 +01:00
|
|
|
assert response.body == static_content
|
|
|
|
|
|
|
|
# blueprint
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for("static", name="test_bp_static.static")
|
|
|
|
assert uri == "/bp/testing.file"
|
|
|
|
assert uri == app.url_for(
|
|
|
|
"static", name="test_bp_static.static", filename="any"
|
|
|
|
)
|
|
|
|
assert uri == app.url_for("test_bp_static.static")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get(uri, 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-09-06 12:17:52 +01:00
|
|
|
assert response.body == static_content
|
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
2021-02-07 09:38:37 +00:00
|
|
|
def test_static_content_range_front(file_name, static_file_directory):
|
|
|
|
app = Sanic("base")
|
2017-09-06 12:17:52 +01: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,
|
|
|
|
)
|
|
|
|
|
|
|
|
bp = Blueprint("test_bp_static", url_prefix="/bp")
|
|
|
|
bp.static(
|
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2017-09-06 12:17:52 +01:00
|
|
|
app.blueprint(bp)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
headers = {"Range": "bytes=12-"}
|
|
|
|
uri = app.url_for("static")
|
|
|
|
assert uri == "/testing.file"
|
|
|
|
assert uri == app.url_for("static", name="static")
|
|
|
|
assert uri == app.url_for("static", name="static", filename="any")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get(uri, 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-09-06 12:17:52 +01:00
|
|
|
assert response.body == static_content
|
|
|
|
|
|
|
|
# blueprint
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for("static", name="test_bp_static.static")
|
|
|
|
assert uri == "/bp/testing.file"
|
|
|
|
assert uri == app.url_for(
|
|
|
|
"static", name="test_bp_static.static", filename="any"
|
|
|
|
)
|
|
|
|
assert uri == app.url_for("test_bp_static.static")
|
|
|
|
assert uri == app.url_for("test_bp_static.static", filename="any")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get(uri, 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-09-06 12:17:52 +01:00
|
|
|
assert response.body == static_content
|
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
2021-02-07 09:38:37 +00:00
|
|
|
def test_static_content_range_back(file_name, static_file_directory):
|
|
|
|
app = Sanic("base")
|
2017-09-06 12:17:52 +01: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,
|
|
|
|
)
|
|
|
|
|
|
|
|
bp = Blueprint("test_bp_static", url_prefix="/bp")
|
|
|
|
bp.static(
|
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2017-09-06 12:17:52 +01:00
|
|
|
app.blueprint(bp)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
headers = {"Range": "bytes=-12"}
|
|
|
|
uri = app.url_for("static")
|
|
|
|
assert uri == "/testing.file"
|
|
|
|
assert uri == app.url_for("static", name="static")
|
|
|
|
assert uri == app.url_for("static", name="static", filename="any")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get(uri, 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-09-06 12:17:52 +01:00
|
|
|
assert response.body == static_content
|
|
|
|
|
|
|
|
# blueprint
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for("static", name="test_bp_static.static")
|
|
|
|
assert uri == "/bp/testing.file"
|
|
|
|
assert uri == app.url_for(
|
|
|
|
"static", name="test_bp_static.static", filename="any"
|
|
|
|
)
|
|
|
|
assert uri == app.url_for("test_bp_static.static")
|
|
|
|
assert uri == app.url_for("test_bp_static.static", filename="any")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get(uri, 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-09-06 12:17:52 +01:00
|
|
|
assert response.body == static_content
|
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
2021-02-07 09:38:37 +00:00
|
|
|
def test_static_content_range_empty(file_name, static_file_directory):
|
|
|
|
app = Sanic("base")
|
2017-09-06 12:17:52 +01: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,
|
|
|
|
)
|
|
|
|
|
|
|
|
bp = Blueprint("test_bp_static", url_prefix="/bp")
|
|
|
|
bp.static(
|
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2017-09-06 12:17:52 +01:00
|
|
|
app.blueprint(bp)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for("static")
|
|
|
|
assert uri == "/testing.file"
|
|
|
|
assert uri == app.url_for("static", name="static")
|
|
|
|
assert uri == app.url_for("static", name="static", filename="any")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get(uri)
|
|
|
|
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-09-06 12:17:52 +01:00
|
|
|
assert response.body == bytes(
|
2018-12-30 11:18:06 +00:00
|
|
|
get_file_content(static_file_directory, file_name)
|
|
|
|
)
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
# blueprint
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for("static", name="test_bp_static.static")
|
|
|
|
assert uri == "/bp/testing.file"
|
|
|
|
assert uri == app.url_for(
|
|
|
|
"static", name="test_bp_static.static", filename="any"
|
|
|
|
)
|
|
|
|
assert uri == app.url_for("test_bp_static.static")
|
|
|
|
assert uri == app.url_for("test_bp_static.static", filename="any")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get(uri)
|
|
|
|
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-09-06 12:17:52 +01:00
|
|
|
assert response.body == bytes(
|
2018-12-30 11:18:06 +00:00
|
|
|
get_file_content(static_file_directory, file_name)
|
|
|
|
)
|
2017-09-06 12:17:52 +01: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):
|
2021-02-07 09:38:37 +00:00
|
|
|
app = Sanic("base")
|
2017-09-06 12:17:52 +01: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,
|
|
|
|
)
|
|
|
|
|
|
|
|
bp = Blueprint("test_bp_static", url_prefix="/bp")
|
|
|
|
bp.static(
|
|
|
|
"/testing.file",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
use_content_range=True,
|
|
|
|
)
|
2017-09-06 12:17:52 +01:00
|
|
|
app.blueprint(bp)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
headers = {"Range": "bytes=1-0"}
|
|
|
|
uri = app.url_for("static")
|
|
|
|
assert uri == "/testing.file"
|
|
|
|
assert uri == app.url_for("static", name="static")
|
|
|
|
assert uri == app.url_for("static", name="static", filename="any")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get(uri, headers=headers)
|
|
|
|
assert response.status == 416
|
2018-12-30 11:18:06 +00:00
|
|
|
assert "Content-Length" in response.headers
|
|
|
|
assert "Content-Range" in response.headers
|
2023-10-25 02:28:52 +01:00
|
|
|
assert response.headers["Content-Range"] == "bytes */{}".format(
|
2018-12-30 11:18:06 +00:00
|
|
|
len(get_file_content(static_file_directory, file_name)),
|
|
|
|
)
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
# blueprint
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for("static", name="test_bp_static.static")
|
|
|
|
assert uri == "/bp/testing.file"
|
|
|
|
assert uri == app.url_for(
|
|
|
|
"static", name="test_bp_static.static", filename="any"
|
|
|
|
)
|
|
|
|
assert uri == app.url_for("test_bp_static.static")
|
|
|
|
assert uri == app.url_for("test_bp_static.static", filename="any")
|
2017-09-06 12:17:52 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get(uri, headers=headers)
|
|
|
|
assert response.status == 416
|
2018-12-30 11:18:06 +00:00
|
|
|
assert "Content-Length" in response.headers
|
|
|
|
assert "Content-Range" in response.headers
|
2023-10-25 02:28:52 +01:00
|
|
|
assert response.headers["Content-Range"] == "bytes */{}".format(
|
2018-12-30 11:18:06 +00:00
|
|
|
len(get_file_content(static_file_directory, file_name)),
|
|
|
|
)
|