8
8
from localstack .services .apigateway .next_gen .execute_api .gateway_response import (
9
9
AccessDeniedError ,
10
10
BaseGatewayException ,
11
+ UnauthorizedError ,
11
12
)
12
13
from localstack .services .apigateway .next_gen .execute_api .handlers import GatewayExceptionHandler
13
14
from localstack .services .apigateway .next_gen .execute_api .router import ApiGatewayEndpoint
@@ -69,27 +70,27 @@ def test_non_gateway_exception(self, get_context, apigw_response):
69
70
def test_gateway_exception (self , get_context , apigw_response ):
70
71
exception_handler = GatewayExceptionHandler ()
71
72
72
- # Create an Access Denied exception with no Gateway Response configured
73
- exception = AccessDeniedError ( "Access Denied " )
73
+ # Create an UnauthorizedError exception with no Gateway Response configured
74
+ exception = UnauthorizedError ( "Unauthorized " )
74
75
exception_handler (
75
76
chain = RestApiGatewayHandlerChain (),
76
77
exception = exception ,
77
78
context = get_context (),
78
79
response = apigw_response ,
79
80
)
80
81
81
- assert apigw_response .status_code == 403
82
- assert apigw_response .json == {"message" : "Access Denied " }
83
- assert apigw_response .headers .get ("x-amzn-errortype" ) == "AccessDeniedException "
82
+ assert apigw_response .status_code == 401
83
+ assert apigw_response .json == {"message" : "Unauthorized " }
84
+ assert apigw_response .headers .get ("x-amzn-errortype" ) == "UnauthorizedException "
84
85
85
86
def test_gateway_exception_with_default_4xx (self , get_context , apigw_response ):
86
87
exception_handler = GatewayExceptionHandler ()
87
88
88
89
# Configure DEFAULT_4XX response
89
90
gateway_responses = {GatewayResponseType .DEFAULT_4XX : GatewayResponse (statusCode = "400" )}
90
91
91
- # Create an Access Denied exception with DEFAULT_4xx configured
92
- exception = AccessDeniedError ( "Access Denied " )
92
+ # Create an UnauthorizedError exception with DEFAULT_4xx configured
93
+ exception = UnauthorizedError ( "Unauthorized " )
93
94
exception_handler (
94
95
chain = RestApiGatewayHandlerChain (),
95
96
exception = exception ,
@@ -98,24 +99,41 @@ def test_gateway_exception_with_default_4xx(self, get_context, apigw_response):
98
99
)
99
100
100
101
assert apigw_response .status_code == 400
101
- assert apigw_response .json == {"message" : "Access Denied " }
102
- assert apigw_response .headers .get ("x-amzn-errortype" ) == "AccessDeniedException "
102
+ assert apigw_response .json == {"message" : "Unauthorized " }
103
+ assert apigw_response .headers .get ("x-amzn-errortype" ) == "UnauthorizedException "
103
104
104
105
def test_gateway_exception_with_gateway_response (self , get_context , apigw_response ):
105
106
exception_handler = GatewayExceptionHandler ()
106
107
107
108
# Configure Access Denied response
108
- gateway_responses = {GatewayResponseType .ACCESS_DENIED : GatewayResponse (statusCode = "400 " )}
109
+ gateway_responses = {GatewayResponseType .UNAUTHORIZED : GatewayResponse (statusCode = "405 " )}
109
110
110
- # Create an Access Denied exception with ACCESS_DENIED configured
111
- exception = AccessDeniedError ( "Access Denied " )
111
+ # Create an UnauthorizedError exception with UNAUTHORIZED configured
112
+ exception = UnauthorizedError ( "Unauthorized " )
112
113
exception_handler (
113
114
chain = RestApiGatewayHandlerChain (),
114
115
exception = exception ,
115
116
context = get_context (gateway_responses ),
116
117
response = apigw_response ,
117
118
)
118
119
119
- assert apigw_response .status_code == 400
120
- assert apigw_response .json == {"message" : "Access Denied" }
120
+ assert apigw_response .status_code == 405
121
+ assert apigw_response .json == {"message" : "Unauthorized" }
122
+ assert apigw_response .headers .get ("x-amzn-errortype" ) == "UnauthorizedException"
123
+
124
+ def test_gateway_exception_access_denied (self , get_context , apigw_response ):
125
+ # special case where the `Message` field is capitalized
126
+ exception_handler = GatewayExceptionHandler ()
127
+
128
+ # Create an AccessDeniedError exception with no Gateway Response configured
129
+ exception = AccessDeniedError ("Access Denied" )
130
+ exception_handler (
131
+ chain = RestApiGatewayHandlerChain (),
132
+ exception = exception ,
133
+ context = get_context (),
134
+ response = apigw_response ,
135
+ )
136
+
137
+ assert apigw_response .status_code == 403
138
+ assert apigw_response .json == {"Message" : "Access Denied" }
121
139
assert apigw_response .headers .get ("x-amzn-errortype" ) == "AccessDeniedException"
0 commit comments