mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-10 04:19:37 +00:00
This PR adds an interface for the ConnexionMiddleware, similar to the
interface of the Connexion Apps.
The Connexion Apps are now a simple wrapper around the
ConnexionMiddleware and framework app, delegating the work to the
middleware. This enables a similar interface and behavior for users when
using either the middleware or apps.
The arguments are repeated everywhere there is a user interface, but are
parsed in a central place. Repeating the arguments is not DRY, but
needed to provide users with IDE autocomplete, typing, etc. They are
parsed in a single `_Options` class, which also provides a mechanism to
set default options on an App level, and override them on the more
granular API level.
This makes the long list of provided parameters a lot more manageable,
so I would like to use it for the `Jsonifier` as well, and re-add the
`debug` and `extra_files` arguments which I have dropped in previous
PRs. I'll submit a separate PR for this.
I renamed the `options` parameter to `swagger_ui_options` since it only
contains swagger UI options. This is a breaking change though, and we'll
need to highlight this upon release.
We still have quite a lot of `App`, `MiddlewareApp`, and abstract
classes. It would be great if we could find a way to reduce those
further, or at least find better naming to make it more clear what each
one does 🙂 .
Finally, I added examples on how the middleware can be used with third
party frameworks under `examples/frameworks`. Currently there's an
example for Starlette and Quart, but this should be easy to extend. They
also show how the `ASGIDecorator` and `StarletteDecorator` from my
previous PR can be used.
61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
import json
|
|
|
|
|
|
def test_headers_jsonifier(simple_app):
|
|
app_client = simple_app.test_client()
|
|
|
|
response = app_client.post("/v1.0/goodday/dan", data={}) # type: flask.Response
|
|
assert response.status_code == 201
|
|
# Default Werkzeug behavior was changed in 2.1 (https://github.com/pallets/werkzeug/issues/2352)
|
|
assert response.headers["Location"] in ["http://localhost/my/uri", "/my/uri"]
|
|
|
|
|
|
def test_headers_produces(simple_app):
|
|
app_client = simple_app.test_client()
|
|
|
|
response = app_client.post("/v1.0/goodevening/dan", data={}) # type: flask.Response
|
|
assert response.status_code == 201
|
|
# Default Werkzeug behavior was changed in 2.1 (https://github.com/pallets/werkzeug/issues/2352)
|
|
assert response.headers["Location"] in ["http://localhost/my/uri", "/my/uri"]
|
|
|
|
|
|
def test_header_not_returned(simple_openapi_app):
|
|
app_client = simple_openapi_app.test_client()
|
|
|
|
response = app_client.post(
|
|
"/v1.0/goodday/noheader", data={}
|
|
) # type: flask.Response
|
|
assert (
|
|
response.status_code == 500
|
|
) # view_func has not returned what was promised in spec
|
|
assert response.content_type == "application/problem+json"
|
|
data = json.loads(response.data.decode("utf-8", "replace"))
|
|
assert data["type"] == "about:blank"
|
|
assert data["title"] == "Response headers do not conform to specification"
|
|
assert (
|
|
data["detail"]
|
|
== "Keys in header don't match response specification. Difference: location"
|
|
)
|
|
assert data["status"] == 500
|
|
|
|
|
|
def test_no_content_response_have_headers(simple_app):
|
|
app_client = simple_app.test_client()
|
|
resp = app_client.get("/v1.0/test-204-with-headers")
|
|
assert resp.status_code == 204
|
|
assert "X-Something" in resp.headers
|
|
|
|
|
|
def test_no_content_object_and_have_headers(simple_app):
|
|
app_client = simple_app.test_client()
|
|
resp = app_client.get("/v1.0/test-204-with-headers-nocontent-obj")
|
|
assert resp.status_code == 204
|
|
assert "X-Something" in resp.headers
|
|
|
|
|
|
def test_optional_header(simple_openapi_app):
|
|
app_client = simple_openapi_app.test_client()
|
|
resp = app_client.get("/v1.0/test-optional-headers")
|
|
assert resp.status_code == 200
|
|
assert "X-Optional-Header" not in resp.headers
|