2016-10-24 09:21:06 +01:00
|
|
|
import inspect
|
|
|
|
import os
|
|
|
|
|
2016-12-25 02:16:19 +00:00
|
|
|
import pytest
|
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
from sanic import Sanic
|
|
|
|
from sanic.utils import sanic_endpoint_test
|
|
|
|
|
2016-12-25 02:16:19 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
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))
|
|
|
|
static_directory = os.path.join(current_directory, 'static')
|
|
|
|
return static_directory
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def static_file_path(static_file_directory):
|
|
|
|
"""The path to the static file that we want to serve"""
|
|
|
|
return os.path.join(static_file_directory, 'test.file')
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2016-12-25 02:16:19 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def static_file_content(static_file_path):
|
|
|
|
"""The content of the static file to check"""
|
|
|
|
with open(static_file_path, 'rb') as file:
|
|
|
|
return file.read()
|
|
|
|
|
|
|
|
|
|
|
|
def test_static_file(static_file_path, static_file_content):
|
2016-10-24 09:21:06 +01:00
|
|
|
app = Sanic('test_static')
|
2016-12-25 02:16:19 +00:00
|
|
|
app.static('/testing.file', static_file_path)
|
2016-10-24 09:21:06 +01:00
|
|
|
|
|
|
|
request, response = sanic_endpoint_test(app, uri='/testing.file')
|
|
|
|
assert response.status == 200
|
2016-12-25 02:16:19 +00:00
|
|
|
assert response.body == static_file_content
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2016-12-25 02:16:19 +00:00
|
|
|
|
|
|
|
def test_static_directory(
|
|
|
|
static_file_directory, static_file_path, static_file_content):
|
|
|
|
|
|
|
|
app = Sanic('test_static')
|
|
|
|
app.static('/dir', static_file_directory)
|
|
|
|
|
|
|
|
request, response = sanic_endpoint_test(app, uri='/dir/test.file')
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == static_file_content
|
|
|
|
|
|
|
|
|
|
|
|
def test_static_url_decode_file(static_file_directory):
|
|
|
|
decode_me_path = os.path.join(static_file_directory, 'decode me.txt')
|
|
|
|
with open(decode_me_path, 'rb') as file:
|
|
|
|
decode_me_contents = file.read()
|
2016-10-24 09:21:06 +01:00
|
|
|
|
|
|
|
app = Sanic('test_static')
|
2016-12-25 02:16:19 +00:00
|
|
|
app.static('/dir', static_file_directory)
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2016-12-25 02:16:19 +00:00
|
|
|
request, response = sanic_endpoint_test(app, uri='/dir/decode me.txt')
|
2016-10-24 09:21:06 +01:00
|
|
|
assert response.status == 200
|
2016-12-25 02:16:19 +00:00
|
|
|
assert response.body == decode_me_contents
|
2017-01-27 02:38:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_static_head_request(static_file_path, static_file_content):
|
|
|
|
app = Sanic('test_static')
|
|
|
|
app.static('/testing.file', static_file_path, use_content_range=True)
|
|
|
|
|
|
|
|
request, response = sanic_endpoint_test(
|
|
|
|
app, uri='/testing.file', method='head')
|
|
|
|
assert response.status == 200
|
|
|
|
assert 'Accept-Ranges' in response.headers
|
|
|
|
assert 'Content-Length' in response.headers
|
|
|
|
assert int(response.headers['Content-Length']) == len(static_file_content)
|
|
|
|
|
|
|
|
|
2017-01-28 19:18:52 +00:00
|
|
|
def test_static_content_range_correct(static_file_path, static_file_content):
|
2017-01-27 02:38:32 +00:00
|
|
|
app = Sanic('test_static')
|
|
|
|
app.static('/testing.file', static_file_path, use_content_range=True)
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
'Range': 'bytes=12-19'
|
|
|
|
}
|
|
|
|
request, response = sanic_endpoint_test(
|
|
|
|
app, uri='/testing.file', headers=headers)
|
|
|
|
assert response.status == 200
|
|
|
|
assert 'Content-Length' in response.headers
|
|
|
|
assert 'Content-Range' in response.headers
|
2017-01-30 17:13:43 +00:00
|
|
|
static_content = bytes(static_file_content)[12:19]
|
|
|
|
assert int(response.headers['Content-Length']) == len(static_content)
|
|
|
|
assert response.body == static_content
|
|
|
|
|
|
|
|
|
|
|
|
def test_static_content_range_front(static_file_path, static_file_content):
|
|
|
|
app = Sanic('test_static')
|
|
|
|
app.static('/testing.file', static_file_path, use_content_range=True)
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
'Range': 'bytes=12-'
|
|
|
|
}
|
|
|
|
request, response = sanic_endpoint_test(
|
|
|
|
app, uri='/testing.file', headers=headers)
|
|
|
|
assert response.status == 200
|
|
|
|
assert 'Content-Length' in response.headers
|
|
|
|
assert 'Content-Range' in response.headers
|
|
|
|
static_content = bytes(static_file_content)[12:]
|
|
|
|
assert int(response.headers['Content-Length']) == len(static_content)
|
|
|
|
assert response.body == static_content
|
|
|
|
|
|
|
|
|
|
|
|
def test_static_content_range_back(static_file_path, static_file_content):
|
|
|
|
app = Sanic('test_static')
|
|
|
|
app.static('/testing.file', static_file_path, use_content_range=True)
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
'Range': 'bytes=-12'
|
|
|
|
}
|
|
|
|
request, response = sanic_endpoint_test(
|
|
|
|
app, uri='/testing.file', headers=headers)
|
|
|
|
assert response.status == 200
|
|
|
|
assert 'Content-Length' in response.headers
|
|
|
|
assert 'Content-Range' in response.headers
|
|
|
|
static_content = bytes(static_file_content)[-12:]
|
|
|
|
assert int(response.headers['Content-Length']) == len(static_content)
|
|
|
|
assert response.body == static_content
|
2017-01-27 02:38:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_static_content_range_empty(static_file_path, static_file_content):
|
|
|
|
app = Sanic('test_static')
|
|
|
|
app.static('/testing.file', static_file_path, use_content_range=True)
|
|
|
|
|
|
|
|
request, response = sanic_endpoint_test(app, uri='/testing.file')
|
|
|
|
assert response.status == 200
|
|
|
|
assert 'Content-Length' in response.headers
|
|
|
|
assert 'Content-Range' not in response.headers
|
|
|
|
assert int(response.headers['Content-Length']) == len(static_file_content)
|
|
|
|
assert response.body == bytes(static_file_content)
|
|
|
|
|
|
|
|
|
|
|
|
def test_static_content_range_error(static_file_path, static_file_content):
|
|
|
|
app = Sanic('test_static')
|
|
|
|
app.static('/testing.file', static_file_path, use_content_range=True)
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
'Range': 'bytes=1-0'
|
|
|
|
}
|
|
|
|
request, response = sanic_endpoint_test(
|
|
|
|
app, uri='/testing.file', headers=headers)
|
|
|
|
assert response.status == 416
|
|
|
|
assert 'Content-Length' in response.headers
|
|
|
|
assert 'Content-Range' in response.headers
|
|
|
|
assert response.headers['Content-Range'] == "bytes */%s" % (
|
|
|
|
len(static_file_content),)
|