Skip to content

Commit 557145f

Browse files
committed
Falcon project integration tests
1 parent dfcf668 commit 557145f

File tree

10 files changed

+351
-397
lines changed

10 files changed

+351
-397
lines changed

openapi_core/contrib/falcon/handlers.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
11
"""OpenAPI core contrib falcon handlers module"""
22
from json import dumps
33

4+
from falcon import status_codes
45
from falcon.constants import MEDIA_JSON
5-
from falcon.status_codes import (
6-
HTTP_400, HTTP_404, HTTP_405, HTTP_415,
7-
)
86

7+
from openapi_core.exceptions import MissingRequiredParameter
98
from openapi_core.templating.media_types.exceptions import MediaTypeNotFound
109
from openapi_core.templating.paths.exceptions import (
1110
ServerNotFound, OperationNotFound, PathNotFound,
1211
)
12+
from openapi_core.validation.exceptions import InvalidSecurity
1313

1414

1515
class FalconOpenAPIErrorsHandler:
1616

1717
OPENAPI_ERROR_STATUS = {
18+
MissingRequiredParameter: 400,
1819
ServerNotFound: 400,
20+
InvalidSecurity: 403,
1921
OperationNotFound: 405,
2022
PathNotFound: 404,
2123
MediaTypeNotFound: 415,
2224
}
2325

24-
FALCON_STATUS_CODES = {
25-
400: HTTP_400,
26-
404: HTTP_404,
27-
405: HTTP_405,
28-
415: HTTP_415,
29-
}
30-
3126
@classmethod
3227
def handle(cls, req, resp, errors):
3328
data_errors = [
@@ -40,8 +35,10 @@ def handle(cls, req, resp, errors):
4035
data_str = dumps(data)
4136
data_error_max = max(data_errors, key=cls.get_error_status)
4237
resp.content_type = MEDIA_JSON
43-
resp.status = cls.FALCON_STATUS_CODES.get(
44-
data_error_max['status'], HTTP_400)
38+
resp.status = getattr(
39+
status_codes, f"HTTP_{data_error_max['status']}",
40+
status_codes.HTTP_400,
41+
)
4542
resp.text = data_str
4643
resp.complete = True
4744

tests/integration/contrib/falcon/data/v3.0/falconproject/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from falcon import App
22

33
from falconproject.openapi import openapi_middleware
4-
from falconproject.resources import PetListResource, PetDetailResource
4+
from falconproject.pets.resources import PetListResource, PetDetailResource
55

66
app = App(middleware=[openapi_middleware])
77

tests/integration/contrib/falcon/data/v3.0/falconproject/openapi.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import yaml
66

77
openapi_spec_path = Path("tests/integration/data/v3.0/petstore.yaml")
8-
spec_yaml = openapi_spec_path.read_text()
9-
spec_dict = yaml.load(spec_yaml)
8+
spec_dict = yaml.load(openapi_spec_path.read_text(), yaml.Loader)
109
spec = create_spec(spec_dict)
1110
openapi_middleware = FalconOpenAPIMiddleware.from_spec(spec)

tests/integration/contrib/falcon/data/v3.0/falconproject/pets/__init__.py

Whitespace-only changes.

tests/integration/contrib/falcon/data/v3.0/falconproject/resources.py renamed to tests/integration/contrib/falcon/data/v3.0/falconproject/pets/resources.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from json import dumps
22

33
from falcon.constants import MEDIA_JSON
4-
from falcon.status_codes import HTTP_200
4+
from falcon.status_codes import HTTP_200, HTTP_201
55

66

77
class PetListResource:
@@ -27,6 +27,23 @@ def on_get(self, request, response):
2727
response.text = dumps({"data": data})
2828
response.set_header('X-Rate-Limit', '12')
2929

30+
def on_post(self, request, response):
31+
assert request.openapi
32+
assert not request.openapi.errors
33+
assert request.openapi.parameters.cookie == {
34+
'user': 1,
35+
}
36+
assert request.openapi.parameters.header == {
37+
'api-key': '12345',
38+
}
39+
assert request.openapi.body.__class__.__name__ == 'PetCreate'
40+
assert request.openapi.body.name == 'Cat'
41+
assert request.openapi.body.ears.__class__.__name__ == 'Ears'
42+
assert request.openapi.body.ears.healthy is True
43+
44+
response.status = HTTP_201
45+
response.set_header('X-Rate-Limit', '12')
46+
3047

3148
class PetDetailResource:
3249
def on_get(self, request, response, petId=None):

tests/integration/contrib/falcon/data/v3.0/openapi.yaml

Lines changed: 0 additions & 60 deletions
This file was deleted.

tests/integration/contrib/falcon/test_falcon.py

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)