Added the tests, code formatting changes, and the Range Request feature.

This commit is contained in:
Kyle Blöm
2017-01-30 17:04:51 -08:00
parent 6d18fb6bae
commit d193a1eb70
7 changed files with 335 additions and 126 deletions

View File

@@ -1,11 +1,13 @@
from json import loads as json_loads, dumps as json_dumps
from sanic import Sanic
from sanic.response import json, text, redirect
from sanic.utils import sanic_endpoint_test
from sanic.exceptions import ServerError
import pytest
from sanic import Sanic
from sanic.exceptions import ServerError
from sanic.response import json, text, redirect
from sanic.utils import sanic_endpoint_test
# ------------------------------------------------------------ #
# GET
# ------------------------------------------------------------ #
@@ -112,7 +114,8 @@ def test_query_string():
async def handler(request):
return text('OK')
request, response = sanic_endpoint_test(app, params=[("test1", "1"), ("test2", "false"), ("test2", "true")])
request, response = sanic_endpoint_test(
app, params=[("test1", "1"), ("test2", "false"), ("test2", "true")])
assert request.args.get('test1') == '1'
assert request.args.get('test2') == 'false'
@@ -150,7 +153,8 @@ def test_post_json():
payload = {'test': 'OK'}
headers = {'content-type': 'application/json'}
request, response = sanic_endpoint_test(app, data=json_dumps(payload), headers=headers)
request, response = sanic_endpoint_test(
app, data=json_dumps(payload), headers=headers)
assert request.json.get('test') == 'OK'
assert response.text == 'OK'

View File

@@ -60,3 +60,94 @@ def test_static_url_decode_file(static_file_directory):
request, response = sanic_endpoint_test(app, uri='/dir/decode me.txt')
assert response.status == 200
assert response.body == decode_me_contents
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)
def test_static_content_range_correct(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-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
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
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),)