From c05187bd5c5bd704a4c5ff60d7e489540d54bf5f Mon Sep 17 00:00:00 2001 From: Swaraj Kumar Date: Wed, 12 Mar 2025 18:24:15 +0530 Subject: [PATCH] Problem details test case Change-Id: Ifc069d399a2c12e632b109e65a78b0533e8e7742 Signed-off-by: Swaraj Kumar --- tests/test_problemdetails.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_problemdetails.py b/tests/test_problemdetails.py index c44d5eb..dad3fa4 100644 --- a/tests/test_problemdetails.py +++ b/tests/test_problemdetails.py @@ -20,3 +20,33 @@ def test_problem_details_initialization(): assert problem.title == "Bad Request" assert problem.detail == "Invalid input data" +def test_problem_details_to_dict(): + """ + Test that ProblemDetails generates the correct dictionary representation. + """ + problem = ProblemDetails(404, "Not Found", "The requested resource does not exist.") + expected_dict = { + "title": "Not Found", + "status": 404, + "detail": "The requested resource does not exist." + } + assert problem.to_dict() == expected_dict + +def test_problem_details_to_json(test_app): + """ + Test that ProblemDetails generates the correct Flask JSON response. + """ + problem = ProblemDetails(500, "Internal Server Error", "Something went wrong") + with Flask(__name__).test_request_context(): + response, status, headers = problem.to_json() + assert status == 500 + assert headers["Content-Type"] == "application/problem+json" + # Convert response data to JSON and compare + response_data = json.loads(response.get_data(as_text=True)) + expected_json = { + "title": "Internal Server Error", + "status": 500, + "detail": "Something went wrong" + } + assert response_data == expected_json + -- 2.16.6