Add stream decorator for HTTPMethodView
This commit is contained in:
@@ -2,6 +2,7 @@ from sanic import Sanic
|
||||
from sanic.blueprints import Blueprint
|
||||
from sanic.views import CompositionView
|
||||
from sanic.views import HTTPMethodView
|
||||
from sanic.views import stream as stream_decorator
|
||||
from sanic.response import stream, text
|
||||
|
||||
bp = Blueprint('test_blueprint_request_stream')
|
||||
@@ -13,6 +14,16 @@ class SimpleView(HTTPMethodView):
|
||||
def get(self, request):
|
||||
return text('OK')
|
||||
|
||||
@stream_decorator
|
||||
async def post(self, request):
|
||||
result = ''
|
||||
while True:
|
||||
body = await request.stream.get()
|
||||
if body is None:
|
||||
break
|
||||
result += body.decode('utf-8')
|
||||
return text(result)
|
||||
|
||||
|
||||
@app.stream('/stream')
|
||||
async def handler(request):
|
||||
@@ -31,7 +42,7 @@ async def get(request):
|
||||
|
||||
|
||||
@bp.stream('/bp_stream')
|
||||
async def bp_handler(request):
|
||||
async def bp_stream(request):
|
||||
result = ''
|
||||
while True:
|
||||
body = await request.stream.get()
|
||||
@@ -41,6 +52,11 @@ async def bp_handler(request):
|
||||
return text(result)
|
||||
|
||||
|
||||
@bp.get('/bp_get')
|
||||
async def bp_get(request):
|
||||
return text('OK')
|
||||
|
||||
|
||||
def get_handler(request):
|
||||
return text('OK')
|
||||
|
||||
@@ -73,6 +89,10 @@ def test_request_stream():
|
||||
assert response.status == 200
|
||||
assert response.text == 'OK'
|
||||
|
||||
request, response = app.test_client.post('/method_view', data=data)
|
||||
assert response.status == 200
|
||||
assert response.text == data
|
||||
|
||||
request, response = app.test_client.get('/composition_view')
|
||||
assert response.status == 200
|
||||
assert response.text == 'OK'
|
||||
@@ -89,6 +109,10 @@ def test_request_stream():
|
||||
assert response.status == 200
|
||||
assert response.text == data
|
||||
|
||||
request, response = app.test_client.get('/bp_get')
|
||||
assert response.status == 200
|
||||
assert response.text == 'OK'
|
||||
|
||||
request, response = app.test_client.post('/bp_stream', data=data)
|
||||
assert response.status == 200
|
||||
assert response.text == data
|
||||
|
||||
Reference in New Issue
Block a user