From 31ad850e37964332763fd98b2f3f071873ae82a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kyle=20Bl=C3=B6m?= Date: Thu, 26 Jan 2017 18:38:32 -0800 Subject: [PATCH] added Range request test cases --- tests/test_static.py | 56 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/test_static.py b/tests/test_static.py index 82b0d1f9..5f9d9e09 100644 --- a/tests/test_static.py +++ b/tests/test_static.py @@ -60,3 +60,59 @@ 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(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 + assert int(response.headers['Content-Length']) == 19-12 + assert response.body == bytes(static_file_content)[12:19] + + +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),)