Add a check for request body functions and a test for NotImplementedError.
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
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):
|
||||
@@ -13,6 +20,14 @@ class CustomRequest(Request):
|
||||
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():
|
||||
app = Sanic(request_class=CustomRequest)
|
||||
|
||||
Reference in New Issue
Block a user