From 5a96996003ff62b15ad9cdf4e4978acf2ccee410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= Date: Sun, 1 Mar 2020 16:34:20 +0200 Subject: [PATCH] A test was missing that body_init/body_push/body_finish are never called. Rewritten using receive_body and case switching to make it fail if bypassed. --- tests/test_custom_request.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/tests/test_custom_request.py b/tests/test_custom_request.py index 81f20c8e..8516f998 100644 --- a/tests/test_custom_request.py +++ b/tests/test_custom_request.py @@ -6,18 +6,13 @@ from sanic.response import json_dumps, text class CustomRequest(Request): - __slots__ = ("body_buffer",) - - def body_init(self): - self.body_buffer = BytesIO() - - def body_push(self, data): - self.body_buffer.write(data) - - def body_finish(self): - self.body = self.body_buffer.getvalue() - self.body_buffer.close() - + """Alternative implementation for loading body (non-streaming handlers)""" + async def receive_body(self): + buffer = BytesIO() + async for data in self.stream: + buffer.write(data) + self.body = buffer.getvalue().upper() + buffer.close() def test_custom_request(): app = Sanic(request_class=CustomRequest) @@ -37,8 +32,8 @@ def test_custom_request(): "/post", data=json_dumps(payload), headers=headers ) - assert request.body == b'{"test":"OK"}' - assert request.json.get("test") == "OK" + assert request.body == b'{"TEST":"OK"}' + assert request.json.get("TEST") == "OK" assert response.text == "OK" assert response.status == 200