Merge pull request #604 from seemethere/add_docker_unittest_support

Fixing the unittests
This commit is contained in:
Raphael Deem 2017-03-29 02:03:54 -07:00 committed by GitHub
commit 171110b445
8 changed files with 50 additions and 47 deletions

6
Dockerfile Normal file
View File

@ -0,0 +1,6 @@
FROM python:3.6
ADD . /app
WORKDIR /app
RUN pip install tox

4
Makefile Normal file
View File

@ -0,0 +1,4 @@
test:
find . -name "*.pyc" -delete
docker build -t sanic/test-image .
docker run -t sanic/test-image tox

View File

@ -1,18 +1,10 @@
aiocache
aiofiles aiofiles
aiohttp aiohttp==1.3.5
beautifulsoup4 beautifulsoup4
bottle
coverage coverage
falcon
gunicorn
httptools httptools
kyoukai flake8
pytest pytest
recommonmark
sphinx
sphinx_rtd_theme
tornado
tox tox
ujson ujson
uvloop uvloop

View File

@ -3,6 +3,7 @@ import types
SANIC_PREFIX = 'SANIC_' SANIC_PREFIX = 'SANIC_'
class Config(dict): class Config(dict):
def __init__(self, defaults=None, load_env=True): def __init__(self, defaults=None, load_env=True):
super().__init__(defaults or {}) super().__init__(defaults or {})

View File

@ -132,8 +132,8 @@ class StreamingHTTPResponse(BaseHTTPResponse):
async def stream( async def stream(
self, version="1.1", keep_alive=False, keep_alive_timeout=None): self, version="1.1", keep_alive=False, keep_alive_timeout=None):
"""Streams headers, runs the `streaming_fn` callback that writes content """Streams headers, runs the `streaming_fn` callback that writes
to the response body, then finalizes the response body. content to the response body, then finalizes the response body.
""" """
headers = self.get_headers( headers = self.get_headers(
version, keep_alive=keep_alive, version, keep_alive=keep_alive,
@ -331,7 +331,11 @@ def stream(
:param headers: Custom Headers. :param headers: Custom Headers.
""" """
return StreamingHTTPResponse( return StreamingHTTPResponse(
streaming_fn, headers=headers, content_type=content_type, status=status) streaming_fn,
headers=headers,
content_type=content_type,
status=status
)
def redirect(to, headers=None, status=302, def redirect(to, headers=None, status=302,

View File

@ -2,48 +2,46 @@ from sanic import Sanic
from sanic.response import text from sanic.response import text
from sanic.exceptions import PayloadTooLarge from sanic.exceptions import PayloadTooLarge
data_received_app = Sanic('data_received')
data_received_app.config.REQUEST_MAX_SIZE = 1
data_received_default_app = Sanic('data_received_default')
data_received_default_app.config.REQUEST_MAX_SIZE = 1
on_header_default_app = Sanic('on_header')
on_header_default_app.config.REQUEST_MAX_SIZE = 500
@data_received_app.route('/1')
async def handler1(request):
return text('OK')
@data_received_app.exception(PayloadTooLarge)
def handler_exception(request, exception):
return text('Payload Too Large from error_handler.', 413)
def test_payload_too_large_from_error_handler(): def test_payload_too_large_from_error_handler():
data_received_app = Sanic('data_received')
data_received_app.config.REQUEST_MAX_SIZE = 1
@data_received_app.route('/1')
async def handler1(request):
return text('OK')
@data_received_app.exception(PayloadTooLarge)
def handler_exception(request, exception):
return text('Payload Too Large from error_handler.', 413)
response = data_received_app.test_client.get('/1', gather_request=False) response = data_received_app.test_client.get('/1', gather_request=False)
assert response.status == 413 assert response.status == 413
assert response.text == 'Payload Too Large from error_handler.' assert response.text == 'Payload Too Large from error_handler.'
@data_received_default_app.route('/1')
async def handler2(request):
return text('OK')
def test_payload_too_large_at_data_received_default(): def test_payload_too_large_at_data_received_default():
data_received_default_app = Sanic('data_received_default')
data_received_default_app.config.REQUEST_MAX_SIZE = 1
@data_received_default_app.route('/1')
async def handler2(request):
return text('OK')
response = data_received_default_app.test_client.get( response = data_received_default_app.test_client.get(
'/1', gather_request=False) '/1', gather_request=False)
assert response.status == 413 assert response.status == 413
assert response.text == 'Error: Payload Too Large' assert response.text == 'Error: Payload Too Large'
@on_header_default_app.route('/1')
async def handler3(request):
return text('OK')
def test_payload_too_large_at_on_header_default(): def test_payload_too_large_at_on_header_default():
on_header_default_app = Sanic('on_header')
on_header_default_app.config.REQUEST_MAX_SIZE = 500
@on_header_default_app.post('/1')
async def handler3(request):
return text('OK')
data = 'a' * 1000 data = 'a' * 1000
response = on_header_default_app.test_client.post( response = on_header_default_app.test_client.post(
'/1', gather_request=False, data=data) '/1', gather_request=False, data=data)

View File

@ -88,4 +88,7 @@ def test_chained_redirect(redirect_app):
assert request.url.endswith('/1') assert request.url.endswith('/1')
assert response.status == 200 assert response.status == 200
assert response.text == 'OK' assert response.text == 'OK'
assert response.url.path.endswith('/3') try:
assert response.url.endswith('/3')
except AttributeError:
assert response.url.path.endswith('/3')

View File

@ -10,12 +10,7 @@ python =
[testenv] [testenv]
deps = deps =
aiofiles -rrequirements-dev.txt
aiohttp
websockets
pytest
beautifulsoup4
coverage
commands = commands =
pytest tests {posargs} pytest tests {posargs}