mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-06 04:19:26 +00:00
This PR updates the examples for Connexion 3.0 and merges them for OpenAPI and Swagger. 2 examples required some changes to make them work: - The reverse proxy example required some fixes to the SwaggerUIMiddleware to leverage the `root_path` correctly. This is included in the PR. - The enforced defaults example requires the json validator to adapt the body and pass it on. We currently pass on the original body after validation, and I'm not sure if we should change this. I'll submit a separate PR to discuss this.
71 lines
1.6 KiB
Python
71 lines
1.6 KiB
Python
from unittest.mock import MagicMock
|
|
|
|
from connexion.decorators.parameter import parameter_to_arg, pythonic
|
|
|
|
|
|
async def test_injection():
|
|
request = MagicMock(name="request")
|
|
request.query_params = {}
|
|
request.path_params = {"p1": "123"}
|
|
request.headers = {}
|
|
request.content_type = "application/json"
|
|
|
|
async def coro():
|
|
return
|
|
|
|
request.json = coro
|
|
request.loop = None
|
|
request.context = {}
|
|
|
|
func = MagicMock()
|
|
|
|
def handler(**kwargs):
|
|
func(**kwargs)
|
|
|
|
class Op:
|
|
consumes = ["application/json"]
|
|
parameters = []
|
|
method = "GET"
|
|
|
|
def body_name(self, *args, **kwargs):
|
|
return "body"
|
|
|
|
parameter_decorator = parameter_to_arg(Op(), handler)
|
|
parameter_decorator(request)
|
|
func.assert_called_with(p1="123")
|
|
|
|
|
|
async def test_injection_with_context():
|
|
request = MagicMock(name="request")
|
|
|
|
async def coro():
|
|
return
|
|
|
|
request.json = coro
|
|
request.loop = None
|
|
request.context = {}
|
|
request.content_type = "application/json"
|
|
request.path_params = {"p1": "123"}
|
|
|
|
func = MagicMock()
|
|
|
|
def handler(context_, **kwargs):
|
|
func(context_, **kwargs)
|
|
|
|
class Op2:
|
|
consumes = ["application/json"]
|
|
parameters = []
|
|
method = "GET"
|
|
|
|
def body_name(self, *args, **kwargs):
|
|
return "body"
|
|
|
|
parameter_decorator = parameter_to_arg(Op2(), handler)
|
|
parameter_decorator(request)
|
|
func.assert_called_with(request.context, p1="123")
|
|
|
|
|
|
def test_pythonic_params():
|
|
assert pythonic("orderBy[eq]") == "order_by_eq"
|
|
assert pythonic("ids[]") == "ids"
|