@@ -498,72 +498,85 @@ def custom_method():
498
498
assert headers ["Access-Control-Allow-Methods" ] == "CUSTOM"
499
499
500
500
501
- def test_service_error_response ():
502
- # GIVEN a service error response
501
+ def test_service_error_responses ():
502
+ # SCENARIO handling different kind of service errors being raised
503
503
app = ApiGatewayResolver (cors = CORSConfig ())
504
504
505
- @app .route (method = "GET" , rule = "/bad-request-error" , cors = False )
505
+ def json_dump (obj ):
506
+ return json .dumps (obj , separators = ("," , ":" ))
507
+
508
+ # GIVEN an BadRequestError
509
+ @app .get (rule = "/bad-request-error" , cors = False )
506
510
def bad_request_error ():
507
511
raise BadRequestError ("Missing required parameter" )
508
512
509
- @app .route (method = "GET" , rule = "/unauthorized-error" , cors = False )
510
- def unauthorized_error ():
511
- raise UnauthorizedError ("Unauthorized" )
512
-
513
- @app .route (method = "GET" , rule = "/service-error" , cors = True )
514
- def service_error ():
515
- raise ServiceError (403 , "Unauthorized" )
516
-
517
- @app .route (method = "GET" , rule = "/not-found-error" , cors = False )
518
- def not_found_error ():
519
- raise NotFoundError
520
-
521
- @app .route (method = "GET" , rule = "/internal-server-error" , cors = False )
522
- def internal_server_error ():
523
- raise InternalServerError ("Internal server error" )
524
-
525
513
# WHEN calling the handler
526
514
# AND path is /bad-request-error
527
515
result = app ({"path" : "/bad-request-error" , "httpMethod" : "GET" }, None )
528
516
# THEN return the bad request error response
529
517
# AND status code equals 400
530
518
assert result ["statusCode" ] == 400
531
- assert result ["body" ] == json .dumps ({"message" : "Missing required parameter" })
532
519
assert result ["headers" ]["Content-Type" ] == APPLICATION_JSON
520
+ expected = {"code" : 400 , "message" : "Missing required parameter" }
521
+ assert result ["body" ] == json_dump (expected )
522
+
523
+ # GIVEN an UnauthorizedError
524
+ @app .get (rule = "/unauthorized-error" , cors = False )
525
+ def unauthorized_error ():
526
+ raise UnauthorizedError ("Unauthorized" )
533
527
534
528
# WHEN calling the handler
535
529
# AND path is /unauthorized-error
536
530
result = app ({"path" : "/unauthorized-error" , "httpMethod" : "GET" }, None )
537
531
# THEN return the unauthorized error response
538
532
# AND status code equals 401
539
533
assert result ["statusCode" ] == 401
540
- assert result ["body" ] == json .dumps ({"message" : "Unauthorized" })
541
534
assert result ["headers" ]["Content-Type" ] == APPLICATION_JSON
535
+ expected = {"code" : 401 , "message" : "Unauthorized" }
536
+ assert result ["body" ] == json_dump (expected )
542
537
543
- # WHEN calling the handler
544
- # AND path is /service-error
545
- result = app ({"path" : "/service-error" , "httpMethod" : "GET" }, None )
546
- # THEN return the service error response
547
- # AND status code equals 403
548
- assert result ["statusCode" ] == 403
549
- assert result ["body" ] == json .dumps ({"message" : "Unauthorized" })
550
- assert result ["headers" ]["Content-Type" ] == APPLICATION_JSON
551
- assert "Access-Control-Allow-Origin" in result ["headers" ]
538
+ # GIVEN an NotFoundError
539
+ @app .get (rule = "/not-found-error" , cors = False )
540
+ def not_found_error ():
541
+ raise NotFoundError
552
542
553
543
# WHEN calling the handler
554
544
# AND path is /not-found-error
555
545
result = app ({"path" : "/not-found-error" , "httpMethod" : "GET" }, None )
556
546
# THEN return the not found error response
557
547
# AND status code equals 404
558
548
assert result ["statusCode" ] == 404
559
- assert result ["body" ] == json .dumps ({"message" : "Not found" })
560
549
assert result ["headers" ]["Content-Type" ] == APPLICATION_JSON
550
+ expected = {"code" : 404 , "message" : "Not found" }
551
+ assert result ["body" ] == json_dump (expected )
552
+
553
+ # GIVEN an InternalServerError
554
+ @app .get (rule = "/internal-server-error" , cors = False )
555
+ def internal_server_error ():
556
+ raise InternalServerError ("Internal server error" )
561
557
562
558
# WHEN calling the handler
563
559
# AND path is /internal-server-error
564
560
result = app ({"path" : "/internal-server-error" , "httpMethod" : "GET" }, None )
565
561
# THEN return the internal server error response
566
562
# AND status code equals 500
567
563
assert result ["statusCode" ] == 500
568
- assert result ["body" ] == json .dumps ({"message" : "Internal server error" })
569
564
assert result ["headers" ]["Content-Type" ] == APPLICATION_JSON
565
+ expected = {"code" : 500 , "message" : "Internal server error" }
566
+ assert result ["body" ] == json_dump (expected )
567
+
568
+ # GIVEN an ServiceError with a custom status code
569
+ @app .get (rule = "/service-error" , cors = True )
570
+ def service_error ():
571
+ raise ServiceError (502 , "Something went wrong!" )
572
+
573
+ # WHEN calling the handler
574
+ # AND path is /service-error
575
+ result = app ({"path" : "/service-error" , "httpMethod" : "GET" }, None )
576
+ # THEN return the service error response
577
+ # AND status code equals 502
578
+ assert result ["statusCode" ] == 502
579
+ assert result ["headers" ]["Content-Type" ] == APPLICATION_JSON
580
+ assert "Access-Control-Allow-Origin" in result ["headers" ]
581
+ expected = {"code" : 502 , "message" : "Something went wrong!" }
582
+ assert result ["body" ] == json_dump (expected )
0 commit comments