From dc6b4925dfcb1b0e4b90676c4167574841e8d43c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= Date: Wed, 4 Mar 2020 12:15:34 +0200 Subject: [PATCH] Add test for new API. --- tests/test_request_stream.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/test_request_stream.py b/tests/test_request_stream.py index 41f11f69..0cb10a44 100644 --- a/tests/test_request_stream.py +++ b/tests/test_request_stream.py @@ -2,7 +2,7 @@ import pytest from sanic.blueprints import Blueprint from sanic.exceptions import HeaderExpectationFailed -from sanic.response import stream, text +from sanic.response import json, stream, text from sanic.views import CompositionView, HTTPMethodView from sanic.views import stream as stream_decorator @@ -544,3 +544,33 @@ def test_request_stream(app): request, response = app.test_client.post("/bp_stream", data=data) assert response.status == 200 assert response.text == data + +def test_streaming_new_api(app): + @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) + return json(ret) + + 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