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
|
|
|
|
|
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-03-23 07:22:00 +00:00
|
|
|
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt', 'python.png'])
|
2017-02-01 15:00:57 +00:00
|
|
|
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
|
|
|
|
2017-02-14 19:51:20 +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
|
|
|
|
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-12-25 02:16:19 +00:00
|
|
|
|
|
|
|
app = Sanic('test_static')
|
2017-02-01 15:00:57 +00:00
|
|
|
app.static(base_uri, static_file_directory)
|
2016-12-25 02:16:19 +00:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get(
|
|
|
|
uri='{}/{}'.format(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
|
|
|
|
2017-02-09 01:37:32 +00:00
|
|
|
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
|
2017-02-09 01:59:34 +00:00
|
|
|
def test_static_head_request(file_name, static_file_directory):
|
2016-10-24 09:21:06 +01:00
|
|
|
app = Sanic('test_static')
|
2017-02-09 01:37:32 +00:00
|
|
|
app.static(
|
2017-02-09 01:59:34 +00:00
|
|
|
'/testing.file', get_file_path(static_file_directory, file_name),
|
2017-02-09 01:37:32 +00:00
|
|
|
use_content_range=True)
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.head('/testing.file')
|
2017-01-31 01:04:51 +00:00
|
|
|
assert response.status == 200
|
|
|
|
assert 'Accept-Ranges' in response.headers
|
|
|
|
assert 'Content-Length' in response.headers
|
2017-02-09 01:59:34 +00:00
|
|
|
assert int(response.headers[
|
|
|
|
'Content-Length']) == len(
|
|
|
|
get_file_content(static_file_directory, file_name))
|
|
|
|
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2017-02-09 01:37:32 +00:00
|
|
|
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
|
2017-02-09 01:59:34 +00:00
|
|
|
def test_static_content_range_correct(file_name, static_file_directory):
|
2017-01-31 01:04:51 +00:00
|
|
|
app = Sanic('test_static')
|
2017-02-09 01:37:32 +00:00
|
|
|
app.static(
|
2017-02-09 01:59:34 +00:00
|
|
|
'/testing.file', get_file_path(static_file_directory, file_name),
|
2017-02-09 01:37:32 +00:00
|
|
|
use_content_range=True)
|
2017-01-31 01:04:51 +00:00
|
|
|
|
|
|
|
headers = {
|
|
|
|
'Range': 'bytes=12-19'
|
|
|
|
}
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/testing.file', headers=headers)
|
2017-01-31 01:04:51 +00:00
|
|
|
assert response.status == 200
|
|
|
|
assert 'Content-Length' in response.headers
|
|
|
|
assert 'Content-Range' in response.headers
|
2017-02-09 01:59:34 +00:00
|
|
|
static_content = bytes(get_file_content(
|
|
|
|
static_file_directory, file_name))[12:19]
|
|
|
|
assert int(response.headers[
|
|
|
|
'Content-Length']) == len(static_content)
|
|
|
|
assert response.body == static_content
|
|
|
|
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2017-02-09 01:37:32 +00:00
|
|
|
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
|
2017-02-09 01:59:34 +00:00
|
|
|
def test_static_content_range_front(file_name, static_file_directory):
|
2017-01-31 01:04:51 +00:00
|
|
|
app = Sanic('test_static')
|
2017-02-09 01:37:32 +00:00
|
|
|
app.static(
|
2017-02-09 01:59:34 +00:00
|
|
|
'/testing.file', get_file_path(static_file_directory, file_name),
|
2017-02-09 01:37:32 +00:00
|
|
|
use_content_range=True)
|
2017-01-31 01:04:51 +00:00
|
|
|
|
|
|
|
headers = {
|
|
|
|
'Range': 'bytes=12-'
|
|
|
|
}
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/testing.file', headers=headers)
|
2017-01-31 01:04:51 +00:00
|
|
|
assert response.status == 200
|
|
|
|
assert 'Content-Length' in response.headers
|
|
|
|
assert 'Content-Range' in response.headers
|
2017-02-09 01:59:34 +00:00
|
|
|
static_content = bytes(get_file_content(
|
|
|
|
static_file_directory, file_name))[12:]
|
|
|
|
assert int(response.headers[
|
|
|
|
'Content-Length']) == len(static_content)
|
|
|
|
assert response.body == static_content
|
|
|
|
|
2017-02-09 01:37:32 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
|
2017-02-09 01:59:34 +00:00
|
|
|
def test_static_content_range_back(file_name, static_file_directory):
|
2017-01-31 01:04:51 +00:00
|
|
|
app = Sanic('test_static')
|
2017-02-09 01:37:32 +00:00
|
|
|
app.static(
|
2017-02-09 01:59:34 +00:00
|
|
|
'/testing.file', get_file_path(static_file_directory, file_name),
|
2017-02-09 01:37:32 +00:00
|
|
|
use_content_range=True)
|
2017-02-09 01:59:34 +00:00
|
|
|
|
2017-01-31 01:04:51 +00:00
|
|
|
headers = {
|
|
|
|
'Range': 'bytes=-12'
|
|
|
|
}
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/testing.file', headers=headers)
|
2017-01-31 01:04:51 +00:00
|
|
|
assert response.status == 200
|
|
|
|
assert 'Content-Length' in response.headers
|
|
|
|
assert 'Content-Range' in response.headers
|
2017-02-09 01:59:34 +00:00
|
|
|
static_content = bytes(get_file_content(
|
|
|
|
static_file_directory, file_name))[-12:]
|
|
|
|
assert int(response.headers[
|
|
|
|
'Content-Length']) == len(static_content)
|
|
|
|
assert response.body == static_content
|
|
|
|
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2017-02-09 01:37:32 +00:00
|
|
|
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
|
2017-02-09 01:59:34 +00:00
|
|
|
def test_static_content_range_empty(file_name, static_file_directory):
|
2017-01-31 01:04:51 +00:00
|
|
|
app = Sanic('test_static')
|
2017-02-09 01:37:32 +00:00
|
|
|
app.static(
|
2017-02-09 01:59:34 +00:00
|
|
|
'/testing.file', get_file_path(static_file_directory, file_name),
|
2017-02-09 01:37:32 +00:00
|
|
|
use_content_range=True)
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/testing.file')
|
2017-01-31 01:04:51 +00:00
|
|
|
assert response.status == 200
|
|
|
|
assert 'Content-Length' in response.headers
|
|
|
|
assert 'Content-Range' not in response.headers
|
2017-02-09 01:59:34 +00:00
|
|
|
assert int(response.headers[
|
|
|
|
'Content-Length']) == len(get_file_content(static_file_directory, file_name))
|
|
|
|
assert response.body == bytes(
|
|
|
|
get_file_content(static_file_directory, file_name))
|
|
|
|
|
2017-01-31 01:04:51 +00:00
|
|
|
|
2017-02-09 01:37:32 +00:00
|
|
|
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
|
2017-02-09 01:59:34 +00:00
|
|
|
def test_static_content_range_error(file_name, static_file_directory):
|
2017-01-31 01:04:51 +00:00
|
|
|
app = Sanic('test_static')
|
2017-02-09 01:37:32 +00:00
|
|
|
app.static(
|
2017-02-09 01:59:34 +00:00
|
|
|
'/testing.file', get_file_path(static_file_directory, file_name),
|
2017-02-09 01:37:32 +00:00
|
|
|
use_content_range=True)
|
2017-01-31 01:04:51 +00:00
|
|
|
|
|
|
|
headers = {
|
|
|
|
'Range': 'bytes=1-0'
|
|
|
|
}
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/testing.file', headers=headers)
|
2017-01-31 01:04:51 +00:00
|
|
|
assert response.status == 416
|
|
|
|
assert 'Content-Length' in response.headers
|
|
|
|
assert 'Content-Range' in response.headers
|
|
|
|
assert response.headers['Content-Range'] == "bytes */%s" % (
|
2017-02-09 01:59:34 +00:00
|
|
|
len(get_file_content(static_file_directory, file_name)),)
|
2017-09-27 09:24:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt', 'python.png'])
|
|
|
|
def test_static_file(static_file_directory, file_name):
|
|
|
|
app = Sanic('test_static')
|
|
|
|
app.static(
|
|
|
|
'/testing.file',
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
host="www.example.com"
|
|
|
|
)
|
|
|
|
|
|
|
|
headers = {"Host": "www.example.com"}
|
|
|
|
request, response = app.test_client.get('/testing.file', headers=headers)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
|
|
|
request, response = app.test_client.get('/testing.file')
|
|
|
|
assert response.status == 404
|