Updates static tests to test for issue #374

Adds a test to test for serving a static directory at the root uri '/'
to address concerns found in #374. Also rewrites the tests so that they
are parametrized and do more with less.
This commit is contained in:
Eli Uriegas 2017-02-01 09:00:57 -06:00
parent dece636d54
commit 6a322ba3f8

View File

@ -16,47 +16,35 @@ def static_file_directory():
return static_directory return static_directory
@pytest.fixture(scope='module') def get_file_path(static_file_directory, file_name):
def static_file_path(static_file_directory): return os.path.join(static_file_directory, file_name)
"""The path to the static file that we want to serve"""
return os.path.join(static_file_directory, 'test.file')
@pytest.fixture(scope='module') def get_file_content(static_file_directory, file_name):
def static_file_content(static_file_path):
"""The content of the static file to check""" """The content of the static file to check"""
with open(static_file_path, 'rb') as file: with open(get_file_path(static_file_directory, file_name), 'rb') as file:
return file.read() return file.read()
def test_static_file(static_file_path, static_file_content): @pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
def test_static_file(static_file_directory, file_name):
app = Sanic('test_static') app = Sanic('test_static')
app.static('/testing.file', static_file_path) app.static(
'/testing.file', get_file_path(static_file_directory, file_name))
request, response = sanic_endpoint_test(app, uri='/testing.file') request, response = sanic_endpoint_test(app, uri='/testing.file')
assert response.status == 200 assert response.status == 200
assert response.body == static_file_content assert response.body == get_file_content(static_file_directory, file_name)
def test_static_directory( @pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
static_file_directory, static_file_path, static_file_content): @pytest.mark.parametrize('base_uri', ['/static', '', '/dir'])
def test_static_directory(file_name, base_uri, static_file_directory):
app = Sanic('test_static') app = Sanic('test_static')
app.static('/dir', static_file_directory) app.static(base_uri, static_file_directory)
request, response = sanic_endpoint_test(app, uri='/dir/test.file') request, response = sanic_endpoint_test(
app, uri='{}/{}'.format(base_uri, file_name))
assert response.status == 200 assert response.status == 200
assert response.body == static_file_content assert response.body == get_file_content(static_file_directory, file_name)
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()
app = Sanic('test_static')
app.static('/dir', static_file_directory)
request, response = sanic_endpoint_test(app, uri='/dir/decode me.txt')
assert response.status == 200
assert response.body == decode_me_contents