From ffa54aae886863cd43a1e243e9962a40f2c86e87 Mon Sep 17 00:00:00 2001 From: p1c2u Date: Sat, 1 May 2021 00:01:22 +0100 Subject: [PATCH] Falcon3 tests --- openapi_core/contrib/falcon/handlers.py | 7 ++++++- openapi_core/contrib/falcon/responses.py | 8 +++++++- requirements_dev.txt | 2 +- tests/integration/contrib/falcon/conftest.py | 2 +- .../integration/contrib/falcon/test_falcon_middlewares.py | 8 ++++---- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/openapi_core/contrib/falcon/handlers.py b/openapi_core/contrib/falcon/handlers.py index 85d96a6..a4f6053 100644 --- a/openapi_core/contrib/falcon/handlers.py +++ b/openapi_core/contrib/falcon/handlers.py @@ -36,11 +36,16 @@ class FalconOpenAPIErrorsHandler(object): data = { 'errors': data_errors, } + data_str = dumps(data) data_error_max = max(data_errors, key=lambda x: x['status']) resp.content_type = MEDIA_JSON resp.status = cls.FALCON_STATUS_CODES.get( data_error_max['status'], HTTP_400) - resp.body = dumps(data) + # in falcon 3 body is deprecated + if hasattr(resp, 'text'): + resp.text = data_str + else: + resp.body = data_str resp.complete = True @classmethod diff --git a/openapi_core/contrib/falcon/responses.py b/openapi_core/contrib/falcon/responses.py index 9cca659..22ee93a 100644 --- a/openapi_core/contrib/falcon/responses.py +++ b/openapi_core/contrib/falcon/responses.py @@ -13,8 +13,14 @@ class FalconOpenAPIResponseFactory(object): else: mimetype = response.options.default_media_type + # in falcon 3 body is deprecated + if hasattr(response, "text"): + data = response.text + else: + data = response.body + return OpenAPIResponse( - data=response.body, + data=data, status_code=status_code, mimetype=mimetype, ) diff --git a/requirements_dev.txt b/requirements_dev.txt index 2254af2..5ee74a8 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -2,7 +2,7 @@ mock==2.0.0 pytest==3.5.0 pytest-flake8 pytest-cov==2.5.1 -falcon==2.0.0 +falcon==3.0.0 flask django==2.2.18; python_version>="3.0" requests==2.22.0 diff --git a/tests/integration/contrib/falcon/conftest.py b/tests/integration/contrib/falcon/conftest.py index 5ad0503..6513356 100644 --- a/tests/integration/contrib/falcon/conftest.py +++ b/tests/integration/contrib/falcon/conftest.py @@ -43,7 +43,7 @@ def response_factory(environ_factory): data, status_code=200, content_type='application/json'): options = ResponseOptions() resp = Response(options) - resp.body = data + resp.text = data resp.content_type = content_type resp.status = HTTP_200 return resp diff --git a/tests/integration/contrib/falcon/test_falcon_middlewares.py b/tests/integration/contrib/falcon/test_falcon_middlewares.py index d41a738..b55a78b 100644 --- a/tests/integration/contrib/falcon/test_falcon_middlewares.py +++ b/tests/integration/contrib/falcon/test_falcon_middlewares.py @@ -1,6 +1,6 @@ from json import dumps -from falcon import API +from falcon import App from falcon.testing import TestClient import pytest @@ -24,7 +24,7 @@ class TestFalconOpenAPIMiddleware(object): @pytest.fixture def app(self, middleware): - return API(middleware=[middleware]) + return App(middleware=[middleware]) @pytest.yield_fixture def client(self, app): @@ -67,7 +67,7 @@ class TestFalconOpenAPIMiddleware(object): }) response.content_type = MEDIA_HTML response.status = HTTP_200 - response.body = 'success' + response.text = 'success' self.view_response_callable = view_response_callable headers = {'Content-Type': 'application/json'} result = client.simulate_get( @@ -190,7 +190,7 @@ class TestFalconOpenAPIMiddleware(object): }) response.status = HTTP_200 response.content_type = MEDIA_JSON - response.body = dumps({ + response.text = dumps({ 'data': 'data', }) self.view_response_callable = view_response_callable