feat: fixes exception due to unread bytes in stream (#1897)

* feat: fixes exception due to unread bytes in stream

* feat: additonal unit tests to cover changes

* fix: automated changes by `make fix-import`

* fix: additonal changes by `make fix-import`

Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
This commit is contained in:
Andrew Scott
2020-08-27 00:22:02 -07:00
committed by GitHub
parent 58e4087d4b
commit 3f7c9ea3f5
8 changed files with 40 additions and 17 deletions

View File

@@ -244,8 +244,8 @@ async def handler3(request):
def test_keep_alive_timeout_reuse():
"""If the server keep-alive timeout and client keep-alive timeout are
both longer than the delay, the client _and_ server will successfully
reuse the existing connection."""
both longer than the delay, the client _and_ server will successfully
reuse the existing connection."""
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

View File

@@ -46,8 +46,8 @@ def test_custom_context(app):
invalid = str(e)
j = loads(response.body)
j['response_mw_valid'] = user
j['response_mw_invalid'] = invalid
j["response_mw_valid"] = user
j["response_mw_invalid"] = invalid
return json(j)
request, response = app.test_client.get("/")
@@ -59,8 +59,7 @@ def test_custom_context(app):
"has_missing": False,
"invalid": "'types.SimpleNamespace' object has no attribute 'missing'",
"response_mw_valid": "sanic",
"response_mw_invalid":
"'types.SimpleNamespace' object has no attribute 'missing'"
"response_mw_invalid": "'types.SimpleNamespace' object has no attribute 'missing'",
}

View File

@@ -1,4 +1,5 @@
import pytest
import asyncio
from sanic.blueprints import Blueprint
from sanic.exceptions import HeaderExpectationFailed
@@ -6,6 +7,7 @@ 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
from sanic.server import HttpProtocol
data = "abc" * 1_000_000
@@ -337,6 +339,22 @@ def test_request_stream_handle_exception(app):
assert "Method GET not allowed for URL /post/random_id" in response.text
@pytest.mark.asyncio
async def test_request_stream_unread(app):
"""ensure no error is raised when leaving unread bytes in byte-buffer"""
err = None
protocol = HttpProtocol(loop=asyncio.get_event_loop(), app=app)
try:
protocol.request = None
protocol._body_chunks.append("this is a test")
await protocol.stream_append()
except AttributeError as e:
err = e
assert err is None and not protocol._body_chunks
def test_request_stream_blueprint(app):
"""for self.is_request_stream = True"""
bp = Blueprint("test_blueprint_request_stream_blueprint")