2017-05-08 15:04:45 +01:00
|
|
|
import asyncio
|
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
|
|
|
|
|
2017-06-06 13:07:32 +01:00
|
|
|
data = 'abc' * 100000
|
2017-05-05 12:09:32 +01:00
|
|
|
|
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
def test_request_stream_method_view():
|
|
|
|
'''for self.is_request_stream = True'''
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
app = Sanic('test_request_stream_method_view')
|
|
|
|
|
|
|
|
class SimpleView(HTTPMethodView):
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
@stream_decorator
|
|
|
|
async def post(self, request):
|
|
|
|
assert isinstance(request.stream, asyncio.Queue)
|
|
|
|
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')
|
|
|
|
|
|
|
|
assert app.is_request_stream is True
|
|
|
|
|
|
|
|
request, response = app.test_client.get('/method_view')
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def test_request_stream_app():
|
2017-05-09 14:31:15 +01:00
|
|
|
'''for self.is_request_stream = True and decorators'''
|
2017-05-08 15:04:45 +01:00
|
|
|
|
|
|
|
app = Sanic('test_request_stream_app')
|
|
|
|
|
2017-06-06 13:07:32 +01:00
|
|
|
contents = []
|
|
|
|
|
|
|
|
@app.post('/cancel_stream', stream=True)
|
|
|
|
async def cancel_stream(request):
|
|
|
|
assert isinstance(request.stream, asyncio.Queue)
|
|
|
|
request.cancel_stream()
|
|
|
|
result = ''
|
|
|
|
while True:
|
|
|
|
body = await request.stream.get()
|
|
|
|
contents.append(body)
|
|
|
|
if body is None:
|
|
|
|
break
|
|
|
|
result += body.decode('utf-8')
|
|
|
|
return text(result)
|
|
|
|
|
2017-05-09 14:31:15 +01:00
|
|
|
@app.get('/get')
|
|
|
|
async def get(request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('GET')
|
|
|
|
|
|
|
|
@app.head('/head')
|
|
|
|
async def head(request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('HEAD')
|
|
|
|
|
|
|
|
@app.delete('/delete')
|
|
|
|
async def delete(request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('DELETE')
|
|
|
|
|
|
|
|
@app.options('/options')
|
|
|
|
async def options(request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('OPTIONS')
|
|
|
|
|
|
|
|
@app.post('/_post/<id>')
|
|
|
|
async def _post(request, id):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('_POST')
|
|
|
|
|
|
|
|
@app.post('/post/<id>', stream=True)
|
|
|
|
async def post(request, id):
|
2017-05-08 15:04:45 +01:00
|
|
|
assert isinstance(request.stream, asyncio.Queue)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-05-09 14:31:15 +01:00
|
|
|
@app.put('/_put')
|
|
|
|
async def _put(request):
|
2017-05-08 15:04:45 +01:00
|
|
|
assert request.stream is None
|
2017-05-09 14:31:15 +01:00
|
|
|
return text('_PUT')
|
|
|
|
|
|
|
|
@app.put('/put', stream=True)
|
|
|
|
async def put(request):
|
|
|
|
assert isinstance(request.stream, asyncio.Queue)
|
|
|
|
|
|
|
|
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.patch('/_patch')
|
|
|
|
async def _patch(request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('_PATCH')
|
|
|
|
|
|
|
|
@app.patch('/patch', stream=True)
|
|
|
|
async def patch(request):
|
|
|
|
assert isinstance(request.stream, asyncio.Queue)
|
|
|
|
|
|
|
|
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)
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
assert app.is_request_stream is True
|
|
|
|
|
2017-06-06 13:07:32 +01:00
|
|
|
assert len(contents) == 0
|
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
request, response = app.test_client.get('/get')
|
|
|
|
assert response.status == 200
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.text == 'GET'
|
|
|
|
|
|
|
|
request, response = app.test_client.head('/head')
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == ''
|
|
|
|
|
|
|
|
request, response = app.test_client.delete('/delete')
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'DELETE'
|
|
|
|
|
|
|
|
request, response = app.test_client.options('/options')
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'OPTIONS'
|
|
|
|
|
|
|
|
request, response = app.test_client.post('/_post/1', data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == '_POST'
|
|
|
|
|
|
|
|
request, response = app.test_client.post('/post/1', data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
|
|
|
request, response = app.test_client.put('/_put', data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == '_PUT'
|
|
|
|
|
|
|
|
request, response = app.test_client.put('/put', data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
|
|
|
request, response = app.test_client.patch('/_patch', data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == '_PATCH'
|
2017-05-08 15:04:45 +01:00
|
|
|
|
2017-05-09 14:31:15 +01:00
|
|
|
request, response = app.test_client.patch('/patch', data=data)
|
2017-05-08 15:04:45 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
|
|
|
|
|
|
|
def test_request_stream_blueprint():
|
|
|
|
'''for self.is_request_stream = True'''
|
|
|
|
|
|
|
|
app = Sanic('test_request_stream_blueprint')
|
|
|
|
bp = Blueprint('test_blueprint_request_stream_blueprint')
|
|
|
|
|
2017-05-09 14:31:15 +01:00
|
|
|
@app.get('/get')
|
|
|
|
async def get(request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('GET')
|
|
|
|
|
|
|
|
@bp.head('/head')
|
|
|
|
async def head(request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('HEAD')
|
|
|
|
|
|
|
|
@bp.delete('/delete')
|
|
|
|
async def delete(request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('DELETE')
|
|
|
|
|
|
|
|
@bp.options('/options')
|
|
|
|
async def options(request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('OPTIONS')
|
|
|
|
|
|
|
|
@bp.post('/_post/<id>')
|
|
|
|
async def _post(request, id):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('_POST')
|
|
|
|
|
|
|
|
@bp.post('/post/<id>', stream=True)
|
|
|
|
async def post(request, id):
|
2017-05-08 15:04:45 +01:00
|
|
|
assert isinstance(request.stream, asyncio.Queue)
|
2017-05-07 13:11:40 +01:00
|
|
|
|
2017-05-09 14:31:15 +01:00
|
|
|
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)
|
|
|
|
|
|
|
|
@bp.put('/_put')
|
|
|
|
async def _put(request):
|
2017-05-08 15:04:45 +01:00
|
|
|
assert request.stream is None
|
2017-05-09 14:31:15 +01:00
|
|
|
return text('_PUT')
|
|
|
|
|
|
|
|
@bp.put('/put', stream=True)
|
|
|
|
async def put(request):
|
|
|
|
assert isinstance(request.stream, asyncio.Queue)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
@bp.patch('/_patch')
|
|
|
|
async def _patch(request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('_PATCH')
|
|
|
|
|
|
|
|
@bp.patch('/patch', stream=True)
|
|
|
|
async def patch(request):
|
|
|
|
assert isinstance(request.stream, asyncio.Queue)
|
|
|
|
|
|
|
|
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)
|
2017-05-08 15:04:45 +01:00
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
|
|
|
assert app.is_request_stream is True
|
|
|
|
|
2017-05-09 14:31:15 +01:00
|
|
|
request, response = app.test_client.get('/get')
|
2017-05-08 15:04:45 +01:00
|
|
|
assert response.status == 200
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.text == 'GET'
|
|
|
|
|
|
|
|
request, response = app.test_client.head('/head')
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == ''
|
|
|
|
|
|
|
|
request, response = app.test_client.delete('/delete')
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'DELETE'
|
|
|
|
|
|
|
|
request, response = app.test_client.options('/options')
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'OPTIONS'
|
|
|
|
|
|
|
|
request, response = app.test_client.post('/_post/1', data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == '_POST'
|
|
|
|
|
|
|
|
request, response = app.test_client.post('/post/1', data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
|
|
|
request, response = app.test_client.put('/_put', data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == '_PUT'
|
|
|
|
|
|
|
|
request, response = app.test_client.put('/put', data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
|
|
|
request, response = app.test_client.patch('/_patch', data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == '_PATCH'
|
2017-05-08 15:04:45 +01:00
|
|
|
|
2017-05-09 14:31:15 +01:00
|
|
|
request, response = app.test_client.patch('/patch', data=data)
|
2017-05-08 15:04:45 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
|
|
|
|
|
|
|
def test_request_stream_composition_view():
|
|
|
|
'''for self.is_request_stream = True'''
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
app = Sanic('test_request_stream__composition_view')
|
|
|
|
|
|
|
|
def get_handler(request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
async def post_handler(request):
|
|
|
|
assert isinstance(request.stream, asyncio.Queue)
|
|
|
|
result = ''
|
2017-05-05 12:09:32 +01:00
|
|
|
while True:
|
|
|
|
body = await request.stream.get()
|
|
|
|
if body is None:
|
|
|
|
break
|
2017-05-08 15:04:45 +01:00
|
|
|
result += body.decode('utf-8')
|
|
|
|
return text(result)
|
2017-05-05 12:09:32 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
view = CompositionView()
|
|
|
|
view.add(['GET'], get_handler)
|
|
|
|
view.add(['POST'], post_handler, stream=True)
|
|
|
|
app.add_route(view, '/composition_view')
|
2017-05-05 12:09:32 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
assert app.is_request_stream is True
|
2017-05-05 12:09:32 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
request, response = app.test_client.get('/composition_view')
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'OK'
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
request, response = app.test_client.post('/composition_view', data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
2017-05-07 13:11:40 +01:00
|
|
|
|
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
def test_request_stream():
|
|
|
|
'''test for complex application'''
|
|
|
|
|
|
|
|
bp = Blueprint('test_blueprint_request_stream')
|
|
|
|
app = Sanic('test_request_stream')
|
|
|
|
|
|
|
|
class SimpleView(HTTPMethodView):
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
@stream_decorator
|
|
|
|
async def post(self, request):
|
|
|
|
assert isinstance(request.stream, asyncio.Queue)
|
|
|
|
result = ''
|
|
|
|
while True:
|
|
|
|
body = await request.stream.get()
|
|
|
|
if body is None:
|
|
|
|
break
|
|
|
|
result += body.decode('utf-8')
|
|
|
|
return text(result)
|
|
|
|
|
2017-05-09 14:31:15 +01:00
|
|
|
@app.post('/stream', stream=True)
|
2017-05-08 15:04:45 +01:00
|
|
|
async def handler(request):
|
|
|
|
assert isinstance(request.stream, asyncio.Queue)
|
|
|
|
|
|
|
|
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):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('OK')
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2017-05-09 14:31:15 +01:00
|
|
|
@bp.post('/bp_stream', stream=True)
|
2017-05-08 15:04:45 +01:00
|
|
|
async def bp_stream(request):
|
|
|
|
assert isinstance(request.stream, asyncio.Queue)
|
|
|
|
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-08 15:04:45 +01:00
|
|
|
@bp.get('/bp_get')
|
|
|
|
async def bp_get(request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('OK')
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
def get_handler(request):
|
|
|
|
assert request.stream is None
|
|
|
|
return text('OK')
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
async def post_handler(request):
|
|
|
|
assert isinstance(request.stream, asyncio.Queue)
|
|
|
|
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-08 15:04:45 +01:00
|
|
|
app.add_route(SimpleView.as_view(), '/method_view')
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
view = CompositionView()
|
|
|
|
view.add(['GET'], get_handler)
|
|
|
|
view.add(['POST'], post_handler, stream=True)
|
2017-05-05 12:09:32 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
app.blueprint(bp)
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
app.add_route(view, '/composition_view')
|
2017-05-05 12:09:32 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
assert app.is_request_stream is True
|
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
|