From f22988491451b7c6d4b5a8dd5ac5776caa60eabf Mon Sep 17 00:00:00 2001 From: keming Date: Wed, 7 Apr 2021 18:59:17 +0800 Subject: [PATCH 1/2] fix falcon example --- examples/falcon_demo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/falcon_demo.py b/examples/falcon_demo.py index 15cc9afb..270363e2 100644 --- a/examples/falcon_demo.py +++ b/examples/falcon_demo.py @@ -57,6 +57,7 @@ def on_get(self, req, resp): health check """ self.check() + logger.debug("ping <> pong") resp.media = {"msg": "pong"} @@ -95,7 +96,6 @@ def on_post(self, req, resp, source, target): class JSONFormatter(logging.Formatter): def __init__(self, *args, **kwargs): - super().__init__(self, *args, **kwargs) lr = logging.LogRecord(None, None, "", 0, "", (), None, None) self.default_keys = [key for key in lr.__dict__] From be42b9b3015005d94a582de1cb2d60abcbd2b337 Mon Sep 17 00:00:00 2001 From: keming Date: Wed, 7 Apr 2021 18:59:31 +0800 Subject: [PATCH 2/2] fix falcon 3 --- spectree/plugins/falcon_plugin.py | 7 ++++++- tests/test_plugin_falcon.py | 12 +++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/spectree/plugins/falcon_plugin.py b/spectree/plugins/falcon_plugin.py index f1860668..15afc891 100644 --- a/spectree/plugins/falcon_plugin.py +++ b/spectree/plugins/falcon_plugin.py @@ -31,8 +31,10 @@ def on_get(self, req, resp): class FalconPlugin(BasePlugin): def __init__(self, spectree): super().__init__(spectree) + from falcon import HTTPUnsupportedMediaType from falcon.routing.compiled import _FIELD_PATTERN + self.UnsupportedMediaType = HTTPUnsupportedMediaType self.FIELD_PATTERN = _FIELD_PATTERN # NOTE from `falcon.routing.compiled.CompiledRouterNode` self.ESCAPE = r"[\.\(\)\[\]\?\$\*\+\^\|]" @@ -141,7 +143,10 @@ def request_validation(self, req, query, json, headers, cookies): req.context.headers = headers.parse_obj(req.headers) if cookies: req.context.cookies = cookies.parse_obj(req.cookies) - media = req.media or {} + try: + media = req.media + except self.UnsupportedMediaType: + media = None if json: req.context.json = json.parse_obj(media) diff --git a/tests/test_plugin_falcon.py b/tests/test_plugin_falcon.py index cae04ce2..5ac3978c 100644 --- a/tests/test_plugin_falcon.py +++ b/tests/test_plugin_falcon.py @@ -96,16 +96,22 @@ def client(): def test_falcon_validate(client): - resp = client.simulate_request("GET", "/ping") + resp = client.simulate_request( + "GET", "/ping", headers={"Content-Type": "plain/text"} + ) assert resp.status_code == 422 assert resp.headers.get("X-Error") == "Validation Error", resp.headers - resp = client.simulate_request("GET", "/ping", headers={"lang": "en-US"}) + resp = client.simulate_request( + "GET", "/ping", headers={"lang": "en-US", "Content-Type": "plain/text"} + ) assert resp.json == {"msg": "pong"} assert resp.headers.get("X-Error") is None assert resp.headers.get("X-Name") == "health check" - resp = client.simulate_request("GET", "/api/user/falcon") + resp = client.simulate_request( + "GET", "/api/user/falcon", headers={"Content-Type": "plain/text"} + ) assert resp.json == {"name": "falcon"} resp = client.simulate_request("POST", "/api/user/falcon")