mirror of
https://github.com/correl/openapi-core.git
synced 2024-12-29 11:09:25 +00:00
Flask wrapper added
This commit is contained in:
parent
624bf3de95
commit
6f1e45e255
3 changed files with 101 additions and 5 deletions
|
@ -64,7 +64,7 @@ class MockRequest(BaseOpenAPIRequest):
|
||||||
self.mimetype = mimetype
|
self.mimetype = mimetype
|
||||||
|
|
||||||
|
|
||||||
class WerkzeugOpenAPIRequest(BaseOpenAPIRequest):
|
class FlaskOpenAPIRequest(BaseOpenAPIRequest):
|
||||||
|
|
||||||
def __init__(self, request):
|
def __init__(self, request):
|
||||||
self.request = request
|
self.request = request
|
||||||
|
@ -83,15 +83,18 @@ class WerkzeugOpenAPIRequest(BaseOpenAPIRequest):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def path_pattern(self):
|
def path_pattern(self):
|
||||||
|
if self.request.url_rule is None:
|
||||||
|
return self.path
|
||||||
|
|
||||||
return self.request.url_rule.rule
|
return self.request.url_rule.rule
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parameters(self):
|
def parameters(self):
|
||||||
return {
|
return {
|
||||||
'path': self.request['view_args'],
|
'path': self.request.view_args,
|
||||||
'query': self.request['args'],
|
'query': self.request.args,
|
||||||
'headers': self.request['headers'],
|
'headers': self.request.headers,
|
||||||
'cookies': self.request['cookies'],
|
'cookies': self.request.cookies,
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -3,3 +3,4 @@ pytest
|
||||||
pytest-pep8
|
pytest-pep8
|
||||||
pytest-flakes
|
pytest-flakes
|
||||||
pytest-cov
|
pytest-cov
|
||||||
|
flask
|
92
tests/integration/test_wrappers.py
Normal file
92
tests/integration/test_wrappers.py
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from flask.wrappers import Request
|
||||||
|
from werkzeug.datastructures import EnvironHeaders, ImmutableMultiDict
|
||||||
|
from werkzeug.routing import Map, Rule, Subdomain
|
||||||
|
from werkzeug.test import create_environ
|
||||||
|
|
||||||
|
from openapi_core.wrappers import FlaskOpenAPIRequest
|
||||||
|
|
||||||
|
|
||||||
|
class TestFlaskOpenAPIRequest(object):
|
||||||
|
|
||||||
|
server_name = 'localhost'
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def environ(self):
|
||||||
|
return create_environ()
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def map(self):
|
||||||
|
return Map([
|
||||||
|
# Static URLs
|
||||||
|
Rule('/', endpoint='static/index'),
|
||||||
|
Rule('/about', endpoint='static/about'),
|
||||||
|
Rule('/help', endpoint='static/help'),
|
||||||
|
# Knowledge Base
|
||||||
|
Subdomain('kb', [
|
||||||
|
Rule('/', endpoint='kb/index'),
|
||||||
|
Rule('/browse/', endpoint='kb/browse'),
|
||||||
|
Rule('/browse/<int:id>/', endpoint='kb/browse'),
|
||||||
|
Rule('/browse/<int:id>/<int:page>', endpoint='kb/browse')
|
||||||
|
])
|
||||||
|
], default_subdomain='www')
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def request_factory(self, map, environ):
|
||||||
|
def create_request(method, path, subdomain=None):
|
||||||
|
req = Request(environ)
|
||||||
|
urls = map.bind_to_environ(
|
||||||
|
environ, server_name=self.server_name, subdomain=subdomain)
|
||||||
|
req.url_rule, req.view_args = urls.match(
|
||||||
|
path, method, return_rule=True)
|
||||||
|
return req
|
||||||
|
return create_request
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def openapi_request(self, request):
|
||||||
|
return FlaskOpenAPIRequest(request)
|
||||||
|
|
||||||
|
def test_simple(self, request_factory, environ, request):
|
||||||
|
request = request_factory('GET', '/', subdomain='www')
|
||||||
|
|
||||||
|
openapi_request = FlaskOpenAPIRequest(request)
|
||||||
|
|
||||||
|
path = {}
|
||||||
|
query = ImmutableMultiDict([])
|
||||||
|
headers = EnvironHeaders(environ)
|
||||||
|
cookies = {}
|
||||||
|
assert openapi_request.parameters == {
|
||||||
|
'path': path,
|
||||||
|
'query': query,
|
||||||
|
'headers': headers,
|
||||||
|
'cookies': cookies,
|
||||||
|
}
|
||||||
|
assert openapi_request.host_url == request.host_url
|
||||||
|
assert openapi_request.path == request.path
|
||||||
|
assert openapi_request.method == request.method.lower()
|
||||||
|
assert openapi_request.path_pattern == request.path
|
||||||
|
assert openapi_request.body == request.data
|
||||||
|
assert openapi_request.mimetype == request.mimetype
|
||||||
|
|
||||||
|
def test_url_rule(self, request_factory, environ, request):
|
||||||
|
request = request_factory('GET', '/browse/12/', subdomain='kb')
|
||||||
|
|
||||||
|
openapi_request = FlaskOpenAPIRequest(request)
|
||||||
|
|
||||||
|
path = {'id': 12}
|
||||||
|
query = ImmutableMultiDict([])
|
||||||
|
headers = EnvironHeaders(environ)
|
||||||
|
cookies = {}
|
||||||
|
assert openapi_request.parameters == {
|
||||||
|
'path': path,
|
||||||
|
'query': query,
|
||||||
|
'headers': headers,
|
||||||
|
'cookies': cookies,
|
||||||
|
}
|
||||||
|
assert openapi_request.host_url == request.host_url
|
||||||
|
assert openapi_request.path == request.path
|
||||||
|
assert openapi_request.method == request.method.lower()
|
||||||
|
assert openapi_request.path_pattern == request.url_rule.rule
|
||||||
|
assert openapi_request.body == request.data
|
||||||
|
assert openapi_request.mimetype == request.mimetype
|
Loading…
Reference in a new issue