Skip to content

Commit 09f87a9

Browse files
committed
Falcon contrib
1 parent db80167 commit 09f87a9

File tree

6 files changed

+137
-2
lines changed

6 files changed

+137
-2
lines changed

Diff for: openapi_core/contrib/falcon/requests.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def create(cls, req, route_params):
2323
cookie=req.cookies,
2424
)
2525
return OpenAPIRequest(
26-
host_url=req.host,
26+
host_url=None,
2727
path=req.path,
28-
path_pattern=req.uri_template,
28+
path_pattern=req.uri_template or req.path,
2929
method=method,
3030
parameters=parameters,
3131
# Support falcon-jsonify.

Diff for: requirements_dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mock==2.0.0
22
pytest==3.5.0
33
pytest-flake8
44
pytest-cov==2.5.1
5+
falcon==2.0.0
56
flask
67
django==2.2.10; python_version>="3.0"
78
requests==2.22.0

Diff for: setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ tests_require =
3838
pytest
3939
pytest-flake8
4040
pytest-cov
41+
falcon
4142
flask
4243
webob
4344

Diff for: tests/integration/contrib/falcon/conftest.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from falcon import Request, Response
2+
from falcon.routing import DefaultRouter
3+
from falcon.testing import create_environ
4+
import pytest
5+
from six import BytesIO
6+
7+
8+
@pytest.fixture
9+
def environ_factory():
10+
def create_env(method, path, server_name):
11+
return create_environ(
12+
host=server_name,
13+
path=path,
14+
)
15+
return create_env
16+
17+
18+
@pytest.fixture
19+
def router():
20+
router = DefaultRouter()
21+
router.add_route('/browse/<int:id>/', None)
22+
return router
23+
24+
25+
@pytest.fixture
26+
def request_factory(environ_factory, router):
27+
server_name = 'localhost'
28+
29+
def create_request(method, path, subdomain=None, query_string=None):
30+
environ = environ_factory(method, path, server_name)
31+
options = None
32+
# return create_req(options=options, **environ)
33+
req = Request(environ, options)
34+
req.uri_template = router.find(path, req)
35+
return req
36+
return create_request
37+
38+
39+
@pytest.fixture
40+
def response_factory(environ_factory):
41+
def create_response(
42+
data, status_code=200, content_type='application/json'):
43+
options = {
44+
'content_type': content_type,
45+
'data': data,
46+
'status': status_code,
47+
}
48+
return Response(options)
49+
return create_response
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
openapi: "3.0.0"
2+
info:
3+
title: Basic OpenAPI specification used with test_flask.TestFlaskOpenAPIIValidation
4+
version: "0.1"
5+
servers:
6+
- url: 'http://localhost'
7+
paths:
8+
'/browse/{id}/':
9+
parameters:
10+
- name: id
11+
in: path
12+
required: true
13+
description: the ID of the resource to retrieve
14+
schema:
15+
type: integer
16+
get:
17+
responses:
18+
200:
19+
description: Return the resource.
20+
content:
21+
application/json:
22+
schema:
23+
type: object
24+
required:
25+
- data
26+
properties:
27+
data:
28+
type: string
29+
default:
30+
description: Return errors.
31+
content:
32+
application/json:
33+
schema:
34+
type: object
35+
required:
36+
- errors
37+
properties:
38+
errors:
39+
type: array
40+
items:
41+
type: object
42+
properties:
43+
title:
44+
type: string
45+
code:
46+
type: string
47+
message:
48+
type: string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pytest
2+
3+
from openapi_core.contrib.falcon.requests import FalconOpenAPIRequestFactory
4+
from openapi_core.contrib.falcon.responses import FalconOpenAPIResponseFactory
5+
from openapi_core.shortcuts import create_spec
6+
from openapi_core.validation.request.validators import RequestValidator
7+
from openapi_core.validation.response.validators import ResponseValidator
8+
9+
10+
class TestFalconOpenAPIValidation(object):
11+
12+
@pytest.fixture
13+
def spec(self, factory):
14+
specfile = 'contrib/falcon/data/v3.0/falcon_factory.yaml'
15+
return create_spec(factory.spec_from_file(specfile))
16+
17+
def test_response_validator_path_pattern(self,
18+
spec,
19+
request_factory,
20+
response_factory):
21+
validator = ResponseValidator(spec)
22+
request = request_factory('GET', '/browse/12/', subdomain='kb')
23+
openapi_request = FalconOpenAPIRequestFactory.create(
24+
request, '/browse/12/')
25+
response = response_factory('{"data": "data"}', status_code=200)
26+
openapi_response = FalconOpenAPIResponseFactory.create(response)
27+
result = validator.validate(openapi_request, openapi_response)
28+
assert not result.errors
29+
30+
def test_request_validator_path_pattern(self, spec, request_factory):
31+
validator = RequestValidator(spec)
32+
request = request_factory('GET', '/browse/12/', subdomain='kb')
33+
openapi_request = FalconOpenAPIRequestFactory.create(
34+
request, '/browse/12/')
35+
result = validator.validate(openapi_request)
36+
assert not result.errors

0 commit comments

Comments
 (0)