Skip to content

Commit

Permalink
Update error handler test
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeSneyders committed Nov 5, 2023
1 parent 070374f commit c14d127
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions tests/api/test_bootstrap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from unittest import mock

import jinja2
Expand All @@ -7,6 +8,7 @@
from connexion.exceptions import InvalidSpecification
from connexion.http_facts import METHODS
from connexion.json_schema import ExtendedSafeLoader
from connexion.lifecycle import ConnexionRequest, ConnexionResponse
from connexion.middleware.abstract import AbstractRoutingAPI
from connexion.options import SwaggerUIOptions

Expand Down Expand Up @@ -302,10 +304,16 @@ def test_add_error_handler(app_class, simple_api_spec_dir):
app = app_class(__name__, specification_dir=simple_api_spec_dir)
app.add_api("openapi.yaml")

def custom_error_handler(_request, _exception):
pass
def not_found(request: ConnexionRequest, exc: Exception) -> ConnexionResponse:
return ConnexionResponse(
status_code=404, body=json.dumps({"error": "NotFound"})
)

app.add_error_handler(Exception, custom_error_handler)
app.add_error_handler(500, custom_error_handler)
app.add_error_handler(Exception, not_found)
app.add_error_handler(404, not_found)

app.middleware._build_middleware_stack()
app_client = app.test_client()

response = app_client.get("/does_not_exist")
assert response.status_code == 404
assert response.json()["error"] == "NotFound"

0 comments on commit c14d127

Please sign in to comment.