|
| 1 | +import pytest |
| 2 | + |
| 3 | +from flask.wrappers import Request |
| 4 | +from werkzeug.datastructures import EnvironHeaders, ImmutableMultiDict |
| 5 | +from werkzeug.routing import Map, Rule, Subdomain |
| 6 | +from werkzeug.test import create_environ |
| 7 | + |
| 8 | +from openapi_core.wrappers import FlaskOpenAPIRequest |
| 9 | + |
| 10 | + |
| 11 | +class TestFlaskOpenAPIRequest(object): |
| 12 | + |
| 13 | + server_name = 'localhost' |
| 14 | + |
| 15 | + @pytest.fixture |
| 16 | + def environ(self): |
| 17 | + return create_environ() |
| 18 | + |
| 19 | + @pytest.fixture |
| 20 | + def map(self): |
| 21 | + return Map([ |
| 22 | + # Static URLs |
| 23 | + Rule('/', endpoint='static/index'), |
| 24 | + Rule('/about', endpoint='static/about'), |
| 25 | + Rule('/help', endpoint='static/help'), |
| 26 | + # Knowledge Base |
| 27 | + Subdomain('kb', [ |
| 28 | + Rule('/', endpoint='kb/index'), |
| 29 | + Rule('/browse/', endpoint='kb/browse'), |
| 30 | + Rule('/browse/<int:id>/', endpoint='kb/browse'), |
| 31 | + Rule('/browse/<int:id>/<int:page>', endpoint='kb/browse') |
| 32 | + ]) |
| 33 | + ], default_subdomain='www') |
| 34 | + |
| 35 | + @pytest.fixture |
| 36 | + def request_factory(self, map, environ): |
| 37 | + def create_request(method, path, subdomain=None): |
| 38 | + req = Request(environ) |
| 39 | + urls = map.bind_to_environ( |
| 40 | + environ, server_name=self.server_name, subdomain=subdomain) |
| 41 | + req.url_rule, req.view_args = urls.match( |
| 42 | + path, method, return_rule=True) |
| 43 | + return req |
| 44 | + return create_request |
| 45 | + |
| 46 | + @pytest.fixture |
| 47 | + def openapi_request(self, request): |
| 48 | + return FlaskOpenAPIRequest(request) |
| 49 | + |
| 50 | + def test_simple(self, request_factory, environ, request): |
| 51 | + request = request_factory('GET', '/', subdomain='www') |
| 52 | + |
| 53 | + openapi_request = FlaskOpenAPIRequest(request) |
| 54 | + |
| 55 | + path = {} |
| 56 | + query = ImmutableMultiDict([]) |
| 57 | + headers = EnvironHeaders(environ) |
| 58 | + cookies = {} |
| 59 | + assert openapi_request.parameters == { |
| 60 | + 'path': path, |
| 61 | + 'query': query, |
| 62 | + 'headers': headers, |
| 63 | + 'cookies': cookies, |
| 64 | + } |
| 65 | + assert openapi_request.host_url == request.host_url |
| 66 | + assert openapi_request.path == request.path |
| 67 | + assert openapi_request.method == request.method.lower() |
| 68 | + assert openapi_request.path_pattern == request.path |
| 69 | + assert openapi_request.body == request.data |
| 70 | + assert openapi_request.mimetype == request.mimetype |
| 71 | + |
| 72 | + def test_url_rule(self, request_factory, environ, request): |
| 73 | + request = request_factory('GET', '/browse/12/', subdomain='kb') |
| 74 | + |
| 75 | + openapi_request = FlaskOpenAPIRequest(request) |
| 76 | + |
| 77 | + path = {'id': 12} |
| 78 | + query = ImmutableMultiDict([]) |
| 79 | + headers = EnvironHeaders(environ) |
| 80 | + cookies = {} |
| 81 | + assert openapi_request.parameters == { |
| 82 | + 'path': path, |
| 83 | + 'query': query, |
| 84 | + 'headers': headers, |
| 85 | + 'cookies': cookies, |
| 86 | + } |
| 87 | + assert openapi_request.host_url == request.host_url |
| 88 | + assert openapi_request.path == request.path |
| 89 | + assert openapi_request.method == request.method.lower() |
| 90 | + assert openapi_request.path_pattern == request.url_rule.rule |
| 91 | + assert openapi_request.body == request.data |
| 92 | + assert openapi_request.mimetype == request.mimetype |
0 commit comments