fix request_timeout and request_streaming tests

This commit is contained in:
Yun Xu 2019-05-17 00:22:34 -07:00
parent 046ca6eaf1
commit 2a64dabe82
2 changed files with 76 additions and 104 deletions

View File

@ -71,15 +71,13 @@ def test_request_stream_app(app):
@app.post("/post/<id>", stream=True) @app.post("/post/<id>", stream=True)
async def post(request, id): async def post(request, id):
assert isinstance(request.stream, StreamBuffer) assert isinstance(request.stream, StreamBuffer)
result = ""
async def streaming(response): while True:
while True: body = await request.stream.read()
body = await request.stream.read() if body is None:
if body is None: break
break result += body.decode("utf-8")
await response.write(body.decode("utf-8")) return text(result)
return stream(streaming)
@app.put("/_put") @app.put("/_put")
async def _put(request): async def _put(request):
@ -89,15 +87,13 @@ def test_request_stream_app(app):
@app.put("/put", stream=True) @app.put("/put", stream=True)
async def put(request): async def put(request):
assert isinstance(request.stream, StreamBuffer) assert isinstance(request.stream, StreamBuffer)
result = ""
async def streaming(response): while True:
while True: body = await request.stream.read()
body = await request.stream.read() if body is None:
if body is None: break
break result += body.decode("utf-8")
await response.write(body.decode("utf-8")) return text(result)
return stream(streaming)
@app.patch("/_patch") @app.patch("/_patch")
async def _patch(request): async def _patch(request):
@ -107,15 +103,14 @@ def test_request_stream_app(app):
@app.patch("/patch", stream=True) @app.patch("/patch", stream=True)
async def patch(request): async def patch(request):
assert isinstance(request.stream, StreamBuffer) 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 streaming(response):
while True:
body = await request.stream.read()
if body is None:
break
await response.write(body.decode("utf-8"))
return stream(streaming)
assert app.is_request_stream is True assert app.is_request_stream is True
@ -166,15 +161,13 @@ def test_request_stream_handle_exception(app):
@app.post("/post/<id>", stream=True) @app.post("/post/<id>", stream=True)
async def post(request, id): async def post(request, id):
assert isinstance(request.stream, StreamBuffer) assert isinstance(request.stream, StreamBuffer)
result = ""
async def streaming(response): while True:
while True: body = await request.stream.read()
body = await request.stream.read() if body is None:
if body is None: break
break result += body.decode("utf-8")
await response.write(body.decode("utf-8")) return text(result)
return stream(streaming)
# 404 # 404
request, response = app.test_client.post("/in_valid_post", data=data) request, response = app.test_client.post("/in_valid_post", data=data)
@ -222,15 +215,13 @@ def test_request_stream_blueprint(app):
@bp.post("/post/<id>", stream=True) @bp.post("/post/<id>", stream=True)
async def post(request, id): async def post(request, id):
assert isinstance(request.stream, StreamBuffer) assert isinstance(request.stream, StreamBuffer)
result = ""
async def streaming(response): while True:
while True: body = await request.stream.read()
body = await request.stream.read() if body is None:
if body is None: break
break result += body.decode("utf-8")
await response.write(body.decode("utf-8")) return text(result)
return stream(streaming)
@bp.put("/_put") @bp.put("/_put")
async def _put(request): async def _put(request):
@ -240,15 +231,13 @@ def test_request_stream_blueprint(app):
@bp.put("/put", stream=True) @bp.put("/put", stream=True)
async def put(request): async def put(request):
assert isinstance(request.stream, StreamBuffer) assert isinstance(request.stream, StreamBuffer)
result = ""
async def streaming(response): while True:
while True: body = await request.stream.read()
body = await request.stream.read() if body is None:
if body is None: break
break result += body.decode("utf-8")
await response.write(body.decode("utf-8")) return text(result)
return stream(streaming)
@bp.patch("/_patch") @bp.patch("/_patch")
async def _patch(request): async def _patch(request):
@ -258,27 +247,23 @@ def test_request_stream_blueprint(app):
@bp.patch("/patch", stream=True) @bp.patch("/patch", stream=True)
async def patch(request): async def patch(request):
assert isinstance(request.stream, StreamBuffer) assert isinstance(request.stream, StreamBuffer)
result = ""
async def streaming(response): while True:
while True: body = await request.stream.read()
body = await request.stream.read() if body is None:
if body is None: break
break result += body.decode("utf-8")
await response.write(body.decode("utf-8")) return text(result)
return stream(streaming)
async def post_add_route(request): async def post_add_route(request):
assert isinstance(request.stream, StreamBuffer) assert isinstance(request.stream, StreamBuffer)
result = ""
async def streaming(response): while True:
while True: body = await request.stream.read()
body = await request.stream.read() if body is None:
if body is None: break
break result += body.decode("utf-8")
await response.write(body.decode("utf-8")) return text(result)
return stream(streaming)
bp.add_route( bp.add_route(
post_add_route, "/post/add_route", methods=["POST"], stream=True post_add_route, "/post/add_route", methods=["POST"], stream=True
@ -388,15 +373,13 @@ def test_request_stream(app):
@app.post("/stream", stream=True) @app.post("/stream", stream=True)
async def handler(request): async def handler(request):
assert isinstance(request.stream, StreamBuffer) assert isinstance(request.stream, StreamBuffer)
result = ""
async def streaming(response): while True:
while True: body = await request.stream.read()
body = await request.stream.read() if body is None:
if body is None: break
break result += body.decode("utf-8")
await response.write(body.decode("utf-8")) return text(result)
return stream(streaming)
@app.get("/get") @app.get("/get")
async def get(request): async def get(request):

View File

@ -13,37 +13,26 @@ class DelayableSanicConnectionPool(httpcore.ConnectionPool):
self._request_delay = request_delay self._request_delay = request_delay
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
async def request( async def send(
self, self,
method, request,
url,
headers=(),
body=b"",
stream=False, stream=False,
ssl=None, ssl=None,
timeout=None, timeout=None,
): ):
if ssl is None: connection = await self.acquire_connection(request.url.origin)
ssl = self.ssl_config if connection.h11_connection is None and connection.h2_connection is None:
if timeout is None: await connection.connect(ssl=ssl, timeout=timeout)
timeout = self.timeout
parsed_url = httpcore.URL(url)
request = httpcore.Request(
method, parsed_url, headers=headers, body=body
)
connection = await self.acquire_connection(
parsed_url, ssl=ssl, timeout=timeout
)
if self._request_delay: if self._request_delay:
print(f"\t>> Sleeping ({self._request_delay})")
await asyncio.sleep(self._request_delay) await asyncio.sleep(self._request_delay)
response = await connection.send(request) try:
if not stream: response = await connection.send(
try: request, stream=stream, ssl=ssl, timeout=timeout
await response.read() )
finally: except BaseException as exc:
await response.close() self.active_connections.remove(connection)
self.max_connections.release()
raise exc
return response return response