sanic/tests/test_custom_request.py

60 lines
1.6 KiB
Python
Raw Normal View History

from io import BytesIO
import pytest
from sanic import Sanic
from sanic.request import Request
from sanic.response import json_dumps, text
class DeprecCustomRequest(Request):
"""Using old API should fail when receive_body is not implemented"""
def body_push(self, data):
pass
class CustomRequest(Request):
"""Alternative implementation for loading body (non-streaming handlers)"""
async def receive_body(self):
buffer = BytesIO()
async for data in self.stream:
buffer.write(data)
self.body = buffer.getvalue().upper()
buffer.close()
# Old API may be implemented but won't be used here
def body_push(self, data):
assert False
def test_deprecated_custom_request():
with pytest.raises(NotImplementedError):
Sanic(request_class=DeprecCustomRequest)
def test_custom_request():
Fix Ctrl+C and tests on Windows. (#1808) * Fix Ctrl+C on Windows. * Disable testing of a function N/A on Windows. * Add test for coverage, avoid crash on missing _stopping. * Initialise StreamingHTTPResponse.protocol = None * Improved comments. * Reduce amount of data in test_request_stream to avoid failures on Windows. * The Windows test doesn't work on Windows :( * Use port numbers more likely to be free than 8000. * Disable the other signal tests on Windows as well. * Windows doesn't properly support SO_REUSEADDR, so that's disabled in Python, and thus rebinding fails. For successful testing, reuse port instead. * app.run argument handling: added server kwargs (alike create_server), added warning on extra kwargs, made auto_reload explicit argument. Another go at Windows tests * Revert "app.run argument handling: added server kwargs (alike create_server), added warning on extra kwargs, made auto_reload explicit argument. Another go at Windows tests" This reverts commit dc5d682448e3f6595bdca5cb764e5f26ca29e295. * Use random test server port on most tests. Should avoid port/addr reuse issues. * Another test to random port instead of 8000. * Fix deprecation warnings about missing name on Sanic() in tests. * Linter and typing * Increase test coverage * Rewrite test for ctrlc_windows_workaround * py36 compat * py36 compat * py36 compat * Don't rely on loop internals but add a stopping flag to app. * App may be restarted. * py36 compat * Linter * Add a constant for OS checking. Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
2020-03-26 04:42:46 +00:00
app = Sanic(name=__name__, request_class=CustomRequest)
@app.route("/post", methods=["POST"])
async def post_handler(request):
return text("OK")
@app.route("/get")
async def get_handler(request):
return text("OK")
payload = {"test": "OK"}
headers = {"content-type": "application/json"}
request, response = app.test_client.post(
"/post", data=json_dumps(payload), headers=headers
)
assert request.body == b'{"TEST":"OK"}'
assert request.json.get("TEST") == "OK"
assert response.text == "OK"
assert response.status == 200
request, response = app.test_client.get("/get")
assert request.body == b""
assert response.text == "OK"
assert response.status == 200