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
|
|
|
|
|
|
|
|
|
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"""
|
2017-02-01 15:00:57 +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()
|
|
|
|
|
|
|
|
|
2017-02-01 15:00:57 +00:00
|
|
|
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
|
|
|
|
def test_static_file(static_file_directory, file_name):
|
2016-10-24 09:21:06 +01:00
|
|
|
app = Sanic('test_static')
|
2017-02-01 15:00:57 +00:00
|
|
|
app.static(
|
|
|
|
'/testing.file', get_file_path(static_file_directory, file_name))
|
2016-10-24 09:21:06 +01:00
|
|
|
|
|
|
|
request, response = sanic_endpoint_test(app, uri='/testing.file')
|
|
|
|
assert response.status == 200
|
2017-02-01 15:00:57 +00:00
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
2016-12-25 02:16:19 +00:00
|
|
|
|
|
|
|
|
2017-02-01 15:00:57 +00:00
|
|
|
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
|
|
|
|
@pytest.mark.parametrize('base_uri', ['/static', '', '/dir'])
|
|
|
|
def test_static_directory(file_name, base_uri, static_file_directory):
|
2016-10-24 09:21:06 +01:00
|
|
|
|
|
|
|
app = Sanic('test_static')
|
2017-02-01 15:00:57 +00:00
|
|
|
app.static(base_uri, static_file_directory)
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2017-02-01 15:00:57 +00:00
|
|
|
request, response = sanic_endpoint_test(
|
|
|
|
app, uri='{}/{}'.format(base_uri, file_name))
|
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)
|