sanic/tests/test_request_stream.py

656 lines
19 KiB
Python
Raw Normal View History

2019-06-04 06:08:24 +01:00
import pytest
2019-06-11 09:21:37 +01:00
2017-05-05 12:09:32 +01:00
from sanic.blueprints import Blueprint
2019-06-04 06:08:24 +01:00
from sanic.exceptions import HeaderExpectationFailed
2018-12-04 06:28:22 +00:00
from sanic.request import StreamBuffer
from sanic.response import json, 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
Fix Ctrl+C and tests on Windows. (#1808) * Fix Ctrl+C on Windows. * Disable testing of a function N/A on Windows. * Add test for coverage, avoid crash on missing _stopping. * Initialise StreamingHTTPResponse.protocol = None * Improved comments. * Reduce amount of data in test_request_stream to avoid failures on Windows. * The Windows test doesn't work on Windows :( * Use port numbers more likely to be free than 8000. * Disable the other signal tests on Windows as well. * Windows doesn't properly support SO_REUSEADDR, so that's disabled in Python, and thus rebinding fails. For successful testing, reuse port instead. * app.run argument handling: added server kwargs (alike create_server), added warning on extra kwargs, made auto_reload explicit argument. Another go at Windows tests * Revert "app.run argument handling: added server kwargs (alike create_server), added warning on extra kwargs, made auto_reload explicit argument. Another go at Windows tests" This reverts commit dc5d682448e3f6595bdca5cb764e5f26ca29e295. * Use random test server port on most tests. Should avoid port/addr reuse issues. * Another test to random port instead of 8000. * Fix deprecation warnings about missing name on Sanic() in tests. * Linter and typing * Increase test coverage * Rewrite test for ctrlc_windows_workaround * py36 compat * py36 compat * py36 compat * Don't rely on loop internals but add a stopping flag to app. * App may be restarted. * py36 compat * Linter * Add a constant for OS checking. Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
2020-03-26 04:42:46 +00:00
data = "abc" * 1_000_000
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
class SimpleView(HTTPMethodView):
def get(self, request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("OK")
@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 = ""
while True:
2018-12-04 06:28:22 +00:00
body = await request.stream.read()
if body is None:
break
2018-12-30 11:18:06 +00:00
result += body.decode("utf-8")
return text(result)
2018-12-30 11:18:06 +00:00
app.add_route(SimpleView.as_view(), "/method_view")
assert app.is_request_stream is True
2018-12-30 11:18:06 +00:00
request, response = app.test_client.get("/method_view")
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "OK"
2018-12-30 11:18:06 +00:00
request, response = app.test_client.post("/method_view", data=data)
assert response.status == 200
assert response.text == data
2019-06-11 09:21:37 +01:00
@pytest.mark.parametrize(
"headers, expect_raise_exception",
[
({"EXPECT": "100-continue"}, False),
({"EXPECT": "100-continue-extra"}, True),
],
)
def test_request_stream_100_continue(app, headers, expect_raise_exception):
2019-06-04 06:08:24 +01:00
class SimpleView(HTTPMethodView):
@stream_decorator
async def post(self, request):
assert isinstance(request.stream, StreamBuffer)
result = ""
while True:
body = await request.stream.read()
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
if not expect_raise_exception:
2019-06-11 09:21:37 +01:00
request, response = app.test_client.post(
"/method_view", data=data, headers={"EXPECT": "100-continue"}
)
assert response.status == 200
assert response.text == data
else:
with pytest.raises(ValueError) as e:
2019-06-11 09:21:37 +01:00
app.test_client.post(
"/method_view",
data=data,
headers={"EXPECT": "100-continue-extra"},
)
2019-06-04 18:37:03 +01:00
assert "Unknown Expect: 100-continue-extra" in str(e)
2019-06-04 06:08:24 +01:00
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"""
2018-12-30 11:18:06 +00:00
@app.get("/get")
async def get(request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("GET")
2018-12-30 11:18:06 +00:00
@app.head("/head")
async def head(request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("HEAD")
2018-12-30 11:18:06 +00:00
@app.delete("/delete")
async def delete(request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("DELETE")
2018-12-30 11:18:06 +00:00
@app.options("/options")
async def options(request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("OPTIONS")
2018-12-30 11:18:06 +00:00
@app.post("/_post/<id>")
async def _post(request, id):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("_POST")
2018-12-30 11:18:06 +00:00
@app.post("/post/<id>", stream=True)
async def post(request, id):
2018-12-04 06:28:22 +00:00
assert isinstance(request.stream, StreamBuffer)
result = ""
while True:
body = await request.stream.read()
if body is None:
break
result += body.decode("utf-8")
return text(result)
2018-12-30 11:18:06 +00:00
@app.put("/_put")
async def _put(request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("_PUT")
2018-12-30 11:18:06 +00:00
@app.put("/put", stream=True)
async def put(request):
2018-12-04 06:28:22 +00:00
assert isinstance(request.stream, StreamBuffer)
result = ""
while True:
body = await request.stream.read()
if body is None:
break
result += body.decode("utf-8")
return text(result)
2018-12-30 11:18:06 +00:00
@app.patch("/_patch")
async def _patch(request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("_PATCH")
2018-12-30 11:18:06 +00:00
@app.patch("/patch", stream=True)
async def patch(request):
2018-12-04 06:28:22 +00:00
assert isinstance(request.stream, StreamBuffer)
result = ""
while True:
body = await request.stream.read()
if body is None:
break
result += body.decode("utf-8")
return text(result)
assert app.is_request_stream is True
2018-12-30 11:18:06 +00:00
request, response = app.test_client.get("/get")
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "GET"
2018-12-30 11:18:06 +00:00
request, response = app.test_client.head("/head")
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == ""
2018-12-30 11:18:06 +00:00
request, response = app.test_client.delete("/delete")
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "DELETE"
2018-12-30 11:18:06 +00:00
request, response = app.test_client.options("/options")
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "OPTIONS"
2018-12-30 11:18:06 +00:00
request, response = app.test_client.post("/_post/1", data=data)
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "_POST"
2018-12-30 11:18:06 +00:00
request, response = app.test_client.post("/post/1", data=data)
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)
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "_PUT"
2018-12-30 11:18:06 +00:00
request, response = app.test_client.put("/put", data=data)
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)
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "_PATCH"
2018-12-30 11:18:06 +00:00
request, response = app.test_client.patch("/patch", data=data)
assert response.status == 200
assert response.text == data
@pytest.mark.asyncio
async def test_request_stream_app_asgi(app):
"""for self.is_request_stream = True and decorators"""
@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):
assert isinstance(request.stream, StreamBuffer)
result = ""
while True:
body = await request.stream.read()
if body is None:
break
result += body.decode("utf-8")
return text(result)
@app.put("/_put")
async def _put(request):
assert request.stream is None
return text("_PUT")
@app.put("/put", stream=True)
async def put(request):
assert isinstance(request.stream, StreamBuffer)
result = ""
while True:
body = await request.stream.read()
if body is None:
break
result += body.decode("utf-8")
return text(result)
@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, StreamBuffer)
result = ""
while True:
body = await request.stream.read()
if body is None:
break
result += body.decode("utf-8")
return text(result)
assert app.is_request_stream is True
request, response = await app.asgi_client.get("/get")
assert response.status == 200
assert response.text == "GET"
request, response = await app.asgi_client.head("/head")
assert response.status == 200
assert response.text == ""
request, response = await app.asgi_client.delete("/delete")
assert response.status == 200
assert response.text == "DELETE"
request, response = await app.asgi_client.options("/options")
assert response.status == 200
assert response.text == "OPTIONS"
request, response = await app.asgi_client.post("/_post/1", data=data)
assert response.status == 200
assert response.text == "_POST"
request, response = await app.asgi_client.post("/post/1", data=data)
assert response.status == 200
assert response.text == data
request, response = await app.asgi_client.put("/_put", data=data)
assert response.status == 200
assert response.text == "_PUT"
request, response = await app.asgi_client.put("/put", data=data)
assert response.status == 200
assert response.text == data
request, response = await app.asgi_client.patch("/_patch", data=data)
assert response.status == 200
assert response.text == "_PATCH"
request, response = await app.asgi_client.patch("/patch", data=data)
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)
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
assert "Requested URL /in_valid_post not found" in response.text
2017-06-09 16:42:48 +01:00
2017-06-10 17:48:30 +01:00
# 405
request, response = app.test_client.get("/post/random_id")
2017-06-10 17:48:30 +01:00
assert response.status == 405
assert "Method GET not allowed for URL /post/random_id" in response.text
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")
2018-12-30 11:18:06 +00:00
@app.get("/get")
async def get(request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("GET")
2018-12-30 11:18:06 +00:00
@bp.head("/head")
async def head(request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("HEAD")
2018-12-30 11:18:06 +00:00
@bp.delete("/delete")
async def delete(request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("DELETE")
2018-12-30 11:18:06 +00:00
@bp.options("/options")
async def options(request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("OPTIONS")
2018-12-30 11:18:06 +00:00
@bp.post("/_post/<id>")
async def _post(request, id):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("_POST")
2018-12-30 11:18:06 +00:00
@bp.post("/post/<id>", stream=True)
async def post(request, id):
2018-12-04 06:28:22 +00:00
assert isinstance(request.stream, StreamBuffer)
result = ""
while True:
body = await request.stream.read()
if body is None:
break
result += body.decode("utf-8")
return text(result)
2018-12-30 11:18:06 +00:00
@bp.put("/_put")
async def _put(request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("_PUT")
2018-12-30 11:18:06 +00:00
@bp.put("/put", stream=True)
async def put(request):
2018-12-04 06:28:22 +00:00
assert isinstance(request.stream, StreamBuffer)
result = ""
while True:
body = await request.stream.read()
if body is None:
break
result += body.decode("utf-8")
return text(result)
2018-12-30 11:18:06 +00:00
@bp.patch("/_patch")
async def _patch(request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("_PATCH")
2018-12-30 11:18:06 +00:00
@bp.patch("/patch", stream=True)
async def patch(request):
2018-12-04 06:28:22 +00:00
assert isinstance(request.stream, StreamBuffer)
result = ""
while True:
body = await request.stream.read()
if body is None:
break
result += body.decode("utf-8")
return text(result)
async def post_add_route(request):
assert isinstance(request.stream, StreamBuffer)
result = ""
while True:
body = await request.stream.read()
if body is None:
break
result += body.decode("utf-8")
return text(result)
bp.add_route(
post_add_route, "/post/add_route", methods=["POST"], stream=True
)
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")
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "GET"
2018-12-30 11:18:06 +00:00
request, response = app.test_client.head("/head")
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == ""
2018-12-30 11:18:06 +00:00
request, response = app.test_client.delete("/delete")
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "DELETE"
2018-12-30 11:18:06 +00:00
request, response = app.test_client.options("/options")
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "OPTIONS"
2018-12-30 11:18:06 +00:00
request, response = app.test_client.post("/_post/1", data=data)
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "_POST"
2018-12-30 11:18:06 +00:00
request, response = app.test_client.post("/post/1", data=data)
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)
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "_PUT"
2018-12-30 11:18:06 +00:00
request, response = app.test_client.put("/put", data=data)
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)
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "_PATCH"
2018-12-30 11:18:06 +00:00
request, response = app.test_client.patch("/patch", data=data)
assert response.status == 200
assert response.text == data
request, response = app.test_client.post("/post/add_route", data=data)
assert response.status == 200
assert response.text == data
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
def get_handler(request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("OK")
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")
return text(result)
2017-05-05 12:09:32 +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
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")
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)
assert response.status == 200
assert response.text == data
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")
class SimpleView(HTTPMethodView):
def get(self, request):
assert request.stream is None
2018-12-30 11:18:06 +00:00
return text("OK")
@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 = ""
while True:
2018-12-04 06:28:22 +00:00
body = await request.stream.read()
if body is None:
break
2018-12-30 11:18:06 +00:00
result += body.decode("utf-8")
return text(result)
2018-12-30 11:18:06 +00:00
@app.post("/stream", stream=True)
async def handler(request):
2018-12-04 06:28:22 +00:00
assert isinstance(request.stream, StreamBuffer)
result = ""
while True:
body = await request.stream.read()
if body is None:
break
result += body.decode("utf-8")
return text(result)
2018-12-30 11:18:06 +00:00
@app.get("/get")
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)
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 = ""
while True:
2018-12-04 06:28:22 +00:00
body = await request.stream.read()
if body is None:
break
2018-12-30 11:18:06 +00:00
result += body.decode("utf-8")
return text(result)
2017-05-07 10:33:47 +01:00
2018-12-30 11:18:06 +00:00
@bp.get("/bp_get")
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
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
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 = ""
while True:
2018-12-04 06:28:22 +00:00
body = await request.stream.read()
if body is None:
break
2018-12-30 11:18:06 +00:00
result += body.decode("utf-8")
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
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
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
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)
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")
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "OK"
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
def test_streaming_new_api(app):
@app.post("/non-stream")
async def handler(request):
assert request.body == b"x"
await request.receive_body() # This should do nothing
assert request.body == b"x"
return text("OK")
@app.post("/1", stream=True)
async def handler(request):
assert request.stream
assert not request.body
await request.receive_body()
return text(request.body.decode().upper())
@app.post("/2", stream=True)
async def handler(request):
ret = []
async for data in request.stream:
# We should have no b"" or None, just proper chunks
assert data
assert isinstance(data, bytes)
ret.append(data.decode("ASCII"))
return json(ret)
request, response = app.test_client.post("/non-stream", data="x")
assert response.status == 200
request, response = app.test_client.post("/1", data="TEST data")
assert request.body == b"TEST data"
assert response.status == 200
assert response.text == "TEST DATA"
request, response = app.test_client.post("/2", data=data)
assert response.status == 200
res = response.json
assert isinstance(res, list)
assert len(res) > 1
assert "".join(res) == data