openapi-core/tests/integration/contrib/falcon/conftest.py

52 lines
1.4 KiB
Python
Raw Normal View History

2020-02-17 16:33:01 +00:00
from falcon import Request, Response, RequestOptions, ResponseOptions
2020-01-28 09:23:02 +00:00
from falcon.routing import DefaultRouter
2020-02-17 16:33:01 +00:00
from falcon.status_codes import HTTP_200
2020-01-28 09:23:02 +00:00
from falcon.testing import create_environ
import pytest
@pytest.fixture
def environ_factory():
def create_env(method, path, server_name):
return create_environ(
host=server_name,
path=path,
)
return create_env
@pytest.fixture
def router():
router = DefaultRouter()
2020-02-17 16:33:01 +00:00
router.add_route("/browse/{id:int}/", lambda x: x)
2020-01-28 09:23:02 +00:00
return router
@pytest.fixture
def request_factory(environ_factory, router):
server_name = 'localhost'
2020-02-17 16:33:01 +00:00
def create_request(
method, path, subdomain=None, query_string=None,
content_type='application/json'):
2020-01-28 09:23:02 +00:00
environ = environ_factory(method, path, server_name)
2020-02-17 16:33:01 +00:00
options = RequestOptions()
2020-01-28 09:23:02 +00:00
# return create_req(options=options, **environ)
req = Request(environ, options)
2020-02-17 16:33:01 +00:00
resource, method_map, params, req.uri_template = router.find(path, req)
2020-01-28 09:23:02 +00:00
return req
return create_request
@pytest.fixture
def response_factory(environ_factory):
def create_response(
data, status_code=200, content_type='application/json'):
2020-02-17 16:33:01 +00:00
options = ResponseOptions()
resp = Response(options)
resp.body = data
resp.content_type = content_type
resp.status = HTTP_200
return resp
2020-01-28 09:23:02 +00:00
return create_response