2017-05-05 12:09:32 +01:00
|
|
|
from sanic.blueprints import Blueprint
|
2018-12-04 06:28:22 +00:00
|
|
|
from sanic.request import StreamBuffer
|
2019-04-23 22:44:42 +01:00
|
|
|
from sanic.response import stream, text
|
|
|
|
from sanic.views import CompositionView, HTTPMethodView
|
|
|
|
from sanic.views import stream as stream_decorator
|
2018-12-04 06:28:22 +00:00
|
|
|
|
2017-05-05 12:09:32 +01:00
|
|
|
|
2018-12-04 06:51:09 +00:00
|
|
|
data = "abc" * 10000000
|
2017-05-05 12:09:32 +01:00
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_request_stream_method_view(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
"""for self.is_request_stream = True"""
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
class SimpleView(HTTPMethodView):
|
|
|
|
def get(self, request):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-05-08 15:04:45 +01:00
|
|
|
|
|
|
|
@stream_decorator
|
|
|
|
async def post(self, request):
|
2018-12-04 06:28:22 +00:00
|
|
|
assert isinstance(request.stream, StreamBuffer)
|
2018-12-30 11:18:06 +00:00
|
|
|
result = ""
|
2017-05-08 15:04:45 +01:00
|
|
|
while True:
|
2018-12-04 06:28:22 +00:00
|
|
|
body = await request.stream.read()
|
2017-05-08 15:04:45 +01:00
|
|
|
if body is None:
|
|
|
|
break
|
2018-12-30 11:18:06 +00:00
|
|
|
result += body.decode("utf-8")
|
2017-05-08 15:04:45 +01:00
|
|
|
return text(result)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
app.add_route(SimpleView.as_view(), "/method_view")
|
2017-05-08 15:04:45 +01:00
|
|
|
|
|
|
|
assert app.is_request_stream is True
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/method_view")
|
2017-05-08 15:04:45 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "OK"
|
2017-05-08 15:04:45 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/method_view", data=data)
|
2017-05-08 15:04:45 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_request_stream_app(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
"""for self.is_request_stream = True and decorators"""
|
2017-05-08 15:04:45 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/get")
|
2017-05-09 14:31:15 +01:00
|
|
|
async def get(request):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("GET")
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.head("/head")
|
2017-05-09 14:31:15 +01:00
|
|
|
async def head(request):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("HEAD")
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.delete("/delete")
|
2017-05-09 14:31:15 +01:00
|
|
|
async def delete(request):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("DELETE")
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.options("/options")
|
2017-05-09 14:31:15 +01:00
|
|
|
async def options(request):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OPTIONS")
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.post("/_post/<id>")
|
2017-05-09 14:31:15 +01:00
|
|
|
async def _post(request, id):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("_POST")
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.post("/post/<id>", stream=True)
|
2017-05-09 14:31:15 +01:00
|
|
|
async def post(request, id):
|
2018-12-04 06:28:22 +00:00
|
|
|
assert isinstance(request.stream, StreamBuffer)
|
2019-05-17 08:22:34 +01:00
|
|
|
result = ""
|
|
|
|
while True:
|
|
|
|
body = await request.stream.read()
|
|
|
|
if body is None:
|
|
|
|
break
|
|
|
|
result += body.decode("utf-8")
|
|
|
|
return text(result)
|
2017-05-08 15:04:45 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.put("/_put")
|
2017-05-09 14:31:15 +01:00
|
|
|
async def _put(request):
|
2017-05-08 15:04:45 +01:00
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("_PUT")
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.put("/put", stream=True)
|
2017-05-09 14:31:15 +01:00
|
|
|
async def put(request):
|
2018-12-04 06:28:22 +00:00
|
|
|
assert isinstance(request.stream, StreamBuffer)
|
2019-05-17 08:22:34 +01:00
|
|
|
result = ""
|
|
|
|
while True:
|
|
|
|
body = await request.stream.read()
|
|
|
|
if body is None:
|
|
|
|
break
|
|
|
|
result += body.decode("utf-8")
|
|
|
|
return text(result)
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.patch("/_patch")
|
2017-05-09 14:31:15 +01:00
|
|
|
async def _patch(request):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("_PATCH")
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.patch("/patch", stream=True)
|
2017-05-09 14:31:15 +01:00
|
|
|
async def patch(request):
|
2018-12-04 06:28:22 +00:00
|
|
|
assert isinstance(request.stream, StreamBuffer)
|
2019-05-17 08:22:34 +01:00
|
|
|
result = ""
|
|
|
|
while True:
|
|
|
|
body = await request.stream.read()
|
|
|
|
if body is None:
|
|
|
|
break
|
|
|
|
result += body.decode("utf-8")
|
|
|
|
return text(result)
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
assert app.is_request_stream is True
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/get")
|
2017-05-08 15:04:45 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "GET"
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.head("/head")
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == ""
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.delete("/delete")
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "DELETE"
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.options("/options")
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "OPTIONS"
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/_post/1", data=data)
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "_POST"
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/post/1", data=data)
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.put("/_put", data=data)
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "_PUT"
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.put("/put", data=data)
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.patch("/_patch", data=data)
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "_PATCH"
|
2017-05-08 15:04:45 +01:00
|
|
|
|
2018-12-30 11:18:06 +00: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
|
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_request_stream_handle_exception(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
"""for handling exceptions properly"""
|
2017-06-09 16:42:48 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.post("/post/<id>", stream=True)
|
2017-06-09 16:42:48 +01:00
|
|
|
async def post(request, id):
|
2018-12-04 06:28:22 +00:00
|
|
|
assert isinstance(request.stream, StreamBuffer)
|
2019-05-17 08:22:34 +01:00
|
|
|
result = ""
|
|
|
|
while True:
|
|
|
|
body = await request.stream.read()
|
|
|
|
if body is None:
|
|
|
|
break
|
|
|
|
result += body.decode("utf-8")
|
|
|
|
return text(result)
|
2017-06-09 16:42:48 +01:00
|
|
|
|
|
|
|
# 404
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/in_valid_post", data=data)
|
2017-06-09 16:42:48 +01:00
|
|
|
assert response.status == 404
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "Error: Requested URL /in_valid_post not found"
|
2017-06-09 16:42:48 +01:00
|
|
|
|
2017-06-10 17:48:30 +01:00
|
|
|
# 405
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/post/random_id", data=data)
|
2017-06-10 17:48:30 +01:00
|
|
|
assert response.status == 405
|
2018-12-30 11:18:06 +00:00
|
|
|
assert (
|
|
|
|
response.text == "Error: Method GET not allowed for URL"
|
|
|
|
" /post/random_id"
|
|
|
|
)
|
2017-06-10 17:48:30 +01:00
|
|
|
|
2017-06-09 16:42:48 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_request_stream_blueprint(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
"""for self.is_request_stream = True"""
|
|
|
|
bp = Blueprint("test_blueprint_request_stream_blueprint")
|
2017-05-08 15:04:45 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/get")
|
2017-05-09 14:31:15 +01:00
|
|
|
async def get(request):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("GET")
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.head("/head")
|
2017-05-09 14:31:15 +01:00
|
|
|
async def head(request):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("HEAD")
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.delete("/delete")
|
2017-05-09 14:31:15 +01:00
|
|
|
async def delete(request):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("DELETE")
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.options("/options")
|
2017-05-09 14:31:15 +01:00
|
|
|
async def options(request):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OPTIONS")
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.post("/_post/<id>")
|
2017-05-09 14:31:15 +01:00
|
|
|
async def _post(request, id):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("_POST")
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.post("/post/<id>", stream=True)
|
2017-05-09 14:31:15 +01:00
|
|
|
async def post(request, id):
|
2018-12-04 06:28:22 +00:00
|
|
|
assert isinstance(request.stream, StreamBuffer)
|
2019-05-17 08:22:34 +01:00
|
|
|
result = ""
|
|
|
|
while True:
|
|
|
|
body = await request.stream.read()
|
|
|
|
if body is None:
|
|
|
|
break
|
|
|
|
result += body.decode("utf-8")
|
|
|
|
return text(result)
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.put("/_put")
|
2017-05-09 14:31:15 +01:00
|
|
|
async def _put(request):
|
2017-05-08 15:04:45 +01:00
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("_PUT")
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.put("/put", stream=True)
|
2017-05-09 14:31:15 +01:00
|
|
|
async def put(request):
|
2018-12-04 06:28:22 +00:00
|
|
|
assert isinstance(request.stream, StreamBuffer)
|
2019-05-17 08:22:34 +01:00
|
|
|
result = ""
|
|
|
|
while True:
|
|
|
|
body = await request.stream.read()
|
|
|
|
if body is None:
|
|
|
|
break
|
|
|
|
result += body.decode("utf-8")
|
|
|
|
return text(result)
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.patch("/_patch")
|
2017-05-09 14:31:15 +01:00
|
|
|
async def _patch(request):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("_PATCH")
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.patch("/patch", stream=True)
|
2017-05-09 14:31:15 +01:00
|
|
|
async def patch(request):
|
2018-12-04 06:28:22 +00:00
|
|
|
assert isinstance(request.stream, StreamBuffer)
|
2019-05-17 08:22:34 +01:00
|
|
|
result = ""
|
|
|
|
while True:
|
|
|
|
body = await request.stream.read()
|
|
|
|
if body is None:
|
|
|
|
break
|
|
|
|
result += body.decode("utf-8")
|
|
|
|
return text(result)
|
2017-05-08 15:04:45 +01:00
|
|
|
|
2019-02-05 13:47:46 +00:00
|
|
|
async def post_add_route(request):
|
|
|
|
assert isinstance(request.stream, StreamBuffer)
|
2019-05-17 08:22:34 +01:00
|
|
|
result = ""
|
|
|
|
while True:
|
|
|
|
body = await request.stream.read()
|
|
|
|
if body is None:
|
|
|
|
break
|
|
|
|
result += body.decode("utf-8")
|
|
|
|
return text(result)
|
2019-02-05 13:47:46 +00:00
|
|
|
|
2019-02-28 14:56:41 +00:00
|
|
|
bp.add_route(
|
|
|
|
post_add_route, "/post/add_route", methods=["POST"], stream=True
|
|
|
|
)
|
2017-05-08 15:04:45 +01:00
|
|
|
app.blueprint(bp)
|
|
|
|
|
|
|
|
assert app.is_request_stream is True
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/get")
|
2017-05-08 15:04:45 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "GET"
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.head("/head")
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == ""
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.delete("/delete")
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "DELETE"
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.options("/options")
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "OPTIONS"
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/_post/1", data=data)
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "_POST"
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/post/1", data=data)
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.put("/_put", data=data)
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "_PUT"
|
2017-05-09 14:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.put("/put", data=data)
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.patch("/_patch", data=data)
|
2017-05-09 14:31:15 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "_PATCH"
|
2017-05-08 15:04:45 +01:00
|
|
|
|
2018-12-30 11:18:06 +00: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
|
|
|
|
|
2019-02-05 13:47:46 +00:00
|
|
|
request, response = app.test_client.post("/post/add_route", data=data)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_request_stream_composition_view(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
"""for self.is_request_stream = True"""
|
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
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-05-08 15:04:45 +01:00
|
|
|
|
|
|
|
async def post_handler(request):
|
2018-12-04 06:28:22 +00:00
|
|
|
assert isinstance(request.stream, StreamBuffer)
|
2018-12-30 11:18:06 +00:00
|
|
|
result = ""
|
2017-05-05 12:09:32 +01:00
|
|
|
while True:
|
2018-12-04 06:28:22 +00:00
|
|
|
body = await request.stream.read()
|
2017-05-05 12:09:32 +01:00
|
|
|
if body is None:
|
|
|
|
break
|
2018-12-30 11:18:06 +00:00
|
|
|
result += body.decode("utf-8")
|
2017-05-08 15:04:45 +01:00
|
|
|
return text(result)
|
2017-05-05 12:09:32 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
view = CompositionView()
|
2018-12-30 11:18:06 +00:00
|
|
|
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
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/composition_view")
|
2017-05-08 15:04:45 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "OK"
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/composition_view", data=data)
|
2017-05-08 15:04:45 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
2017-05-07 13:11:40 +01:00
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_request_stream(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
"""test for complex application"""
|
|
|
|
bp = Blueprint("test_blueprint_request_stream")
|
2017-05-08 15:04:45 +01:00
|
|
|
|
|
|
|
class SimpleView(HTTPMethodView):
|
|
|
|
def get(self, request):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-05-08 15:04:45 +01:00
|
|
|
|
|
|
|
@stream_decorator
|
|
|
|
async def post(self, request):
|
2018-12-04 06:28:22 +00:00
|
|
|
assert isinstance(request.stream, StreamBuffer)
|
2018-12-30 11:18:06 +00:00
|
|
|
result = ""
|
2017-05-08 15:04:45 +01:00
|
|
|
while True:
|
2018-12-04 06:28:22 +00:00
|
|
|
body = await request.stream.read()
|
2017-05-08 15:04:45 +01:00
|
|
|
if body is None:
|
|
|
|
break
|
2018-12-30 11:18:06 +00:00
|
|
|
result += body.decode("utf-8")
|
2017-05-08 15:04:45 +01:00
|
|
|
return text(result)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.post("/stream", stream=True)
|
2017-05-08 15:04:45 +01:00
|
|
|
async def handler(request):
|
2018-12-04 06:28:22 +00:00
|
|
|
assert isinstance(request.stream, StreamBuffer)
|
2019-05-17 08:22:34 +01:00
|
|
|
result = ""
|
|
|
|
while True:
|
|
|
|
body = await request.stream.read()
|
|
|
|
if body is None:
|
|
|
|
break
|
|
|
|
result += body.decode("utf-8")
|
|
|
|
return text(result)
|
2017-05-08 15:04:45 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/get")
|
2017-05-08 15:04:45 +01:00
|
|
|
async def get(request):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.post("/bp_stream", stream=True)
|
2017-05-08 15:04:45 +01:00
|
|
|
async def bp_stream(request):
|
2018-12-04 06:28:22 +00:00
|
|
|
assert isinstance(request.stream, StreamBuffer)
|
2018-12-30 11:18:06 +00:00
|
|
|
result = ""
|
2017-05-08 15:04:45 +01:00
|
|
|
while True:
|
2018-12-04 06:28:22 +00:00
|
|
|
body = await request.stream.read()
|
2017-05-08 15:04:45 +01:00
|
|
|
if body is None:
|
|
|
|
break
|
2018-12-30 11:18:06 +00:00
|
|
|
result += body.decode("utf-8")
|
2017-05-08 15:04:45 +01:00
|
|
|
return text(result)
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.get("/bp_get")
|
2017-05-08 15:04:45 +01:00
|
|
|
async def bp_get(request):
|
|
|
|
assert request.stream is None
|
2018-12-30 11:18:06 +00:00
|
|
|
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
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2017-05-08 15:04:45 +01:00
|
|
|
async def post_handler(request):
|
2018-12-04 06:28:22 +00:00
|
|
|
assert isinstance(request.stream, StreamBuffer)
|
2018-12-30 11:18:06 +00:00
|
|
|
result = ""
|
2017-05-08 15:04:45 +01:00
|
|
|
while True:
|
2018-12-04 06:28:22 +00:00
|
|
|
body = await request.stream.read()
|
2017-05-08 15:04:45 +01:00
|
|
|
if body is None:
|
|
|
|
break
|
2018-12-30 11:18:06 +00:00
|
|
|
result += body.decode("utf-8")
|
2017-05-08 15:04:45 +01:00
|
|
|
return text(result)
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2018-12-30 11:18:06 +00: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()
|
2018-12-30 11:18:06 +00:00
|
|
|
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
|
|
|
|
2018-12-30 11:18:06 +00: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
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/method_view")
|
2017-05-07 10:33:47 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "OK"
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/method_view", data=data)
|
2017-05-07 13:11:40 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/composition_view")
|
2017-05-07 10:33:47 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "OK"
|
2017-05-07 10:33:47 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
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
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/get")
|
2017-05-05 12:09:32 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "OK"
|
2017-05-05 12:09:32 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/stream", data=data)
|
2017-05-07 10:33:47 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/bp_get")
|
2017-05-07 13:11:40 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "OK"
|
2017-05-07 13:11:40 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/bp_stream", data=data)
|
2017-05-05 12:09:32 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == data
|