Skip to content

Commit

Permalink
Merge pull request #116 from 0b01001001/dev
Browse files Browse the repository at this point in the history
fix falcon 3 test cases
  • Loading branch information
kemingy authored Apr 7, 2021
2 parents d7cd12d + be42b9b commit cefc9f4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/falcon_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def on_get(self, req, resp):
health check
"""
self.check()
logger.debug("ping <> pong")
resp.media = {"msg": "pong"}


Expand Down Expand Up @@ -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__]

Expand Down
7 changes: 6 additions & 1 deletion spectree/plugins/falcon_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"[\.\(\)\[\]\?\$\*\+\^\|]"
Expand Down Expand Up @@ -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)

Expand Down
12 changes: 9 additions & 3 deletions tests/test_plugin_falcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit cefc9f4

Please sign in to comment.