2017-05-05 12:09:32 +01:00
|
|
|
from sanic import Sanic
|
|
|
|
from sanic.blueprints import Blueprint
|
2017-05-07 10:33:47 +01:00
|
|
|
from sanic.views import CompositionView
|
|
|
|
from sanic.views import HTTPMethodView
|
2017-05-07 13:11:40 +01:00
|
|
|
from sanic.views import stream as stream_decorator
|
2017-05-05 12:09:32 +01:00
|
|
|
from sanic.response import stream, text
|
|
|
|
|
|
|
|
bp = Blueprint('test_blueprint_request_stream')
|
|
|
|
app = Sanic('test_request_stream')
|
|
|
|
|
|
|
|
|
2017-05-07 10:33:47 +01:00
|
|
|
class SimpleView(HTTPMethodView):
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-05-07 13:11:40 +01:00
|
|
|
@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)
|
|
|
|
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2017-05-05 12:09:32 +01:00
|
|
|
@app.stream('/stream')
|
|
|
|
async def handler(request):
|
|
|
|
async def streaming(response):
|
|
|
|
while True:
|
|
|
|
body = await request.stream.get()
|
|
|
|
if body is None:
|
|
|
|
break
|
|
|
|
response.write(body.decode('utf-8'))
|
|
|
|
return stream(streaming)
|
|
|
|
|
|
|
|
|
|
|
|
@app.get('/get')
|
|
|
|
async def get(request):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
|
|
|
|
@bp.stream('/bp_stream')
|
2017-05-07 13:11:40 +01:00
|
|
|
async def bp_stream(request):
|
2017-05-05 12:09:32 +01:00
|
|
|
result = ''
|
|
|
|
while True:
|
|
|
|
body = await request.stream.get()
|
|
|
|
if body is None:
|
|
|
|
break
|
|
|
|
result += body.decode('utf-8')
|
|
|
|
return text(result)
|
|
|
|
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2017-05-07 13:11:40 +01:00
|
|
|
@bp.get('/bp_get')
|
|
|
|
async def bp_get(request):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
|
2017-05-07 10:33:47 +01:00
|
|
|
def get_handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
|
|
|
|
async def post_handler(request):
|
|
|
|
result = ''
|
|
|
|
while True:
|
|
|
|
body = await request.stream.get()
|
|
|
|
if body is None:
|
|
|
|
break
|
|
|
|
result += body.decode('utf-8')
|
|
|
|
return text(result)
|
|
|
|
|
|
|
|
|
|
|
|
app.add_route(SimpleView.as_view(), '/method_view')
|
|
|
|
|
|
|
|
view = CompositionView()
|
|
|
|
view.add(['GET'], get_handler)
|
|
|
|
view.add(['POST'], post_handler, stream=True)
|
|
|
|
|
2017-05-05 12:09:32 +01:00
|
|
|
app.blueprint(bp)
|
|
|
|
|
2017-05-07 10:33:47 +01:00
|
|
|
app.add_route(view, '/composition_view')
|
|
|
|
|
2017-05-05 12:09:32 +01:00
|
|
|
|
|
|
|
def test_request_stream():
|
|
|
|
data = "abc" * 100000
|
2017-05-07 10:33:47 +01:00
|
|
|
|
|
|
|
request, response = app.test_client.get('/method_view')
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'OK'
|
|
|
|
|
2017-05-07 13:11:40 +01:00
|
|
|
request, response = app.test_client.post('/method_view', data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
2017-05-07 10:33:47 +01:00
|
|
|
request, response = app.test_client.get('/composition_view')
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'OK'
|
|
|
|
|
|
|
|
request, response = app.test_client.post('/composition_view', data=data)
|
2017-05-05 12:09:32 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
|
|
|
request, response = app.test_client.get('/get')
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'OK'
|
|
|
|
|
2017-05-07 10:33:47 +01:00
|
|
|
request, response = app.test_client.post('/stream', data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
2017-05-07 13:11:40 +01:00
|
|
|
request, response = app.test_client.get('/bp_get')
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'OK'
|
|
|
|
|
2017-05-05 12:09:32 +01:00
|
|
|
request, response = app.test_client.post('/bp_stream', data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|